23 #include "ip2cloader.h" 
   25 #include "configuration/doomseekerconfig.h" 
   26 #include "doomseekerfilepaths.h" 
   28 #include "ip2c/ip2c.h" 
   29 #include "ip2c/ip2cparser.h" 
   30 #include "ip2c/ip2cupdater.h" 
   40         bool updateInProgress;
 
   50         d->updateInProgress = 
false;
 
   51         d->inFallbackMode = 
false;
 
   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)
 
   96                 case IP2CUpdater::UpToDate:
 
   97                         gLog << tr(
"IP2C update not needed.");
 
   99                 case IP2CUpdater::UpdateCheckError:
 
  100                         gLog << tr(
"IP2C update errored. See log for details.");
 
  103                         gLog << tr(
"IP2C update bugged out.");
 
  110 void IP2CLoader::update()
 
  112         d->updateInProgress = 
true;
 
  113         if (!d->ip2cParser->isParsing())
 
  115                 gLog << tr(
"Starting IP2C update.");
 
  116                 IP2C::instance()->setDataAccessLockEnabled(
true);
 
  122                 gLog << tr(
"IP2C update must wait until parsing of current database finishes. " 
  123                                 "Waiting 1 second.");
 
  124                 QTimer::singleShot(1000, 
this, SLOT(update()));
 
  128 void IP2CLoader::ip2cFinishUpdate(
const QByteArray &downloadedData)
 
  130         d->updateInProgress = 
false;
 
  131         if (!downloadedData.isEmpty())
 
  133                 gLog << tr(
"IP2C database finished downloading.");
 
  135                 d->ip2cUpdater->getRollbackData(filePath);
 
  136                 if (!d->ip2cUpdater->saveDownloadedData(filePath))
 
  137                         gLog << tr(
"Unable to save IP2C database at path: %1").arg(filePath);
 
  142                 gLog << tr(
"IP2C download has failed.");
 
  147 void IP2CLoader::ip2cFinishedParsing(
bool bSuccess)
 
  153                 if (d->inFallbackMode)
 
  155                         gLog << tr(
"Failed to read IP2C fallback. Stopping.");
 
  159                 gLog << tr(
"Failed to read IP2C database. Reverting...");
 
  161                 d->inFallbackMode = 
true;
 
  162                 if (d->ip2cUpdater == 
nullptr || !d->ip2cUpdater->hasRollbackData())
 
  164                         gLog << tr(
"IP2C revert attempt failed. Nothing to go back to.");
 
  167                         QFile file(filePath);
 
  171                         if (!preinstalledDbPath.isEmpty())
 
  173                                 gLog << tr(
"Trying to use preinstalled IP2C database.");
 
  174                                 d->ip2cParser->readDatabaseThreaded(preinstalledDbPath);
 
  182                         d->ip2cUpdater->rollback(filePath);
 
  185                         d->ip2cParser->readDatabaseThreaded(filePath);
 
  190                 gLog << tr(
"IP2C parsing finished.");
 
  196 void IP2CLoader::ip2cJobsFinished()
 
  198         if (!d->ip2cUpdater->isWorking() && !d->ip2cParser->isParsing() && !d->updateInProgress)
 
  200                 IP2C::instance()->setDataAccessLockEnabled(
false);
 
  205 void IP2CLoader::ip2cParseDatabase()
 
  208         if (!filePath.isEmpty())
 
  210                 gLog << tr(
"Please wait. IP2C database is being read. This may take some time.");
 
  213                 d->inFallbackMode = 
false;
 
  214                 IP2C::instance()->setDataAccessLockEnabled(
true);
 
  215                 d->ip2cParser->readDatabaseThreaded(filePath);
 
  219                 if (!gConfig.doomseeker.bIP2CountryAutoUpdate)
 
  221                         gLog << tr(
"Did not find any IP2C database. IP2C functionality will be disabled.");
 
  222                         gLog << tr(
"You may install an IP2C database from the \"File\" menu.");