source: Vago/trunk/Vago/packageWizard/packagewizard.cpp@ 771

Last change on this file since 771 was 771, checked in by s10k, 12 years ago
File size: 7.5 KB
RevLine 
[771]1#include "packagewizard.h"
2
3PackageWizard::PackageWizard(QString workspaceWizardLocation, QSettings *vagoSettings, Logger *myLogger)
4{
5 this->workspaceWizardLocation=workspaceWizardLocation;
6 this->vagoSettings=vagoSettings;
7 this->myLogger=myLogger;
8 this->packagesLocation=this->workspaceWizardLocation+"/Packages";
9}
10
11int PackageWizard::exec(){
12 QWizard myWizard;
13
14 myWizard.setWindowIcon(QIcon(":/new/icons/package.png"));
15
16 //Center and resize QWizard (http://www.thedazzlersinc.com/source/2012/06/04/qt-center-window-in-screen/)
17 myWizard.resize(640,480);
18 QRect position = myWizard.frameGeometry();
19 position.moveCenter(QDesktopWidget().availableGeometry().center());
20 myWizard.move(position.topLeft());
21 //
22
23 PackagePage2 *page2 = new PackagePage2(this->myLogger);
24 PackagePage3 *page3 = new PackagePage3();
25 PackagePage4 *page4 = new PackagePage4();
26 PackagePageFinal *pageFinal = new PackagePageFinal(this->vagoSettings);
27
28 myWizard.addPage(createIntroPage());
29 myWizard.addPage(page2);
30 myWizard.addPage(page3);
31 myWizard.addPage(page4);
32 myWizard.addPage(pageFinal);
33
34 myWizard.setWindowTitle("AIE2 Package Creator");
35
36 //If wizard finished with sucess
37 if(myWizard.exec()){ //modal and wait for finalization
38 createPackage(myWizard, page4);
39 }
40
41 return 0;
42}
43
44QWizardPage* PackageWizard::createIntroPage() {
45 QWizardPage *page = new QWizardPage;
46 page->setTitle("Introduction");
47
48 QLabel *label = new QLabel("Welcome to the Anniversary Edition Installer 2 (AIE2) package"
49 " creator wizard.\n"
50 "This wizard will allow you to create in a few and simple steps a package for AIE2.");
51 label->setWordWrap(true);
52
53 QVBoxLayout *layout = new QVBoxLayout;
54 layout->addWidget(label);
55 page->setLayout(layout);
56
57 return page;
58}
59
60void PackageWizard::createPackage(const QWizard &myWizard, PackagePage4 *page4){
61 const QString aeVersion="2.0";
62
63 //Get info page 2
64 QString modName=myWizard.field("leModName").toString();
65 QString authors=myWizard.field("leAuthors").toString();
66 QString version=myWizard.field("leVersion").toString();
67 QString description=myWizard.field("ptDescription").toString();
68 QString packageNumber=myWizard.field("lePackageNumber").toString();
69 bool bslReplace=myWizard.field("rbReplace").toBool();
70
71 //Get info page 3
72 QString dependentPackages=myWizard.field("leDependentPackages").toString();
73 QString incompatiblePackages=myWizard.field("leIncompatiblePackages").toString();
74 QString unlockLevels=myWizard.field("leUnlockLevels").toString();
75
76 //Get info page 4
77 const DropTableWidget *commonTable=page4->commonTable;
78 const DropTableWidget *windowsTable=page4->windowsTable;
79 const DropTableWidget *macTable=page4->macTable;
80
81 //Get info from final page
82 bool openFolder=myWizard.field("cbOpenFolder").toBool();
83 bool createZip=myWizard.field("cbCreateZip").toBool();
84 //Remember the final page choices to next time
85 this->vagoSettings->setValue("PackageCreator/OpenFolder",openFolder);
86 this->vagoSettings->setValue("PackageCreator/CreateZip",createZip);
87
88 const QString packageName=packageNumber+Util::fullTrim(modName);
89
90 // Start package creation...
91
92 // Create Packages folder if it doesn't exist
93 if(!QDir(this->packagesLocation).exists()){
94 QDir().mkpath(this->packagesLocation);
95 }
96
97 QString modDir=this->packagesLocation+"/"+packageName+"/";
98
99 QDir().mkdir(modDir);
100
101 bool bslExist=false;
102
103 if(commonTable->rowCount()>0){
104 copyPackageFolders(commonTable,"common",modDir,bslExist);
105 }
106
107 if(windowsTable->rowCount()>0){
108 copyPackageFolders(windowsTable,"win_only",modDir,bslExist);
109 }
110
111 if(macTable->rowCount()>0){
112 copyPackageFolders(macTable,"mac_only",modDir,bslExist);
113 }
114
115 QFile *modInfo = new QFile(modDir+"Mod_Info.cfg");
116
117 if (!modInfo->open(QIODevice::WriteOnly | QIODevice::Text)){ //open to write
118 Util::showErrorPopUp("Couldn't' create Mod_Info.cfg file.");
119 myLogger->writeString("Couldn't' create Mod_Info.cfg file when creating AE Package.");
120 return;
121 }
122
123 QTextStream *modWriteStream = new QTextStream (modInfo);
124 *modWriteStream << "AEInstallVersion -> "+aeVersion+"\n";
125 *modWriteStream << "NameOfMod -> "+modName+"\n";
126 *modWriteStream << "ModVersion -> "+version+"\n";
127 *modWriteStream << "Creator -> "+authors+"\n";
128 *modWriteStream << "Readme -> "+description.replace("\n"," \\n ")+"\n";
129 if(!incompatiblePackages.isEmpty()){
130 *modWriteStream << "IncompatibleWith -> "+incompatiblePackages+"\n";
131 }
132 if(!dependentPackages.isEmpty()){
133 *modWriteStream << "DependsOn -> "+dependentPackages+"\n";
134 }
135
136 if(bslExist){
137 if(bslReplace){
138 *modWriteStream << "HasBsl -> Yes\n";
139 }
140 else{
141 *modWriteStream << "HasBsl -> Addon\n";
142 }
143 }
144
145 if(!unlockLevels.isEmpty()){
146 *modWriteStream << "UnlockLevel -> "+unlockLevels+"\n";
147 }
148
149 *modWriteStream << "Vago -> "+GlobalVars::AppVersion;
150
151 delete modWriteStream; //it auto closes the files/streams
152 delete modInfo;
153
154 //Create zipped package using PKZIP 2.0 (http://osdab.42cows.org/snippets/zip.php?mode=advanced)
155 if(createZip){
156 Zip uz;
157
158 Zip::ErrorCode ec = uz.createArchive(this->packagesLocation+"/"+packageName+".zip");
159 checkForZipError(ec);
160
161 ec=uz.addDirectory(modDir);
162 checkForZipError(ec);
163
164 ec = uz.closeArchive();
165 checkForZipError(ec);
166 }
167
168 if(openFolder){
169 QDesktopServices::openUrl(QUrl("file:///"+this->packagesLocation));
170 }
171}
172
173void PackageWizard::copyPackageFolders(const DropTableWidget *myTable, QString tableDir, QString modDir, bool &bslExist){
174
175 QString sourceFolder;
176 bool onisExist=false;
177 QString path;
178
179 for(int i=0; i<myTable->rowCount(); i++){
180
181 sourceFolder=myTable->item(i,2)->text();
182
183 if(myTable->item(i,1)->text()==".oni"){
184 path=modDir+"oni/"+tableDir;
185 if(!onisExist){
186 onisExist=true;
187 }
188 }
189 else if(myTable->item(i,1)->text()==".bsl"){
190 path=modDir+"bsl/"+tableDir;
191 if(!bslExist){
192 bslExist=true;
193 }
194 }
195 else{
196 path=modDir+"patches/"+tableDir;
197 if(!bslExist){
198 bslExist=true;
199 }
200 }
201 QDir().mkpath(path); //create path if doesn't exist
202 if(!Util::cpDir(sourceFolder,path+Util::cutName(sourceFolder))){//copy contents (creates dest destination automatically if not exists yet)
203 QString errorString="An error occurred while copping the folder/files to the package folder: \n"
204 "Copying from "+sourceFolder+"\n to "+path+Util::cutName(sourceFolder);
205 Util::showErrorLogPopUp(errorString);
206 this->myLogger->writeString(errorString);
207 }
208 }
209}
210
211/**
212 Convenience function for checking for zipping errors
213 */
214void PackageWizard::checkForZipError(Zip::ErrorCode ec){
215 if (ec != Zip::Ok){
216 const QString error="Error found while zipping the package. Error number = "+QString::number(ec);
217 Util::showErrorPopUp(error);
218 this->myLogger->writeString(error);
219 }
220}
Note: See TracBrowser for help on using the repository browser.