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