BuildmLearn Toolkit  2.0.4
BuildmLearn Toolkit is an easy-to-use program that helps users make mobile apps without any knowledge of application development.
 All Classes Functions Enumerations Groups Pages
flashcardcore.cpp
1 /*
2  Copyright (c) 2012, BuildmLearn Contributors listed at http://buildmlearn.org/people/
3  All rights reserved.
4 
5  Redistribution and use in source and binary forms, with or without
6  modification, are permitted provided that the following conditions are met:
7 
8  * Redistributions of source code must retain the above copyright notice, this
9  list of conditions and the following disclaimer.
10 
11  * Redistributions in binary form must reproduce the above copyright notice,
12  this list of conditions and the following disclaimer in the documentation
13  and/or other materials provided with the distribution.
14 
15  * Neither the name of the BuildmLearn nor the names of its
16  contributors may be used to endorse or promote products derived from
17  this software without specific prior written permission.
18 
19  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30 
31 #include "templates/flashcard/flashcardcore.h"
32 
33 #include "templates/flashcard/flashcardeditor.h"
34 #include "templates/flashcard/flashcardsimulator.h"
35 #include "miscellaneous/application.h"
36 #include "miscellaneous/iofactory.h"
37 #include "core/templatefactory.h"
38 #include "core/templategenerator.h"
39 #include "core/templateentrypoint.h"
40 
41 #include <QDir>
42 #include <QTextStream>
43 #include <QProcess>
44 
45 
46 FlashCardCore::FlashCardCore(TemplateEntryPoint* entry_point, QObject* parent)
47  :TemplateCore(entry_point, parent) {
48  m_editor = new FlashCardEditor(this);
49  m_simulator = new FlashCardSimulator(this);
50 }
51 
52 FlashCardCore::~FlashCardCore() {
53  qDebug("Destroying FlashCardCore instance.");
54 }
55 
56 TemplateCore::GenerationResult FlashCardCore::generateMobileApplication(const QString &input_apk_file, QString &output_file) {
57  emit generationProgress(5, tr("Preparing workspace..."));
58 
59  qApp->templateManager()->generator()->cleanWorkspace();
60 
61  emit generationProgress(10, tr("Extracting raw data from editor..."));
62 
63  // We need data which will be imported into apk/zip file.
64  QString quiz_data = editor()->generateBundleData();
65 
66  if (quiz_data.isEmpty()) {
67  // No date received, this is big problem.
68  return BundleProblem;
69  }
70 
71  QString temp_folder = qApp->templateManager()->tempDirectory();
72  QDir temp_directory(temp_folder);
73  QString base_folder = temp_folder + "/" + APP_LOW_NAME;
74  QDir base_directory(base_folder);
75 
76  // Preparation of target bundle file
77  emit generationProgress(20, tr("Creating base temporary folder..."));
78 
79  temp_directory.mkdir(APP_LOW_NAME);
80  base_directory.mkdir("assets");
81 
82  QFile index_file(base_folder + "/assets/flash_content.xml");
83  index_file.open(QIODevice::WriteOnly | QIODevice::Text);
84 
85  emit generationProgress(30, tr("Writting info data into file..."));
86 
87  QTextStream out(&index_file);
88  out << quiz_data;
89 
90  out.flush();
91  index_file.close();
92 
93  emit generationProgress(40, tr("Copying template apk file..."));
94 
95  // Copying of target apk file.
96  QString new_apk_name = input_apk_file;
97 
98  if (!QFile::copy(APP_TEMPLATES_PATH + "/" + entryPoint()->baseFolder() + "/" + entryPoint()->mobileApplicationApkFile(),
99  base_folder + "/" + new_apk_name)) {
100  qApp->templateManager()->generator()->cleanWorkspace();
101  return CopyProblem;
102  }
103 
104  emit generationProgress(60, tr("Inserting data into apk file..."));
105 
106  // Inserting bundle file into apk file.
107  QProcess zip;
108 
109  zip.setWorkingDirectory(base_folder);
110  zip.start(qApp->zipUtilityPath(), QStringList() << "-m" << "-r" << new_apk_name << "assets");
111  zip.waitForFinished();
112 
113  if (zip.exitCode() != EXIT_STATUS_ZIP_NORMAL) {
114  // Error during inserting quiz data via zip.
115  qApp->templateManager()->generator()->cleanWorkspace();
116  return ZipProblem;
117  }
118 
119  emit generationProgress(70, tr("Signing apk file..."));
120 
121  // Signing and renaming target file.
122  QString pem_certificate = QDir::toNativeSeparators(APP_CERT_PATH + "/" + CERTIFICATE_PATH);
123  QString pk_certificate = QDir::toNativeSeparators(APP_CERT_PATH + "/" + KEY_PATH);
124  QProcess signapk;
125 
126  signapk.setWorkingDirectory(base_folder);
127  signapk.start(qApp->javaInterpreterPath(), QStringList() << "-jar" << qApp->signApkUtlityPath() <<
128  pem_certificate << pk_certificate << new_apk_name <<
129  QDir::toNativeSeparators(new_apk_name + ".new"));
130  signapk.waitForFinished();
131 
132  if (signapk.exitCode() != EXIT_STATUS_SIGNAPK_WORKING) {
133  qApp->templateManager()->generator()->cleanWorkspace();
134  return SignApkProblem;
135  }
136 
137  emit generationProgress(90, tr("Copying final apk file to output directory..."));
138 
139  // Now, our file is created. We need to move it to target directory.
140  if (!IOFactory::copyFile(base_folder + "/" + new_apk_name + ".new",
141  qApp->templateManager()->outputDirectory() + "/" + new_apk_name)) {
142  qApp->templateManager()->generator()->cleanWorkspace();
143  return CopyProblem;
144  }
145 
146  output_file = QDir(qApp->templateManager()->outputDirectory()).filePath(new_apk_name);
147 
148  // Removing temporary files and exit.
149  qApp->templateManager()->generator()->cleanWorkspace();
150  return Success;
151 }
152 
153 FlashCardEditor *FlashCardCore::flashCardEditor() {
154  return static_cast<FlashCardEditor*>(m_editor);
155 }
156 
157 FlashCardSimulator *FlashCardCore::flashCardSimulator() {
158  return static_cast<FlashCardSimulator*>(m_simulator);
159 }
160 
virtual TemplateEditor * editor() const
Access to editor widget of the template.
Definition: templatecore.h:84
static bool copyFile(const QString &source, const QString &destination)
Copies source file into destination path.
Definition: iofactory.cpp:69
GenerationResult generateMobileApplication(const QString &input_apk_file, QString &output_file)
Generates APK file from current project with active settings.
void generationProgress(int percent_completed, const QString &progress_info)
Emitted when there is something new concerning generating of mobile APK application.
virtual TemplateEntryPoint * entryPoint() const
Access to entry point of the template.
Definition: templatecore.h:78
The entry point for a template.
The core class container for single template.
Definition: templatecore.h:43
GenerationResult
Possible results of generation of bundle data.
Definition: templatecore.h:48
virtual QString generateBundleData()=0
Generates RAW data which represent data of this template.