Huggle  build:^490^dce1e5c
 All Classes Namespaces Functions Variables Enumerations Enumerator Pages
protectpage.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 "protectpage.hpp"
12 #include "ui_protectpage.h"
13 
14 using namespace Huggle;
15 
16 ProtectPage::ProtectPage(QWidget *parent) : QDialog(parent), ui(new Ui::ProtectPage)
17 {
18  ui->setupUi(this);
19  this->ptkk = NULL;
20  this->ptkq = NULL;
21  this->ptpge = NULL;
22  this->ptpt = NULL;
23  /// \todo LOCALIZE ME
24  this->ui->comboBox_3->addItem("Everyone (no protection)");
25  /// \todo LOCALIZE ME
26  this->ui->comboBox_3->addItem("Autoconfirmed (semi)");
27  /// \todo LOCALIZE ME
28  this->ui->comboBox_3->addItem("Sysops (full)");
29  this->ui->comboBox_3->setCurrentIndex(2);
30  this->protecttoken = "";
31  this->tt = NULL;
32  ui->comboBox->addItem(Configuration::LocalConfig_ProtectReason);
33 }
34 
35 ProtectPage::~ProtectPage()
36 {
37  delete ui;
38  delete ptpge;
39  delete tt;
40 }
41 
43 {
44  this->ptpge = Page;
45 }
46 
48 {
49  this->ptkq = new ApiQuery();
50  ptkq->SetAction(ActionQuery);
51  ptkq->Parameters = "prop=info&intoken=protect&titles=" + QUrl::toPercentEncoding(this->ptpge->PageName);
52  /// \todo LOCALIZE ME
53  ptkq->Target = "Fetching token to protect" + this->ptpge->PageName;
54  ptkq->RegisterConsumer("ProtectPage::getTokenToProtect()");
56  ptkq->Process();
57 
58  this->tt = new QTimer(this);
59  connect(this->tt, SIGNAL(timeout()), this, SLOT(onTick()));
60  this->PtQueryPhase = 0;
61  this->tt->start(200);
62 }
63 
64 void ProtectPage::onTick()
65 {
66  switch(this->PtQueryPhase)
67  {
68  case 0:
69  this->checkTokenToProtect();
70  return;
71  case 1:
72  this->Protect();
73  return;
74  }
75  this->tt->stop();
76 }
77 
79 {
80  if(ptkq == NULL)
81  {
82  return;
83  }
84  if(!ptkq->Processed())
85  {
86  return;
87  }
88  if(ptkq->Result->Failed)
89  {
90  /// \todo LOCALIZE ME
91  Failed("ERROR: Token cannot be retrieved. The reason was: " + ptkq->Result->ErrorMessage);
92  return;
93  }
94  QDomDocument r;
95  r.setContent(ptkq->Result->Data);
96  QDomNodeList l = r.elementsByTagName("page");
97  if (l.count() == 0)
98  {
99  Core::DebugLog(this->ptkq->Result->Data);
100  /// \todo LOCALIZE ME
101  Failed("No page info was available (are you an admin?)");
102  return;
103  }
104  QDomElement element = l.at(0).toElement();
105  if (!element.attributes().contains("protecttoken"))
106  {
107  Failed("No token");
108  return;
109  }
110  this->protecttoken = element.attribute("protecttoken");
111  this->PtQueryPhase++;
112  this->ptkq->UnregisterConsumer("ProtectPage::getTokenToProtect");
113  this->ptkq = NULL;
114  Core::DebugLog("Protection token for " + this->ptpge->PageName + ": " + this->protecttoken);
115  this->ptpt = new ApiQuery();
116  ptpt->SetAction(ActionProtect);
117  QString protection = "edit=sysop|move=sysop";
118  switch (this->ui->comboBox_3->currentIndex())
119  {
120  case 0:
121  protection = "edit=all|move=all";
122  break;
123  case 1:
124  protection = "edit=autoconfirmed|move=autoconfirmed";
125  break;
126  }
127  ptpt->Parameters = "title=" + QUrl::toPercentEncoding(this->ptpge->PageName)
128  + "&reason=" + QUrl::toPercentEncoding(Configuration::LocalConfig_ProtectReason)
129  + "&expiry=" + QUrl::toPercentEncoding(ui->comboBox_2->currentText())
130  + "&protections=" + QUrl::toPercentEncoding(protection)
131  + "&token=" + QUrl::toPercentEncoding(protecttoken);
132  ptpt->Target = "Protecting " + this->ptpge->PageName;
133  ptpt->RegisterConsumer("ProtectPage");
135  ptpt->Process();
136 }
137 
138 void ProtectPage::on_pushButton_clicked()
139 {
140  this->hide();
141 }
142 
143 void ProtectPage::on_pushButton_2_clicked()
144 {
145  this->getTokenToProtect();
146  this->ui->pushButton_2->setEnabled(false);
147 }
148 
149 void ProtectPage::Failed(QString reason)
150 {
151  QMessageBox *_pmb = new QMessageBox();
152  /// \todo LOCALIZE ME
153  _pmb->setWindowTitle("Unable to protect page");
154  /// \todo LOCALIZE ME
155  _pmb->setText("Unable to protect the page because " + reason);
156  _pmb->exec();
157  delete _pmb;
158  this->tt->stop();
159  delete this->tt;
160  this->tt = NULL;
161  ui->pushButton->setEnabled(true);
162  if (this->ptkq != NULL)
163  {
164  this->ptkq->UnregisterConsumer("ProtectPage::getTokenToProtect");
165  }
166  if (this->ptpt != NULL)
167  {
168  this->ptpt->UnregisterConsumer("ProtectPage");
169  }
170  this->ptpt = NULL;
171  this->ptkq = NULL;
172 }
173 
175 {
176  if (!this->ptpt->Processed())
177  {
178  return;
179  }
180  if (ptpt == NULL)
181  {
182  return;
183  }
184  if (ptpt->Result->Failed)
185  {
186  /// \todo LOCALIZE ME
187  Failed("The API query failed. Reason supplied was: " + ptpt->Result->ErrorMessage);
188  return;
189  }
190  ui->pushButton_2->setText("Page has been protected");
191  Core::DebugLog("The page " + ptpge->PageName + " has successfully been protected");
192  this->ptpt->UnregisterConsumer("ProtectPage");
193  this->tt->stop();
194  ptpt = NULL;
195 }
196 
void setPageToProtect(WikiPage *Page)
set a page that is supposed to be protected, this needs to be called by owner who created this form ...
Definition: protectpage.cpp:42
QString Target
This is optional property which contains a label of target this query is for.
Definition: apiquery.hpp:98
ApiQuery * ptkk
Pointer for second token.
Definition: protectpage.hpp:61
ProtectPage(QWidget *parent=0)
Definition: protectpage.cpp:16
virtual bool Processed()
Returns true in case that query is processed.
Definition: query.cpp:45
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
QTimer * tt
DOCUMENT ME.
Definition: protectpage.hpp:68
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
The ProtectPage class display a window where user can protect a page given they have the permissions ...
Definition: protectpage.hpp:36
ApiQuery * ptpt
Pointer for protection.
Definition: protectpage.hpp:63
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
WikiPage * ptpge
DOCUMENT ME.
Definition: protectpage.hpp:66
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 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
ApiQuery * ptkq
Pointer to get first token.
Definition: protectpage.hpp:59
void Failed(QString reason)
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