Contents of /trunk/src/notes.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 299 - (show annotations)
Fri Aug 27 12:04:34 2010 UTC (13 years, 8 months ago) by harbaum
File MIME type: text/plain
File size: 30182 byte(s)
Fixed [#6018]
1 /*
2 * Copyright (C) 2008 Till Harbaum <till@harbaum.org>.
3 *
4 * This file is part of GPXView.
5 *
6 * GPXView is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * GPXView is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with GPXView. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include <stdio.h>
21 #include <string.h>
22 #include <errno.h>
23
24 #include <glib/gunicode.h>
25
26 #include <libxml/parser.h>
27 #include <libxml/tree.h>
28
29 #include <math.h>
30
31 #if defined(USE_MAEMO) && (MAEMO_VERSION_MAJOR >= 5)
32 #include <hildon/hildon-note.h>
33 #include <hildon/hildon-entry.h>
34 #include <hildon/hildon-check-button.h>
35 #endif
36
37 #if !defined(LIBXML_TREE_ENABLED) || !defined(LIBXML_OUTPUT_ENABLED)
38 #error "libxml doesn't support required tree or output"
39 #endif
40
41 #include "gpxview.h"
42
43 #if defined(USE_MAEMO) && (MAEMO_VERSION_MAJOR >= 5)
44 #include <hildon/hildon-note.h>
45 #endif
46
47 void gtk_text_buffer_set_can_paste_rich_text(GtkTextBuffer *buffer, gboolean);
48 void gtk_text_buffer_set_rich_text_format(GtkTextBuffer *buffer, const gchar *);
49
50 #define TAG_STATE GTK_STATE_PRELIGHT
51
52 static notes_t *notes_load(appdata_t *appdata, cache_t *cache) {
53 notes_t *notes = NULL;
54 xmlDoc *doc = NULL;
55 xmlNode *root_element = NULL;
56
57 LIBXML_TEST_VERSION;
58
59 /* build local path */
60 int path_len = strlen(appdata->image_path) + 2 * strlen(cache->id) + 6;
61 char *path = malloc(path_len);
62 snprintf(path, path_len, "%s%s/%s.gpx",
63 appdata->image_path, cache->id, cache->id);
64
65 /* no such file? */
66 if(!g_file_test(path, G_FILE_TEST_EXISTS)) {
67 free(path);
68 return NULL;
69 }
70
71 /* parse the file and get the DOM */
72 doc = xmlReadFile(path, NULL, 0);
73
74 if(doc == NULL) {
75 printf("error: could not parse file %s\n", path);
76 free(path);
77 return NULL;
78 }
79
80 /* Get the root element node */
81 root_element = xmlDocGetRootElement(doc);
82
83 xmlNode *cur_node = NULL;
84 for (cur_node = root_element; cur_node; cur_node = cur_node->next) {
85 if (cur_node->type == XML_ELEMENT_NODE) {
86 if(strcasecmp((char*)cur_node->name, "gpx") == 0) {
87 xmlNode *wpt_node = cur_node->children;
88
89 while(wpt_node != NULL) {
90 if(wpt_node->type == XML_ELEMENT_NODE) {
91 if(strcasecmp((char*)wpt_node->name, "wpt") == 0) {
92
93 notes = malloc(sizeof(notes_t));
94 memset(notes, 0, sizeof(notes_t));
95 notes->pos = gpx_cache_pos(cache);
96
97 char *str;
98 if((str = (char*)xmlGetProp(wpt_node, BAD_CAST "override"))) {
99 notes->override = (strcasecmp(str, "yes") == 0);
100 xmlFree(str);
101 }
102
103 if((str = (char*)xmlGetProp(wpt_node, BAD_CAST "found"))) {
104 /* check if there's a valid number -> found time */
105 if(strtoul(str, NULL, 10) != 0) {
106 notes->found = TRUE;
107 notes->ftime = strtoul(str, NULL, 10);
108 } else {
109 notes->found = (strcasecmp(str, "yes") == 0);
110 notes->ftime = 0;
111 }
112 xmlFree(str);
113 }
114
115 if((str = (char*)xmlGetProp(wpt_node, BAD_CAST "logged"))) {
116 notes->logged = (strcasecmp(str, "yes") == 0);
117 xmlFree(str);
118 }
119
120 if((str = (char*)xmlGetProp(wpt_node, BAD_CAST "lat"))) {
121 notes->pos.lat = g_ascii_strtod(str, NULL);
122 xmlFree(str);
123 }
124
125 if((str = (char*)xmlGetProp(wpt_node, BAD_CAST "lon"))) {
126 notes->pos.lon = g_ascii_strtod(str, NULL);
127 xmlFree(str);
128 }
129
130 xmlNode *sub_node = wpt_node->children;
131
132 while (sub_node != NULL) {
133 if (sub_node->type == XML_ELEMENT_NODE) {
134 if(strcasecmp((char*)sub_node->name, "desc") == 0)
135 notes->text = (char*)
136 xmlNodeListGetString(doc, sub_node->children, 1);
137 }
138 sub_node = sub_node->next;
139 }
140 }
141 }
142 wpt_node = wpt_node->next;
143 }
144 }
145 }
146 }
147
148 xmlFreeDoc(doc);
149 xmlCleanupParser();
150
151 printf("got notes for cache %s\n", cache->id);
152
153 free(path);
154 return notes;
155 }
156
157 void notes_load_all(appdata_t *appdata, gpx_t *gpx) {
158 printf("Load all notes for %s\n", gpx->name);
159
160 cache_t *cache = gpx->cache;
161 while(cache) {
162 /* a note may actually already be there if this gpx file */
163 /* is e.g. assembled from search results */
164 if(!cache->notes)
165 cache->notes = notes_load(appdata, cache);
166
167 cache = cache->next;
168 }
169 }
170
171 static int notes_write_file(cache_context_t *context,
172 char *text, pos_t pos,
173 gboolean override, gboolean found,
174 time_t ftime, gboolean logged) {
175
176 g_assert(context);
177 g_assert(context->cache);
178
179 /* build local path */
180 int path_len = strlen(context->appdata->image_path) +
181 2 * strlen(context->cache->id) + 6;
182
183 char *path = malloc(path_len);
184 snprintf(path, path_len, "%s%s/%s.gpx",
185 context->appdata->image_path,
186 context->cache->id, context->cache->id);
187
188 if(checkdir(path) != 0) {
189 printf("unable to create notes path\n");
190 free(path);
191 return -1;
192 }
193
194 LIBXML_TEST_VERSION;
195
196 xmlDocPtr doc = xmlNewDoc(BAD_CAST "1.0");
197 xmlNodePtr root_node = xmlNewNode(NULL, BAD_CAST "gpx");
198 xmlDocSetRootElement(doc, root_node);
199
200 xmlNodePtr wpt_node = xmlNewChild(root_node, NULL, BAD_CAST "wpt", NULL);
201 /* make sure no invalid position gets saved */
202 if(!isnan(pos.lat) && !isnan(pos.lon)) {
203 if(override)
204 xmlNewProp(wpt_node, BAD_CAST "override", BAD_CAST "yes");
205 else
206 xmlNewProp(wpt_node, BAD_CAST "override", BAD_CAST "no");
207
208 if(logged)
209 xmlNewProp(wpt_node, BAD_CAST "logged", BAD_CAST "yes");
210 else
211 xmlNewProp(wpt_node, BAD_CAST "logged", BAD_CAST "no");
212
213 if(found) {
214 if(ftime) {
215 char str[32];
216 snprintf(str, sizeof(str), "%lu", ftime);
217 xmlNewProp(wpt_node, BAD_CAST "found", BAD_CAST str);
218 } else
219 xmlNewProp(wpt_node, BAD_CAST "found", BAD_CAST "yes");
220 } else
221 xmlNewProp(wpt_node, BAD_CAST "found", BAD_CAST "no");
222
223 char str[32];
224 g_ascii_dtostr(str, sizeof(str), pos.lat);
225 xmlNewProp(wpt_node, BAD_CAST "lat", BAD_CAST str);
226 g_ascii_dtostr(str, sizeof(str), pos.lon);
227 xmlNewProp(wpt_node, BAD_CAST "lon", BAD_CAST str);
228 }
229
230 int len = strlen(context->cache->id) + strlen(" - ") +
231 strlen(context->cache->name) + 1;
232 char *name = malloc(len);
233 snprintf(name, len, "%s - %s", context->cache->id, context->cache->name);
234 xmlNewChild(wpt_node, NULL, BAD_CAST "name", BAD_CAST name);
235 free(name);
236 xmlNewChild(wpt_node, NULL, BAD_CAST "sym", BAD_CAST "Pin, Blue");
237 xmlNewChild(wpt_node, NULL, BAD_CAST "desc", BAD_CAST text);
238
239 /* write everything and free it */
240 printf("writing %s\n", path);
241 xmlSaveFormatFileEnc(path, doc, "UTF-8", 1);
242 xmlFreeDoc(doc);
243 xmlCleanupParser();
244 free(path);
245
246 return 0;
247 }
248
249 static void notes_save(cache_context_t *context) {
250 /* only save if: there is a text which has been changed or */
251 /* there is a position which differs from the original one */
252 /* or has been changed */
253
254 if(context->notes.modified) {
255 #ifdef USE_STACKABLE_WINDOW
256 context->notes_have_been_changed = TRUE;
257 #endif
258
259 printf("something has been modified, saving notes\n");
260
261 GtkTextIter start;
262 GtkTextIter end;
263 gtk_text_buffer_get_start_iter(context->notes.buffer, &start);
264 gtk_text_buffer_get_end_iter(context->notes.buffer, &end);
265 char *text = gtk_text_buffer_get_text(context->notes.buffer,
266 &start, &end, FALSE);
267
268 pos_t pos;
269 pos.lat = lat_entry_get(context->notes.latw);
270 pos.lon = lon_entry_get(context->notes.lonw);
271
272 gboolean override =
273 check_button_get_active(context->notes.overridew);
274 gboolean found =
275 check_button_get_active(context->notes.foundw);
276 gboolean logged =
277 check_button_get_active(context->notes.loggedw);
278
279 if(pos_differ(&pos, &context->cache->pos))
280 printf("position is modified\n");
281 if(override || found)
282 printf("flags are set\n");
283 if(strlen(text))
284 printf("text is present\n");
285
286 /* check if the notes are empty */
287 if(!pos_differ(&pos, &context->cache->pos) &&
288 !override && !found && !logged && (strlen(text) == 0)) {
289 printf("notes are in default state, removing them if present\n");
290
291 /* remove note */
292 int path_len = strlen(context->appdata->image_path) +
293 2 * strlen(context->cache->id) + 6;
294 char *path = malloc(path_len);
295 snprintf(path, path_len, "%s%s/%s.gpx",
296 context->appdata->image_path,
297 context->cache->id, context->cache->id);
298
299 printf("removing note %s\n", path);
300 remove(path);
301 free(path);
302
303 /* search for matching caches and replace note there */
304 gpx_t *gpx = context->appdata->gpx;
305 while(gpx) {
306 cache_t *cache = gpx->cache;
307 while(cache) {
308 if(strcmp(cache->id, context->cache->id)==0) {
309 if(cache->notes) {
310 notes_free(cache->notes);
311 cache->notes = NULL;
312 }
313 }
314 cache = cache->next;
315 }
316 gpx = gpx->next;
317 }
318
319 #ifdef USE_MAEMO
320 /* update search results if present */
321 if(context->appdata->search_results) {
322 printf("Updating search results\n");
323
324 /* add note to all matching search results */
325 cache_t *cache = context->appdata->search_results->cache;
326 while(cache) {
327 if(strcmp(cache->id, context->cache->id)==0)
328 cache->notes = NULL;
329
330 cache = cache->next;
331 }
332 }
333 #endif
334
335
336 } else {
337 /* we have to do two things here: */
338 /* - update the notes.xml file on disk */
339 /* - update the notes entry in the loaded gpx tree */
340
341 /* update file on disk */
342 notes_write_file(context, text, pos, override, found,
343 context->notes.ftime, logged);
344
345 /* search for matching caches and replace note there */
346 notes_t *note = NULL;
347 gpx_t *gpx = context->appdata->gpx;
348 while(gpx) {
349 cache_t *cache = gpx->cache;
350 while(cache) {
351 if(strcmp(cache->id, context->cache->id)==0) {
352 // printf("found %s in %s\n", cache->id, gpx->name);
353
354 if(cache->notes)
355 notes_free(cache->notes);
356
357 /* create a new note for this cache */
358 cache->notes = note = malloc(sizeof(notes_t));
359 memset(cache->notes, 0, sizeof(notes_t));
360 cache->notes->text = strdup(text);
361 cache->notes->pos = pos;
362 cache->notes->override = override;
363 cache->notes->found = found;
364 cache->notes->logged = logged;
365 cache->notes->ftime = context->notes.ftime;
366 }
367 cache = cache->next;
368 }
369 gpx = gpx->next;
370 }
371
372 #ifdef USE_MAEMO
373 /* update search results if present */
374 if(context->appdata->search_results) {
375 printf("Updating search results\n");
376
377 /* add note to all matching search results */
378 cache_t *cache = context->appdata->search_results->cache;
379 while(cache) {
380 if(strcmp(cache->id, context->cache->id)==0)
381 cache->notes = note;
382
383 cache = cache->next;
384 }
385 }
386 #endif
387 }
388
389 if(text) free(text);
390 }
391 }
392
393 /* this is called from the destroy event of the entire notebook */
394 gint notes_destroy_event(GtkWidget *widget, gpointer data ) {
395 cache_context_t *context = (cache_context_t*)data;
396
397 printf("about to destroy notes view\n");
398 notes_save(context);
399
400 return FALSE;
401 }
402
403 #ifndef NO_COPY_N_PASTE
404 static void on_destroy_textview(GtkWidget *widget, gpointer data) {
405 appdata_t *appdata = (appdata_t*)data;
406
407 printf("destroying notes textview\n");
408
409 /* only do this if main windows hasn't already been destroyed */
410 if(!appdata->window) {
411 printf("destroy notes textview: main window is gone\n");
412 return;
413 }
414
415 if(!appdata->active_buffer)
416 printf("There is no active buffer!\n");
417 else {
418 if(appdata->active_buffer ==
419 gtk_text_view_get_buffer(GTK_TEXT_VIEW(widget))) {
420 printf("This was the active buffer\n");
421
422 appdata->active_buffer = NULL;
423
424 gtk_widget_set_sensitive(appdata->menu_cut, FALSE);
425 gtk_widget_set_sensitive(appdata->menu_copy, FALSE);
426 gtk_widget_set_sensitive(appdata->menu_paste, FALSE);
427 }
428 }
429 }
430 #endif
431
432 static void ftime_update(GtkWidget *widget, cache_context_t *context,
433 gboolean update) {
434 /* check if it has been selected */
435 if(check_button_get_active(widget)) {
436 printf("set active\n");
437
438 if(update)
439 context->notes.ftime = time(NULL);
440
441 if(context->notes.ftime) {
442 struct tm *loctime = localtime(&context->notes.ftime);
443 char str[32];
444 strftime(str, sizeof(str), "%x %X", loctime);
445
446 gtk_label_set_text(GTK_LABEL(context->notes.datew), str);
447 } else
448 gtk_label_set_text(GTK_LABEL(context->notes.datew), "");
449
450 } else {
451 printf("invalidating time\n");
452 context->notes.ftime = 0;
453 gtk_label_set_text(GTK_LABEL(context->notes.datew), "");
454 }
455 }
456
457 /* buffer edited */
458 static void callback_modified(GtkWidget *widget, gpointer data ) {
459 cache_context_t *context = (cache_context_t*)data;
460 // printf("something has been edited\n");
461 context->notes.modified = TRUE;
462
463 if(widget == context->notes.foundw) {
464 printf("was foundw\n");
465
466 /* about to remove "found" flag -> ask for confirmation */
467 if(!check_button_get_active(widget)) {
468 #if !defined(USE_MAEMO) || (MAEMO_VERSION_MAJOR < 5)
469 GtkWidget *dialog = gtk_message_dialog_new(
470 GTK_WINDOW(context->appdata->window),
471 GTK_DIALOG_DESTROY_WITH_PARENT,
472 GTK_MESSAGE_QUESTION, GTK_BUTTONS_YES_NO,
473 _("Do you really want to remove the \"found\" flag? "
474 "This will void the recorded date of your find!"));
475
476 gtk_window_set_title(GTK_WINDOW(dialog), _("Reset \"found\" flag?"));
477
478 /* set the active flag again if the user answered "no" */
479 if(GTK_RESPONSE_NO == gtk_dialog_run(GTK_DIALOG(dialog)))
480 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widget), TRUE);
481
482 #else
483 GtkWidget *dialog =
484 hildon_note_new_confirmation(GTK_WINDOW(context->appdata->window),
485 _("Do you really want to remove the \"found\" flag? "
486 "This will void the recorded date of your find!"));
487
488 /* set the active flag again if the user answered "no" */
489 if(GTK_RESPONSE_OK != gtk_dialog_run(GTK_DIALOG(dialog)))
490 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widget), TRUE);
491 #endif
492
493 gtk_widget_destroy(dialog);
494 }
495
496 ftime_update(widget, context, TRUE);
497 }
498
499 if(widget == context->notes.loggedw) {
500 printf("was loggedw\n");
501
502 /* about to remove "found" flag -> ask for confirmation */
503 if(!check_button_get_active(widget)) {
504 #if !defined(USE_MAEMO) || (MAEMO_VERSION_MAJOR < 5)
505 GtkWidget *dialog = gtk_message_dialog_new(
506 GTK_WINDOW(context->appdata->window),
507 GTK_DIALOG_DESTROY_WITH_PARENT,
508 GTK_MESSAGE_QUESTION, GTK_BUTTONS_YES_NO,
509 _("Do you really want to remove the \"logged\" flag? "
510 "This may cause problems on your next Garmin Field "
511 "Notes upload!"));
512
513 gtk_window_set_title(GTK_WINDOW(dialog), _("Reset \"logged\" flag?"));
514
515 /* set the active flag again if the user answered "no" */
516 if(GTK_RESPONSE_NO == gtk_dialog_run(GTK_DIALOG(dialog)))
517 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widget), TRUE);
518 else {
519 gtk_widget_set_sensitive(widget, FALSE);
520 gtk_widget_set_sensitive(context->notes.foundw, TRUE);
521 }
522
523 #else
524 GtkWidget *dialog =
525 hildon_note_new_confirmation(GTK_WINDOW(context->appdata->window),
526 _("Do you really want to remove the \"logged\" flag? "
527 "This may cause problems on your next Garmin Field "
528 "Notes upload!"));
529
530 /* set the active flag again if the user answered "no" */
531 if(GTK_RESPONSE_OK != gtk_dialog_run(GTK_DIALOG(dialog)))
532 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widget), TRUE);
533 else {
534 gtk_widget_set_sensitive(widget, FALSE);
535 gtk_widget_set_sensitive(context->notes.foundw, TRUE);
536 }
537
538 #endif
539
540 gtk_widget_destroy(dialog);
541 }
542 }
543 }
544
545 #ifndef NO_COPY_N_PASTE
546 static gboolean focus_in(GtkWidget *widget, GdkEventFocus *event,
547 gpointer data) {
548 appdata_t *appdata = (appdata_t*)data;
549
550 printf("note focus in!\n");
551
552 /* these buffers are read/write, thus all items are enabled */
553 gtk_widget_set_sensitive(appdata->menu_cut, TRUE);
554 gtk_widget_set_sensitive(appdata->menu_copy, TRUE);
555 gtk_widget_set_sensitive(appdata->menu_paste, TRUE);
556
557 if(GTK_WIDGET_TYPE(widget) == GTK_TYPE_TEXT_VIEW) {
558 appdata->active_buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(widget));
559 } else
560 printf("not a text view\n");
561
562 return FALSE;
563 }
564 #endif
565
566 static gboolean focus_out(GtkWidget *widget, GdkEventFocus *event,
567 gpointer data) {
568 cache_context_t *context = (cache_context_t*)data;
569
570 notes_save(context);
571 #if !defined(USE_MAEMO) && defined(ENABLE_OSM_GPS_MAP)
572 map_update(context->appdata);
573 #endif
574
575 return FALSE;
576 }
577
578 GtkWidget *cache_notes(cache_context_t *context) {
579 cache_t *cache = context->cache;
580
581 context->notes.modified = FALSE;
582
583 if(context->cache->notes)
584 context->notes.ftime = context->cache->notes->ftime;
585 else
586 context->notes.ftime = 0;
587
588 GtkWidget *vbox = gtk_vbox_new(FALSE, 2);
589
590 /* -------------- custom coordinate ---------------- */
591
592 GtkWidget *table = gtk_table_new(2, 4, FALSE);
593 #if !defined(USE_MAEMO) || (MAEMO_VERSION_MAJOR < 5)
594 gtk_table_set_col_spacing(GTK_TABLE(table), 0, 16);
595 #endif
596
597 gtk_table_set_col_spacing(GTK_TABLE(table), 2, 16);
598
599 gtk_table_attach_defaults(GTK_TABLE(table),
600 gtk_label_new(_("New coordinate:")), 0, 1, 0, 1);
601 context->notes.overridew = check_button_new_with_label(_("Override"));
602 check_button_set_active(context->notes.overridew,
603 cache->notes && cache->notes->override);
604 gtk_table_attach_defaults(GTK_TABLE(table),
605 context->notes.overridew, 2, 3, 0, 1);
606
607 GtkWidget *hbox = gtk_hbox_new(FALSE, 2);
608
609 context->notes.foundw = check_button_new_with_label(_("Found"));
610 check_button_set_active(context->notes.foundw,
611 cache->notes && cache->notes->found);
612 gtk_box_pack_start_defaults(GTK_BOX(hbox), context->notes.foundw);
613
614 context->notes.loggedw = check_button_new_with_label(_("Logged"));
615 check_button_set_active(context->notes.loggedw,
616 cache->notes && cache->notes->logged);
617 gtk_box_pack_start_defaults(GTK_BOX(hbox), context->notes.loggedw);
618
619 gtk_table_attach_defaults(GTK_TABLE(table), hbox, 3, 4, 0, 1);
620
621 /* only found and logged caches have a log flag the user can change */
622 if(!(cache->notes && cache->notes->found && cache->notes->logged))
623 gtk_widget_set_sensitive(context->notes.loggedw, FALSE);
624 else
625 gtk_widget_set_sensitive(context->notes.foundw, FALSE);
626
627 context->notes.datew = gtk_label_new("");
628 gtk_misc_set_alignment(GTK_MISC(context->notes.datew), 0.5f, 0.5f);
629 gtk_table_attach_defaults(GTK_TABLE(table),
630 context->notes.datew, 3, 4, 1, 2);
631 ftime_update(context->notes.foundw, context, FALSE);
632
633 pos_t pos = gpx_cache_pos(cache);
634 if(cache->notes) pos = cache->notes->pos;
635
636 context->notes.latw = lat_entry_new(pos.lat);
637 context->notes.lonw = lon_entry_new(pos.lon);
638 GtkWidget *picker =
639 preset_coordinate_picker(context->appdata,
640 context->notes.latw, context->notes.lonw);
641
642 g_signal_connect(G_OBJECT(context->notes.latw), "focus-out-event",
643 G_CALLBACK(focus_out), context);
644 g_signal_connect(G_OBJECT(context->notes.lonw), "focus-out-event",
645 G_CALLBACK(focus_out), context);
646
647
648 GtkWidget *ihbox = gtk_hbox_new(FALSE, 0);
649
650 gtk_box_pack_start_defaults(GTK_BOX(ihbox), context->notes.latw);
651 gtk_box_pack_start_defaults(GTK_BOX(ihbox), context->notes.lonw);
652 gtk_box_pack_start_defaults(GTK_BOX(ihbox), picker);
653
654 gtk_table_attach_defaults(GTK_TABLE(table),
655 ihbox, 0, 3, 1, 2);
656
657 #if !defined(USE_MAEMO) || (MAEMO_VERSION_MAJOR < 5)
658 hbox = gtk_hbox_new(FALSE, 0);
659 gtk_box_pack_start(GTK_BOX(hbox), table, FALSE, FALSE, 0);
660 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
661 #else
662 gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
663 #endif
664
665 #ifndef USE_PANNABLE_AREA
666 GtkWidget *scrolled_window = gtk_scrolled_window_new(NULL, NULL);
667 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window),
668 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
669 #else
670 GtkWidget *pannable_area = hildon_pannable_area_new();
671 #endif
672
673 context->notes.buffer = gtk_text_buffer_new(NULL);
674
675 if(cache->notes && cache->notes->text)
676 gtk_text_buffer_set_text(context->notes.buffer, cache->notes->text, -1);
677
678 #ifndef USE_HILDON_TEXT_VIEW
679 GtkWidget *view = gtk_text_view_new_with_buffer(context->notes.buffer);
680 #else
681 GtkWidget *view = hildon_text_view_new();
682 hildon_text_view_set_buffer(HILDON_TEXT_VIEW(view), context->notes.buffer);
683 #endif
684
685 /* if the cache has been marked found in the logs already, there's */
686 /* no need/use to be able to change all this */
687 if(cache->found) {
688 check_button_set_active(context->notes.loggedw, TRUE);
689 check_button_set_active(context->notes.foundw, TRUE);
690 gtk_widget_set_sensitive(table, FALSE);
691
692 gtk_text_view_set_editable(GTK_TEXT_VIEW(view), FALSE);
693 gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(view), FALSE);
694 } else
695 gtk_text_view_set_editable(GTK_TEXT_VIEW(view), TRUE);
696
697 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(view), GTK_WRAP_WORD);
698 gtk_text_view_set_left_margin(GTK_TEXT_VIEW(view), 2 );
699 gtk_text_view_set_right_margin(GTK_TEXT_VIEW(view), 2 );
700
701 #ifdef USE_MAEMO
702 /* Enable Rich Text Support */
703 gtk_text_buffer_set_can_paste_rich_text(context->notes.buffer, TRUE );
704 gtk_text_buffer_set_rich_text_format(context->notes.buffer, "RTF" );
705 #endif
706
707 #ifndef NO_COPY_N_PASTE
708 g_signal_connect(G_OBJECT(view), "focus-in-event",
709 G_CALLBACK(focus_in), context->appdata);
710 g_signal_connect(G_OBJECT(view), "destroy",
711 G_CALLBACK(on_destroy_textview), context->appdata);
712 #endif
713
714 #ifndef USE_PANNABLE_AREA
715 gtk_container_add(GTK_CONTAINER(scrolled_window), view);
716
717 gtk_scrolled_window_set_shadow_type( GTK_SCROLLED_WINDOW(scrolled_window),
718 GTK_SHADOW_IN);
719 gtk_box_pack_start(GTK_BOX(vbox), scrolled_window, TRUE, TRUE, 0);
720 #else
721 gtk_container_add(GTK_CONTAINER(pannable_area), view);
722 gtk_box_pack_start(GTK_BOX(vbox), pannable_area, TRUE, TRUE, 0);
723 #endif
724
725 gtk_text_view_place_cursor_onscreen(GTK_TEXT_VIEW(view));
726
727 g_signal_connect(G_OBJECT(context->notes.buffer), "modified-changed",
728 G_CALLBACK(callback_modified), context);
729 g_signal_connect(G_OBJECT(context->notes.buffer), "changed",
730 G_CALLBACK(callback_modified), context);
731 g_signal_connect(G_OBJECT(context->notes.lonw), "changed",
732 G_CALLBACK(callback_modified), context);
733 g_signal_connect(G_OBJECT(context->notes.latw), "changed",
734 G_CALLBACK(callback_modified), context);
735 g_signal_connect(G_OBJECT(context->notes.overridew), "toggled",
736 G_CALLBACK(callback_modified), context);
737 g_signal_connect(G_OBJECT(context->notes.foundw), "toggled",
738 G_CALLBACK(callback_modified), context);
739 g_signal_connect(G_OBJECT(context->notes.loggedw), "toggled",
740 G_CALLBACK(callback_modified), context);
741
742 return vbox;
743 }
744
745 void notes_free(notes_t *notes) {
746 if(notes) {
747 if(notes->text) xmlFree(notes->text);
748 free(notes);
749 }
750 }
751
752 pos_t notes_get_pos(cache_context_t *context) {
753 pos_t pos = context->cache->pos;
754 if(check_button_get_active(context->notes.overridew)) {
755 pos.lat = lat_entry_get(context->notes.latw);
756 pos.lon = lon_entry_get(context->notes.lonw);
757 }
758 return pos;
759 }
760
761 gboolean notes_get_override(cache_context_t *context) {
762 /* get override value */
763 return check_button_get_active(context->notes.overridew);
764 }
765
766 typedef struct {
767 GtkWidget *info_label;
768 GtkWidget *dialog;
769 appdata_t *appdata;
770 } export_context_t;
771
772 typedef struct log_chain_s {
773 cache_t *cache;
774 struct log_chain_s *next;
775 } log_chain_t;
776
777 void notes_log_export(appdata_t *appdata) {
778 gpx_t *gpx = appdata->gpx;
779 log_chain_t *log = NULL, *llog, **clog = &log;
780
781 export_context_t context;
782 memset(&context, 0, sizeof(export_context_t));
783 context.appdata = appdata;
784
785 printf("export log\n");
786
787 int logs2export = 0;
788
789 /* make sure all notes are loaded */
790 while(gpx) {
791 if(!gpx->notes_loaded) {
792 notes_load_all(appdata, gpx);
793 gpx->notes_loaded = TRUE;
794 }
795
796 cache_t *cache = gpx->cache;
797 while(cache) {
798 if(cache->notes && cache->notes->found && !cache->notes->logged) {
799 gboolean already_chained = FALSE;
800 llog = log;
801 while(llog) {
802 if(strcasecmp(llog->cache->id, cache->id) == 0)
803 already_chained = TRUE;
804
805 llog = llog->next;
806 }
807
808 if(!already_chained) {
809 logs2export++;
810
811 printf("chaining log for %s\n", cache->id);
812
813 *clog = g_new0(log_chain_t, 1);
814 (*clog)->cache = cache;
815 clog = &((*clog)->next);
816 } else
817 printf("dup for %s\n", cache->id);
818
819 }
820
821 cache = cache->next;
822 }
823
824 gpx = gpx->next;
825 }
826
827
828 /* ------------- confirmation dialog ---------------- */
829 char *old_fieldnotes_path = strdup(appdata->fieldnotes_path);
830
831 context.dialog = gtk_dialog_new_with_buttons(_("Garmin Field Notes Export"),
832 GTK_WINDOW(appdata->window), GTK_DIALOG_MODAL,
833 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
834 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
835 NULL);
836
837 #if defined(USE_MAEMO) && defined(HILDON_HELP)
838 hildon_help_dialog_help_enable(GTK_DIALOG(context.dialog),
839 HELP_ID_FIELDNOTES, appdata->osso_context);
840 #endif
841
842 GtkWidget *vbox = gtk_vbox_new(FALSE,0);
843
844 /* ------------------ info text -------------- */
845
846 char *msg = g_strdup_printf(_("This will export the notes of %d caches "
847 "into the given file. This file can be "
848 "uploaded to geocaching.com for logging."),
849 logs2export);
850
851 context.info_label = gtk_label_new(msg);
852 g_free(msg);
853 gtk_label_set_line_wrap_mode(GTK_LABEL(context.info_label), PANGO_WRAP_WORD);
854 gtk_label_set_line_wrap(GTK_LABEL(context.info_label), TRUE);
855 gtk_misc_set_alignment(GTK_MISC(context.info_label), 0.f, 0.5f);
856 gtk_box_pack_start_defaults(GTK_BOX(vbox), context.info_label);
857
858 /* ------------------ path/file ------------------ */
859 gtk_box_pack_start_defaults(GTK_BOX(vbox), gtk_hseparator_new());
860
861 gtk_box_pack_start_defaults(GTK_BOX(vbox),
862 export_file(_("Save POI database"), &appdata->fieldnotes_path));
863
864 GtkWidget *label =
865 gtk_label_new(_("(a %s in the filename will be replaced by the "
866 "current date and time)"));
867 gtk_label_set_line_wrap_mode(GTK_LABEL(label), PANGO_WRAP_WORD);
868 gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
869 gtk_misc_set_alignment(GTK_MISC(label), 0.f, 0.5f);
870 gtk_box_pack_start_defaults(GTK_BOX(vbox), label);
871
872 /* ------------------ info ------------------ */
873
874 gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(context.dialog)->vbox), vbox);
875
876 gtk_widget_show_all(context.dialog);
877
878 if(GTK_RESPONSE_ACCEPT == gtk_dialog_run(GTK_DIALOG(context.dialog))) {
879
880 /* ---------------- do actual export ------------------ */
881
882 time_t now = time(NULL);
883 struct tm *tm_now = localtime(&now);
884 char now_str[32];
885 strftime(now_str, sizeof(now_str)-1, "%F_%H-%M", tm_now);
886 char *fname = g_strdup_printf(appdata->fieldnotes_path, now_str);
887
888 printf("--- about to export logs to %s ---\n", fname);
889 FILE *file = fopen(fname, "w");
890 g_free(fname);
891
892 if(file) {
893 llog = log;
894 while(llog) {
895 printf("Exporting %s\n", llog->cache->id);
896
897 /* ----------- build utc time string ----------- */
898
899 char *tz = getenv("TZ");
900 setenv("TZ", "", 1);
901 tzset();
902 struct tm *tm = localtime(&llog->cache->notes->ftime);
903 char tstr[32];
904 strftime(tstr, sizeof(tstr)-1, "%FT%H:%MZ", tm);
905 if(tz) setenv("TZ", tz, 1);
906 else unsetenv("TZ");
907 tzset();
908
909 /* escape \" in text */
910 char *text = NULL;
911 if(llog->cache->notes->text) {
912 text = g_strdup(llog->cache->notes->text);
913 char *p = text;
914 while(*p) {
915 /* convert " to ' */
916 if(*p == '\"') *p = '\'';
917 p++;
918 }
919 } else
920 text = g_strdup("");
921
922 /* ----------- build complete log string ------------ */
923 char *str = g_strdup_printf("%s,%s,Found it,\"%s\"\n",
924 llog->cache->id, tstr, text);
925 g_free(text);
926
927 /* ----------- convert to unicode ------------ */
928 glong written = 0;
929 gunichar2 *uc = g_utf8_to_utf16(str, -1, NULL, &written, NULL);
930 g_free(str);
931
932 /* -------------- and write it --------------- */
933 fwrite(uc, written, sizeof(gunichar2), file);
934 g_free(uc);
935
936 /* -------------- set "logged" flag in notes and update them ------ */
937 llog->cache->notes->logged = TRUE;
938
939 /* update flags in all copies of this cache */
940
941 gpx_t *tgpx = appdata->gpx;
942 while(tgpx) {
943 cache_t *tcache = tgpx->cache;
944 while(tcache) {
945 if((tcache != llog->cache) &&
946 (strcmp(tcache->id, llog->cache->id) == 0)) {
947 printf("found dup cache %s in %s\n", tcache->id, tgpx->name);
948
949 if(tcache->notes)
950 notes_free(tcache->notes);
951
952 /* create a new note for this cache */
953 tcache->notes = malloc(sizeof(notes_t));
954 memset(tcache->notes, 0, sizeof(notes_t));
955 if(llog->cache->notes->text)
956 tcache->notes->text = strdup(llog->cache->notes->text);
957 tcache->notes->pos = llog->cache->notes->pos;
958 tcache->notes->override = llog->cache->notes->override;
959 tcache->notes->found = llog->cache->notes->found;
960 tcache->notes->logged = llog->cache->notes->logged;
961 tcache->notes->ftime = llog->cache->notes->ftime;
962 }
963 tcache = tcache->next;
964 }
965 tgpx = tgpx->next;
966 }
967
968 /* finally write the notes file itself */
969 cache_context_t ccontext;
970 ccontext.appdata = appdata;
971 ccontext.cache = llog->cache;
972
973 notes_write_file(&ccontext,
974 llog->cache->notes->text, llog->cache->notes->pos,
975 llog->cache->notes->override, llog->cache->notes->found,
976 llog->cache->notes->ftime, llog->cache->notes->logged);
977
978 llog = llog->next;
979 }
980
981 fclose(file);
982 } else
983 errorf(_("Can't open file:\n\n%s"), strerror(errno));
984
985 } else {
986 /* restore old path, in case it has been altered but not been used */
987 free(appdata->fieldnotes_path);
988 appdata->fieldnotes_path = strdup(old_fieldnotes_path);
989 }
990
991 gtk_widget_destroy(context.dialog);
992
993 free(old_fieldnotes_path);
994
995 /* free list */
996 while(log) {
997 log_chain_t *next = log->next;
998 g_free(log);
999 log = next;
1000 }
1001 }