BiblioteQ: Source/biblioteq_swifty.h Source File

BiblioteQ

biblioteq_swifty.h
1 /*
2 ** Copyright (c) Alexis Megas.
3 ** All rights reserved.
4 **
5 ** Redistribution and use in source and binary forms, with or without
6 ** modification, are permitted provided that the following conditions
7 ** are met:
8 ** 1. Redistributions of source code must retain the above copyright
9 ** notice, this list of conditions and the following disclaimer.
10 ** 2. Redistributions in binary form must reproduce the above copyright
11 ** notice, this list of conditions and the following disclaimer in the
12 ** documentation and/or other materials provided with the distribution.
13 ** 3. The name of the author may not be used to endorse or promote products
14 ** derived from Swifty without specific prior written permission.
15 **
16 ** SWIFTY IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 ** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 ** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 ** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 ** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 ** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 ** SWIFTY, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #ifndef _swifty_h_
29 #define _swifty_h_
30
31 #include <QNetworkAccessManager>
32 #include <QNetworkReply>
33 #include <QPointer>
34
35 class swifty: public QNetworkAccessManager
36 {
37 Q_OBJECT
38
39 public:
40 swifty(const QString &current_version,
41 const QString &search_for_string,
42 const QUrl &url,
43 QObject *parent):QNetworkAccessManager(parent)
44 {
45 m_current_version = m_newest_version = current_version;
46 m_search_for_string = search_for_string;
47 m_url = url;
48 }
49
50 ~swifty()
51 {
52 }
53
54 QString newest_version(void) const
55 {
56 return m_newest_version;
57 }
58
59 void download()
60 {
61 m_buffer.clear();
62
63 if(m_reply)
64 m_reply->deleteLater();
65
66 m_reply = get(QNetworkRequest(m_url));
67 connect(m_reply,
68 SIGNAL(finished(void)),
69 this,
70 SLOT(slot_finished(void)));
71 connect(m_reply,
72 SIGNAL(readyRead(void)),
73 this,
74 SLOT(slot_ready_read(void)));
75 }
76
77 private:
78 QByteArray m_buffer;
79 QPointer<QNetworkReply> m_reply;
80 QString m_current_version;
81 QString m_newest_version;
82 QString m_search_for_string;
83 QUrl m_url;
84
85 private slots:
86 void slot_finished(void)
87 {
88 if(m_reply)
89 m_reply->deleteLater();
90
91 auto index = m_buffer.indexOf(m_search_for_string.toUtf8());
92
93 if(index >= 0)
94 {
95 auto version = m_buffer.mid
96 (index + m_search_for_string.toUtf8().length());
97
98 version = version.mid(0, version.indexOf('\n')).replace('"', "").
99 trimmed();
100
101 if(m_current_version != version)
102 {
103 m_newest_version = version;
104 emit different(m_newest_version);
105 }
106 else
107 emit same();
108 }
109 }
110
111 void slot_ready_read(void)
112 {
113 while(m_reply && m_reply->bytesAvailable() > 0)
114 m_buffer.append(m_reply->readAll());
115 }
116
117 signals:
118 void different(const QString &new_version);
119 void same(void);
120 };
121
122 #endif
swifty
Definition: biblioteq_swifty.h:36