maemo5qtprojectgenerator.cpp 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046
  1. #include <QApplication>
  2. #include <QDateTime>
  3. #include <QDebug>
  4. #include <QFile>
  5. #include <QLocale>
  6. #include <QXmlQuery>
  7. #include "defines.h"
  8. #include "configxmlparser.h"
  9. #include "directoryutilities.h"
  10. #include "filecopier.h"
  11. #include "fileutilities.h"
  12. #include "infoplistparser.h"
  13. #include "settings.h"
  14. #include "maemo5defines.h"
  15. #include "maemo5processrunner.h"
  16. #include "maemo5qtprojectgenerator.h"
  17. #include "maemo5settingsgui.h"
  18. #include "maemo5statusupdater.h"
  19. Maemo5QtProjectGenerator::Maemo5QtProjectGenerator(GeneratorOutputView & outputView,
  20. QObject * parent)
  21. : m_outputView(outputView),
  22. m_processRunner(NULL),
  23. m_currentGeneratorPhase(M5_INITIAL),
  24. m_generatorTarget(NO_TARGET),
  25. m_resolvedWidgetType(UNKNOWN_WIDGET)
  26. {
  27. m_processRunner = new Maemo5ProcessRunner(*this);
  28. // TODO should come from some defines
  29. m_ignorePatterns.insert("*.png");
  30. }
  31. Maemo5QtProjectGenerator::~Maemo5QtProjectGenerator()
  32. {
  33. delete m_processRunner;
  34. m_processRunner = 0;
  35. }
  36. IStatusUpdater * Maemo5QtProjectGenerator::createStatusUpdater(QObject * parent)
  37. {
  38. IStatusUpdater
  39. * rv = new Maemo5StatusUpdater(*this,
  40. parent);
  41. return rv;
  42. }
  43. const PlatformType& Maemo5QtProjectGenerator::platform()
  44. {
  45. m_platform.m_platformName = MAEMO5_PLATFORMNAME;
  46. m_platform.m_platformVersion = 5;
  47. m_platform.m_qtVersion = Settings::get(Settings::QtVersion).toInt();
  48. return m_platform;
  49. }
  50. void Maemo5QtProjectGenerator::build(const QString & widgetFile,
  51. RcSession & /* rcSession */)
  52. {
  53. emit started(BUILD);
  54. m_generatorTarget = BUILD;
  55. m_currentGeneratorPhase = M5_INITIAL;
  56. m_resolvedWidgetType = UNKNOWN_WIDGET;
  57. if (!registerStatus(prepareEnvironment(widgetFile)))
  58. return;
  59. updateSubstitutions(widgetFile);
  60. copyWidgetDirectory(widgetFile);
  61. }
  62. void Maemo5QtProjectGenerator::rebuild(RcSession & /* rcSession */)
  63. {
  64. emit started(REBUILD);
  65. m_generatorTarget = REBUILD;
  66. m_currentGeneratorPhase = M5_INITIAL;
  67. startBuild();
  68. }
  69. void Maemo5QtProjectGenerator::preview(const QString &widgetFile)
  70. {
  71. // TODO this method will be removed from IQtProjectGenerator interface
  72. ;
  73. }
  74. void Maemo5QtProjectGenerator::closeProject()
  75. {
  76. m_generatorTarget = NO_TARGET;
  77. m_currentGeneratorPhase = M5_INITIAL;
  78. m_resolvedWidgetType = UNKNOWN_WIDGET;
  79. }
  80. GeneratorOutputView & Maemo5QtProjectGenerator::outputView() const
  81. {
  82. return m_outputView;
  83. }
  84. GeneratorData & Maemo5QtProjectGenerator::generatorData()
  85. {
  86. return m_generatorData;
  87. }
  88. GeneratorPhase Maemo5QtProjectGenerator::getCurrentPhase() const
  89. {
  90. return m_currentGeneratorPhase;
  91. }
  92. GeneratorTarget Maemo5QtProjectGenerator::getTarget() const
  93. {
  94. return m_generatorTarget;
  95. }
  96. QString Maemo5QtProjectGenerator::getStatusText() const
  97. {
  98. QString statusText;
  99. switch(m_currentGeneratorPhase.m_phase)
  100. {
  101. case M5_UNZIP_WIDGET:
  102. statusText = HAG_STATUS_EXTRACTING_WIDGET;
  103. break;
  104. case M5_DPKG_BUILDPACKAGE:
  105. statusText = QString("Running 'mad dpkg-buildpackage'");
  106. break;
  107. case M5_INITIAL:
  108. statusText = HAG_STATUS_IDLE;
  109. break;
  110. case M5_ENDED_SUCCESSFULLY:
  111. statusText = HAG_STATUS_COMPLETED_OK;
  112. break;
  113. case M5_ENDED_WITH_ERRORS:
  114. statusText = HAG_STATUS_COMPLETED_NOTOK;
  115. break;
  116. }
  117. return statusText;
  118. }
  119. QWidget * Maemo5QtProjectGenerator::createSettingsWidget(const QString & widgetFile,
  120. QWidget * parent) const
  121. {
  122. const QString
  123. TODO_COPYRIGHT_DIR("/templates/copyright/");
  124. QString
  125. copyrightDirPath = QApplication::applicationDirPath() + TODO_COPYRIGHT_DIR;
  126. QDir
  127. copyRightDir(copyrightDirPath);
  128. QList<QString>
  129. licenses = copyRightDir.entryList(QDir::Dirs
  130. | QDir::NoDotAndDotDot);
  131. // OBS (const_cast<Maemo5QtProjectGenerator*>(this))->sniffAppNames(widgetFile);
  132. QPair<QString, QString>
  133. appNames = FileUtilities::sniffAppNames(widgetFile);
  134. QWidget
  135. * rv = new Maemo5SettingsGui(licenses,
  136. // OBS m_appName,
  137. appNames.first,
  138. parent);
  139. return rv;
  140. }
  141. void Maemo5QtProjectGenerator::saveSettings(QWidget * settingsWidget) const
  142. {
  143. Maemo5SettingsGui
  144. * maemo5SettingsGui = dynamic_cast<Maemo5SettingsGui*>(settingsWidget);
  145. if (maemo5SettingsGui != NULL)
  146. {
  147. maemo5SettingsGui->saveSettings();
  148. }
  149. }
  150. QStringList Maemo5QtProjectGenerator::errorsForTarget(GeneratorTarget generatorTarget) const
  151. {
  152. QStringList
  153. rv;
  154. QString
  155. bashPath = Maemo5SettingsGui::bashPath();
  156. if (bashPath.length() == 0)
  157. {
  158. rv.append("Path to bash.exe is not set");
  159. }
  160. else if (!QFileInfo(bashPath).isFile())
  161. {
  162. rv.append("Path to bash.exe is set, but is invalid");
  163. }
  164. QFile log(Settings::get(Settings::LogFilePath).toString());
  165. if (!log.open(QIODevice::ReadWrite | QIODevice::Text))
  166. {
  167. // Failed to open the log file
  168. if (generatorTarget != NO_TARGET)
  169. {
  170. rv.append("Failed to open log file for writing! Check settings.");
  171. }
  172. }
  173. log.close();
  174. if (Settings::get(Settings::HybridAuthor).toString().length() == 0)
  175. {
  176. rv.append("Author is not set");
  177. }
  178. if (Settings::get(Settings::HybridEmail).toString().length() == 0)
  179. {
  180. rv.append("Author email is not set");
  181. }
  182. else
  183. {
  184. QRegExp
  185. validEmailRx("(\\w|\\.-)+@(\\w|\\.)+");
  186. if (validEmailRx.indexIn(Settings::get(Settings::HybridEmail).toString()) == -1)
  187. {
  188. rv.append("Author email is set, but seems broken");
  189. }
  190. }
  191. // TODO - check long and short descriptions too
  192. return rv;
  193. }
  194. IQtProjectGenerator::PkgInstallResult
  195. Maemo5QtProjectGenerator::installPkg(const QFileInfo & pkgFile) const
  196. {
  197. return PKGINSTALL_NOT_SUPPORTED;
  198. }
  199. void Maemo5QtProjectGenerator::dpkgBuildPackageFinished(int exitCode,
  200. QProcess::ExitStatus exitStatus)
  201. {
  202. if (!exitCode && !exitStatus)
  203. {
  204. m_currentGeneratorPhase = M5_ENDED_SUCCESSFULLY;
  205. emit finished(EXIT_SUCCESS);
  206. }
  207. else
  208. {
  209. registerStatus(false);
  210. QString msg = QString(ERR_PROCESS_FINISHED_WITH_ERRORS_CANNOT_CONTINUE).arg("mad dpkg-buildpackage")
  211. + QString(MSG_PROCESS_EXIT_CODE_STATUS).arg(exitCode).arg(exitStatus);
  212. m_outputView.printOutput(msg);
  213. emit finished(EXIT_FAILURE);
  214. }
  215. }
  216. void Maemo5QtProjectGenerator::widgetFilesCopied(int exitCode,
  217. QProcess::ExitStatus exitStatus)
  218. {
  219. if (!exitCode && !exitStatus)
  220. {
  221. prepareProject();
  222. }
  223. else
  224. {
  225. registerStatus(false);
  226. QString msg = QString(ERR_PROCESS_FINISHED_WITH_ERRORS_CANNOT_CONTINUE).arg(UNZIP_APP_NAME) + QString(MSG_PROCESS_EXIT_CODE_STATUS).arg(exitCode).arg(exitStatus);
  227. m_outputView.printOutput(msg);
  228. emit finished(EXIT_FAILURE);
  229. }
  230. }
  231. void Maemo5QtProjectGenerator::copyWidgetDirectory(const QString &filename)
  232. {
  233. QFileInfo targetInfo(filename);
  234. if (!targetInfo.isDir())
  235. {
  236. // Not a directory, so proceed with unzip instead
  237. unzipWidgetFile(filename);
  238. }
  239. else
  240. {
  241. // Directory -- copy files directly
  242. qDebug() << "Copy directory";
  243. m_resolvedWidgetType = DIRECTORY;
  244. DirectoryUtilities dirUtil(m_outputView, m_generatorData);
  245. QString dest = m_generatorData.widgetDirectory();
  246. if (dirUtil.copyDirectory(filename, dest))
  247. widgetFilesCopied(0);
  248. else
  249. return;
  250. }
  251. }
  252. bool Maemo5QtProjectGenerator::prepareProject()
  253. {
  254. // copy framework and plugin source files to the build directory
  255. if (!registerStatus(copyFrameworkAndPluginSrcFiles()))
  256. return false;
  257. // find out type of widget package
  258. sniffWidgetType();
  259. if (!registerStatus(m_resolvedWidgetType != UNKNOWN_WIDGET))
  260. {
  261. return false;
  262. }
  263. if (m_resolvedWidgetType != WRT_WIDGET)
  264. {
  265. // create info.plist and copy widget files to right place
  266. if (!registerStatus(prepareEnvironmentForNonWRTWidget()))
  267. return false;
  268. }
  269. // read in info.plist
  270. if (!registerStatus(handleWidgetConfigFile()))
  271. return false;
  272. if (!registerStatus(copyHybridApiJs()))
  273. return false;
  274. if (!Settings::get(Settings::TextSelectionEnabled).toBool())
  275. {
  276. if (!registerStatus(copyDisableSelectionCSS()))
  277. {
  278. return false;
  279. }
  280. }
  281. // copy all maemo5 build related files, except the copyright
  282. if (!registerStatus(copyMaemo5Files()))
  283. return false;
  284. // copy the copyright file
  285. if (!registerStatus(copyCopyrightFile()))
  286. return false;
  287. // read in info of widget directory
  288. handleWidgetDirectory();
  289. // update main.cpp file
  290. if (!registerStatus(updateMainFile()))
  291. return false;
  292. // update mainwindow.cpp file
  293. if (!registerStatus(updateMainWindowFile()))
  294. return false;
  295. // generate Qt resource file
  296. parseWidgetResourceFile();
  297. // Update the framework project file
  298. if (!registerStatus(updateFrameworkProjectFile()))
  299. return false;
  300. /* TODO LATER - mobility plugins are ignored in the first version
  301. // Create mw.pro file that ties the plugins to the project,
  302. // also updates the plugin project files to e.g. include correct
  303. // Qt Mobility paths.
  304. if (!registerStatus(parsePluginsProjectFile()))
  305. return false;
  306. */
  307. startBuild();
  308. return true;
  309. }
  310. bool Maemo5QtProjectGenerator::prepareEnvironment(const QString &widgetFile)
  311. {
  312. // clear data structures
  313. m_generatorData.clearAll();
  314. m_generatorData.initPaths(widgetFile,
  315. MAEMO5_PLATFORMNAME);
  316. QString basePath = m_generatorData.basePath();
  317. QString buildDirectory = m_generatorData.buildDirectory();
  318. QString projectDirectory = m_generatorData.projectDirectory();
  319. QString srcDirectory = m_generatorData.srcDirectory();
  320. QString dataDirectory = m_generatorData.m5DataDirectory();
  321. QString debianDirectory = m_generatorData.m5DebianDirectory();
  322. QString widgetDirectory = m_generatorData.widgetDirectory();
  323. QDir dir;
  324. // Check the base directory, create if missing
  325. if (!dir.exists(basePath) && !dir.mkdir(basePath))
  326. {
  327. return false;
  328. }
  329. // Check the build directory, create if missing
  330. if (!dir.exists(buildDirectory) && !dir.mkdir(buildDirectory))
  331. {
  332. return false;
  333. }
  334. // Check the project directory, create if missing
  335. if (!dir.exists(projectDirectory) && !dir.mkdir(projectDirectory))
  336. {
  337. return false;
  338. }
  339. // check the data (icons, etc) directory
  340. if (!dir.exists(dataDirectory) && !dir.mkdir(dataDirectory))
  341. {
  342. return false;
  343. }
  344. if (!dir.exists(debianDirectory) && !dir.mkdir(debianDirectory))
  345. {
  346. return false;
  347. }
  348. // check the src (source for Qt app) directory, create if missing
  349. if (!dir.exists(srcDirectory) && !dir.mkdir(srcDirectory))
  350. {
  351. return false;
  352. }
  353. // Check the widget directory
  354. if (!dir.exists(widgetDirectory))
  355. {
  356. if (!dir.mkdir(widgetDirectory))
  357. {
  358. return false;
  359. }
  360. }
  361. else
  362. {
  363. // Clean the project directory
  364. DirectoryUtilities::cleanDirectory(widgetDirectory, true, widgetDirectory);
  365. }
  366. return true;
  367. }
  368. void Maemo5QtProjectGenerator::unzipWidgetFile(const QString & widgetFile)
  369. {
  370. QFileInfo widgetFileInfo(widgetFile);
  371. if (widgetFileInfo.completeSuffix() == WRT_WIDGET_FILE_EXTENSION)
  372. m_resolvedWidgetType = WRT_WIDGET;
  373. QString unzipDir = m_generatorData.widgetDirectory();
  374. // Run unzip normally
  375. qDebug() << "Unzip";
  376. QStringList params;
  377. params << widgetFile << unzipDir;
  378. m_currentGeneratorPhase = M5_UNZIP_WIDGET;
  379. m_processRunner->runApp(m_currentGeneratorPhase, params);
  380. }
  381. void Maemo5QtProjectGenerator::sniffWidgetType()
  382. {
  383. // TODO this is verbatim the same as SymbianQtProjectGenerator - factor out?
  384. // find config.xml
  385. QString w3cConfigFileName(m_generatorData.widgetDirectory() + W3C_CONFIG_FILE);
  386. if (QFile::exists(w3cConfigFileName))
  387. {
  388. // analyze config.xml
  389. QFile w3cConfigFileIn(w3cConfigFileName);
  390. if (!w3cConfigFileIn.open(QIODevice::ReadOnly | QIODevice::Text))
  391. {
  392. QString errMsg = QString(ERR_FILE_OPEN_READ_FAILED_CANNOT_CONTINUE).arg(w3cConfigFileName);
  393. m_outputView.printOutput(errMsg);
  394. m_resolvedWidgetType = UNKNOWN_WIDGET;
  395. }
  396. QTextStream streamIn(&w3cConfigFileIn);
  397. w3cConfigFileIn.close();
  398. ConfigXmlParser parser(m_outputView, m_generatorData);
  399. parser.parse();
  400. QMap<QString, QString> &configXmlData = m_generatorData.configXmlData();
  401. if (configXmlData.contains("widget.name:"))
  402. {
  403. m_resolvedWidgetType = W3C_WIDGET;
  404. }
  405. else if (configXmlData.contains("widget.widgetname:"))
  406. {
  407. m_resolvedWidgetType = OPERA_WIDGET;
  408. }
  409. }
  410. }
  411. bool Maemo5QtProjectGenerator::registerStatus(bool status)
  412. {
  413. if (!status)
  414. m_currentGeneratorPhase = M5_ENDED_WITH_ERRORS;
  415. return status;
  416. }
  417. bool Maemo5QtProjectGenerator::copyFrameworkAndPluginSrcFiles()
  418. {
  419. DirectoryUtilities dirUtil(m_outputView, m_generatorData);
  420. // copy the framework files
  421. qDebug() << QApplication::applicationDirPath() + FRAMEWORK_DIR << m_generatorData.fwDirectory();
  422. if (!dirUtil.copyDirectory(QApplication::applicationDirPath() + FRAMEWORK_DIR, m_generatorData.fwDirectory()))
  423. return false;
  424. // copy the template files
  425. qDebug() << QApplication::applicationDirPath() + TEMPLATES_DIR << m_generatorData.srcDirectory();
  426. bool
  427. rv = dirUtil.copyDirectory(QApplication::applicationDirPath() + TEMPLATES_DIR,
  428. m_generatorData.srcDirectory());
  429. /* TODO do it later, first version ignores mobility plugins
  430. // copy the pugins
  431. qDebug() << QApplication::applicationDirPath() + PLUGINS_DIR << m_generatorData.mwDirectory();
  432. return dirUtil.copyDirectory(QApplication::applicationDirPath() + PLUGINS_DIR, m_generatorData.mwDirectory());
  433. */
  434. return rv;
  435. }
  436. bool Maemo5QtProjectGenerator::prepareEnvironmentForNonWRTWidget()
  437. {
  438. FileUtilities fileUtilities(m_outputView, m_generatorData);
  439. bool retval = false;
  440. if (m_resolvedWidgetType == OPERA_WIDGET || m_resolvedWidgetType == W3C_WIDGET)
  441. {
  442. retval = fileUtilities.configXmlToInfoPlistFile(m_resolvedWidgetType);
  443. }
  444. else if (m_resolvedWidgetType == DIRECTORY)
  445. {
  446. retval = fileUtilities.directoryToInfoPlistFile();
  447. }
  448. return retval;
  449. }
  450. bool Maemo5QtProjectGenerator::handleWidgetConfigFile()
  451. {
  452. InfoPlistParser infoPlistParser(m_outputView, m_generatorData);
  453. return infoPlistParser.parse();
  454. }
  455. bool Maemo5QtProjectGenerator::copyHybridApiJs()
  456. {
  457. QString sourceFileName(QApplication::applicationDirPath()+QString(TEMPLATES_DIR) + QString(HYBRIDAPI_JS_FILE));
  458. QString destinationFileName(m_generatorData.infoPlistValue(KEY_WIDGET_ROOT_DIR) + HYBRIDAPI_JS_FILE);
  459. QFileInfo sourceInfo(sourceFileName), destinationInfo(destinationFileName);
  460. if (!sourceInfo.exists())
  461. {
  462. QString errMsg = QString(ERR_FILE_NOT_FOUND).arg(HYBRIDAPI_JS_FILE);
  463. m_outputView.printOutput(errMsg);
  464. return false;
  465. }
  466. if (destinationInfo.exists())
  467. {
  468. QFile destinationFile(destinationFileName);
  469. destinationFile.remove();
  470. }
  471. QFile::copy(sourceFileName, destinationFileName);
  472. return true;
  473. }
  474. bool Maemo5QtProjectGenerator::copyDisableSelectionCSS()
  475. {
  476. QString sourceFileName(QApplication::applicationDirPath()+QString(TEMPLATES_DIR) + QString(DISABLE_SELECTION_CSS_FILE));
  477. QString destinationFileName(m_generatorData.infoPlistValue(KEY_WIDGET_ROOT_DIR) + DISABLE_SELECTION_CSS_FILE);
  478. QFileInfo sourceInfo(sourceFileName), destinationInfo(destinationFileName);
  479. if (!sourceInfo.exists())
  480. {
  481. QString errMsg = QString(ERR_FILE_NOT_FOUND).arg(DISABLE_SELECTION_CSS_FILE);
  482. m_outputView.printOutput(errMsg);
  483. return false;
  484. }
  485. if (destinationInfo.exists())
  486. {
  487. QFile destinationFile(destinationFileName);
  488. destinationFile.remove();
  489. }
  490. m_outputView.printOutput("Copy: " + sourceFileName + ", " + destinationFileName);
  491. QFile::copy(sourceFileName, destinationFileName);
  492. return true;
  493. }
  494. void Maemo5QtProjectGenerator::handleWidgetDirectory()
  495. {
  496. QDir widgetDir = m_generatorData.widgetDirectory();
  497. QDir::Filters filters = QDir::Files | QDir::AllDirs | QDir::NoDotAndDotDot;
  498. QStringList nameFilters;
  499. DirectoryUtilities dirUtil(m_outputView, m_generatorData);
  500. qDebug() << "DIR VERSION" << widgetDir;
  501. dirUtil.getFiles(widgetDir.canonicalPath(),
  502. filters,
  503. nameFilters,
  504. widgetDir.canonicalPath().length(),
  505. WIDGET_SUBDIR,
  506. m_generatorData.widgetSrcFiles());
  507. m_outputView.printOutput(QString(MSG_WIDGET_DIRECTORY_PROCESSED_N_FILES_FOUND).arg(m_generatorData.widgetSrcFiles().count()));
  508. }
  509. bool Maemo5QtProjectGenerator::updateMainFile()
  510. {
  511. FileUtilities fileUtilities(m_outputView, m_generatorData);
  512. return fileUtilities.updateMainFile();
  513. }
  514. bool Maemo5QtProjectGenerator::updateMainWindowFile()
  515. {
  516. FileUtilities fileUtilities(m_outputView, m_generatorData);
  517. return fileUtilities.updateMainWindowFile();
  518. }
  519. void Maemo5QtProjectGenerator::parseWidgetResourceFile()
  520. {
  521. FileUtilities fileUtilities(m_outputView, m_generatorData);
  522. fileUtilities.createWidgetResourceFile();
  523. }
  524. bool Maemo5QtProjectGenerator::updateFrameworkProjectFile()
  525. {
  526. FileUtilities fileUtilities(m_outputView, m_generatorData);
  527. QString fwProjectFile = m_generatorData.fwDirectory() + FRAMEWORK_PROJECT_FILE;
  528. qDebug() << "Updating fw project file: " << fwProjectFile;
  529. return fileUtilities.updateProjectFile(fwProjectFile);
  530. }
  531. bool Maemo5QtProjectGenerator::copyMaemo5Files()
  532. {
  533. bool
  534. rv = false;
  535. // TODO should come from defines
  536. const QString
  537. TODO_MAEMO5_DIR("/templates/maemo5/");
  538. qDebug() << QApplication::applicationDirPath() + TODO_MAEMO5_DIR;
  539. FileCopier
  540. fileCopier(QApplication::applicationDirPath() + TODO_MAEMO5_DIR,
  541. m_generatorData.projectDirectory(),
  542. &m_filenameSubstitutions,
  543. &m_ignorePatterns,
  544. &m_keywordSubstitutions,
  545. m_outputView);
  546. rv = fileCopier.copy();
  547. if (rv)
  548. {
  549. // Remove src/hybridapp.pro file (orginally copied there
  550. // by the reused symbian code) - we have our own src.pro in there!
  551. // TODO perhaps clean up this hack
  552. rv = QFile::remove(m_generatorData.srcDirectory() + PROJECT_TEMPLATE_FILE);
  553. }
  554. return rv;
  555. }
  556. bool Maemo5QtProjectGenerator::copyCopyrightFile()
  557. {
  558. bool
  559. rv = false;
  560. // TODO should come from defines
  561. const QString
  562. TODO_COPYRIGHT_FILE("/templates/copyright/%1/copyright");
  563. // TODO should come from settings
  564. QString
  565. copyrightFile = TODO_COPYRIGHT_FILE.arg("gpl");
  566. qDebug() << QApplication::applicationDirPath() + copyrightFile;
  567. FileCopier
  568. fileCopier(QApplication::applicationDirPath() + copyrightFile,
  569. m_generatorData.m5DebianDirectory(),
  570. NULL,
  571. NULL,
  572. &m_keywordSubstitutions,
  573. m_outputView);
  574. rv = fileCopier.copy();
  575. return rv;
  576. }
  577. void Maemo5QtProjectGenerator::startBuild()
  578. {
  579. QStringList params;
  580. m_currentGeneratorPhase = M5_DPKG_BUILDPACKAGE;
  581. m_processRunner->runApp(m_currentGeneratorPhase, params);
  582. }
  583. /* OBS
  584. bool Maemo5QtProjectGenerator::syncUnzipWidgetFile(const QString & wgzFile,
  585. const QString & unzipDir)
  586. {
  587. QFileInfo widgetFileInfo(wgzFile);
  588. QStringList arguments;
  589. arguments << "-xo" << wgzFile << "-d" << unzipDir;
  590. QProcess
  591. * process = new QProcess( this );
  592. // ? process->setReadChannelMode(QProcess::MergedChannels);
  593. // do we need to read output in order to prevent the process
  594. // from stalling?
  595. process->start(UNZIP_APP_NAME,
  596. arguments);
  597. process->waitForFinished(-1);
  598. bool
  599. rv = process->exitStatus() == QProcess::NormalExit
  600. && (process->exitCode() == 0);
  601. delete process;
  602. return rv;
  603. }
  604. */
  605. /* OBS
  606. bool RemoveContent(QDir tmpDir)
  607. {
  608. bool
  609. rv = false;
  610. QFileInfoList
  611. entries = tmpDir.entryInfoList(QDir::Dirs
  612. | QDir::Files
  613. | QDir::NoDotAndDotDot);
  614. foreach (QFileInfo entry, entries)
  615. {
  616. if (entry.isDir())
  617. {
  618. QDir
  619. dirEntry(entry.absoluteFilePath());
  620. rv = RemoveContent(dirEntry);
  621. rv = rv && tmpDir.rmdir(dirEntry.dirName());
  622. }
  623. else
  624. {
  625. rv = tmpDir.remove(entry.fileName());
  626. }
  627. }
  628. return rv;
  629. }
  630. bool ClearUpDir(QDir tmpDir)
  631. {
  632. bool
  633. rv = false;
  634. if (tmpDir.exists())
  635. {
  636. rv = RemoveContent(tmpDir);
  637. }
  638. else
  639. {
  640. rv = tmpDir.mkpath(tmpDir.absolutePath());
  641. }
  642. return rv;
  643. }
  644. */
  645. /* OBS
  646. QString FindFile(const QDir & dir,
  647. const QString & fileName)
  648. {
  649. QString
  650. rv;
  651. if (dir.exists(fileName))
  652. {
  653. rv = dir.absolutePath() + QDir::separator() + fileName;
  654. }
  655. else
  656. {
  657. QFileInfoList
  658. entries = dir.entryInfoList(QDir::Dirs
  659. | QDir::NoDotAndDotDot);
  660. foreach (QFileInfo entry, entries)
  661. {
  662. rv = FindFile(QDir(entry.absoluteFilePath()),
  663. fileName);
  664. if (rv.length() > 0)
  665. break;
  666. }
  667. }
  668. return rv;
  669. }
  670. */
  671. /* OBS
  672. void Maemo5QtProjectGenerator::sniffAppNames(const QString & widgetFilePath)
  673. {
  674. m_appName = "";
  675. m_appDisplayName = "";
  676. QFileInfo
  677. widgetFile(widgetFilePath);
  678. if (widgetFile.isDir())
  679. {
  680. QDir
  681. widgetDir(widgetFilePath);
  682. m_appDisplayName = widgetDir.dirName();
  683. m_appName = m_appDisplayName.toLower();
  684. m_appName.replace(" ", "");
  685. }
  686. else
  687. {
  688. QDir
  689. tmpDir(QDir::tempPath() + "/hybrid");
  690. bool
  691. success = ClearUpDir(tmpDir);
  692. success = syncUnzipWidgetFile(widgetFilePath,
  693. tmpDir.absolutePath());
  694. if (success)
  695. {
  696. QXmlQuery
  697. xmlQuery;
  698. QString
  699. infoPlistPath(FindFile(tmpDir, INFO_PLIST_FILE));
  700. QString
  701. xpath = "doc('%1')/descendant::dict/key[text()='DisplayName']/following-sibling::string[1]/child::text()";
  702. xpath = xpath.arg(infoPlistPath);
  703. xmlQuery.setQuery(xpath);
  704. QString
  705. text;
  706. success = xmlQuery.evaluateTo(&text);
  707. if (success)
  708. {
  709. m_appDisplayName = text.trimmed();
  710. m_appName = m_appDisplayName.toLower();
  711. m_appName.replace(" ", "");
  712. }
  713. else
  714. {
  715. QString
  716. configXmlPath(FindFile(tmpDir, W3C_CONFIG_FILE));
  717. xpath = "doc('%1')/widget/widgetname/child::text()";
  718. xpath = xpath.arg(configXmlPath);
  719. xmlQuery.setQuery(xpath);
  720. success = xmlQuery.evaluateTo(&text);
  721. if (success)
  722. {
  723. m_appDisplayName = text.trimmed();
  724. m_appName = m_appDisplayName.toLower();
  725. m_appName.replace(" ", "");
  726. }
  727. else
  728. {
  729. // DEFAULT CASE
  730. m_appDisplayName = "Hybrid Widget";
  731. m_appName = "hybrid";
  732. }
  733. }
  734. }
  735. }
  736. }
  737. */
  738. namespace
  739. {
  740. QString localeIndependentDayOfWeek(int dayOfWeek)
  741. {
  742. static const char * const dayAbbreviations[] = {
  743. "Mon",
  744. "Tue",
  745. "Wed",
  746. "Thu",
  747. "Fri",
  748. "Sat",
  749. "Sun"
  750. };
  751. QString
  752. rv("Day");
  753. if (dayOfWeek > 0 && dayOfWeek <= (int)(sizeof(dayAbbreviations)/sizeof(char*)))
  754. {
  755. rv = dayAbbreviations[dayOfWeek - 1];
  756. }
  757. return rv;
  758. }
  759. QString localeIndependentMonth(int month)
  760. {
  761. static const char * const monthAbbreviations[] = {
  762. "Jan",
  763. "Feb",
  764. "Mar",
  765. "Apr",
  766. "May",
  767. "Jun",
  768. "Jul",
  769. "Aug",
  770. "Sep",
  771. "Oct",
  772. "Nov",
  773. "Dec"
  774. };
  775. QString
  776. rv("Month");
  777. if (month > 0 && month <= (int)(sizeof(monthAbbreviations)/sizeof(char*)))
  778. {
  779. rv = monthAbbreviations[month-1];
  780. }
  781. return rv;
  782. }
  783. } // namespace
  784. void Maemo5QtProjectGenerator::updateSubstitutions(const QString & widgetFile)
  785. {
  786. // OBS sniffAppNames(widgetFile);
  787. QPair<QString, QString>
  788. appNames = FileUtilities::sniffAppNames(widgetFile);
  789. m_appName = appNames.first;
  790. m_appDisplayName = appNames.second;
  791. // FILENAME SUBSTITUTIONS
  792. m_filenameSubstitutions.clear();
  793. m_filenameSubstitutions[FNAMESUBST_HYBRIDAPP] = m_appName;
  794. // the template file "hybridapp_default.ex" needs to be changed to
  795. // "foo.default.ex" ("foo" being package name). The reason for '_'
  796. // switching to '.' is that if the template file had two '.' dots,
  797. // Qt's FileInfo::suffix() function would be slightly confused, and
  798. // it's simpler to not have two dots in the template file.
  799. m_filenameSubstitutions[FNAMESUBST_HYBRIDAPP "_default"]
  800. = m_appName + QString(".default");
  801. // see comment aboce for the reason of '_' switching to '.'
  802. // template file name in this case is "hybridapp_doc-base.EX"
  803. // and it should turn into "foo.doc-base.EX" ("foo" being package name)
  804. m_filenameSubstitutions[FNAMESUBST_HYBRIDAPP "_doc-base"]
  805. = m_appName + QString(".doc-base");
  806. m_filenameSubstitutions[FNAMESUBST_HYBRIDAPP "-doc"]
  807. = m_appName + QString("-doc");
  808. // KEYWORD SUBSTITUTIONS
  809. m_keywordSubstitutions.clear();
  810. m_keywordSubstitutions[TXTSUBST_HYBRIDAPP] = m_appName; //"@hybridapp@"
  811. m_keywordSubstitutions[TXTSUBST_HYBRIDAPP_UC] = m_appName.toUpper(); //"@HYBRIDAPP@"
  812. m_keywordSubstitutions[TXTSUBST_HYBRIDAPPNAME] = m_appDisplayName; //"@hybridappname@"
  813. m_keywordSubstitutions[TXTSUBST_HYBRIDBINARY] = m_appName; // @hybridbinary@"
  814. m_keywordSubstitutions[TXTSUBST_HYBRIDAUTHOR]
  815. = Settings::get(Settings::HybridAuthor).toString(); // @hybridauthor@"
  816. m_keywordSubstitutions[TXTSUBST_HYBRIDEMAIL]
  817. = Settings::get(Settings::HybridEmail).toString(); // @hybridemail@"
  818. // TODO proper timezone handling, like "+0300"
  819. QDateTime
  820. now = QDateTime::currentDateTime().toUTC();
  821. QDate
  822. today = now.date();
  823. QString
  824. nowStr = now.toString("%1, d %2 yyyy hh:mm:ss +0000");
  825. nowStr = nowStr.arg(localeIndependentDayOfWeek(today.dayOfWeek()),
  826. localeIndependentMonth(today.month()));
  827. m_keywordSubstitutions[TXTSUBST_HYBRIDDATETIME] = nowStr; // @hybriddatetime@"
  828. m_keywordSubstitutions[TXTSUBST_HYBRIDSHORTDESC]
  829. = Settings::get(Settings::HybridShortDesc, m_appName).toString(); // @hybridshortdesc@"
  830. m_keywordSubstitutions[TXTSUBST_HYBRIDLONGDESC]
  831. = Settings::get(Settings::HybridShortDesc, m_appName).toString(); // @hybridlongdesc@"
  832. QString
  833. currentYear = now.toString("yyyy");
  834. m_keywordSubstitutions[TXTSUBST_HYBRIDYEAR] = currentYear; //"@hybridyear@"
  835. }