|
| 1 | +/* |
| 2 | + SPDX-FileCopyrightText: 2001 Waldo Bastian <[email protected]> |
| 3 | +
|
| 4 | + SPDX-License-Identifier: LGPL-2.0-only |
| 5 | +*/ |
| 6 | + |
| 7 | +#include "autostart.h" |
| 8 | + |
| 9 | +#include "../plasmaautostart/plasmaautostart.h" |
| 10 | + |
| 11 | +#include <QDir> |
| 12 | +#include <QHash> |
| 13 | +#include <QStandardPaths> |
| 14 | + |
| 15 | +AutoStart::AutoStart() |
| 16 | + : m_phase(-1) |
| 17 | + , m_phasedone(false) |
| 18 | +{ |
| 19 | + loadAutoStartList(); |
| 20 | +} |
| 21 | + |
| 22 | +AutoStart::~AutoStart() |
| 23 | +{ |
| 24 | +} |
| 25 | + |
| 26 | +void AutoStart::setPhase(int phase) |
| 27 | +{ |
| 28 | + if (phase > m_phase) { |
| 29 | + m_phase = phase; |
| 30 | + m_phasedone = false; |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +void AutoStart::setPhaseDone() |
| 35 | +{ |
| 36 | + m_phasedone = true; |
| 37 | +} |
| 38 | + |
| 39 | +static QString extractName(QString path) // krazy:exclude=passbyvalue |
| 40 | +{ |
| 41 | + int i = path.lastIndexOf(QLatin1Char('/')); |
| 42 | + if (i >= 0) { |
| 43 | + path = path.mid(i + 1); |
| 44 | + } |
| 45 | + i = path.lastIndexOf(QLatin1Char('.')); |
| 46 | + if (i >= 0) { |
| 47 | + path.truncate(i); |
| 48 | + } |
| 49 | + return path; |
| 50 | +} |
| 51 | + |
| 52 | +void AutoStart::loadAutoStartList() |
| 53 | +{ |
| 54 | + // XDG autostart dirs |
| 55 | + |
| 56 | + // Make unique list of relative paths |
| 57 | + QHash<QString, QString> files; |
| 58 | + const QStringList dirs = QStandardPaths::locateAll(QStandardPaths::GenericConfigLocation, QStringLiteral("autostart"), QStandardPaths::LocateDirectory); |
| 59 | + for (const QString &dir : dirs) { |
| 60 | + const QDir d(dir); |
| 61 | + const QStringList fileNames = d.entryList(QStringList() << QStringLiteral("*.desktop")); |
| 62 | + for (const QString &file : fileNames) { |
| 63 | + if (!files.contains(file)) { |
| 64 | + files.insert(file, d.absoluteFilePath(file)); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + for (auto it = files.constBegin(); it != files.constEnd(); ++it) { |
| 70 | + PlasmaAutostart config(*it); |
| 71 | + if (!config.autostarts(QStringLiteral("KDE"), PlasmaAutostart::CheckAll)) { |
| 72 | + continue; |
| 73 | + } |
| 74 | + |
| 75 | + AutoStartItem item; |
| 76 | + item.service = *it; |
| 77 | + item.name = extractName(it.key()); |
| 78 | + item.startAfter = config.startAfter(); |
| 79 | + item.phase = qMax(PlasmaAutostart::BaseDesktop, config.startPhase()); |
| 80 | + m_startList.append(item); |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +QString AutoStart::startService() |
| 85 | +{ |
| 86 | + if (m_startList.isEmpty()) { |
| 87 | + return QString(); |
| 88 | + } |
| 89 | + |
| 90 | + while (!m_started.isEmpty()) { |
| 91 | + // Check for items that depend on previously started items |
| 92 | + QString lastItem = m_started[0]; |
| 93 | + QMutableVectorIterator<AutoStartItem> it(m_startList); |
| 94 | + while (it.hasNext()) { |
| 95 | + const auto &item = it.next(); |
| 96 | + if (item.phase == m_phase && item.startAfter == lastItem) { |
| 97 | + m_started.prepend(item.name); |
| 98 | + QString service = item.service; |
| 99 | + it.remove(); |
| 100 | + return service; |
| 101 | + } |
| 102 | + } |
| 103 | + m_started.removeFirst(); |
| 104 | + } |
| 105 | + |
| 106 | + // Check for items that don't depend on anything |
| 107 | + QMutableVectorIterator<AutoStartItem> it(m_startList); |
| 108 | + while (it.hasNext()) { |
| 109 | + const auto &item = it.next(); |
| 110 | + if (item.phase == m_phase && item.startAfter.isEmpty()) { |
| 111 | + m_started.prepend(item.name); |
| 112 | + QString service = item.service; |
| 113 | + it.remove(); |
| 114 | + return service; |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + // Just start something in this phase |
| 119 | + it = m_startList; |
| 120 | + while (it.hasNext()) { |
| 121 | + const auto &item = it.next(); |
| 122 | + if (item.phase == m_phase) { |
| 123 | + m_started.prepend(item.name); |
| 124 | + QString service = item.service; |
| 125 | + it.remove(); |
| 126 | + return service; |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + return QString(); |
| 131 | +} |
| 132 | + |
| 133 | +QVector<AutoStartItem> AutoStart::startList() const |
| 134 | +{ |
| 135 | + QVector<AutoStartItem> ret; |
| 136 | + for (const auto &asi : m_startList) { |
| 137 | + if (asi.phase == m_phase) |
| 138 | + ret << asi; |
| 139 | + } |
| 140 | + return ret; |
| 141 | +} |
0 commit comments