File: checksetselectionmanager.cpp

package info (click to toggle)
kdevelop 4%3A5.6.2-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 57,892 kB
  • sloc: cpp: 278,773; javascript: 3,558; python: 3,385; sh: 1,317; ansic: 689; xml: 273; php: 95; makefile: 40; lisp: 13; sed: 12
file content (468 lines) | stat: -rw-r--r-- 17,812 bytes parent folder | download
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/*
 * This file is part of KDevelop
 *
 * Copyright 2010-2012,2020 Friedrich W. H. Kossebau <kossebau@kde.org>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */

#include "checksetselectionmanager.h"

// plugin
#include "checksetselectionlock.h"
#include "debug.h"
// KF
#include <KConfigGroup>
#include <KConfig>
#include <KDirWatch>
// Qt
#include <QFileInfo>
#include <QDir>
#include <QUuid>

namespace ClangTidy {

QStringList checkSetSelectionFileNameFilter() { return QStringList { QStringLiteral("*.kdevctcs"), QStringLiteral("*.kdevlock") }; }
inline QLatin1String checkSetSelectionFileSuffix() { return QLatin1String(".kdevctcs"); }
inline QLatin1String checkSetSelectionDirSubPath() { return QLatin1String("/kdevclangtidy/checksetselections"); }
inline QLatin1String defaultCheckSetSelectionFileSubPath() { return QLatin1String("/kdevclangtidy/defaultchecksetselection"); }


QVector<QString> lockedCheckSetSelectionIds(const CheckSetSelectionFileInfoLookup& checkSetSelectionFileInfoLookup)
{
    QVector<QString> result;

    for (auto it = checkSetSelectionFileInfoLookup.constBegin(), end = checkSetSelectionFileInfoLookup.constEnd();
         it != end; ++it) {
        if (it.value().isLocked()) {
            result.append(it.key());
        }
    }

    return result;
}

void updateLockStatus(CheckSetSelectionFileInfoLookup& checkSetSelectionFileInfoLookup,
                      const QVector<QString>& lockedCheckSetSelectionIds,
                      const QVector<QString>& unlockedCheckSetSelectionIds)
{
    if (lockedCheckSetSelectionIds.isEmpty() && unlockedCheckSetSelectionIds.isEmpty()) {
        return;
    }

    for (auto it = checkSetSelectionFileInfoLookup.begin(), end = checkSetSelectionFileInfoLookup.end();
         it != end; ++it) {
        bool isLocked;

        if (lockedCheckSetSelectionIds.contains(it.key())) {
            isLocked = true;
        } else if (unlockedCheckSetSelectionIds.contains(it.key())) {
            isLocked = false;
        } else {
            continue;
        }

        it.value().setLocked(isLocked);
    }
}

QString defaultCheckSetSelectionFilePath()
{
    return QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + defaultCheckSetSelectionFileSubPath();
}

QString checkSetSelectionFilePath(const QString& checkSetSelectionId)
{
    return QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation)
           + checkSetSelectionDirSubPath() + QLatin1Char('/') + checkSetSelectionId + checkSetSelectionFileSuffix();
}

QString checkSetSelectionFileName(const QString& checkSetSelectionId)
{
    return checkSetSelectionId + checkSetSelectionFileSuffix();
}

// TODO: add global lock
// TODO: make calls async
// TODO: only load checkset setlections on demand
CheckSetSelectionManager::CheckSetSelectionManager()
    : m_checkSetSelectionFileWatcher(new KDirWatch(this))
{
    connect(m_checkSetSelectionFileWatcher, &KDirWatch::dirty,
            this, &CheckSetSelectionManager::onCheckSetSelectionsFolderChanged);

    // get all folder where checkSetSelections could be stored
    const QStringList dataFolderPaths =
        QStandardPaths::standardLocations(QStandardPaths::GenericDataLocation);

    for (const QString& dataFolderPath : dataFolderPaths) {
        const QString checkSetSelectionFolderPath = dataFolderPath + checkSetSelectionDirSubPath();
        // watch folder for changes
        m_checkSetSelectionFileWatcher->addDir(checkSetSelectionFolderPath, KDirWatch::WatchDirOnly);

        // read current files
        onCheckSetSelectionsFolderChanged(checkSetSelectionFolderPath);
    }

    // default checkset selection
    // While there is no proper config syncing offer in the used frameworks, use a
    // single file with the id as content as workaround and watch for it changing
    auto* defaultCheckSetSelectionWatcher = new KDirWatch(this);
    connect(defaultCheckSetSelectionWatcher, &KDirWatch::created,
            this, &CheckSetSelectionManager::onDefaultCheckSetSelectionChanged);
    connect(defaultCheckSetSelectionWatcher, &KDirWatch::dirty,
            this, &CheckSetSelectionManager::onDefaultCheckSetSelectionChanged);
    const QString _defaultCheckSetSelectionFilePath = defaultCheckSetSelectionFilePath();

    defaultCheckSetSelectionWatcher->addFile(_defaultCheckSetSelectionFilePath);

    onDefaultCheckSetSelectionChanged(_defaultCheckSetSelectionFilePath);

    // report any problems with existing checkset selections?
}

CheckSetSelectionManager::~CheckSetSelectionManager() = default;

int CheckSetSelectionManager::checkSetSelectionsCount() const
{
    return m_checkSetSelections.count();
}

QVector<CheckSetSelection> CheckSetSelectionManager::checkSetSelections() const
{
    return m_checkSetSelections;
}

CheckSetSelection CheckSetSelectionManager::checkSetSelection(const QString& checkSetSelectionId) const
{
    CheckSetSelection result;

    for (const CheckSetSelection& checkSetSelection : m_checkSetSelections) {
        if (checkSetSelection.id() == checkSetSelectionId) {
            result = checkSetSelection;
            break;
        }
    }

    return result;
}

QString CheckSetSelectionManager::defaultCheckSetSelectionId() const
{
    return m_defaultCheckSetSelectionId;
}

CheckSetSelection CheckSetSelectionManager::defaultCheckSetSelection() const
{
    return checkSetSelection(m_defaultCheckSetSelectionId);
}

bool CheckSetSelectionManager::isCheckSetSelectionLocked(const QString& checkSetSelectionId) const
{
    bool result = false;

    // search in all folders for the info
    for (const CheckSetSelectionFileInfoLookup& checkSetSelectionFileInfoLookup : m_checkSetSelectionFileInfoLookupPerFolder) {
        CheckSetSelectionFileInfoLookup::ConstIterator it =
            checkSetSelectionFileInfoLookup.find(checkSetSelectionId);
        if (it != checkSetSelectionFileInfoLookup.constEnd()) {
            result = it->isLocked();
            break;
        }
    }

    return result;
}

void CheckSetSelectionManager::saveCheckSetSelections(QVector<CheckSetSelection>& checkSetSelections)
{
    // TODO: do not save if locked by someone else -> needs passing of our lock? or just registering our own and check?
    // create and set unique id
    std::for_each(checkSetSelections.begin(), checkSetSelections.end(), [this](CheckSetSelection& checkSetSelection) {
        const QString checkSetSelectionId = checkSetSelection.id();

        bool needsId = true;
        if (!checkSetSelectionId.isEmpty()) {
            // already existing?
            auto hasCheckSetSelectionId = [&checkSetSelectionId] (const CheckSetSelection& existingProfile) {
                return (checkSetSelectionId == existingProfile.id());
            };
            if (std::any_of(m_checkSetSelections.constBegin(), m_checkSetSelections.constEnd(), hasCheckSetSelectionId)) {
                needsId = false;
            }
        }

        // set new uuid for non-existing
        if (needsId) {
            checkSetSelection.setId(QUuid::createUuid().toString());
        }

        saveCheckSetSelection(checkSetSelection);
    });
}

void CheckSetSelectionManager::removeCheckSetSelections(const QVector<QString>& checkSetSelectionIds)
{
    for (const QString& checkSetSelectionId : checkSetSelectionIds) {
        removeCheckSetSelection(checkSetSelectionId);
    }
}

void CheckSetSelectionManager::setDefaultCheckSetSelection(const QString& checkSetSelectionId)
{
    QFile defaultCheckSetSelectionFile(defaultCheckSetSelectionFilePath());
    defaultCheckSetSelectionFile.open(QIODevice::WriteOnly);

    defaultCheckSetSelectionFile.write(checkSetSelectionId.toUtf8());
    defaultCheckSetSelectionFile.close();
}

CheckSetSelectionLock CheckSetSelectionManager::createLock(const QString& checkSetSelectionId)
{
    const QString checkSetSelectionFilePath = filePathOfCheckSetSelection(checkSetSelectionId);

    return CheckSetSelectionLock(checkSetSelectionFilePath, checkSetSelectionId);
}

CheckSetSelection CheckSetSelectionManager::loadCheckSetSelection(const QString& absoluteFilePath) const
{
    CheckSetSelection result;

    KConfig configFile(absoluteFilePath, KConfig::SimpleConfig);

    // check version
    KConfigGroup formatConfigGroup = configFile.group("KDEVCTCS");
    const QString formatVersion = formatConfigGroup.readEntry("Version");
    if (!formatVersion.startsWith(QLatin1String("1."))) {
        return result;
    }

    result.setId(QFileInfo(absoluteFilePath).baseName());

    KConfigGroup generalConfigGroup = configFile.group("General");
    result.setName(generalConfigGroup.readEntry("Name"));

    KConfigGroup layoutConfigGroup = configFile.group("Checks");
    result.setSelection(layoutConfigGroup.readEntry("Selection", QString()));

    return result;
}

void CheckSetSelectionManager::saveCheckSetSelection(const CheckSetSelection& checkSetSelection) const
{
    const QString fileName = checkSetSelectionFilePath(checkSetSelection.id());
    KConfig configFile(fileName, KConfig::SimpleConfig);

    KConfigGroup formatConfigGroup = configFile.group("KDEVCTCS");
    formatConfigGroup.writeEntry("Version", "1.0");

    KConfigGroup generalConfigGroup = configFile.group("General");
    generalConfigGroup.writeEntry("Name", checkSetSelection.name());

    KConfigGroup layoutConfigGroup = configFile.group("Checks");
    layoutConfigGroup.writeEntry("Selection", checkSetSelection.selectionAsString());
}

void CheckSetSelectionManager::removeCheckSetSelection(const QString& checkSetSelectionId)
{
    const QString filePath = filePathOfCheckSetSelection(checkSetSelectionId);
    if (!filePath.isEmpty()) {
        QFile::remove(filePath);
    }
}

QString CheckSetSelectionManager::filePathOfCheckSetSelection(const QString& checkSetSelectionId) const
{
    QString result;

    for (QHash<QString, CheckSetSelectionFileInfoLookup>::ConstIterator foldersIt =
             m_checkSetSelectionFileInfoLookupPerFolder.constBegin();
         foldersIt != m_checkSetSelectionFileInfoLookupPerFolder.constEnd() && result.isEmpty();
         ++foldersIt) {
        const CheckSetSelectionFileInfoLookup& fileInfoList = foldersIt.value();
        for (CheckSetSelectionFileInfoLookup::ConstIterator folderIt = fileInfoList.constBegin();
             folderIt != fileInfoList.constEnd();
             ++folderIt) {
            if (folderIt.key() == checkSetSelectionId) {
                result = foldersIt.key() + QLatin1Char('/') + checkSetSelectionFileName(checkSetSelectionId);
                break;
            }
        }
    }

    return result;
}

void CheckSetSelectionManager::onCheckSetSelectionsFolderChanged(const QString& checkSetSelectionFolderPath)
{
    CheckSetSelectionFileInfoLookup& checkSetSelectionFileInfoLookup =
        m_checkSetSelectionFileInfoLookupPerFolder[checkSetSelectionFolderPath];

    // TODO: reparse for new, removed and changed files
    // assume all are removed and unlocked in the beginning
    QVector<QString> removedCheckSetSelectionIds = checkSetSelectionFileInfoLookup.keys().toVector();
    QVector<CheckSetSelection> newCheckSetSelections;
    QVector<CheckSetSelection> changedCheckSetSelections;

    QVector<QString> newUnlockedCheckSetSelectionIds = lockedCheckSetSelectionIds(checkSetSelectionFileInfoLookup);
    QVector<QString> newLockedCheckSetSelectionIds;
    // iterate all files in folder
    const QFileInfoList checkSetSelectionFileInfoList =
        QDir(checkSetSelectionFolderPath).entryInfoList(checkSetSelectionFileNameFilter(), QDir::Files);

    for (const QFileInfo& checkSetSelectionFileInfo : checkSetSelectionFileInfoList) {
        // a lock file ?
        if (checkSetSelectionFileInfo.suffix() == QLatin1String("kdevlock")) {
            const QString lockedCheckSetSelectionId = checkSetSelectionFileInfo.baseName();
            // if not in old locks, is a new lock
            if (!newUnlockedCheckSetSelectionIds.removeOne(lockedCheckSetSelectionId)) {
                newLockedCheckSetSelectionIds.append(lockedCheckSetSelectionId);
            }
            continue;
        }

        // not a checkset file ?
        if (checkSetSelectionFileInfo.suffix() != QLatin1String("kdevctcs")) {
            continue;
        }

        // all other files assumed to be checkSetSelection files
        const QString checkSetSelectionId = checkSetSelectionFileInfo.baseName();
        // load file
        const CheckSetSelection checkSetSelection = loadCheckSetSelection(checkSetSelectionFileInfo.absoluteFilePath());
        // loading failed? Treat as not existing
        if (checkSetSelection.id().isEmpty()) {
            continue;
        }

        const CheckSetSelectionFileInfoLookup::Iterator infoIt =
            checkSetSelectionFileInfoLookup.find(checkSetSelectionId);
        const bool isKnown = (infoIt != checkSetSelectionFileInfoLookup.end());
        const QDateTime fileInfoLastModified = checkSetSelectionFileInfo.lastModified();
        // is known?
        if (isKnown) {
            removedCheckSetSelectionIds.removeOne(checkSetSelectionId);

            // check timestamp
            if (fileInfoLastModified == infoIt->lastModified()) {
                continue;
            }

            // update timestamp
            infoIt->setLastModified(fileInfoLastModified);
        } else {
            CheckSetSelectionFileInfo info(fileInfoLastModified, false);
            checkSetSelectionFileInfoLookup.insert(checkSetSelectionId, info);
        }

        if (isKnown) {
            auto it = std::find_if(m_checkSetSelections.begin(), m_checkSetSelections.end(),
                                   [&checkSetSelectionId](const CheckSetSelection& existingProfile) {
                return (existingProfile.id() == checkSetSelectionId);
            });
            if (it != m_checkSetSelections.end()) {
                *it = checkSetSelection;
            }
        } else {
            newCheckSetSelections.append(checkSetSelection);
        }
        changedCheckSetSelections.append(checkSetSelection);
    }

    // remove all removed checksets
    {
        auto isProfileToRemove = [&removedCheckSetSelectionIds](const CheckSetSelection& selection) {
            return removedCheckSetSelectionIds.contains(selection.id());
        };
        m_checkSetSelections.erase(std::remove_if(m_checkSetSelections.begin(), m_checkSetSelections.end(), isProfileToRemove),
                            m_checkSetSelections.end());
    }

    for (const QString& checkSetSelectionId : qAsConst(removedCheckSetSelectionIds)) {
        checkSetSelectionFileInfoLookup.remove(checkSetSelectionId);
        if (checkSetSelectionId == m_defaultCheckSetSelectionId) {
            m_defaultCheckSetSelectionId.clear();
        }
        // TODO: how to select new one?
    }

    // add new checksets
    m_checkSetSelections.append(newCheckSetSelections);
    // if there was no default checkset before, set default to first
    const bool isDefaultCheckSetSelectionChanged = (m_defaultCheckSetSelectionId.isEmpty() && !m_checkSetSelections.isEmpty());
    if (isDefaultCheckSetSelectionChanged) {
        m_defaultCheckSetSelectionId = m_checkSetSelections.at(0).id();
    }

    // update lock info
    updateLockStatus(checkSetSelectionFileInfoLookup, newLockedCheckSetSelectionIds, newUnlockedCheckSetSelectionIds);

    // signal changes
    if (!changedCheckSetSelections.isEmpty()) {
        emit checkSetSelectionsChanged(changedCheckSetSelections);
    }
    if (!removedCheckSetSelectionIds.isEmpty()) {
        emit checkSetSelectionsRemoved(removedCheckSetSelectionIds);
    }

    if (!newUnlockedCheckSetSelectionIds.isEmpty()) {
        emit checkSetSelectionsUnlocked(newUnlockedCheckSetSelectionIds);
    }
    if (!newLockedCheckSetSelectionIds.isEmpty()) {
        emit checkSetSelectionsLocked(newLockedCheckSetSelectionIds);
    }
    if (isDefaultCheckSetSelectionChanged) {
        emit defaultCheckSetSelectionChanged(m_defaultCheckSetSelectionId);
    }
}

void CheckSetSelectionManager::onDefaultCheckSetSelectionChanged(const QString& path)
{
    QFile defaultCheckSetSelectionFile(path);
    if (!defaultCheckSetSelectionFile.open(QIODevice::ReadOnly)) {
        qCDebug(KDEV_CLANGTIDY) << "Failed to open checkset selection file " << path;
        return;
    }

    const QByteArray fileContent = defaultCheckSetSelectionFile.readAll();
    const QString checkSetSelectionId = QString::fromUtf8(fileContent);
    defaultCheckSetSelectionFile.close();

    // no id set?
    if (checkSetSelectionId.isEmpty()) {
        return;
    }

    // no change?
    if (m_defaultCheckSetSelectionId == checkSetSelectionId) {
        return;
    }

    bool isExisting = false;
    for (const CheckSetSelection& checkSetSelection : qAsConst(m_checkSetSelections)) {
        if (checkSetSelection.id() == checkSetSelectionId) {
            isExisting = true;
            break;
        }
    }

    if (isExisting) {
        m_defaultCheckSetSelectionId = checkSetSelectionId;
        emit defaultCheckSetSelectionChanged(m_defaultCheckSetSelectionId);
    }
}

}