cmCPackIFWRepository.cxx 6.5 KB

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