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