Huggle  build:^490^dce1e5c
 All Classes Namespaces Functions Variables Enumerations Enumerator Pages
query.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 "query.hpp"
12 
13 using namespace Huggle;
14 
15 unsigned int Query::LastID = 0;
16 QNetworkAccessManager Query::NetworkManager;
17 
19 {
20  this->Result = NULL;
21  this->Type = QueryNull;
22  this->Status = StatusNull;
23  this->ID = this->LastID;
24  this->LastID++;
25  this->CustomStatus = "";
26  this->callback = NULL;
27  this->HiddenQuery = false;
28  this->Dependency = NULL;
29  this->Timeout = 60;
30  this->CallbackResult = NULL;
31  this->StartTime = QDateTime::currentDateTime();
32  this->RetryOnTimeoutFailure = true;
33 }
34 
36 {
37  delete Result;
38  if (this->CallbackResult != NULL)
39  {
40  throw new Exception("Memory leak: Query::CallbackResult was not deleted before destructor was called");
41  }
42  this->Result = NULL;
43 }
44 
46 {
47  if (this->Status == StatusDone || this->Status == StatusInError)
48  {
49  return true;
50  }
51  if (QDateTime::currentDateTime() > this->StartTime.addSecs(this->Timeout))
52  {
53  if (!this->Repeated && this->RetryOnTimeoutFailure)
54  {
55  this->Kill();
56  this->StartTime = QDateTime::currentDateTime();
57  this->Repeated = true;
58  this->Process();
59  return false;
60  }
61  // query is timed out
62  if (this->Result == NULL)
63  {
64  this->Result = new QueryResult();
65  }
66  this->Kill();
67  this->Result->Failed = true;
68  this->Result->ErrorMessage = "Timed out";
69  this->Status = StatusInError;
70  return true;
71  }
72  return false;
73 }
74 
76 {
77  switch (this->Type)
78  {
79  case QueryNull:
80  return "null";
81  case QueryWl:
82  return "Wl Query";
83  case QueryApi:
84  return "Api Query";
85  case QueryRevert:
86  return "Revert Query";
87  case QueryEdit:
88  return "Edit Query";
89  }
90  return "Unknown";
91 }
92 
94 {
95  return "Invalid target";
96 }
97 
98 QString Query::QueryStatusToString()
99 {
100  if (this->CustomStatus != "")
101  {
102  return CustomStatus;
103  }
104 
105  switch (this->Status)
106  {
107  case StatusNull:
108  return "NULL";
109  case StatusDone:
110  return "Done";
111  case StatusProcessing:
112  return "Processing";
113  case StatusInError:
114  if (this->Result != NULL)
115  {
116  if (this->Result->Failed && this->Result->ErrorMessage != "")
117  {
118  return "In error: " + this->Result->ErrorMessage;
119  }
120  }
121  return "InError";
122  }
123  return "Unknown";
124 }
125 
127 {
128  if (this->callback != NULL)
129  {
130  this->RegisterConsumer("delegate");
131  this->CallbackResult = this->callback(this);
132  }
133 }
134 
135 unsigned int Query::QueryID()
136 {
137  return this->ID;
138 }
bool HiddenQuery
Definition: query.hpp:96
bool Repeated
When a query fail and retry this is changed to true so that it doesn't endlessly restart.
Definition: query.hpp:144
virtual void Process()
Execute query.
Definition: query.hpp:113
virtual QString QueryTargetToString()
Return a target of a query.
Definition: query.cpp:93
virtual bool Processed()
Returns true in case that query is processed.
Definition: query.cpp:45
Result of query.
Definition: queryresult.hpp:19
void RegisterConsumer(const int consumer)
Registers a consumer.
Definition: collectable.cpp:57
void ProcessCallback()
Definition: query.cpp:126
Default.
Definition: query.hpp:48
static unsigned int LastID
This is a last ID used by a constructor of a query.
Definition: query.hpp:142
virtual QString QueryTypeToString()
Convert a type of this query to a string.
Definition: query.cpp:75
Callback callback
Callback.
Definition: query.hpp:87
virtual void Kill()
Terminates a query.
Definition: query.hpp:121
QueryType Type
Type of a query.
Definition: query.hpp:80
virtual ~Query()
Destructor for query.
Definition: query.cpp:35
Query()
Creates empty query.
Definition: query.cpp:18
Whitelist.
Definition: query.hpp:50
Every exception raised by huggle is defined by this class.
Definition: exception.hpp:20
Query * Dependency
Dependency for query.
Definition: query.hpp:103
QString ErrorMessage
If query is in error the reason for error is stored here.
Definition: queryresult.hpp:27
unsigned int QueryID()
Definition: query.cpp:135
QString CustomStatus
Custom status.
Definition: query.hpp:76
unsigned int ID
Every query has own unique ID which can be used to work with them.
Definition: query.hpp:140
void * CallbackResult
This is a pointer to object returned by your callback function.
Definition: query.hpp:89
QueryResult * Result
Result of query, see documentation of QueryResult for more.
Definition: query.hpp:68