loadgroupmenu.cpp 1.88 KB
Newer Older
1
/*
Laurent Montel's avatar
Laurent Montel committed
2
    SPDX-FileCopyrightText: 2020-2021 Laurent Montel <montel@kde.org>
Laurent Montel's avatar
Laurent Montel committed
3
4

    SPDX-License-Identifier: LGPL-2.0-or-later
5
6
7
8
9

*/

#include "loadgroupmenu.h"

Laurent Montel's avatar
Laurent Montel committed
10
11
#include <KLocalizedString>

12
13
14
#include <QDir>
#include <QStandardPaths>

15
16
17
LoadGroupMenu::LoadGroupMenu(QWidget *parent)
    : QMenu(parent)
{
Laurent Montel's avatar
Laurent Montel committed
18
    setTitle(i18n("Load Group"));
Laurent Montel's avatar
Laurent Montel committed
19
    init();
20
21
22
23
24
}

LoadGroupMenu::~LoadGroupMenu()
{
}
Laurent Montel's avatar
Laurent Montel committed
25

26
void LoadGroupMenu::refreshMenu()
Laurent Montel's avatar
Laurent Montel committed
27
{
28
29
30
    clear();
    init();
}
Laurent Montel's avatar
Laurent Montel committed
31

Laurent Montel's avatar
Laurent Montel committed
32
QString LoadGroupMenu::defaultWritableGroupPath()
Laurent Montel's avatar
Laurent Montel committed
33
34
35
36
{
    return QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation) + QLatin1String("/groups");
}

37
38
QStringList LoadGroupMenu::defaultReadableGroupPath() const
{
Laurent Montel's avatar
Laurent Montel committed
39
    return QStandardPaths::locateAll(QStandardPaths::AppLocalDataLocation, QLatin1String("/groups/"), QStandardPaths::LocateDirectory);
40
41
}

42
43
void LoadGroupMenu::init()
{
Laurent Montel's avatar
Laurent Montel committed
44
    // Load all ?
Laurent Montel's avatar
Laurent Montel committed
45
    mGroupNames.clear();
Laurent Montel's avatar
Laurent Montel committed
46
    const QStringList groupPath = defaultReadableGroupPath();
47
48
49
50
    if (groupPath.isEmpty()) {
        setEnabled(false);
        return;
    }
Laurent Montel's avatar
Laurent Montel committed
51
52
53
    for (const QString &dirName : groupPath) {
        QDir dir(dirName);
        mGroupNames = dir.entryList(QDir::Files | QDir::NoDotAndDotDot);
Laurent Montel's avatar
Laurent Montel committed
54
        for (const QString &file : qAsConst(mGroupNames)) {
Laurent Montel's avatar
Laurent Montel committed
55
56
57
58
59
60
61
62
            QAction *act = addAction(file);
            const QString fullPath = dirName + QLatin1Char('/') + file;
            connect(act, &QAction::triggered, this, [this, fullPath] {
                Q_EMIT loadGroupRequested(fullPath);
            });
        }
    }
    if (isEmpty()) {
Laurent Montel's avatar
Laurent Montel committed
63
64
65
        setEnabled(false);
        return;
    }
66
67
68
    addSeparator();
    QAction *manageGroup = addAction(i18n("Manage Group"));
    connect(manageGroup, &QAction::triggered, this, &LoadGroupMenu::manageGroupRequested);
Laurent Montel's avatar
Laurent Montel committed
69
}
Laurent Montel's avatar
Laurent Montel committed
70
71
72
73
74
75
76
77
78
79

QStringList LoadGroupMenu::groupNames() const
{
    return mGroupNames;
}

void LoadGroupMenu::setGroupNames(const QStringList &groupNames)
{
    mGroupNames = groupNames;
}