+ // command line options
+ parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("select"),
+ i18nc("@info:shell",
+ "The files and folders passed as arguments "
+ "will be selected.")));
+ parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("split"), i18nc("@info:shell", "Dolphin will get started with a split view.")));
+ parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("new-window"), i18nc("@info:shell", "Dolphin will explicitly open in a new window.")));
+ parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("sudo") << QStringLiteral("admin"),
+ i18nc("@info:shell", "Set up Dolphin for administrative tasks.")));
+ 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"));
+
+ parser.process(app);
+ aboutData.processCommandLine(&parser);
+
+ const bool splitView = parser.isSet(QStringLiteral("split")) || GeneralSettings::splitView();
+ const bool openFiles = parser.isSet(QStringLiteral("select"));
+ const bool adminWorkerInfoWanted = parser.isSet(QStringLiteral("sudo")) || parser.isSet(QStringLiteral("admin"));
+ const QStringList args = parser.positionalArguments();
+ QList<QUrl> urls = Dolphin::validateUris(args);
+ // We later mutate urls, so we need to store if it was empty originally
+ const bool startedWithURLs = !urls.isEmpty();
+
+ if (adminWorkerInfoWanted || std::any_of(urls.cbegin(), urls.cend(), [](const QUrl &url) {
+ return url.scheme() == QStringLiteral("admin");
+ })) {
+ Admin::guideUserTowardsInstallingAdminWorker();
+ }
+
+ if (parser.isSet(QStringLiteral("daemon"))) {
+ // Prevent KApplicationLauncherJob from cause the application to quit on job finish.
+ QCoreApplication::setQuitLockEnabled(false);
+
+ // 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);
+
+#ifdef FLATPAK
+ KDBusService dolphinDBusService(KDBusService::NoExitOnFailure);
+#else
+ KDBusService dolphinDBusService;
+#endif
+ DBusInterface interface;
+ interface.setAsDaemon();
+ return app.exec();
+ }
+
+ if (!parser.isSet(QStringLiteral("new-window"))) {
+ QString token;
+ if (KWindowSystem::isPlatformWayland()) {
+ token = qEnvironmentVariable("XDG_ACTIVATION_TOKEN");
+ qunsetenv("XDG_ACTIVATION_TOKEN");
+ } else if (KWindowSystem::isPlatformX11()) {
+#if HAVE_X11
+ token = QX11Info::nextStartupId();
+#endif
+ }
+
+ if (Dolphin::attachToExistingInstance(urls, openFiles, splitView, QString(), token)) {
+ // Successfully attached to existing instance of Dolphin
+ return 0;
+ }
+ }
+
+ if (!startedWithURLs) {
+ // We need at least one URL to open Dolphin
+ urls.append(Dolphin::homeUrl());
+ }
+
+ if (splitView && urls.size() < 2) {
+ // Split view does only make sense if we have at least 2 URLs
+ urls.append(urls.last());
+ }
+
+ DolphinMainWindow *mainWindow = new DolphinMainWindow();
+
+ if (openFiles) {
+ mainWindow->openFiles(urls, splitView);
+ } else {
+ mainWindow->openDirectories(urls, splitView);
+ }
+
+ mainWindow->show();
+
+ // Allow starting Dolphin on a system that is not running DBus:
+ KDBusService::StartupOptions serviceOptions = KDBusService::Multiple;
+ if (!QDBusConnection::sessionBus().isConnected()) {
+ serviceOptions |= KDBusService::NoExitOnFailure;
+ }
+ KDBusService dolphinDBusService(serviceOptions);
+ DBusInterface interface;
+
+ if (!app.isSessionRestored()) {
+ KConfigGui::setSessionConfig(QStringLiteral("dolphin"), QStringLiteral("dolphin"));
+ }
+
+ // Only restore session if:
+ // 1. Not explicitly opening a new instance
+ // 2. The "remember state" setting is enabled or session restoration after
+ // reboot is in use
+ // 3. There is a session available to restore
+ if (!parser.isSet(QStringLiteral("new-window")) && (app.isSessionRestored() || GeneralSettings::rememberOpenedTabs())) {
+ // 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);