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