Merge branch 'master' of ssh://pumpa.branchable.com
[larjonas-pumpa.git] / src / qasabstractobject.cpp
blob1c0baf9a40317b064a4a65c5529e395368ebeb90
1 /*
2 Copyright 2013-2015 Mats Sjöberg
4 This file is part of the Pumpa programme.
6 Pumpa is free software: you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 Pumpa is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with Pumpa. If not, see <http://www.gnu.org/licenses/>.
20 //------------------------------------------------------------------------------
22 #include "qasabstractobject.h"
24 //------------------------------------------------------------------------------
26 QDateTime parseTime(QString timeStr) {
27 // 2013-05-28T16:43:06Z
28 // 55 minutes ago kl. 20:39 -> 19:44
30 QDateTime dt = QDateTime::fromString(timeStr, Qt::ISODate);
31 // "yyyy-MM-ddThh:mm:ssZ");
32 dt.setTimeSpec(Qt::UTC);
34 return dt;
37 //------------------------------------------------------------------------------
39 QASAbstractObject::QASAbstractObject(int asType, QObject* parent) :
40 QObject(parent),
41 m_asType(asType)
44 //------------------------------------------------------------------------------
46 void QASAbstractObject::connectSignals(QASAbstractObject* obj,
47 bool changed, bool) {
48 if (!obj)
49 return;
51 if (changed)
52 connect(obj, SIGNAL(changed()),
53 this, SIGNAL(changed()), Qt::UniqueConnection);
54 // if (req)
55 // connect(obj, SIGNAL(request(QString, int)),
56 // parent(), SLOT(request(QString, int)), Qt::UniqueConnection);
59 //------------------------------------------------------------------------------
61 // void QASAbstractObject::refresh() {
62 // QDateTime now = QDateTime::currentDateTime();
64 // if (m_lastRefreshed.isNull() || m_lastRefreshed.secsTo(now) > 1)
65 // emit request(apiLink(), m_asType);
67 // m_lastRefreshed = now;
68 // }
70 //------------------------------------------------------------------------------
72 void QASAbstractObject::updateVar(QVariantMap obj, QString& var, QString name,
73 bool& changed) {
74 QString oldVar = var;
75 if (obj.contains(name))
76 var = obj[name].toString();
77 if (oldVar != var) changed = true;
80 //------------------------------------------------------------------------------
82 void QASAbstractObject::updateVar(QVariantMap obj, bool& var, QString name,
83 bool& changed) {
84 bool oldVar = var;
85 if (obj.contains(name))
86 var = obj[name].toBool();
87 if (oldVar != var) changed = true;
90 //------------------------------------------------------------------------------
92 void QASAbstractObject::updateVar(QVariantMap obj, double& var, QString name,
93 bool& changed) {
94 double oldVar = var;
95 if (obj.contains(name))
96 var = obj[name].toDouble();
97 if (oldVar != var) changed = true;
100 //------------------------------------------------------------------------------
102 void QASAbstractObject::updateVar(QVariantMap obj, qulonglong& var,
103 QString name, bool& changed,
104 bool ignoreDecrease) {
105 qulonglong oldVar = var;
106 if (obj.contains(name))
107 var = obj[name].toULongLong();
108 if ((var > oldVar) || ((var < oldVar) && !ignoreDecrease))
109 changed = true;
112 //------------------------------------------------------------------------------
114 void QASAbstractObject::updateVar(QVariantMap obj, QDateTime& var,
115 QString name, bool& changed) {
116 QDateTime oldVar = var;
117 if (obj.contains(name))
118 var = parseTime(obj[name].toString());
119 if (oldVar != var) changed = true;
122 //------------------------------------------------------------------------------
124 void QASAbstractObject::updateVar(QVariantMap obj, QString& var, QString name1,
125 QString name2, bool& changed) {
126 if (obj.contains(name1))
127 updateVar(obj[name1].toMap(), var, name2, changed);
130 //------------------------------------------------------------------------------
132 void QASAbstractObject::updateVar(QVariantMap obj, bool& var, QString name1,
133 QString name2, bool& changed) {
134 if (obj.contains(name1))
135 updateVar(obj[name1].toMap(), var, name2, changed);
138 //------------------------------------------------------------------------------
140 void QASAbstractObject::updateVar(QVariantMap obj, double& var, QString name1,
141 QString name2, bool& changed) {
142 if (obj.contains(name1))
143 updateVar(obj[name1].toMap(), var, name2, changed);
146 //------------------------------------------------------------------------------
148 void QASAbstractObject::updateVar(QVariantMap obj, QString& var, QString name1,
149 QString name2, QString name3, bool& changed) {
150 if (obj.contains(name1))
151 updateVar(obj[name1].toMap(), var, name2, name3, changed);
154 //------------------------------------------------------------------------------
156 void QASAbstractObject::addVar(QVariantMap& obj, QString var, QString name) {
157 if (var.isEmpty())
158 return;
159 obj[name] = var;
162 //------------------------------------------------------------------------------
164 void QASAbstractObject::updateUrlOrProxy(QVariantMap obj, QString& var,
165 bool& changed) {
166 QString oldVar = var;
167 bool dummy;
168 updateVar(obj, var, "url", dummy);
169 updateVar(obj, var, "pump_io", "proxyURL", dummy);
171 if (oldVar.contains("/api/proxy/") && !var.contains("/api/proxy/"))
172 var = oldVar;
174 if (oldVar != var) changed = true;
177 //------------------------------------------------------------------------------
179 qint64 QASAbstractObject::sortIntByDateTime(QDateTime dt) {
180 #if QT_VERSION >= 0x040700
181 return dt.toMSecsSinceEpoch();
182 #else
183 return dt.toTime_t();
184 #endif