cmCPackIFWRepository.cxx 6.6 KB

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