You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

navicache.cc 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. Copyright (C) 2003-2013 Robert Lipe, robertlipe+source@gpsbabel.org
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA
  14. */
  15. #include "defs.h"
  16. #include "cet_util.h"
  17. #include <QtCore/QXmlStreamReader>
  18. #include "src/core/file.h"
  19. static char* noretired = NULL;
  20. static QString read_fname;
  21. static
  22. arglist_t nav_args[] = {
  23. {
  24. "noretired", &noretired, "Suppress retired geocaches",
  25. NULL, ARGTYPE_BOOL, ARG_NOMINMAX
  26. },
  27. ARG_TERMINATOR
  28. };
  29. #define MYNAME "navicache"
  30. #define NC_URL "http://www.navicache.com/cgi-bin/db/displaycache2.pl?CacheID="
  31. static struct
  32. nc_type_mapping {
  33. geocache_type type;
  34. const char* name;
  35. } nc_type_map[] = {
  36. { gt_unknown, "unknown" },
  37. { gt_traditional, "normal" },
  38. { gt_multi, "Multi-part" },
  39. { gt_virtual, "Virtual" },
  40. { gt_event, "event" }
  41. };
  42. static struct
  43. nc_container_mapping {
  44. geocache_container type;
  45. const char* name;
  46. } nc_container_map[] = {
  47. { gc_other, "Unknown" },
  48. { gc_micro, "Micro" },
  49. { gc_regular, "Normal" },
  50. { gc_large, "Large" },
  51. { gc_virtual, "Virtual" }
  52. };
  53. static
  54. geocache_type
  55. nc_mktype(const QString& t)
  56. {
  57. int i;
  58. int sz = sizeof(nc_type_map) / sizeof(nc_type_map[0]);
  59. for (i = 0; i < sz; i++) {
  60. if (0 == t.compare(nc_type_map[i].name, Qt::CaseInsensitive)) {
  61. return nc_type_map[i].type;
  62. }
  63. }
  64. return gt_unknown;
  65. }
  66. static
  67. geocache_container
  68. nc_mkcont(const QString& t)
  69. {
  70. int i;
  71. int sz = sizeof(nc_container_map) / sizeof(nc_container_map[0]);
  72. for (i = 0; i < sz; i++) {
  73. if (0 == t.compare(nc_container_map[i].name, Qt::CaseInsensitive)) {
  74. return nc_container_map[i].type;
  75. }
  76. }
  77. return gc_unknown;
  78. }
  79. static void
  80. nav_rd_init(const QString& fname)
  81. {
  82. read_fname = fname;
  83. }
  84. static void
  85. NaviReadCache(const QXmlStreamReader& reader)
  86. {
  87. const QXmlStreamAttributes a = reader.attributes();
  88. Waypoint* wpt_tmp = new Waypoint;
  89. geocache_data* gc_data;
  90. gc_data = wpt_tmp->AllocGCData();
  91. if (a.hasAttribute("cache_id")) {
  92. int n = a.value("cache_id").toString().toInt();
  93. QString fn = QString("N%1").arg(n, 5, 16, QChar('0'));
  94. wpt_tmp->shortname = fn;
  95. UrlLink l(QString(NC_URL) + QString::number(n));
  96. wpt_tmp->AddUrlLink(l);
  97. }
  98. if (a.hasAttribute("name")) {
  99. wpt_tmp->description = a.value("name").toString();
  100. }
  101. if (a.hasAttribute("user_name")) {
  102. gc_data->placer = a.value("user_name").toString();
  103. }
  104. if (a.hasAttribute("latitude")) {
  105. wpt_tmp->latitude = a.value("latitude").toString().toDouble();
  106. }
  107. if (a.hasAttribute("longitude")) {
  108. wpt_tmp->longitude = a.value("longitude").toString().toDouble();
  109. }
  110. if (a.hasAttribute("difficulty")) {
  111. gc_data->diff = a.value("difficulty").toString().toDouble() * 10;
  112. }
  113. if (a.hasAttribute("terrain")) {
  114. gc_data->terr = a.value("terrain").toString().toDouble() * 10;
  115. }
  116. if (a.hasAttribute("cache_type")) {
  117. QString t = a.value("cache_type").toString();
  118. gc_data->type = nc_mktype(t);
  119. if (t == "normal") {
  120. wpt_tmp->icon_descr = "Geocache-regular";
  121. } else if (t == "multi-part") {
  122. wpt_tmp->icon_descr = "Geocache-multi";
  123. } else if (t == "moving_travelling") {
  124. wpt_tmp->icon_descr = "Geocache-moving";
  125. } else {
  126. wpt_tmp->icon_descr = QString("Geocache-%-%1").arg(t);
  127. }
  128. }
  129. if (a.hasAttribute("hidden_date")) {
  130. QString h = a.value("hidden_date").toString();
  131. QDateTime hd = QDateTime::fromString(h, "yyyy-MM-dd");
  132. wpt_tmp->SetCreationTime(hd);
  133. }
  134. if (a.hasAttribute("retired")) {
  135. if (a.value("terrain").toString() == "yes" && noretired) {
  136. delete wpt_tmp;
  137. return;
  138. }
  139. }
  140. if (a.hasAttribute("cache_size")) {
  141. gc_data->container = nc_mkcont(a.value("cache_size").toString());
  142. }
  143. if (a.hasAttribute("description")) {
  144. gc_data->desc_long.is_html = true;
  145. gc_data->desc_long.utfstring = a.value("description").toString();
  146. }
  147. if (a.hasAttribute("comments")) {
  148. gc_data->desc_short.is_html = true;
  149. gc_data->desc_short.utfstring = a.value("comments").toString();
  150. }
  151. waypt_add(wpt_tmp);
  152. }
  153. static void
  154. nav_read(void)
  155. {
  156. QXmlStreamReader reader;
  157. gpsbabel::File file(read_fname);
  158. file.open(QIODevice::ReadOnly);
  159. reader.setDevice(&file);
  160. while (!reader.atEnd()) {
  161. if (reader.tokenType() == QXmlStreamReader::StartElement) {
  162. if (reader.name() == "CacheDetails") {
  163. NaviReadCache(reader);
  164. }
  165. }
  166. reader.readNext();
  167. }
  168. if (reader.hasError()) {
  169. fatal(MYNAME ":Read error: %s (%s, line %ld, col %ld)\n",
  170. qPrintable(reader.errorString()),
  171. qPrintable(file.fileName()),
  172. (long) reader.lineNumber(),
  173. (long) reader.columnNumber());
  174. }
  175. }
  176. static void
  177. nav_rd_deinit(void)
  178. {
  179. }
  180. static void
  181. nav_wr_init(const QString& fname)
  182. {
  183. fatal(MYNAME ": Does not support writing Navicache files.\n");
  184. }
  185. static void
  186. nav_wr_deinit(void)
  187. {
  188. }
  189. static void
  190. nav_write(void)
  191. {
  192. }
  193. ff_vecs_t navicache_vecs = {
  194. ff_type_file,
  195. { ff_cap_read, ff_cap_none, ff_cap_none },
  196. nav_rd_init,
  197. nav_wr_init,
  198. nav_rd_deinit,
  199. nav_wr_deinit,
  200. nav_read,
  201. nav_write,
  202. NULL,
  203. nav_args,
  204. CET_CHARSET_UTF8, 0 /* CET-REVIEW */
  205. };