Huggle  build:^490^dce1e5c
 All Classes Namespaces Functions Variables Enumerations Enumerator Pages
blockuser.cpp
1 //This program is free software: you can redistribute it and/or modify
2 //it under the terms of the GNU General Public License as published by
3 //the Free Software Foundation, either version 3 of the License, or
4 //(at your option) any later version.
5 
6 //This program is distributed in the hope that it will be useful,
7 //but WITHOUT ANY WARRANTY; without even the implied warranty of
8 //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 //GNU General Public License for more details.
10 
11 #include "blockuser.hpp"
12 #include "ui_blockuser.h"
13 
14 #if !PRODUCTION_BUILD
15 using namespace Huggle;
16 
17 BlockUser::BlockUser(QWidget *parent) : QDialog(parent), ui(new Ui::BlockUser)
18 {
19  ui->setupUi(this);
20  // we should initialise every variable
21  this->blocktoken = "";
22  this->user = NULL;
23  this->b = NULL;
24  this->t0 = NULL;
25  this->tb = NULL;
26  ui->comboBox->addItem(Configuration::LocalConfig_BlockReason);
27 }
28 
29 BlockUser::~BlockUser()
30 {
31  delete t0;
32  delete ui;
33 }
34 
35 void BlockUser::SetWikiUser(WikiUser *User)
36 {
37  this->user = User;
38  if (this->user->IsIP())
39  {
40  ui->checkBox_5->setEnabled(true);
41  }
42 }
43 
45 {
46  // Let's get a token before anything
47  b = new ApiQuery();
48  b->SetAction(ActionQuery);
49  b->Parameters = "prop=info&intoken=block&titles=User:" +
50  QUrl::toPercentEncoding(this->user->Username);
51  /// \todo LOCALIZE ME
52  b->Target = "Getting token to block" + this->user->Username;
53  b->RegisterConsumer("BlockUser::GetToken");
55  b->Process();
56 
57  this->t0 = new QTimer(this);
58  connect(this->t0, SIGNAL(timeout()), this, SLOT(onTick()));
59  this->QueryPhase = 0;
60  this->t0->start(200);
61 }
62 
63 void BlockUser::on_pushButton_2_clicked()
64 {
65  this->hide();
66 }
67 
68 void BlockUser::onTick()
69 {
70  switch (this->QueryPhase)
71  {
72  case 0:
73  this->CheckToken();
74  return;
75  case 1:
76  this->Block();
77  return;
78  }
79  this->t0->stop();
80 }
81 
83 {
84  if (this->b == NULL)
85  {
86  return;
87  }
88  if (!this->b->Processed())
89  {
90  return;
91  }
92  if (this->b->Result->Failed)
93  {
94  /// \todo LOCALIZE ME
95  Failed("token can't be retrieved: " + this->b->Result->ErrorMessage);
96  return;
97  }
98  QDomDocument d;
99  d.setContent(this->b->Result->Data);
100  QDomNodeList l = d.elementsByTagName("user");
101  if (l.count() == 0)
102  {
103  Core::DebugLog(this->b->Result->Data);
104  /// \todo LOCALIZE ME
105  Failed("no user info was present in query (are you sysop?)");
106  return;
107  }
108  QDomElement element = l.at(0).toElement();
109  if (!element.attributes().contains("blocktoken"))
110  {
111  /// \todo LOCALIZE ME
112  Failed("No token");
113  return;
114  }
115  this->blocktoken = element.attribute("blocktoken");
116  this->QueryPhase++;
117  this->b->UnregisterConsumer("BlockUser::GetToken");
118  this->b = NULL;
119  Core::DebugLog("Block token for " + this->user->Username + ": " + this->blocktoken);
120 
121  // let's block them
122  this->tb = new ApiQuery();
123  this->tb->SetAction(ActionQuery);
124  if (user->IsIP())
125  {
126  tb->Parameters = "action=block&user=" + QUrl::toPercentEncoding(this->user->Username) + "reason="
127  + QUrl::toPercentEncoding(Configuration::LocalConfig_BlockReason) + "expiry="
128  + QUrl::toPercentEncoding(Configuration::LocalConfig_BlockTimeAnon) + "token="
129  + QUrl::toPercentEncoding(blocktoken);
130 
131  }else
132  {
133  tb->Parameters = "action=block&user=" + QUrl::toPercentEncoding(this->user->Username) + "reason="
134  + QUrl::toPercentEncoding(Configuration::LocalConfig_BlockReason) + "token="
135  + QUrl::toPercentEncoding(blocktoken);
136  }
137  /// \todo LOCALIZE ME
138  tb->Target = "Blocking" + this->user->Username;
139  tb->UsingPOST = true;
140  tb->RegisterConsumer("BlockUser::on_pushButton_clicked()");
142  tb->Process();
143 }
144 
146 {
147  if (this->tb == NULL)
148  {
149  return;
150  }
151  if (!this->tb->Processed())
152  {
153  return;
154  }
155 
156  if (this->tb->Result->Failed)
157  {
158  /// \todo LOCALIZE ME
159  Failed("user can't be blocked: " + this->tb->Result->ErrorMessage);
160  return;
161  }
162  // let's assume the user was blocked
163  ui->pushButton->setText("Blocked");
164  Core::DebugLog("block result: " + this->tb->Result->Data, 2);
165  this->tb->UnregisterConsumer("BlockUser::on_pushButton_clicked()");
166  this->t0->stop();
167 }
168 
169 void BlockUser::Failed(QString reason)
170 {
171  QMessageBox *_b = new QMessageBox();
172  _b->setWindowTitle("Unable to block user");
173  _b->setText("Unable to block the user because " + reason);
174  _b->exec();
175  delete _b;
176  this->t0->stop();
177  delete this->t0;
178  this->t0 = NULL;
179  ui->pushButton->setEnabled(true);
180  if (this->b != NULL)
181  {
182  b->UnregisterConsumer("BlockUser::GetToken");
183  }
184  if (this->tb != NULL)
185  {
186  tb->UnregisterConsumer("BlockUser::on_pushButton_clicked()");
187  }
188  this->tb = NULL;
189  this->b = NULL;
190 }
191 
192 void BlockUser::on_pushButton_clicked()
193 {
194  this->GetToken();
195  this->sendBlockNotice(dependency);
196  // disable the button so that user can't click it multiple times
197  ui->pushButton->setEnabled(false);
198 }
199 
200 void BlockUser::sendBlockNotice(ApiQuery *dependency)
201 {
202  QString blocknotice;
203  if (user->IsIP())
204  {
205  blocknotice = Configuration::LocalConfig_BlockMessage;
206  }else
207  {
208  blocknotice = Configuration::LocalConfig_BlockMessageIndef;
209  }
210  QString blocksum = Configuration::LocalConfig_BlockSummary;
211  Core::MessageUser(user, blocknotice, "Blocked", blocksum, true, dependency);
212 }
213 
214 #endif
215 
QString Target
This is optional property which contains a label of target this query is for.
Definition: apiquery.hpp:98
virtual bool Processed()
Returns true in case that query is processed.
Definition: query.cpp:45
This form can be used to block users from editing, which requires the block permission.
Definition: blockuser.hpp:37
bool IsIP()
Returns true in case the current user is IP user.
Definition: wikiuser.cpp:240
void UnregisterConsumer(const int consumer)
This function will remove a string which prevent the object from being removed.
Definition: collectable.cpp:68
void RegisterConsumer(const int consumer)
Registers a consumer.
Definition: collectable.cpp:57
static void AppendQuery(Query *item)
Insert a query to internal list of running queries, so that they can be watched This will insert it t...
Definition: core.cpp:557
QString Username
Username.
Definition: wikiuser.hpp:52
static Message * MessageUser(WikiUser *user, QString message, QString title, QString summary, bool section=true, Query *dependency=NULL)
MessageUser Message user.
Definition: core.cpp:427
void Process()
Run.
Definition: apiquery.cpp:138
void SetAction(const Action action)
Change the action type.
Definition: apiquery.cpp:185
ApiQuery * tb
Definition: blockuser.hpp:55
bool UsingPOST
Whether the query will submit parameters using POST data.
Definition: apiquery.hpp:77
ApiQuery * b
Definition: blockuser.hpp:57
static void DebugLog(QString Message, unsigned int Verbosity=1)
This log is only shown if verbosity is same or larger than requested verbosity.
Definition: core.cpp:641
QString ErrorMessage
If query is in error the reason for error is stored here.
Definition: queryresult.hpp:27
QString Parameters
Parameters for action, for example page title.
Definition: apiquery.hpp:84
This class can be used to execute any kind of api query on any wiki.
Definition: apiquery.hpp:55
QString Data
Data retrieved by query.
Definition: queryresult.hpp:25
QueryResult * Result
Result of query, see documentation of QueryResult for more.
Definition: query.hpp:68