-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathxmldatasource.cpp
More file actions
119 lines (103 loc) · 2.67 KB
/
Copy pathxmldatasource.cpp
File metadata and controls
119 lines (103 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
* Copyright (C) 2012 Cutehacks AS. All rights reserved.
* info@cutehacks.com
* http://cutehacks.com
*
* This file is part of Fly.
*
* Fly is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Fly is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Fly. If not, see <http://www.gnu.org/licenses/>.
*/
#include "xmldatasource.h"
#include "receiver.h"
#include "node.h"
XmlDataSource::XmlDataSource(QObject *parent)
: QObject(parent),
m_receiver(new XmlReceiver(this)),
m_refreshRate(-1),
m_waiting(false)
{
QObject::connect(m_receiver, SIGNAL(finished()), this, SLOT(dataReceived()));
}
XmlDataSource::~XmlDataSource()
{
}
int XmlDataSource::refreshRate() const
{
return m_refreshRate;
}
void XmlDataSource::setRefreshRate(int rate)
{
m_refreshRate = rate;
}
QUrl XmlDataSource::url() const
{
return m_url;
}
void XmlDataSource::setUrl(const QUrl &url)
{
m_url = url;
}
bool XmlDataSource::isWaiting() const
{
return m_waiting;
}
void XmlDataSource::insertQuery(const QString &name, const QString &value)
{
if (value.isNull())
m_queryItems.remove(name);
else
m_queryItems.insert(name, value);
}
void XmlDataSource::removeQuery(const QString &name)
{
m_queryItems.remove(name);
}
void XmlDataSource::start()
{
// FIXME: check if there are cached data and if they are not too old
executeQuery();
if (m_refreshRate != -1 && !m_timer.isActive())
m_timer.start(m_refreshRate, this);
}
void XmlDataSource::stop()
{
m_timer.stop();
m_waiting = false;
m_receiver->abortRequest();
}
void XmlDataSource::dataReceived()
{
m_timestamp = QDateTime::currentDateTime();
m_waiting = false;
emit dataUpdated(root());
}
void XmlDataSource::executeQuery()
{
m_waiting = true;
QList<QPair<QString, QString> > query;
const QList<QString> keys = m_queryItems.keys();
foreach (QString key, keys)
query.append(QPair<QString,QString>(key, m_queryItems.value(key)));
m_url.setQueryItems(query);
m_receiver->request(m_url);
}
void XmlDataSource::timerEvent(QTimerEvent *event)
{
if (m_timer.timerId() == event->timerId())
executeQuery();
}
Node *XmlDataSource::root() const
{
return m_receiver->rootNode();
}