File: sessionfilestracker.cpp

package info (click to toggle)
kdevelop 4%3A24.12.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 71,888 kB
  • sloc: cpp: 290,869; python: 3,626; javascript: 3,518; sh: 1,316; ansic: 703; xml: 401; php: 95; lisp: 66; makefile: 31; sed: 12
file content (181 lines) | stat: -rw-r--r-- 4,856 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
    SPDX-FileCopyrightText: 2020 Friedrich W. H. Kossebau <kossebau@kde.org>

    SPDX-License-Identifier: LGPL-2.0-or-later
*/

#include "sessionfilestracker.h"

// library
#include <kdevelopsessionsobserver.h>
// KF
#include <KDirWatch>
#include <KConfig>
#include <KConfigGroup>
// Qt
#include <QFileInfo>
#include <QDir>
#include <QStandardPaths>
#include <QUuid>
#include <QMutexLocker>
#include <QMetaObject>
#include <QCoreApplication>
// Std
#include <algorithm>

namespace {
namespace Strings {
QString sessionConfigFileName() { return QStringLiteral("sessionrc"); }
}
}

Q_GLOBAL_STATIC(SessionFilesTracker, s_SessionFilesTrackerInstance)

SessionFilesTracker *SessionFilesTracker::instance()
{
    return s_SessionFilesTrackerInstance();
}

static void setSessionDataList(QObject* observer, const QVector<KDevelopSessionData>& sessionDataList)
{
    QMetaObject::invokeMethod(observer, "setSessionDataList", Qt::AutoConnection,
                              Q_ARG(QVector<KDevelopSessionData>, sessionDataList));
}

static void cleanupSessionFilesTracker()
{
    if (s_SessionFilesTrackerInstance.exists()) {
        s_SessionFilesTrackerInstance->cleanup();
    }
}


SessionFilesTracker::SessionFilesTracker()
    : QObject()
    , m_dirWatch(new KDirWatch(this))
{
    // KDirWatch might have QFileSystemWatcher,
    // which wants to be deleted before qApp is gone - bug 261541
    qAddPostRoutine(cleanupSessionFilesTracker);

    m_sessionDir = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QLatin1String("/kdevelop/sessions");
    m_dirWatch->stopScan();
    m_dirWatch->addDir(m_sessionDir, KDirWatch::WatchSubDirs);

    connect(m_dirWatch, &KDirWatch::dirty, this, &SessionFilesTracker::sessionSourceChanged);

    updateSessions();
}

SessionFilesTracker::~SessionFilesTracker() = default;

void SessionFilesTracker::cleanup()
{
    delete m_dirWatch;
    m_dirWatch = nullptr;
}


void SessionFilesTracker::registerObserver(QObject* observer)
{
    if (!qobject_cast<KDevelopSessionsObserver*>(observer)) {
        return;
    }

    QMutexLocker lock(&m_mutex);
    const bool isFirstObserver = m_observers.isEmpty();

    m_observers.append(observer);
    setSessionDataList(observer, m_sessionDataList);

    if (isFirstObserver) {
        m_dirWatch->startScan(true);
    }
}

void SessionFilesTracker::unregisterObserver(QObject* observer)
{
    if (!qobject_cast<KDevelopSessionsObserver*>(observer)) {
        return;
    }

    QMutexLocker lock(&m_mutex);
    m_observers.removeOne(observer);

    if (m_observers.isEmpty()) {
        m_dirWatch->stopScan();
    }
}

QVector<KDevelopSessionData> SessionFilesTracker::readSessionDataList() const
{
    QVector<KDevelopSessionData> sessions;

    QDir sessionBaseDir(m_sessionDir);
    const auto dirEntries = sessionBaseDir.entryList(QDir::Dirs);
    sessions.reserve(dirEntries.size());
    for (const QString& sessionDirName : dirEntries) {
        if (QUuid(sessionDirName).isNull()) {
            continue;
        }

        QDir sessionDir(sessionBaseDir.absoluteFilePath(sessionDirName));
        const QString sessionConfigFilePath = sessionDir.filePath(Strings::sessionConfigFileName());
        if (!QFile::exists(sessionConfigFilePath)) {
            continue;
        }

        KConfig sessionDataStorage(sessionConfigFilePath, KConfig::SimpleConfig);
        const KConfigGroup mainSessionData = sessionDataStorage.group(QString());

        const KDevelopSessionData sessionData {
            sessionDirName,
            mainSessionData.readEntry("SessionName"),
            mainSessionData.readEntry("SessionPrettyContents"),
        };

        sessions.append(sessionData);
    }
    sessions.squeeze();
    std::sort(sessions.begin(), sessions.end(), [](const KDevelopSessionData& a, const KDevelopSessionData& b) {
        return a.id < b.id;   
    });

    return sessions;
}

void SessionFilesTracker::sessionSourceChanged(const QString& path)
{
    // session data is not changed too often, so we are fine to simply rescan all the data
    // in case any relevant files (or the main dir) have been touched

    // session dirs removed or added?
    if (m_sessionDir == path) {
        updateSessions();
    } else {
        // session config file?
        QFileInfo pathInfo(path);
        if (pathInfo.fileName() == Strings::sessionConfigFileName()) {
            updateSessions();
        }
    }
}

void SessionFilesTracker::updateSessions()
{
    QMutexLocker lock(&m_mutex);

    const auto newSessionDataList = readSessionDataList();

    if (m_sessionDataList == newSessionDataList) {
        return;
    }

    m_sessionDataList = newSessionDataList;

    for (auto* observer : std::as_const(m_observers)) {
        setSessionDataList(observer, m_sessionDataList);
    }
}

#include "moc_sessionfilestracker.cpp"