]> cloud.milkyroute.net Git - dolphin.git/blob - src/main.cpp
Remove unused includes
[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
27 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
28 #include <Kdelibs4ConfigMigrator>
29 #endif
30
31 #include <QApplication>
32 #include <QCommandLineParser>
33 #include <QDBusConnection>
34 #include <QDBusConnectionInterface>
35 #include <QSessionManager>
36
37 #ifndef Q_OS_WIN
38 #include <unistd.h>
39 #endif
40 #include <iostream>
41
42 int main(int argc, char **argv)
43 {
44 #ifndef Q_OS_WIN
45 // Prohibit using sudo or kdesu (but allow using the root user directly)
46 if (getuid() == 0) {
47 if (!qEnvironmentVariableIsEmpty("SUDO_USER")) {
48 std::cout << "Running Dolphin with sudo can cause bugs and expose you to security vulnerabilities. "
49 "Instead use Dolphin normally and you will be prompted for elevated privileges when "
50 "performing file operations that require them."
51 << std::endl;
52 return EXIT_FAILURE;
53 } else if (!qEnvironmentVariableIsEmpty("KDESU_USER")) {
54 std::cout << "Running Dolphin with kdesu can cause bugs and expose you to security vulnerabilities. "
55 "Instead use Dolphin normally and you will be prompted for elevated privileges when "
56 "performing file operations that require them."
57 << std::endl;
58 return EXIT_FAILURE;
59 }
60 }
61 #endif
62
63 /**
64 * enable high dpi support
65 */
66 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
67 QCoreApplication::setAttribute(Qt::AA_UseHighDpiPixmaps, true);
68 QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling, true);
69 #endif
70 QApplication app(argc, argv);
71 app.setWindowIcon(QIcon::fromTheme(QStringLiteral("system-file-manager"), app.windowIcon()));
72
73 KIO::PreviewJob::setDefaultDevicePixelRatio(app.devicePixelRatio());
74
75 KCrash::initialize();
76
77 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
78 Kdelibs4ConfigMigrator migrate(QStringLiteral("dolphin"));
79 migrate.setConfigFiles(QStringList() << QStringLiteral("dolphinrc"));
80 migrate.setUiFiles(QStringList() << QStringLiteral("dolphinpart.rc") << QStringLiteral("dolphinui.rc"));
81 migrate.migrate();
82 #endif
83
84 KLocalizedString::setApplicationDomain("dolphin");
85
86 KAboutData aboutData(QStringLiteral("dolphin"), i18n("Dolphin"), QStringLiteral(DOLPHIN_VERSION_STRING),
87 i18nc("@title", "File Manager"),
88 KAboutLicense::GPL,
89 i18nc("@info:credit", "(C) 2006-2022 The Dolphin Developers"));
90 aboutData.setHomepage(QStringLiteral("https://kde.org/applications/system/org.kde.dolphin"));
91 aboutData.addAuthor(i18nc("@info:credit", "Felix Ernst"),
92 i18nc("@info:credit", "Maintainer (since 2021) and developer"),
93 QStringLiteral("felixernst@kde.org"));
94 aboutData.addAuthor(i18nc("@info:credit", "Méven Car"),
95 i18nc("@info:credit", "Maintainer (since 2021) and developer (since 2019)"),
96 QStringLiteral("meven@kde.org"));
97 aboutData.addAuthor(i18nc("@info:credit", "Elvis Angelaccio"),
98 i18nc("@info:credit", "Maintainer (2018-2021) and developer"),
99 QStringLiteral("elvis.angelaccio@kde.org"));
100 aboutData.addAuthor(i18nc("@info:credit", "Emmanuel Pescosta"),
101 i18nc("@info:credit", "Maintainer (2014-2018) and developer"),
102 QStringLiteral("emmanuelpescosta099@gmail.com"));
103 aboutData.addAuthor(i18nc("@info:credit", "Frank Reininghaus"),
104 i18nc("@info:credit", "Maintainer (2012-2014) and developer"),
105 QStringLiteral("frank78ac@googlemail.com"));
106 aboutData.addAuthor(i18nc("@info:credit", "Peter Penz"),
107 i18nc("@info:credit", "Maintainer and developer (2006-2012)"),
108 QStringLiteral("peter.penz19@gmail.com"));
109 aboutData.addAuthor(i18nc("@info:credit", "Sebastian Trüg"),
110 i18nc("@info:credit", "Developer"),
111 QStringLiteral("trueg@kde.org"));
112 aboutData.addAuthor(i18nc("@info:credit", "David Faure"),
113 i18nc("@info:credit", "Developer"),
114 QStringLiteral("faure@kde.org"));
115 aboutData.addAuthor(i18nc("@info:credit", "Aaron J. Seigo"),
116 i18nc("@info:credit", "Developer"),
117 QStringLiteral("aseigo@kde.org"));
118 aboutData.addAuthor(i18nc("@info:credit", "Rafael Fernández López"),
119 i18nc("@info:credit", "Developer"),
120 QStringLiteral("ereslibre@kde.org"));
121 aboutData.addAuthor(i18nc("@info:credit", "Kevin Ottens"),
122 i18nc("@info:credit", "Developer"),
123 QStringLiteral("ervin@kde.org"));
124 aboutData.addAuthor(i18nc("@info:credit", "Holger Freyther"),
125 i18nc("@info:credit", "Developer"),
126 QStringLiteral("freyther@gmx.net"));
127 aboutData.addAuthor(i18nc("@info:credit", "Max Blazejak"),
128 i18nc("@info:credit", "Developer"),
129 QStringLiteral("m43ksrocks@gmail.com"));
130 aboutData.addAuthor(i18nc("@info:credit", "Michael Austin"),
131 i18nc("@info:credit", "Documentation"),
132 QStringLiteral("tuxedup@users.sourceforge.net"));
133
134 KAboutData::setApplicationData(aboutData);
135
136 QCommandLineParser parser;
137 aboutData.setupCommandLine(&parser);
138
139 // command line options
140 parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("select"), i18nc("@info:shell", "The files and folders passed as arguments "
141 "will be selected.")));
142 parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("split"), i18nc("@info:shell", "Dolphin will get started with a split view.")));
143 parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("new-window"), i18nc("@info:shell", "Dolphin will explicitly open in a new window.")));
144 parser.addOption(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
158 if (parser.isSet(QStringLiteral("daemon"))) {
159 // Disable session management for the daemonized version
160 // See https://bugs.kde.org/show_bug.cgi?id=417219
161 auto disableSessionManagement = [](QSessionManager &sm) {
162 sm.setRestartHint(QSessionManager::RestartNever);
163 };
164 QObject::connect(&app, &QGuiApplication::commitDataRequest, disableSessionManagement);
165 QObject::connect(&app, &QGuiApplication::saveStateRequest, disableSessionManagement);
166
167 #ifdef FLATPAK
168 KDBusService dolphinDBusService(KDBusService::NoExitOnFailure);
169 #else
170 KDBusService dolphinDBusService;
171 #endif
172 DBusInterface interface;
173 interface.setAsDaemon();
174 return app.exec();
175 }
176
177 if (!parser.isSet(QStringLiteral("new-window"))) {
178 if (Dolphin::attachToExistingInstance(urls, openFiles, splitView)) {
179 // Successfully attached to existing instance of Dolphin
180 return 0;
181 }
182 }
183
184 if (!startedWithURLs) {
185 // We need at least one URL to open Dolphin
186 urls.append(Dolphin::homeUrl());
187 }
188
189 if (splitView && urls.size() < 2) {
190 // Split view does only make sense if we have at least 2 URLs
191 urls.append(urls.last());
192 }
193
194 DolphinMainWindow* mainWindow = new DolphinMainWindow();
195
196 if (openFiles) {
197 mainWindow->openFiles(urls, splitView);
198 } else {
199 mainWindow->openDirectories(urls, splitView);
200 }
201
202 mainWindow->show();
203
204 // Allow starting Dolphin on a system that is not running DBus:
205 KDBusService::StartupOptions serviceOptions = KDBusService::Multiple;
206 if (!QDBusConnection::sessionBus().isConnected()) {
207 serviceOptions |= KDBusService::NoExitOnFailure;
208 }
209 KDBusService dolphinDBusService(serviceOptions);
210 DBusInterface interface;
211
212 if (!app.isSessionRestored()) {
213 KConfigGui::setSessionConfig(QStringLiteral("dolphin"), QStringLiteral("dolphin"));
214 }
215
216 // Only restore session if:
217 // 1. Not explicitly opening a new instance
218 // 2. The "remember state" setting is enabled or session restoration after
219 // reboot is in use
220 // 3. There is a session available to restore
221 if (!parser.isSet(QStringLiteral("new-window"))
222 && (app.isSessionRestored() || GeneralSettings::rememberOpenedTabs())
223 ) {
224 // Get saved state data for the last-closed Dolphin instance
225 const QString serviceName = QStringLiteral("org.kde.dolphin-%1").arg(QCoreApplication::applicationPid());
226 if (Dolphin::dolphinGuiInstances(serviceName).size() > 0) {
227 const QString className = KXmlGuiWindow::classNameOfToplevel(1);
228 if (className == QLatin1String("DolphinMainWindow")) {
229 mainWindow->restore(1);
230 // If the user passed any URLs to Dolphin, open those in the
231 // window after session-restoring it
232 if (startedWithURLs) {
233 if (openFiles) {
234 mainWindow->openFiles(urls, splitView);
235 } else {
236 mainWindow->openDirectories(urls, splitView);
237 }
238 }
239 } else {
240 qCWarning(DolphinDebug) << "Unknown class " << className << " in session saved data!";
241 }
242 }
243 }
244
245 #if HAVE_KUSERFEEDBACK
246 auto feedbackProvider = DolphinFeedbackProvider::instance();
247 Q_UNUSED(feedbackProvider)
248 #endif
249
250 return app.exec(); // krazy:exclude=crash;
251 }