31 #include "core/templatefactory.h"
33 #include "definitions/definitions.h"
34 #include "core/templatecore.h"
35 #include "core/templateeditor.h"
36 #include "core/templateentrypoint.h"
37 #include "core/templategenerator.h"
38 #include "miscellaneous/settings.h"
39 #include "miscellaneous/application.h"
40 #include "templates/quiz/quizentrypoint.h"
41 #include "templates/flashcard/flashcardentrypoint.h"
42 #include "templates/mlearning/basicmlearningentrypoint.h"
43 #include "templates/learnspellings/learnspellingsentrypoint.h"
45 #if QT_VERSION >= 0x050000
46 #include <QStandardPaths>
48 #include <QDesktopServices>
53 #include <QTextStream>
56 TemplateFactory::TemplateFactory(QObject *parent)
58 m_activeEntryPoint(NULL), m_activeCore(NULL),
63 TemplateFactory::~TemplateFactory() {
64 qDebug(
"Destroying TemplateFactory instance.");
68 QList<TemplateEntryPoint*> entries;
71 for (
int i = 0; i <= entries.size(); i++) {
72 if (i == entries.size()) {
73 entries.append(entry);
76 else if (entry->
humanName() < entries.at(i)->humanName()) {
77 entries.insert(i, entry);
87 #if QT_VERSION >= 0x050000
88 return qApp->settings()->value(APP_CFG_TEMPLATES,
"temp_directory",
89 QStandardPaths::writableLocation(QStandardPaths::TempLocation)).toString();
91 return qApp->settings()->value(APP_CFG_TEMPLATES,
"temp_directory",
92 QDesktopServices::storageLocation(QDesktopServices::TempLocation)).toString();
96 void TemplateFactory::setTempDirectory(
const QString &temp_directory) {
97 qApp->settings()->setValue(APP_CFG_TEMPLATES,
"temp_directory", temp_directory);
101 #if QT_VERSION >= 0x050000
102 return qApp->settings()->value(APP_CFG_TEMPLATES,
"output_directory",
103 QStandardPaths::writableLocation(QStandardPaths::DesktopLocation)).toString();
105 return qApp->settings()->value(APP_CFG_TEMPLATES,
"output_directory",
106 QDesktopServices::storageLocation(QDesktopServices::DesktopLocation)).toString();
110 void TemplateFactory::setOutputDirectory(
const QString &output_directory) {
111 qApp->settings()->setValue(APP_CFG_TEMPLATES,
"output_directory", output_directory);
115 return qApp->settings()->value(APP_CFG_TEMPLATES,
"application_file_name_pattern",
116 "application-%1-%2-%3.apk").toString();
119 void TemplateFactory::setApplicationFileNamePattern(
const QString &file_name_pattern) {
120 qApp->settings()->setValue(APP_CFG_TEMPLATES,
"application_file_name_pattern", file_name_pattern);
127 QDateTime::currentDateTime().toString(
"yyyyMMddThhmmss"));
134 QDomDocument
TemplateFactory::generateBundleHeader(
const QString &template_type,
const QString &author_name,
const QString &author_email,
const QString &project_title,
const QString &project_description,
const QString &template_version) {
135 QDomDocument xml_bundle;
136 QDomElement root_element = xml_bundle.createElement(
"buildmlearn_application");
137 QDomElement author_element = xml_bundle.createElement(
"author");
138 QDomElement name_element = xml_bundle.createElement(
"name");
139 QDomElement email_element = xml_bundle.createElement(
"email");
140 QDomElement title_element = xml_bundle.createElement(
"title");
141 QDomElement description_element = xml_bundle.createElement(
"description");
142 QDomElement version_element = xml_bundle.createElement(
"version");
143 QDomElement data_element = xml_bundle.createElement(
"data");
146 root_element.setAttribute(
"type", template_type);
147 name_element.appendChild(xml_bundle.createTextNode(author_name));
148 email_element.appendChild(xml_bundle.createTextNode(author_email));
149 title_element.appendChild(xml_bundle.createTextNode(project_title));
150 description_element.appendChild(xml_bundle.createTextNode(project_description));
151 version_element.appendChild(xml_bundle.createTextNode(template_version));
153 author_element.appendChild(name_element);
154 author_element.appendChild(email_element);
156 root_element.appendChild(author_element);
157 root_element.appendChild(title_element);
158 root_element.appendChild(description_element);
159 root_element.appendChild(version_element);
160 root_element.appendChild(data_element);
162 xml_bundle.appendChild(root_element);
174 void TemplateFactory::clearEntryAndCore() {
175 if (m_activeEntryPoint != NULL) {
176 m_activeEntryPoint = NULL;
179 if (m_activeCore != NULL) {
180 m_activeCore->deleteLater();
189 m_activeEntryPoint = entry_point;
202 QFile bundle_file(bundle_file_name);
204 if (!bundle_file.open(QIODevice::Text | QIODevice::ReadOnly | QIODevice::Unbuffered)) {
205 qApp->trayIcon()->showMessage(tr(
"Cannot load XML bundle"),
206 tr(
"Bundle cannot be loaded because XML file cannot be opened for reading."),
207 QSystemTrayIcon::Critical);
214 QString bundle_data(bundle_file.readAll());
219 if (target_entry_point == NULL) {
220 qApp->trayIcon()->showMessage(tr(
"Cannot load XML bundle"),
221 tr(
"Bundle cannot be loaded because XML file is corrupted."),
222 QSystemTrayIcon::Critical);
229 if (loaded_core == NULL) {
230 qApp->trayIcon()->showMessage(tr(
"Cannot load XML bundle"),
231 tr(
"Target template was not able to load XML bundle data."),
232 QSystemTrayIcon::Critical);
242 m_activeEntryPoint = target_entry_point;
243 m_activeCore = loaded_core;
251 if (bundle_data.isEmpty()) {
255 QDomDocument xml_document;
256 xml_document.setContent(bundle_data);
258 return m_availableTemplates.value(xml_document.documentElement().attribute(
"type"), NULL);
266 if (xml_bundle_contents.isEmpty()) {
268 qWarning(
"There is nothing to save for template \"%s\".", qPrintable(
activeCore()->entryPoint()->humanName()));
272 QFile target_xml_file(bundle_file_name);
274 if (target_xml_file.open(QIODevice::Truncate | QIODevice::WriteOnly |
275 QIODevice::Unbuffered | QIODevice::Text)) {
276 QTextStream stream(&target_xml_file);
278 stream << xml_bundle_contents;
281 target_xml_file.flush();
282 target_xml_file.close();
303 void TemplateFactory::setupTemplates() {
306 m_availableTemplates.insert(flashcard_entry->
typeIndentifier(), flashcard_entry);
309 m_availableTemplates.insert(info_entry->
typeIndentifier(), info_entry);
312 m_availableTemplates.insert(quiz_entry->
typeIndentifier(), quiz_entry);
315 m_availableTemplates.insert(learnspellings_entry->
typeIndentifier(), learnspellings_entry);
QString typeIndentifier() const
Identifier of the template.
TemplateEntryPoint * entryPointForBundle(const QString &bundle_data)
Decides which entry point raw XML bundle data belong to.
void newTemplateCoreCreated(TemplateCore *core)
Emitted if new project using some template core is started.
QString applicationFileName(const QString &project_name)
Generates file name for output APK file.
static bool entryPointIsLessThan(TemplateEntryPoint *s1, TemplateEntryPoint &s2)
Performs lexicographical comparison of two entry points.
TemplateGenerator * generator() const
Access to component which supervises generating of APK files.
virtual TemplateEditor * editor() const
Access to editor widget of the template.
bool saveCurrentProject()
Saves current project to assigned file if there is any.
QString outputDirectory() const
Access to directory used throughout APK generation process.
QDomDocument generateBundleHeader(const QString &template_type, const QString &author_name, const QString &author_email, const QString &project_title, const QString &project_description, const QString &template_version)
Generates common XML bundle.
QString applicationFileNamePattern() const
Access to pattern used for name of output APK file.
Entry point of Learn Spellings template.
virtual TemplateCore * createNewCore()=0
Creates new instance of template core.
TemplateCore * activeCore() const
Access to active core.
bool startNewProject(TemplateEntryPoint *entry_point)
Starts new project core from given entry point.
void setAssignedFile(const QString &assigned_file)
Sets new assigned file.
virtual TemplateCore * loadCoreFromBundleData(const QString &raw_data)=0
Creates new instance and fills it template-specific data ("load project" functionality).
bool loadProject(const QString &bundle_file_name)
Loads stored project and initializes new core according to it.
TemplateEntryPoint * activeEntryPoint() const
Access to active entry point.
virtual QString humanName() const
Human-readable name of template.
bool saveCurrentProjectAs(const QString &bundle_file_name)
Saves current project to given file.
QList< TemplateEntryPoint * > availableTemplates()
Access to available templates.
Generator responsible for generating APK mobile applications.
void quit()
Quits running actions of template manager.
The entry point for a template.
void setIsDirty(bool is_dirty)
Sets new dirtiness status.
The core class container for single template.
virtual QString generateBundleData()=0
Generates RAW data which represent data of this template.
QString tempDirectory() const
Access to temporary directory used throughout APK generation process.