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.

155 lines
3.3KB

  1. /*
  2. Output only format for Vcard format, VCF
  3. Copyright (C) 2005 Robert Lipe, robertlipe+source@gpsbabel.org
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA
  15. */
  16. #include "defs.h"
  17. #include "jeeps/gpsmath.h"
  18. #include <ctype.h>
  19. #include <math.h>
  20. #include <stdlib.h>
  21. static gbfile* file_out;
  22. static short_handle mkshort_handle;
  23. static char* vcf_encrypt = NULL;
  24. #define MYNAME "VCF"
  25. static
  26. arglist_t vcf_args[] = {
  27. {
  28. "encrypt", &vcf_encrypt,
  29. "Encrypt hints using ROT13", NULL, ARGTYPE_BOOL, ARG_NOMINMAX
  30. },
  31. ARG_TERMINATOR
  32. };
  33. static void
  34. wr_init(const QString& fname)
  35. {
  36. file_out = gbfopen(fname, "w", MYNAME);
  37. mkshort_handle = mkshort_new_handle();
  38. }
  39. static void
  40. wr_deinit(void)
  41. {
  42. gbfclose(file_out);
  43. mkshort_del_handle(&mkshort_handle);
  44. }
  45. /*
  46. * Print a possibly empty input string, replacing newlines with escaped
  47. * newlines as we go.
  48. */
  49. static void
  50. vcf_print_utf(const utf_string* s)
  51. {
  52. char* p, *p2, *p3;
  53. char* stripped_html;
  54. if (!s) {
  55. return;
  56. }
  57. stripped_html = strip_html(s);
  58. p = gstrsub(stripped_html, "\n", "\\n");
  59. p2 = gstrsub(p, "<p>", "\\n");
  60. p3 = gstrsub(p2, ";", "\\;");
  61. gbfputs(p3, file_out);
  62. xfree(p);
  63. xfree(p2);
  64. xfree(p3);
  65. xfree(stripped_html);
  66. }
  67. static void
  68. vcf_print(const char* s)
  69. {
  70. char* p;
  71. if (!s) {
  72. return;
  73. }
  74. p = gstrsub(s, "\n", "\\n");
  75. gbfputs(p, file_out);
  76. xfree(p);
  77. }
  78. static void
  79. vcf_print(const QString& s)
  80. {
  81. vcf_print(CSTR(s));
  82. }
  83. static void
  84. vcf_disp(const Waypoint* wpt)
  85. {
  86. int latint, lonint;
  87. lonint = abs((int) wpt->longitude);
  88. latint = abs((int) wpt->latitude);
  89. gbfprintf(file_out, "BEGIN:VCARD\nVERSION:3.0\n");
  90. gbfprintf(file_out, "N:%s;%s;;;\n", CSTRc(wpt->description),CSTRc(wpt->shortname));
  91. gbfprintf(file_out, "ADR:%c%d %06.3f %c%d %06.3f\n", wpt->latitude < 0 ? 'S' : 'N', abs(latint), 60.0 * (fabs(wpt->latitude) - latint), wpt->longitude < 0 ? 'W' : 'E', abs(lonint), 60.0 * (fabs(wpt->longitude) - lonint));
  92. if (wpt->HasUrlLink()) {
  93. UrlLink link = wpt->GetUrlLink();
  94. gbfprintf(file_out, "URL:%s\n", CSTR(link.url_));
  95. }
  96. gbfprintf(file_out, "NOTE:");
  97. vcf_print_utf(&wpt->gc_data->desc_short);
  98. gbfprintf(file_out, "\\n");
  99. vcf_print_utf(&wpt->gc_data->desc_long);
  100. gbfprintf(file_out, "\\n\\nHINT:\\n");
  101. if (vcf_encrypt) {
  102. QString s = rot13(wpt->gc_data->hint);
  103. vcf_print(s);
  104. } else {
  105. vcf_print(CSTR(wpt->gc_data->hint));
  106. }
  107. gbfprintf(file_out, "\nEND:VCARD\n");
  108. }
  109. static void
  110. data_write(void)
  111. {
  112. setshort_length(mkshort_handle, 6);
  113. waypt_disp_all(vcf_disp);
  114. }
  115. ff_vecs_t vcf_vecs = {
  116. ff_type_file,
  117. { ff_cap_write, ff_cap_none, ff_cap_none},
  118. NULL,
  119. wr_init,
  120. NULL,
  121. wr_deinit,
  122. NULL,
  123. data_write,
  124. NULL,
  125. vcf_args,
  126. CET_CHARSET_ASCII, 0 /* CET-REVIEW */
  127. };