ircignoresmanager.cpp
1 //------------------------------------------------------------------------------
2 // ircignoresmanager.cpp
3 //------------------------------------------------------------------------------
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 // 02110-1301 USA
19 //
20 //------------------------------------------------------------------------------
21 // Copyright (C) 2014 "Zalewa" <zalewapl@gmail.com>
22 //------------------------------------------------------------------------------
23 #include "ircignoresmanager.h"
24 #include "ui_ircignoresmanager.h"
25 
26 #include "gui/commongui.h"
27 #include "irc/configuration/chatnetworkscfg.h"
28 #include "irc/entities/ircnetworkentity.h"
29 #include "patternlist.h"
30 #include <QKeyEvent>
31 
32 DClass<IRCIgnoresManager> : public Ui::IRCIgnoresManager
33 {
34 public:
35  QString networkDescription;
36 };
37 
38 DPointered(IRCIgnoresManager)
39 
40 
41 IRCIgnoresManager::IRCIgnoresManager(QWidget *parent, const QString &networkDescription)
42  : QDialog(parent)
43 {
44  d->networkDescription = networkDescription;
45  d->setupUi(this);
47 
48  loadItems();
49 }
50 
51 IRCIgnoresManager::~IRCIgnoresManager()
52 {
53 }
54 
55 void IRCIgnoresManager::done(int result)
56 {
57  if (result == Accepted)
58  saveItems();
59  QDialog::done(result);
60 }
61 
62 void IRCIgnoresManager::keyPressEvent(QKeyEvent *event)
63 {
64  if (event->key() == Qt::Key_Delete)
65  deleteSelected();
66  QDialog::keyPressEvent(event);
67 }
68 
69 void IRCIgnoresManager::loadItems()
70 {
71  ChatNetworksCfg cfg;
72  IRCNetworkEntity network = cfg.network(d->networkDescription);
73  for (const auto &pattern : network.ignoredUsers())
74  {
75  QListWidgetItem *item = new QListWidgetItem(pattern.userPattern(), d->list);
76  item->setFlags(item->flags() | Qt::ItemIsEditable);
77  d->list->addItem(item);
78  }
79 }
80 
81 void IRCIgnoresManager::saveItems()
82 {
83  ChatNetworksCfg cfg;
84  IRCNetworkEntity network = cfg.network(d->networkDescription);
85  network.setIgnoredUsers(patterns());
86  cfg.replaceNetwork(network.description(), network, this);
87 }
88 
89 PatternList IRCIgnoresManager::patterns() const
90 {
91  PatternList result;
92  for (int row = 0; row < d->list->count(); ++row)
93  {
94  QString text = d->list->item(row)->text();
95  if (!text.trimmed().isEmpty())
96  result << Pattern(text, QRegularExpression::CaseInsensitiveOption, Pattern::Wildcard);
97  }
98  return result;
99 }
100 
101 void IRCIgnoresManager::deleteSelected()
102 {
103  QList<QListWidgetItem *> selection = d->list->selectedItems();
104  for (QListWidgetItem *item : selection)
105  {
106  d->list->removeItemWidget(item);
107  }
108  qDeleteAll(selection);
109 }