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
TemplateFactory Class Reference

The top-level manager of templates. More...

#include <templatefactory.h>

Collaboration diagram for TemplateFactory:
Collaboration graph

Public Slots

void quit ()
 Quits running actions of template manager.
 
bool startNewProject (TemplateEntryPoint *entry_point)
 Starts new project core from given entry point. More...
 
bool loadProject (const QString &bundle_file_name)
 Loads stored project and initializes new core according to it. More...
 
bool saveCurrentProjectAs (const QString &bundle_file_name)
 Saves current project to given file. More...
 
bool saveCurrentProject ()
 Saves current project to assigned file if there is any. More...
 

Signals

void newTemplateCoreCreated (TemplateCore *core)
 Emitted if new project using some template core is started. More...
 

Public Member Functions

 TemplateFactory (QObject *parent=0)
 
QList< TemplateEntryPoint * > availableTemplates ()
 Access to available templates. More...
 
QString tempDirectory () const
 Access to temporary directory used throughout APK generation process. More...
 
void setTempDirectory (const QString &temp_directory)
 
QString outputDirectory () const
 Access to directory used throughout APK generation process. More...
 
void setOutputDirectory (const QString &output_directory)
 
QString applicationFileNamePattern () const
 Access to pattern used for name of output APK file. More...
 
void setApplicationFileNamePattern (const QString &file_name_pattern)
 
QString applicationFileName (const QString &project_name)
 Generates file name for output APK file. More...
 
TemplateEntryPointactiveEntryPoint () const
 Access to active entry point. More...
 
TemplateCoreactiveCore () const
 Access to active core. More...
 
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. More...
 
TemplateGeneratorgenerator () const
 Access to component which supervises generating of APK files. More...
 
TemplateEntryPointentryPointForBundle (const QString &bundle_data)
 Decides which entry point raw XML bundle data belong to. More...
 

Static Public Member Functions

static bool entryPointIsLessThan (TemplateEntryPoint *s1, TemplateEntryPoint &s2)
 Performs lexicographical comparison of two entry points. More...
 

Detailed Description

The top-level manager of templates.

This class is used in a singleton-like fashion from main Application object via Application::templateManager(). It contains all available template entry points.

See also
TemplateEntryPoint, TemplateCore, Application

Definition at line 69 of file templatefactory.h.

Member Function Documentation

TemplateCore* TemplateFactory::activeCore ( ) const
inline

Access to active core.

Returns
Returns pointer to active core or NULL if not core is active.

Definition at line 115 of file templatefactory.h.

115  {
116  return m_activeCore;
117  }

Here is the caller graph for this function:

TemplateEntryPoint* TemplateFactory::activeEntryPoint ( ) const
inline

Access to active entry point.

Returns
Returns pointer to active entry point or NULL if no entry point is active.

Definition at line 109 of file templatefactory.h.

109  {
110  return m_activeEntryPoint;
111  }

Here is the caller graph for this function:

QString TemplateFactory::applicationFileName ( const QString &  project_name)

Generates file name for output APK file.

Parameters
project_nameName of source project.
Returns
Access to output APK application file name pattern.

Definition at line 123 of file templatefactory.cpp.

123  {
124  if (activeEntryPoint() != NULL) {
125  return applicationFileNamePattern().arg(activeEntryPoint()->name(),
126  project_name,
127  QDateTime::currentDateTime().toString("yyyyMMddThhmmss"));
128  }
129  else {
130  return QString();
131  }
132 }
QString applicationFileNamePattern() const
Access to pattern used for name of output APK file.
TemplateEntryPoint * activeEntryPoint() const
Access to active entry point.

Here is the call graph for this function:

QString TemplateFactory::applicationFileNamePattern ( ) const

Access to pattern used for name of output APK file.

Returns
Returns pattern used for name of output APK file.

Definition at line 114 of file templatefactory.cpp.

114  {
115  return qApp->settings()->value(APP_CFG_TEMPLATES, "application_file_name_pattern",
116  "application-%1-%2-%3.apk").toString();
117 }

Here is the caller graph for this function:

QList< TemplateEntryPoint * > TemplateFactory::availableTemplates ( )

Access to available templates.

See also
TemplateEntryPoint
Returns
Method returns list of available templates. Templates are not sorted in any particular order.

Definition at line 67 of file templatefactory.cpp.

67  {
68  QList<TemplateEntryPoint*> entries;
69 
70  foreach (TemplateEntryPoint *entry, m_availableTemplates.values()) {
71  for (int i = 0; i <= entries.size(); i++) {
72  if (i == entries.size()) {
73  entries.append(entry);
74  break;
75  }
76  else if (entry->humanName() < entries.at(i)->humanName()) {
77  entries.insert(i, entry);
78  break;
79  }
80  }
81  }
82 
83  return entries;
84 }
virtual QString humanName() const
Human-readable name of template.
The entry point for a template.

Here is the call graph for this function:

TemplateEntryPoint * TemplateFactory::entryPointForBundle ( const QString &  bundle_data)

Decides which entry point raw XML bundle data belong to.

Parameters
bundle_dataRaw XML bundle data.
Returns
Returns pointer to appropriate entry point or NULL if no correct entry point exists.

Definition at line 250 of file templatefactory.cpp.

250  {
251  if (bundle_data.isEmpty()) {
252  return NULL;
253  }
254 
255  QDomDocument xml_document;
256  xml_document.setContent(bundle_data);
257 
258  return m_availableTemplates.value(xml_document.documentElement().attribute("type"), NULL);
259 }

Here is the caller graph for this function:

bool TemplateFactory::entryPointIsLessThan ( TemplateEntryPoint s1,
TemplateEntryPoint s2 
)
static

Performs lexicographical comparison of two entry points.

Note
This is used for sorting entry points in FormNewProject class.
Parameters
s1First entry point.
s2Second entry point.
Returns
Returns true if s1 is less than s2, otherwise returns false.

Definition at line 167 of file templatefactory.cpp.

167  {
168  return s1->humanName() < s2.humanName();
169 }
virtual QString humanName() const
Human-readable name of template.

Here is the call graph for this function:

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 
)

Generates common XML bundle.

Returns
Returns XML bundle structure.

Definition at line 134 of file templatefactory.cpp.

134  {
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");
144 
145 
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));
152 
153  author_element.appendChild(name_element);
154  author_element.appendChild(email_element);
155 
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);
161 
162  xml_bundle.appendChild(root_element);
163 
164  return xml_bundle;
165 }
TemplateGenerator * TemplateFactory::generator ( ) const

Access to component which supervises generating of APK files.

Returns
Returns pointer to generator.

Definition at line 318 of file templatefactory.cpp.

318  {
319  return m_generator;
320 }
bool TemplateFactory::loadProject ( const QString &  bundle_file_name)
slot

Loads stored project and initializes new core according to it.

Parameters
bundle_file_nameXML bundle file name of saved project.

Definition at line 197 of file templatefactory.cpp.

197  {
198  // TODO: Load project from XML bundle file.
199  // Detect which template is it, then start new project with that template
200  // and fill data in.
201 
202  QFile bundle_file(bundle_file_name);
203 
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);
208 
209  return false;
210  }
211 
212  // Bundle file is opened, read its contents and determine which template entry point
213  // it belongs to.
214  QString bundle_data(bundle_file.readAll());
215  bundle_file.close();
216 
217  TemplateEntryPoint *target_entry_point = entryPointForBundle(bundle_data);
218 
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);
223 
224  return false;
225  }
226 
227  TemplateCore *loaded_core = target_entry_point->loadCoreFromBundleData(bundle_data);
228 
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);
233 
234  return false;
235  }
236 
237  // Clear previous data and start loaded core.
238  clearEntryAndCore();
239  loaded_core->setAssignedFile(bundle_file_name);
240  loaded_core->editor()->setIsDirty(false);
241 
242  m_activeEntryPoint = target_entry_point;
243  m_activeCore = loaded_core;
244 
245  emit newTemplateCoreCreated(m_activeCore);
246 
247  return true;
248 }
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.
virtual TemplateEditor * editor() const
Access to editor widget of the template.
Definition: templatecore.h:84
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).
The entry point for a template.
void setIsDirty(bool is_dirty)
Sets new dirtiness status.
The core class container for single template.
Definition: templatecore.h:43

Here is the call graph for this function:

void TemplateFactory::newTemplateCoreCreated ( TemplateCore core)
signal

Emitted if new project using some template core is started.

Parameters
coreCore object pointer.

Here is the caller graph for this function:

QString TemplateFactory::outputDirectory ( ) const

Access to directory used throughout APK generation process.

Returns
Returns output directory used throughout APK generation process.

Definition at line 100 of file templatefactory.cpp.

100  {
101 #if QT_VERSION >= 0x050000
102  return qApp->settings()->value(APP_CFG_TEMPLATES, "output_directory",
103  QStandardPaths::writableLocation(QStandardPaths::DesktopLocation)).toString();
104 #else
105  return qApp->settings()->value(APP_CFG_TEMPLATES, "output_directory",
106  QDesktopServices::storageLocation(QDesktopServices::DesktopLocation)).toString();
107 #endif
108 }
bool TemplateFactory::saveCurrentProject ( )
slot

Saves current project to assigned file if there is any.

Returns
Returns true if project was saved, otherwise returns false.

Definition at line 294 of file templatefactory.cpp.

294  {
295  if (activeCore() == NULL || activeCore()->assignedFile().isEmpty()) {
296  return false;
297  }
298  else {
299  return saveCurrentProjectAs(activeCore()->assignedFile());
300  }
301 }
TemplateCore * activeCore() const
Access to active core.
bool saveCurrentProjectAs(const QString &bundle_file_name)
Saves current project to given file.

Here is the call graph for this function:

bool TemplateFactory::saveCurrentProjectAs ( const QString &  bundle_file_name)
slot

Saves current project to given file.

Parameters
bundle_file_nameFile to save project XML bundle to.
Returns
Returns true if project was saved, otherwise returns false.

Definition at line 261 of file templatefactory.cpp.

261  {
262  // TODO: Save current project to given file.
263 
264  QString xml_bundle_contents = activeCore()->editor()->generateBundleData();
265 
266  if (xml_bundle_contents.isEmpty()) {
267  // There is nothing to save. This is quite problem.
268  qWarning("There is nothing to save for template \"%s\".", qPrintable(activeCore()->entryPoint()->humanName()));
269  return false;
270  }
271 
272  QFile target_xml_file(bundle_file_name);
273 
274  if (target_xml_file.open(QIODevice::Truncate | QIODevice::WriteOnly |
275  QIODevice::Unbuffered | QIODevice::Text)) {
276  QTextStream stream(&target_xml_file);
277 
278  stream << xml_bundle_contents;
279  stream.flush();
280 
281  target_xml_file.flush();
282  target_xml_file.close();
283 
284  activeCore()->setAssignedFile(bundle_file_name);
285  activeCore()->editor()->setIsDirty(false);
286 
287  return true;
288  }
289  else {
290  return false;
291  }
292 }
virtual TemplateEditor * editor() const
Access to editor widget of the template.
Definition: templatecore.h:84
TemplateCore * activeCore() const
Access to active core.
void setAssignedFile(const QString &assigned_file)
Sets new assigned file.
void setIsDirty(bool is_dirty)
Sets new dirtiness status.
virtual QString generateBundleData()=0
Generates RAW data which represent data of this template.

Here is the call graph for this function:

Here is the caller graph for this function:

bool TemplateFactory::startNewProject ( TemplateEntryPoint entry_point)
slot

Starts new project core from given entry point.

Parameters
entry_pointEntry point to be started.

Definition at line 185 of file templatefactory.cpp.

185  {
186  // Start new project with selected template entry point.
187  clearEntryAndCore();
188 
189  m_activeEntryPoint = entry_point;
190  m_activeCore = entry_point->createNewCore();
191 
192  emit newTemplateCoreCreated(m_activeCore);
193 
194  return true;
195 }
void newTemplateCoreCreated(TemplateCore *core)
Emitted if new project using some template core is started.
virtual TemplateCore * createNewCore()=0
Creates new instance of template core.

Here is the call graph for this function:

QString TemplateFactory::tempDirectory ( ) const

Access to temporary directory used throughout APK generation process.

Returns
Returns temporary directory used throughout APK generation process.

Definition at line 86 of file templatefactory.cpp.

86  {
87 #if QT_VERSION >= 0x050000
88  return qApp->settings()->value(APP_CFG_TEMPLATES, "temp_directory",
89  QStandardPaths::writableLocation(QStandardPaths::TempLocation)).toString();
90 #else
91  return qApp->settings()->value(APP_CFG_TEMPLATES, "temp_directory",
92  QDesktopServices::storageLocation(QDesktopServices::TempLocation)).toString();
93 #endif
94 }

The documentation for this class was generated from the following files: