Messagelib

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

KDE's Doxygen guidelines are available online.