cmCPackIFWRepository.cxx 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmCPackIFWRepository.h"
  4. #include <cstddef>
  5. #include "cmCPackIFWGenerator.h"
  6. #include "cmGeneratedFileStream.h"
  7. #include "cmSystemTools.h"
  8. #include "cmXMLParser.h"
  9. #include "cmXMLWriter.h"
  10. cmCPackIFWRepository::cmCPackIFWRepository()
  11. : Update(cmCPackIFWRepository::None)
  12. {
  13. }
  14. bool cmCPackIFWRepository::IsValid() const
  15. {
  16. bool valid = true;
  17. switch (this->Update) {
  18. case cmCPackIFWRepository::None:
  19. case cmCPackIFWRepository::Add:
  20. case cmCPackIFWRepository::Remove:
  21. valid = !this->Url.empty();
  22. break;
  23. case cmCPackIFWRepository::Replace:
  24. valid = !this->OldUrl.empty() && !this->NewUrl.empty();
  25. break;
  26. }
  27. return valid;
  28. }
  29. bool cmCPackIFWRepository::ConfigureFromOptions()
  30. {
  31. // Name;
  32. if (this->Name.empty()) {
  33. return false;
  34. }
  35. std::string prefix =
  36. "CPACK_IFW_REPOSITORY_" + cmsys::SystemTools::UpperCase(this->Name) + "_";
  37. // Update
  38. if (this->IsOn(prefix + "ADD")) {
  39. this->Update = cmCPackIFWRepository::Add;
  40. } else if (this->IsOn(prefix + "REMOVE")) {
  41. this->Update = cmCPackIFWRepository::Remove;
  42. } else if (this->IsOn(prefix + "REPLACE")) {
  43. this->Update = cmCPackIFWRepository::Replace;
  44. } else {
  45. this->Update = cmCPackIFWRepository::None;
  46. }
  47. // Url
  48. if (const char* url = this->GetOption(prefix + "URL")) {
  49. this->Url = url;
  50. } else {
  51. this->Url.clear();
  52. }
  53. // Old url
  54. if (const char* oldUrl = this->GetOption(prefix + "OLD_URL")) {
  55. this->OldUrl = oldUrl;
  56. } else {
  57. this->OldUrl.clear();
  58. }
  59. // New url
  60. if (const char* newUrl = this->GetOption(prefix + "NEW_URL")) {
  61. this->NewUrl = newUrl;
  62. } else {
  63. this->NewUrl.clear();
  64. }
  65. // Enabled
  66. if (this->IsOn(prefix + "DISABLED")) {
  67. this->Enabled = "0";
  68. } else {
  69. this->Enabled.clear();
  70. }
  71. // Username
  72. if (const char* username = this->GetOption(prefix + "USERNAME")) {
  73. this->Username = username;
  74. } else {
  75. this->Username.clear();
  76. }
  77. // Password
  78. if (const char* password = this->GetOption(prefix + "PASSWORD")) {
  79. this->Password = password;
  80. } else {
  81. this->Password.clear();
  82. }
  83. // DisplayName
  84. if (const char* displayName = this->GetOption(prefix + "DISPLAY_NAME")) {
  85. this->DisplayName = displayName;
  86. } else {
  87. this->DisplayName.clear();
  88. }
  89. return this->IsValid();
  90. }
  91. /** \class cmCPackeIFWUpdatesPatcher
  92. * \brief Helper class that parses and patch Updates.xml file (QtIFW)
  93. */
  94. class cmCPackeIFWUpdatesPatcher : public cmXMLParser
  95. {
  96. public:
  97. cmCPackeIFWUpdatesPatcher(cmCPackIFWRepository* r, cmXMLWriter& x)
  98. : repository(r)
  99. , xout(x)
  100. , patched(false)
  101. {
  102. }
  103. cmCPackIFWRepository* repository;
  104. cmXMLWriter& xout;
  105. bool patched;
  106. protected:
  107. void StartElement(const std::string& name, const char** atts) override
  108. {
  109. this->xout.StartElement(name);
  110. this->StartFragment(atts);
  111. }
  112. void StartFragment(const char** atts)
  113. {
  114. for (size_t i = 0; atts[i]; i += 2) {
  115. const char* key = atts[i];
  116. const char* value = atts[i + 1];
  117. this->xout.Attribute(key, value);
  118. }
  119. }
  120. void EndElement(const std::string& name) override
  121. {
  122. if (name == "Updates" && !this->patched) {
  123. this->repository->WriteRepositoryUpdates(this->xout);
  124. this->patched = true;
  125. }
  126. this->xout.EndElement();
  127. if (this->patched) {
  128. return;
  129. }
  130. if (name == "Checksum") {
  131. this->repository->WriteRepositoryUpdates(this->xout);
  132. this->patched = true;
  133. }
  134. }
  135. void CharacterDataHandler(const char* data, int length) override
  136. {
  137. std::string content(data, data + length);
  138. if (content.empty() || content == " " || content == " " ||
  139. content == "\n") {
  140. return;
  141. }
  142. this->xout.Content(content);
  143. }
  144. };
  145. bool cmCPackIFWRepository::PatchUpdatesXml()
  146. {
  147. // Lazy directory initialization
  148. if (this->Directory.empty() && this->Generator) {
  149. this->Directory = this->Generator->toplevel;
  150. }
  151. // Filenames
  152. std::string updatesXml = this->Directory + "/repository/Updates.xml";
  153. std::string updatesPatchXml =
  154. this->Directory + "/repository/UpdatesPatch.xml";
  155. // Output stream
  156. cmGeneratedFileStream fout(updatesPatchXml);
  157. cmXMLWriter xout(fout);
  158. xout.StartDocument();
  159. this->WriteGeneratedByToStrim(xout);
  160. // Patch
  161. {
  162. cmCPackeIFWUpdatesPatcher patcher(this, xout);
  163. patcher.ParseFile(updatesXml.data());
  164. }
  165. xout.EndDocument();
  166. fout.Close();
  167. return cmSystemTools::RenameFile(updatesPatchXml, updatesXml);
  168. }
  169. void cmCPackIFWRepository::WriteRepositoryConfig(cmXMLWriter& xout)
  170. {
  171. xout.StartElement("Repository");
  172. // Url
  173. xout.Element("Url", this->Url);
  174. // Enabled
  175. if (!this->Enabled.empty()) {
  176. xout.Element("Enabled", this->Enabled);
  177. }
  178. // Username
  179. if (!this->Username.empty()) {
  180. xout.Element("Username", this->Username);
  181. }
  182. // Password
  183. if (!this->Password.empty()) {
  184. xout.Element("Password", this->Password);
  185. }
  186. // DisplayName
  187. if (!this->DisplayName.empty()) {
  188. xout.Element("DisplayName", this->DisplayName);
  189. }
  190. xout.EndElement();
  191. }
  192. void cmCPackIFWRepository::WriteRepositoryUpdate(cmXMLWriter& xout)
  193. {
  194. xout.StartElement("Repository");
  195. switch (this->Update) {
  196. case cmCPackIFWRepository::None:
  197. break;
  198. case cmCPackIFWRepository::Add:
  199. xout.Attribute("action", "add");
  200. break;
  201. case cmCPackIFWRepository::Remove:
  202. xout.Attribute("action", "remove");
  203. break;
  204. case cmCPackIFWRepository::Replace:
  205. xout.Attribute("action", "replace");
  206. break;
  207. }
  208. // Url
  209. if (this->Update == cmCPackIFWRepository::Add ||
  210. this->Update == cmCPackIFWRepository::Remove) {
  211. xout.Attribute("url", this->Url);
  212. } else if (this->Update == cmCPackIFWRepository::Replace) {
  213. xout.Attribute("oldUrl", this->OldUrl);
  214. xout.Attribute("newUrl", this->NewUrl);
  215. }
  216. // Enabled
  217. if (!this->Enabled.empty()) {
  218. xout.Attribute("enabled", this->Enabled);
  219. }
  220. // Username
  221. if (!this->Username.empty()) {
  222. xout.Attribute("username", this->Username);
  223. }
  224. // Password
  225. if (!this->Password.empty()) {
  226. xout.Attribute("password", this->Password);
  227. }
  228. // DisplayName
  229. if (!this->DisplayName.empty()) {
  230. xout.Attribute("displayname", this->DisplayName);
  231. }
  232. xout.EndElement();
  233. }
  234. void cmCPackIFWRepository::WriteRepositoryUpdates(cmXMLWriter& xout)
  235. {
  236. if (!this->RepositoryUpdate.empty()) {
  237. xout.StartElement("RepositoryUpdate");
  238. for (cmCPackIFWRepository* r : this->RepositoryUpdate) {
  239. r->WriteRepositoryUpdate(xout);
  240. }
  241. xout.EndElement();
  242. }
  243. }