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