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