Contents of /trunk/src/notes.c

Parent Directory Parent Directory | Revision Log Revision Log


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