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