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 "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"
16 #if HAVE_KUSERFEEDBACK
17 #include "userfeedback/dolphinfeedbackprovider.h"
23 #include <KDBusService>
24 #include <KIO/PreviewJob>
26 #include <KLocalizedString>
27 #include <KWindowSystem>
29 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
30 #include <Kdelibs4ConfigMigrator>
33 #define HAVE_STYLE_MANAGER __has_include(<KStyleManager>)
34 #if HAVE_STYLE_MANAGER
35 #include <KStyleManager>
38 #include <QApplication>
39 #include <QCommandLineParser>
40 #include <QDBusConnection>
41 #include <QDBusConnectionInterface>
42 #include <QSessionManager>
45 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
46 #include <private/qtx11extras_p.h>
57 int main(int argc
, char **argv
)
60 // Prohibit using sudo or kdesu (but allow using the root user directly)
62 if (!qEnvironmentVariableIsEmpty("SUDO_USER")) {
63 std::cout
<< "Running Dolphin with sudo 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 "
68 } else if (!qEnvironmentVariableIsEmpty("KDESU_USER")) {
69 std::cout
<< "Running Dolphin with kdesu is not supported as it can cause bugs and expose you to security vulnerabilities. Instead, install the "
70 "`kio-admin` package from your distro and use it to manage root-owned locations by right-clicking on them and selecting \"Open as "
79 * trigger initialisation of proper icon theme
81 #if KICONTHEMES_VERSION >= QT_VERSION_CHECK(6, 3, 0)
82 KIconTheme::initTheme();
86 * enable high dpi support
88 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
89 QCoreApplication::setAttribute(Qt::AA_UseHighDpiPixmaps
, true);
90 QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling
, true);
92 QApplication
app(argc
, argv
);
93 app
.setWindowIcon(QIcon::fromTheme(QStringLiteral("system-file-manager"), app
.windowIcon()));
95 #if HAVE_STYLE_MANAGER
97 * trigger initialisation of proper application style
99 KStyleManager::initStyle();
102 * For Windows and macOS: use Breeze if available
103 * Of all tested styles that works the best for us
105 #if defined(Q_OS_MACOS) || defined(Q_OS_WIN)
106 QApplication::setStyle(QStringLiteral("breeze"));
110 KCrash::initialize();
112 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
113 Kdelibs4ConfigMigrator
migrate(QStringLiteral("dolphin"));
114 migrate
.setConfigFiles(QStringList() << QStringLiteral("dolphinrc"));
115 migrate
.setUiFiles(QStringList() << QStringLiteral("dolphinpart.rc") << QStringLiteral("dolphinui.rc"));
119 KLocalizedString::setApplicationDomain("dolphin");
121 KAboutData
aboutData(QStringLiteral("dolphin"),
123 QStringLiteral(DOLPHIN_VERSION_STRING
),
124 i18nc("@title", "File Manager"),
126 i18nc("@info:credit", "(C) 2006-2022 The Dolphin Developers"));
127 aboutData
.setHomepage(QStringLiteral("https://apps.kde.org/dolphin"));
128 aboutData
.addAuthor(i18nc("@info:credit", "Felix Ernst"),
129 i18nc("@info:credit", "Maintainer (since 2021) and developer"),
130 QStringLiteral("felixernst@kde.org"));
131 aboutData
.addAuthor(i18nc("@info:credit", "Méven Car"),
132 i18nc("@info:credit", "Maintainer (since 2021) and developer (since 2019)"),
133 QStringLiteral("meven@kde.org"));
134 aboutData
.addAuthor(i18nc("@info:credit", "Elvis Angelaccio"),
135 i18nc("@info:credit", "Maintainer (2018-2021) and developer"),
136 QStringLiteral("elvis.angelaccio@kde.org"));
137 aboutData
.addAuthor(i18nc("@info:credit", "Emmanuel Pescosta"),
138 i18nc("@info:credit", "Maintainer (2014-2018) and developer"),
139 QStringLiteral("emmanuelpescosta099@gmail.com"));
140 aboutData
.addAuthor(i18nc("@info:credit", "Frank Reininghaus"),
141 i18nc("@info:credit", "Maintainer (2012-2014) and developer"),
142 QStringLiteral("frank78ac@googlemail.com"));
143 aboutData
.addAuthor(i18nc("@info:credit", "Peter Penz"),
144 i18nc("@info:credit", "Maintainer and developer (2006-2012)"),
145 QStringLiteral("peter.penz19@gmail.com"));
146 aboutData
.addAuthor(i18nc("@info:credit", "Sebastian Trüg"), i18nc("@info:credit", "Developer"), QStringLiteral("trueg@kde.org"));
147 aboutData
.addAuthor(i18nc("@info:credit", "David Faure"), i18nc("@info:credit", "Developer"), QStringLiteral("faure@kde.org"));
148 aboutData
.addAuthor(i18nc("@info:credit", "Aaron J. Seigo"), i18nc("@info:credit", "Developer"), QStringLiteral("aseigo@kde.org"));
149 aboutData
.addAuthor(i18nc("@info:credit", "Rafael Fernández López"), i18nc("@info:credit", "Developer"), QStringLiteral("ereslibre@kde.org"));
150 aboutData
.addAuthor(i18nc("@info:credit", "Kevin Ottens"), i18nc("@info:credit", "Developer"), QStringLiteral("ervin@kde.org"));
151 aboutData
.addAuthor(i18nc("@info:credit", "Holger Freyther"), i18nc("@info:credit", "Developer"), QStringLiteral("freyther@gmx.net"));
152 aboutData
.addAuthor(i18nc("@info:credit", "Max Blazejak"), i18nc("@info:credit", "Developer"), QStringLiteral("m43ksrocks@gmail.com"));
153 aboutData
.addAuthor(i18nc("@info:credit", "Michael Austin"), i18nc("@info:credit", "Documentation"), QStringLiteral("tuxedup@users.sourceforge.net"));
155 KAboutData::setApplicationData(aboutData
);
157 QCommandLineParser parser
;
158 aboutData
.setupCommandLine(&parser
);
160 // command line options
161 parser
.addOption(QCommandLineOption(QStringList() << QStringLiteral("select"),
163 "The files and folders passed as arguments "
164 "will be selected.")));
165 parser
.addOption(QCommandLineOption(QStringList() << QStringLiteral("split"), i18nc("@info:shell", "Dolphin will get started with a split view.")));
166 parser
.addOption(QCommandLineOption(QStringList() << QStringLiteral("new-window"), i18nc("@info:shell", "Dolphin will explicitly open in a new window.")));
168 QCommandLineOption(QStringList() << QStringLiteral("daemon"), i18nc("@info:shell", "Start Dolphin Daemon (only required for DBus Interface).")));
169 parser
.addPositionalArgument(QStringLiteral("+[Url]"), i18nc("@info:shell", "Document to open"));
172 aboutData
.processCommandLine(&parser
);
174 const bool splitView
= parser
.isSet(QStringLiteral("split")) || GeneralSettings::splitView();
175 const bool openFiles
= parser
.isSet(QStringLiteral("select"));
176 const QStringList args
= parser
.positionalArguments();
177 QList
<QUrl
> urls
= Dolphin::validateUris(args
);
178 // We later mutate urls, so we need to store if it was empty originally
179 const bool startedWithURLs
= !urls
.isEmpty();
181 if (parser
.isSet(QStringLiteral("daemon"))) {
182 // Disable session management for the daemonized version
183 // See https://bugs.kde.org/show_bug.cgi?id=417219
184 auto disableSessionManagement
= [](QSessionManager
&sm
) {
185 sm
.setRestartHint(QSessionManager::RestartNever
);
187 QObject::connect(&app
, &QGuiApplication::commitDataRequest
, disableSessionManagement
);
188 QObject::connect(&app
, &QGuiApplication::saveStateRequest
, disableSessionManagement
);
191 KDBusService
dolphinDBusService(KDBusService::NoExitOnFailure
);
193 KDBusService dolphinDBusService
;
195 DBusInterface interface
;
196 interface
.setAsDaemon();
200 if (!parser
.isSet(QStringLiteral("new-window"))) {
202 if (KWindowSystem::isPlatformWayland()) {
203 token
= qEnvironmentVariable("XDG_ACTIVATION_TOKEN");
204 qunsetenv("XDG_ACTIVATION_TOKEN");
205 } else if (KWindowSystem::isPlatformX11()) {
207 token
= QX11Info::nextStartupId();
211 if (Dolphin::attachToExistingInstance(urls
, openFiles
, splitView
, QString(), token
)) {
212 // Successfully attached to existing instance of Dolphin
217 if (!startedWithURLs
) {
218 // We need at least one URL to open Dolphin
219 urls
.append(Dolphin::homeUrl());
222 if (splitView
&& urls
.size() < 2) {
223 // Split view does only make sense if we have at least 2 URLs
224 urls
.append(urls
.last());
227 DolphinMainWindow
*mainWindow
= new DolphinMainWindow();
230 mainWindow
->openFiles(urls
, splitView
);
232 mainWindow
->openDirectories(urls
, splitView
);
237 // Allow starting Dolphin on a system that is not running DBus:
238 KDBusService::StartupOptions serviceOptions
= KDBusService::Multiple
;
239 if (!QDBusConnection::sessionBus().isConnected()) {
240 serviceOptions
|= KDBusService::NoExitOnFailure
;
242 KDBusService
dolphinDBusService(serviceOptions
);
243 DBusInterface interface
;
245 if (!app
.isSessionRestored()) {
246 KConfigGui::setSessionConfig(QStringLiteral("dolphin"), QStringLiteral("dolphin"));
249 // Only restore session if:
250 // 1. Not explicitly opening a new instance
251 // 2. The "remember state" setting is enabled or session restoration after
253 // 3. There is a session available to restore
254 if (!parser
.isSet(QStringLiteral("new-window")) && (app
.isSessionRestored() || GeneralSettings::rememberOpenedTabs())) {
255 // Get saved state data for the last-closed Dolphin instance
256 const QString serviceName
= QStringLiteral("org.kde.dolphin-%1").arg(QCoreApplication::applicationPid());
257 if (Dolphin::dolphinGuiInstances(serviceName
).size() > 0) {
258 const QString className
= KXmlGuiWindow::classNameOfToplevel(1);
259 if (className
== QLatin1String("DolphinMainWindow")) {
260 mainWindow
->restore(1);
261 // If the user passed any URLs to Dolphin, open those in the
262 // window after session-restoring it
263 if (startedWithURLs
) {
265 mainWindow
->openFiles(urls
, splitView
);
267 mainWindow
->openDirectories(urls
, splitView
);
271 qCWarning(DolphinDebug
) << "Unknown class " << className
<< " in session saved data!";
276 mainWindow
->setSessionAutoSaveEnabled(GeneralSettings::rememberOpenedTabs());
278 #if HAVE_KUSERFEEDBACK
279 auto feedbackProvider
= DolphinFeedbackProvider::instance();
280 Q_UNUSED(feedbackProvider
)
283 return app
.exec(); // krazy:exclude=crash;