| 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 | ui->lbFieldsReadOnly->setText("<html><span style='color:#0000ff;'>* Fields read only  by AEI2 when the package " | 
 
 
 
 
 | 25 | "isn't at the mod depot.</span></html>"); // Don't use rich text in qtdesigner because it generates platform dependent code | 
 
 
 
 
 | 26 | QString htmlAsterisk="<html><span style='color:#0000ff;'>*</span></html>"; | 
 
 
 
 
 | 27 | ui->lbAsteriscAuthors->setText(htmlAsterisk); | 
 
 
 
 
 | 28 | ui->lbAsteriscModName->setText(htmlAsterisk); | 
 
 
 
 
 | 29 | ui->lbAsteriscVersion->setText(htmlAsterisk); | 
 
 
 
 
 | 30 | ui->lbAsteriscDescription->setText(htmlAsterisk); | 
 
 
 
 
 | 31 | } | 
 
 
 
 
 | 32 |  | 
 
 
 
 
 | 33 | bool PackagePage2::validatePage(){ | 
 
 
 
 
 | 34 | QString modName=ui->leModName->text(); | 
 
 
 
 
 | 35 | QString authors=ui->leAuthors->text(); | 
 
 
 
 
 | 36 | QString version=ui->leVersion->text(); | 
 
 
 
 
 | 37 | QString description=ui->ptDescription->toPlainText(); | 
 
 
 
 
 | 38 | QString number=ui->lePackageNumber->text(); | 
 
 
 
 
 | 39 |  | 
 
 
 
 
 | 40 | bool emptyContent=Util::checkEmptySpaces(QStringList()<<modName<<authors<<version<<description<<number); | 
 
 
 
 
 | 41 |  | 
 
 
 
 
 | 42 |  | 
 
 
 
 
 | 43 | if(emptyContent){ | 
 
 
 
 
 | 44 | Util::showErrorPopUp("You need to fill all fields first!"); | 
 
 
 
 
 | 45 | return false; | 
 
 
 
 
 | 46 | } | 
 
 
 
 
 | 47 |  | 
 
 
 
 
 | 48 | if(number.size()!=5){ | 
 
 
 
 
 | 49 | Util::showErrorPopUp("Invalid number format. It should contain 5 numbers."); | 
 
 
 
 
 | 50 | return false; | 
 
 
 
 
 | 51 | } | 
 
 
 
 
 | 52 |  | 
 
 
 
 
 | 53 | if(!Util::isStringInteger(number)){ | 
 
 
 
 
 | 54 | Util::showErrorPopUp("Number is not numeric."); | 
 
 
 
 
 | 55 | return false; | 
 
 
 
 
 | 56 | } | 
 
 
 
 
 | 57 |  | 
 
 
 
 
 | 58 | return true; | 
 
 
 
 
 | 59 | } | 
 
 
 
 
 | 60 |  | 
 
 
 
 
 | 61 | PackagePage2::~PackagePage2() | 
 
 
 
 
 | 62 | { | 
 
 
 
 
 | 63 | delete ui; | 
 
 
 
 
 | 64 | } | 
 
 
 
 
 | 65 |  | 
 
 
 
 
 | 66 | void PackagePage2::on_pbCheck_clicked() | 
 
 
 
 
 | 67 | { | 
 
 
 
 
 | 68 | QString number = ui->lePackageNumber->text(); | 
 
 
 
 
 | 69 |  | 
 
 
 
 
 | 70 | if(Util::checkEmptySpaces(QStringList(number))){ | 
 
 
 
 
 | 71 | Util::showErrorPopUp("Number is empty. Please fill it first."); | 
 
 
 
 
 | 72 | return; | 
 
 
 
 
 | 73 | } | 
 
 
 
 
 | 74 |  | 
 
 
 
 
 | 75 | if(number.size()!=5){ | 
 
 
 
 
 | 76 | Util::showErrorPopUp("Invalid number format. It should contain 5 numeric characters."); | 
 
 
 
 
 | 77 | return; | 
 
 
 
 
 | 78 | } | 
 
 
 
 
 | 79 |  | 
 
 
 
 
 | 80 | if(Util::isStringInteger(number)){ | 
 
 
 
 
 | 81 |  | 
 
 
 
 
 | 82 | bool necessaryToRedownload=false; | 
 
 
 
 
 | 83 |  | 
 
 
 
 
 | 84 | QFile file(GlobalVars::VagoTemporaryDir+"/"+this->ZipCacheFile); | 
 
 
 
 
 | 85 |  | 
 
 
 
 
 | 86 | if(!file.exists()){ | 
 
 
 
 
 | 87 | necessaryToRedownload=true; //File doesn't exist yet, necessary to download | 
 
 
 
 
 | 88 | } | 
 
 
 
 
 | 89 | else if (QDateTime::currentDateTime().toTime_t()-QFileInfo(file).lastModified().toTime_t() > 150){ //checks between 2 minutes (give more 30 seconds due to zip extraction) | 
 
 
 
 
 | 90 | necessaryToRedownload=true; //File already exists but already expired (+2 mins without update) | 
 
 
 
 
 | 91 | } | 
 
 
 
 
 | 92 |  | 
 
 
 
 
 | 93 | if(necessaryToRedownload){ | 
 
 
 
 
 | 94 | //let's start the search in the web, so we make sure it doesn't exists yet | 
 
 
 
 
 | 95 | QNetworkAccessManager *manager = new QNetworkAccessManager(this); | 
 
 
 
 
 | 96 | connect(manager, SIGNAL(finished(QNetworkReply*)), | 
 
 
 
 
 | 97 | this, SLOT(downloadPackagesCache(QNetworkReply*))); | 
 
 
 
 
 | 98 |  | 
 
 
 
 
 | 99 | //This timestamp is to guarantee that the cache received is fresh even through proxys | 
 
 
 
 
 | 100 | QDateTime currTime = QDateTime::currentDateTime(); | 
 
 
 
 
 | 101 | QString t_time =  QString::number(currTime.toTime_t()); | 
 
 
 
 
 | 102 |  | 
 
 
 
 
 | 103 | manager->get(QNetworkRequest(QUrl(this->PackagesCacheUrl+"?ts="+t_time))); | 
 
 
 
 
 | 104 | } | 
 
 
 
 
 | 105 | else{ //Not needed to download! :) Let's use our local cache. | 
 
 
 
 
 | 106 | checkForPackagesInCache(); | 
 
 
 
 
 | 107 | } | 
 
 
 
 
 | 108 | } | 
 
 
 
 
 | 109 | else{ | 
 
 
 
 
 | 110 | Util::showErrorPopUp("Number is not numeric."); | 
 
 
 
 
 | 111 | } | 
 
 
 
 
 | 112 | } | 
 
 
 
 
 | 113 |  | 
 
 
 
 
 | 114 | void PackagePage2::downloadPackagesCache(QNetworkReply *result){ | 
 
 
 
 
 | 115 |  | 
 
 
 
 
 | 116 | if(result->error()==QNetworkReply::NoError){ | 
 
 
 
 
 | 117 |  | 
 
 
 
 
 | 118 | QFile file(GlobalVars::VagoTemporaryDir+"/"+this->ZipCacheFile); | 
 
 
 
 
 | 119 |  | 
 
 
 
 
 | 120 | // Create temp folder if it doesn't exist | 
 
 
 
 
 | 121 | if(!QDir(GlobalVars::VagoTemporaryDir).exists()){ | 
 
 
 
 
 | 122 | QDir().mkdir(GlobalVars::VagoTemporaryDir); | 
 
 
 
 
 | 123 | } | 
 
 
 
 
 | 124 |  | 
 
 
 
 
 | 125 | if(!file.open(QIODevice::WriteOnly)){ | 
 
 
 
 
 | 126 | UtilVago::showAndLogErrorPopUp(this->myLogger, "Error fetching package data: creating cache file."); | 
 
 
 
 
 | 127 | return; | 
 
 
 
 
 | 128 | } | 
 
 
 
 
 | 129 | file.write(result->readAll()); | 
 
 
 
 
 | 130 | file.close(); | 
 
 
 
 
 | 131 |  | 
 
 
 
 
 | 132 | //Let's extract the cache data | 
 
 
 
 
 | 133 | if(JlCompress::extractFile(GlobalVars::VagoTemporaryDir+"/"+this->ZipCacheFile, "/"+this->CacheFile ,GlobalVars::VagoTemporaryDir+"/"+this->CacheFile).isEmpty()){ | 
 
 
 
 
 | 134 | UtilVago::showAndLogErrorPopUp(this->myLogger, "An error occurred while unzipping the package data."); | 
 
 
 
 
 | 135 | } | 
 
 
 
 
 | 136 |  | 
 
 
 
 
 | 137 | checkForPackagesInCache(); | 
 
 
 
 
 | 138 |  | 
 
 
 
 
 | 139 | } | 
 
 
 
 
 | 140 | else{ | 
 
 
 
 
 | 141 | UtilVago::showAndLogErrorPopUpLogButton(this->myLogger, "An error occurred checking number availability:\n\n"+result->errorString()); | 
 
 
 
 
 | 142 | } | 
 
 
 
 
 | 143 |  | 
 
 
 
 
 | 144 | result->deleteLater(); | 
 
 
 
 
 | 145 | } | 
 
 
 
 
 | 146 |  | 
 
 
 
 
 | 147 | void PackagePage2::checkForPackagesInCache(){ | 
 
 
 
 
 | 148 | QString packageNumber=ui->lePackageNumber->text(); | 
 
 
 
 
 | 149 |  | 
 
 
 
 
 | 150 | QFile file(GlobalVars::VagoTemporaryDir+"/"+this->CacheFile); //let's read the chache unzipped | 
 
 
 
 
 | 151 | if(!file.open(QIODevice::ReadOnly)){ | 
 
 
 
 
 | 152 | UtilVago::showAndLogErrorPopUp(this->myLogger, "Error reading downloaded package cache data."); | 
 
 
 
 
 | 153 | return; | 
 
 
 
 
 | 154 | } | 
 
 
 
 
 | 155 | //Read file cache to ram | 
 
 
 
 
 | 156 | QString data=file.readAll(); | 
 
 
 
 
 | 157 |  | 
 
 
 
 
 | 158 |  | 
 
 
 
 
 | 159 | //Let's play with json engine | 
 
 
 
 
 | 160 | QScriptEngine engine; | 
 
 
 
 
 | 161 | QScriptValue sc = engine.evaluate("(" + data + ")"); | 
 
 
 
 
 | 162 |  | 
 
 
 
 
 | 163 | QScriptValue currNumber; | 
 
 
 
 
 | 164 | QString existingModName,existingModUrl; | 
 
 
 
 
 | 165 |  | 
 
 
 
 
 | 166 | QScriptValueIterator it(sc); | 
 
 
 
 
 | 167 |  | 
 
 
 
 
 | 168 | while (it.hasNext()) { | 
 
 
 
 
 | 169 | it.next(); | 
 
 
 
 
 | 170 | currNumber=it.value().toObject().property("field_package_number").toObject().property("und").toObject().property("0").toObject().property("value"); | 
 
 
 
 
 | 171 | if(currNumber.isValid() && currNumber.toString() == packageNumber){ | 
 
 
 
 
 | 172 | existingModName = it.value().toObject().property("title").toString(); | 
 
 
 
 
 | 173 | existingModUrl = it.value().toObject().property("path").toString(); | 
 
 
 
 
 | 174 | break; | 
 
 
 
 
 | 175 | } | 
 
 
 
 
 | 176 | } | 
 
 
 
 
 | 177 |  | 
 
 
 
 
 | 178 | if(!existingModName.isEmpty()){ | 
 
 
 
 
 | 179 | Util::showRichErrorPopUp("Package "+packageNumber+" is already being used by the following mod:<br/><br/>"+ | 
 
 
 
 
 | 180 | existingModName+"<br/><br/>"+ | 
 
 
 
 
 | 181 | "More information <a href='"+existingModUrl+"'>here</a>."); | 
 
 
 
 
 | 182 | } | 
 
 
 
 
 | 183 | else{ | 
 
 
 
 
 | 184 | Util::showPopUp("It seems that the package number " + packageNumber + " is not being used yet! :)"); | 
 
 
 
 
 | 185 | } | 
 
 
 
 
 | 186 | } | 
 
 
 
 
 | 187 |  | 
 
 
 
 
 | 188 | void PackagePage2::on_cbType_currentIndexChanged(int index) | 
 
 
 
 
 | 189 | { | 
 
 
 
 
 | 190 | ui->lePackageNumber->setText(QString().setNum(index+1)+"XXXX"); | 
 
 
 
 
 | 191 | } |