]> cloud.milkyroute.net Git - dolphin.git/blob - src/main.cpp
Fix selection rect after porting from QFontMetrics::width()
[dolphin.git] / src / main.cpp
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz <peter.penz19@gmail.com> *
3 * Copyright (C) 2006 by Stefan Monov <logixoul@gmail.com> *
4 * Copyright (C) 2015 by Mathieu Tarral <mathieu.tarral@gmail.com> *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the *
18 * Free Software Foundation, Inc., *
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
20 ***************************************************************************/
21
22 #include "dbusinterface.h"
23 #include "dolphin_generalsettings.h"
24 #include "dolphin_version.h"
25 #include "dolphindebug.h"
26 #include "dolphinmainwindow.h"
27 #include "global.h"
28
29 #include <KAboutData>
30 #include <KCrash>
31 #include <KDBusService>
32 #include <KLocalizedString>
33 #include <Kdelibs4ConfigMigrator>
34 #include <KConfigGui>
35
36 #include <QApplication>
37 #include <QCommandLineParser>
38 #include <QDBusConnection>
39 #include <QDBusInterface>
40 #include <QDBusAbstractInterface>
41 #include <QDBusConnectionInterface>
42
43 #ifndef Q_OS_WIN
44 #include <unistd.h>
45 #endif
46 #include <iostream>
47
48 extern "C" Q_DECL_EXPORT int kdemain(int argc, char **argv)
49 {
50 #ifndef Q_OS_WIN
51 // Prohibit using sudo or kdesu (but allow using the root user directly)
52 if (getuid() == 0) {
53 if (!qEnvironmentVariableIsEmpty("SUDO_USER")) {
54 std::cout << "Executing Dolphin with sudo is not possible due to unfixable security vulnerabilities." << std::endl;
55 return EXIT_FAILURE;
56 } else if (!qEnvironmentVariableIsEmpty("KDESU_USER")) {
57 std::cout << "Executing Dolphin with kdesu is not possible due to unfixable security vulnerabilities." << std::endl;
58 return EXIT_FAILURE;
59 }
60 }
61 #endif
62
63 /**
64 * enable high dpi support
65 */
66 QCoreApplication::setAttribute(Qt::AA_UseHighDpiPixmaps, true);
67 QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling, true);
68
69 QApplication app(argc, argv);
70 app.setWindowIcon(QIcon::fromTheme(QStringLiteral("system-file-manager"), app.windowIcon()));
71
72 KCrash::initialize();
73
74 Kdelibs4ConfigMigrator migrate(QStringLiteral("dolphin"));
75 migrate.setConfigFiles(QStringList() << QStringLiteral("dolphinrc"));
76 migrate.setUiFiles(QStringList() << QStringLiteral("dolphinpart.rc") << QStringLiteral("dolphinui.rc"));
77 migrate.migrate();
78
79 KLocalizedString::setApplicationDomain("dolphin");
80
81 KAboutData aboutData(QStringLiteral("dolphin"), i18n("Dolphin"), QStringLiteral(DOLPHIN_VERSION_STRING),
82 i18nc("@title", "File Manager"),
83 KAboutLicense::GPL,
84 i18nc("@info:credit", "(C) 2006-2018 Peter Penz, Frank Reininghaus, Emmanuel Pescosta and Elvis Angelaccio"));
85 aboutData.setHomepage(QStringLiteral("https://kde.org/applications/system/org.kde.dolphin"));
86 aboutData.addAuthor(i18nc("@info:credit", "Elvis Angelaccio"),
87 i18nc("@info:credit", "Maintainer (since 2018) and developer"),
88 QStringLiteral("elvis.angelaccio@kde.org"));
89 aboutData.addAuthor(i18nc("@info:credit", "Emmanuel Pescosta"),
90 i18nc("@info:credit", "Maintainer (2014-2018) and developer"),
91 QStringLiteral("emmanuelpescosta099@gmail.com"));
92 aboutData.addAuthor(i18nc("@info:credit", "Frank Reininghaus"),
93 i18nc("@info:credit", "Maintainer (2012-2014) and developer"),
94 QStringLiteral("frank78ac@googlemail.com"));
95 aboutData.addAuthor(i18nc("@info:credit", "Peter Penz"),
96 i18nc("@info:credit", "Maintainer and developer (2006-2012)"),
97 QStringLiteral("peter.penz19@gmail.com"));
98 aboutData.addAuthor(i18nc("@info:credit", "Sebastian Trüg"),
99 i18nc("@info:credit", "Developer"),
100 QStringLiteral("trueg@kde.org"));
101 aboutData.addAuthor(i18nc("@info:credit", "David Faure"),
102 i18nc("@info:credit", "Developer"),
103 QStringLiteral("faure@kde.org"));
104 aboutData.addAuthor(i18nc("@info:credit", "Aaron J. Seigo"),
105 i18nc("@info:credit", "Developer"),
106 QStringLiteral("aseigo@kde.org"));
107 aboutData.addAuthor(i18nc("@info:credit", "Rafael Fernández López"),
108 i18nc("@info:credit", "Developer"),
109 QStringLiteral("ereslibre@kde.org"));
110 aboutData.addAuthor(i18nc("@info:credit", "Kevin Ottens"),
111 i18nc("@info:credit", "Developer"),
112 QStringLiteral("ervin@kde.org"));
113 aboutData.addAuthor(i18nc("@info:credit", "Holger Freyther"),
114 i18nc("@info:credit", "Developer"),
115 QStringLiteral("freyther@gmx.net"));
116 aboutData.addAuthor(i18nc("@info:credit", "Max Blazejak"),
117 i18nc("@info:credit", "Developer"),
118 QStringLiteral("m43ksrocks@gmail.com"));
119 aboutData.addAuthor(i18nc("@info:credit", "Michael Austin"),
120 i18nc("@info:credit", "Documentation"),
121 QStringLiteral("tuxedup@users.sourceforge.net"));
122
123 KAboutData::setApplicationData(aboutData);
124
125 QCommandLineParser parser;
126 aboutData.setupCommandLine(&parser);
127
128 // command line options
129 parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("select"), i18nc("@info:shell", "The files and folders passed as arguments "
130 "will be selected.")));
131 parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("split"), i18nc("@info:shell", "Dolphin will get started with a split view.")));
132 parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("new-window"), i18nc("@info:shell", "Dolphin will explicitly open in a new window.")));
133 parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("daemon"), i18nc("@info:shell", "Start Dolphin Daemon (only required for DBus Interface)")));
134 parser.addPositionalArgument(QStringLiteral("+[Url]"), i18nc("@info:shell", "Document to open"));
135
136 parser.process(app);
137 aboutData.processCommandLine(&parser);
138
139 const bool splitView = parser.isSet(QStringLiteral("split")) || GeneralSettings::splitView();
140 const bool openFiles = parser.isSet(QStringLiteral("select"));
141 const QStringList args = parser.positionalArguments();
142 QList<QUrl> urls = Dolphin::validateUris(args);
143 // We later mutate urls, so we need to store if it was empty originally
144 const bool startedWithURLs = !urls.isEmpty();
145
146
147 if (parser.isSet(QStringLiteral("daemon"))) {
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 if (!app.isSessionRestored()) {
182 KConfigGui::setSessionConfig(QStringLiteral("dolphin"), QStringLiteral("dolphin"));
183 }
184
185 // Only restore session if:
186 // 1. Dolphin was not started with command line args
187 // 2. The "remember state" setting is enabled or session restoration after
188 // reboot is in use
189 // 3. There is a session available to restore
190 if (!startedWithURLs && (app.isSessionRestored() || GeneralSettings::rememberOpenedTabs()) ) {
191 // Get saved state data for the last-closed Dolphin instance
192 const QString serviceName = QStringLiteral("org.kde.dolphin-%1").arg(QCoreApplication::applicationPid());
193 if (Dolphin::dolphinGuiInstances(serviceName).size() > 0) {
194 const QString className = KXmlGuiWindow::classNameOfToplevel(1);
195 if (className == QLatin1String("DolphinMainWindow")) {
196 mainWindow->restore(1);
197 } else {
198 qCWarning(DolphinDebug) << "Unknown class " << className << " in session saved data!";
199 }
200 }
201 }
202
203 KDBusService dolphinDBusService;
204 DBusInterface interface;
205
206 return app.exec(); // krazy:exclude=crash;
207 }