infoplistparser.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #include <QDir>
  2. #include <QDomDocument>
  3. #include <QApplication>
  4. #include "infoplistparser.h"
  5. #include "defines.h"
  6. InfoPlistParser::InfoPlistParser(GeneratorOutputView &outputView, GeneratorData &generatorData)
  7. : m_outputView(outputView),
  8. m_generatorData(generatorData)
  9. {
  10. }
  11. bool InfoPlistParser::parse()
  12. {
  13. QString widgetDirectory = m_generatorData.widgetDirectory();
  14. QDir dir(widgetDirectory);
  15. dir.setFilter(QDir::Files | QDir::AllDirs | QDir::NoDotAndDotDot);
  16. // info.plist not found in widget directory
  17. if(!dir.exists(INFO_PLIST_FILE))
  18. {
  19. // find the first subdirectory and try it
  20. foreach(QFileInfo entry, dir.entryInfoList())
  21. if(entry.isDir()) {
  22. dir.cd(entry.baseName());
  23. break;
  24. }
  25. }
  26. if(!dir.exists(INFO_PLIST_FILE)) {
  27. QString errMsg = QString(ERR_FILE_NOT_FOUND).arg(INFO_PLIST_FILE);
  28. m_outputView.printOutput(errMsg);
  29. return false;
  30. }
  31. QMap<QString, QString> &infoPlistData = m_generatorData.infoPlistData();
  32. // read in info.plist
  33. QFile infoPlistFile(dir.filePath(INFO_PLIST_FILE));
  34. infoPlistData.insert(KEY_WIDGET_ROOT_DIR, dir.canonicalPath() + "/");
  35. // check the existence of icon file
  36. if(dir.exists(ICON_FILE)) {
  37. QString fullFileName(dir.canonicalPath() + "/" + ICON_FILE);
  38. QString shortFileName = fullFileName.mid(widgetDirectory.length());
  39. infoPlistData.insert(KEY_WIDGET_ICON_FILE, shortFileName);
  40. }
  41. if(!infoPlistFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
  42. QString errMsg = QString(ERR_FILE_OPEN_READ_FAILED_CANNOT_CONTINUE).arg(INFO_PLIST_FILE);
  43. m_outputView.printOutput(errMsg);
  44. return false;
  45. }
  46. QDomDocument infoPlistDocument;
  47. if(!infoPlistDocument.setContent(&infoPlistFile)) {
  48. QString errMsg = QString(ERR_FILE_PARSING_FAILED_CANNOT_CONTINUE).arg(INFO_PLIST_FILE);
  49. m_outputView.printOutput(errMsg);
  50. infoPlistFile.close();
  51. return false;
  52. }
  53. QDomElement infoPlistDocumentElement = infoPlistDocument.documentElement();
  54. QDomNode node = infoPlistDocumentElement.firstChild();
  55. QDomElement element;
  56. bool dictNodeFound = false;
  57. while(!node.isNull()) {
  58. if(node.isElement()) {
  59. element = node.toElement();
  60. if(element.tagName() == INFO_PLIST_TAG_DICT) {
  61. node = node.firstChild();
  62. dictNodeFound = true;
  63. break;
  64. }
  65. node = node.nextSibling();
  66. }
  67. }
  68. if(dictNodeFound) {
  69. QString key;
  70. QString value;
  71. while(!node.isNull()) {
  72. if(node.isElement()) {
  73. element = node.toElement();
  74. if(element.tagName() == INFO_PLIST_TAG_KEY) {
  75. key = element.text();
  76. node = node.nextSibling();
  77. element = node.toElement();
  78. if(key == INFO_PLIST_KEY_ALLOW_NETWORK_ACCESS || key == INFO_PLIST_KEY_MINI_VIEW_ENABLED) {
  79. value = element.tagName();
  80. }
  81. else {
  82. value = element.text();
  83. }
  84. infoPlistData.insert(key, value);
  85. }
  86. }
  87. node = node.nextSibling();
  88. }
  89. }
  90. else {
  91. QString errMsg = QString(ERR_XML_NODE_NOT_FOUND).arg(INFO_PLIST_TAG_DICT).arg(INFO_PLIST_FILE);
  92. m_outputView.printOutput(errMsg);
  93. return false;
  94. }
  95. QString msg = QString(MSG_FILE_PROCESSING_COMPLETED).arg(INFO_PLIST_FILE);
  96. m_outputView.printOutput(msg);
  97. return true;
  98. }