| [771] | 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 |  | 
|---|
| [1093] | 8 | PackagePage2::PackagePage2(QWidget *parent) : | 
|---|
| [771] | 9 | QWizardPage(parent), | 
|---|
|  | 10 | ui(new Ui::PackagePage2) | 
|---|
|  | 11 | { | 
|---|
|  | 12 | ui->setupUi(this); | 
|---|
| [1093] | 13 |  | 
|---|
| [771] | 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); | 
|---|
| [790] | 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); | 
|---|
| [771] | 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 |  | 
|---|
| [1093] | 40 | bool emptyContent=Util::Validation::checkEmptySpaces(QStringList()<<modName<<authors<<version<<description<<number); | 
|---|
| [771] | 41 |  | 
|---|
|  | 42 |  | 
|---|
|  | 43 | if(emptyContent){ | 
|---|
| [1093] | 44 | Util::Dialogs::showError("You need to fill all fields first!"); | 
|---|
| [771] | 45 | return false; | 
|---|
|  | 46 | } | 
|---|
|  | 47 |  | 
|---|
|  | 48 | if(number.size()!=5){ | 
|---|
| [1093] | 49 | Util::Dialogs::showError("Invalid number format. It should contain 5 numbers."); | 
|---|
| [771] | 50 | return false; | 
|---|
|  | 51 | } | 
|---|
|  | 52 |  | 
|---|
| [1093] | 53 | if(!Util::Validation::isStringInteger(number)){ | 
|---|
|  | 54 | Util::Dialogs::showError("Number is not numeric."); | 
|---|
| [771] | 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 |  | 
|---|
| [1093] | 70 | if(Util::Validation::checkEmptySpaces(QStringList(number))){ | 
|---|
|  | 71 | Util::Dialogs::showError("Number is empty. Please fill it first."); | 
|---|
| [771] | 72 | return; | 
|---|
|  | 73 | } | 
|---|
|  | 74 |  | 
|---|
|  | 75 | if(number.size()!=5){ | 
|---|
| [1093] | 76 | Util::Dialogs::showError("Invalid number format. It should contain 5 numeric characters."); | 
|---|
| [771] | 77 | return; | 
|---|
|  | 78 | } | 
|---|
|  | 79 |  | 
|---|
| [1093] | 80 | if(Util::Validation::isStringInteger(number)){ | 
|---|
| [771] | 81 |  | 
|---|
|  | 82 | bool necessaryToRedownload=false; | 
|---|
|  | 83 |  | 
|---|
| [1047] | 84 | QFile file(GlobalVars::VagoTemporaryDir+"/"+this->ZipCacheFile); | 
|---|
| [771] | 85 |  | 
|---|
| [1047] | 86 | if(!file.exists()){ | 
|---|
| [771] | 87 | necessaryToRedownload=true; //File doesn't exist yet, necessary to download | 
|---|
|  | 88 | } | 
|---|
| [1047] | 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) | 
|---|
| [771] | 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{ | 
|---|
| [1093] | 110 | Util::Dialogs::showError("Number is not numeric."); | 
|---|
| [771] | 111 | } | 
|---|
|  | 112 | } | 
|---|
|  | 113 |  | 
|---|
|  | 114 | void PackagePage2::downloadPackagesCache(QNetworkReply *result){ | 
|---|
|  | 115 |  | 
|---|
|  | 116 | if(result->error()==QNetworkReply::NoError){ | 
|---|
|  | 117 |  | 
|---|
| [1047] | 118 | QFile file(GlobalVars::VagoTemporaryDir+"/"+this->ZipCacheFile); | 
|---|
| [771] | 119 |  | 
|---|
|  | 120 | // Create temp folder if it doesn't exist | 
|---|
|  | 121 | if(!QDir(GlobalVars::VagoTemporaryDir).exists()){ | 
|---|
|  | 122 | QDir().mkdir(GlobalVars::VagoTemporaryDir); | 
|---|
|  | 123 | } | 
|---|
|  | 124 |  | 
|---|
| [1047] | 125 | if(!file.open(QIODevice::WriteOnly)){ | 
|---|
| [1093] | 126 | UtilVago::showAndLogErrorPopUp("Error fetching package data: creating cache file."); | 
|---|
| [771] | 127 | return; | 
|---|
|  | 128 | } | 
|---|
| [1047] | 129 | file.write(result->readAll()); | 
|---|
|  | 130 | file.close(); | 
|---|
| [771] | 131 |  | 
|---|
|  | 132 | //Let's extract the cache data | 
|---|
| [1047] | 133 | if(JlCompress::extractFile(GlobalVars::VagoTemporaryDir+"/"+this->ZipCacheFile, "/"+this->CacheFile ,GlobalVars::VagoTemporaryDir+"/"+this->CacheFile).isEmpty()){ | 
|---|
| [1093] | 134 | UtilVago::showAndLogErrorPopUp("An error occurred while unzipping the package data."); | 
|---|
| [1047] | 135 | } | 
|---|
| [771] | 136 |  | 
|---|
|  | 137 | checkForPackagesInCache(); | 
|---|
|  | 138 |  | 
|---|
|  | 139 | } | 
|---|
|  | 140 | else{ | 
|---|
| [1093] | 141 | UtilVago::showAndLogErrorPopUpLogButton("An error occurred checking number availability:\n\n"+result->errorString()); | 
|---|
| [771] | 142 | } | 
|---|
|  | 143 |  | 
|---|
|  | 144 | result->deleteLater(); | 
|---|
|  | 145 | } | 
|---|
|  | 146 |  | 
|---|
|  | 147 | void PackagePage2::checkForPackagesInCache(){ | 
|---|
|  | 148 | QString packageNumber=ui->lePackageNumber->text(); | 
|---|
|  | 149 |  | 
|---|
| [1047] | 150 | QFile file(GlobalVars::VagoTemporaryDir+"/"+this->CacheFile); //let's read the chache unzipped | 
|---|
|  | 151 | if(!file.open(QIODevice::ReadOnly)){ | 
|---|
| [1093] | 152 | UtilVago::showAndLogErrorPopUp("Error reading downloaded package cache data."); | 
|---|
| [771] | 153 | return; | 
|---|
|  | 154 | } | 
|---|
|  | 155 | //Read file cache to ram | 
|---|
| [1047] | 156 | QString data=file.readAll(); | 
|---|
| [771] | 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()){ | 
|---|
| [1093] | 179 | Util::Dialogs::showRichError("Package "+packageNumber+" is already being used by the following mod:<br/><br/>"+ | 
|---|
| [771] | 180 | existingModName+"<br/><br/>"+ | 
|---|
|  | 181 | "More information <a href='"+existingModUrl+"'>here</a>."); | 
|---|
|  | 182 | } | 
|---|
|  | 183 | else{ | 
|---|
| [1093] | 184 | Util::Dialogs::showInfo("It seems that the package number " + packageNumber + " is not being used yet! :)"); | 
|---|
| [771] | 185 | } | 
|---|
|  | 186 | } | 
|---|
|  | 187 |  | 
|---|
|  | 188 | void PackagePage2::on_cbType_currentIndexChanged(int index) | 
|---|
|  | 189 | { | 
|---|
| [1039] | 190 | ui->lePackageNumber->setText(QString().setNum(index+1)+"XXXX"); | 
|---|
| [771] | 191 | } | 
|---|