ip2cupdater.h
1 //------------------------------------------------------------------------------
2 // ip2cupdater.h
3 //------------------------------------------------------------------------------
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 // 02110-1301 USA
19 //
20 //------------------------------------------------------------------------------
21 // Copyright (C) 2009 Braden "Blzut3" Obrzut <admin@maniacsvault.net>
22 //------------------------------------------------------------------------------
23 #ifndef __IP2CUPDATER_H__
24 #define __IP2CUPDATER_H__
25 
26 #include <QByteArray>
27 #include <QNetworkReply>
28 #include <QObject>
29 
37 class IP2CUpdater : public QObject
38 {
39  Q_OBJECT
40 
41 public:
42  enum UpdateStatus
43  {
44  UpToDate,
45  UpdateNeeded,
46  UpdateCheckError
47  };
48 
49  IP2CUpdater(QObject *parent = nullptr);
50  ~IP2CUpdater() override;
51 
52  void downloadDatabase(const QString &savePath);
53  const QByteArray &downloadedData();
54 
66  bool getRollbackData(const QString &databasePath);
67 
68  bool hasDownloadedData() const
69  {
70  return !retrievedData.isEmpty();
71  }
72  bool hasRollbackData() const
73  {
74  return !rollbackData.isEmpty();
75  }
76 
77  bool isWorking() const;
78 
89  void needsUpdate(const QString &filePath);
90 
91 
104  bool rollback(const QString &savePath);
105 
114  bool saveDownloadedData(const QString &savePath);
115 
116 signals:
120  void databaseDownloadFinished(const QByteArray &downloadedData);
121  void downloadProgress(qint64 value, qint64 max);
125  void updateNeeded(int status);
126 
127 private:
128  static const QUrl dbChecksumUrl();
129  static const QUrl dbDownloadUrl();
130 
131  QNetworkAccessManager *pNetworkAccessManager;
132  QNetworkReply *pCurrentNetworkReply;
133 
134  QString lastAsyncCallPath;
135  QByteArray retrievedData;
136  QByteArray rollbackData;
137 
138  void abort();
139  void get(const QUrl &url, const char *finishedSlot);
140  bool handleRedirect(QNetworkReply &reply, const char *finishedSlot);
141  bool save(const QByteArray &saveWhat, const QString &savePath);
142 
143 private slots:
144  void checksumDownloadFinished();
145  void downloadFinished();
146 };
147 
148 #endif