Contents of /trunk/src/notes.c

Parent Directory Parent Directory | Revision Log Revision Log


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