File indexing completed on 2022-09-27 16:31:46

0001 /*
0002     SPDX-FileCopyrightText: 2020-2022 Laurent Montel <montel@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 
0006 */
0007 
0008 #include "loadgroupmenu.h"
0009 
0010 #include <KLocalizedString>
0011 
0012 #include <QDir>
0013 #include <QStandardPaths>
0014 
0015 LoadGroupMenu::LoadGroupMenu(QWidget *parent)
0016     : QMenu(parent)
0017 {
0018     setTitle(i18n("Load Group"));
0019     init();
0020 }
0021 
0022 LoadGroupMenu::~LoadGroupMenu() = default;
0023 
0024 void LoadGroupMenu::refreshMenu()
0025 {
0026     clear();
0027     init();
0028 }
0029 
0030 QString LoadGroupMenu::defaultWritableGroupPath()
0031 {
0032     return QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation) + QLatin1String("/groups");
0033 }
0034 
0035 QStringList LoadGroupMenu::defaultReadableGroupPath() const
0036 {
0037     return QStandardPaths::locateAll(QStandardPaths::AppLocalDataLocation, QStringLiteral("/groups/"), QStandardPaths::LocateDirectory);
0038 }
0039 
0040 void LoadGroupMenu::init()
0041 {
0042     // Load all ?
0043     mGroupNames.clear();
0044     const QStringList groupPath = defaultReadableGroupPath();
0045     if (groupPath.isEmpty()) {
0046         setEnabled(false);
0047         return;
0048     }
0049     for (const QString &dirName : groupPath) {
0050         QDir dir(dirName);
0051         mGroupNames += dir.entryList(QDir::Files | QDir::NoDotAndDotDot);
0052         for (const QString &file : std::as_const(mGroupNames)) {
0053             QAction *act = addAction(file);
0054             const QString fullPath = dirName + QLatin1Char('/') + file;
0055             connect(act, &QAction::triggered, this, [this, fullPath] {
0056                 Q_EMIT loadGroupRequested(fullPath);
0057             });
0058         }
0059     }
0060     if (isEmpty()) {
0061         setEnabled(false);
0062         return;
0063     }
0064     addSeparator();
0065     QAction *manageGroup = addAction(i18n("Manage Group"));
0066     connect(manageGroup, &QAction::triggered, this, &LoadGroupMenu::manageGroupRequested);
0067 }
0068 
0069 QStringList LoadGroupMenu::groupNames() const
0070 {
0071     return mGroupNames;
0072 }
0073 
0074 void LoadGroupMenu::setGroupNames(const QStringList &groupNames)
0075 {
0076     mGroupNames = groupNames;
0077 }