23 #include "ip2cupdater.h"
29 #include <QTemporaryFile>
35 IP2CUpdater::IP2CUpdater()
37 pCurrentNetworkReply = NULL;
38 pNetworkAccessManager =
new FixedNetworkAccessManager();
41 IP2CUpdater::~IP2CUpdater()
43 if (pCurrentNetworkReply != NULL)
45 pCurrentNetworkReply->disconnect();
46 pCurrentNetworkReply->abort();
47 pCurrentNetworkReply->deleteLater();
50 if (pNetworkAccessManager != NULL)
52 pNetworkAccessManager->deleteLater();
56 void IP2CUpdater::downloadDatabase(
const QUrl& netLocation)
58 retrievedData.clear();
60 QNetworkRequest request;
61 request.setUrl(netLocation);
62 qDebug() << netLocation;
65 pCurrentNetworkReply = pNetworkAccessManager->get(request);
66 this->connect(pCurrentNetworkReply, SIGNAL( downloadProgress(qint64, qint64) ),
67 SIGNAL( downloadProgress(qint64, qint64) ) );
68 this->connect(pCurrentNetworkReply, SIGNAL( finished() ),
69 SLOT( downloadFinished() ) );
72 void IP2CUpdater::downloadFinished()
74 QByteArray data = pCurrentNetworkReply->readAll();
76 QUrl possibleRedirectUrl = pCurrentNetworkReply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl();
77 QUrl url = pCurrentNetworkReply->request().url();
78 if (!possibleRedirectUrl.isEmpty()
79 && possibleRedirectUrl != url)
82 if (possibleRedirectUrl.isRelative())
84 possibleRedirectUrl = url.resolved(possibleRedirectUrl);
87 pCurrentNetworkReply->deleteLater();
88 downloadDatabase(possibleRedirectUrl);
92 pCurrentNetworkReply->deleteLater();
93 pCurrentNetworkReply = NULL;
96 QTemporaryFile tmpFile;
101 QString tmpFilePath = tmpFile.fileName();
103 QByteArray uncompressedData;
104 gzFile gz = gzopen(tmpFilePath.toAscii().constData(),
"rb");
109 while((bytesRead = gzread(gz, chunk, 131072)) != 0)
111 uncompressedData.append(QByteArray(chunk, bytesRead));
115 retrievedData = uncompressedData;
125 rollbackData.clear();
127 QFile file(pathToFile);
133 if (!file.open(QIODevice::ReadOnly))
138 rollbackData = file.readAll();
146 if (filePath.isEmpty())
151 if (minimumUpdateAge == 0)
153 minimumUpdateAge = 1;
156 QFileInfo fileInfo(filePath);
157 if (fileInfo.exists())
159 QDateTime current = QDateTime::currentDateTime();
160 QDateTime lastModified = fileInfo.lastModified();
162 int daysTo = lastModified.daysTo(current);
170 return (
unsigned)daysTo >= minimumUpdateAge;
178 bool bSuccess = save(rollbackData);
179 rollbackData.clear();
184 bool IP2CUpdater::save(
const QByteArray& saveWhat)
186 if (saveWhat.isEmpty())
191 QFile file(pathToFile);
192 if (!file.open(QIODevice::WriteOnly))
197 file.write(saveWhat);
205 return save(retrievedData);
static bool needsUpdate(const QString &filePath, unsigned minimumUpdateAge)
Checks if IP2C file must be updated.
bool getRollbackData()
Obtains rollback data from pathToFile file.
void databaseDownloadFinished(const QByteArray &downloadedData)
In case of failure the downloadedData array will be empty.
static QString userAgent()
WWW User Agent used for HTTP communications.
bool rollback()
Saves data to the pathToFile file. This data must be first obtained through the rollback method...
bool saveDownloadedData()
Saves recently downloaded data to the pathToFile file.