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