File 0013-Revert-Finish-cleaning-up-autostart-related-code.patch of Package mingw32-kinit
From 022d85ee14b58482a6629659340f5257f1b57325 Mon Sep 17 00:00:00 2001
From: Ralf Habacker <ralf.habacker@freenet.de>
Date: Thu, 22 Jul 2021 18:59:41 +0200
Subject: [PATCH 13/18] Revert "Finish cleaning up autostart-related code"
This reverts commit a6cc8da6bd56ccb42abdc7e55d5a4d777dcfa642.
---
docs/session-autostart.txt | 62 +++++++++++++
src/klauncher/CMakeLists.txt | 1 +
src/klauncher/autostart.cpp | 166 +++++++++++++++++++++++++++++++++++
src/klauncher/autostart.h | 57 ++++++++++++
4 files changed, 286 insertions(+)
create mode 100644 docs/session-autostart.txt
create mode 100644 src/klauncher/autostart.cpp
create mode 100644 src/klauncher/autostart.h
diff --git a/docs/session-autostart.txt b/docs/session-autostart.txt
new file mode 100644
index 0000000..bbfd036
--- /dev/null
+++ b/docs/session-autostart.txt
@@ -0,0 +1,62 @@
+KDE SESSION AUTOSTART
+=====================
+
+KDE session startup occurs in the following sequence:
+
+ Window manager startup
+ Autostart phase 1
+ Session restoration
+ Autostart phase 2
+
+Applications may be scheduled to be automatically run at KDE session startup
+in either autostart phase 1 or phase 2. Autostart phase 1 is the original
+autostart phase; phase 2 was introduced in KDE 3. To run in either phase, an
+application's .desktop file must be located in a KDE autostart directory such
+as $KDEDIR/share/autostart or $KDEHOME/share/autostart. The .desktop file can
+contain the following optional entries to control its autostart:
+
+ X-KDE-autostart-condition = rcfile:group:entry:default
+
+ rcfile = name of a config file (including path if necessary)
+ group = name of a group within the config file
+ entry = name of a boolean entry within the group
+ default = true or false
+
+ Starts the application only if the specified boolean entry in the
+ specified config file has the value 'true'. If the specified entry is
+ missing from the config file, the application will only be started if
+ 'default' is 'true'.
+ If the entry is not within a group, the group entry can be left empty.
+
+ X-KDE-autostart-after = desktop_name
+
+ desktop_name = the name of another .desktop file excluding path and
+ the .desktop suffix. E.g. panel.desktop would appear
+ as 'X-KDE-autostart-after=panel'
+
+ Waits until the .desktop file specified by 'desktop_name' has been
+ autostarted. The entry is ignored if the specified application is not
+ scheduled in the same autostart phase as this .desktop file.
+
+ X-KDE-autostart-phase = phase
+
+ phase = 1 or 2
+
+ Starts the application in the autostart phase specified by 'phase'.
+ If this entry is missing or 'phase' < 1, 'phase' defaults to 1. If
+ 'phase' > 2 the application will not be autostarted since the specified
+ autostart phase will never be reached.
+
+
+ Hidden = true
+
+ Disables autostarting the application.
+
+
+KUniqueApplication and session restoration
+------------------------------------------
+
+If KUniqueApplication applications are autostarted before they are restored
+from the previous session, they will never see the session restoration command.
+So if you need to autostart a KUniqueApplication which may also be restored
+in session restoration, you should schedule it for autostart in phase 2.
diff --git a/src/klauncher/CMakeLists.txt b/src/klauncher/CMakeLists.txt
index b57eaea..5741e8d 100644
--- a/src/klauncher/CMakeLists.txt
+++ b/src/klauncher/CMakeLists.txt
@@ -2,6 +2,7 @@ set(klauncher_SRCS
klauncher.cpp
klauncher_main.cpp
klauncher_adaptor.cpp
+ autostart.cpp
../klauncher_cmds.cpp
${CMAKE_CURRENT_BINARY_DIR}/org.kde.KLauncher.xml # just so that it gets generated
)
diff --git a/src/klauncher/autostart.cpp b/src/klauncher/autostart.cpp
new file mode 100644
index 0000000..27f03be
--- /dev/null
+++ b/src/klauncher/autostart.cpp
@@ -0,0 +1,166 @@
+/*
+ * This file is part of the KDE libraries
+ * Copyright (c) 2001 Waldo Bastian <bastian@kde.org>
+ *
+ * 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 "autostart.h"
+
+#include <kautostart.h>
+#include <QStandardPaths>
+#include <QDir>
+
+class AutoStartItem
+{
+public:
+ QString name;
+ QString service;
+ QString startAfter;
+ int phase;
+};
+
+AutoStart::AutoStart()
+ : m_phase(-1), m_phasedone(false)
+{
+ m_startList = new AutoStartList;
+}
+
+AutoStart::~AutoStart()
+{
+ qDeleteAll(*m_startList);
+ m_startList->clear();
+ delete m_startList;
+}
+
+void
+AutoStart::setPhase(int phase)
+{
+ if (phase > m_phase) {
+ m_phase = phase;
+ m_phasedone = false;
+ }
+}
+
+void AutoStart::setPhaseDone()
+{
+ m_phasedone = true;
+}
+
+static QString extractName(QString path) // krazy:exclude=passbyvalue
+{
+ int i = path.lastIndexOf(QLatin1Char('/'));
+ if (i >= 0) {
+ path = path.mid(i + 1);
+ }
+ i = path.lastIndexOf(QLatin1Char('.'));
+ if (i >= 0) {
+ path = path.left(i);
+ }
+ return path;
+}
+
+void
+AutoStart::loadAutoStartList()
+{
+ // XDG autostart dirs
+
+ // Make unique list of relative paths
+ QStringList files;
+ const QStringList dirs = QStandardPaths::locateAll(QStandardPaths::GenericConfigLocation, QStringLiteral("autostart"), QStandardPaths::LocateDirectory);
+ for (const QString &dir : dirs) {
+ const QStringList fileNames = QDir(dir).entryList(QStringList() << QStringLiteral("*.desktop"));
+ for (const QString &file : fileNames) {
+ if (!files.contains(file)) {
+ files.append(file);
+ }
+ }
+ }
+
+ for (QStringList::ConstIterator it = files.constBegin(); it != files.constEnd(); ++it) {
+ KAutostart config(*it);
+ if (!config.autostarts(QStringLiteral("KDE"), KAutostart::CheckAll)) {
+ continue;
+ }
+
+ const QString file = QStandardPaths::locate(QStandardPaths::GenericConfigLocation, QStringLiteral("autostart/") + *it);
+ AutoStartItem *item = new AutoStartItem;
+ item->name = extractName(*it);
+ item->service = file;
+ item->startAfter = config.startAfter();
+ item->phase = config.startPhase();
+ if (item->phase < 0) {
+ item->phase = 0;
+ }
+ m_startList->append(item);
+ }
+}
+
+QString
+AutoStart::startService()
+{
+ if (m_startList->isEmpty()) {
+ return QString();
+ }
+
+ while (!m_started.isEmpty()) {
+
+ // Check for items that depend on previously started items
+ QString lastItem = m_started[0];
+ QMutableListIterator<AutoStartItem *> it(*m_startList);
+ while (it.hasNext()) {
+ AutoStartItem *item = it.next();
+ if (item->phase == m_phase
+ && item->startAfter == lastItem) {
+ m_started.prepend(item->name);
+ QString service = item->service;
+ it.remove();
+ delete item;
+ return service;
+ }
+ }
+ m_started.removeFirst();
+ }
+
+ // Check for items that don't depend on anything
+ AutoStartItem *item;
+ QMutableListIterator<AutoStartItem *> it(*m_startList);
+ while (it.hasNext()) {
+ item = it.next();
+ if (item->phase == m_phase
+ && item->startAfter.isEmpty()) {
+ m_started.prepend(item->name);
+ QString service = item->service;
+ it.remove();
+ delete item;
+ return service;
+ }
+ }
+
+ // Just start something in this phase
+ it = *m_startList;
+ while (it.hasNext()) {
+ item = it.next();
+ if (item->phase == m_phase) {
+ m_started.prepend(item->name);
+ QString service = item->service;
+ it.remove();
+ delete item;
+ return service;
+ }
+ }
+
+ return QString();
+}
diff --git a/src/klauncher/autostart.h b/src/klauncher/autostart.h
new file mode 100644
index 0000000..fd3071e
--- /dev/null
+++ b/src/klauncher/autostart.h
@@ -0,0 +1,57 @@
+/*
+ This file is part of the KDE libraries
+ Copyright (c) 2001 Waldo Bastian <bastian@kde.org>
+
+ 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.
+*/
+
+#ifndef _AUTOSTART_H_
+#define _AUTOSTART_H_
+
+#include <QStringList>
+
+class AutoStartItem;
+typedef QList<AutoStartItem *> AutoStartList;
+
+class AutoStart
+{
+public:
+ AutoStart();
+ ~AutoStart();
+
+ AutoStart(const AutoStart &) = delete;
+ AutoStart &operator=(const AutoStart &) = delete;
+
+ void loadAutoStartList();
+ QString startService();
+ void setPhase(int phase);
+ void setPhaseDone();
+ int phase() const
+ {
+ return m_phase;
+ }
+ bool phaseDone() const
+ {
+ return m_phasedone;
+ }
+
+private:
+ AutoStartList *m_startList;
+ QStringList m_started;
+ int m_phase;
+ bool m_phasedone;
+};
+
+#endif
--
2.26.2