Huggle  build:^490^dce1e5c
 All Classes Namespaces Functions Variables Enumerations Enumerator Pages
deleteform.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 "deleteform.hpp"
12 #include "ui_deleteform.h"
13 
14 #if !PRODUCTION_BUILD
15 using namespace Huggle;
16 
17 DeleteForm::DeleteForm(QWidget *parent) : QDialog(parent), ui(new Ui::DeleteForm)
18 {
19  ui->setupUi(this);
20  this->page = NULL;
21  this->dt = NULL;
22  this->deletetoken = "";
23  this->delquery = NULL;
24  this->tokenquery = NULL;
25 }
26 
27 DeleteForm::~DeleteForm()
28 {
29  delete ui;
30  delete page;
31 }
32 
33 void DeleteForm::setPage(WikiPage *Page)
34 {
35  if (Page == NULL)
36  {
37  throw new Exception("Page must not be NULL", "void DeleteForm::setPage(WikiPage *Page)");
38  }
39  this->page = Page;
40 }
41 
43 {
44  this->tokenquery = new ApiQuery();
45  tokenquery->SetAction(ActionQuery);
46  tokenquery->Parameters = "action=query&prop=info&intoken=delete&titles=" + QUrl::toPercentEncoding(this->page->PageName);
47  /// \todo LOCALIZE ME
48  tokenquery->Target = "Getting token to delete " + this->page->PageName;
49  tokenquery->RegisterConsumer(HUGGLECONSUMER_DELETEFORM);
50  Core::AppendQuery(tokenquery);
51  tokenquery->Process();
52 
53  this->dt = new QTimer(this);
54  connect(this->dt, SIGNAL(timeout()), this, SLOT(onTick()));
55  this->delQueryPhase = 0;
56  this->dt->start(200);
57 }
58 
59 void DeleteForm::onTick()
60 {
61  switch (this->delQueryPhase)
62  {
63  case 0:
64  this->checkDelToken();
65  return;
66  case 1:
67  this->Delete();
68  return;
69  }
70  this->dt->stop();
71 }
72 
74 {
75  if (tokenquery == NULL)
76  {
77  return;
78  }
79  if (!tokenquery->Processed())
80  {
81  return;
82  }
83  if (this->tokenquery->Result->Failed)
84  {
85  /// \todo LOCALIZE ME
86  Failed("ERROR: Retreiving the delete token failed. The reason provided was: " + this->tokenquery->Result->ErrorMessage);
87  return;
88  }
89  QDomDocument d;
90  d.setContent(this->tokenquery->Result->Data);
91  QDomNodeList l = d.elementsByTagName("page");
92  if (l.count() == 0)
93  {
94  Core::DebugLog(this->tokenquery->Result->Data);
95  /// \todo LOCALIZE ME
96  Failed("no page info was present in query (are you sysop?)");
97  return;
98  }
99  QDomElement element = l.at(0).toElement();
100  if (!element.attributes().contains("deletetoken"))
101  {
102  /// \todo LOCALIZE ME
103  Failed("No token");
104  return;
105  }
106  this->deletetoken = element.attribute("deletetoken");
107  this->delQueryPhase++;
108  this->tokenquery->UnregisterConsumer(HUGGLECONSUMER_DELETEFORM);
109  this->tokenquery = NULL;
110  Core::DebugLog("Delete token for " + this->page->PageName + ": " + this->deletetoken);
111 
112  // let's delete the page
113  this->delquery = new ApiQuery();
114  this->delquery->SetAction(ActionDelete);
115  delquery->Parameters = "title=" + QUrl::toPercentEncoding(this->page->PageName)
116  + "&reason=" + QUrl::toPercentEncoding(ui->comboBox->lineEdit()->text())
117  + "&token=" + QUrl::toPercentEncoding(deletetoken);
118  delquery->Target = "Deleting " + this->page->PageName;
119  delquery->UsingPOST = true;
120  delquery->RegisterConsumer(HUGGLECONSUMER_DELETEFORM);
121  Core::AppendQuery(delquery);
122  delquery->Process();
123 }
124 
126 {
127  if (this->delquery == NULL)
128  {
129  return;
130  }
131  if (!this->delquery->Processed())
132  {
133  return;
134  }
135 
136  if (this->delquery->Result->Failed)
137  {
138  /// \todo LOCALIZE ME
139  Failed("page can't be deleted: " + this->delquery->Result->ErrorMessage);
140  return;
141  }
142  // let's assume the page was deleted
143  ui->pushButton->setText("deleted");
144  Core::DebugLog("deletion result: " + this->delquery->Result->Data, 2);
145  this->delquery->UnregisterConsumer(HUGGLECONSUMER_DELETEFORM);
146  this->dt->stop();
147 }
148 
149 void DeleteForm::Failed(QString reason)
150 {
151  QMessageBox *_b = new QMessageBox();
152  /// \todo LOCALIZE ME
153  _b->setWindowTitle("Unable to delete page");
154  /// \todo LOCALIZE ME
155  _b->setText("Unable to delete the page because " + reason);
156  _b->exec();
157  delete _b;
158  this->dt->stop();
159  delete this->dt;
160  this->dt = NULL;
161  ui->pushButton->setEnabled(true);
162  if (this->tokenquery != NULL)
163  {
164  tokenquery->UnregisterConsumer(HUGGLECONSUMER_DELETEFORM);
165  }
166  if (this->delquery != NULL)
167  {
168  delquery->UnregisterConsumer(HUGGLECONSUMER_DELETEFORM);
169  }
170  this->delquery = NULL;
171  this->tokenquery = NULL;
172 }
173 
174 
175 void DeleteForm::on_pushButton_clicked()
176 {
177  this->getToken();
178  ui->pushButton->setEnabled(false);
179 }
180 
181 void DeleteForm::on_pushButton_2_clicked()
182 {
183  this->hide();
184 }
185 
186 #endif
QString Target
This is optional property which contains a label of target this query is for.
Definition: apiquery.hpp:98
void Failed(QString reason)
Definition: deleteform.cpp:149
virtual bool Processed()
Returns true in case that query is processed.
Definition: query.cpp:45
QTimer * dt
Set the page to delete.
Definition: deleteform.hpp:57
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
void Process()
Run.
Definition: apiquery.cpp:138
Mediawiki page.
Definition: wikipage.hpp:43
void SetAction(const Action action)
Change the action type.
Definition: apiquery.cpp:185
Every exception raised by huggle is defined by this class.
Definition: exception.hpp:20
bool UsingPOST
Whether the query will submit parameters using POST data.
Definition: apiquery.hpp:77
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 is a delete form.
Definition: deleteform.hpp:38
QString PageName
Name of page.
Definition: wikipage.hpp:48
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