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