cmCPackIFWRepository.cxx 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmCPackIFWRepository.h"
  11. #include "cmCPackIFWGenerator.h"
  12. #include <CPack/cmCPackLog.h>
  13. #include <cmGeneratedFileStream.h>
  14. #include <cmXMLParser.h>
  15. #include <cmXMLWriter.h>
  16. #ifdef cmCPackLogger
  17. #undef cmCPackLogger
  18. #endif
  19. #define cmCPackLogger(logType, msg) \
  20. do { \
  21. std::ostringstream cmCPackLog_msg; \
  22. cmCPackLog_msg << msg; \
  23. if (Generator) { \
  24. Generator->Logger->Log(logType, __FILE__, __LINE__, \
  25. cmCPackLog_msg.str().c_str()); \
  26. } \
  27. } while (0)
  28. cmCPackIFWRepository::cmCPackIFWRepository()
  29. : Update(None)
  30. , Generator(0)
  31. {
  32. }
  33. bool cmCPackIFWRepository::IsValid() const
  34. {
  35. bool valid = true;
  36. switch (Update) {
  37. case None:
  38. valid = !Url.empty();
  39. break;
  40. case Add:
  41. valid = !Url.empty();
  42. break;
  43. case Remove:
  44. valid = !Url.empty();
  45. break;
  46. case Replace:
  47. valid = !OldUrl.empty() && !NewUrl.empty();
  48. break;
  49. }
  50. return valid;
  51. }
  52. const char* cmCPackIFWRepository::GetOption(const std::string& op) const
  53. {
  54. return Generator ? Generator->GetOption(op) : 0;
  55. }
  56. bool cmCPackIFWRepository::IsOn(const std::string& op) const
  57. {
  58. return Generator ? Generator->IsOn(op) : false;
  59. }
  60. bool cmCPackIFWRepository::IsVersionLess(const char* version)
  61. {
  62. return Generator ? Generator->IsVersionLess(version) : false;
  63. }
  64. bool cmCPackIFWRepository::IsVersionGreater(const char* version)
  65. {
  66. return Generator ? Generator->IsVersionGreater(version) : false;
  67. }
  68. bool cmCPackIFWRepository::IsVersionEqual(const char* version)
  69. {
  70. return Generator ? Generator->IsVersionEqual(version) : false;
  71. }
  72. bool cmCPackIFWRepository::ConfigureFromOptions()
  73. {
  74. // Name;
  75. if (Name.empty())
  76. return false;
  77. std::string prefix =
  78. "CPACK_IFW_REPOSITORY_" + cmsys::SystemTools::UpperCase(Name) + "_";
  79. // Update
  80. if (IsOn(prefix + "ADD")) {
  81. Update = Add;
  82. } else if (IsOn(prefix + "REMOVE")) {
  83. Update = Remove;
  84. } else if (IsOn(prefix + "REPLACE")) {
  85. Update = Replace;
  86. } else {
  87. Update = None;
  88. }
  89. // Url
  90. if (const char* url = GetOption(prefix + "URL")) {
  91. Url = url;
  92. } else {
  93. Url = "";
  94. }
  95. // Old url
  96. if (const char* oldUrl = GetOption(prefix + "OLD_URL")) {
  97. OldUrl = oldUrl;
  98. } else {
  99. OldUrl = "";
  100. }
  101. // New url
  102. if (const char* newUrl = GetOption(prefix + "NEW_URL")) {
  103. NewUrl = newUrl;
  104. } else {
  105. NewUrl = "";
  106. }
  107. // Enabled
  108. if (IsOn(prefix + "DISABLED")) {
  109. Enabled = "0";
  110. } else {
  111. Enabled = "";
  112. }
  113. // Username
  114. if (const char* username = GetOption(prefix + "USERNAME")) {
  115. Username = username;
  116. } else {
  117. Username = "";
  118. }
  119. // Password
  120. if (const char* password = GetOption(prefix + "PASSWORD")) {
  121. Password = password;
  122. } else {
  123. Password = "";
  124. }
  125. // DisplayName
  126. if (const char* displayName = GetOption(prefix + "DISPLAY_NAME")) {
  127. DisplayName = displayName;
  128. } else {
  129. DisplayName = "";
  130. }
  131. return IsValid();
  132. }
  133. /** \class cmCPackeIFWUpdatesPatcher
  134. * \brief Helper class that parses and patch Updates.xml file (QtIFW)
  135. */
  136. class cmCPackeIFWUpdatesPatcher : public cmXMLParser
  137. {
  138. public:
  139. cmCPackeIFWUpdatesPatcher(cmCPackIFWRepository* r, cmXMLWriter& x)
  140. : repository(r)
  141. , xout(x)
  142. , patched(false)
  143. {
  144. }
  145. cmCPackIFWRepository* repository;
  146. cmXMLWriter& xout;
  147. bool patched;
  148. protected:
  149. virtual void StartElement(const std::string& name, const char** atts)
  150. {
  151. xout.StartElement(name);
  152. StartFragment(atts);
  153. }
  154. void StartFragment(const char** atts)
  155. {
  156. for (size_t i = 0; atts[i]; i += 2) {
  157. const char* key = atts[i];
  158. const char* value = atts[i + 1];
  159. xout.Attribute(key, value);
  160. }
  161. }
  162. virtual void EndElement(const std::string& name)
  163. {
  164. if (name == "Updates" && !patched) {
  165. repository->WriteRepositoryUpdates(xout);
  166. patched = true;
  167. }
  168. xout.EndElement();
  169. if (patched)
  170. return;
  171. if (name == "Checksum") {
  172. repository->WriteRepositoryUpdates(xout);
  173. patched = true;
  174. }
  175. }
  176. virtual void CharacterDataHandler(const char* data, int length)
  177. {
  178. std::string content(data, data + length);
  179. if (content == "" || content == " " || content == " " || content == "\n")
  180. return;
  181. xout.Content(content);
  182. }
  183. };
  184. bool cmCPackIFWRepository::PatchUpdatesXml()
  185. {
  186. // Lazy directory initialization
  187. if (Directory.empty() && Generator) {
  188. Directory = Generator->toplevel;
  189. }
  190. // Filenames
  191. std::string updatesXml = Directory + "/repository/Updates.xml";
  192. std::string updatesPatchXml = Directory + "/repository/UpdatesPatch.xml";
  193. // Output stream
  194. cmGeneratedFileStream fout(updatesPatchXml.data());
  195. cmXMLWriter xout(fout);
  196. xout.StartDocument();
  197. WriteGeneratedByToStrim(xout);
  198. // Patch
  199. {
  200. cmCPackeIFWUpdatesPatcher patcher(this, xout);
  201. patcher.ParseFile(updatesXml.data());
  202. }
  203. xout.EndDocument();
  204. fout.Close();
  205. return cmSystemTools::RenameFile(updatesPatchXml.data(), updatesXml.data());
  206. }
  207. void cmCPackIFWRepository::WriteRepositoryConfig(cmXMLWriter& xout)
  208. {
  209. xout.StartElement("Repository");
  210. // Url
  211. xout.Element("Url", Url);
  212. // Enabled
  213. if (!Enabled.empty()) {
  214. xout.Element("Enabled", Enabled);
  215. }
  216. // Username
  217. if (!Username.empty()) {
  218. xout.Element("Username", Username);
  219. }
  220. // Password
  221. if (!Password.empty()) {
  222. xout.Element("Password", Password);
  223. }
  224. // DisplayName
  225. if (!DisplayName.empty()) {
  226. xout.Element("DisplayName", DisplayName);
  227. }
  228. xout.EndElement();
  229. }
  230. void cmCPackIFWRepository::WriteRepositoryUpdate(cmXMLWriter& xout)
  231. {
  232. xout.StartElement("Repository");
  233. switch (Update) {
  234. case None:
  235. break;
  236. case Add:
  237. xout.Attribute("action", "add");
  238. break;
  239. case Remove:
  240. xout.Attribute("action", "remove");
  241. break;
  242. case Replace:
  243. xout.Attribute("action", "replace");
  244. break;
  245. }
  246. // Url
  247. if (Update == Add || Update == Remove) {
  248. xout.Attribute("url", Url);
  249. } else if (Update == Replace) {
  250. xout.Attribute("oldurl", OldUrl);
  251. xout.Attribute("newurl", NewUrl);
  252. }
  253. // Enabled
  254. if (!Enabled.empty()) {
  255. xout.Attribute("enabled", Enabled);
  256. }
  257. // Username
  258. if (!Username.empty()) {
  259. xout.Attribute("username", Username);
  260. }
  261. // Password
  262. if (!Password.empty()) {
  263. xout.Attribute("password", Password);
  264. }
  265. // DisplayName
  266. if (!DisplayName.empty()) {
  267. xout.Attribute("displayname", DisplayName);
  268. }
  269. xout.EndElement();
  270. }
  271. void cmCPackIFWRepository::WriteRepositoryUpdates(cmXMLWriter& xout)
  272. {
  273. if (!RepositoryUpdate.empty()) {
  274. xout.StartElement("RepositoryUpdate");
  275. for (RepositoriesVector::iterator rit = RepositoryUpdate.begin();
  276. rit != RepositoryUpdate.end(); ++rit) {
  277. (*rit)->WriteRepositoryUpdate(xout);
  278. }
  279. xout.EndElement();
  280. }
  281. }
  282. void cmCPackIFWRepository::WriteGeneratedByToStrim(cmXMLWriter& xout)
  283. {
  284. if (Generator)
  285. Generator->WriteGeneratedByToStrim(xout);
  286. }