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