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