File 0014-Revert-KLauncher-remove-dead-code-related-to-autosta.patch of Package mingw32-kinit

From b5c6dfcf823ecfb3d1f5809df474d9fdfe3c5f2b Mon Sep 17 00:00:00 2001
From: Ralf Habacker <ralf.habacker@freenet.de>
Date: Thu, 22 Jul 2021 19:03:35 +0200
Subject: [PATCH 14/18] Revert "KLauncher: remove dead code related to
 autostart."

This reverts commit 0d4d861ba3b91e65f398e84e5260a914c43b1857.
---
 CMakeLists.txt                      |  2 +
 src/klauncher/klauncher.cpp         | 63 ++++++++++++++++++++++++++---
 src/klauncher/klauncher.h           | 15 ++++++-
 src/klauncher/klauncher_adaptor.cpp |  6 +++
 src/klauncher/klauncher_adaptor.h   |  5 +++
 tests/CMakeLists.txt                |  9 +++++
 tests/autostarttest.cpp             | 57 ++++++++++++++++++++++++++
 7 files changed, 151 insertions(+), 6 deletions(-)
 create mode 100644 tests/CMakeLists.txt
 create mode 100644 tests/autostarttest.cpp

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 791d773..19bf95d 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -91,6 +91,8 @@ if (KF5DocTools_FOUND)
     add_subdirectory(docs)
 endif()
 
+add_subdirectory(tests)
+
 install( FILES
 	"${CMAKE_CURRENT_BINARY_DIR}/KF5InitConfig.cmake"
     KF5InitMacros.cmake ${CMAKE_CURRENT_BINARY_DIR}/KF5InitConfigVersion.cmake
diff --git a/src/klauncher/klauncher.cpp b/src/klauncher/klauncher.cpp
index 44aa76e..356bc06 100644
--- a/src/klauncher/klauncher.cpp
+++ b/src/klauncher/klauncher.cpp
@@ -96,10 +96,12 @@ KLauncher::KLauncher()
     Q_ASSERT(g_klauncher_self == nullptr);
     g_klauncher_self = this;
 
+    mAutoTimer.setSingleShot(true);
     new KLauncherAdaptor(this);
     mSlaveLauncherAdaptor = new KSlaveLauncherAdaptor(this);
     QDBusConnection::sessionBus().registerObject(QStringLiteral("/KLauncher"), this); // same as ktoolinvocation.cpp
 
+    connect(&mAutoTimer, SIGNAL(timeout()), this, SLOT(slotAutoStart()));
     connect(QDBusConnection::sessionBus().interface(),
             SIGNAL(serviceOwnerChanged(QString,QString,QString)),
             SLOT(slotNameOwnerChanged(QString,QString,QString)));
@@ -403,6 +405,49 @@ KLauncher::slotNameOwnerChanged(const QString &appId, const QString &oldOwner,
     }
 }
 
+void
+KLauncher::autoStart(int phase)
+{
+    if (mAutoStart.phase() >= phase) {
+        return;
+    }
+    mAutoStart.setPhase(phase);
+    if (phase == 0) {
+        mAutoStart.loadAutoStartList();
+    }
+    mAutoTimer.start(0);
+}
+
+void
+KLauncher::slotAutoStart()
+{
+    KService::Ptr s;
+    do {
+        QString service = mAutoStart.startService();
+        qCDebug(KLAUNCHER) << "Service: " << mAutoStart.phase() << service;
+        if (service.isEmpty()) {
+            // Done
+            if (!mAutoStart.phaseDone()) {
+                mAutoStart.setPhaseDone();
+                switch (mAutoStart.phase()) {
+                case 0:
+                    emit autoStart0Done();
+                    break;
+                case 1:
+                    emit autoStart1Done();
+                    break;
+                case 2:
+                    emit autoStart2Done();
+                    break;
+                }
+            }
+            return;
+        }
+        s = new KService(service);
+    } while (!start_service(s, QStringList(), QStringList(), "0", false, true, QDBusMessage()));
+    // Loop till we find a service that we can start.
+}
+
 void
 KLauncher::requestDone(KLaunchRequest *request)
 {
@@ -433,6 +478,10 @@ KLauncher::requestDone(KLaunchRequest *request)
 #endif
     }
 
+    if (request->autoStart) {
+        mAutoTimer.start(0);
+    }
+
     if (request->transaction.type() != QDBusMessage::InvalidMessage) {
         if (requestResult.dbusName.isNull()) { // null strings can't be sent
             requestResult.dbusName.clear();
@@ -558,6 +607,7 @@ KLauncher::requestStart(KLaunchRequest *request)
 void KLauncher::exec_blind(const QString &name, const QStringList &arg_list, const QStringList &envs, const QString &startup_id)
 {
     KLaunchRequest *request = new KLaunchRequest;
+    request->autoStart = false;
     request->name = name;
     request->arg_list =  arg_list;
     request->dbus_startup_type = KService::DBusNone;
@@ -600,7 +650,7 @@ KLauncher::start_service_by_desktop_path(const QString &serviceName, const QStri
         cancel_service_startup_info(nullptr, startup_id.toLocal8Bit(), envs);   // cancel it if any
         return false;
     }
-    return start_service(service, urls, envs, startup_id.toLocal8Bit(), blind, msg);
+    return start_service(service, urls, envs, startup_id.toLocal8Bit(), blind, false, msg);
 }
 
 bool
@@ -614,13 +664,13 @@ KLauncher::start_service_by_desktop_name(const QString &serviceName, const QStri
         cancel_service_startup_info(nullptr, startup_id.toLocal8Bit(), envs);   // cancel it if any
         return false;
     }
-    return start_service(service, urls, envs, startup_id.toLocal8Bit(), blind, msg);
+    return start_service(service, urls, envs, startup_id.toLocal8Bit(), blind, false, msg);
 }
 
 bool
 KLauncher::start_service(KService::Ptr service, const QStringList &_urls,
                          const QStringList &envs, const QByteArray &startup_id,
-                         bool blind, const QDBusMessage &msg)
+                         bool blind, bool autoStart, const QDBusMessage &msg)
 {
     QStringList urls = _urls;
     bool runPermitted = KDesktopFile::isAuthorizedDesktopFile(service->entryPath());
@@ -636,6 +686,7 @@ KLauncher::start_service(KService::Ptr service, const QStringList &_urls,
         return false;
     }
     KLaunchRequest *request = new KLaunchRequest;
+    request->autoStart = autoStart;
 
     enum DiscreteGpuCheck { NotChecked, Present, Absent };
     static DiscreteGpuCheck s_gpuCheck = NotChecked;
@@ -678,7 +729,7 @@ KLauncher::start_service(KService::Ptr service, const QStringList &_urls,
             if (!startup_id2.isEmpty() && startup_id2 != "0") {
                 startup_id2 = "0";    // can't use the same startup_id several times // krazy:exclude=doublequote_chars
             }
-            start_service(service, singleUrl, _envs, startup_id2, true, msg);
+            start_service(service, singleUrl, _envs, startup_id2, true, false, msg);
         }
         const QString firstURL = urls.at(0);
         urls.clear();
@@ -732,7 +783,7 @@ KLauncher::start_service(KService::Ptr service, const QStringList &_urls,
     send_service_startup_info(request, service, startup_id, _envs);
 
     // Request will be handled later.
-    if (!blind) {
+    if (!blind && !autoStart) {
         msg.setDelayedReply(true);
         request->transaction = msg;
     }
@@ -831,6 +882,7 @@ KLauncher::kdeinit_exec(const QString &app, const QStringList &args,
                         const QString &startup_id, bool wait, const QDBusMessage &msg)
 {
     KLaunchRequest *request = new KLaunchRequest;
+    request->autoStart = false;
     request->arg_list = args;
     request->name = app;
     request->dbus_startup_type = KService::DBusNone;
@@ -1029,6 +1081,7 @@ KLauncher::requestSlave(const QString &protocol,
     }
 #endif
     KLaunchRequest *request = new KLaunchRequest;
+    request->autoStart = false;
     request->name = name;
     request->arg_list =  arg_list;
     request->dbus_startup_type = KService::DBusNone;
diff --git a/src/klauncher/klauncher.h b/src/klauncher/klauncher.h
index 4e2a4ad..5a1cb36 100644
--- a/src/klauncher/klauncher.h
+++ b/src/klauncher/klauncher.h
@@ -8,6 +8,8 @@
 #ifndef _KLAUNCHER_H_
 #define _KLAUNCHER_H_
 
+#include "autostart.h"
+
 #include <qplatformdefs.h>
 
 #include <config-kdeinit.h> // HAVE_X11
@@ -60,6 +62,7 @@ public:
     status_t status;
     QDBusMessage transaction;
     KService::DBusStartupType dbus_startup_type;
+    bool autoStart;
     bool wait; //wait until the process dies before finishing the request
     QString errorMsg;
 #if HAVE_X11
@@ -108,7 +111,7 @@ protected:
 
     bool start_service(KService::Ptr service, const QStringList &urls,
                        const QStringList &envs, const QByteArray &startup_id,
-                       bool blind, const QDBusMessage &msg);
+                       bool blind, bool autoStart, const QDBusMessage &msg);
 
     void createArgs(KLaunchRequest *request, const KService::Ptr service,
                     const QList<QUrl> &url);
@@ -120,7 +123,14 @@ protected:
     void cancel_service_startup_info(KLaunchRequest *request, const QByteArray &startup_id,
                                      const QStringList &envs);
 
+Q_SIGNALS:
+    void autoStart0Done();
+    void autoStart1Done();
+    void autoStart2Done();
+
 public: // remote methods, called by KLauncherAdaptor
+    void autoStart(int phase = 1);
+
     /**
      * Starts a program.
      * 'envs' are environment variables that will be added
@@ -195,6 +205,7 @@ public: // remote methods, called by KLauncherAdaptor
     void terminate_kdeinit();
 
 public Q_SLOTS:
+    void slotAutoStart();
     void slotDequeue();
     void slotKDEInitData(int);
     void slotNameOwnerChanged(const QString &name, const QString &oldOnwer, const QString &newOwner);
@@ -217,7 +228,9 @@ protected:
     KIO::ConnectionServer mConnectionServer;
     QList<QPointer<IdleSlave>> mSlaveList;
     QTimer mTimer;
+    QTimer mAutoTimer;
     bool bProcessingQueue;
+    AutoStart mAutoStart;
     QString mSlaveDebug;
     QString mSlaveValgrind;
     QString mSlaveValgrindSkin;
diff --git a/src/klauncher/klauncher_adaptor.cpp b/src/klauncher/klauncher_adaptor.cpp
index 758e695..500172e 100644
--- a/src/klauncher/klauncher_adaptor.cpp
+++ b/src/klauncher/klauncher_adaptor.cpp
@@ -35,6 +35,12 @@ KLauncherAdaptor::~KLauncherAdaptor()
     // destructor
 }
 
+void KLauncherAdaptor::autoStart(int phase)
+{
+    // handle method call org.kde.KLauncher.autoStart
+    static_cast<KLauncher *>(parent())->autoStart(phase);
+}
+
 void KLauncherAdaptor::exec_blind(const QString &name, const QStringList &arg_list)
 {
     // handle method call org.kde.KLauncher.exec_blind
diff --git a/src/klauncher/klauncher_adaptor.h b/src/klauncher/klauncher_adaptor.h
index b04d34f..103ae29 100644
--- a/src/klauncher/klauncher_adaptor.h
+++ b/src/klauncher/klauncher_adaptor.h
@@ -30,6 +30,7 @@ public:
 
 public: // PROPERTIES
 public Q_SLOTS: // METHODS
+    void autoStart(int phase = 1);
     void exec_blind(const QString &name, const QStringList &arg_list);
     void exec_blind(const QString &name, const QStringList &arg_list, const QStringList &envs, const QString &startup_id);
     int kdeinit_exec(const QString &app, const QStringList &args, const QStringList &env, const QString &startup_id, const QDBusMessage &msg, QString &dbusServiceName, QString &error, int &pid);
@@ -40,6 +41,10 @@ public Q_SLOTS: // METHODS
     int start_service_by_desktop_name(const QString &serviceName, const QStringList &urls, const QStringList &envs, const QString &startup_id, bool blind, const QDBusMessage &msg, QString &dbusServiceName, QString &error, int &pid);
     int start_service_by_desktop_path(const QString &serviceName, const QStringList &urls, const QStringList &envs, const QString &startup_id, bool blind, const QDBusMessage &msg, QString &dbusServiceName, QString &error, int &pid);
     void terminate_kdeinit();
+Q_SIGNALS: // SIGNALS
+    void autoStart0Done();
+    void autoStart1Done();
+    void autoStart2Done();
 };
 
 #endif
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
new file mode 100644
index 0000000..fafc0ea
--- /dev/null
+++ b/tests/CMakeLists.txt
@@ -0,0 +1,9 @@
+add_executable(autostart_test
+    autostarttest.cpp
+    ../src/klauncher/autostart.cpp
+)
+
+target_link_libraries(autostart_test
+    Qt5::Core
+    KF5::Service
+)
diff --git a/tests/autostarttest.cpp b/tests/autostarttest.cpp
new file mode 100644
index 0000000..3356fa3
--- /dev/null
+++ b/tests/autostarttest.cpp
@@ -0,0 +1,57 @@
+/*
+ *  This file is part of the KDE libraries
+ *  Copyright (c) 2014 Vishesh Handa <me@vhanda.in>
+ *
+ *  This library is free software; you can redistribute it and/or
+ *  modify it under the terms of the GNU Library General Public
+ *  License version 2 as published by the Free Software Foundation.
+ *
+ *  This library is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ *  Library General Public License for more details.
+ *
+ *  You should have received a copy of the GNU Library General Public License
+ *  along with this library; see the file COPYING.LIB.  If not, write to
+ *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ *  Boston, MA 02110-1301, USA.
+ **/
+
+#include <QCoreApplication>
+#include <QCommandLineParser>
+#include <QDebug>
+#include <QTextStream>
+
+#include "../src/klauncher/autostart.h"
+
+int main(int argc, char **argv)
+{
+    QCoreApplication app(argc, argv);
+
+    QCommandLineParser parser;
+    parser.addPositionalArgument(QStringLiteral("phase"), QStringLiteral("autostart phase number"));
+    parser.process(app);
+
+    QStringList args = parser.positionalArguments();
+    if (args.size() != 1) {
+        parser.showHelp(1);
+    }
+
+    int phase = args.first().toInt();
+    AutoStart autostart;
+    autostart.setPhase(phase);
+    autostart.loadAutoStartList();
+
+    QTextStream out(stdout);
+
+    while (!autostart.phaseDone()) {
+        QString service = autostart.startService();
+        if (service.isEmpty()) {
+            break;
+        }
+
+        out << service << "\n";
+    }
+
+    return 0;
+}
-- 
2.26.2

openSUSE Build Service is sponsored by