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