]> cloud.milkyroute.net Git - dolphin.git/blob - src/main.cpp
Create thumbnails with scene devicePixelRatio
[dolphin.git] / src / main.cpp
1 /*
2 * SPDX-FileCopyrightText: 2006 Peter Penz <peter.penz19@gmail.com>
3 * SPDX-FileCopyrightText: 2006 Stefan Monov <logixoul@gmail.com>
4 * SPDX-FileCopyrightText: 2015 Mathieu Tarral <mathieu.tarral@gmail.com>
5 *
6 * SPDX-License-Identifier: GPL-2.0-or-later
7 */
8
9 #include "config-dolphin.h"
10 #include "dbusinterface.h"
11 #include "dolphin_generalsettings.h"
12 #include "dolphin_version.h"
13 #include "dolphindebug.h"
14 #include "dolphinmainwindow.h"
15 #include "global.h"
16 #if HAVE_KUSERFEEDBACK
17 #include "userfeedback/dolphinfeedbackprovider.h"
18 #endif
19
20 #include <KAboutData>
21 #include <KConfigGui>
22 #include <KCrash>
23 #include <KDBusService>
24 #include <KIO/PreviewJob>
25 #include <KLocalizedString>
26 #include <KWindowSystem>
27
28 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
29 #include <Kdelibs4ConfigMigrator>
30 #endif
31
32 #include <QApplication>
33 #include <QCommandLineParser>
34 #include <QDBusConnection>
35 #include <QDBusConnectionInterface>
36 #include <QSessionManager>
37
38 #if HAVE_X11
39 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
40 #include <private/qtx11extras_p.h>
41 #else
42 #include <QX11Info>
43 #endif
44 #endif
45
46 #ifndef Q_OS_WIN
47 #include <unistd.h>
48 #endif
49 #include <iostream>
50
51 int main(int argc, char **argv)
52 {
53 #ifndef Q_OS_WIN
54 // Prohibit using sudo or kdesu (but allow using the root user directly)
55 if (getuid() == 0) {
56 if (!qEnvironmentVariableIsEmpty("SUDO_USER")) {
57 std::cout << "Running Dolphin with sudo is not supported as it can cause bugs and expose you to security vulnerabilities. Instead, install the "
58 "`kio-admin` package from your distro and use it to manage root-owned locations by right-clicking on them and selecting \"Open as "
59 "Administrator\"."
60 << std::endl;
61 return EXIT_FAILURE;
62 } else if (!qEnvironmentVariableIsEmpty("KDESU_USER")) {
63 std::cout << "Running Dolphin with kdesu is not supported as it can cause bugs and expose you to security vulnerabilities. Instead, install the "
64 "`kio-admin` package from your distro and use it to manage root-owned locations by right-clicking on them and selecting \"Open as "
65 "Administrator\"."
66 << std::endl;
67 return EXIT_FAILURE;
68 }
69 }
70 #endif
71
72 /**
73 * enable high dpi support
74 */
75 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
76 QCoreApplication::setAttribute(Qt::AA_UseHighDpiPixmaps, true);
77 QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling, true);
78 #endif
79 QApplication app(argc, argv);
80 app.setWindowIcon(QIcon::fromTheme(QStringLiteral("system-file-manager"), app.windowIcon()));
81
82 KCrash::initialize();
83
84 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
85 Kdelibs4ConfigMigrator migrate(QStringLiteral("dolphin"));
86 migrate.setConfigFiles(QStringList() << QStringLiteral("dolphinrc"));
87 migrate.setUiFiles(QStringList() << QStringLiteral("dolphinpart.rc") << QStringLiteral("dolphinui.rc"));
88 migrate.migrate();
89 #endif
90
91 KLocalizedString::setApplicationDomain("dolphin");
92
93 KAboutData aboutData(QStringLiteral("dolphin"),
94 i18n("Dolphin"),
95 QStringLiteral(DOLPHIN_VERSION_STRING),
96 i18nc("@title", "File Manager"),
97 KAboutLicense::GPL,
98 i18nc("@info:credit", "(C) 2006-2022 The Dolphin Developers"));
99 aboutData.setHomepage(QStringLiteral("https://kde.org/applications/system/org.kde.dolphin"));
100 aboutData.addAuthor(i18nc("@info:credit", "Felix Ernst"),
101 i18nc("@info:credit", "Maintainer (since 2021) and developer"),
102 QStringLiteral("felixernst@kde.org"));
103 aboutData.addAuthor(i18nc("@info:credit", "Méven Car"),
104 i18nc("@info:credit", "Maintainer (since 2021) and developer (since 2019)"),
105 QStringLiteral("meven@kde.org"));
106 aboutData.addAuthor(i18nc("@info:credit", "Elvis Angelaccio"),
107 i18nc("@info:credit", "Maintainer (2018-2021) and developer"),
108 QStringLiteral("elvis.angelaccio@kde.org"));
109 aboutData.addAuthor(i18nc("@info:credit", "Emmanuel Pescosta"),
110 i18nc("@info:credit", "Maintainer (2014-2018) and developer"),
111 QStringLiteral("emmanuelpescosta099@gmail.com"));
112 aboutData.addAuthor(i18nc("@info:credit", "Frank Reininghaus"),
113 i18nc("@info:credit", "Maintainer (2012-2014) and developer"),
114 QStringLiteral("frank78ac@googlemail.com"));
115 aboutData.addAuthor(i18nc("@info:credit", "Peter Penz"),
116 i18nc("@info:credit", "Maintainer and developer (2006-2012)"),
117 QStringLiteral("peter.penz19@gmail.com"));
118 aboutData.addAuthor(i18nc("@info:credit", "Sebastian Trüg"), i18nc("@info:credit", "Developer"), QStringLiteral("trueg@kde.org"));
119 aboutData.addAuthor(i18nc("@info:credit", "David Faure"), i18nc("@info:credit", "Developer"), QStringLiteral("faure@kde.org"));
120 aboutData.addAuthor(i18nc("@info:credit", "Aaron J. Seigo"), i18nc("@info:credit", "Developer"), QStringLiteral("aseigo@kde.org"));
121 aboutData.addAuthor(i18nc("@info:credit", "Rafael Fernández López"), i18nc("@info:credit", "Developer"), QStringLiteral("ereslibre@kde.org"));
122 aboutData.addAuthor(i18nc("@info:credit", "Kevin Ottens"), i18nc("@info:credit", "Developer"), QStringLiteral("ervin@kde.org"));
123 aboutData.addAuthor(i18nc("@info:credit", "Holger Freyther"), i18nc("@info:credit", "Developer"), QStringLiteral("freyther@gmx.net"));
124 aboutData.addAuthor(i18nc("@info:credit", "Max Blazejak"), i18nc("@info:credit", "Developer"), QStringLiteral("m43ksrocks@gmail.com"));
125 aboutData.addAuthor(i18nc("@info:credit", "Michael Austin"), i18nc("@info:credit", "Documentation"), QStringLiteral("tuxedup@users.sourceforge.net"));
126
127 KAboutData::setApplicationData(aboutData);
128
129 QCommandLineParser parser;
130 aboutData.setupCommandLine(&parser);
131
132 // command line options
133 parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("select"),
134 i18nc("@info:shell",
135 "The files and folders passed as arguments "
136 "will be selected.")));
137 parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("split"), i18nc("@info:shell", "Dolphin will get started with a split view.")));
138 parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("new-window"), i18nc("@info:shell", "Dolphin will explicitly open in a new window.")));
139 parser.addOption(
140 QCommandLineOption(QStringList() << QStringLiteral("daemon"), i18nc("@info:shell", "Start Dolphin Daemon (only required for DBus Interface).")));
141 parser.addPositionalArgument(QStringLiteral("+[Url]"), i18nc("@info:shell", "Document to open"));
142
143 parser.process(app);
144 aboutData.processCommandLine(&parser);
145
146 const bool splitView = parser.isSet(QStringLiteral("split")) || GeneralSettings::splitView();
147 const bool openFiles = parser.isSet(QStringLiteral("select"));
148 const QStringList args = parser.positionalArguments();
149 QList<QUrl> urls = Dolphin::validateUris(args);
150 // We later mutate urls, so we need to store if it was empty originally
151 const bool startedWithURLs = !urls.isEmpty();
152
153 if (parser.isSet(QStringLiteral("daemon"))) {
154 // Disable session management for the daemonized version
155 // See https://bugs.kde.org/show_bug.cgi?id=417219
156 auto disableSessionManagement = [](QSessionManager &sm) {
157 sm.setRestartHint(QSessionManager::RestartNever);
158 };
159 QObject::connect(&app, &QGuiApplication::commitDataRequest, disableSessionManagement);
160 QObject::connect(&app, &QGuiApplication::saveStateRequest, disableSessionManagement);
161
162 #ifdef FLATPAK
163 KDBusService dolphinDBusService(KDBusService::NoExitOnFailure);
164 #else
165 KDBusService dolphinDBusService;
166 #endif
167 DBusInterface interface;
168 interface.setAsDaemon();
169 return app.exec();
170 }
171
172 if (!parser.isSet(QStringLiteral("new-window"))) {
173 QString token;
174 if (KWindowSystem::isPlatformWayland()) {
175 token = qEnvironmentVariable("XDG_ACTIVATION_TOKEN");
176 qunsetenv("XDG_ACTIVATION_TOKEN");
177 } else if (KWindowSystem::isPlatformX11()) {
178 #if HAVE_X11
179 token = QX11Info::nextStartupId();
180 #endif
181 }
182
183 if (Dolphin::attachToExistingInstance(urls, openFiles, splitView, QString(), token)) {
184 // Successfully attached to existing instance of Dolphin
185 return 0;
186 }
187 }
188
189 if (!startedWithURLs) {
190 // We need at least one URL to open Dolphin
191 urls.append(Dolphin::homeUrl());
192 }
193
194 if (splitView && urls.size() < 2) {
195 // Split view does only make sense if we have at least 2 URLs
196 urls.append(urls.last());
197 }
198
199 DolphinMainWindow *mainWindow = new DolphinMainWindow();
200
201 if (openFiles) {
202 mainWindow->openFiles(urls, splitView);
203 } else {
204 mainWindow->openDirectories(urls, splitView);
205 }
206
207 mainWindow->show();
208
209 // Allow starting Dolphin on a system that is not running DBus:
210 KDBusService::StartupOptions serviceOptions = KDBusService::Multiple;
211 if (!QDBusConnection::sessionBus().isConnected()) {
212 serviceOptions |= KDBusService::NoExitOnFailure;
213 }
214 KDBusService dolphinDBusService(serviceOptions);
215 DBusInterface interface;
216
217 if (!app.isSessionRestored()) {
218 KConfigGui::setSessionConfig(QStringLiteral("dolphin"), QStringLiteral("dolphin"));
219 }
220
221 // Only restore session if:
222 // 1. Not explicitly opening a new instance
223 // 2. The "remember state" setting is enabled or session restoration after
224 // reboot is in use
225 // 3. There is a session available to restore
226 if (!parser.isSet(QStringLiteral("new-window")) && (app.isSessionRestored() || GeneralSettings::rememberOpenedTabs())) {
227 // Get saved state data for the last-closed Dolphin instance
228 const QString serviceName = QStringLiteral("org.kde.dolphin-%1").arg(QCoreApplication::applicationPid());
229 if (Dolphin::dolphinGuiInstances(serviceName).size() > 0) {
230 const QString className = KXmlGuiWindow::classNameOfToplevel(1);
231 if (className == QLatin1String("DolphinMainWindow")) {
232 mainWindow->restore(1);
233 // If the user passed any URLs to Dolphin, open those in the
234 // window after session-restoring it
235 if (startedWithURLs) {
236 if (openFiles) {
237 mainWindow->openFiles(urls, splitView);
238 } else {
239 mainWindow->openDirectories(urls, splitView);
240 }
241 }
242 } else {
243 qCWarning(DolphinDebug) << "Unknown class " << className << " in session saved data!";
244 }
245 }
246 }
247
248 #if HAVE_KUSERFEEDBACK
249 auto feedbackProvider = DolphinFeedbackProvider::instance();
250 Q_UNUSED(feedbackProvider)
251 #endif
252
253 return app.exec(); // krazy:exclude=crash;
254 }