Messagelib

remotecontentconfigurewidget.cpp
1/*
2 SPDX-FileCopyrightText: 2020-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "remotecontentconfigurewidget.h"
8#include "remotecontentdialog.h"
9#include "remotecontentmanager.h"
10#include "remotecontentstatustypecombobox.h"
11#include <KLineEditEventHandler>
12#include <KLocalizedString>
13#include <KMessageBox>
14#include <KTreeWidgetSearchLine>
15#include <QHeaderView>
16#include <QMenu>
17#include <QPointer>
18#include <QTreeWidget>
19#include <QVBoxLayout>
20
21using namespace MessageViewer;
22
23RemoteContentWidgetItem::RemoteContentWidgetItem(QTreeWidget *parent)
24 : QTreeWidgetItem(parent)
25 , mStatusTypeCombobox(new RemoteContentStatusTypeComboBox)
26{
27 treeWidget()->setItemWidget(this, ColumnType::RuleType, mStatusTypeCombobox);
28}
29
30RemoteContentWidgetItem::~RemoteContentWidgetItem() = default;
31
32void RemoteContentWidgetItem::setStatus(MessageViewer::RemoteContentInfo::RemoteContentInfoStatus type)
33{
34 mStatusTypeCombobox->setStatus(type);
35}
36
37RemoteContentInfo::RemoteContentInfoStatus RemoteContentWidgetItem::status() const
38{
39 return mStatusTypeCombobox->status();
40}
41
42RemoteContentConfigureWidget::RemoteContentConfigureWidget(QWidget *parent)
43 : QWidget(parent)
44 , mTreeWidget(new QTreeWidget(this))
45{
46 auto mainLayout = new QVBoxLayout(this);
47 mainLayout->setObjectName(QLatin1StringView("mainLayout"));
48 mainLayout->setContentsMargins({});
49
50 mTreeWidget->setObjectName(QLatin1StringView("treewidget"));
51 mTreeWidget->setRootIsDecorated(false);
52 mTreeWidget->setHeaderLabels({i18n("Domain"), i18n("Status")});
53 mTreeWidget->setContextMenuPolicy(Qt::CustomContextMenu);
54 mTreeWidget->setAlternatingRowColors(true);
55 mTreeWidget->header()->setSectionResizeMode(QHeaderView::ResizeToContents);
56
57 auto searchLineEdit = new KTreeWidgetSearchLine(this, mTreeWidget);
58 searchLineEdit->setObjectName(QLatin1StringView("searchlineedit"));
59 searchLineEdit->setClearButtonEnabled(true);
60 mainLayout->addWidget(searchLineEdit);
62
63 mainLayout->addWidget(mTreeWidget);
64 connect(mTreeWidget, &QTreeWidget::customContextMenuRequested, this, &RemoteContentConfigureWidget::slotCustomContextMenuRequested);
65 connect(mTreeWidget, &QTreeWidget::itemDoubleClicked, this, [this](QTreeWidgetItem *item) {
66 if (item) {
67 auto rulesItem = static_cast<RemoteContentWidgetItem *>(item);
68 modifyRemoteContent(rulesItem);
69 }
70 });
71 readSettings();
72}
73
74RemoteContentConfigureWidget::~RemoteContentConfigureWidget() = default;
75
76void RemoteContentConfigureWidget::slotCustomContextMenuRequested(const QPoint &pos)
77{
78 QTreeWidgetItem *item = mTreeWidget->itemAt(pos);
79 QMenu menu(this);
80 menu.addAction(QIcon::fromTheme(QStringLiteral("list-add")), i18n("Add..."), this, &RemoteContentConfigureWidget::slotAdd);
81 if (item) {
82 menu.addSeparator();
83 menu.addAction(QIcon::fromTheme(QStringLiteral("document-edit")), i18n("Modify..."), this, [this, item]() {
84 modifyRemoteContent(static_cast<RemoteContentWidgetItem *>(item));
85 });
86 menu.addSeparator();
87 menu.addAction(QIcon::fromTheme(QStringLiteral("edit-delete")), i18n("Remove Rule"), this, [this, item]() {
89 i18n("Do you want to delete this rule '%1'?", item->text(0)),
90 i18nc("@title:window", "Delete Rule"),
93 if (answer == KMessageBox::ButtonCode::PrimaryAction) {
94 delete item;
95 }
96 });
97 }
98 menu.exec(QCursor::pos());
99}
100
101void RemoteContentConfigureWidget::modifyRemoteContent(RemoteContentWidgetItem *rulesItem)
102{
103 if (!rulesItem) {
104 return;
105 }
107 dlg->setWindowTitle(i18nc("@title:window", "Edit Remote Content"));
109 info.setUrl(rulesItem->text(0));
110 info.setStatus(rulesItem->status());
111 dlg->setInfo(info);
112 if (dlg->exec()) {
113 info = dlg->info();
114 RemoteContentManager::self()->addRemoteContent(info);
115 fillContentInfo(info, rulesItem);
116 }
117 delete dlg;
118}
119
120void RemoteContentConfigureWidget::slotAdd()
121{
123 dlg->setWindowTitle(i18nc("@title:window", "Add Remote Content"));
124 if (dlg->exec()) {
125 const auto info = dlg->info();
126 const int count = mTreeWidget->topLevelItemCount();
127 bool isUnique = true;
128 for (int i = 0; i < count; ++i) {
129 const auto item = mTreeWidget->topLevelItem(i);
130 if (item->text(0) == info.url()) {
131 isUnique = false;
132 KMessageBox::error(this, i18n("An entry already defines this url. Please modify it."), i18nc("@title:window", "Add new Url"));
133 break;
134 }
135 }
136 if (isUnique) {
137 RemoteContentManager::self()->addRemoteContent(info);
138 insertRemoteContentInfo(info);
139 }
140 }
141 delete dlg;
142}
143
144void RemoteContentConfigureWidget::saveSettings()
145{
147 const int count = mTreeWidget->topLevelItemCount();
148 lst.reserve(count);
149 for (int i = 0; i < count; ++i) {
150 const auto item = static_cast<RemoteContentWidgetItem *>(mTreeWidget->topLevelItem(i));
152 info.setUrl(item->text(0));
153 info.setStatus(item->status());
154 lst.append(std::move(info));
155 }
156 RemoteContentManager::self()->setRemoveContentInfo(lst);
157}
158
159void RemoteContentConfigureWidget::readSettings()
160{
161 const QList<RemoteContentInfo> remoteContentInfos = RemoteContentManager::self()->removeContentInfo();
162 for (const RemoteContentInfo &info : remoteContentInfos) {
163 insertRemoteContentInfo(info);
164 }
165 mTreeWidget->setSortingEnabled(true);
166 mTreeWidget->header()->setSortIndicatorShown(true);
167 mTreeWidget->header()->setSectionsClickable(true);
168 mTreeWidget->sortByColumn(0, Qt::AscendingOrder);
169}
170
171void RemoteContentConfigureWidget::insertRemoteContentInfo(const RemoteContentInfo &info)
172{
173 auto item = new RemoteContentWidgetItem(mTreeWidget);
174 fillContentInfo(info, item);
175}
176
177void RemoteContentConfigureWidget::fillContentInfo(const RemoteContentInfo &info, RemoteContentWidgetItem *item)
178{
179 item->setText(0, info.url());
180 item->setToolTip(0, info.url());
181 item->setStatus(info.status());
182}
183
184#include "moc_remotecontentconfigurewidget.cpp"
The RemoteContentDialog class.
The RemoteContentInfo class.
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
void catchReturnKey(QObject *lineEdit)
ButtonCode warningTwoActions(QWidget *parent, const QString &text, const QString &title, const KGuiItem &primaryAction, const KGuiItem &secondaryAction, const QString &dontAskAgainName=QString(), Options options=Options(Notify|Dangerous))
void error(QWidget *parent, const QString &text, const QString &title, const KGuiItem &buttonOk, Options options=Notify)
KGuiItem cancel()
KGuiItem del()
QPoint pos()
void setSectionsClickable(bool clickable)
void setSortIndicatorShown(bool show)
QIcon fromTheme(const QString &name)
void append(const T &value)
void reserve(int alloc)
T qobject_cast(QObject *object)
CustomContextMenu
AscendingOrder
void sortByColumn(int column)
QHeaderView * header() const const
void setSortingEnabled(bool enable)
QTreeWidgetItem * itemAt(const QPoint &p) const const
void itemDoubleClicked(QTreeWidgetItem *item, int column)
QTreeWidgetItem * topLevelItem(int index) const const
void setText(int column, const QString &text)
void setToolTip(int column, const QString &toolTip)
QString text(int column) const const
int type() const const
void customContextMenuRequested(const QPoint &pos)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sun Feb 25 2024 18:37:31 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.