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