ircdelayedoperationignore.cpp
1 //------------------------------------------------------------------------------
2 // ircdelayedoperationignore.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 "ircdelayedoperationignore.h"
24 
25 #include "irc/configuration/chatnetworkscfg.h"
26 #include "irc/entities/ircnetworkentity.h"
27 #include "irc/entities/ircuserprefix.h"
28 #include "irc/ircnetworkadapter.h"
29 #include "irc/ircnetworkconnectioninfo.h"
30 #include "irc/ircresponseparser.h"
31 #include "patternlist.h"
32 #include <QInputDialog>
33 
34 DClass<IRCDelayedOperationIgnore>
35 {
36 public:
37  bool showPatternPopup;
38  IRCNetworkAdapter *network;
39  QString nickname;
40  QWidget *parentWidget;
41 
42  QString cleanNickname() const
43  {
44  return network->userPrefixes().cleanNickname(nickname);
45  }
46 
47  QString networkDescription() const
48  {
49  return network->connection().networkEntity.description();
50  }
51 };
52 
53 DPointered(IRCDelayedOperationIgnore)
54 
55 
57  IRCNetworkAdapter *network, const QString &nickname)
58  : IRCDelayedOperation(parent)
59 {
60  d->showPatternPopup = false;
61  d->network = network;
62  d->nickname = nickname;
63  d->parentWidget = parent;
64  this->connect(d->network->responseParser(),
65  SIGNAL(whoIsUser(QString,QString,QString,QString)),
66  SLOT(onWhoIsUser(QString,QString,QString)));
67 }
68 
69 IRCDelayedOperationIgnore::~IRCDelayedOperationIgnore()
70 {
71 }
72 
73 void IRCDelayedOperationIgnore::start()
74 {
75  d->network->sendMessage(QString("/whois %1").arg(d->cleanNickname()));
76 }
77 
78 void IRCDelayedOperationIgnore::onWhoIsUser(const QString &nickname, const QString &user,
79  const QString &hostName)
80 {
81  QString ignorePattern = "*!*@" + hostName;
82  if (d->showPatternPopup)
83  {
84  QString label = tr("Ignore user %1 (username=%2) on network %3:").arg(
85  nickname, user, d->networkDescription());
86  ignorePattern = QInputDialog::getText(d->parentWidget, tr("IRC - Ignore user"),
87  label, QLineEdit::Normal, ignorePattern);
88  }
89  if (!ignorePattern.trimmed().isEmpty())
90  {
91  ChatNetworksCfg cfg;
92  IRCNetworkEntity network = cfg.network(d->networkDescription());
93  PatternList patterns = network.ignoredUsers();
94  patterns << QRegExp(ignorePattern, Qt::CaseInsensitive, QRegExp::Wildcard);
95  network.setIgnoredUsers(patterns);
96  cfg.replaceNetwork(network.description(), network, d->parentWidget);
97  d->network->setNetworkEntity(network);
98  }
99  this->deleteLater();
100 }
101 
103 {
104  d->showPatternPopup = b;
105 }