Contents of /trunk/src/notes.c

Parent Directory Parent Directory | Revision Log Revision Log


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