Contents of /trunk/src/notes.c

Parent Directory Parent Directory | Revision Log Revision Log


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