Contents of /trunk/src/notes.c

Parent Directory Parent Directory | Revision Log Revision Log


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