File indexing completed on 2024-05-12 05:52:36
0001 /* 0002 SPDX-FileCopyrightText: 2020-2024 Laurent Montel <montel@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 0006 */ 0007 0008 #include "groupmanagementwidget.h" 0009 #include "kdebugsettingsutil.h" 0010 0011 #include <KLocalizedString> 0012 #include <KMessageBox> 0013 0014 #include <QDir> 0015 #include <QFileDialog> 0016 #include <QInputDialog> 0017 #include <QListWidget> 0018 #include <QMenu> 0019 #include <QVBoxLayout> 0020 0021 GroupManagementWidget::GroupManagementWidget(QWidget *parent) 0022 : QWidget(parent) 0023 , mListWidget(new QListWidget(this)) 0024 { 0025 auto mainLayout = new QVBoxLayout(this); 0026 mainLayout->setContentsMargins({}); 0027 mainLayout->setObjectName(QLatin1StringView("mainLayout")); 0028 0029 mListWidget->setObjectName(QLatin1StringView("mListWidget")); 0030 mainLayout->addWidget(mListWidget); 0031 mListWidget->setSelectionMode(QAbstractItemView::ExtendedSelection); 0032 mListWidget->setContextMenuPolicy(Qt::CustomContextMenu); 0033 connect(mListWidget, &QListWidget::itemDoubleClicked, this, &GroupManagementWidget::renameGroup); 0034 connect(mListWidget, &QListWidget::customContextMenuRequested, this, &GroupManagementWidget::slotCustomContextMenu); 0035 init(); 0036 } 0037 0038 GroupManagementWidget::~GroupManagementWidget() = default; 0039 0040 void GroupManagementWidget::exportGroup(QListWidgetItem *item) 0041 { 0042 if (!item) { 0043 return; 0044 } 0045 const QString fullPath = item->data(FullPathRole).toString(); 0046 QFile f(fullPath); 0047 const QString newPath = QFileDialog::getSaveFileName(this, i18n("Export Group"), QDir::homePath() + QStringLiteral("/%1").arg(item->text())); 0048 if (!newPath.isEmpty()) { 0049 if (f.copy(newPath)) { 0050 KMessageBox::information(this, i18n("Group exported to %1", newPath), i18n("Export Group")); 0051 } else { 0052 KMessageBox::error(this, i18n("Impossible to export group \'%2\' to \'%1\'", newPath, item->text()), i18n("Export Group")); 0053 } 0054 } 0055 } 0056 0057 void GroupManagementWidget::renameGroup(QListWidgetItem *item) 0058 { 0059 if (!item) { 0060 return; 0061 } 0062 const QString fullPath = item->data(FullPathRole).toString(); 0063 const QString currentName = item->text(); 0064 QFile f(fullPath); 0065 const QString newName = QInputDialog::getText(this, i18n("Rename Group"), i18n("New Name:")); 0066 const QString newNameTrimmed = newName.trimmed(); 0067 if (!newNameTrimmed.isEmpty() && (currentName != newNameTrimmed)) { 0068 const QString newFullPath{KDebugSettingsUtil::defaultWritableGroupPath() + QLatin1Char('/') + newNameTrimmed}; 0069 if (!f.rename(newFullPath)) { 0070 KMessageBox::error(this, i18n("Impossible to rename group as \'%1\'.", newNameTrimmed), i18n("Rename Group")); 0071 } else { 0072 item->setText(newNameTrimmed); 0073 item->setData(FullPathRole, newFullPath); 0074 Q_EMIT groupsChanged(); 0075 } 0076 } 0077 } 0078 0079 void GroupManagementWidget::slotCustomContextMenu() 0080 { 0081 const auto items = mListWidget->selectedItems(); 0082 if (!items.isEmpty()) { 0083 QMenu menu(this); 0084 if (mListWidget->selectedItems().count() == 1) { 0085 const auto item = items.at(0); 0086 menu.addAction(QIcon::fromTheme(QStringLiteral("edit")), i18n("Rename Group..."), this, [this, item]() { 0087 renameGroup(item); 0088 }); 0089 menu.addSeparator(); 0090 menu.addAction(QIcon::fromTheme(QStringLiteral("document-export")), i18n("Export Group..."), this, [this, item]() { 0091 exportGroup(item); 0092 }); 0093 menu.addSeparator(); 0094 } 0095 menu.addAction(QIcon::fromTheme(QStringLiteral("edit-delete")), i18n("Remove Groups"), this, [this, items]() { 0096 for (auto item : items) { 0097 const QString fullPath = item->data(FullPathRole).toString(); 0098 QFile f(fullPath); 0099 if (!f.remove()) { 0100 KMessageBox::error(this, i18n("Impossible to remove \'%1\'", fullPath), i18n("Remove Group")); 0101 } 0102 delete item; 0103 } 0104 Q_EMIT groupsChanged(); 0105 }); 0106 menu.exec(QCursor::pos()); 0107 } 0108 } 0109 0110 void GroupManagementWidget::init() 0111 { 0112 const QStringList groups = KDebugSettingsUtil::groupFileList(); 0113 if (!groups.isEmpty()) { 0114 const QString groupPath = KDebugSettingsUtil::defaultWritableGroupPath(); 0115 for (const QString &file : groups) { 0116 auto item = new QListWidgetItem(file, mListWidget); 0117 const QString fullPath = groupPath + QLatin1Char('/') + file; 0118 item->setData(FullPathRole, fullPath); 0119 } 0120 } 0121 } 0122 0123 #include "moc_groupmanagementwidget.cpp"