23 #include "ip2cloader.h" 25 #include "configuration/doomseekerconfig.h" 26 #include "doomseekerfilepaths.h" 27 #include "ip2c/ip2c.h" 28 #include "ip2c/ip2cparser.h" 29 #include "ip2c/ip2cupdater.h" 40 bool updateInProgress;
50 d->updateInProgress =
false;
51 d->inFallbackMode =
false;
53 d->ip2cParser =
new IP2CParser(IP2C::instance());
54 this->connect(d->ip2cParser, SIGNAL( parsingFinished(
bool) ),
55 SLOT( ip2cFinishedParsing(
bool) ) );
58 this->connect(d->ip2cUpdater, SIGNAL( databaseDownloadFinished(
const QByteArray&) ),
59 SLOT( ip2cFinishUpdate(
const QByteArray&) ) );
60 this->connect(d->ip2cUpdater, SIGNAL( downloadProgress(qint64, qint64) ),
61 SIGNAL( downloadProgress(qint64, qint64) ) );
62 this->connect(d->ip2cUpdater, SIGNAL(updateNeeded(
int)),
63 SLOT(onUpdateNeeded(
int)));
66 IP2CLoader::~IP2CLoader()
68 if (d->ip2cParser->isParsing())
70 gLog << tr(
"IP2C parser is still working, awaiting stop...");
71 while (d->ip2cParser->isParsing())
78 delete d->ip2cUpdater;
81 void IP2CLoader::load()
83 if (gConfig.doomseeker.bIP2CountryAutoUpdate)
88 void IP2CLoader::onUpdateNeeded(
int status)
90 if (status == IP2CUpdater::UpdateNeeded)
98 case IP2CUpdater::UpToDate:
99 gLog << tr(
"IP2C update not needed.");
101 case IP2CUpdater::UpdateCheckError:
102 gLog << tr(
"IP2C update errored. See log for details.");
105 gLog << tr(
"IP2C update bugged out.");
112 void IP2CLoader::update()
114 d->updateInProgress =
true;
115 if (!d->ip2cParser->isParsing())
117 gLog << tr(
"Starting IP2C update.");
118 IP2C::instance()->setDataAccessLockEnabled(
true);
124 gLog << tr(
"IP2C update must wait until parsing of current database finishes. " 126 QTimer::singleShot(1000,
this, SLOT(update()));
130 void IP2CLoader::ip2cFinishUpdate(
const QByteArray& downloadedData)
132 d->updateInProgress =
false;
133 if (!downloadedData.isEmpty())
135 gLog << tr(
"IP2C database finished downloading.");
137 d->ip2cUpdater->getRollbackData(filePath);
138 if (!d->ip2cUpdater->saveDownloadedData(filePath))
140 gLog << tr(
"Unable to save IP2C database at path: %1").arg(filePath);
146 gLog << tr(
"IP2C download has failed.");
151 void IP2CLoader::ip2cFinishedParsing(
bool bSuccess)
157 if (d->inFallbackMode)
159 gLog << tr(
"Failed to read IP2C fallback. Stopping.");
163 gLog << tr(
"Failed to read IP2C database. Reverting...");
165 d->inFallbackMode =
true;
166 if (d->ip2cUpdater == NULL || !d->ip2cUpdater->hasRollbackData())
168 gLog << tr(
"IP2C revert attempt failed. Nothing to go back to.");
171 QFile file(filePath);
175 if (!preinstalledDbPath.isEmpty())
177 gLog << tr(
"Trying to use preinstalled IP2C database.");
178 d->ip2cParser->readDatabaseThreaded(preinstalledDbPath);
188 d->ip2cUpdater->rollback(filePath);
191 d->ip2cParser->readDatabaseThreaded(filePath);
196 gLog << tr(
"IP2C parsing finished.");
201 void IP2CLoader::ip2cJobsFinished()
203 if (!d->ip2cUpdater->isWorking() && !d->ip2cParser->isParsing() && !d->updateInProgress)
205 IP2C::instance()->setDataAccessLockEnabled(
false);
210 void IP2CLoader::ip2cParseDatabase()
213 if (!filePath.isEmpty())
215 gLog << tr(
"Please wait. IP2C database is being read. This may take some time.");
218 d->inFallbackMode =
false;
219 IP2C::instance()->setDataAccessLockEnabled(
true);
220 d->ip2cParser->readDatabaseThreaded(filePath);
224 if (!gConfig.doomseeker.bIP2CountryAutoUpdate)
226 gLog << tr(
"Did not find any IP2C database. IP2C functionality will be disabled.");
227 gLog << tr(
"You may install an IP2C database from the \"File\" menu.");
IP2CUpdater is responsible for downloading a new version of database from the site.
static QString ip2cDatabaseAny()
static QString ip2cDatabase()