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