ircignoresmanager.cpp
1 //------------------------------------------------------------------------------
2 // ircignoresmanager.cpp
3 //------------------------------------------------------------------------------
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
9 //
10 // This program 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
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; 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 <QKeyEvent>
27 #include "irc/configuration/chatnetworkscfg.h"
28 #include "irc/entities/ircnetworkentity.h"
29 #include "patternlist.h"
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  {
57  saveItems();
58  }
59  QDialog::done(result);
60 }
61 
62 void IRCIgnoresManager::keyPressEvent(QKeyEvent *event)
63 {
64  if (event->key() == Qt::Key_Delete)
65  {
66  deleteSelected();
67  }
68  QDialog::keyPressEvent(event);
69 }
70 
71 void IRCIgnoresManager::loadItems()
72 {
73  ChatNetworksCfg cfg;
74  IRCNetworkEntity network = cfg.network(d->networkDescription);
75  foreach (const QRegExp &pattern, network.ignoredUsers())
76  {
77  QListWidgetItem *item = new QListWidgetItem(pattern.pattern(), d->list);
78  item->setFlags(item->flags() | Qt::ItemIsEditable);
79  d->list->addItem(item);
80  }
81 }
82 
83 void IRCIgnoresManager::saveItems()
84 {
85  ChatNetworksCfg cfg;
86  IRCNetworkEntity network = cfg.network(d->networkDescription);
87  network.setIgnoredUsers(patterns());
88  cfg.replaceNetwork(network.description(), network, this);
89 }
90 
91 PatternList IRCIgnoresManager::patterns() const
92 {
93  PatternList result;
94  for (int row = 0; row < d->list->count(); ++row)
95  {
96  QString text = d->list->item(row)->text();
97  if (!text.trimmed().isEmpty())
98  {
99  result << QRegExp(text, Qt::CaseInsensitive, QRegExp::Wildcard);
100  }
101  }
102  return result;
103 }
104 
105 void IRCIgnoresManager::deleteSelected()
106 {
107  QList<QListWidgetItem*> selection = d->list->selectedItems();
108  foreach (QListWidgetItem *item, selection)
109  {
110  d->list->removeItemWidget(item);
111  }
112  qDeleteAll(selection);
113 }
const QString & description() const
A short, human-readable description for the network. (Preferably a single word).
bool replaceNetwork(const QString &oldDescription, const IRCNetworkEntity &newNetwork, QWidget *errorDisplayParentWidget)
Replace network definition in config and move around log files.
Data structure that describes and defines a connection to an IRC network or server.