File: predicatesmonitor_p.cpp

package info (click to toggle)
plasma-workspace 4%3A6.3.6-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 104,900 kB
  • sloc: cpp: 125,434; xml: 31,579; python: 3,976; perl: 572; sh: 234; javascript: 74; ruby: 39; ansic: 13; makefile: 9
file content (89 lines) | stat: -rw-r--r-- 2,960 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
 * SPDX-FileCopyrightText: 2024 Bohdan Onofriichuk <bogdan.onofriuchuk@gmail.com>
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "predicatesmonitor_p.h"

#include "devicenotifier_debug.h"

#include <QDirIterator>
#include <QUrl>

#include <KConfigGroup>
#include <KDesktopFile>
#include <KDirWatch>
#include <qloggingcategory.h>

PredicatesMonitor::PredicatesMonitor(QObject *parent)
    : QObject(parent)
    , m_dirWatch(new KDirWatch(this))
{
    qCDebug(APPLETS::DEVICENOTIFIER) << "Begin initializing predicates monitor";
    const QStringList folders =
        QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, QStringLiteral("solid/actions"), QStandardPaths::LocateDirectory);

    for (const QString &folder : folders) {
        qCDebug(APPLETS::DEVICENOTIFIER) << "Predicates Monitor: add watched dir: " << folder;
        m_dirWatch->addDir(folder, KDirWatch::WatchFiles);
    }

    updatePredicates(QString());

    connect(m_dirWatch, &KDirWatch::created, this, &PredicatesMonitor::onPredicatesChanged);
    connect(m_dirWatch, &KDirWatch::deleted, this, &PredicatesMonitor::onPredicatesChanged);
    connect(m_dirWatch, &KDirWatch::dirty, this, &PredicatesMonitor::onPredicatesChanged);
    qCDebug(APPLETS::DEVICENOTIFIER) << "initializing predicates monitor ended";
}

PredicatesMonitor::~PredicatesMonitor()
{
}

std::shared_ptr<PredicatesMonitor> PredicatesMonitor::instance()
{
    static std::weak_ptr<PredicatesMonitor> s_clip;
    if (s_clip.expired()) {
        std::shared_ptr<PredicatesMonitor> ptr{new PredicatesMonitor};
        s_clip = ptr;
        return ptr;
    }
    return s_clip.lock();
}

const QHash<QString, Solid::Predicate> &PredicatesMonitor::predicates()
{
    return m_predicates;
}

void PredicatesMonitor::onPredicatesChanged(const QString &path)
{
    qCDebug(APPLETS::DEVICENOTIFIER) << "Predicates Monitor: predicates changed";
    updatePredicates(path);
    Q_EMIT predicatesChanged(m_predicates);
}

void PredicatesMonitor::updatePredicates(const QString &path)
{
    Q_UNUSED(path)
    qCDebug(APPLETS::DEVICENOTIFIER) << "Predicates Monitor: Begin updating predicates";
    m_predicates.clear();
    QStringList files;
    const QStringList dirs = QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, QStringLiteral("solid/actions"), QStandardPaths::LocateDirectory);
    for (const QString &dir : dirs) {
        QDirIterator it(dir, {QStringLiteral("*.desktop")});
        while (it.hasNext()) {
            files.prepend(it.next());
        }
    }
    for (const QString &path : std::as_const(files)) {
        KDesktopFile cfg(path);
        const QString string_predicate = cfg.desktopGroup().readEntry("X-KDE-Solid-Predicate");
        m_predicates.insert(QUrl(path).fileName(), Solid::Predicate::fromString(string_predicate));
    }

    qCDebug(APPLETS::DEVICENOTIFIER) << "Predicates Monitor: predicates updated";
}

#include "moc_predicatesmonitor_p.cpp"