Huggle  build:^490^dce1e5c
 All Classes Namespaces Functions Variables Enumerations Enumerator Pages
reportuser.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 "reportuser.hpp"
12 #include "ui_reportuser.h"
13 using namespace Huggle;
14 
15 ReportUser::ReportUser(QWidget *parent) : QDialog(parent), ui(new Ui::ReportUser)
16 {
17  ui->setupUi(this);
18  this->user = NULL;
19  this->q = NULL;
20  ui->tableWidget->horizontalHeader()->setSelectionBehavior(QAbstractItemView::SelectRows);
21  ui->pushButton->setEnabled(false);
22  /// \todo LOCALIZE ME
23  ui->pushButton->setText("Retrieving history...");
24  QStringList header;
25  ui->tableWidget->setColumnCount(5);
26  this->diff = new QTimer(this);
27  connect(this->diff, SIGNAL(timeout()), this, SLOT(On_DiffTick()));
28  /// \todo LOCALIZE ME
29  header << "Page" << "Time" << "Link" << "DiffID" << "Include in report";
30  ui->tableWidget->setHorizontalHeaderLabels(header);
31  this->tq = NULL;
32  ui->tableWidget->verticalHeader()->setVisible(false);
33  ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
34  Messaging = false;
35  this->qd = NULL;
36  report = "";
37  Loading = false;
38 #if QT_VERSION >= 0x050000
39 // Qt5 code
40  ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
41 #else
42 // Qt4 code
43  ui->tableWidget->horizontalHeader()->setResizeMode(QHeaderView::ResizeToContents);
44 #endif
45  //ui->tableWidget->horizontalHeaderItem(0)->setSizeHint(QSize(20,-1));
46  ui->tableWidget->setShowGrid(false);
47  this->t2 = NULL;
48  this->timer = NULL;
49  /// \todo LOCALIZE ME
50  ui->webView->setHtml("Please select a diff in list in order to open preview");
51 }
52 
54 {
55  if (q != NULL)
56  {
57  delete q;
58  q = NULL;
59  }
60  this->user = u;
61  ui->label->setText(u->Username);
62  q = new ApiQuery();
63  q->Parameters = "list=recentchanges&rcuser=" + QUrl::toPercentEncoding(u->Username) +
64  "&rcprop=user%7Ccomment%7Ctimestamp%7Ctitle%7Cids%7Csizes&rclimit=20&rctype=edit%7Cnew";
65  q->SetAction(ActionQuery);
66  q->Process();
67  this->timer = new QTimer(this);
68  connect(this->timer, SIGNAL(timeout()), this, SLOT(Tick()));
69  this->timer->start(200);
70  return true;
71 }
72 
73 ReportUser::~ReportUser()
74 {
75  if (this->q != NULL)
76  {
77  this->q->SafeDelete();
78  }
79  if (this->qd != NULL)
80  {
81  this->qd->SafeDelete();
82  }
83  delete this->diff;
84  if (this->tq != NULL)
85  {
86  this->tq->SafeDelete();
87  }
88  delete this->ui;
89 }
90 
92 {
93  if (this->q == NULL)
94  {
95  return;
96  }
97 
98  if (Loading)
99  {
100  if (q->Processed())
101  {
102  QDomDocument d;
103  d.setContent(q->Result->Data);
104  QDomNodeList results = d.elementsByTagName("rev");
105  if (results.count() == 0)
106  {
107  /// \todo LOCALIZE ME
108  ui->pushButton->setText("Error unable to retrieve report page at " + Configuration::LocalConfig_ReportPath);
109  this->timer->stop();
110  return;
111  }
112  QDomElement e = results.at(0).toElement();
113  _p = e.text();
114  if (!this->CheckUser())
115  {
116  /// \todo LOCALIZE ME
117  ui->pushButton->setText("This user is already reported");
118  this->timer->stop();
119  return;
120  }
121  this->InsertUser();
122  Core::EditPage(Core::AIVP, _p, "Reporting " + user->Username);
123  this->timer->stop();
124  this->user->IsReported = true;
125  WikiUser::UpdateUser(this->user);
126  /// \todo LOCALIZE ME
127  ui->pushButton->setText("Reported");
128  return;
129  }
130  return;
131  }
132 
133  if (q->Processed())
134  {
136  QDomDocument d;
137  d.setContent(q->Result->Data);
138  QDomNodeList results = d.elementsByTagName("rc");
139  int xx = 0;
140  if (results.count() > 0)
141  {
142  while (results.count() > xx)
143  {
144  QDomElement edit = results.at(xx).toElement();
145  if (!edit.attributes().contains("type"))
146  {
147  continue;
148  }
149  QString page = "unknown page";
150  if (edit.attributes().contains("title"))
151  {
152  page = edit.attribute("title");
153  }
154  QString time = "unknown time";
155  if (edit.attributes().contains("timestamp"))
156  {
157  time = edit.attribute("timestamp");
158  }
159  QString diff = "";
160  if (edit.attributes().contains("revid"))
161  {
162  diff = edit.attribute("revid");
163  }
164  QString link = Core::GetProjectScriptURL() + "index.php?title=" + page + "&diff=" + diff;
165  ui->tableWidget->insertRow(0);
166  ui->tableWidget->setItem(0, 0, new QTableWidgetItem(page));
167  ui->tableWidget->setItem(0, 1, new QTableWidgetItem(time));
168  ui->tableWidget->setItem(0, 2, new QTableWidgetItem(link));
169  ui->tableWidget->setItem(0, 3, new QTableWidgetItem(diff));
170  QCheckBox *Item = new QCheckBox(this);
171  this->CheckBoxes.insert(0, Item);
172  ui->tableWidget->setCellWidget(0, 4, Item);
173  xx++;
174  }
175  }
176  this->timer->stop();
177  ui->pushButton->setEnabled(true);
178  ui->pushButton->setText("Report");
179  }
180 }
181 
183 {
184  if (this->qd == NULL)
185  {
186  return;
187  }
188  if (!this->qd->Processed())
189  {
190  return;
191  }
192  if (this->qd->Result->Failed)
193  {
194  /// \todo LOCALIZE ME
195  ui->webView->setHtml("Unable to retrieve diff: " + this->qd->Result->ErrorMessage);
196  this->diff->stop();
197  return;
198  }
199 
200  QString Summary;
201  QString Diff;
202 
203  QDomDocument d;
204  d.setContent(this->qd->Result->Data);
205  QDomNodeList l = d.elementsByTagName("rev");
206  QDomNodeList diff = d.elementsByTagName("diff");
207  if (diff.count() > 0)
208  {
209  QDomElement e = diff.at(0).toElement();
210  if (e.nodeName() == "diff")
211  {
212  Diff = e.text();
213  }
214  } else
215  {
216  Core::DebugLog(this->qd->Result->Data);
217  /// \todo LOCALIZE ME
218  ui->webView->setHtml("Unable to retrieve diff because api returned no data for it, debug information:<br><hr>" +
219  HuggleWeb::Encode(this->qd->Result->Data));
220  this->diff->stop();
221  return;
222  }
223  // get last id
224  if (l.count() > 0)
225  {
226  QDomElement e = l.at(0).toElement();
227  if (e.nodeName() == "rev")
228  {
229  if (e.attributes().contains("comment"))
230  {
231  Summary = e.attribute("comment");
232  }
233  }
234  }
235 
236  if (Summary == "")
237  {
238  /// \todo LOCALIZE ME
239  Summary = "<font color=red>No summary was provided</font>";
240  } else
241  {
242  Summary = HuggleWeb::Encode(Summary);
243  }
244 
245  ui->webView->setHtml(Core::HtmlHeader + "<tr></td colspan=2><b>Summary:</b> "
246  + Summary + "</td></tr>" + Diff + Core::HtmlFooter);
247  this->diff->stop();
248 }
249 
251 {
252  if (this->tq == NULL)
253  {
254  this->t2->stop();
255  return;
256  }
257 
258  if (!tq->Processed())
259  {
260  return;
261  }
262 
263  QDomDocument d;
264  d.setContent(tq->Result->Data);
265  QDomNodeList results = d.elementsByTagName("rev");
266  ui->pushButton_3->setEnabled(true);
267  if (results.count() == 0)
268  {
269  QMessageBox mb;
270  /// \todo LOCALIZE ME
271  mb.setText("Error unable to retrieve report page at " + Configuration::LocalConfig_ReportPath);
272  mb.exec();
273  this->timer->stop();
274  delete tq;
275  this->tq = NULL;
276  return;
277  }
278  QDomElement e = results.at(0).toElement();
279  _p = e.text();
280  if (!this->CheckUser())
281  {
282  QMessageBox mb;
283  /// \todo LOCALIZE ME
284  mb.setText("This user is already reported");
285  mb.exec();
286  this->timer->stop();
287  delete tq;
288  this->user->IsReported = true;
289  WikiUser::UpdateUser(user);
290  this->tq = NULL;
291  return;
292  } else
293  {
294  QMessageBox mb;
295  /// \todo LOCALIZE ME
296  mb.setText("This user is not reported now");
297  mb.exec();
298  this->timer->stop();
299  delete tq;
300  this->tq = NULL;
301  }
302 }
303 
305 {
306  ui->pushButton->setEnabled(false);
307  // we need to get a report info for all selected diffs
308  QString reports = "";
309  int xx = 0;
310  int EvidenceID = 0;
311  while (xx < ui->tableWidget->rowCount())
312  {
313  if (this->CheckBoxes.count() > xx)
314  {
315  if (this->CheckBoxes.at(xx)->isChecked())
316  {
317  EvidenceID++;
318  reports += "[" + QString(Core::GetProjectScriptURL() + "index.php?title=" +
319  QUrl::toPercentEncoding(ui->tableWidget->item(xx, 0)->text()) + "&diff="
320  + ui->tableWidget->item(xx, 3)->text()).toUtf8() + " #" + QString::number(EvidenceID) + "] ";
321  }
322  }
323  xx++;
324  }
325  if (reports == "")
326  {
327  QMessageBox::StandardButton mb;
328  /// \todo LOCALIZE ME
329  mb = QMessageBox::question(this, "Question", "You didn't provide any diffs as evidence, this will make it extremery hard for administrators to figure out if this "\
330  "user is vandal or not. Are you sure you want to continue?", QMessageBox::Yes|QMessageBox::No);
331  if (mb == QMessageBox::No)
332  {
333  ui->pushButton->setEnabled(true);
334  return;
335  }
336  }
337  // obtain current page
338  Loading = true;
339  /// \todo LOCALIZE ME
340  ui->pushButton->setText("Retrieving current report page");
341  if (this->q != NULL)
342  {
343  delete q;
344  }
345 
346  q = new ApiQuery();
347  q->SetAction(ActionQuery);
348  q->Parameters = "prop=revisions&rvprop=" + QUrl::toPercentEncoding("timestamp|user|comment|content") + "&titles=" +
349  QUrl::toPercentEncoding(Configuration::LocalConfig_ReportPath);
350  q->Process();
351  this->report = reports;
352  this->timer->start(800);
353  return;
354 }
355 
356 void ReportUser::on_pushButton_2_clicked()
357 {
358  QUrl u = QUrl::fromEncoded(QString(Core::GetProjectWikiURL() + QUrl::toPercentEncoding
359  (this->user->GetTalk()) + "?action=render").toUtf8());
360  ui->webView->load(u);
361 }
362 
363 void ReportUser::on_tableWidget_clicked(const QModelIndex &index)
364 {
365  /// \todo LOCALIZE ME
366  ui->webView->setHtml("Please wait...");
367  this->diff->stop();
368  if (this->qd != NULL)
369  {
370  this->qd->Kill();
371  this->qd->SafeDelete();
372  }
373  this->qd = new ApiQuery();
374  this->qd->Parameters = "prop=revisions&rvprop=" + QUrl::toPercentEncoding( "ids|user|timestamp|comment" ) + "&rvlimit=1&rvtoken=rollback&rvstartid=" +
375  ui->tableWidget->item(index.row(), 3)->text() + "&rvendid=" + ui->tableWidget->item(index.row(), 3)->text() + "&rvdiffto=prev&titles=" +
376  QUrl::toPercentEncoding(ui->tableWidget->item(index.row(), 0)->text());
377  this->qd->SetAction(ActionQuery);
378  this->qd->Process();
379  this->diff->start(200);
380 }
381 
382 bool ReportUser::CheckUser()
383 {
384  if (_p.contains(this->user->Username))
385  {
386  return false;
387  }
388  return true;
389 }
390 
391 void ReportUser::InsertUser()
392 {
394  if (!this->user->IsIP())
395  {
397  }
398  xx = xx.replace("$1", this->user->Username);
399  xx = xx.replace("$2", report);
400  xx = xx.replace("$3", ui->lineEdit->text());
401  _p = _p + "\n" + xx;
402 }
403 
404 void ReportUser::on_pushButton_3_clicked()
405 {
406  if (this->tq != NULL)
407  {
408  delete this->tq;
409  }
410 
411  this->tq = new ApiQuery();
412  this->tq->SetAction(ActionQuery);
413  this->tq->Parameters = "prop=revisions&rvprop=" + QUrl::toPercentEncoding("timestamp|user|comment|content") + "&titles=" +
414  QUrl::toPercentEncoding(Configuration::LocalConfig_ReportPath);
415  this->tq->Process();
416  if (this->t2 == NULL)
417  {
418  this->t2 = new QTimer(this);
419  }
420  connect(this->t2, SIGNAL(timeout()), this, SLOT(Test()));
421  this->t2->start(100);
422 }
static QString GetProjectScriptURL()
Return a script url like http://en.wikipedia.org/w/.
Definition: core.cpp:674
virtual bool SafeDelete()
Use this if you are not sure if you can delete this object in this moment.
Definition: collectable.cpp:38
static QString GetProjectWikiURL()
Return a full url like http://en.wikipedia.org/wiki/.
Definition: core.cpp:669
static WikiPage * AIVP
Pointer to AIV page.
Definition: core.hpp:133
void on_tableWidget_clicked(const QModelIndex &index)
Definition: reportuser.cpp:363
Report user.
Definition: reportuser.hpp:39
QString _p
Content of report.
Definition: reportuser.hpp:68
static QString HtmlHeader
This string contains a html header.
Definition: core.hpp:115
void Kill()
Terminate the query.
Definition: apiquery.cpp:229
static QString HtmlFooter
This string contains a html footer.
Definition: core.hpp:117
virtual bool Processed()
Returns true in case that query is processed.
Definition: query.cpp:45
bool IsIP()
Returns true in case the current user is IP user.
Definition: wikiuser.cpp:240
bool SetUser(WikiUser *u)
Set a user.
Definition: reportuser.cpp:53
void on_pushButton_clicked()
Definition: reportuser.cpp:304
QString Username
Username.
Definition: wikiuser.hpp:52
void Process()
Run.
Definition: apiquery.cpp:138
static QString LocalConfig_RUTemplateReport
Regular users.
ReportUser(QWidget *parent=0)
Definition: reportuser.cpp:15
void SetAction(const Action action)
Change the action type.
Definition: apiquery.cpp:185
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
QString GetTalk()
Return a link to talk page of this user (like User talk:Jimbo)
Definition: wikiuser.cpp:245
static QString LocalConfig_IPVTemplateReport
IP vandals.
This class can be used to execute any kind of api query on any wiki.
Definition: apiquery.hpp:55
static void UpdateUser(WikiUser *us)
Update a list of problematic users.
Definition: wikiuser.cpp:59
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