| 1 | #include "packagepage2.h" | 
 
 
 
 
 | 2 | #include "ui_packagepage2.h" | 
 
 
 
 
 | 3 |  | 
 
 
 
 
 | 4 | const QString PackagePage2::ZipCacheFile = "jsoncache.zip"; | 
 
 
 
 
 | 5 | const QString PackagePage2::PackagesCacheUrl = "http://mods.oni2.net/jsoncache/"+ZipCacheFile; | 
 
 
 
 
 | 6 | const QString PackagePage2::CacheFile = "nodes.json"; | 
 
 
 
 
 | 7 |  | 
 
 
 
 
 | 8 | PackagePage2::PackagePage2(Logger *myLogger, QWidget *parent) : | 
 
 
 
 
 | 9 | QWizardPage(parent), | 
 
 
 
 
 | 10 | ui(new Ui::PackagePage2) | 
 
 
 
 
 | 11 | { | 
 
 
 
 
 | 12 | ui->setupUi(this); | 
 
 
 
 
 | 13 | this->myLogger=myLogger; | 
 
 
 
 
 | 14 | this->setTitle("Mandatory Fields"); | 
 
 
 
 
 | 15 |  | 
 
 
 
 
 | 16 | //Register fields to be accessible in another pages //Not using mandatory field, it would require empty verification too... | 
 
 
 
 
 | 17 | registerField("leModName", ui->leModName); | 
 
 
 
 
 | 18 | registerField("leAuthors", ui->leAuthors); | 
 
 
 
 
 | 19 | registerField("leVersion", ui->leVersion); | 
 
 
 
 
 | 20 | registerField("ptDescription", ui->ptDescription,"plainText"); | 
 
 
 
 
 | 21 | registerField("lePackageNumber", ui->lePackageNumber); | 
 
 
 
 
 | 22 | registerField("rbReplace", ui->rbReplace); | 
 
 
 
 
 | 23 | } | 
 
 
 
 
 | 24 |  | 
 
 
 
 
 | 25 | bool PackagePage2::validatePage(){ | 
 
 
 
 
 | 26 | QString modName=ui->leModName->text(); | 
 
 
 
 
 | 27 | QString authors=ui->leAuthors->text(); | 
 
 
 
 
 | 28 | QString version=ui->leVersion->text(); | 
 
 
 
 
 | 29 | QString description=ui->ptDescription->toPlainText(); | 
 
 
 
 
 | 30 | QString number=ui->lePackageNumber->text(); | 
 
 
 
 
 | 31 |  | 
 
 
 
 
 | 32 | bool emptyContent=Util::checkEmptySpaces(QStringList()<<modName<<authors<<version<<description<<number); | 
 
 
 
 
 | 33 |  | 
 
 
 
 
 | 34 |  | 
 
 
 
 
 | 35 | if(emptyContent){ | 
 
 
 
 
 | 36 | Util::showErrorPopUp("You need to fill all fields first!"); | 
 
 
 
 
 | 37 | return false; | 
 
 
 
 
 | 38 | } | 
 
 
 
 
 | 39 |  | 
 
 
 
 
 | 40 | if(number.size()!=5){ | 
 
 
 
 
 | 41 | Util::showErrorPopUp("Invalid number format. It should contain 5 numbers."); | 
 
 
 
 
 | 42 | return false; | 
 
 
 
 
 | 43 | } | 
 
 
 
 
 | 44 |  | 
 
 
 
 
 | 45 | if(!Util::isStringInteger(number)){ | 
 
 
 
 
 | 46 | Util::showErrorPopUp("Number is not numeric."); | 
 
 
 
 
 | 47 | return false; | 
 
 
 
 
 | 48 | } | 
 
 
 
 
 | 49 |  | 
 
 
 
 
 | 50 | return true; | 
 
 
 
 
 | 51 | } | 
 
 
 
 
 | 52 |  | 
 
 
 
 
 | 53 | PackagePage2::~PackagePage2() | 
 
 
 
 
 | 54 | { | 
 
 
 
 
 | 55 | delete ui; | 
 
 
 
 
 | 56 | } | 
 
 
 
 
 | 57 |  | 
 
 
 
 
 | 58 | void PackagePage2::on_pbCheck_clicked() | 
 
 
 
 
 | 59 | { | 
 
 
 
 
 | 60 | QString number = ui->lePackageNumber->text(); | 
 
 
 
 
 | 61 |  | 
 
 
 
 
 | 62 | if(Util::checkEmptySpaces(QStringList(number))){ | 
 
 
 
 
 | 63 | Util::showErrorPopUp("Number is empty. Please fill it first."); | 
 
 
 
 
 | 64 | return; | 
 
 
 
 
 | 65 | } | 
 
 
 
 
 | 66 |  | 
 
 
 
 
 | 67 | if(number.size()!=5){ | 
 
 
 
 
 | 68 | Util::showErrorPopUp("Invalid number format. It should contain 5 numbers."); | 
 
 
 
 
 | 69 | return; | 
 
 
 
 
 | 70 | } | 
 
 
 
 
 | 71 |  | 
 
 
 
 
 | 72 | if(Util::isStringInteger(number)){ | 
 
 
 
 
 | 73 |  | 
 
 
 
 
 | 74 | bool necessaryToRedownload=false; | 
 
 
 
 
 | 75 |  | 
 
 
 
 
 | 76 | QFile *file = new QFile(GlobalVars::VagoTemporaryDir+"/"+this->ZipCacheFile); | 
 
 
 
 
 | 77 |  | 
 
 
 
 
 | 78 | if(!file->exists()){ | 
 
 
 
 
 | 79 | necessaryToRedownload=true; //File doesn't exist yet, necessary to download | 
 
 
 
 
 | 80 | } | 
 
 
 
 
 | 81 | else if (QDateTime::currentDateTime().toTime_t()-QFileInfo(*file).lastModified().toTime_t() > 150){ //checks between 2 minutes (give more 30 seconds due to zip extraction) | 
 
 
 
 
 | 82 | necessaryToRedownload=true; //File already exists but already expired (+2 mins without update) | 
 
 
 
 
 | 83 | } | 
 
 
 
 
 | 84 | delete file; | 
 
 
 
 
 | 85 |  | 
 
 
 
 
 | 86 | if(necessaryToRedownload){ | 
 
 
 
 
 | 87 | //let's start the search in the web, so we make sure it doesn't exists yet | 
 
 
 
 
 | 88 | QNetworkAccessManager *manager = new QNetworkAccessManager(this); | 
 
 
 
 
 | 89 | connect(manager, SIGNAL(finished(QNetworkReply*)), | 
 
 
 
 
 | 90 | this, SLOT(downloadPackagesCache(QNetworkReply*))); | 
 
 
 
 
 | 91 |  | 
 
 
 
 
 | 92 | //This timestamp is to guarantee that the cache received is fresh even through proxys | 
 
 
 
 
 | 93 | QDateTime currTime = QDateTime::currentDateTime(); | 
 
 
 
 
 | 94 | QString t_time =  QString::number(currTime.toTime_t()); | 
 
 
 
 
 | 95 |  | 
 
 
 
 
 | 96 | manager->get(QNetworkRequest(QUrl(this->PackagesCacheUrl+"?ts="+t_time))); | 
 
 
 
 
 | 97 | } | 
 
 
 
 
 | 98 | else{ //Not needed to download! :) Let's use our local cache. | 
 
 
 
 
 | 99 | checkForPackagesInCache(); | 
 
 
 
 
 | 100 | } | 
 
 
 
 
 | 101 | } | 
 
 
 
 
 | 102 | else{ | 
 
 
 
 
 | 103 | Util::showErrorPopUp("Number is not numeric."); | 
 
 
 
 
 | 104 | } | 
 
 
 
 
 | 105 | } | 
 
 
 
 
 | 106 |  | 
 
 
 
 
 | 107 | void PackagePage2::downloadPackagesCache(QNetworkReply *result){ | 
 
 
 
 
 | 108 |  | 
 
 
 
 
 | 109 | if(result->error()==QNetworkReply::NoError){ | 
 
 
 
 
 | 110 |  | 
 
 
 
 
 | 111 | QFile *file = new QFile(GlobalVars::VagoTemporaryDir+"/"+this->ZipCacheFile); | 
 
 
 
 
 | 112 |  | 
 
 
 
 
 | 113 | // Create temp folder if it doesn't exist | 
 
 
 
 
 | 114 | if(!QDir(GlobalVars::VagoTemporaryDir).exists()){ | 
 
 
 
 
 | 115 | QDir().mkdir(GlobalVars::VagoTemporaryDir); | 
 
 
 
 
 | 116 | } | 
 
 
 
 
 | 117 |  | 
 
 
 
 
 | 118 | if(!file->open(QIODevice::WriteOnly)){ | 
 
 
 
 
 | 119 | const QString error="Error fetching package data: creating cache file."; | 
 
 
 
 
 | 120 | this->myLogger->writeString(error); | 
 
 
 
 
 | 121 | Util::showErrorPopUp(error); | 
 
 
 
 
 | 122 | return; | 
 
 
 
 
 | 123 | } | 
 
 
 
 
 | 124 | file->write(result->readAll()); | 
 
 
 
 
 | 125 | file->close(); | 
 
 
 
 
 | 126 |  | 
 
 
 
 
 | 127 | //Let's extract the cache data | 
 
 
 
 
 | 128 | UnZip uz; | 
 
 
 
 
 | 129 | UnZip::ErrorCode ec = uz.openArchive(GlobalVars::VagoTemporaryDir+"/"+this->ZipCacheFile); | 
 
 
 
 
 | 130 | checkForUnzipError(ec); | 
 
 
 
 
 | 131 |  | 
 
 
 
 
 | 132 | //Extract the cache files | 
 
 
 
 
 | 133 | ec = uz.extractAll(GlobalVars::VagoTemporaryDir); | 
 
 
 
 
 | 134 | checkForUnzipError(ec); | 
 
 
 
 
 | 135 |  | 
 
 
 
 
 | 136 | //Close zip archive | 
 
 
 
 
 | 137 | uz.closeArchive(); | 
 
 
 
 
 | 138 |  | 
 
 
 
 
 | 139 | delete file; | 
 
 
 
 
 | 140 |  | 
 
 
 
 
 | 141 | checkForPackagesInCache(); | 
 
 
 
 
 | 142 |  | 
 
 
 
 
 | 143 | } | 
 
 
 
 
 | 144 | else{ | 
 
 
 
 
 | 145 | const QString error="An error occurred checking number availability:\n\n"+result->errorString(); | 
 
 
 
 
 | 146 | this->myLogger->writeString(error); | 
 
 
 
 
 | 147 | Util::showErrorLogPopUp(error); | 
 
 
 
 
 | 148 | } | 
 
 
 
 
 | 149 |  | 
 
 
 
 
 | 150 | result->deleteLater(); | 
 
 
 
 
 | 151 | } | 
 
 
 
 
 | 152 |  | 
 
 
 
 
 | 153 | void PackagePage2::checkForPackagesInCache(){ | 
 
 
 
 
 | 154 | QString packageNumber=ui->lePackageNumber->text(); | 
 
 
 
 
 | 155 |  | 
 
 
 
 
 | 156 | QFile *file = new QFile(GlobalVars::VagoTemporaryDir+"/"+this->CacheFile); //let's read the chache unzipped | 
 
 
 
 
 | 157 | if(!file->open(QIODevice::ReadOnly)){ | 
 
 
 
 
 | 158 | const QString error="Error reading downloaded package cache data."; | 
 
 
 
 
 | 159 | this->myLogger->writeString(error); | 
 
 
 
 
 | 160 | Util::showErrorPopUp(error); | 
 
 
 
 
 | 161 | return; | 
 
 
 
 
 | 162 | } | 
 
 
 
 
 | 163 | //Read file cache to ram | 
 
 
 
 
 | 164 | QString data=file->readAll(); | 
 
 
 
 
 | 165 |  | 
 
 
 
 
 | 166 |  | 
 
 
 
 
 | 167 | //Let's play with json engine | 
 
 
 
 
 | 168 | QScriptEngine engine; | 
 
 
 
 
 | 169 | QScriptValue sc = engine.evaluate("(" + data + ")"); | 
 
 
 
 
 | 170 |  | 
 
 
 
 
 | 171 | QScriptValue currNumber; | 
 
 
 
 
 | 172 | QString existingModName,existingModUrl; | 
 
 
 
 
 | 173 |  | 
 
 
 
 
 | 174 | QScriptValueIterator it(sc); | 
 
 
 
 
 | 175 |  | 
 
 
 
 
 | 176 | while (it.hasNext()) { | 
 
 
 
 
 | 177 | it.next(); | 
 
 
 
 
 | 178 | currNumber=it.value().toObject().property("field_package_number").toObject().property("und").toObject().property("0").toObject().property("value"); | 
 
 
 
 
 | 179 | if(currNumber.isValid() && currNumber.toString() == packageNumber){ | 
 
 
 
 
 | 180 | existingModName = it.value().toObject().property("title").toString(); | 
 
 
 
 
 | 181 | existingModUrl = it.value().toObject().property("path").toString(); | 
 
 
 
 
 | 182 | break; | 
 
 
 
 
 | 183 | } | 
 
 
 
 
 | 184 | } | 
 
 
 
 
 | 185 |  | 
 
 
 
 
 | 186 | if(!existingModName.isEmpty()){ | 
 
 
 
 
 | 187 | Util::showRichErrorPopUp("Package "+packageNumber+" is already being used by the following mod:<br/><br/>"+ | 
 
 
 
 
 | 188 | existingModName+"<br/><br/>"+ | 
 
 
 
 
 | 189 | "More information <a href='"+existingModUrl+"'>here</a>."); | 
 
 
 
 
 | 190 | } | 
 
 
 
 
 | 191 | else{ | 
 
 
 
 
 | 192 | Util::showPopUp("Package "+packageNumber+" seems it's not being used yet! :)"); | 
 
 
 
 
 | 193 | } | 
 
 
 
 
 | 194 |  | 
 
 
 
 
 | 195 | delete file; | 
 
 
 
 
 | 196 | } | 
 
 
 
 
 | 197 |  | 
 
 
 
 
 | 198 | void PackagePage2::on_cbType_currentIndexChanged(int index) | 
 
 
 
 
 | 199 | { | 
 
 
 
 
 | 200 | ui->lePackageNumber->setText(QString().setNum(index+1)+"000X"); | 
 
 
 
 
 | 201 | } | 
 
 
 
 
 | 202 |  | 
 
 
 
 
 | 203 | void PackagePage2::checkForUnzipError(UnZip::ErrorCode ec){ | 
 
 
 
 
 | 204 | if (ec != UnZip::Ok){ | 
 
 
 
 
 | 205 | const QString error="Error found while unzipping the package data. Error number = "+QString::number(ec); | 
 
 
 
 
 | 206 | Util::showErrorPopUp(error); | 
 
 
 
 
 | 207 | this->myLogger->writeString(error); | 
 
 
 
 
 | 208 | } | 
 
 
 
 
 | 209 | } |