Huggle  build:^490^dce1e5c
 All Classes Namespaces Functions Variables Enumerations Enumerator Pages
huggletool.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 "huggletool.hpp"
12 #include "ui_huggletool.h"
13 
14 using namespace Huggle;
15 
16 HuggleTool::HuggleTool(QWidget *parent) : QDockWidget(parent), ui(new Ui::HuggleTool)
17 {
18  ui->setupUi(this);
19  this->query = NULL;
20  this->tick = new QTimer(this);
21  connect(this->tick, SIGNAL(timeout()), this, SLOT(onTick()));
22  this->edit = NULL;
23 }
24 
25 HuggleTool::~HuggleTool()
26 {
27  delete tick;
28  delete ui;
29 }
30 
31 void HuggleTool::SetTitle(QString title)
32 {
33  ui->lineEdit->setText(title);
34  ui->comboBox_2->lineEdit()->setText(title);
35 }
36 
37 void HuggleTool::SetInfo(QString info)
38 {
39  ui->lineEdit->setText(info);
40 }
41 
42 void HuggleTool::SetUser(QString user)
43 {
44  ui->comboBox->lineEdit()->setText(user);
45 }
46 
47 void HuggleTool::SetPage(WikiPage *page)
48 {
49  if (page == NULL)
50  {
51  throw new Exception("HuggleTool::SetPage(WikiPage* page) page must not be null");
52  }
53  this->ui->comboBox_2->lineEdit()->setText(page->PageName);
54  this->tick->stop();
55  this->DeleteQuery();
56  this->ui->pushButton->setEnabled(true);
57  // change color to default
58  this->ui->comboBox_2->lineEdit()->setStyleSheet("color: black;");
59 }
60 
61 void Huggle::HuggleTool::on_pushButton_clicked()
62 {
63  if (this->ui->comboBox_2->lineEdit()->text() == "")
64  {
65  return;
66  }
67  ui->pushButton->setEnabled(false);
68  this->ui->comboBox_2->lineEdit()->setStyleSheet("color: green;");
69  // retrieve information about the page
70  this->query = new ApiQuery();
71  QueryPhase = 1;
72  this->query->SetAction(ActionQuery);
73  this->query->Parameters = "prop=revisions&rvprop=ids%7Cflags%7Ctimestamp%7Cuser%7Cuserid%7Csize%7Csha1%7Ccomment&rvlimit=1&titles="
74  + QUrl::toPercentEncoding(this->ui->comboBox_2->lineEdit()->text());
75  this->query->RegisterConsumer(HUGGLECONSUMER_HUGGLETOOL);
76  this->query->Process();
77  this->tick->start(200);
78 }
79 
80 void HuggleTool::onTick()
81 {
82  switch (this->QueryPhase)
83  {
84  case 0:
85  this->tick->stop();
86  return;
87  case 1:
88  this->FinishPage();
89  return;
90  case 2:
91  this->FinishEdit();
92  return;
93  }
94 }
95 
97 {
98  if (this->query == NULL)
99  {
100  return;
101  }
102  if (!this->query->Processed())
103  {
104  return;
105  }
106 
107  edit = new WikiEdit();
108  edit->RegisterConsumer("MainForm");
109  edit->Page = new WikiPage(this->ui->comboBox_2->lineEdit()->text());
110  QDomDocument d;
111  d.setContent(this->query->Result->Data);
112  QDomNodeList l = d.elementsByTagName("rev");
113  if (l.count() > 0)
114  {
115  QDomElement e = l.at(0).toElement();
116  if (e.attributes().contains("missing"))
117  {
118  // there is no such a page
119  this->DeleteQuery();
120  this->ui->comboBox_2->lineEdit()->setStyleSheet("color: red;");
121  /// \todo LOCALIZE ME
122  Core::Log("There is no page " + ui->comboBox_2->lineEdit()->text() + " on wiki");
123  this->tick->stop();
124  return;
125  }
126  if (e.attributes().contains("user"))
127  {
128  edit->User = new WikiUser(e.attribute("user"));
129  }
130  if (e.attributes().contains("revid"))
131  {
132  edit->RevID = e.attribute("revid").toInt();
133  }
134  }
135  if (edit->User == NULL)
136  {
137  edit->User = new WikiUser();
138  }
140  edit->UnregisterConsumer(HUGGLECONSUMER_WIKIEDIT);
141  this->QueryPhase = 2;
142 }
143 
144 void HuggleTool::FinishEdit()
145 {
146  if (this->edit == NULL)
147  {
148  return;
149  }
150  if (!this->edit->IsPostProcessed())
151  {
152  return;
153  }
154  this->tick->stop();
156 }
157 
158 void HuggleTool::DeleteQuery()
159 {
160  if (this->query == NULL)
161  {
162  return;
163  }
164  this->query->UnregisterConsumer(HUGGLECONSUMER_HUGGLETOOL);
165  this->query = NULL;
166 }
static void Log(QString Message)
Write text to terminal as well as ring log.
Definition: core.cpp:563
int RevID
Revision ID.
Definition: wikiedit.hpp:105
static void PostProcessEdit(WikiEdit *_e)
Definition: core.cpp:774
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
void ProcessEdit(WikiEdit *e, bool IgnoreHistory=false)
Recreate interface, should be called everytime you do anything with main form.
Definition: mainwindow.cpp:258
Mediawiki page.
Definition: wikipage.hpp:43
Toolbar on top of window.
Definition: huggletool.hpp:35
bool IsPostProcessed()
Return true in case this edit was post processed already.
Definition: wikiedit.cpp:297
Every exception raised by huggle is defined by this class.
Definition: exception.hpp:20
WikiUser * User
User who changed the page.
Definition: wikiedit.hpp:89
Wiki edit.
Definition: wikiedit.hpp:67
static MainWindow * Main
Pointer to main.
Definition: core.hpp:111
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
int QueryPhase
Page download phase.
Definition: huggletool.hpp:62
WikiPage * Page
Page that was changed by edit.
Definition: wikiedit.hpp:87
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
QTimer * tick
Timer that is used to switch between events that happen when the data for page are retrieved...
Definition: huggletool.hpp:55