Contents of /trunk/src/notes.c

Parent Directory Parent Directory | Revision Log Revision Log


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