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>
6 * SPDX-License-Identifier: GPL-2.0-or-later
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"
17 #if HAVE_KUSERFEEDBACK
18 #include "userfeedback/dolphinfeedbackprovider.h"
24 #include <KDBusService>
25 #include <KIO/PreviewJob>
27 #include <KLocalizedString>
28 #include <KWindowSystem>
30 #define HAVE_STYLE_MANAGER __has_include(<KStyleManager>)
31 #if HAVE_STYLE_MANAGER
32 #include <KStyleManager>
35 #include <QApplication>
36 #include <QCommandLineParser>
37 #include <QDBusConnection>
38 #include <QDBusConnectionInterface>
39 #include <QSessionManager>
42 #include <private/qtx11extras_p.h>
50 constexpr auto dolphinTranslationDomain
{"dolphin"};
52 int main(int argc
, char **argv
)
55 // Prohibit using sudo or kdesu (but allow using the root user directly)
56 if (getuid() == 0 && (!qEnvironmentVariableIsEmpty("SUDO_USER") || !qEnvironmentVariableIsEmpty("KDESU_USER"))) {
57 QCoreApplication
app(argc
, argv
); // Needed for the xi18ndc() call below.
58 std::cout
<< qPrintable(
59 xi18ndc(dolphinTranslationDomain
,
60 "@info:shell %1 is a terminal command",
61 "Running <application>Dolphin</application> with <command>sudo</command> is discouraged. Please run <icode>%1</icode> instead.",
62 QStringLiteral("dolphin --sudo")))
64 // 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.
70 * trigger initialisation of proper icon theme
72 KIconTheme::initTheme();
74 QApplication
app(argc
, argv
);
75 app
.setWindowIcon(QIcon::fromTheme(QStringLiteral("org.kde.dolphin"), app
.windowIcon()));
77 #if HAVE_STYLE_MANAGER
79 * trigger initialisation of proper application style
81 KStyleManager::initStyle();
84 * For Windows and macOS: use Breeze if available
85 * Of all tested styles that works the best for us
87 #if defined(Q_OS_MACOS) || defined(Q_OS_WIN)
88 QApplication::setStyle(QStringLiteral("breeze"));
92 KLocalizedString::setApplicationDomain(dolphinTranslationDomain
);
94 KAboutData
aboutData(QStringLiteral("dolphin"),
96 QStringLiteral(DOLPHIN_VERSION_STRING
),
97 i18nc("@title", "File Manager"),
99 i18nc("@info:credit", "(C) 2006-2025 The Dolphin Developers"));
100 aboutData
.setHomepage(QStringLiteral("https://apps.kde.org/dolphin"));
101 aboutData
.addAuthor(i18nc("@info:credit", "Felix Ernst"),
102 i18nc("@info:credit", "Maintainer (since 2021) and developer"),
103 QStringLiteral("felixernst@kde.org"));
104 aboutData
.addAuthor(i18nc("@info:credit", "Méven Car"),
105 i18nc("@info:credit", "Maintainer (since 2021) and developer (since 2019)"),
106 QStringLiteral("meven@kde.org"));
107 aboutData
.addAuthor(i18nc("@info:credit", "Elvis Angelaccio"),
108 i18nc("@info:credit", "Maintainer (2018-2021) and developer"),
109 QStringLiteral("elvis.angelaccio@kde.org"));
110 aboutData
.addAuthor(i18nc("@info:credit", "Emmanuel Pescosta"),
111 i18nc("@info:credit", "Maintainer (2014-2018) and developer"),
112 QStringLiteral("emmanuelpescosta099@gmail.com"));
113 aboutData
.addAuthor(i18nc("@info:credit", "Frank Reininghaus"),
114 i18nc("@info:credit", "Maintainer (2012-2014) and developer"),
115 QStringLiteral("frank78ac@googlemail.com"));
116 aboutData
.addAuthor(i18nc("@info:credit", "Peter Penz"),
117 i18nc("@info:credit", "Maintainer and developer (2006-2012)"),
118 QStringLiteral("peter.penz19@gmail.com"));
119 aboutData
.addAuthor(i18nc("@info:credit", "Sebastian Trüg"), i18nc("@info:credit", "Developer"), QStringLiteral("trueg@kde.org"));
120 aboutData
.addAuthor(i18nc("@info:credit", "David Faure"), i18nc("@info:credit", "Developer"), QStringLiteral("faure@kde.org"));
121 aboutData
.addAuthor(i18nc("@info:credit", "Aaron J. Seigo"), i18nc("@info:credit", "Developer"), QStringLiteral("aseigo@kde.org"));
122 aboutData
.addAuthor(i18nc("@info:credit", "Rafael Fernández López"), i18nc("@info:credit", "Developer"), QStringLiteral("ereslibre@kde.org"));
123 aboutData
.addAuthor(i18nc("@info:credit", "Kevin Ottens"), i18nc("@info:credit", "Developer"), QStringLiteral("ervin@kde.org"));
124 aboutData
.addAuthor(i18nc("@info:credit", "Holger Freyther"), i18nc("@info:credit", "Developer"), QStringLiteral("freyther@gmx.net"));
125 aboutData
.addAuthor(i18nc("@info:credit", "Max Blazejak"), i18nc("@info:credit", "Developer"), QStringLiteral("m43ksrocks@gmail.com"));
126 aboutData
.addAuthor(i18nc("@info:credit", "Michael Austin"), i18nc("@info:credit", "Documentation"), QStringLiteral("tuxedup@users.sourceforge.net"));
128 KAboutData::setApplicationData(aboutData
);
129 KCrash::initialize();
131 QCommandLineParser parser
;
132 aboutData
.setupCommandLine(&parser
);
134 // command line options
135 parser
.addOption(QCommandLineOption(QStringList() << QStringLiteral("select"),
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(QCommandLineOption(QStringList() << QStringLiteral("sudo") << QStringLiteral("admin"),
142 i18nc("@info:shell", "Set up Dolphin for administrative tasks.")));
144 QCommandLineOption(QStringList() << QStringLiteral("daemon"), i18nc("@info:shell", "Start Dolphin Daemon (only required for DBus Interface).")));
145 parser
.addPositionalArgument(QStringLiteral("+[Url]"), i18nc("@info:shell", "Document to open"));
148 aboutData
.processCommandLine(&parser
);
150 const bool splitView
= parser
.isSet(QStringLiteral("split")) || GeneralSettings::splitView();
151 const bool openFiles
= parser
.isSet(QStringLiteral("select"));
152 const bool adminWorkerInfoWanted
= parser
.isSet(QStringLiteral("sudo")) || parser
.isSet(QStringLiteral("admin"));
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();
158 if (adminWorkerInfoWanted
|| std::any_of(urls
.cbegin(), urls
.cend(), [](const QUrl
&url
) {
159 return url
.scheme() == QStringLiteral("admin");
161 Admin::guideUserTowardsInstallingAdminWorker();
164 if (parser
.isSet(QStringLiteral("daemon"))) {
165 // Prevent KApplicationLauncherJob from cause the application to quit on job finish.
166 QCoreApplication::setQuitLockEnabled(false);
168 // Disable session management for the daemonized version
169 // See https://bugs.kde.org/show_bug.cgi?id=417219
170 auto disableSessionManagement
= [](QSessionManager
&sm
) {
171 sm
.setRestartHint(QSessionManager::RestartNever
);
173 QObject::connect(&app
, &QGuiApplication::commitDataRequest
, disableSessionManagement
);
174 QObject::connect(&app
, &QGuiApplication::saveStateRequest
, disableSessionManagement
);
177 KDBusService
dolphinDBusService(KDBusService::NoExitOnFailure
);
179 KDBusService dolphinDBusService
;
181 DBusInterface interface
;
182 interface
.setAsDaemon();
186 if (!parser
.isSet(QStringLiteral("new-window"))) {
188 if (KWindowSystem::isPlatformWayland()) {
189 token
= qEnvironmentVariable("XDG_ACTIVATION_TOKEN");
190 qunsetenv("XDG_ACTIVATION_TOKEN");
191 } else if (KWindowSystem::isPlatformX11()) {
193 token
= QX11Info::nextStartupId();
197 if (Dolphin::attachToExistingInstance(urls
, openFiles
, splitView
, QString(), token
)) {
198 // Successfully attached to existing instance of Dolphin
203 if (!startedWithURLs
) {
204 // We need at least one URL to open Dolphin
205 urls
.append(Dolphin::homeUrl());
208 if (splitView
&& urls
.size() < 2) {
209 // Split view does only make sense if we have at least 2 URLs
210 urls
.append(urls
.last());
213 DolphinMainWindow
*mainWindow
= new DolphinMainWindow();
216 mainWindow
->openFiles(urls
, splitView
);
218 mainWindow
->openDirectories(urls
, splitView
);
223 // Allow starting Dolphin on a system that is not running DBus:
224 KDBusService::StartupOptions serviceOptions
= KDBusService::Multiple
;
225 if (!QDBusConnection::sessionBus().isConnected()) {
226 serviceOptions
|= KDBusService::NoExitOnFailure
;
228 KDBusService
dolphinDBusService(serviceOptions
);
229 DBusInterface interface
;
231 if (!app
.isSessionRestored()) {
232 KConfigGui::setSessionConfig(QStringLiteral("dolphin"), QStringLiteral("dolphin"));
235 // Only restore session if:
236 // 1. Not explicitly opening a new instance
237 // 2. The "remember state" setting is enabled or session restoration after
239 // 3. There is a session available to restore
240 if (!parser
.isSet(QStringLiteral("new-window")) && (app
.isSessionRestored() || GeneralSettings::rememberOpenedTabs())) {
241 // Get saved state data for the last-closed Dolphin instance
242 const QString serviceName
= QStringLiteral("org.kde.dolphin-%1").arg(QCoreApplication::applicationPid());
243 if (Dolphin::dolphinGuiInstances(serviceName
).size() > 0) {
244 const QString className
= KXmlGuiWindow::classNameOfToplevel(1);
245 if (className
== QLatin1String("DolphinMainWindow")) {
246 mainWindow
->restore(1);
247 // If the user passed any URLs to Dolphin, open those in the
248 // window after session-restoring it
249 if (startedWithURLs
) {
251 mainWindow
->openFiles(urls
, splitView
);
253 mainWindow
->openDirectories(urls
, splitView
);
257 qCWarning(DolphinDebug
) << "Unknown class " << className
<< " in session saved data!";
262 mainWindow
->setSessionAutoSaveEnabled(GeneralSettings::rememberOpenedTabs());
264 if (adminWorkerInfoWanted
) {
265 Admin::guideUserTowardsUsingAdminWorker();
268 #if HAVE_KUSERFEEDBACK
269 auto feedbackProvider
= DolphinFeedbackProvider::instance();
270 Q_UNUSED(feedbackProvider
)
273 return app
.exec(); // krazy:exclude=crash;