File indexing completed on 2022-11-29 20:24:01

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 "groupmanagementwidget.h"
0009 #include "loadgroupmenu.h"
0010 
0011 #include <KLocalizedString>
0012 #include <KMessageBox>
0013 
0014 #include <QDir>
0015 #include <QInputDialog>
0016 #include <QListWidget>
0017 #include <QVBoxLayout>
0018 
0019 GroupManagementWidget::GroupManagementWidget(QWidget *parent)
0020     : QWidget(parent)
0021     , mListWidget(new QListWidget(this))
0022 {
0023     auto mainLayout = new QVBoxLayout(this);
0024     mainLayout->setContentsMargins({});
0025     mainLayout->setObjectName(QStringLiteral("mainLayout"));
0026 
0027     mListWidget->setObjectName(QStringLiteral("mListWidget"));
0028     mainLayout->addWidget(mListWidget);
0029     mListWidget->setSelectionMode(QAbstractItemView::MultiSelection);
0030     mListWidget->setContextMenuPolicy(Qt::CustomContextMenu);
0031     connect(mListWidget, &QListWidget::itemDoubleClicked, this, &GroupManagementWidget::renameGroup);
0032     connect(mListWidget, &QListWidget::customContextMenuRequested, this, &GroupManagementWidget::slotCustomContextMenu);
0033     init();
0034 }
0035 
0036 GroupManagementWidget::~GroupManagementWidget() = default;
0037 
0038 void GroupManagementWidget::renameGroup(QListWidgetItem *item)
0039 {
0040     if (!item) {
0041         return;
0042     }
0043     const QString fullPath = item->data(FullPathRole).toString();
0044     QFile f(fullPath);
0045     const QString newName = QInputDialog::getText(this, i18n("Rename Group"), i18n("New Name:"));
0046     const QString newNameTrimmed = newName.trimmed();
0047     if (!newNameTrimmed.isEmpty()) {
0048         const QString newFullPath{LoadGroupMenu::defaultWritableGroupPath() + QLatin1Char('/') + newNameTrimmed};
0049         if (!f.rename(newFullPath)) {
0050             KMessageBox::error(this, i18n("Impossible to rename group as \'%1\'", newNameTrimmed), i18n("Rename Group"));
0051         } else {
0052             item->setText(newNameTrimmed);
0053             item->setData(FullPathRole, newFullPath);
0054             Q_EMIT groupsChanged();
0055         }
0056     }
0057 }
0058 
0059 void GroupManagementWidget::slotCustomContextMenu()
0060 {
0061     const auto items = mListWidget->selectedItems();
0062     if (!items.isEmpty()) {
0063         QMenu menu(this);
0064         if (mListWidget->selectedItems().count() == 1) {
0065             const auto item = items.at(0);
0066             menu.addAction(QIcon::fromTheme(QStringLiteral("edit")), i18n("Rename Groups"), this, [this, item]() {
0067                 renameGroup(item);
0068             });
0069             menu.addSeparator();
0070         }
0071         menu.addAction(QIcon::fromTheme(QStringLiteral("edit-delete")), i18n("Remove Groups"), this, [this, items]() {
0072             for (auto item : items) {
0073                 const QString fullPath = item->data(FullPathRole).toString();
0074                 QFile f(fullPath);
0075                 if (!f.remove()) {
0076                     KMessageBox::error(this, i18n("Impossible to remove \'%1\'", fullPath), i18n("Remove Group"));
0077                 }
0078                 delete item;
0079             }
0080             Q_EMIT groupsChanged();
0081         });
0082         menu.exec(QCursor::pos());
0083     }
0084 }
0085 
0086 void GroupManagementWidget::init()
0087 {
0088     const QString groupPath = LoadGroupMenu::defaultWritableGroupPath();
0089     if (groupPath.isEmpty()) {
0090         return;
0091     }
0092     QDir dir(groupPath);
0093     const QStringList groups = dir.entryList(QDir::Files | QDir::NoDotAndDotDot);
0094     if (groups.isEmpty()) {
0095         return;
0096     }
0097     for (const QString &file : groups) {
0098         auto item = new QListWidgetItem(file, mListWidget);
0099         const QString fullPath = groupPath + QLatin1Char('/') + file;
0100         item->setData(FullPathRole, fullPath);
0101     }
0102 }