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