1 #include "ip2cloader.h" 3 #include "configuration/doomseekerconfig.h" 4 #include "doomseekerfilepaths.h" 6 #include "ip2c/ip2cparser.h" 7 #include "ip2c/ip2cupdater.h" 18 bool updateInProgress;
27 d->updateInProgress =
false;
29 d->ip2cParser =
new IP2CParser(IP2C::instance());
30 this->connect(d->ip2cParser, SIGNAL( parsingFinished(
bool) ),
31 SLOT( ip2cFinishedParsing(
bool) ) );
34 d->ip2cUpdater->setFilePath(DoomseekerFilePaths::ip2cDatabase());
35 this->connect(d->ip2cUpdater, SIGNAL( databaseDownloadFinished(
const QByteArray&) ),
36 SLOT( ip2cFinishUpdate(
const QByteArray&) ) );
37 this->connect(d->ip2cUpdater, SIGNAL( downloadProgress(qint64, qint64) ),
38 SIGNAL( downloadProgress(qint64, qint64) ) );
39 this->connect(d->ip2cUpdater, SIGNAL(updateNeeded(
int)),
40 SLOT(onUpdateNeeded(
int)));
43 IP2CLoader::~IP2CLoader()
45 if (d->ip2cParser->isParsing())
47 gLog << tr(
"IP2C parser is still working, awaiting stop...");
48 while (d->ip2cParser->isParsing())
55 delete d->ip2cUpdater;
58 void IP2CLoader::load()
60 if (gConfig.doomseeker.bIP2CountryAutoUpdate)
62 QString databasePath = DoomseekerFilePaths::ip2cDatabase();
63 d->ip2cUpdater->needsUpdate(databasePath);
68 void IP2CLoader::onUpdateNeeded(
int status)
70 if (status == IP2CUpdater::UpdateNeeded)
78 case IP2CUpdater::UpToDate:
79 gLog << tr(
"IP2C update not needed.");
81 case IP2CUpdater::UpdateCheckError:
82 gLog << tr(
"IP2C update errored. See log for details.");
85 gLog << tr(
"IP2C update bugged out.");
92 void IP2CLoader::update()
94 d->updateInProgress =
true;
95 if (!d->ip2cParser->isParsing())
97 gLog << tr(
"Starting IP2C update.");
98 IP2C::instance()->setDataAccessLockEnabled(
true);
99 d->ip2cUpdater->downloadDatabase();
104 gLog << tr(
"IP2C update must wait until parsing of current database finishes. " 106 QTimer::singleShot(1000,
this, SLOT(update()));
110 void IP2CLoader::ip2cFinishUpdate(
const QByteArray& downloadedData)
112 d->updateInProgress =
false;
113 if (!downloadedData.isEmpty())
115 gLog << tr(
"IP2C database finished downloading.");
116 QString filePath = DoomseekerFilePaths::ip2cDatabase();
117 d->ip2cUpdater->getRollbackData();
118 if (!d->ip2cUpdater->saveDownloadedData())
120 gLog << tr(
"Unable to save IP2C database at path: %1").arg(filePath);
126 gLog << tr(
"IP2C download has failed.");
131 void IP2CLoader::ip2cFinishedParsing(
bool bSuccess)
133 QString filePath = DoomseekerFilePaths::ip2cDatabase();
137 gLog << tr(
"Failed to read IP2C database. Reverting...");
139 if (d->ip2cUpdater == NULL || !d->ip2cUpdater->hasRollbackData())
141 gLog << tr(
"IP2C revert attempt failed. Nothing to go back to.");
144 QFile file(filePath);
147 gLog << tr(
"Using precompiled IP2C database.");
148 d->ip2cParser->readDatabaseThreaded(DoomseekerFilePaths::IP2C_QT_SEARCH_PATH);
153 d->ip2cUpdater->rollback();
156 d->ip2cParser->readDatabaseThreaded(filePath);
161 gLog << tr(
"IP2C parsing finished.");
166 void IP2CLoader::ip2cJobsFinished()
168 if (!d->ip2cUpdater->isWorking() && !d->ip2cParser->isParsing() && !d->updateInProgress)
170 IP2C::instance()->setDataAccessLockEnabled(
false);
175 void IP2CLoader::ip2cParseDatabase()
177 QString filePath = DoomseekerFilePaths::IP2C_QT_SEARCH_PATH;
179 gLog << tr(
"Please wait. IP2C database is being read. This may take some time.");
182 IP2C::instance()->setDataAccessLockEnabled(
true);
183 d->ip2cParser->readDatabaseThreaded(filePath);
IP2CUpdater is responsible for downloading a new version of database from the site.