main.cpp 9.34 KB
Newer Older
1 2 3 4 5 6 7
/*
 * SPDX-FileCopyrightText: 2006 Peter Penz <peter.penz19@gmail.com>
 * SPDX-FileCopyrightText: 2006 Stefan Monov <logixoul@gmail.com>
 * SPDX-FileCopyrightText: 2015 Mathieu Tarral <mathieu.tarral@gmail.com>
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */
8

Roman Inflianskas's avatar
Roman Inflianskas committed
9 10
#include "dbusinterface.h"
#include "dolphin_generalsettings.h"
11
#include "dolphin_version.h"
Roman Inflianskas's avatar
Roman Inflianskas committed
12
#include "dolphindebug.h"
Peter Penz's avatar
Peter Penz committed
13
#include "dolphinmainwindow.h"
Ashish Bansal's avatar
Ashish Bansal committed
14
#include "global.h"
Peter Penz's avatar
Peter Penz committed
15

16
#include <KAboutData>
17
#include <KCrash>
Roman Inflianskas's avatar
Roman Inflianskas committed
18
#include <KDBusService>
Laurent Montel's avatar
Laurent Montel committed
19
#include <KLocalizedString>
20
#include <Kdelibs4ConfigMigrator>
21
#include <KConfigGui>
22

Roman Inflianskas's avatar
Roman Inflianskas committed
23 24
#include <QApplication>
#include <QCommandLineParser>
25 26 27 28
#include <QDBusConnection>
#include <QDBusInterface>
#include <QDBusAbstractInterface>
#include <QDBusConnectionInterface>
29
#include <QSessionManager>
Roman Inflianskas's avatar
Roman Inflianskas committed
30

31 32 33 34 35
#ifndef Q_OS_WIN
#include <unistd.h>
#endif
#include <iostream>

Andrius da Costa Ribas's avatar
Andrius da Costa Ribas committed
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 42 43 44 45 46 47
        if (!qEnvironmentVariableIsEmpty("SUDO_USER")) {
            std::cout << "Executing Dolphin with sudo is not possible due to unfixable security vulnerabilities." << std::endl;
            return EXIT_FAILURE;
        } else if (!qEnvironmentVariableIsEmpty("KDESU_USER")) {
            std::cout << "Executing Dolphin with kdesu is not possible due to unfixable security vulnerabilities." << std::endl;
            return EXIT_FAILURE;
        }
48 49 50
    }
#endif

51 52 53 54 55 56
    /**
     * enable high dpi support
     */
    QCoreApplication::setAttribute(Qt::AA_UseHighDpiPixmaps, true);
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling, true);

57
    QApplication app(argc, argv);
58
    app.setWindowIcon(QIcon::fromTheme(QStringLiteral("system-file-manager"), app.windowIcon()));
59

60 61
    KCrash::initialize();

Laurent Montel's avatar
Laurent Montel committed
62 63 64 65 66
    Kdelibs4ConfigMigrator migrate(QStringLiteral("dolphin"));
    migrate.setConfigFiles(QStringList() << QStringLiteral("dolphinrc"));
    migrate.setUiFiles(QStringList() << QStringLiteral("dolphinpart.rc") << QStringLiteral("dolphinui.rc"));
    migrate.migrate();

67 68
    KLocalizedString::setApplicationDomain("dolphin");

69
    KAboutData aboutData(QStringLiteral("dolphin"), i18n("Dolphin"), QStringLiteral(DOLPHIN_VERSION_STRING),
70 71
                         i18nc("@title", "File Manager"),
                         KAboutLicense::GPL,
72
                         i18nc("@info:credit", "(C) 2006-2018 Peter Penz, Frank Reininghaus, Emmanuel Pescosta and Elvis Angelaccio"));
Nate Graham's avatar
Nate Graham committed
73
    aboutData.setHomepage(QStringLiteral("https://kde.org/applications/system/org.kde.dolphin"));
74 75 76
    aboutData.addAuthor(i18nc("@info:credit", "Elvis Angelaccio"),
                        i18nc("@info:credit", "Maintainer (since 2018) and developer"),
                        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 81
    aboutData.addAuthor(i18nc("@info:credit", "Frank Reininghaus"),
                        i18nc("@info:credit", "Maintainer (2012-2014) and developer"),
82
                        QStringLiteral("frank78ac@googlemail.com"));
83 84
    aboutData.addAuthor(i18nc("@info:credit", "Peter Penz"),
                        i18nc("@info:credit", "Maintainer and developer (2006-2012)"),
85
                        QStringLiteral("peter.penz19@gmail.com"));
86 87
    aboutData.addAuthor(i18nc("@info:credit", "Sebastian Trüg"),
                        i18nc("@info:credit", "Developer"),
88
                        QStringLiteral("trueg@kde.org"));
89 90
    aboutData.addAuthor(i18nc("@info:credit", "David Faure"),
                        i18nc("@info:credit", "Developer"),
91
                        QStringLiteral("faure@kde.org"));
92 93
    aboutData.addAuthor(i18nc("@info:credit", "Aaron J. Seigo"),
                        i18nc("@info:credit", "Developer"),
94
                        QStringLiteral("aseigo@kde.org"));
95 96
    aboutData.addAuthor(i18nc("@info:credit", "Rafael Fernández López"),
                        i18nc("@info:credit", "Developer"),
97
                        QStringLiteral("ereslibre@kde.org"));
98 99
    aboutData.addAuthor(i18nc("@info:credit", "Kevin Ottens"),
                        i18nc("@info:credit", "Developer"),
100
                        QStringLiteral("ervin@kde.org"));
101 102
    aboutData.addAuthor(i18nc("@info:credit", "Holger Freyther"),
                        i18nc("@info:credit", "Developer"),
103
                        QStringLiteral("freyther@gmx.net"));
104 105
    aboutData.addAuthor(i18nc("@info:credit", "Max Blazejak"),
                        i18nc("@info:credit", "Developer"),
106
                        QStringLiteral("m43ksrocks@gmail.com"));
107 108
    aboutData.addAuthor(i18nc("@info:credit", "Michael Austin"),
                        i18nc("@info:credit", "Documentation"),
109
                        QStringLiteral("tuxedup@users.sourceforge.net"));
110 111 112 113 114 115 116

    KAboutData::setApplicationData(aboutData);

    QCommandLineParser parser;
    aboutData.setupCommandLine(&parser);

    // 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 122
    parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("daemon"), i18nc("@info:shell", "Start Dolphin Daemon (only required for DBus Interface)")));
    parser.addPositionalArgument(QStringLiteral("+[Url]"), i18nc("@info:shell", "Document to open"));
123 124 125 126

    parser.process(app);
    aboutData.processCommandLine(&parser);

127 128 129 130
    const bool splitView = parser.isSet(QStringLiteral("split")) || GeneralSettings::splitView();
    const bool openFiles = parser.isSet(QStringLiteral("select"));
    const QStringList args = parser.positionalArguments();
    QList<QUrl> urls = Dolphin::validateUris(args);
131 132 133
    // We later mutate urls, so we need to store if it was empty originally
    const bool startedWithURLs = !urls.isEmpty();

134

135
    if (parser.isSet(QStringLiteral("daemon"))) {
136 137 138 139 140 141 142 143
        // Disable session management for the daemonized version
        // See https://bugs.kde.org/show_bug.cgi?id=417219
        auto disableSessionManagement = [](QSessionManager &sm) {
            sm.setRestartHint(QSessionManager::RestartNever);
        };
        QObject::connect(&app, &QGuiApplication::commitDataRequest, disableSessionManagement);
        QObject::connect(&app, &QGuiApplication::saveStateRequest, disableSessionManagement);

144 145
        KDBusService dolphinDBusService;
        DBusInterface interface;
146
        interface.setAsDaemon();
Ashish Bansal's avatar
Ashish Bansal committed
147 148
        return app.exec();
    }
149

150 151 152 153 154 155 156
    if (!parser.isSet(QStringLiteral("new-window"))) {
        if (Dolphin::attachToExistingInstance(urls, openFiles, splitView)) {
            // Successfully attached to existing instance of Dolphin
            return 0;
        }
    }

157
    if (!startedWithURLs) {
158 159 160
        // We need at least one URL to open Dolphin
        urls.append(Dolphin::homeUrl());
    }
161

162 163 164 165
    if (splitView && urls.size() < 2) {
        // Split view does only make sense if we have at least 2 URLs
        urls.append(urls.last());
    }
166

167 168
    DolphinMainWindow* mainWindow = new DolphinMainWindow();

169
    if (openFiles) {
170
        mainWindow->openFiles(urls, splitView);
171
    } else {
172
        mainWindow->openDirectories(urls, splitView);
173 174
    }

175
    mainWindow->show();
176

177 178 179
    KDBusService dolphinDBusService;
    DBusInterface interface;

180 181 182 183 184
    if (!app.isSessionRestored()) {
        KConfigGui::setSessionConfig(QStringLiteral("dolphin"), QStringLiteral("dolphin"));
    }

    // Only restore session if:
185
    // 1. Not explicitly opening a new instance
186 187 188
    // 2. The "remember state" setting is enabled or session restoration after
    //    reboot is in use
    // 3. There is a session available to restore
189 190 191
    if (!parser.isSet(QStringLiteral("new-window"))
        && (app.isSessionRestored() || GeneralSettings::rememberOpenedTabs())
    ) {
192 193 194 195 196 197
        // Get saved state data for the last-closed Dolphin instance
        const QString serviceName = QStringLiteral("org.kde.dolphin-%1").arg(QCoreApplication::applicationPid());
        if (Dolphin::dolphinGuiInstances(serviceName).size() > 0) {
            const QString className = KXmlGuiWindow::classNameOfToplevel(1);
            if (className == QLatin1String("DolphinMainWindow")) {
                mainWindow->restore(1);
198 199 200 201 202
                // If the user passed any URLs to Dolphin, open those in the
                // window after session-restoring it
                if (startedWithURLs) {
                    mainWindow->openDirectories(urls, splitView);
                }
203 204 205 206

                // Now handle invalid locations in the set of active views to
                // avoid issues like https://bugs.kde.org/show_bug.cgi?id=427619
                mainWindow->setViewsWithInvalidPathsToHome();
207 208 209
            } else {
                qCWarning(DolphinDebug) << "Unknown class " << className << " in session saved data!";
            }
210
        }
211
    }
Peter Penz's avatar
Peter Penz committed
212

213
    return app.exec(); // krazy:exclude=crash;
Peter Penz's avatar
Peter Penz committed
214
}