Huggle  build:^490^dce1e5c
 All Classes Namespaces Functions Variables Enumerations Enumerator Pages
processlist.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 "processlist.hpp"
12 #include "ui_processlist.h"
13 
14 using namespace Huggle;
15 
16 ProcessList::ProcessList(QWidget *parent) : QDockWidget(parent), ui(new Ui::ProcessList)
17 {
18  ui->setupUi(this);
19  ui->tableWidget->setColumnCount(4);
20  QStringList header;
21  header << "ID" << "Type" << "Target" << "Status" << "Result";
22  ui->tableWidget->setHorizontalHeaderLabels(header);
23  ui->tableWidget->verticalHeader()->setVisible(false);
24  ui->tableWidget->horizontalHeader()->setSelectionBehavior(QAbstractItemView::SelectRows);
25  ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
26 #if QT_VERSION >= 0x050000
27 // Qt5 code
28  ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
29 #else
30 // Qt4 code
31  ui->tableWidget->horizontalHeader()->setResizeMode(QHeaderView::ResizeToContents);
32 #endif
33  //ui->tableWidget->horizontalHeaderItem(0)->setSizeHint(QSize(20,-1));
34  ui->tableWidget->setShowGrid(false);
35  this->Removed = new QList<ProcessListRemovedItem*> ();
36 }
37 
39 {
40  if (q == NULL)
41  {
42  throw new Exception("NULL query");
43  }
44  q->RegisterConsumer(HUGGLECONSUMER_PROCESSLIST);
45  int size = ui->tableWidget->rowCount();
46  ui->tableWidget->insertRow(size);
47  ui->tableWidget->setItem(size, 0, new QTableWidgetItem(QString::number(q->QueryID())));
48  ui->tableWidget->setItem(size, 1, new QTableWidgetItem(q->QueryTypeToString()));
49  ui->tableWidget->setItem(size, 2, new QTableWidgetItem(q->QueryTargetToString()));
50  ui->tableWidget->setItem(size, 3, new QTableWidgetItem(q->QueryStatusToString()));
51  q->UnregisterConsumer(HUGGLECONSUMER_PROCESSLIST);
52 }
53 
54 void ProcessList::Clear()
55 {
56  delete this->Removed;
57  ui->tableWidget->clear();
58 }
59 
61 {
62  q->RegisterConsumer(HUGGLECONSUMER_PROCESSLIST);
63  int result = GetItem(q);
64  q->UnregisterConsumer(HUGGLECONSUMER_PROCESSLIST);
65  return result != -1;
66 }
67 
69 {
70  q->RegisterConsumer(HUGGLECONSUMER_PROCESSLIST);
71  if (!IsExpired(q))
72  {
73  this->Removed->append(new ProcessListRemovedItem(q->QueryID()));
74  }
75  q->UnregisterConsumer(HUGGLECONSUMER_PROCESSLIST);
76 }
77 
79 {
80  q->RegisterConsumer(HUGGLECONSUMER_PROCESSLIST);
81  int query = GetItem(q);
82  if (query == -1)
83  {
84  this->InsertQuery(q);
85  return;
86  }
87 
88  ui->tableWidget->setItem(query, 0, new QTableWidgetItem(QString::number(q->QueryID())));
89  ui->tableWidget->setItem(query, 1, new QTableWidgetItem(q->QueryTypeToString()));
90  ui->tableWidget->setItem(query, 2, new QTableWidgetItem(q->QueryTargetToString()));
91  ui->tableWidget->setItem(query, 3, new QTableWidgetItem(q->QueryStatusToString()));
92  q->UnregisterConsumer(HUGGLECONSUMER_PROCESSLIST);
93 }
94 
95 bool ProcessList::IsExpired(Query *q)
96 {
97  q->RegisterConsumer(HUGGLECONSUMER_PROCESSLIST);
98  int i = 0;
99  while (i<Removed->count())
100  {
101  if ((unsigned int)Removed->at(i)->GetID() == q->QueryID())
102  {
103  q->UnregisterConsumer(HUGGLECONSUMER_PROCESSLIST);
104  return true;
105  }
106  i++;
107  }
108  q->UnregisterConsumer(HUGGLECONSUMER_PROCESSLIST);
109  return false;
110 }
111 
112 void ProcessList::RemoveExpired()
113 {
114  if (Removed->count() == 0)
115  {
116  return;
117  }
118  QList<ProcessListRemovedItem*> rm;
119  int i = 0;
120  while (i<Removed->count())
121  {
122  if (Removed->at(i)->Expired())
123  {
124  rm.append(Removed->at(i));
125  }
126  i++;
127  }
128  i = 0;
129  while (i<rm.count())
130  {
131  ProcessListRemovedItem *item = rm.at(i);
132  Removed->removeOne(item);
133  int row = this->GetItem(item->GetID());
134  if (row != -1)
135  {
136  ui->tableWidget->removeRow(row);
137  }
138  delete item;
139  i++;
140  }
141 }
142 
143 int ProcessList::GetItem(Query *q)
144 {
145  q->RegisterConsumer(HUGGLECONSUMER_PROCESSLIST);
146  int curr = 0;
147  int size = ui->tableWidget->rowCount();
148  while (curr < size)
149  {
150  if (ui->tableWidget->item(curr,0)->text() == QString::number(q->QueryID()))
151  {
152  q->UnregisterConsumer(HUGGLECONSUMER_PROCESSLIST);
153  return curr;
154  }
155  curr++;
156  }
157  q->UnregisterConsumer(HUGGLECONSUMER_PROCESSLIST);
158  return -1;
159 }
160 
161 int ProcessList::GetItem(int Id)
162 {
163  int curr = 0;
164  int size = ui->tableWidget->rowCount();
165  while (curr < size)
166  {
167  if (ui->tableWidget->item(curr,0)->text() == QString::number(Id))
168  {
169  return curr;
170  }
171  curr++;
172  }
173  return -1;
174 }
175 
176 ProcessList::~ProcessList()
177 {
178  delete ui;
179 }
180 
181 ProcessListRemovedItem::ProcessListRemovedItem(int ID)
182 {
183  this->id = ID;
184  this->time = QDateTime::currentDateTime();
185 }
186 
187 int ProcessListRemovedItem::GetID()
188 {
189  return id;
190 }
191 
192 bool ProcessListRemovedItem::Expired()
193 {
194  return this->time < QDateTime::currentDateTime().addSecs(-Configuration::QueryListTimeLimit);
195 }
196 
void InsertQuery(Query *q)
Insert a query to process list, the query is automatically removed once it&#39;s done.
Definition: processlist.cpp:38
Removed item that was in the process list.
Definition: processlist.hpp:35
List of processes in a main window.
Definition: processlist.hpp:50
static int QueryListTimeLimit
Number of seconds for which the processed queries remain in list of processes.
virtual QString QueryTargetToString()
Return a target of a query.
Definition: query.cpp:93
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
virtual QString QueryTypeToString()
Convert a type of this query to a string.
Definition: query.cpp:75
void RemoveQuery(Query *q)
Remove a query from list no matter if it finished or not.
Definition: processlist.cpp:68
Every exception raised by huggle is defined by this class.
Definition: exception.hpp:20
unsigned int QueryID()
Definition: query.cpp:135
bool ContainsQuery(Query *q)
Return true if there is already this in a list.
Definition: processlist.cpp:60
void UpdateQuery(Query *q)
Update information about query in list.
Definition: processlist.cpp:78
Query base class for all http queries executed by huggle.
Definition: query.hpp:64