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