1 #include "ip2cloader.h"
3 #include "configuration/doomseekerconfig.h"
4 #include "doomseekerfilepaths.h"
6 #include "ip2c/ip2cparser.h"
7 #include "ip2c/ip2cupdater.h"
23 d->ip2cParser =
new IP2CParser(IP2C::instance());
24 this->connect(d->ip2cParser, SIGNAL( parsingFinished(
bool) ),
25 SLOT( ip2cFinishedParsing(
bool) ) );
28 d->ip2cUpdater->setFilePath(DoomseekerFilePaths::ip2cDatabase());
29 this->connect(d->ip2cUpdater, SIGNAL( databaseDownloadFinished(
const QByteArray&) ),
30 SLOT( ip2cFinishUpdate(
const QByteArray&) ) );
31 this->connect(d->ip2cUpdater, SIGNAL( downloadProgress(qint64, qint64) ),
32 SIGNAL( downloadProgress(qint64, qint64) ) );
35 IP2CLoader::~IP2CLoader()
37 if (d->ip2cParser->isParsing())
39 gLog << tr(
"IP2C parser is still working. Program will close once this job is done.");
40 while (d->ip2cParser->isParsing());
44 delete d->ip2cUpdater;
47 void IP2CLoader::load()
49 bool bParseIP2CDatabase =
true;
50 bool bPerformAutomaticIP2CUpdates = gConfig.doomseeker.bIP2CountryAutoUpdate;
52 if (bPerformAutomaticIP2CUpdates)
54 int maxAge = gConfig.doomseeker.ip2CountryDatabaseMaximumAge;
56 QString databasePath = DoomseekerFilePaths::ip2cDatabase();
60 bParseIP2CDatabase =
false;
64 if (bParseIP2CDatabase)
70 void IP2CLoader::update()
75 void IP2CLoader::ip2cFinishUpdate(
const QByteArray& downloadedData)
77 if (!downloadedData.isEmpty())
79 gLog << tr(
"IP2C database finished downloading.");
80 QString filePath = DoomseekerFilePaths::ip2cDatabase();
81 d->ip2cUpdater->getRollbackData();
82 if (!d->ip2cUpdater->saveDownloadedData())
84 gLog << tr(
"Unable to save IP2C database at path: %1").arg(filePath);
90 gLog << tr(
"IP2C download has failed.");
95 void IP2CLoader::ip2cFinishedParsing(
bool bSuccess)
97 QString filePath = DoomseekerFilePaths::ip2cDatabase();
101 gLog << tr(
"Failed to read IP2C database. Reverting...");
103 if (d->ip2cUpdater == NULL || !d->ip2cUpdater->hasRollbackData())
105 gLog << tr(
"IP2C revert attempt failed. Nothing to go back to.");
108 QFile file(filePath);
111 gLog << tr(
"Using precompiled IP2C database.");
112 d->ip2cParser->readDatabaseThreaded(DoomseekerFilePaths::IP2C_QT_SEARCH_PATH);
117 d->ip2cUpdater->rollback();
120 d->ip2cParser->readDatabaseThreaded(filePath);
125 if (d->ip2cUpdater != NULL)
127 gLog << tr(
"IP2C database updated successfully.");
134 void IP2CLoader::ip2cJobsFinished()
136 IP2C::instance()->setDataAccessLockEnabled(
false);
138 if (d->ip2cParser != NULL)
140 gLog << tr(
"IP2C parsing finished.");
143 if (d->ip2cUpdater != NULL)
145 gLog << tr(
"IP2C update finished.");
150 void IP2CLoader::ip2cParseDatabase()
152 QString filePath = DoomseekerFilePaths::IP2C_QT_SEARCH_PATH;
154 gLog << tr(
"Please wait. IP2C database is being read and converted if necessary. This may take some time.");
157 IP2C::instance()->setDataAccessLockEnabled(
true);
158 d->ip2cParser->readDatabaseThreaded(filePath);
161 void IP2CLoader::ip2cStartUpdate()
163 gLog << tr(
"Starting IP2C update.");
164 IP2C::instance()->setDataAccessLockEnabled(
true);
165 QString downloadUrl = gConfig.doomseeker.ip2CountryUrl;
166 d->ip2cUpdater->downloadDatabase(downloadUrl);
static bool needsUpdate(const QString &filePath, unsigned minimumUpdateAge)
Checks if IP2C file must be updated.
IP2CUpdater is responsible for downloading a new version of database from the site.