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
basicmlearningcore.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/mlearning/basicmlearningcore.h"
32 
33 #include "templates/mlearning/basicmlearningeditor.h"
34 #include "templates/mlearning/basicmlearningsimulator.h"
35 #include "miscellaneous/application.h"
36 #include "miscellaneous/iofactory.h"
37 #include "core/templatefactory.h"
38 #include "core/templateentrypoint.h"
39 #include "core/templategenerator.h"
40 
41 #include <QDir>
42 #include <QTextStream>
43 #include <QProcess>
44 
45 
46 BasicmLearningCore::BasicmLearningCore(TemplateEntryPoint *entry_point,
47  QObject *parent)
48  : TemplateCore(entry_point, parent) {
49  m_editor = new BasicmLearningEditor(this);
50  m_simulator = new BasicmLearningSimulator(this);
51 }
52 
53 BasicmLearningCore::~BasicmLearningCore() {
54 }
55 
56 TemplateCore::GenerationResult BasicmLearningCore::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/info_content.xml");
83  index_file.open(QIODevice::WriteOnly | QIODevice::Text);
84 
85  emit generationProgress(30, tr("Writting item 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  if (!QFile::copy(APP_TEMPLATES_PATH + "/" + entryPoint()->baseFolder() + "/" + entryPoint()->mobileApplicationApkFile(),
98  base_folder + "/" + new_apk_name)) {
99  qApp->templateManager()->generator()->cleanWorkspace();
100  return CopyProblem;
101  }
102 
103  emit generationProgress(60, tr("Inserting data into apk file..."));
104 
105  // Inserting bundle file into apk file.
106  QProcess zip;
107 
108  zip.setWorkingDirectory(base_folder);
109  zip.start(qApp->zipUtilityPath(), QStringList() << "-m" << "-r" << new_apk_name << "assets");
110  zip.waitForFinished();
111 
112  if (zip.exitCode() != EXIT_STATUS_ZIP_NORMAL) {
113  // Error during inserting quiz data via zip.
114  qApp->templateManager()->generator()->cleanWorkspace();
115  return ZipProblem;
116  }
117 
118  emit generationProgress(70, tr("Signing apk file..."));
119 
120  // Signing and renaming target file.
121  QString pem_certificate = QDir::toNativeSeparators(APP_CERT_PATH + "/" + CERTIFICATE_PATH);
122  QString pk_certificate = QDir::toNativeSeparators(APP_CERT_PATH + "/" + KEY_PATH);
123  QProcess signapk;
124 
125  signapk.setWorkingDirectory(base_folder);
126  signapk.start(qApp->javaInterpreterPath(), QStringList() << "-jar" << qApp->signApkUtlityPath() <<
127  pem_certificate << pk_certificate << new_apk_name <<
128  QDir::toNativeSeparators(new_apk_name + ".new"));
129  signapk.waitForFinished();
130 
131  if (signapk.exitCode() != EXIT_STATUS_SIGNAPK_WORKING) {
132  qApp->templateManager()->generator()->cleanWorkspace();
133  return SignApkProblem;
134  }
135 
136  emit generationProgress(90, tr("Copying final apk file to output directory..."));
137 
138  // Now, our file is created. We need to move it to target directory.
139  if (!IOFactory::copyFile(base_folder + "/" + new_apk_name + ".new",
140  qApp->templateManager()->outputDirectory() + "/" + new_apk_name)) {
141  qApp->templateManager()->generator()->cleanWorkspace();
142  return CopyProblem;
143  }
144 
145  output_file = QDir(qApp->templateManager()->outputDirectory()).filePath(new_apk_name);
146 
147  // Removing temporary files and exit.
148  qApp->templateManager()->generator()->cleanWorkspace();
149  return Success;
150 }
151 
152 BasicmLearningEditor *BasicmLearningCore::learningEditor() {
153  return static_cast<BasicmLearningEditor*>(editor());
154 }
155 
156 BasicmLearningSimulator *BasicmLearningCore::learningSimulator() {
157  return static_cast<BasicmLearningSimulator*>(simulator());
158 }
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
virtual TemplateSimulator * simulator() const
Access to simulator widget of the template.
Definition: templatecore.h:90
void generationProgress(int percent_completed, const QString &progress_info)
Emitted when there is something new concerning generating of mobile APK application.
GenerationResult generateMobileApplication(const QString &input_apk_file, QString &output_file)
Generates APK file from current project with active settings.
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.