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