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 "irc/configuration/chatnetworkscfg.h"
27 #include "irc/entities/ircnetworkentity.h"
28 #include "patternlist.h"
29 #include <QKeyEvent>
30 
31 DClass<IRCIgnoresManager> : public Ui::IRCIgnoresManager
32 {
33 public:
34  QString networkDescription;
35 };
36 
37 DPointered(IRCIgnoresManager)
38 
39 
40 IRCIgnoresManager::IRCIgnoresManager(QWidget *parent, const QString &networkDescription)
41  : QDialog(parent)
42 {
43  d->networkDescription = networkDescription;
44  d->setupUi(this);
45 
46  loadItems();
47 }
48 
49 IRCIgnoresManager::~IRCIgnoresManager()
50 {
51 }
52 
53 void IRCIgnoresManager::done(int result)
54 {
55  if (result == Accepted)
56  saveItems();
57  QDialog::done(result);
58 }
59 
60 void IRCIgnoresManager::keyPressEvent(QKeyEvent *event)
61 {
62  if (event->key() == Qt::Key_Delete)
63  deleteSelected();
64  QDialog::keyPressEvent(event);
65 }
66 
67 void IRCIgnoresManager::loadItems()
68 {
69  ChatNetworksCfg cfg;
70  IRCNetworkEntity network = cfg.network(d->networkDescription);
71  for (const QRegExp &pattern : network.ignoredUsers())
72  {
73  QListWidgetItem *item = new QListWidgetItem(pattern.pattern(), d->list);
74  item->setFlags(item->flags() | Qt::ItemIsEditable);
75  d->list->addItem(item);
76  }
77 }
78 
79 void IRCIgnoresManager::saveItems()
80 {
81  ChatNetworksCfg cfg;
82  IRCNetworkEntity network = cfg.network(d->networkDescription);
83  network.setIgnoredUsers(patterns());
84  cfg.replaceNetwork(network.description(), network, this);
85 }
86 
87 PatternList IRCIgnoresManager::patterns() const
88 {
89  PatternList result;
90  for (int row = 0; row < d->list->count(); ++row)
91  {
92  QString text = d->list->item(row)->text();
93  if (!text.trimmed().isEmpty())
94  result << QRegExp(text, Qt::CaseInsensitive, QRegExp::Wildcard);
95  }
96  return result;
97 }
98 
99 void IRCIgnoresManager::deleteSelected()
100 {
101  QList<QListWidgetItem *> selection = d->list->selectedItems();
102  for (QListWidgetItem *item : selection)
103  {
104  d->list->removeItemWidget(item);
105  }
106  qDeleteAll(selection);
107 }