Parent Directory | Revision Log
Various small bug 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(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 | #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 | #if defined(USE_MAEMO) && (MAEMO_VERSION_MAJOR >= 5) |
43 | #include <hildon/hildon-note.h> |
44 | #endif |
45 | |
46 | void gtk_text_buffer_set_can_paste_rich_text(GtkTextBuffer *buffer, gboolean); |
47 | void gtk_text_buffer_set_rich_text_format(GtkTextBuffer *buffer, const gchar *); |
48 | |
49 | #define TAG_STATE GTK_STATE_PRELIGHT |
50 | |
51 | static 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 | 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 | static int notes_write_file(cache_context_t *context, |
198 | char *text, pos_t pos, |
199 | gboolean override, gboolean found, |
200 | time_t ftime, gboolean logged) { |
201 | |
202 | g_assert(context); |
203 | g_assert(context->cache); |
204 | |
205 | /* build local path */ |
206 | int path_len = strlen(context->appdata->image_path) + |
207 | 2 * strlen(context->cache->id) + 6; |
208 | |
209 | char *path = malloc(path_len); |
210 | snprintf(path, path_len, "%s%s/%s.gpx", |
211 | context->appdata->image_path, |
212 | context->cache->id, context->cache->id); |
213 | |
214 | if(checkdir(path) != 0) { |
215 | printf("unable to create notes path\n"); |
216 | free(path); |
217 | return -1; |
218 | } |
219 | |
220 | LIBXML_TEST_VERSION; |
221 | |
222 | xmlDocPtr doc = xmlNewDoc(BAD_CAST "1.0"); |
223 | xmlNodePtr root_node = xmlNewNode(NULL, BAD_CAST "gpx"); |
224 | xmlDocSetRootElement(doc, root_node); |
225 | |
226 | xmlNodePtr wpt_node = xmlNewChild(root_node, NULL, BAD_CAST "wpt", NULL); |
227 | /* make sure no invalid position gets saved */ |
228 | if(!isnan(pos.lat) && !isnan(pos.lon)) { |
229 | if(override) |
230 | xmlNewProp(wpt_node, BAD_CAST "override", BAD_CAST "yes"); |
231 | else |
232 | xmlNewProp(wpt_node, BAD_CAST "override", BAD_CAST "no"); |
233 | |
234 | if(logged) |
235 | xmlNewProp(wpt_node, BAD_CAST "logged", BAD_CAST "yes"); |
236 | else |
237 | xmlNewProp(wpt_node, BAD_CAST "logged", BAD_CAST "no"); |
238 | |
239 | if(found) { |
240 | if(ftime) { |
241 | char str[32]; |
242 | snprintf(str, sizeof(str), "%lu", ftime); |
243 | xmlNewProp(wpt_node, BAD_CAST "found", BAD_CAST str); |
244 | } else |
245 | xmlNewProp(wpt_node, BAD_CAST "found", BAD_CAST "yes"); |
246 | } else |
247 | xmlNewProp(wpt_node, BAD_CAST "found", BAD_CAST "no"); |
248 | |
249 | char str[32]; |
250 | g_ascii_dtostr(str, sizeof(str), pos.lat); |
251 | xmlNewProp(wpt_node, BAD_CAST "lat", BAD_CAST str); |
252 | g_ascii_dtostr(str, sizeof(str), pos.lon); |
253 | xmlNewProp(wpt_node, BAD_CAST "lon", BAD_CAST str); |
254 | } |
255 | |
256 | int len = strlen(context->cache->id) + strlen(" - ") + |
257 | strlen(context->cache->name) + 1; |
258 | char *name = malloc(len); |
259 | snprintf(name, len, "%s - %s", context->cache->id, context->cache->name); |
260 | xmlNewChild(wpt_node, NULL, BAD_CAST "name", BAD_CAST name); |
261 | free(name); |
262 | xmlNewChild(wpt_node, NULL, BAD_CAST "sym", BAD_CAST "Pin, Blue"); |
263 | xmlNewChild(wpt_node, NULL, BAD_CAST "desc", BAD_CAST text); |
264 | |
265 | /* write everything and free it */ |
266 | printf("writing %s\n", path); |
267 | xmlSaveFormatFileEnc(path, doc, "UTF-8", 1); |
268 | xmlFreeDoc(doc); |
269 | xmlCleanupParser(); |
270 | free(path); |
271 | |
272 | return 0; |
273 | } |
274 | |
275 | static void notes_save(cache_context_t *context) { |
276 | /* only save if: there is a text which has been changed or */ |
277 | /* there is a position which differs from the original one */ |
278 | /* or has been changed */ |
279 | |
280 | if(context->notes.modified) { |
281 | #ifdef USE_STACKABLE_WINDOW |
282 | context->notes_have_been_changed = TRUE; |
283 | #endif |
284 | |
285 | printf("something has been modified, saving notes\n"); |
286 | |
287 | GtkTextIter start; |
288 | GtkTextIter end; |
289 | gtk_text_buffer_get_start_iter(context->notes.buffer, &start); |
290 | gtk_text_buffer_get_end_iter(context->notes.buffer, &end); |
291 | char *text = gtk_text_buffer_get_text(context->notes.buffer, |
292 | &start, &end, FALSE); |
293 | |
294 | pos_t pos; |
295 | pos.lat = lat_entry_get(context->notes.latw); |
296 | pos.lon = lon_entry_get(context->notes.lonw); |
297 | |
298 | gboolean override = |
299 | check_button_get_active(context->notes.overridew); |
300 | gboolean found = |
301 | check_button_get_active(context->notes.foundw); |
302 | gboolean logged = |
303 | check_button_get_active(context->notes.loggedw); |
304 | |
305 | if(pos_differ(&pos, &context->cache->pos)) |
306 | printf("position is modified\n"); |
307 | if(override || found) |
308 | printf("flags are set\n"); |
309 | if(strlen(text)) |
310 | printf("text is present\n"); |
311 | |
312 | /* check if the notes are empty */ |
313 | if(!pos_differ(&pos, &context->cache->pos) && |
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 | notes_write_file(context, text, pos, override, found, |
369 | context->notes.ftime, logged); |
370 | |
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 | } |
418 | |
419 | /* 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 | return FALSE; |
427 | } |
428 | |
429 | #ifndef NO_COPY_N_PASTE |
430 | 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 | #endif |
457 | |
458 | static void ftime_update(GtkWidget *widget, cache_context_t *context, |
459 | gboolean update) { |
460 | /* check if it has been selected */ |
461 | if(check_button_get_active(widget)) { |
462 | 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 | if(!check_button_get_active(widget)) { |
494 | #if !defined(USE_MAEMO) || (MAEMO_VERSION_MAJOR < 5) |
495 | 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 | #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 | 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 | if(!check_button_get_active(widget)) { |
530 | #if !defined(USE_MAEMO) || (MAEMO_VERSION_MAJOR < 5) |
531 | 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 | #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 | gtk_widget_destroy(dialog); |
567 | } |
568 | } |
569 | } |
570 | |
571 | #ifndef NO_COPY_N_PASTE |
572 | 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 | #endif |
591 | |
592 | 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 | 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 | #if !defined(USE_MAEMO) || (MAEMO_VERSION_MAJOR < 5) |
620 | gtk_table_set_col_spacing(GTK_TABLE(table), 0, 16); |
621 | #endif |
622 | |
623 | gtk_table_set_col_spacing(GTK_TABLE(table), 2, 16); |
624 | |
625 | gtk_table_attach_defaults(GTK_TABLE(table), |
626 | gtk_label_new(_("New coordinate:")), 0, 1, 0, 1); |
627 | context->notes.overridew = check_button_new_with_label(_("Override")); |
628 | check_button_set_active(context->notes.overridew, |
629 | cache->notes && cache->notes->override); |
630 | gtk_table_attach_defaults(GTK_TABLE(table), |
631 | context->notes.overridew, 2, 3, 0, 1); |
632 | |
633 | GtkWidget *hbox = gtk_hbox_new(FALSE, 2); |
634 | |
635 | context->notes.foundw = check_button_new_with_label(_("Found")); |
636 | check_button_set_active(context->notes.foundw, |
637 | cache->notes && cache->notes->found); |
638 | gtk_box_pack_start_defaults(GTK_BOX(hbox), context->notes.foundw); |
639 | |
640 | context->notes.loggedw = check_button_new_with_label(_("Logged")); |
641 | check_button_set_active(context->notes.loggedw, |
642 | cache->notes && cache->notes->logged); |
643 | gtk_box_pack_start_defaults(GTK_BOX(hbox), context->notes.loggedw); |
644 | |
645 | gtk_table_attach_defaults(GTK_TABLE(table), hbox, 3, 4, 0, 1); |
646 | |
647 | /* only found and logged caches have a log flag the user can change */ |
648 | if(!(cache->notes && cache->notes->found && cache->notes->logged)) |
649 | gtk_widget_set_sensitive(context->notes.loggedw, FALSE); |
650 | else |
651 | gtk_widget_set_sensitive(context->notes.foundw, FALSE); |
652 | |
653 | context->notes.datew = gtk_label_new(""); |
654 | gtk_misc_set_alignment(GTK_MISC(context->notes.datew), 0.5f, 0.5f); |
655 | gtk_table_attach_defaults(GTK_TABLE(table), |
656 | context->notes.datew, 3, 4, 1, 2); |
657 | ftime_update(context->notes.foundw, context, FALSE); |
658 | |
659 | pos_t pos = gpx_cache_pos(cache); |
660 | if(cache->notes) pos = cache->notes->pos; |
661 | |
662 | gtk_table_attach_defaults(GTK_TABLE(table), |
663 | context->notes.latw = lat_entry_new(pos.lat), 0, 1, 1, 2); |
664 | g_signal_connect(G_OBJECT(context->notes.latw), "focus-out-event", |
665 | G_CALLBACK(focus_out), context); |
666 | |
667 | gtk_table_attach_defaults(GTK_TABLE(table), |
668 | context->notes.lonw = lon_entry_new(pos.lon), 2, 3, 1, 2); |
669 | g_signal_connect(G_OBJECT(context->notes.lonw), "focus-out-event", |
670 | G_CALLBACK(focus_out), context); |
671 | |
672 | #if !defined(USE_MAEMO) || (MAEMO_VERSION_MAJOR < 5) |
673 | hbox = gtk_hbox_new(FALSE, 0); |
674 | gtk_box_pack_start(GTK_BOX(hbox), table, FALSE, FALSE, 0); |
675 | gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0); |
676 | #else |
677 | gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0); |
678 | #endif |
679 | |
680 | #ifndef USE_PANNABLE_AREA |
681 | GtkWidget *scrolled_window = gtk_scrolled_window_new(NULL, NULL); |
682 | gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window), |
683 | GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC); |
684 | #else |
685 | GtkWidget *pannable_area = hildon_pannable_area_new(); |
686 | #endif |
687 | |
688 | context->notes.buffer = gtk_text_buffer_new(NULL); |
689 | |
690 | if(cache->notes && cache->notes->text) |
691 | gtk_text_buffer_set_text(context->notes.buffer, cache->notes->text, -1); |
692 | |
693 | #ifndef USE_HILDON_TEXT_VIEW |
694 | GtkWidget *view = gtk_text_view_new_with_buffer(context->notes.buffer); |
695 | #else |
696 | GtkWidget *view = hildon_text_view_new(); |
697 | hildon_text_view_set_buffer(HILDON_TEXT_VIEW(view), context->notes.buffer); |
698 | #endif |
699 | |
700 | /* if the cache has been marked found in the logs already, there's */ |
701 | /* no need/use to be able to change all this */ |
702 | if(cache->found) { |
703 | gtk_widget_set_sensitive(table, FALSE); |
704 | gtk_widget_set_sensitive(view, FALSE); |
705 | } |
706 | |
707 | gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(view), GTK_WRAP_WORD); |
708 | gtk_text_view_set_editable(GTK_TEXT_VIEW(view), TRUE); |
709 | gtk_text_view_set_left_margin(GTK_TEXT_VIEW(view), 2 ); |
710 | gtk_text_view_set_right_margin(GTK_TEXT_VIEW(view), 2 ); |
711 | |
712 | #ifdef USE_MAEMO |
713 | /* Enable Rich Text Support */ |
714 | gtk_text_buffer_set_can_paste_rich_text(context->notes.buffer, TRUE ); |
715 | gtk_text_buffer_set_rich_text_format(context->notes.buffer, "RTF" ); |
716 | #endif |
717 | |
718 | #ifndef NO_COPY_N_PASTE |
719 | g_signal_connect(G_OBJECT(view), "focus-in-event", |
720 | G_CALLBACK(focus_in), context->appdata); |
721 | g_signal_connect(G_OBJECT(view), "destroy", |
722 | G_CALLBACK(on_destroy_textview), context->appdata); |
723 | #endif |
724 | |
725 | #ifndef USE_PANNABLE_AREA |
726 | gtk_container_add(GTK_CONTAINER(scrolled_window), view); |
727 | |
728 | gtk_scrolled_window_set_shadow_type( GTK_SCROLLED_WINDOW(scrolled_window), |
729 | GTK_SHADOW_IN); |
730 | gtk_box_pack_start(GTK_BOX(vbox), scrolled_window, TRUE, TRUE, 0); |
731 | #else |
732 | gtk_container_add(GTK_CONTAINER(pannable_area), view); |
733 | gtk_box_pack_start(GTK_BOX(vbox), pannable_area, TRUE, TRUE, 0); |
734 | #endif |
735 | |
736 | gtk_text_view_place_cursor_onscreen(GTK_TEXT_VIEW(view)); |
737 | |
738 | g_signal_connect(G_OBJECT(context->notes.buffer), "modified-changed", |
739 | G_CALLBACK(callback_modified), context); |
740 | g_signal_connect(G_OBJECT(context->notes.buffer), "changed", |
741 | G_CALLBACK(callback_modified), context); |
742 | g_signal_connect(G_OBJECT(context->notes.lonw), "changed", |
743 | G_CALLBACK(callback_modified), context); |
744 | g_signal_connect(G_OBJECT(context->notes.latw), "changed", |
745 | G_CALLBACK(callback_modified), context); |
746 | g_signal_connect(G_OBJECT(context->notes.overridew), "toggled", |
747 | G_CALLBACK(callback_modified), context); |
748 | g_signal_connect(G_OBJECT(context->notes.foundw), "toggled", |
749 | G_CALLBACK(callback_modified), context); |
750 | g_signal_connect(G_OBJECT(context->notes.loggedw), "toggled", |
751 | G_CALLBACK(callback_modified), context); |
752 | |
753 | return vbox; |
754 | } |
755 | |
756 | void notes_free(notes_t *notes) { |
757 | if(notes) { |
758 | if(notes->text) xmlFree(notes->text); |
759 | free(notes); |
760 | } |
761 | } |
762 | |
763 | pos_t notes_get_pos(cache_context_t *context) { |
764 | pos_t pos = context->cache->pos; |
765 | if(check_button_get_active(context->notes.overridew)) { |
766 | pos.lat = lat_entry_get(context->notes.latw); |
767 | pos.lon = lon_entry_get(context->notes.lonw); |
768 | } |
769 | return pos; |
770 | } |
771 | |
772 | gboolean notes_get_override(cache_context_t *context) { |
773 | /* get override value */ |
774 | return check_button_get_active(context->notes.overridew); |
775 | } |
776 | |
777 | typedef struct { |
778 | GtkWidget *info_label; |
779 | GtkWidget *dialog; |
780 | appdata_t *appdata; |
781 | GtkWidget *path_label; |
782 | } export_context_t; |
783 | |
784 | static void on_browse(GtkWidget *widget, gpointer data) { |
785 | GtkWidget *dialog; |
786 | |
787 | export_context_t *context = (export_context_t*)data; |
788 | |
789 | #ifdef USE_MAEMO |
790 | dialog = hildon_file_chooser_dialog_new(GTK_WINDOW(context->dialog), |
791 | GTK_FILE_CHOOSER_ACTION_SAVE); |
792 | #else |
793 | dialog = gtk_file_chooser_dialog_new(_("Save POI database"), |
794 | GTK_WINDOW(context->dialog), |
795 | GTK_FILE_CHOOSER_ACTION_SAVE, |
796 | GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, |
797 | GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT, |
798 | NULL); |
799 | #endif |
800 | |
801 | printf("set filename <%s>\n", context->appdata->fieldnotes_path); |
802 | |
803 | if(!g_file_test(context->appdata->fieldnotes_path, G_FILE_TEST_EXISTS)) { |
804 | char *last_sep = strrchr(context->appdata->fieldnotes_path, '/'); |
805 | if(last_sep) { |
806 | *last_sep = 0; // seperate path from file |
807 | |
808 | /* the user just created a new document */ |
809 | gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), |
810 | context->appdata->fieldnotes_path); |
811 | gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), last_sep+1); |
812 | |
813 | /* restore full filename */ |
814 | *last_sep = '/'; |
815 | } |
816 | } else |
817 | gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dialog), |
818 | context->appdata->fieldnotes_path); |
819 | |
820 | if (gtk_dialog_run (GTK_DIALOG(dialog)) == GTK_FM_OK) { |
821 | gchar *name = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog)); |
822 | if(name) { |
823 | free(context->appdata->fieldnotes_path); |
824 | context->appdata->fieldnotes_path = strdup(name); |
825 | gtk_label_set_text(GTK_LABEL(context->path_label), |
826 | context->appdata->fieldnotes_path); |
827 | } |
828 | } |
829 | |
830 | gtk_widget_destroy (dialog); |
831 | } |
832 | |
833 | typedef struct log_chain_s { |
834 | cache_t *cache; |
835 | struct log_chain_s *next; |
836 | } log_chain_t; |
837 | |
838 | void notes_log_export(appdata_t *appdata) { |
839 | gpx_t *gpx = appdata->gpx; |
840 | log_chain_t *log = NULL, *llog, **clog = &log; |
841 | |
842 | export_context_t context; |
843 | memset(&context, 0, sizeof(export_context_t)); |
844 | context.appdata = appdata; |
845 | |
846 | printf("export log\n"); |
847 | |
848 | int logs2export = 0; |
849 | |
850 | /* make sure all notes are loaded */ |
851 | while(gpx) { |
852 | if(!gpx->notes_loaded) { |
853 | notes_load_all(appdata, gpx); |
854 | gpx->notes_loaded = TRUE; |
855 | } |
856 | |
857 | cache_t *cache = gpx->cache; |
858 | while(cache) { |
859 | if(cache->notes && cache->notes->found && !cache->notes->logged) { |
860 | gboolean already_chained = FALSE; |
861 | llog = log; |
862 | while(llog) { |
863 | if(strcasecmp(llog->cache->id, cache->id) == 0) |
864 | already_chained = TRUE; |
865 | |
866 | llog = llog->next; |
867 | } |
868 | |
869 | if(!already_chained) { |
870 | logs2export++; |
871 | |
872 | printf("chaining log for %s\n", cache->id); |
873 | |
874 | *clog = g_new0(log_chain_t, 1); |
875 | (*clog)->cache = cache; |
876 | clog = &((*clog)->next); |
877 | } else |
878 | printf("dup for %s\n", cache->id); |
879 | |
880 | } |
881 | |
882 | cache = cache->next; |
883 | } |
884 | |
885 | gpx = gpx->next; |
886 | } |
887 | |
888 | |
889 | /* ------------- confirmation dialog ---------------- */ |
890 | char *old_fieldnotes_path = strdup(appdata->fieldnotes_path); |
891 | |
892 | context.dialog = gtk_dialog_new_with_buttons(_("Garmin Field Notes Export"), |
893 | GTK_WINDOW(appdata->window), GTK_DIALOG_MODAL, |
894 | GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, |
895 | GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT, |
896 | NULL); |
897 | |
898 | #if defined(USE_MAEMO) && defined(HILDON_HELP) |
899 | hildon_help_dialog_help_enable(GTK_DIALOG(context.dialog), |
900 | HELP_ID_FIELDNOTES, appdata->osso_context); |
901 | #endif |
902 | |
903 | GtkWidget *vbox = gtk_vbox_new(FALSE,0); |
904 | |
905 | /* ------------------ info text -------------- */ |
906 | |
907 | char *msg = g_strdup_printf(_("This will export the notes of %d caches " |
908 | "into the given file. This file can be " |
909 | "uploaded to geocaching.com for logging."), |
910 | logs2export); |
911 | |
912 | context.info_label = gtk_label_new(msg); |
913 | g_free(msg); |
914 | gtk_label_set_line_wrap_mode(GTK_LABEL(context.info_label), PANGO_WRAP_WORD); |
915 | gtk_label_set_line_wrap(GTK_LABEL(context.info_label), TRUE); |
916 | gtk_misc_set_alignment(GTK_MISC(context.info_label), 0.f, 0.5f); |
917 | gtk_box_pack_start_defaults(GTK_BOX(vbox), context.info_label); |
918 | |
919 | /* ------------------ path/file ------------------ */ |
920 | gtk_box_pack_start_defaults(GTK_BOX(vbox), gtk_hseparator_new()); |
921 | |
922 | GtkWidget *hbox = gtk_hbox_new(FALSE, 0); |
923 | GtkWidget *label = gtk_label_new(_("Export to:")); |
924 | gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE,0); |
925 | gtk_misc_set_alignment(GTK_MISC(label), 0.f, 0.5f); |
926 | GtkWidget *button = gtk_button_new_with_label(_("Browse")); |
927 | #if defined(USE_MAEMO) && (MAEMO_VERSION_MAJOR == 5) |
928 | hildon_gtk_widget_set_theme_size(button, |
929 | (HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH)); |
930 | #endif |
931 | gtk_signal_connect(GTK_OBJECT(button), "clicked", |
932 | GTK_SIGNAL_FUNC(on_browse), (gpointer)&context); |
933 | gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE,0); |
934 | gtk_box_pack_start_defaults(GTK_BOX(vbox), hbox); |
935 | |
936 | context.path_label = gtk_label_new(appdata->fieldnotes_path); |
937 | gtk_misc_set_alignment(GTK_MISC(context.path_label), 0.f, 0.5f); |
938 | gtk_label_set_ellipsize(GTK_LABEL(context.path_label), |
939 | PANGO_ELLIPSIZE_MIDDLE); |
940 | gtk_box_pack_start_defaults(GTK_BOX(vbox), context.path_label); |
941 | |
942 | label = gtk_label_new(_("(a %s in the filename will be replaced by the " |
943 | "current date and time)")); |
944 | gtk_label_set_line_wrap_mode(GTK_LABEL(label), PANGO_WRAP_WORD); |
945 | gtk_label_set_line_wrap(GTK_LABEL(label), TRUE); |
946 | gtk_misc_set_alignment(GTK_MISC(label), 0.f, 0.5f); |
947 | gtk_box_pack_start_defaults(GTK_BOX(vbox), label); |
948 | |
949 | /* ------------------ info ------------------ */ |
950 | |
951 | gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(context.dialog)->vbox), vbox); |
952 | |
953 | gtk_widget_show_all(context.dialog); |
954 | |
955 | if(GTK_RESPONSE_ACCEPT == gtk_dialog_run(GTK_DIALOG(context.dialog))) { |
956 | |
957 | /* ---------------- do actual export ------------------ */ |
958 | |
959 | time_t now = time(NULL); |
960 | struct tm *tm_now = localtime(&now); |
961 | char now_str[32]; |
962 | strftime(now_str, sizeof(now_str)-1, "%F_%H:%M", tm_now); |
963 | char *fname = g_strdup_printf(appdata->fieldnotes_path, now_str); |
964 | printf("--- about to export logs to %s ---\n", fname); |
965 | FILE *file = fopen(fname, "w"); |
966 | g_free(fname); |
967 | |
968 | if(file) { |
969 | |
970 | llog = log; |
971 | while(llog) { |
972 | printf("Exporting %s\n", llog->cache->id); |
973 | |
974 | /* ----------- build utc time string ----------- */ |
975 | |
976 | char *tz = getenv("TZ"); |
977 | setenv("TZ", "", 1); |
978 | tzset(); |
979 | struct tm *tm = localtime(&llog->cache->notes->ftime); |
980 | char tstr[32]; |
981 | strftime(tstr, sizeof(tstr)-1, "%FT%H:%MZ", tm); |
982 | if(tz) setenv("TZ", tz, 1); |
983 | else unsetenv("TZ"); |
984 | tzset(); |
985 | |
986 | /* escape \" in text */ |
987 | char *text = NULL; |
988 | if(llog->cache->notes->text) { |
989 | text = g_strdup(llog->cache->notes->text); |
990 | char *p = text; |
991 | while(*p) { |
992 | /* convert " to ' */ |
993 | if(*p == '\"') *p = '\''; |
994 | p++; |
995 | } |
996 | } else |
997 | text = g_strdup(""); |
998 | |
999 | /* ----------- build complete log string ------------ */ |
1000 | char *str = g_strdup_printf("%s,%s,Found it,\"%s\"\n", |
1001 | llog->cache->id, tstr, text); |
1002 | g_free(text); |
1003 | |
1004 | /* ----------- convert to unicode ------------ */ |
1005 | glong written = 0; |
1006 | gunichar2 *uc = g_utf8_to_utf16(str, -1, NULL, &written, NULL); |
1007 | g_free(str); |
1008 | |
1009 | /* -------------- and write it --------------- */ |
1010 | fwrite(uc, written, sizeof(gunichar2), file); |
1011 | g_free(uc); |
1012 | |
1013 | /* -------------- set "logged" flag in notes and update them ------ */ |
1014 | llog->cache->notes->logged = TRUE; |
1015 | |
1016 | /* update flags in all copies of this cache */ |
1017 | |
1018 | gpx_t *tgpx = appdata->gpx; |
1019 | while(tgpx) { |
1020 | cache_t *tcache = tgpx->cache; |
1021 | while(tcache) { |
1022 | if((tcache != llog->cache) && |
1023 | (strcmp(tcache->id, llog->cache->id) == 0)) { |
1024 | printf("found dup cache %s in %s\n", tcache->id, tgpx->name); |
1025 | |
1026 | if(tcache->notes) |
1027 | notes_free(tcache->notes); |
1028 | |
1029 | /* create a new note for this cache */ |
1030 | tcache->notes = malloc(sizeof(notes_t)); |
1031 | memset(tcache->notes, 0, sizeof(notes_t)); |
1032 | if(llog->cache->notes->text) |
1033 | tcache->notes->text = strdup(llog->cache->notes->text); |
1034 | tcache->notes->pos = llog->cache->notes->pos; |
1035 | tcache->notes->override = llog->cache->notes->override; |
1036 | tcache->notes->found = llog->cache->notes->found; |
1037 | tcache->notes->logged = llog->cache->notes->logged; |
1038 | tcache->notes->ftime = llog->cache->notes->ftime; |
1039 | } |
1040 | tcache = tcache->next; |
1041 | } |
1042 | tgpx = tgpx->next; |
1043 | } |
1044 | |
1045 | /* finally write the notes file itself */ |
1046 | cache_context_t ccontext; |
1047 | ccontext.appdata = appdata; |
1048 | ccontext.cache = llog->cache; |
1049 | |
1050 | notes_write_file(&ccontext, |
1051 | llog->cache->notes->text, llog->cache->notes->pos, |
1052 | llog->cache->notes->override, llog->cache->notes->found, |
1053 | llog->cache->notes->ftime, llog->cache->notes->logged); |
1054 | |
1055 | llog = llog->next; |
1056 | } |
1057 | |
1058 | fclose(file); |
1059 | } |
1060 | } else { |
1061 | /* restore old path, in case it has been altered but not been used */ |
1062 | free(appdata->fieldnotes_path); |
1063 | appdata->fieldnotes_path = strdup(old_fieldnotes_path); |
1064 | } |
1065 | |
1066 | gtk_widget_destroy(context.dialog); |
1067 | |
1068 | free(old_fieldnotes_path); |
1069 | |
1070 | /* free list */ |
1071 | while(log) { |
1072 | log_chain_t *next = log->next; |
1073 | g_free(log); |
1074 | log = next; |
1075 | } |
1076 | } |