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 = new QFile(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 |
delete file; |
93 |
|
94 |
if(necessaryToRedownload){ |
95 |
//let's start the search in the web, so we make sure it doesn't exists yet |
96 |
QNetworkAccessManager *manager = new QNetworkAccessManager(this); |
97 |
connect(manager, SIGNAL(finished(QNetworkReply*)), |
98 |
this, SLOT(downloadPackagesCache(QNetworkReply*))); |
99 |
|
100 |
//This timestamp is to guarantee that the cache received is fresh even through proxys |
101 |
QDateTime currTime = QDateTime::currentDateTime(); |
102 |
QString t_time = QString::number(currTime.toTime_t()); |
103 |
|
104 |
manager->get(QNetworkRequest(QUrl(this->PackagesCacheUrl+"?ts="+t_time))); |
105 |
} |
106 |
else{ //Not needed to download! :) Let's use our local cache. |
107 |
checkForPackagesInCache(); |
108 |
} |
109 |
} |
110 |
else{ |
111 |
Util::showErrorPopUp("Number is not numeric."); |
112 |
} |
113 |
} |
114 |
|
115 |
void PackagePage2::downloadPackagesCache(QNetworkReply *result){ |
116 |
|
117 |
if(result->error()==QNetworkReply::NoError){ |
118 |
|
119 |
QFile *file = new QFile(GlobalVars::VagoTemporaryDir+"/"+this->ZipCacheFile); |
120 |
|
121 |
// Create temp folder if it doesn't exist |
122 |
if(!QDir(GlobalVars::VagoTemporaryDir).exists()){ |
123 |
QDir().mkdir(GlobalVars::VagoTemporaryDir); |
124 |
} |
125 |
|
126 |
if(!file->open(QIODevice::WriteOnly)){ |
127 |
const QString error="Error fetching package data: creating cache file."; |
128 |
this->myLogger->writeString(error); |
129 |
Util::showErrorPopUp(error); |
130 |
return; |
131 |
} |
132 |
file->write(result->readAll()); |
133 |
file->close(); |
134 |
|
135 |
//Let's extract the cache data |
136 |
UnZip uz; |
137 |
UnZip::ErrorCode ec = uz.openArchive(GlobalVars::VagoTemporaryDir+"/"+this->ZipCacheFile); |
138 |
checkForUnzipError(ec); |
139 |
|
140 |
//Extract the cache files |
141 |
ec = uz.extractAll(GlobalVars::VagoTemporaryDir); |
142 |
checkForUnzipError(ec); |
143 |
|
144 |
//Close zip archive |
145 |
uz.closeArchive(); |
146 |
|
147 |
delete file; |
148 |
|
149 |
checkForPackagesInCache(); |
150 |
|
151 |
} |
152 |
else{ |
153 |
const QString error="An error occurred checking number availability:\n\n"+result->errorString(); |
154 |
this->myLogger->writeString(error); |
155 |
Util::showErrorLogPopUp(error); |
156 |
} |
157 |
|
158 |
result->deleteLater(); |
159 |
} |
160 |
|
161 |
void PackagePage2::checkForPackagesInCache(){ |
162 |
QString packageNumber=ui->lePackageNumber->text(); |
163 |
|
164 |
QFile *file = new QFile(GlobalVars::VagoTemporaryDir+"/"+this->CacheFile); //let's read the chache unzipped |
165 |
if(!file->open(QIODevice::ReadOnly)){ |
166 |
const QString error="Error reading downloaded package cache data."; |
167 |
this->myLogger->writeString(error); |
168 |
Util::showErrorPopUp(error); |
169 |
return; |
170 |
} |
171 |
//Read file cache to ram |
172 |
QString data=file->readAll(); |
173 |
|
174 |
|
175 |
//Let's play with json engine |
176 |
QScriptEngine engine; |
177 |
QScriptValue sc = engine.evaluate("(" + data + ")"); |
178 |
|
179 |
QScriptValue currNumber; |
180 |
QString existingModName,existingModUrl; |
181 |
|
182 |
QScriptValueIterator it(sc); |
183 |
|
184 |
while (it.hasNext()) { |
185 |
it.next(); |
186 |
currNumber=it.value().toObject().property("field_package_number").toObject().property("und").toObject().property("0").toObject().property("value"); |
187 |
if(currNumber.isValid() && currNumber.toString() == packageNumber){ |
188 |
existingModName = it.value().toObject().property("title").toString(); |
189 |
existingModUrl = it.value().toObject().property("path").toString(); |
190 |
break; |
191 |
} |
192 |
} |
193 |
|
194 |
if(!existingModName.isEmpty()){ |
195 |
Util::showRichErrorPopUp("Package "+packageNumber+" is already being used by the following mod:<br/><br/>"+ |
196 |
existingModName+"<br/><br/>"+ |
197 |
"More information <a href='"+existingModUrl+"'>here</a>."); |
198 |
} |
199 |
else{ |
200 |
Util::showPopUp("It seems that the package number " + packageNumber + " is not being used yet! :)"); |
201 |
} |
202 |
|
203 |
delete file; |
204 |
} |
205 |
|
206 |
void PackagePage2::on_cbType_currentIndexChanged(int index) |
207 |
{ |
208 |
ui->lePackageNumber->setText(QString().setNum(index+1)+"XXXX"); |
209 |
} |
210 |
|
211 |
void PackagePage2::checkForUnzipError(UnZip::ErrorCode ec){ |
212 |
if (ec != UnZip::Ok){ |
213 |
const QString error="Error found while unzipping the package data. Error number = "+QString::number(ec); |
214 |
Util::showErrorPopUp(error); |
215 |
this->myLogger->writeString(error); |
216 |
} |
217 |
} |