]> cloud.milkyroute.net Git - dolphin.git/blob - src/main.cpp
GIT_SILENT Sync po/docbooks with svn
[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 #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 can cause bugs and expose you to security vulnerabilities. "
58 "Instead use Dolphin normally and you will be prompted for elevated privileges when "
59 "performing file operations that require them."
60 << std::endl;
61 return EXIT_FAILURE;
62 } else if (!qEnvironmentVariableIsEmpty("KDESU_USER")) {
63 std::cout << "Running Dolphin with kdesu can cause bugs and expose you to security vulnerabilities. "
64 "Instead use Dolphin normally and you will be prompted for elevated privileges when "
65 "performing file operations that require them."
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 KIO::PreviewJob::setDefaultDevicePixelRatio(app.devicePixelRatio());
83
84 KCrash::initialize();
85
86 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
87 Kdelibs4ConfigMigrator migrate(QStringLiteral("dolphin"));
88 migrate.setConfigFiles(QStringList() << QStringLiteral("dolphinrc"));
89 migrate.setUiFiles(QStringList() << QStringLiteral("dolphinpart.rc") << QStringLiteral("dolphinui.rc"));
90 migrate.migrate();
91 #endif
92
93 KLocalizedString::setApplicationDomain("dolphin");
94
95 KAboutData aboutData(QStringLiteral("dolphin"), i18n("Dolphin"), QStringLiteral(DOLPHIN_VERSION_STRING),
96 i18nc("@title", "File Manager"),
97 KAboutLicense::GPL,
98 i18nc("@info:credit", "(C) 2006-2022 The Dolphin Developers"));
99 aboutData.setHomepage(QStringLiteral("https://kde.org/applications/system/org.kde.dolphin"));
100 aboutData.addAuthor(i18nc("@info:credit", "Felix Ernst"),
101 i18nc("@info:credit", "Maintainer (since 2021) and developer"),
102 QStringLiteral("felixernst@kde.org"));
103 aboutData.addAuthor(i18nc("@info:credit", "Méven Car"),
104 i18nc("@info:credit", "Maintainer (since 2021) and developer (since 2019)"),
105 QStringLiteral("meven@kde.org"));
106 aboutData.addAuthor(i18nc("@info:credit", "Elvis Angelaccio"),
107 i18nc("@info:credit", "Maintainer (2018-2021) and developer"),
108 QStringLiteral("elvis.angelaccio@kde.org"));
109 aboutData.addAuthor(i18nc("@info:credit", "Emmanuel Pescosta"),
110 i18nc("@info:credit", "Maintainer (2014-2018) and developer"),
111 QStringLiteral("emmanuelpescosta099@gmail.com"));
112 aboutData.addAuthor(i18nc("@info:credit", "Frank Reininghaus"),
113 i18nc("@info:credit", "Maintainer (2012-2014) and developer"),
114 QStringLiteral("frank78ac@googlemail.com"));
115 aboutData.addAuthor(i18nc("@info:credit", "Peter Penz"),
116 i18nc("@info:credit", "Maintainer and developer (2006-2012)"),
117 QStringLiteral("peter.penz19@gmail.com"));
118 aboutData.addAuthor(i18nc("@info:credit", "Sebastian Trüg"),
119 i18nc("@info:credit", "Developer"),
120 QStringLiteral("trueg@kde.org"));
121 aboutData.addAuthor(i18nc("@info:credit", "David Faure"),
122 i18nc("@info:credit", "Developer"),
123 QStringLiteral("faure@kde.org"));
124 aboutData.addAuthor(i18nc("@info:credit", "Aaron J. Seigo"),
125 i18nc("@info:credit", "Developer"),
126 QStringLiteral("aseigo@kde.org"));
127 aboutData.addAuthor(i18nc("@info:credit", "Rafael Fernández López"),
128 i18nc("@info:credit", "Developer"),
129 QStringLiteral("ereslibre@kde.org"));
130 aboutData.addAuthor(i18nc("@info:credit", "Kevin Ottens"),
131 i18nc("@info:credit", "Developer"),
132 QStringLiteral("ervin@kde.org"));
133 aboutData.addAuthor(i18nc("@info:credit", "Holger Freyther"),
134 i18nc("@info:credit", "Developer"),
135 QStringLiteral("freyther@gmx.net"));
136 aboutData.addAuthor(i18nc("@info:credit", "Max Blazejak"),
137 i18nc("@info:credit", "Developer"),
138 QStringLiteral("m43ksrocks@gmail.com"));
139 aboutData.addAuthor(i18nc("@info:credit", "Michael Austin"),
140 i18nc("@info:credit", "Documentation"),
141 QStringLiteral("tuxedup@users.sourceforge.net"));
142
143 KAboutData::setApplicationData(aboutData);
144
145 QCommandLineParser parser;
146 aboutData.setupCommandLine(&parser);
147
148 // command line options
149 parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("select"), i18nc("@info:shell", "The files and folders passed as arguments "
150 "will be selected.")));
151 parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("split"), i18nc("@info:shell", "Dolphin will get started with a split view.")));
152 parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("new-window"), i18nc("@info:shell", "Dolphin will explicitly open in a new window.")));
153 parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("daemon"), i18nc("@info:shell", "Start Dolphin Daemon (only required for DBus Interface).")));
154 parser.addPositionalArgument(QStringLiteral("+[Url]"), i18nc("@info:shell", "Document to open"));
155
156 parser.process(app);
157 aboutData.processCommandLine(&parser);
158
159 const bool splitView = parser.isSet(QStringLiteral("split")) || GeneralSettings::splitView();
160 const bool openFiles = parser.isSet(QStringLiteral("select"));
161 const QStringList args = parser.positionalArguments();
162 QList<QUrl> urls = Dolphin::validateUris(args);
163 // We later mutate urls, so we need to store if it was empty originally
164 const bool startedWithURLs = !urls.isEmpty();
165
166
167 if (parser.isSet(QStringLiteral("daemon"))) {
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);
172 };
173 QObject::connect(&app, &QGuiApplication::commitDataRequest, disableSessionManagement);
174 QObject::connect(&app, &QGuiApplication::saveStateRequest, disableSessionManagement);
175
176 #ifdef FLATPAK
177 KDBusService dolphinDBusService(KDBusService::NoExitOnFailure);
178 #else
179 KDBusService dolphinDBusService;
180 #endif
181 DBusInterface interface;
182 interface.setAsDaemon();
183 return app.exec();
184 }
185
186 if (!parser.isSet(QStringLiteral("new-window"))) {
187
188 QString token;
189 if (KWindowSystem::isPlatformWayland()) {
190 token = qEnvironmentVariable("XDG_ACTIVATION_TOKEN");
191 qunsetenv("XDG_ACTIVATION_TOKEN");
192 } else if (KWindowSystem::isPlatformX11()) {
193 #if HAVE_X11
194 token = QX11Info::nextStartupId();
195 #endif
196 }
197
198 if (Dolphin::attachToExistingInstance(urls, openFiles, splitView, QString(), token)) {
199 // Successfully attached to existing instance of Dolphin
200 return 0;
201 }
202 }
203
204 if (!startedWithURLs) {
205 // We need at least one URL to open Dolphin
206 urls.append(Dolphin::homeUrl());
207 }
208
209 if (splitView && urls.size() < 2) {
210 // Split view does only make sense if we have at least 2 URLs
211 urls.append(urls.last());
212 }
213
214 DolphinMainWindow* mainWindow = new DolphinMainWindow();
215
216 if (openFiles) {
217 mainWindow->openFiles(urls, splitView);
218 } else {
219 mainWindow->openDirectories(urls, splitView);
220 }
221
222 mainWindow->show();
223
224 // Allow starting Dolphin on a system that is not running DBus:
225 KDBusService::StartupOptions serviceOptions = KDBusService::Multiple;
226 if (!QDBusConnection::sessionBus().isConnected()) {
227 serviceOptions |= KDBusService::NoExitOnFailure;
228 }
229 KDBusService dolphinDBusService(serviceOptions);
230 DBusInterface interface;
231
232 if (!app.isSessionRestored()) {
233 KConfigGui::setSessionConfig(QStringLiteral("dolphin"), QStringLiteral("dolphin"));
234 }
235
236 // Only restore session if:
237 // 1. Not explicitly opening a new instance
238 // 2. The "remember state" setting is enabled or session restoration after
239 // reboot is in use
240 // 3. There is a session available to restore
241 if (!parser.isSet(QStringLiteral("new-window"))
242 && (app.isSessionRestored() || GeneralSettings::rememberOpenedTabs())
243 ) {
244 // Get saved state data for the last-closed Dolphin instance
245 const QString serviceName = QStringLiteral("org.kde.dolphin-%1").arg(QCoreApplication::applicationPid());
246 if (Dolphin::dolphinGuiInstances(serviceName).size() > 0) {
247 const QString className = KXmlGuiWindow::classNameOfToplevel(1);
248 if (className == QLatin1String("DolphinMainWindow")) {
249 mainWindow->restore(1);
250 // If the user passed any URLs to Dolphin, open those in the
251 // window after session-restoring it
252 if (startedWithURLs) {
253 if (openFiles) {
254 mainWindow->openFiles(urls, splitView);
255 } else {
256 mainWindow->openDirectories(urls, splitView);
257 }
258 }
259 } else {
260 qCWarning(DolphinDebug) << "Unknown class " << className << " in session saved data!";
261 }
262 }
263 }
264
265 #if HAVE_KUSERFEEDBACK
266 auto feedbackProvider = DolphinFeedbackProvider::instance();
267 Q_UNUSED(feedbackProvider)
268 #endif
269
270 return app.exec(); // krazy:exclude=crash;
271 }