Parent Directory | Revision Log
Various small bug fixes
1 | /* |
2 | * This file is part of GPXView. |
3 | * |
4 | * GPXView is free software: you can redistribute it and/or modify |
5 | * it under the terms of the GNU General Public License as published by |
6 | * the Free Software Foundation, either version 3 of the License, or |
7 | * (at your option) any later version. |
8 | * |
9 | * GPXView is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | * GNU General Public License for more details. |
13 | * |
14 | * You should have received a copy of the GNU General Public License |
15 | * along with GPXView. If not, see <http://www.gnu.org/licenses/>. |
16 | */ |
17 | |
18 | #define __USE_GNU |
19 | #include <string.h> |
20 | |
21 | #include <stdio.h> |
22 | #include <math.h> |
23 | |
24 | #include <curl/curl.h> |
25 | |
26 | #include <time.h> |
27 | #include <sys/time.h> |
28 | |
29 | #include "gpxview.h" |
30 | #include "custom_size_renderer.h" |
31 | #include "custom_rating_renderer.h" |
32 | #include "custom_type_renderer.h" |
33 | |
34 | #ifdef USE_MAEMO |
35 | #include <hildon/hildon-banner.h> |
36 | #if MAEMO_VERSION_MAJOR >= 5 |
37 | #include <hildon/hildon-note.h> |
38 | #include <hildon/hildon-entry.h> |
39 | #include <hildon/hildon-check-button.h> |
40 | #endif |
41 | #endif |
42 | |
43 | #include <locale.h> |
44 | |
45 | extern char *strcasestr (__const char *__haystack, __const char *__needle); |
46 | |
47 | #if defined(USE_BREAD_CRUMB_TRAIL) || defined(BCT) |
48 | static void crumb_add(appdata_t *appdata, char *name, int level, |
49 | gpointer user_data); |
50 | |
51 | enum { |
52 | CRUMB_GPXLIST = 0, |
53 | CRUMB_CACHELIST, |
54 | CRUMB_SEARCH_GLOBAL, |
55 | CRUMB_SEARCH_GPX, |
56 | CRUMB_CACHE, |
57 | }; |
58 | #endif |
59 | |
60 | /* size of first buffer malloc; start small to exercise grow routines */ |
61 | #define FIRSTSIZE 1 |
62 | |
63 | void errorf(const char *fmt, ...) { |
64 | va_list args; |
65 | char *buf = NULL; |
66 | size_t bufsize; |
67 | char *newbuf; |
68 | size_t nextsize = 0; |
69 | int outsize; |
70 | |
71 | bufsize = 0; |
72 | for (;;) { |
73 | if (bufsize == 0) { |
74 | if ((buf = (char *)malloc(FIRSTSIZE)) == NULL) |
75 | return; |
76 | |
77 | bufsize = 1; |
78 | } else if ((newbuf = (char *)realloc(buf, nextsize)) != NULL) { |
79 | buf = newbuf; |
80 | bufsize = nextsize; |
81 | } else { |
82 | free(buf); |
83 | return; |
84 | } |
85 | |
86 | va_start(args, fmt); |
87 | outsize = vsnprintf(buf, bufsize, fmt, args); |
88 | va_end(args); |
89 | |
90 | if (outsize == -1) { |
91 | nextsize = bufsize * 2; |
92 | } else if (outsize == bufsize) { |
93 | nextsize = bufsize * 2; |
94 | } else if (outsize > bufsize) { |
95 | nextsize = outsize + 2; // Linux! |
96 | } else if (outsize == bufsize - 1) { |
97 | nextsize = bufsize * 2; |
98 | } else { |
99 | /* Output was not truncated */ |
100 | break; |
101 | } |
102 | } |
103 | |
104 | #if !defined(USE_MAEMO) || (MAEMO_VERSION_MAJOR < 5) |
105 | GtkWidget *dialog = gtk_message_dialog_new( |
106 | GTK_WINDOW(NULL), |
107 | GTK_DIALOG_DESTROY_WITH_PARENT, |
108 | GTK_MESSAGE_ERROR, |
109 | GTK_BUTTONS_CLOSE, buf); |
110 | |
111 | gtk_window_set_title(GTK_WINDOW(dialog), _("ERROR")); |
112 | #else |
113 | GtkWidget *dialog = |
114 | hildon_note_new_information(GTK_WINDOW(NULL), buf); |
115 | #endif |
116 | |
117 | gtk_dialog_run(GTK_DIALOG(dialog)); |
118 | gtk_widget_destroy(dialog); |
119 | |
120 | free(buf); |
121 | } |
122 | |
123 | gpx_t *choose_file(appdata_t *appdata, gboolean whole_dir) { |
124 | GtkWidget *dialog; |
125 | gpx_t *gpx = NULL; |
126 | |
127 | #ifdef USE_MAEMO |
128 | dialog = hildon_file_chooser_dialog_new(GTK_WINDOW(appdata->window), |
129 | whole_dir?GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER : |
130 | GTK_FILE_CHOOSER_ACTION_OPEN); |
131 | |
132 | #ifdef HILDON_HELP |
133 | hildon_help_dialog_help_enable(GTK_DIALOG(dialog), |
134 | HELP_ID_IMPORT, appdata->osso_context); |
135 | #endif |
136 | #else |
137 | dialog = gtk_file_chooser_dialog_new (whole_dir?_("Import directory"): |
138 | _("Import file"), |
139 | GTK_WINDOW(appdata->window), |
140 | whole_dir?GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER : |
141 | GTK_FILE_CHOOSER_ACTION_OPEN, |
142 | GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, |
143 | GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT, |
144 | NULL); |
145 | #endif |
146 | |
147 | /* use path if one is present */ |
148 | if(appdata->path) |
149 | gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), |
150 | appdata->path); |
151 | |
152 | gtk_widget_show_all (GTK_WIDGET(dialog)); |
153 | if (gtk_dialog_run (GTK_DIALOG(dialog)) == GTK_FM_OK) { |
154 | char *filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog)); |
155 | |
156 | if(filename) { |
157 | gpx_dialog_t *dialog = gpx_busy_dialog_new(GTK_WIDGET(appdata->window)); |
158 | |
159 | if(!whole_dir) |
160 | gpx = gpx_parse(dialog, filename, appdata->username); |
161 | else { |
162 | /* cur trailing '/' if present */ |
163 | if(strlastchr(filename) == '/') |
164 | filename[strlen(filename)] = 0; |
165 | |
166 | gpx = gpx_parse_dir(dialog, filename, appdata->username); |
167 | } |
168 | |
169 | gpx_busy_dialog_destroy(dialog); |
170 | |
171 | /* save path if gpx was successfully loaded */ |
172 | if(gpx) { |
173 | char *r = strrchr(filename, '/'); |
174 | |
175 | /* there is a delimiter, use everything left of it as path */ |
176 | if(r && !whole_dir) { |
177 | *r = 0; |
178 | if(appdata->path) free(appdata->path); |
179 | appdata->path = strdup(filename); |
180 | /* restore path ... just in case ... */ |
181 | *r = '/'; |
182 | } |
183 | |
184 | if(whole_dir) |
185 | appdata->path = strdup(filename); |
186 | } else |
187 | errorf(_("Load error")); |
188 | |
189 | g_free (filename); |
190 | } else { |
191 | #ifndef USE_MAEMO |
192 | errorf(_("Error accessing the file.")); |
193 | #else |
194 | errorf(_("Error accessing the file. This may happen if the file " |
195 | "resides on a remote file system. Please copy the file onto " |
196 | "the device (e.g. onto the memory card) and try again.")); |
197 | #endif |
198 | } |
199 | } |
200 | |
201 | gtk_widget_destroy (dialog); |
202 | |
203 | return gpx; |
204 | } |
205 | |
206 | /******************** begin of cachelist ********************/ |
207 | |
208 | enum { |
209 | CACHELIST_COL_TYPE = 0, |
210 | CACHELIST_COL_ID, |
211 | CACHELIST_COL_NAME, |
212 | CACHELIST_COL_SIZE, |
213 | CACHELIST_COL_RATING, |
214 | CACHELIST_COL_BEARING, |
215 | CACHELIST_COL_DISTANCE, |
216 | CACHELIST_COL_DATA, |
217 | CACHELIST_COL_AVAIL, |
218 | CACHELIST_COL_ARCHIVE, |
219 | CACHELIST_NUM_COLS |
220 | } ; |
221 | |
222 | void cachelist_goto_cache(appdata_t *appdata, cache_t *cache) { |
223 | #if !defined(USE_BREAD_CRUMB_TRAIL) && !defined(BCT) |
224 | cache_dialog(appdata, cache); |
225 | #else |
226 | crumb_add(appdata, cache->name, CRUMB_CACHE, cache); |
227 | |
228 | gtk_container_remove(GTK_CONTAINER(appdata->vbox), appdata->cur_view); |
229 | appdata->cur_view = cache_view(appdata, cache); |
230 | gtk_box_pack_start_defaults(GTK_BOX(appdata->vbox), appdata->cur_view); |
231 | gtk_widget_show_all(appdata->vbox); |
232 | #endif |
233 | } |
234 | |
235 | static void cachelist_view_onRowActivated(GtkTreeView *treeview, |
236 | GtkTreePath *path, |
237 | GtkTreeViewColumn *col, |
238 | gpointer userdata) { |
239 | appdata_t *appdata = (appdata_t*)userdata; |
240 | GtkTreeIter iter; |
241 | GtkTreeModel *model = gtk_tree_view_get_model(treeview); |
242 | |
243 | #ifdef USE_MAEMO |
244 | /* check if a cache is already selected and ignore click if yes */ |
245 | /* (was probably a double click) */ |
246 | if(appdata->cur_cache) return; |
247 | #endif |
248 | |
249 | if(gtk_tree_model_get_iter(model, &iter, path)) { |
250 | cache_t *cache; |
251 | gtk_tree_model_get(model, &iter, CACHELIST_COL_DATA, &cache, -1); |
252 | cachelist_goto_cache(appdata, cache); |
253 | } |
254 | } |
255 | |
256 | typedef struct { |
257 | appdata_t *appdata; |
258 | GtkTreePath *path; |
259 | gboolean done; |
260 | } cachelist_expose_t; |
261 | |
262 | static gboolean cachelist_expose(GtkWidget *widget, GdkEventExpose *event, |
263 | gpointer user_data) { |
264 | cachelist_expose_t *ce = (cachelist_expose_t*)user_data; |
265 | |
266 | if(event->type == GDK_EXPOSE) { |
267 | if(ce->path && !ce->done) { |
268 | gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(widget), |
269 | ce->path, NULL, TRUE, 0.5, 0.5); |
270 | gtk_tree_path_free(ce->path); |
271 | ce->done = TRUE; |
272 | } |
273 | } |
274 | |
275 | return FALSE; |
276 | } |
277 | |
278 | static void cachelist_destroy(GtkWidget *widget, gpointer user_data) { |
279 | cachelist_expose_t *ce = (cachelist_expose_t*)user_data; |
280 | |
281 | printf("cachelist timer removed\n"); |
282 | g_assert(ce->appdata->cachelist_handler_id); |
283 | gtk_timeout_remove(ce->appdata->cachelist_handler_id); |
284 | ce->appdata->cachelist_handler_id = 0; |
285 | |
286 | free(user_data); |
287 | } |
288 | |
289 | #define CACHELIST_UPDATE_TIMEOUT (30000) |
290 | |
291 | static GtkWidget *cachelist_create(appdata_t *appdata, gpx_t *gpx, |
292 | cache_t *sel_cache); |
293 | |
294 | void cachelist_redraw(appdata_t *appdata) { |
295 | if(!appdata->cur_view) { |
296 | printf("cachelist redraw: no active view\n"); |
297 | return; |
298 | } |
299 | |
300 | g_assert(!appdata->cur_cache); |
301 | int redraw = 0; |
302 | if(appdata->search_results) |
303 | redraw = 1; |
304 | else { |
305 | if(appdata->cur_gpx) |
306 | redraw = 2; // redraw cachelist |
307 | } |
308 | |
309 | if(redraw) { |
310 | GtkWidget *container = appdata->vbox; |
311 | |
312 | #ifdef USE_STACKABLE_WINDOW |
313 | HildonWindowStack *stack = hildon_window_stack_get_default(); |
314 | container = hildon_window_stack_peek(stack); |
315 | #endif |
316 | |
317 | gtk_container_remove(GTK_CONTAINER(container), appdata->cur_view); |
318 | switch(redraw) { |
319 | case 1: |
320 | appdata->cur_view = cachelist_create(appdata, |
321 | appdata->search_results, NULL); |
322 | break; |
323 | case 2: |
324 | appdata->cur_view = cachelist_create(appdata, |
325 | appdata->cur_gpx, NULL); |
326 | break; |
327 | } |
328 | |
329 | #ifdef USE_STACKABLE_WINDOW |
330 | if(container != appdata->vbox) |
331 | gtk_container_add(GTK_CONTAINER(container), appdata->cur_view); |
332 | else |
333 | #endif |
334 | gtk_box_pack_start_defaults(GTK_BOX(container), appdata->cur_view); |
335 | |
336 | gtk_widget_show_all(container); |
337 | } |
338 | } |
339 | |
340 | |
341 | static gboolean cachelist_update(gpointer data) { |
342 | |
343 | printf("cachelist timer fired!\n"); |
344 | |
345 | appdata_t *appdata = (appdata_t*)data; |
346 | |
347 | if(appdata->cur_cache) |
348 | return TRUE; |
349 | |
350 | #ifdef USE_MAEMO |
351 | if(appdata->cachelist_disable_screensaver) |
352 | if (osso_display_blanking_pause(appdata->osso_context) != OSSO_OK) |
353 | fprintf(stderr, "error with display blank\n"); |
354 | #endif |
355 | |
356 | if(appdata->cachelist_update) |
357 | cachelist_redraw(appdata); |
358 | |
359 | return TRUE; |
360 | } |
361 | |
362 | static void cachelist_timer_reset(appdata_t *appdata) { |
363 | printf("cachelist timer reset\n"); |
364 | g_assert(appdata->cachelist_handler_id); |
365 | gtk_timeout_remove(appdata->cachelist_handler_id); |
366 | appdata->cachelist_handler_id = |
367 | gtk_timeout_add(CACHELIST_UPDATE_TIMEOUT, cachelist_update, appdata); |
368 | } |
369 | |
370 | static gboolean cachelist_update_reset0(GtkWidget *widget, |
371 | GdkEventButton *event, |
372 | gpointer user_data) { |
373 | cachelist_timer_reset((appdata_t*)user_data); |
374 | return FALSE; |
375 | } |
376 | |
377 | static void cachelist_update_reset1(GtkAdjustment *adj, |
378 | gpointer user_data) { |
379 | cachelist_timer_reset((appdata_t*)user_data); |
380 | } |
381 | |
382 | static GtkWidget *cachelist_create(appdata_t *appdata, gpx_t *gpx, |
383 | cache_t *sel_cache) { |
384 | GtkCellRenderer *renderer; |
385 | GtkWidget *view; |
386 | GtkListStore *store; |
387 | GtkTreeIter iter; |
388 | |
389 | if(!gpx->notes_loaded) { |
390 | notes_load_all(appdata, gpx); |
391 | gpx->notes_loaded = TRUE; |
392 | } |
393 | |
394 | appdata->cur_items = appdata->cachelist_items; |
395 | |
396 | /* first sort the caches */ |
397 | pos_t *refpos = get_pos(appdata); |
398 | gpx_sort(gpx, GPX_SORT_BY_DISTANCE, refpos); |
399 | |
400 | view = gtk_tree_view_new(); |
401 | |
402 | /* --- "Type" column --- */ |
403 | renderer = custom_cell_renderer_type_new(); |
404 | gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(view), |
405 | -1, "Type", renderer, "type", CACHELIST_COL_TYPE, NULL); |
406 | |
407 | /* --- "Id" column --- */ |
408 | if(appdata->cachelist_items & CACHELIST_ITEM_ID) { |
409 | renderer = gtk_cell_renderer_text_new(); |
410 | gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(view), |
411 | -1, "Id", renderer, "text", CACHELIST_COL_ID, NULL); |
412 | } |
413 | |
414 | /* --- "Name" column --- */ |
415 | renderer = gtk_cell_renderer_text_new(); |
416 | g_object_set(renderer, "ellipsize", PANGO_ELLIPSIZE_END, NULL ); |
417 | |
418 | GtkTreeViewColumn *column = gtk_tree_view_column_new_with_attributes( |
419 | "Name", renderer, "text", CACHELIST_COL_NAME, NULL); |
420 | gtk_tree_view_column_set_expand(column, TRUE); |
421 | gtk_tree_view_insert_column(GTK_TREE_VIEW(view), column, -1); |
422 | |
423 | g_object_set(renderer, "foreground", "#ff0000", NULL ); |
424 | gtk_tree_view_column_add_attribute(column, renderer, "strikethrough", |
425 | CACHELIST_COL_AVAIL); |
426 | gtk_tree_view_column_add_attribute(column, renderer, |
427 | "foreground-set", CACHELIST_COL_ARCHIVE); |
428 | |
429 | /* --- "Size" column --- */ |
430 | if(appdata->cachelist_items & CACHELIST_ITEM_SIZE) { |
431 | renderer = custom_cell_renderer_size_new(); |
432 | gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(view), |
433 | -1, "Size", renderer, "size", CACHELIST_COL_SIZE, NULL); |
434 | } |
435 | |
436 | /* --- "Rating" column --- */ |
437 | if(appdata->cachelist_items & CACHELIST_ITEM_RATING) { |
438 | renderer = custom_cell_renderer_rating_new(); |
439 | gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(view), |
440 | -1, "Rating", renderer, "rating", CACHELIST_COL_RATING, NULL); |
441 | } |
442 | |
443 | /* --- "Bearing" column --- */ |
444 | renderer = gtk_cell_renderer_pixbuf_new(); |
445 | gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(view), |
446 | -1, "Bearing", renderer, "pixbuf", CACHELIST_COL_BEARING, NULL); |
447 | |
448 | /* --- "Distance" column --- */ |
449 | renderer = gtk_cell_renderer_text_new(); |
450 | gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(view), |
451 | -1, "Distance", renderer, "text", CACHELIST_COL_DISTANCE, NULL); |
452 | |
453 | store = gtk_list_store_new(CACHELIST_NUM_COLS, G_TYPE_INT, |
454 | G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INT, |
455 | G_TYPE_INT, GDK_TYPE_PIXBUF, G_TYPE_STRING, |
456 | G_TYPE_POINTER, G_TYPE_BOOLEAN, |
457 | G_TYPE_BOOLEAN); |
458 | |
459 | GtkTreeSelection *sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(view)); |
460 | |
461 | GtkTreeIter sel_iter; |
462 | gboolean sel_iter_valid = FALSE; |
463 | GtkTreePath *path = NULL; |
464 | cache_t *cache = gpx->cache; |
465 | while(cache) { |
466 | char str[32]; |
467 | gpx_pos_get_distance_str(str, sizeof(str), |
468 | *refpos, gpx_cache_pos(cache), appdata->imperial); |
469 | |
470 | int dint = (int)(cache->difficulty*2-2); |
471 | if(dint < 0) dint = 0; |
472 | if(dint > 8) dint = 8; |
473 | |
474 | int tint = (int)(cache->terrain*2-2); |
475 | if(tint < 0) tint = 0; |
476 | if(tint > 8) tint = 8; |
477 | |
478 | /* cache type includes "solved" flag in lowest bit */ |
479 | int type = cache->type<<8; |
480 | if(cache->notes) type |= 4; |
481 | if(cache->notes && cache->notes->override) type |= 1; |
482 | if(cache->notes && cache->notes->found) type |= 2; |
483 | if(cache->found) type |= 2; |
484 | if(cache->mine) type |= 8; |
485 | |
486 | if((!(type & 2)) || !appdata->cachelist_hide_found) { |
487 | |
488 | /* Append a row and fill in some data */ |
489 | gtk_list_store_append (store, &iter); |
490 | |
491 | gtk_list_store_set(store, &iter, |
492 | CACHELIST_COL_TYPE, type, |
493 | CACHELIST_COL_ID, cache->id, |
494 | CACHELIST_COL_NAME, cache->name, |
495 | CACHELIST_COL_SIZE, cache->container, |
496 | CACHELIST_COL_RATING, 100 * dint + tint, |
497 | CACHELIST_COL_BEARING, |
498 | icon_bearing(*refpos, gpx_cache_pos(cache)), |
499 | CACHELIST_COL_DISTANCE, str, |
500 | CACHELIST_COL_DATA, cache, |
501 | CACHELIST_COL_AVAIL, !cache->available || |
502 | cache->archived, |
503 | CACHELIST_COL_ARCHIVE, cache->archived, |
504 | -1); |
505 | |
506 | if(cache == sel_cache) { |
507 | sel_iter = iter; |
508 | sel_iter_valid = TRUE; |
509 | } |
510 | } |
511 | |
512 | cache = cache->next; |
513 | } |
514 | |
515 | gtk_tree_view_set_model(GTK_TREE_VIEW(view), GTK_TREE_MODEL(store)); |
516 | g_object_unref(store); |
517 | |
518 | if(sel_iter_valid) { |
519 | gtk_tree_selection_select_iter(sel, &sel_iter); |
520 | path = gtk_tree_model_get_path(GTK_TREE_MODEL(store), &sel_iter); |
521 | } |
522 | |
523 | /* make list react on clicks */ |
524 | g_signal_connect(view, "row-activated", |
525 | (GCallback)cachelist_view_onRowActivated, appdata); |
526 | |
527 | cachelist_expose_t *ce = malloc(sizeof(cachelist_expose_t)); |
528 | ce->appdata = appdata; |
529 | ce->path = path; |
530 | ce->done = FALSE; |
531 | |
532 | g_signal_connect(view, "expose-event", |
533 | (GCallback)cachelist_expose, ce); |
534 | g_signal_connect(view, "destroy", |
535 | (GCallback)cachelist_destroy, ce); |
536 | |
537 | /* put this inside a scrolled view */ |
538 | #ifndef USE_PANNABLE_AREA |
539 | GtkWidget *scrolled_window = gtk_scrolled_window_new (NULL, NULL); |
540 | gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window), |
541 | GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC); |
542 | gtk_container_add(GTK_CONTAINER(scrolled_window), view); |
543 | #else |
544 | GtkWidget *pannable_area = hildon_pannable_area_new(); |
545 | |
546 | gtk_container_add(GTK_CONTAINER(pannable_area), view); |
547 | #endif |
548 | |
549 | /* add a timer for automatic update */ |
550 | g_assert(!appdata->cachelist_handler_id); |
551 | appdata->cachelist_handler_id = |
552 | gtk_timeout_add(CACHELIST_UPDATE_TIMEOUT, cachelist_update, appdata); |
553 | |
554 | /* update timer is being reset if the user scrolls or selects */ |
555 | g_signal_connect(view, "button-press-event", |
556 | (GCallback)cachelist_update_reset0, appdata); |
557 | |
558 | #ifndef USE_PANNABLE_AREA |
559 | g_signal_connect(gtk_scrolled_window_get_vadjustment( |
560 | GTK_SCROLLED_WINDOW(scrolled_window)), |
561 | "value-changed", |
562 | (GCallback)cachelist_update_reset1, appdata); |
563 | |
564 | return scrolled_window; |
565 | #else |
566 | g_signal_connect(hildon_pannable_area_get_vadjustment( |
567 | HILDON_PANNABLE_AREA(pannable_area)), |
568 | "value-changed", |
569 | (GCallback)cachelist_update_reset1, appdata); |
570 | |
571 | |
572 | return pannable_area; |
573 | #endif |
574 | } |
575 | |
576 | #ifndef USE_MAEMO |
577 | void cachelist_dialog(appdata_t *appdata, gpx_t *gpx) { |
578 | GtkWidget *dialog = |
579 | gtk_dialog_new_with_buttons(gpx->name, GTK_WINDOW(appdata->window), |
580 | GTK_DIALOG_NO_SEPARATOR | GTK_DIALOG_MODAL | |
581 | GTK_DIALOG_DESTROY_WITH_PARENT, |
582 | GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE, |
583 | NULL); |
584 | |
585 | gtk_window_set_default_size(GTK_WINDOW(dialog), DIALOG_WIDTH, DIALOG_HEIGHT); |
586 | |
587 | gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), |
588 | cachelist_create(appdata, gpx, NULL)); |
589 | |
590 | gtk_widget_show_all(dialog); |
591 | |
592 | gtk_dialog_run(GTK_DIALOG(dialog)); |
593 | gtk_widget_destroy(dialog); |
594 | } |
595 | #else |
596 | #ifdef USE_STACKABLE_WINDOW |
597 | static void search_result_free(gpx_t *result); |
598 | |
599 | void on_cachelist_destroy(GtkWidget *widget, appdata_t *appdata) { |
600 | printf("cachelist destroy\n"); |
601 | |
602 | HildonWindowStack *stack = hildon_window_stack_get_default(); |
603 | appdata->window = HILDON_WINDOW(hildon_window_stack_peek(stack)); |
604 | |
605 | if(appdata->search_results) { |
606 | search_result_free(appdata->search_results); |
607 | appdata->search_results = NULL; |
608 | } |
609 | appdata->cur_gpx = NULL; |
610 | |
611 | #ifdef ENABLE_OSM_GPS_MAP |
612 | map_update(appdata); |
613 | #endif |
614 | |
615 | /* restore cur_view */ |
616 | appdata->cur_view = g_object_get_data(G_OBJECT(widget), "cur_view"); |
617 | } |
618 | |
619 | void cachelist_dialog(appdata_t *appdata, gpx_t *gpx) { |
620 | GtkWidget *window = hildon_stackable_window_new(); |
621 | appdata->window = HILDON_WINDOW(window); |
622 | |
623 | /* store last "cur_view" in window */ |
624 | g_object_set_data(G_OBJECT(window), "cur_view", appdata->cur_view); |
625 | |
626 | appdata->cur_gpx = gpx; |
627 | char *title = g_strdup_printf("%s - GPXView", gpx->name); |
628 | gtk_window_set_title(GTK_WINDOW(window), title); |
629 | g_free(title); |
630 | |
631 | appdata->cur_view = cachelist_create(appdata, gpx, NULL); |
632 | gtk_container_add(GTK_CONTAINER(window), appdata->cur_view); |
633 | |
634 | |
635 | hildon_window_set_app_menu(HILDON_WINDOW(window), |
636 | menu_create(appdata, MENU_CACHELIST)); |
637 | |
638 | g_signal_connect(G_OBJECT(window), "destroy", |
639 | G_CALLBACK(on_cachelist_destroy), appdata); |
640 | |
641 | gtk_widget_show_all(window); |
642 | |
643 | #ifdef ENABLE_OSM_GPS_MAP |
644 | map_update(appdata); |
645 | #endif |
646 | } |
647 | #endif |
648 | #endif |
649 | |
650 | /******************** end of cachelist ********************/ |
651 | |
652 | /******************** begin of gpxlist ********************/ |
653 | |
654 | enum { |
655 | GPXLIST_COL_ICON = 0, |
656 | GPXLIST_COL_FILENAME, |
657 | GPXLIST_COL_NAME, |
658 | GPXLIST_COL_DATE, |
659 | GPXLIST_COL_CACHES, |
660 | GPXLIST_COL_OPEN, |
661 | #ifdef USE_PANNABLE_AREA |
662 | GPXLIST_COL_DELETE, |
663 | #endif |
664 | GPXLIST_COL_DATA, |
665 | GPXLIST_NUM_COLS |
666 | } ; |
667 | |
668 | static GdkPixbuf *gpx_icon_get(gpx_t *gpx) { |
669 | if(gpx->filename && g_file_test(gpx->filename, G_FILE_TEST_IS_DIR)) |
670 | return icon_get(ICON_FILE, 1); |
671 | |
672 | if(gpx->filename&& !strcasecmp(gpx->filename+strlen(gpx->filename)-4,".zip")) |
673 | return icon_get(ICON_FILE, 2); |
674 | |
675 | return icon_get(ICON_FILE, 0); |
676 | } |
677 | |
678 | void gpxlist_set(GtkListStore *store, GtkTreeIter *iter, gpx_t *gpx) { |
679 | char date_str[32], cnum[32]; |
680 | |
681 | if(gpx->year && gpx->month && gpx->day) { |
682 | GDate *date = g_date_new_dmy(gpx->day, gpx->month, gpx->year); |
683 | g_date_strftime(date_str, sizeof(date_str), "%x", date); |
684 | g_date_free(date); |
685 | } else |
686 | strcpy(date_str, "---"); |
687 | |
688 | char *fname = strrchr(gpx->filename, '/'); |
689 | if(!fname) fname = gpx->filename; |
690 | else fname++; /* skip '/' */ |
691 | |
692 | snprintf(cnum, sizeof(cnum), "%d", gpx_total_caches(gpx)); |
693 | |
694 | /* Append a row and fill in some data */ |
695 | gtk_list_store_set(store, iter, |
696 | GPXLIST_COL_ICON, gpx_icon_get(gpx), |
697 | GPXLIST_COL_FILENAME, fname, |
698 | GPXLIST_COL_NAME, gpx->name, |
699 | GPXLIST_COL_DATE, gpx->closed?NULL:date_str, |
700 | GPXLIST_COL_OPEN, !gpx->closed, |
701 | GPXLIST_COL_CACHES, gpx->closed?NULL:cnum, |
702 | #ifdef USE_PANNABLE_AREA |
703 | GPXLIST_COL_DELETE, icon_get(ICON_MISC, 4), |
704 | #endif |
705 | GPXLIST_COL_DATA, gpx, |
706 | -1); |
707 | } |
708 | |
709 | static void gpxlist_remove(appdata_t *appdata, |
710 | GtkListStore *store, GtkTreeIter *iter, |
711 | gpx_t *gpx) { |
712 | |
713 | printf("removing %s\n", gpx->name); |
714 | |
715 | /* de-chain */ |
716 | gpx_t **prev = &appdata->gpx; |
717 | while(*prev != gpx) prev = &((*prev)->next); |
718 | *prev = gpx->next; |
719 | |
720 | /* remove gconf entry if file was closed */ |
721 | gconf_remove_closed_name(appdata, gpx->filename); |
722 | |
723 | /* free gpx itself */ |
724 | gpx_free(gpx); |
725 | |
726 | /* and remove from store */ |
727 | gtk_list_store_remove(store, iter); |
728 | } |
729 | |
730 | static void gpxlist_close(appdata_t *appdata, |
731 | GtkListStore *store, GtkTreeIter *iter, |
732 | gpx_t *gpx) { |
733 | |
734 | printf("closing %s\n", gpx->name); |
735 | |
736 | g_assert(!gpx->closed); |
737 | gpx->closed = TRUE; |
738 | |
739 | /* free all associated caches */ |
740 | gpx_free_caches(gpx); |
741 | |
742 | /* update entry */ |
743 | gpxlist_set(store, iter, gpx); |
744 | |
745 | /* save name in gconf so we know this has been closed */ |
746 | gconf_save_closed_name(appdata, gpx->filename, gpx->name); |
747 | } |
748 | |
749 | void gpxlist_goto_cachelist(appdata_t *appdata, gpx_t *gpx) { |
750 | #if !defined(USE_BREAD_CRUMB_TRAIL) && !defined(BCT) |
751 | #ifdef USE_STACKABLE_WINDOW |
752 | if(!appdata->cur_gpx) |
753 | #endif |
754 | cachelist_dialog(appdata, gpx); |
755 | #ifdef USE_STACKABLE_WINDOW |
756 | else |
757 | printf("selected gpx, but cachelist window already present\n"); |
758 | #endif |
759 | #else |
760 | gtk_container_remove(GTK_CONTAINER(appdata->vbox), appdata->cur_view); |
761 | appdata->cur_view = cachelist_create(appdata, gpx, NULL); |
762 | gtk_box_pack_start_defaults(GTK_BOX(appdata->vbox), appdata->cur_view); |
763 | gtk_widget_show_all(appdata->vbox); |
764 | |
765 | crumb_add(appdata, gpx->name, CRUMB_CACHELIST, gpx); |
766 | #endif |
767 | } |
768 | |
769 | static void gpxlist_view_onRowActivated(GtkTreeView *treeview, |
770 | GtkTreePath *path, |
771 | GtkTreeViewColumn *col, |
772 | gpointer userdata) { |
773 | appdata_t *appdata = (appdata_t*)userdata; |
774 | GtkTreeIter iter; |
775 | GtkTreeModel *model = gtk_tree_view_get_model(treeview); |
776 | |
777 | #ifdef USE_MAEMO |
778 | /* check if a cache is already selected and ignore click if yes */ |
779 | /* (was probably a double click) */ |
780 | if(appdata->cur_gpx) return; |
781 | #endif |
782 | |
783 | if (gtk_tree_model_get_iter(model, &iter, path)) { |
784 | gpx_t *gpx; |
785 | gtk_tree_model_get(model, &iter, GPXLIST_COL_DATA, &gpx, -1); |
786 | |
787 | #ifdef USE_PANNABLE_AREA |
788 | /* get name of column the user clicked on */ |
789 | const char *col_name = NULL; |
790 | if(col) col_name = gtk_tree_view_column_get_title(col); |
791 | |
792 | if(col_name && !strcmp(col_name, "Del")) { |
793 | printf("clicked delete\n"); |
794 | |
795 | #if !defined(USE_MAEMO) || (MAEMO_VERSION_MAJOR < 5) |
796 | /* ask user what he wants */ |
797 | GtkWidget *dialog = gtk_message_dialog_new( |
798 | GTK_WINDOW(appdata->window), |
799 | GTK_DIALOG_DESTROY_WITH_PARENT, |
800 | GTK_MESSAGE_QUESTION, |
801 | GTK_BUTTONS_CANCEL, |
802 | _("Do you want to close this entry only or do " |
803 | "you want to remove it completely from the list?")); |
804 | |
805 | gtk_dialog_add_buttons(GTK_DIALOG(dialog), |
806 | _("Remove"), 1, |
807 | _("Close"), 2, |
808 | NULL); |
809 | |
810 | gtk_window_set_title(GTK_WINDOW(dialog), _("Close or remove entry?")); |
811 | #else |
812 | |
813 | GtkWidget *dialog = |
814 | gtk_dialog_new_with_buttons(_("Close or remove entry?"), |
815 | GTK_WINDOW(appdata->window), |
816 | GTK_DIALOG_DESTROY_WITH_PARENT, |
817 | GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT, |
818 | _("Remove"), 1, |
819 | _("Close"), 2, |
820 | NULL); |
821 | |
822 | GtkWidget *content_area = |
823 | gtk_dialog_get_content_area (GTK_DIALOG (dialog)); |
824 | |
825 | GtkWidget *hbox = gtk_hbox_new(FALSE, 0); |
826 | |
827 | gtk_box_pack_start(GTK_BOX(hbox), |
828 | gtk_image_new_from_stock( GTK_STOCK_DIALOG_QUESTION, |
829 | GTK_ICON_SIZE_DIALOG), |
830 | FALSE, FALSE, 0); |
831 | |
832 | GtkWidget *label = gtk_label_new( |
833 | _("Do you want to close this entry only or do " |
834 | "you want to remove it completely from the list?")); |
835 | |
836 | gtk_label_set_line_wrap(GTK_LABEL(label), TRUE); |
837 | gtk_label_set_line_wrap_mode(GTK_LABEL(label), PANGO_WRAP_WORD); |
838 | |
839 | gtk_box_pack_start_defaults(GTK_BOX(hbox), label); |
840 | gtk_container_add (GTK_CONTAINER (content_area), hbox); |
841 | |
842 | gtk_widget_show_all (dialog); |
843 | #endif |
844 | |
845 | if(gpx->closed) |
846 | gtk_dialog_set_response_sensitive(GTK_DIALOG(dialog), 2, FALSE); |
847 | |
848 | /* set the active flag again if the user answered "no" */ |
849 | switch(gtk_dialog_run(GTK_DIALOG(dialog))) { |
850 | |
851 | case 1: |
852 | gpxlist_remove(appdata, GTK_LIST_STORE(model), &iter, gpx); |
853 | break; |
854 | |
855 | case 2: |
856 | gpxlist_close(appdata, GTK_LIST_STORE(model), &iter, gpx); |
857 | break; |
858 | |
859 | default: |
860 | break; |
861 | } |
862 | |
863 | gtk_widget_destroy(dialog); |
864 | |
865 | } else |
866 | #endif |
867 | { |
868 | |
869 | /* this gpx file may be closed. Since the user definitely wants */ |
870 | /* to use it, we just open it again */ |
871 | if(gpx->closed) { |
872 | gpx_dialog_t *dialog = |
873 | gpx_busy_dialog_new(GTK_WIDGET(appdata->window)); |
874 | gpx_t *new = NULL; |
875 | |
876 | if(g_file_test(gpx->filename, G_FILE_TEST_IS_DIR)) |
877 | new = gpx_parse_dir(dialog, gpx->filename, appdata->username); |
878 | else |
879 | new = gpx_parse(dialog, gpx->filename, appdata->username); |
880 | |
881 | if(new) { |
882 | gpx_t **prev = &(appdata->gpx); |
883 | while(*prev && *prev != gpx) |
884 | prev = &(*prev)->next; |
885 | |
886 | /* this entry _must_ be in the list */ |
887 | g_assert(*prev); |
888 | |
889 | /* replace gpx entry with the new "open" one */ |
890 | (*prev) = new; |
891 | new->next = gpx->next; |
892 | gpx->next = NULL; |
893 | |
894 | /* free old closed one */ |
895 | gpx_free(gpx); |
896 | |
897 | gpx = new; |
898 | |
899 | /* finally update the visible list */ |
900 | gpxlist_set(appdata->gpxstore, &iter, gpx); |
901 | |
902 | /* and remove gconf entry */ |
903 | gconf_remove_closed_name(appdata, gpx->filename); |
904 | |
905 | #ifndef USE_PANNABLE_AREA |
906 | gtk_widget_set_sensitive(appdata->menu_close, TRUE); |
907 | #endif |
908 | } else { |
909 | printf("unable to reopen file %s\n", gpx->filename); |
910 | return; |
911 | } |
912 | |
913 | gpx_busy_dialog_destroy(dialog); |
914 | } |
915 | |
916 | gpxlist_goto_cachelist(appdata, gpx); |
917 | } |
918 | } |
919 | } |
920 | |
921 | /* search gpx file in gpx list */ |
922 | gboolean gpxlist_find(appdata_t *appdata, GtkTreeIter *iter, gpx_t *gpx) { |
923 | GtkTreeModel *model = |
924 | gtk_tree_view_get_model(GTK_TREE_VIEW(appdata->gpxview)); |
925 | |
926 | gboolean found = |
927 | gtk_tree_model_get_iter_first(model, iter); |
928 | |
929 | while(found) { |
930 | gpx_t *this_gpx; |
931 | gtk_tree_model_get(model, iter, GPXLIST_COL_DATA, &this_gpx, -1); |
932 | |
933 | if(gpx == this_gpx) |
934 | return TRUE; |
935 | |
936 | found = gtk_tree_model_iter_next(model, iter); |
937 | } |
938 | |
939 | return FALSE; |
940 | } |
941 | |
942 | |
943 | #ifndef USE_PANNABLE_AREA |
944 | static gboolean |
945 | view_selection_func(GtkTreeSelection *selection, GtkTreeModel *model, |
946 | GtkTreePath *path, gboolean path_currently_selected, |
947 | gpointer userdata) { |
948 | appdata_t *appdata = (appdata_t*)userdata; |
949 | GtkTreeIter iter; |
950 | |
951 | if(gtk_tree_model_get_iter(model, &iter, path)) { |
952 | gpx_t *gpx; |
953 | gtk_tree_model_get(model, &iter, GPXLIST_COL_DATA, &gpx, -1); |
954 | |
955 | gtk_widget_set_sensitive(appdata->menu_remove, !path_currently_selected); |
956 | |
957 | if(!gpx->closed) |
958 | gtk_widget_set_sensitive(appdata->menu_close, !path_currently_selected); |
959 | } |
960 | |
961 | return TRUE; /* allow selection state to change */ |
962 | } |
963 | #endif |
964 | |
965 | static GtkWidget *gpxlist_create_view_and_model(appdata_t *appdata, |
966 | gpx_t *sel_gpx) { |
967 | gpx_t *gpx = appdata->gpx; |
968 | GtkCellRenderer *renderer; |
969 | |
970 | /* saved displayed items */ |
971 | appdata->cur_items = appdata->gpxlist_items; |
972 | |
973 | #ifndef USE_PANNABLE_AREA |
974 | /* nothing selected yet */ |
975 | gtk_widget_set_sensitive(appdata->menu_remove, FALSE); |
976 | gtk_widget_set_sensitive(appdata->menu_close, FALSE); |
977 | #endif |
978 | |
979 | appdata->gpxview = gtk_tree_view_new (); |
980 | |
981 | GtkTreeSelection *selection = |
982 | gtk_tree_view_get_selection(GTK_TREE_VIEW(appdata->gpxview)); |
983 | #ifndef USE_PANNABLE_AREA |
984 | gtk_tree_selection_set_select_function(selection, view_selection_func, |
985 | appdata, NULL); |
986 | #endif |
987 | |
988 | /* --- "Icon" column --- */ |
989 | renderer = gtk_cell_renderer_pixbuf_new(); |
990 | gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(appdata->gpxview), |
991 | -1, "Icon", renderer, |
992 | "pixbuf", GPXLIST_COL_ICON, |
993 | #ifdef USE_PANNABLE_AREA |
994 | /* at least one entry needs to be sensitive. */ |
995 | /* This is the delete icon if the PANNABLE_AREA is used */ |
996 | "sensitive", GPXLIST_COL_OPEN, |
997 | #endif |
998 | NULL); |
999 | |
1000 | /* --- "FileName" column --- */ |
1001 | if(appdata->gpxlist_items & GPXLIST_ITEM_FILENAME) { |
1002 | renderer = gtk_cell_renderer_text_new(); |
1003 | gtk_tree_view_insert_column_with_attributes( |
1004 | GTK_TREE_VIEW(appdata->gpxview), |
1005 | -1, "Filename", renderer, |
1006 | "text", GPXLIST_COL_FILENAME, |
1007 | "sensitive", GPXLIST_COL_OPEN, |
1008 | NULL); |
1009 | } |
1010 | |
1011 | /* --- "Name" column --- */ |
1012 | renderer = gtk_cell_renderer_text_new(); |
1013 | g_object_set(renderer, "ellipsize", PANGO_ELLIPSIZE_END, NULL ); |
1014 | |
1015 | GtkTreeViewColumn *column = gtk_tree_view_column_new_with_attributes( |
1016 | "Name", renderer, |
1017 | "text", GPXLIST_COL_NAME, |
1018 | "sensitive", GPXLIST_COL_OPEN, |
1019 | NULL); |
1020 | gtk_tree_view_column_set_expand(column, TRUE); |
1021 | gtk_tree_view_insert_column(GTK_TREE_VIEW(appdata->gpxview), column, -1); |
1022 | |
1023 | /* --- "Date" column --- */ |
1024 | if(appdata->gpxlist_items & GPXLIST_ITEM_DATE) { |
1025 | renderer = gtk_cell_renderer_text_new(); |
1026 | g_object_set(renderer, "xalign", 1.0, NULL ); |
1027 | gtk_tree_view_insert_column_with_attributes( |
1028 | GTK_TREE_VIEW(appdata->gpxview), |
1029 | -1, "Date", renderer, |
1030 | "text", GPXLIST_COL_DATE, |
1031 | "sensitive", GPXLIST_COL_OPEN, |
1032 | NULL); |
1033 | } |
1034 | |
1035 | /* --- "Number of caches" column --- */ |
1036 | if(appdata->gpxlist_items & GPXLIST_ITEM_CNUM) { |
1037 | renderer = gtk_cell_renderer_text_new(); |
1038 | g_object_set(renderer, "xalign", 1.0, NULL ); |
1039 | gtk_tree_view_insert_column_with_attributes( |
1040 | GTK_TREE_VIEW(appdata->gpxview), |
1041 | -1, "#Caches", renderer, |
1042 | "text", GPXLIST_COL_CACHES, |
1043 | "sensitive", GPXLIST_COL_OPEN, |
1044 | NULL); |
1045 | } |
1046 | |
1047 | #ifdef USE_PANNABLE_AREA |
1048 | /* --- "Delete" column --- */ |
1049 | renderer = gtk_cell_renderer_pixbuf_new(); |
1050 | gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(appdata->gpxview), |
1051 | -1, "Del", renderer, |
1052 | "pixbuf", GPXLIST_COL_DELETE, |
1053 | // "sensitive", GPXLIST_COL_OPEN, |
1054 | NULL); |
1055 | #endif |
1056 | |
1057 | /* build and fill the store */ |
1058 | appdata->gpxstore = gtk_list_store_new(GPXLIST_NUM_COLS, GDK_TYPE_PIXBUF, |
1059 | G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, |
1060 | G_TYPE_STRING, G_TYPE_BOOLEAN, |
1061 | #ifdef USE_PANNABLE_AREA |
1062 | GDK_TYPE_PIXBUF, |
1063 | #endif |
1064 | G_TYPE_POINTER); |
1065 | |
1066 | GtkTreePath *path = NULL; |
1067 | GtkTreeIter sel_iter; |
1068 | gboolean sel_iter_valid = FALSE; |
1069 | while(gpx) { |
1070 | GtkTreeIter iter; |
1071 | gtk_list_store_append(appdata->gpxstore, &iter); |
1072 | gpxlist_set(appdata->gpxstore, &iter, gpx); |
1073 | |
1074 | if(gpx == sel_gpx) { |
1075 | sel_iter = iter; |
1076 | sel_iter_valid = TRUE; |
1077 | } |
1078 | |
1079 | gpx = gpx->next; |
1080 | } |
1081 | |
1082 | gtk_tree_view_set_model(GTK_TREE_VIEW(appdata->gpxview), |
1083 | GTK_TREE_MODEL(appdata->gpxstore)); |
1084 | |
1085 | g_object_unref(appdata->gpxstore); |
1086 | |
1087 | if(sel_iter_valid) { |
1088 | gtk_tree_selection_select_iter(selection, &sel_iter); |
1089 | path = gtk_tree_model_get_path(GTK_TREE_MODEL(appdata->gpxstore), |
1090 | &sel_iter); |
1091 | gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(appdata->gpxview), |
1092 | path, NULL, TRUE, 0.0, 0.0); |
1093 | gtk_tree_path_free(path); |
1094 | } |
1095 | |
1096 | /* make list react on clicks */ |
1097 | g_signal_connect(appdata->gpxview, "row-activated", |
1098 | (GCallback)gpxlist_view_onRowActivated, appdata); |
1099 | |
1100 | /* put this inside a scrolled view */ |
1101 | #ifndef USE_PANNABLE_AREA |
1102 | GtkWidget *scrolled_window = gtk_scrolled_window_new(NULL, NULL); |
1103 | gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window), |
1104 | GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC); |
1105 | gtk_container_add(GTK_CONTAINER(scrolled_window), appdata->gpxview); |
1106 | |
1107 | return scrolled_window; |
1108 | #else |
1109 | GtkWidget *pannable_area = hildon_pannable_area_new(); |
1110 | gtk_container_add(GTK_CONTAINER(pannable_area), appdata->gpxview); |
1111 | |
1112 | return pannable_area; |
1113 | #endif |
1114 | } |
1115 | |
1116 | /* add last entry in gpx list to visual representation */ |
1117 | void gpxlist_add(appdata_t *appdata, gpx_t *new) { |
1118 | GtkTreeIter iter; |
1119 | |
1120 | gtk_list_store_append(appdata->gpxstore, &iter); |
1121 | gpxlist_set(appdata->gpxstore, &iter, new); |
1122 | |
1123 | /* and attach entry to end of list */ |
1124 | gpx_t **gpx = &appdata->gpx; |
1125 | while(*gpx) gpx = &((*gpx)->next); |
1126 | *gpx = new; |
1127 | |
1128 | /* select new iter */ |
1129 | GtkTreeSelection *selection = |
1130 | gtk_tree_view_get_selection(GTK_TREE_VIEW(appdata->gpxview)); |
1131 | gtk_tree_selection_select_iter(selection, &iter); |
1132 | GtkTreePath *path = |
1133 | gtk_tree_model_get_path(GTK_TREE_MODEL(appdata->gpxstore), &iter); |
1134 | gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(appdata->gpxview), |
1135 | path, NULL, TRUE, 0.0, 0.0); |
1136 | gtk_tree_path_free(path); |
1137 | } |
1138 | |
1139 | /******************** end of gpxlist ********************/ |
1140 | |
1141 | /******************** begin of menu *********************/ |
1142 | |
1143 | static void |
1144 | cb_menu_about(GtkWidget *window, gpointer data) { |
1145 | about_box((appdata_t*)data); |
1146 | } |
1147 | |
1148 | #if defined(USE_MAEMO) && defined(HILDON_HELP) |
1149 | static void |
1150 | cb_menu_help(GtkWidget *window, gpointer data) { |
1151 | appdata_t *appdata = (appdata_t*)data; |
1152 | |
1153 | hildon_help_show(appdata->osso_context, HELP_ID_INTRO, 0); |
1154 | } |
1155 | #endif |
1156 | |
1157 | static void |
1158 | cb_menu_add(GtkWidget *window, gpointer data) { |
1159 | appdata_t *appdata = (appdata_t *)data; |
1160 | |
1161 | gpx_t *new = choose_file(appdata, FALSE); |
1162 | if(new) gpxlist_add(appdata, new); |
1163 | } |
1164 | |
1165 | static void |
1166 | cb_menu_adddir(GtkWidget *window, gpointer data) { |
1167 | appdata_t *appdata = (appdata_t *)data; |
1168 | |
1169 | gpx_t *new = choose_file(appdata, TRUE); |
1170 | if(new) gpxlist_add(appdata, new); |
1171 | } |
1172 | |
1173 | #ifndef USE_PANNABLE_AREA |
1174 | static void |
1175 | cb_menu_close(GtkWidget *window, gpointer data) { |
1176 | appdata_t *appdata = (appdata_t *)data; |
1177 | GtkTreeSelection *selection; |
1178 | GtkTreeModel *model; |
1179 | GtkTreeIter iter; |
1180 | |
1181 | printf("selected close\n"); |
1182 | |
1183 | /* the entry cannot be closed again */ |
1184 | gtk_widget_set_sensitive(appdata->menu_close, FALSE); |
1185 | |
1186 | selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(appdata->gpxview)); |
1187 | |
1188 | printf("gpxlist close\n"); |
1189 | |
1190 | if (gtk_tree_selection_get_selected(selection, &model, &iter)) { |
1191 | gpx_t *gpx = NULL; |
1192 | gtk_tree_model_get(model, &iter, GPXLIST_COL_DATA, &gpx, -1); |
1193 | |
1194 | if(gpx) gpxlist_close(appdata, GTK_LIST_STORE(model), &iter, gpx); |
1195 | } else { |
1196 | g_print ("no row selected.\n"); |
1197 | } |
1198 | } |
1199 | |
1200 | static void |
1201 | cb_menu_remove(GtkWidget *window, gpointer data) { |
1202 | appdata_t *appdata = (appdata_t *)data; |
1203 | |
1204 | /* disable menu item */ |
1205 | gtk_widget_set_sensitive(appdata->menu_remove, FALSE); |
1206 | gtk_widget_set_sensitive(appdata->menu_close, FALSE); |
1207 | |
1208 | GtkTreeModel *model; |
1209 | GtkTreeIter iter; |
1210 | GtkTreeSelection *selection = |
1211 | gtk_tree_view_get_selection(GTK_TREE_VIEW(appdata->gpxview)); |
1212 | |
1213 | printf("gpxlist remove\n"); |
1214 | |
1215 | if(gtk_tree_selection_get_selected(selection, &model, &iter)) { |
1216 | gpx_t *gpx = NULL; |
1217 | gtk_tree_model_get(model, &iter, GPXLIST_COL_DATA, &gpx, -1); |
1218 | |
1219 | if(gpx) gpxlist_remove(appdata, GTK_LIST_STORE(model), &iter, gpx); |
1220 | } else { |
1221 | g_print ("no row selected.\n"); |
1222 | } |
1223 | } |
1224 | |
1225 | #endif // !USE_PANNABLE_AREA |
1226 | |
1227 | static void search_result_free(gpx_t *result) { |
1228 | printf("freeing search results\n"); |
1229 | |
1230 | /* free found chain */ |
1231 | cache_t *cache = result->cache; |
1232 | while(cache) { |
1233 | cache_t *next = cache->next; |
1234 | free(cache); |
1235 | cache = next; |
1236 | } |
1237 | free(result->name); |
1238 | free(result); |
1239 | } |
1240 | |
1241 | #define MAX_HITS 50 |
1242 | |
1243 | static time_t localize_time(time_t in) { |
1244 | time_t ret; |
1245 | char *tz; |
1246 | struct tm *tm = localtime(&in); |
1247 | |
1248 | tz = getenv("TZ"); |
1249 | setenv("TZ", "", 1); |
1250 | tzset(); |
1251 | ret = mktime(tm); |
1252 | if (tz) |
1253 | setenv("TZ", tz, 1); |
1254 | else |
1255 | unsetenv("TZ"); |
1256 | tzset(); |
1257 | return ret; |
1258 | } |
1259 | |
1260 | static int days_ago(time_t in) { |
1261 | int day_in = localize_time(in) / (60*60*24); |
1262 | int day_now = localize_time(time(NULL)) / (60*60*24); |
1263 | |
1264 | return day_now - day_in; |
1265 | } |
1266 | |
1267 | gpx_t *search_do(appdata_t *appdata, gpx_t *gpx, char *phrase, |
1268 | int what, gboolean local) { |
1269 | /* walk through all caches */ |
1270 | |
1271 | int hits = 0; |
1272 | gpx_t *found = malloc(sizeof(gpx_t)); |
1273 | memset(found, 0, sizeof(gpx_t)); |
1274 | cache_t **cacheP = &(found->cache); |
1275 | |
1276 | if(what & SEARCH_FINDS) { |
1277 | time_t loc_now = localize_time(time(NULL)); |
1278 | printf("now: %ld days since 1/1/1970, days hour is %ld\n", |
1279 | loc_now/(60*60*24), loc_now%(60*60*24)/(60*60)); |
1280 | } |
1281 | |
1282 | while(gpx && hits < MAX_HITS) { |
1283 | |
1284 | /* we need all notes ... */ |
1285 | if(what & SEARCH_FINDS) { |
1286 | notes_load_all(appdata, gpx); |
1287 | gpx->notes_loaded = TRUE; |
1288 | } |
1289 | |
1290 | cache_t *cache = gpx->cache; |
1291 | |
1292 | while(cache && hits < MAX_HITS) { |
1293 | gboolean hit = FALSE; |
1294 | |
1295 | if(what & SEARCH_FINDS) { |
1296 | if(cache->notes && cache->notes->found ) { |
1297 | int days = days_ago(cache->notes->ftime); |
1298 | |
1299 | if(cache->id) |
1300 | printf("find of %s is %d days ago\n", cache->id, days); |
1301 | |
1302 | if(days <= appdata->search_days) |
1303 | hit = 1; |
1304 | } |
1305 | } else if(cache->id && (what & SEARCH_ID) && |
1306 | strcasestr(cache->id, phrase)) |
1307 | hit = 1; |
1308 | else if(cache->name && (what & SEARCH_NAME) && |
1309 | strcasestr(cache->name, phrase)) |
1310 | hit = 1; |
1311 | else if(cache->short_description && (what & SEARCH_DESC) && |
1312 | strcasestr(cache->short_description, phrase)) |
1313 | hit = 1; |
1314 | else if(cache->long_description && (what & SEARCH_DESC) && |
1315 | strcasestr(cache->long_description, phrase)) |
1316 | hit = 1; |
1317 | else if(cache->owner && cache->owner->name && (what & SEARCH_OWNER) && |
1318 | strcasestr(cache->owner->name, phrase)) |
1319 | hit = 1; |
1320 | |
1321 | if(hit) { |
1322 | /* chain a copy of this cache structure into the found list */ |
1323 | *cacheP = malloc(sizeof(cache_t)); |
1324 | memcpy(*cacheP, cache, sizeof(cache_t)); |
1325 | (*cacheP)->next = NULL; |
1326 | cacheP = &((*cacheP)->next); |
1327 | hits++; |
1328 | } |
1329 | cache = cache->next; |
1330 | } |
1331 | |
1332 | if(!local) gpx = gpx->next; |
1333 | else gpx = NULL; /* local search within one gpx only */ |
1334 | } |
1335 | |
1336 | found->name = strdup(_("Search results")); |
1337 | |
1338 | return found; |
1339 | } |
1340 | |
1341 | typedef struct { |
1342 | appdata_t *appdata; |
1343 | GtkWidget *entry, *spinner; |
1344 | GtkWidget *in_id, *in_name, *in_desc, *in_owner, *in_finds; |
1345 | } search_context_t; |
1346 | |
1347 | |
1348 | static GtkWidget *check_button_new_with_label(char *label) { |
1349 | #if !defined(USE_MAEMO) || (MAEMO_VERSION_MAJOR < 5) |
1350 | return gtk_check_button_new_with_label(label); |
1351 | #else |
1352 | GtkWidget *cbut = |
1353 | hildon_check_button_new(HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH); |
1354 | gtk_button_set_label(GTK_BUTTON(cbut), label); |
1355 | return cbut; |
1356 | #endif |
1357 | } |
1358 | |
1359 | static void check_button_set_active(GtkWidget *button, gboolean active) { |
1360 | #if !defined(USE_MAEMO) || (MAEMO_VERSION_MAJOR < 5) |
1361 | gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), active); |
1362 | #else |
1363 | hildon_check_button_set_active(HILDON_CHECK_BUTTON(button), active); |
1364 | #endif |
1365 | } |
1366 | |
1367 | static gboolean check_button_get_active(GtkWidget *button) { |
1368 | #if !defined(USE_MAEMO) || (MAEMO_VERSION_MAJOR < 5) |
1369 | return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button)); |
1370 | #else |
1371 | return hildon_check_button_get_active(HILDON_CHECK_BUTTON(button)); |
1372 | #endif |
1373 | } |
1374 | |
1375 | static void callback_finds_toggled(GtkWidget *widget, gpointer data ) { |
1376 | search_context_t *context = (search_context_t*)data; |
1377 | |
1378 | gboolean in_finds = check_button_get_active(context->in_finds); |
1379 | |
1380 | gtk_widget_set_sensitive(context->entry, !in_finds); |
1381 | gtk_widget_set_sensitive(context->in_id, !in_finds); |
1382 | gtk_widget_set_sensitive(context->in_name, !in_finds); |
1383 | gtk_widget_set_sensitive(context->in_desc, !in_finds); |
1384 | gtk_widget_set_sensitive(context->in_owner, !in_finds); |
1385 | gtk_widget_set_sensitive(context->spinner, in_finds); |
1386 | } |
1387 | |
1388 | static void |
1389 | cb_menu_search(GtkWidget *window, gpointer data) { |
1390 | appdata_t *appdata = (appdata_t *)data; |
1391 | |
1392 | search_context_t context; |
1393 | memset(&context, 0, sizeof(search_context_t)); |
1394 | context.appdata = appdata; |
1395 | |
1396 | GtkWidget *dialog = gtk_dialog_new_with_buttons(_("Enter search phrase"), |
1397 | GTK_WINDOW(appdata->window), GTK_DIALOG_MODAL, |
1398 | GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, |
1399 | GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT, |
1400 | NULL); |
1401 | |
1402 | gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(dialog)->vbox), |
1403 | gtk_label_new(_("Search in:"))); |
1404 | |
1405 | GtkWidget *table = gtk_table_new(2, 2, TRUE); |
1406 | gtk_table_set_col_spacing(GTK_TABLE(table), 0, 8); |
1407 | |
1408 | context.in_id = check_button_new_with_label(_("Waypoint IDs")); |
1409 | check_button_set_active(context.in_id, appdata->search & SEARCH_ID); |
1410 | gtk_table_attach_defaults(GTK_TABLE(table), context.in_id, 0, 1, 0, 1); |
1411 | |
1412 | context.in_name = check_button_new_with_label(_("Names")); |
1413 | check_button_set_active(context.in_name, appdata->search & SEARCH_NAME); |
1414 | gtk_table_attach_defaults(GTK_TABLE(table), context.in_name, 1, 2, 0, 1); |
1415 | |
1416 | context.in_desc = check_button_new_with_label(_("Descriptions")); |
1417 | check_button_set_active(context.in_desc, appdata->search & SEARCH_DESC); |
1418 | gtk_table_attach_defaults(GTK_TABLE(table), context.in_desc, 0, 1, 1, 2); |
1419 | |
1420 | context.in_owner = check_button_new_with_label(_("Owner")); |
1421 | check_button_set_active(context.in_owner, appdata->search & SEARCH_OWNER); |
1422 | gtk_table_attach_defaults(GTK_TABLE(table), context.in_owner, 1, 2, 1, 2); |
1423 | |
1424 | gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(dialog)->vbox), table); |
1425 | |
1426 | /* -------------------------------------------------------------- */ |
1427 | |
1428 | gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(dialog)->vbox), |
1429 | gtk_label_new(_("Search for:"))); |
1430 | context.entry = entry_new(); |
1431 | gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(dialog)->vbox), |
1432 | context.entry); |
1433 | |
1434 | if(appdata->search_str) |
1435 | gtk_entry_set_text(GTK_ENTRY(context.entry), appdata->search_str); |
1436 | |
1437 | /* -------------------------------------------------------------- */ |
1438 | |
1439 | gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(dialog)->vbox), |
1440 | gtk_hseparator_new()); |
1441 | |
1442 | GtkWidget *hbox = gtk_hbox_new(FALSE, 5); |
1443 | |
1444 | context.in_finds = check_button_new_with_label(_("Search finds for")); |
1445 | check_button_set_active(context.in_finds, appdata->search & SEARCH_FINDS); |
1446 | gtk_box_pack_start_defaults(GTK_BOX(hbox), context.in_finds); |
1447 | g_signal_connect(G_OBJECT(context.in_finds), "toggled", |
1448 | G_CALLBACK(callback_finds_toggled), &context); |
1449 | |
1450 | #ifndef USE_MAEMO |
1451 | GtkObject *adj = gtk_adjustment_new(appdata->search_days, 0, 99, 1, 10, 10); |
1452 | context.spinner = gtk_spin_button_new(GTK_ADJUSTMENT(adj), 1, 0); |
1453 | #else |
1454 | context.spinner = hildon_number_editor_new(0, 99); |
1455 | hildon_number_editor_set_value(HILDON_NUMBER_EDITOR(context.spinner), |
1456 | appdata->search_days); |
1457 | #endif |
1458 | gtk_box_pack_start_defaults(GTK_BOX(hbox), context.spinner); |
1459 | |
1460 | gtk_box_pack_start_defaults(GTK_BOX(hbox), gtk_label_new(_("days"))); |
1461 | |
1462 | gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(dialog)->vbox), hbox); |
1463 | |
1464 | /* -------------------------------------------------------------- */ |
1465 | |
1466 | gtk_widget_show_all(dialog); |
1467 | callback_finds_toggled(NULL, &context); |
1468 | |
1469 | if(GTK_RESPONSE_ACCEPT == gtk_dialog_run(GTK_DIALOG(dialog))) { |
1470 | char *p = strdup(gtk_entry_get_text(GTK_ENTRY(context.entry))); |
1471 | |
1472 | /* update saved search string */ |
1473 | if(appdata->search_str) free(appdata->search_str); |
1474 | if(strlen(p) > 0) |
1475 | appdata->search_str = strdup(p); |
1476 | |
1477 | #ifndef USE_MAEMO |
1478 | appdata->search_days = gtk_spin_button_get_value_as_int( |
1479 | GTK_SPIN_BUTTON(context.spinner)); |
1480 | #else |
1481 | appdata->search_days = hildon_number_editor_get_value( |
1482 | HILDON_NUMBER_EDITOR(context.spinner)); |
1483 | #endif |
1484 | |
1485 | if(check_button_get_active(context.in_finds)) |
1486 | appdata->search |= SEARCH_FINDS; |
1487 | else |
1488 | appdata->search &= ~SEARCH_FINDS; |
1489 | |
1490 | if(check_button_get_active(context.in_id)) |
1491 | appdata->search |= SEARCH_ID; |
1492 | else |
1493 | appdata->search &= ~SEARCH_ID; |
1494 | |
1495 | if(check_button_get_active(context.in_name)) |
1496 | appdata->search |= SEARCH_NAME; |
1497 | else |
1498 | appdata->search &= ~SEARCH_NAME; |
1499 | |
1500 | if(check_button_get_active(context.in_desc)) |
1501 | appdata->search |= SEARCH_DESC; |
1502 | else |
1503 | appdata->search &= ~SEARCH_DESC; |
1504 | |
1505 | if(check_button_get_active(context.in_owner)) |
1506 | appdata->search |= SEARCH_OWNER; |
1507 | else |
1508 | appdata->search &= ~SEARCH_OWNER; |
1509 | |
1510 | gtk_widget_destroy(dialog); |
1511 | |
1512 | /* don't search if we are asked to search for nothing */ |
1513 | if(((appdata->search & (SEARCH_ID|SEARCH_NAME|SEARCH_DESC|SEARCH_OWNER)) && |
1514 | strlen(p) > 0) || (appdata->search & SEARCH_FINDS)) { |
1515 | |
1516 | printf("Search for %s (flags = %x)...\n", p, appdata->search); |
1517 | |
1518 | #if !defined(USE_BREAD_CRUMB_TRAIL) && !defined(BCT) |
1519 | gpx_t *found = |
1520 | search_do(appdata, appdata->gpx, p, appdata->search, FALSE); |
1521 | |
1522 | /* do search result dialog here ... */ |
1523 | cachelist_dialog(appdata, found); |
1524 | #ifndef USE_STACKABLE_WINDOW |
1525 | search_result_free(found); |
1526 | #else |
1527 | appdata->search_results = found; |
1528 | #endif |
1529 | #else |
1530 | gpx_t *found = NULL; |
1531 | |
1532 | if(appdata->cur_gpx) |
1533 | found = search_do(appdata, appdata->cur_gpx, p, appdata->search, TRUE); |
1534 | else |
1535 | found = search_do(appdata, appdata->gpx, p, appdata->search, FALSE); |
1536 | |
1537 | gtk_container_remove(GTK_CONTAINER(appdata->vbox), appdata->cur_view); |
1538 | appdata->cur_view = cachelist_create(appdata, found, NULL); |
1539 | gtk_box_pack_start_defaults(GTK_BOX(appdata->vbox), appdata->cur_view); |
1540 | gtk_widget_show_all(appdata->vbox); |
1541 | crumb_add(appdata, found->name, |
1542 | appdata->cur_gpx?CRUMB_SEARCH_GPX:CRUMB_SEARCH_GLOBAL, found); |
1543 | #endif |
1544 | } else |
1545 | printf("No valid search: \"%s\" with flags %x!\n", p, appdata->search); |
1546 | |
1547 | free(p); |
1548 | } else |
1549 | gtk_widget_destroy(dialog); |
1550 | } |
1551 | |
1552 | static void on_window_destroy (GtkWidget *widget, gpointer data); |
1553 | |
1554 | #ifndef USE_MAEMO |
1555 | static void |
1556 | cb_menu_quit(GtkWidget *window, gpointer data) { |
1557 | on_window_destroy(window, data); |
1558 | } |
1559 | #endif |
1560 | |
1561 | #ifndef NO_COPY_N_PASTE |
1562 | static void |
1563 | cb_menu_cut(GtkWidget *widget, gpointer data) { |
1564 | appdata_t *appdata = (appdata_t*)data; |
1565 | |
1566 | if(appdata->active_buffer) { |
1567 | if(GTK_WIDGET_TYPE(appdata->active_buffer) == GTK_TYPE_TEXT_BUFFER) { |
1568 | gtk_text_buffer_cut_clipboard(GTK_TEXT_BUFFER(appdata->active_buffer), |
1569 | appdata->clipboard, TRUE); |
1570 | } else |
1571 | printf("cut: ERROR, not a text buffer\n"); |
1572 | } else |
1573 | printf("cut: ERROR, no active buffer\n"); |
1574 | } |
1575 | |
1576 | static void |
1577 | cb_menu_copy(GtkWidget *widget, gpointer data) { |
1578 | appdata_t *appdata = (appdata_t*)data; |
1579 | |
1580 | if(appdata->active_buffer) { |
1581 | if(GTK_WIDGET_TYPE(appdata->active_buffer) == GTK_TYPE_TEXT_BUFFER) { |
1582 | gtk_text_buffer_copy_clipboard(GTK_TEXT_BUFFER(appdata->active_buffer), |
1583 | appdata->clipboard); |
1584 | } else if(GTK_WIDGET_TYPE(appdata->active_buffer) == gtk_html_get_type()) { |
1585 | printf("copy from html buffer\n"); |
1586 | html_copy_to_clipboard(appdata); |
1587 | } else |
1588 | printf("copy: ERROR, not a text nor a html buffer\n"); |
1589 | } else |
1590 | printf("copy: ERROR, no active buffer\n"); |
1591 | } |
1592 | |
1593 | static void |
1594 | cb_menu_paste(GtkWidget *widget, gpointer data) { |
1595 | appdata_t *appdata = (appdata_t*)data; |
1596 | |
1597 | if(appdata->active_buffer) { |
1598 | if(GTK_WIDGET_TYPE(appdata->active_buffer) == GTK_TYPE_TEXT_BUFFER) { |
1599 | gtk_text_buffer_paste_clipboard(GTK_TEXT_BUFFER(appdata->active_buffer), |
1600 | appdata->clipboard, NULL, TRUE); |
1601 | } else |
1602 | printf("paste: ERROR, not a text buffer\n"); |
1603 | } else |
1604 | printf("paste: ERROR, no active buffer\n"); |
1605 | } |
1606 | #endif |
1607 | |
1608 | static void |
1609 | cb_menu_export_log(GtkWidget *widget, gpointer data) { |
1610 | appdata_t *appdata = (appdata_t*)data; |
1611 | notes_log_export(appdata); |
1612 | } |
1613 | |
1614 | #ifdef ENABLE_MAEMO_MAPPER |
1615 | static void |
1616 | cb_menu_export_mmpoi(GtkWidget *widget, gpointer data) { |
1617 | appdata_t *appdata = (appdata_t*)data; |
1618 | mmpoi_export(appdata); |
1619 | } |
1620 | #endif |
1621 | |
1622 | static void |
1623 | cb_menu_export_garmin(GtkWidget *widget, gpointer data) { |
1624 | appdata_t *appdata = (appdata_t*)data; |
1625 | garmin_export(appdata); |
1626 | } |
1627 | |
1628 | #ifdef ENABLE_OSM_GPS_MAP |
1629 | static void |
1630 | cb_menu_map(GtkWidget *window, gpointer data) { |
1631 | map((appdata_t *)data); |
1632 | } |
1633 | #endif |
1634 | |
1635 | static void |
1636 | cb_menu_geomath(GtkWidget *window, gpointer data) { |
1637 | geomath_dialog((appdata_t *)data); |
1638 | } |
1639 | |
1640 | static void |
1641 | cb_menu_geotext(GtkWidget *window, gpointer data) { |
1642 | geotext_dialog((appdata_t *)data); |
1643 | } |
1644 | |
1645 | static void |
1646 | cb_menu_precpos(GtkWidget *window, gpointer data) { |
1647 | precise_position((appdata_t *)data); |
1648 | } |
1649 | |
1650 | static void |
1651 | cb_menu_geotoad(GtkWidget *window, gpointer data) { |
1652 | geotoad((appdata_t *)data); |
1653 | } |
1654 | |
1655 | #ifdef USE_STACKABLE_WINDOW |
1656 | typedef struct { |
1657 | char *label, *desc; |
1658 | GtkSignalFunc activate_cb; |
1659 | } menu_entry_t; |
1660 | |
1661 | typedef struct { |
1662 | const char *title; |
1663 | const menu_entry_t *menu; |
1664 | int len; |
1665 | } submenu_t; |
1666 | |
1667 | #define COLUMNS 1 |
1668 | |
1669 | void on_submenu_entry_clicked(GtkButton *button, GtkWidget *menu) { |
1670 | |
1671 | /* force closing of submenu dialog */ |
1672 | gtk_dialog_response(GTK_DIALOG(menu), GTK_RESPONSE_NONE); |
1673 | gtk_widget_hide(menu); |
1674 | |
1675 | /* let gtk clean up */ |
1676 | while(gtk_events_pending()) |
1677 | gtk_main_iteration(); |
1678 | } |
1679 | |
1680 | static GtkWidget *app_submenu_create(appdata_t *appdata, |
1681 | const submenu_t *submenu) { |
1682 | |
1683 | /* create a oridinary dialog box */ |
1684 | GtkWidget *dialog = gtk_dialog_new(); |
1685 | gtk_window_set_title(GTK_WINDOW(dialog), _(submenu->title)); |
1686 | gtk_window_set_modal(GTK_WINDOW(dialog), TRUE); |
1687 | gtk_window_set_transient_for(GTK_WINDOW(dialog), |
1688 | GTK_WINDOW(appdata->window)); |
1689 | gtk_dialog_set_has_separator(GTK_DIALOG(dialog), FALSE); |
1690 | |
1691 | GtkWidget *table = gtk_table_new(submenu->len/COLUMNS, COLUMNS, TRUE); |
1692 | int x = 0, y = 0; |
1693 | |
1694 | const menu_entry_t *menu_entries = submenu->menu; |
1695 | while(menu_entries->label) { |
1696 | GtkWidget *button = NULL; |
1697 | |
1698 | button = hildon_button_new_with_text( |
1699 | HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH, |
1700 | HILDON_BUTTON_ARRANGEMENT_VERTICAL, |
1701 | _(menu_entries->label), _(menu_entries->desc)); |
1702 | |
1703 | /* try to center both texts */ |
1704 | hildon_button_set_title_alignment(HILDON_BUTTON(button), 0.5, 0.5); |
1705 | hildon_button_set_value_alignment(HILDON_BUTTON(button), 0.5, 0.5); |
1706 | |
1707 | g_signal_connect(button, "clicked", |
1708 | G_CALLBACK(on_submenu_entry_clicked), dialog); |
1709 | |
1710 | g_signal_connect(button, "clicked", |
1711 | menu_entries->activate_cb, appdata); |
1712 | |
1713 | gtk_table_attach_defaults(GTK_TABLE(table), button, x, x+1, y, y+1); |
1714 | |
1715 | x++; |
1716 | if(x == COLUMNS) { x = 0; y++; } |
1717 | |
1718 | menu_entries++; |
1719 | } |
1720 | |
1721 | gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(dialog)->vbox), table); |
1722 | |
1723 | return dialog; |
1724 | } |
1725 | |
1726 | /* popup the dialog shaped submenu */ |
1727 | static void submenu_popup(GtkWidget *menu) { |
1728 | gtk_widget_show_all(menu); |
1729 | gtk_dialog_run(GTK_DIALOG(menu)); |
1730 | gtk_widget_hide(menu); |
1731 | } |
1732 | |
1733 | static void submenu_cleanup(GtkWidget *menu) { |
1734 | gtk_widget_destroy(menu); |
1735 | } |
1736 | |
1737 | static const menu_entry_t submenu_export_entries[] = { |
1738 | #ifdef ENABLE_MAEMO_MAPPER |
1739 | { "Export to Maemo Mapper" , "Save a Maemo Mapper POI file", |
1740 | G_CALLBACK(cb_menu_export_mmpoi) }, |
1741 | #endif |
1742 | { "Export Field Notes", "Save a Garmin Field Notes file", |
1743 | G_CALLBACK(cb_menu_export_log) }, |
1744 | { "Export Garmin GPX", "Save modified waypoints in GPX file", |
1745 | G_CALLBACK(cb_menu_export_garmin) }, |
1746 | { NULL, NULL, NULL } |
1747 | }; |
1748 | |
1749 | static const submenu_t submenu_export = { |
1750 | "Export", submenu_export_entries, |
1751 | sizeof(submenu_export_entries)/sizeof(menu_entry_t)-1 |
1752 | }; |
1753 | |
1754 | /* the export submenu */ |
1755 | void on_export_clicked(GtkButton *button, appdata_t *appdata) { |
1756 | if(!appdata->export_menu) |
1757 | appdata->export_menu = app_submenu_create(appdata, &submenu_export); |
1758 | |
1759 | submenu_popup(appdata->export_menu); |
1760 | } |
1761 | |
1762 | static const menu_entry_t submenu_tools_entries[] = { |
1763 | { "Geomath", "Geocoordinate calculation", |
1764 | G_CALLBACK(cb_menu_geomath) }, |
1765 | { "Geotext", "Text analysis", |
1766 | G_CALLBACK(cb_menu_geotext) }, |
1767 | { "Precise Position", "Calculate a precise GPS position", |
1768 | G_CALLBACK(cb_menu_precpos) }, |
1769 | { "GeoToad", "Use GeoToad online downloader", |
1770 | G_CALLBACK(cb_menu_geotoad) }, |
1771 | { NULL, NULL, NULL } |
1772 | }; |
1773 | |
1774 | static const submenu_t submenu_tools = { |
1775 | "Tools", submenu_tools_entries, |
1776 | sizeof(submenu_tools_entries)/sizeof(menu_entry_t)-1 |
1777 | }; |
1778 | |
1779 | /* the tools submenu */ |
1780 | void on_tools_clicked(GtkButton *button, appdata_t *appdata) { |
1781 | if(!appdata->tools_menu) |
1782 | appdata->tools_menu = app_submenu_create(appdata, &submenu_tools); |
1783 | |
1784 | submenu_popup(appdata->tools_menu); |
1785 | } |
1786 | |
1787 | HildonAppMenu *menu_create(appdata_t *appdata, int mode) { |
1788 | GtkWidget *button; |
1789 | HildonAppMenu *menu = HILDON_APP_MENU(hildon_app_menu_new()); |
1790 | |
1791 | /* ------- */ |
1792 | button = gtk_button_new_with_label(_("About")); |
1793 | g_signal_connect_after(button, "clicked", |
1794 | G_CALLBACK(cb_menu_about), appdata); |
1795 | hildon_app_menu_append(menu, GTK_BUTTON(button)); |
1796 | |
1797 | button = gtk_button_new_with_label(_("Settings")); |
1798 | g_signal_connect_after(button, "clicked", G_CALLBACK(cb_menu_settings), |
1799 | appdata); |
1800 | hildon_app_menu_append(menu, GTK_BUTTON(button)); |
1801 | |
1802 | if(mode == MENU_GPXLIST) { |
1803 | button = gtk_button_new_with_label(_("Import file")); |
1804 | g_signal_connect_after(button, "clicked", |
1805 | G_CALLBACK(cb_menu_add), appdata); |
1806 | hildon_app_menu_append(menu, GTK_BUTTON(button)); |
1807 | |
1808 | button = gtk_button_new_with_label(_("Import directory")); |
1809 | g_signal_connect_after(button, "clicked", |
1810 | G_CALLBACK(cb_menu_adddir), appdata); |
1811 | hildon_app_menu_append(menu, GTK_BUTTON(button)); |
1812 | |
1813 | button = gtk_button_new_with_label(_("Export")); |
1814 | g_signal_connect_after(button, "clicked", |
1815 | G_CALLBACK(on_export_clicked), appdata); |
1816 | hildon_app_menu_append(menu, GTK_BUTTON(button)); |
1817 | |
1818 | button = gtk_button_new_with_label(_("Search")); |
1819 | g_signal_connect_after(button, "clicked", |
1820 | G_CALLBACK(cb_menu_search), appdata); |
1821 | hildon_app_menu_append(menu, GTK_BUTTON(button)); |
1822 | } |
1823 | |
1824 | button = gtk_button_new_with_label(_("Tools")); |
1825 | g_signal_connect_after(button, "clicked", |
1826 | G_CALLBACK(on_tools_clicked), appdata); |
1827 | hildon_app_menu_append(menu, GTK_BUTTON(button)); |
1828 | |
1829 | #ifdef ENABLE_OSM_GPS_MAP |
1830 | button = gtk_button_new_with_label(_("Map")); |
1831 | g_signal_connect_after(button, "clicked", |
1832 | G_CALLBACK(cb_menu_map), appdata); |
1833 | hildon_app_menu_append(menu, GTK_BUTTON(button)); |
1834 | #endif |
1835 | |
1836 | #ifdef HILDON_HELP |
1837 | button = gtk_button_new_with_label(_("Help")); |
1838 | g_signal_connect_after(button, "clicked", |
1839 | G_CALLBACK(cb_menu_help), appdata); |
1840 | hildon_app_menu_append(menu, GTK_BUTTON(button)); |
1841 | #endif |
1842 | |
1843 | gtk_widget_show_all(GTK_WIDGET(menu)); |
1844 | |
1845 | return menu; |
1846 | } |
1847 | #else |
1848 | |
1849 | void menu_create(appdata_t *appdata) { |
1850 | GtkWidget *menu, *item; |
1851 | menu = gtk_menu_new(); |
1852 | |
1853 | #if defined(USE_BREAD_CRUMB_TRAIL) || defined(BCT) |
1854 | appdata->menu_import = |
1855 | #endif |
1856 | item = gtk_menu_item_new_with_label(_("Import")); |
1857 | gtk_menu_append(GTK_MENU_SHELL(menu), item); |
1858 | GtkWidget *submenu = gtk_menu_new(); |
1859 | gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu); |
1860 | |
1861 | item = gtk_menu_item_new_with_label( _("File") ); |
1862 | gtk_menu_append(GTK_MENU_SHELL(submenu), item); |
1863 | g_signal_connect(item, "activate", GTK_SIGNAL_FUNC(cb_menu_add), appdata); |
1864 | |
1865 | item = gtk_menu_item_new_with_label( _("Directory") ); |
1866 | gtk_menu_append(GTK_MENU_SHELL(submenu), item); |
1867 | g_signal_connect(item, "activate", GTK_SIGNAL_FUNC(cb_menu_adddir), appdata); |
1868 | |
1869 | #ifndef USE_PANNABLE_AREA |
1870 | gtk_menu_append(GTK_MENU_SHELL(submenu), gtk_separator_menu_item_new()); |
1871 | |
1872 | appdata->menu_close = |
1873 | item = gtk_menu_item_new_with_label( _("Close") ); |
1874 | gtk_menu_append(GTK_MENU_SHELL(submenu), item); |
1875 | g_signal_connect(item, "activate", GTK_SIGNAL_FUNC(cb_menu_close), appdata); |
1876 | |
1877 | appdata->menu_remove = |
1878 | item = gtk_menu_item_new_with_label( _("Remove") ); |
1879 | gtk_menu_append(GTK_MENU_SHELL(submenu), item); |
1880 | g_signal_connect(item, "activate", GTK_SIGNAL_FUNC(cb_menu_remove), appdata); |
1881 | #endif |
1882 | |
1883 | #if defined(USE_BREAD_CRUMB_TRAIL) || defined(BCT) |
1884 | appdata->menu_export = |
1885 | #endif |
1886 | item = gtk_menu_item_new_with_label(_("Export")); |
1887 | gtk_menu_append(GTK_MENU_SHELL(menu), item); |
1888 | submenu = gtk_menu_new(); |
1889 | gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu); |
1890 | |
1891 | #ifdef ENABLE_MAEMO_MAPPER |
1892 | item = gtk_menu_item_new_with_label( _("Maemo Mapper POI") ); |
1893 | gtk_menu_append(GTK_MENU_SHELL(submenu), item); |
1894 | g_signal_connect(item, "activate", |
1895 | GTK_SIGNAL_FUNC(cb_menu_export_mmpoi), appdata); |
1896 | #endif |
1897 | |
1898 | item = gtk_menu_item_new_with_label( _("Garmin Field Notes") ); |
1899 | gtk_menu_append(GTK_MENU_SHELL(submenu), item); |
1900 | g_signal_connect(item, "activate", |
1901 | GTK_SIGNAL_FUNC(cb_menu_export_log), appdata); |
1902 | |
1903 | item = gtk_menu_item_new_with_label( _("Garmin GPX") ); |
1904 | gtk_menu_append(GTK_MENU_SHELL(submenu), item); |
1905 | g_signal_connect(item, "activate", |
1906 | GTK_SIGNAL_FUNC(cb_menu_export_garmin), appdata); |
1907 | |
1908 | #if defined(USE_BREAD_CRUMB_TRAIL) || defined(BCT) |
1909 | appdata->menu_search = |
1910 | #endif |
1911 | item = gtk_menu_item_new_with_label( _("Search") ); |
1912 | gtk_menu_append(GTK_MENU_SHELL(menu), item); |
1913 | g_signal_connect(item, "activate", GTK_SIGNAL_FUNC(cb_menu_search), appdata); |
1914 | |
1915 | gtk_menu_append(GTK_MENU_SHELL(menu), gtk_separator_menu_item_new()); |
1916 | |
1917 | /* ----------- copy'n paste submenu ----------------- */ |
1918 | #ifndef NO_COPY_N_PASTE |
1919 | item = gtk_menu_item_new_with_label(_("Edit")); |
1920 | gtk_menu_append(GTK_MENU_SHELL(menu), item); |
1921 | submenu = gtk_menu_new(); |
1922 | gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu); |
1923 | |
1924 | appdata->menu_cut = item = gtk_menu_item_new_with_label( _("Cut") ); |
1925 | gtk_widget_set_sensitive(item, FALSE); |
1926 | gtk_menu_append(GTK_MENU_SHELL(submenu), item); |
1927 | g_signal_connect(item, "activate", GTK_SIGNAL_FUNC(cb_menu_cut), appdata); |
1928 | appdata->menu_copy = item = gtk_menu_item_new_with_label( _("Copy") ); |
1929 | gtk_widget_set_sensitive(item, FALSE); |
1930 | gtk_menu_append(GTK_MENU_SHELL(submenu), item); |
1931 | g_signal_connect(item, "activate", GTK_SIGNAL_FUNC(cb_menu_copy), appdata); |
1932 | appdata->menu_paste = item = gtk_menu_item_new_with_label( _("Paste") ); |
1933 | gtk_widget_set_sensitive(item, FALSE); |
1934 | gtk_menu_append(GTK_MENU_SHELL(submenu), item); |
1935 | g_signal_connect(item, "activate", GTK_SIGNAL_FUNC(cb_menu_paste), appdata); |
1936 | #endif |
1937 | |
1938 | item = gtk_menu_item_new_with_label( _("Settings") ); |
1939 | gtk_menu_append(GTK_MENU_SHELL(menu), item); |
1940 | g_signal_connect(item, "activate", GTK_SIGNAL_FUNC(cb_menu_settings), |
1941 | appdata); |
1942 | |
1943 | gtk_menu_append(GTK_MENU_SHELL(menu), gtk_separator_menu_item_new()); |
1944 | |
1945 | #ifdef ENABLE_OSM_GPS_MAP |
1946 | item = gtk_menu_item_new_with_label( _("Map") ); |
1947 | gtk_menu_append(GTK_MENU_SHELL(menu), item); |
1948 | g_signal_connect(item, "activate", |
1949 | GTK_SIGNAL_FUNC(cb_menu_map), appdata); |
1950 | #endif |
1951 | |
1952 | item = gtk_menu_item_new_with_label(_("Tools")); |
1953 | gtk_menu_append(GTK_MENU_SHELL(menu), item); |
1954 | submenu = gtk_menu_new(); |
1955 | gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu); |
1956 | |
1957 | item = gtk_menu_item_new_with_label( _("Geomath") ); |
1958 | gtk_menu_append(GTK_MENU_SHELL(submenu), item); |
1959 | g_signal_connect(item, "activate", |
1960 | GTK_SIGNAL_FUNC(cb_menu_geomath), appdata); |
1961 | |
1962 | item = gtk_menu_item_new_with_label( _("Geotext") ); |
1963 | gtk_menu_append(GTK_MENU_SHELL(submenu), item); |
1964 | g_signal_connect(item, "activate", |
1965 | GTK_SIGNAL_FUNC(cb_menu_geotext), appdata); |
1966 | |
1967 | item = gtk_menu_item_new_with_label( _("Precise Position") ); |
1968 | gtk_menu_append(GTK_MENU_SHELL(submenu), item); |
1969 | g_signal_connect(item, "activate", |
1970 | GTK_SIGNAL_FUNC(cb_menu_precpos), appdata); |
1971 | |
1972 | item = gtk_menu_item_new_with_label( _("GeoToad") ); |
1973 | gtk_menu_append(GTK_MENU_SHELL(submenu), item); |
1974 | g_signal_connect(item, "activate", |
1975 | GTK_SIGNAL_FUNC(cb_menu_geotoad), appdata); |
1976 | |
1977 | gtk_menu_append(GTK_MENU_SHELL(menu), gtk_separator_menu_item_new()); |
1978 | |
1979 | #if defined(USE_MAEMO) && defined(HILDON_HELP) |
1980 | item = gtk_menu_item_new_with_label( _("Help") ); |
1981 | gtk_menu_append(GTK_MENU_SHELL(menu), item); |
1982 | g_signal_connect(item, "activate", GTK_SIGNAL_FUNC(cb_menu_help), appdata); |
1983 | #endif |
1984 | |
1985 | item = gtk_menu_item_new_with_label( _("About") ); |
1986 | gtk_menu_append(GTK_MENU_SHELL(menu), item); |
1987 | g_signal_connect(item, "activate", GTK_SIGNAL_FUNC(cb_menu_about), appdata); |
1988 | |
1989 | #ifndef USE_MAEMO |
1990 | item = gtk_menu_item_new_with_label( _("Quit") ); |
1991 | gtk_menu_append(GTK_MENU_SHELL(menu), item); |
1992 | g_signal_connect(item, "activate", GTK_SIGNAL_FUNC(cb_menu_quit), appdata); |
1993 | #endif |
1994 | |
1995 | #ifdef USE_MAEMO |
1996 | hildon_window_set_menu(appdata->window, GTK_MENU(menu)); |
1997 | #else |
1998 | /* attach ordinary gtk menu */ |
1999 | GtkWidget *menu_bar = gtk_menu_bar_new(); |
2000 | |
2001 | GtkWidget *root_menu = gtk_menu_item_new_with_label (_("Menu")); |
2002 | gtk_widget_show(root_menu); |
2003 | |
2004 | gtk_menu_bar_append(menu_bar, root_menu); |
2005 | gtk_menu_item_set_submenu(GTK_MENU_ITEM (root_menu), menu); |
2006 | |
2007 | gtk_widget_show(menu_bar); |
2008 | gtk_box_pack_start(GTK_BOX(appdata->vbox), menu_bar, 0, 0, 0); |
2009 | #endif |
2010 | } |
2011 | #endif |
2012 | |
2013 | /********************* end of menu **********************/ |
2014 | |
2015 | void cleanup(appdata_t *appdata) { |
2016 | gconf_save_state(appdata); |
2017 | |
2018 | gpx_free_all(appdata->gpx); |
2019 | |
2020 | #ifdef USE_STACKABLE_WINDOW |
2021 | if(appdata->export_menu) submenu_cleanup(appdata->export_menu); |
2022 | if(appdata->tools_menu) submenu_cleanup(appdata->tools_menu); |
2023 | #endif |
2024 | |
2025 | gnome_vfs_shutdown(); |
2026 | icons_free(); |
2027 | gps_release(appdata); |
2028 | |
2029 | #ifdef USE_MAEMO |
2030 | if(appdata->search_results) { |
2031 | printf("freeing pending search\n"); |
2032 | search_result_free(appdata->search_results); |
2033 | } |
2034 | |
2035 | if(appdata->osso_context) |
2036 | osso_deinitialize(appdata->osso_context); |
2037 | |
2038 | appdata->program = NULL; |
2039 | #endif |
2040 | |
2041 | /* free chain of locations */ |
2042 | location_t *loc = appdata->location; |
2043 | while(loc) { |
2044 | location_t *next = loc->next; |
2045 | if(loc->name) free(loc->name); |
2046 | free(loc); |
2047 | loc = next; |
2048 | } |
2049 | |
2050 | puts("everything is gone"); |
2051 | } |
2052 | |
2053 | static void on_window_destroy (GtkWidget *widget, gpointer data) { |
2054 | appdata_t *appdata = (appdata_t*)data; |
2055 | |
2056 | gtk_main_quit(); |
2057 | appdata->window = NULL; |
2058 | } |
2059 | |
2060 | gboolean on_window_key_press(GtkWidget *widget, |
2061 | GdkEventKey *event, appdata_t *appdata) { |
2062 | int handled = FALSE; |
2063 | |
2064 | // printf("key event %d\n", event->keyval); |
2065 | |
2066 | switch(event->keyval) { |
2067 | #ifdef USE_MAEMO |
2068 | |
2069 | #ifdef HILDON_HARDKEY_INCREASE |
2070 | case HILDON_HARDKEY_INCREASE: |
2071 | html_zoom(appdata, TRUE); |
2072 | handled = TRUE; |
2073 | break; |
2074 | #endif |
2075 | |
2076 | #ifdef HILDON_HARDKEY_DECREASE |
2077 | case HILDON_HARDKEY_DECREASE: |
2078 | html_zoom(appdata, FALSE); |
2079 | handled = TRUE; |
2080 | break; |
2081 | #endif |
2082 | |
2083 | #ifdef HILDON_HARDKEY_FULLSCREEN |
2084 | case HILDON_HARDKEY_FULLSCREEN: |
2085 | { |
2086 | appdata->fullscreen = !appdata->fullscreen; |
2087 | |
2088 | if(appdata->fullscreen) |
2089 | gtk_window_fullscreen(GTK_WINDOW(appdata->window)); |
2090 | else |
2091 | gtk_window_unfullscreen(GTK_WINDOW(appdata->window)); |
2092 | |
2093 | handled = TRUE; |
2094 | } |
2095 | break; |
2096 | #endif |
2097 | |
2098 | #else |
2099 | case '+': |
2100 | printf("zoom+\n"); |
2101 | html_zoom(appdata, TRUE); |
2102 | handled = TRUE; |
2103 | break; |
2104 | case '-': |
2105 | printf("zoom-\n"); |
2106 | html_zoom(appdata, FALSE); |
2107 | handled = TRUE; |
2108 | break; |
2109 | #endif |
2110 | } |
2111 | |
2112 | return handled; |
2113 | } |
2114 | |
2115 | #if defined(USE_BREAD_CRUMB_TRAIL) || defined(BCT) |
2116 | typedef struct { |
2117 | int level; |
2118 | appdata_t *appdata; |
2119 | gpointer data; |
2120 | } crumb_t; |
2121 | |
2122 | static void |
2123 | crumb_back(gpointer data) { |
2124 | crumb_t *crumb = (crumb_t*)data; |
2125 | printf("crumb_back called with %d\n", crumb->level); |
2126 | |
2127 | /* don't do anything if main window has already been destroyed */ |
2128 | if(!crumb->appdata->window) { |
2129 | printf("Main window gone ...\n"); |
2130 | return; |
2131 | } |
2132 | |
2133 | /* whatever is being displayed: we don't need it anymore */ |
2134 | gtk_container_remove(GTK_CONTAINER(crumb->appdata->vbox), |
2135 | crumb->appdata->cur_view); |
2136 | |
2137 | /* back from cache to cachelist */ |
2138 | if(crumb->level == CRUMB_CACHE) { |
2139 | gpx_t *gpx = crumb->appdata->search_results; |
2140 | |
2141 | if(!gpx) { |
2142 | gtk_widget_set_sensitive(crumb->appdata->menu_search, TRUE); |
2143 | gtk_widget_set_sensitive(crumb->appdata->menu_export, TRUE); |
2144 | printf("no search data found, return to gpx\n"); |
2145 | gpx = crumb->appdata->cur_gpx; |
2146 | } else |
2147 | printf("returning to search result\n"); |
2148 | |
2149 | g_assert(gpx != NULL); |
2150 | |
2151 | crumb->appdata->cur_view = cachelist_create(crumb->appdata, gpx, |
2152 | crumb->appdata->cur_cache); |
2153 | |
2154 | /* returning from cache view: invalidate cache reference */ |
2155 | crumb->appdata->cur_cache = NULL; |
2156 | |
2157 | gtk_box_pack_start_defaults(GTK_BOX(crumb->appdata->vbox), |
2158 | crumb->appdata->cur_view); |
2159 | } |
2160 | |
2161 | if(crumb->level == CRUMB_SEARCH_GPX) { |
2162 | printf("returning from a local search!\n"); |
2163 | |
2164 | g_assert((gpx_t*)crumb->data == crumb->appdata->search_results); |
2165 | |
2166 | search_result_free((gpx_t*)crumb->data); |
2167 | crumb->appdata->search_results = NULL; |
2168 | |
2169 | gtk_widget_set_sensitive(crumb->appdata->menu_search, TRUE); |
2170 | |
2171 | crumb->appdata->cur_view = cachelist_create(crumb->appdata, |
2172 | crumb->appdata->cur_gpx, NULL); |
2173 | gtk_box_pack_start_defaults(GTK_BOX(crumb->appdata->vbox), |
2174 | crumb->appdata->cur_view); |
2175 | } |
2176 | |
2177 | /* back from cachelist to gpxlist */ |
2178 | if((crumb->level == CRUMB_CACHELIST) || |
2179 | (crumb->level == CRUMB_SEARCH_GLOBAL)) { |
2180 | |
2181 | crumb->appdata->cur_view = gpxlist_create_view_and_model( |
2182 | crumb->appdata, crumb->appdata->cur_gpx); |
2183 | |
2184 | /* returning from cachelist/global search view: */ |
2185 | /* invalidate gpx reference */ |
2186 | crumb->appdata->cur_gpx = NULL; |
2187 | |
2188 | gtk_box_pack_start_defaults(GTK_BOX(crumb->appdata->vbox), |
2189 | crumb->appdata->cur_view); |
2190 | |
2191 | if((crumb->level == CRUMB_SEARCH_GLOBAL) || |
2192 | (crumb->level == CRUMB_SEARCH_GPX)) { |
2193 | g_assert((gpx_t*)crumb->data == crumb->appdata->search_results); |
2194 | |
2195 | search_result_free((gpx_t*)crumb->data); |
2196 | crumb->appdata->search_results = NULL; |
2197 | } |
2198 | |
2199 | /* enable gpxlist related menu entries */ |
2200 | gtk_widget_set_sensitive(crumb->appdata->menu_import, TRUE); |
2201 | gtk_widget_set_sensitive(crumb->appdata->menu_search, TRUE); |
2202 | gtk_widget_set_sensitive(crumb->appdata->menu_export, TRUE); |
2203 | } |
2204 | |
2205 | gtk_widget_show_all(crumb->appdata->vbox); |
2206 | g_free(data); |
2207 | |
2208 | #ifdef ENABLE_OSM_GPS_MAP |
2209 | map_update(crumb->appdata); |
2210 | #endif |
2211 | } |
2212 | |
2213 | static void crumb_add(appdata_t *appdata, char *name, int level, |
2214 | gpointer user_data) { |
2215 | crumb_t *crumb = malloc(sizeof(crumb_t)); |
2216 | crumb->level = level; |
2217 | crumb->appdata = appdata; |
2218 | crumb->data = user_data; |
2219 | |
2220 | printf("crumb_add with level %d\n", level); |
2221 | |
2222 | /* save that we are working on search results */ |
2223 | if((level == CRUMB_SEARCH_GLOBAL) || |
2224 | (level == CRUMB_SEARCH_GPX)) { |
2225 | appdata->search_results = (gpx_t*)user_data; |
2226 | |
2227 | /* searches cannot be nested */ |
2228 | gtk_widget_set_sensitive(appdata->menu_search, FALSE); |
2229 | } |
2230 | |
2231 | /* save "path" pointers in appdata */ |
2232 | if(crumb->level == CRUMB_CACHELIST) |
2233 | appdata->cur_gpx = (gpx_t*)user_data; |
2234 | |
2235 | if(crumb->level == CRUMB_CACHE) { |
2236 | appdata->cur_cache = (cache_t*)user_data; |
2237 | /* the cache view cannot be searched */ |
2238 | gtk_widget_set_sensitive(appdata->menu_search, FALSE); |
2239 | gtk_widget_set_sensitive(appdata->menu_export, FALSE); |
2240 | } |
2241 | |
2242 | if(level != CRUMB_GPXLIST) { |
2243 | /* disable gpxlist related menu entries */ |
2244 | gtk_widget_set_sensitive(appdata->menu_import, FALSE); |
2245 | #ifndef USE_PANNABLE_AREA |
2246 | gtk_widget_set_sensitive(appdata->menu_remove, FALSE); |
2247 | gtk_widget_set_sensitive(appdata->menu_close, FALSE); |
2248 | #endif |
2249 | } |
2250 | |
2251 | #ifdef USE_BREAD_CRUMB_TRAIL |
2252 | hildon_bread_crumb_trail_push_text(HILDON_BREAD_CRUMB_TRAIL(appdata->bct), |
2253 | name, crumb, (GDestroyNotify)crumb_back); |
2254 | #else |
2255 | bct_push_text(appdata->bct, name, crumb, (GDestroyNotify)crumb_back); |
2256 | #endif |
2257 | |
2258 | #ifdef ENABLE_OSM_GPS_MAP |
2259 | map_update(appdata); |
2260 | #endif |
2261 | } |
2262 | #endif // USE_BREAD_CRUMB_TRAIL |
2263 | |
2264 | void main_after_settings_redraw(appdata_t *appdata, int flags) { |
2265 | printf("main after settings redraw\n"); |
2266 | |
2267 | if(!appdata->cur_view) { |
2268 | printf("no active view\n"); |
2269 | return; |
2270 | } |
2271 | |
2272 | /* a cache screen cannot be changed from the settings and thus doesn't */ |
2273 | /* need to be redrawn */ |
2274 | if(appdata->cur_cache) { |
2275 | printf("No redraw in cache view required\n"); |
2276 | return; |
2277 | } |
2278 | |
2279 | int redraw = 0; // nothing to redraw |
2280 | |
2281 | if(appdata->search_results) { |
2282 | if((appdata->cur_items != appdata->cachelist_items) || flags) |
2283 | redraw = 1; |
2284 | } else { |
2285 | if(!appdata->cur_gpx) { |
2286 | if(appdata->cur_items != appdata->gpxlist_items) |
2287 | redraw = 2; // redraw gpxlist |
2288 | } else { |
2289 | if((appdata->cur_items != appdata->cachelist_items) || flags) |
2290 | redraw = 3; // redraw cachelist |
2291 | } |
2292 | } |
2293 | |
2294 | if(redraw) { |
2295 | GtkWidget *container = appdata->vbox; |
2296 | |
2297 | #ifdef USE_STACKABLE_WINDOW |
2298 | HildonWindowStack *stack = hildon_window_stack_get_default(); |
2299 | container = hildon_window_stack_peek(stack); |
2300 | #endif |
2301 | |
2302 | gtk_container_remove(GTK_CONTAINER(container), appdata->cur_view); |
2303 | switch(redraw) { |
2304 | case 1: |
2305 | appdata->cur_view = cachelist_create(appdata, |
2306 | appdata->search_results, NULL); |
2307 | break; |
2308 | case 2: |
2309 | appdata->cur_view = gpxlist_create_view_and_model(appdata, NULL); |
2310 | break; |
2311 | case 3: |
2312 | appdata->cur_view = cachelist_create(appdata, |
2313 | appdata->cur_gpx, NULL); |
2314 | break; |
2315 | } |
2316 | |
2317 | #ifdef USE_STACKABLE_WINDOW |
2318 | if(container != appdata->vbox) |
2319 | gtk_container_add(GTK_CONTAINER(container), appdata->cur_view); |
2320 | else |
2321 | #endif |
2322 | gtk_box_pack_start_defaults(GTK_BOX(container), appdata->cur_view); |
2323 | |
2324 | gtk_widget_show_all(container); |
2325 | } |
2326 | } |
2327 | |
2328 | int main(int argc, char *argv[]) { |
2329 | appdata_t appdata; |
2330 | |
2331 | /* init appdata */ |
2332 | memset(&appdata, 0, sizeof(appdata)); |
2333 | |
2334 | printf("Using locale for %s in %s\n", PACKAGE, LOCALEDIR); |
2335 | |
2336 | setlocale(LC_ALL, ""); |
2337 | bindtextdomain(PACKAGE, LOCALEDIR); |
2338 | bind_textdomain_codeset(PACKAGE, "UTF-8"); |
2339 | textdomain(PACKAGE); |
2340 | |
2341 | /* prepare thread system */ |
2342 | g_thread_init(NULL); |
2343 | |
2344 | gtk_init (&argc, &argv); |
2345 | |
2346 | misc_init(); |
2347 | |
2348 | curl_global_init(CURL_GLOBAL_ALL); |
2349 | |
2350 | #ifdef USE_MAEMO |
2351 | printf("Installing osso context for \"org.harbaum." APP "\"\n"); |
2352 | appdata.osso_context = osso_initialize("org.harbaum."APP, |
2353 | VERSION, TRUE, NULL); |
2354 | if(appdata.osso_context == NULL) { |
2355 | fprintf(stderr, "error initiating osso context\n"); |
2356 | } |
2357 | |
2358 | #ifdef ENABLE_MAEMO_MAPPER |
2359 | dbus_register(&appdata); |
2360 | #endif |
2361 | #endif |
2362 | |
2363 | icons_init(); |
2364 | |
2365 | if(!gnome_vfs_init()) { |
2366 | g_error("Gnome VFS init failed\n"); |
2367 | } |
2368 | |
2369 | #ifdef USE_MAEMO |
2370 | /* Create the hildon program and setup the title */ |
2371 | appdata.program = HILDON_PROGRAM(hildon_program_get_instance()); |
2372 | g_set_application_name("GPXView"); |
2373 | |
2374 | /* Create HildonWindow and set it to HildonProgram */ |
2375 | #ifdef USE_STACKABLE_WINDOW |
2376 | appdata.window = HILDON_WINDOW(hildon_stackable_window_new()); |
2377 | #else |
2378 | appdata.window = HILDON_WINDOW(hildon_window_new()); |
2379 | #endif |
2380 | hildon_program_add_window(appdata.program, appdata.window); |
2381 | #else |
2382 | /* Create a Window. */ |
2383 | appdata.window = gtk_window_new(GTK_WINDOW_TOPLEVEL); |
2384 | /* Set a decent default size for the window. */ |
2385 | gtk_window_set_default_size(GTK_WINDOW(appdata.window), 640, 480); |
2386 | #endif |
2387 | |
2388 | #if MAEMO_VERSION_MAJOR == 5 |
2389 | gtk_window_set_title(GTK_WINDOW(appdata.window), "GPXView"); |
2390 | #endif |
2391 | |
2392 | g_signal_connect(G_OBJECT(appdata.window), "destroy", |
2393 | G_CALLBACK(on_window_destroy), &appdata); |
2394 | |
2395 | g_signal_connect(G_OBJECT(appdata.window), "key_press_event", |
2396 | G_CALLBACK(on_window_key_press), &appdata); |
2397 | |
2398 | /* prepare clipboard */ |
2399 | appdata.clipboard = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD); |
2400 | gtk_clipboard_set_can_store(appdata.clipboard, NULL, 0); |
2401 | |
2402 | appdata.vbox = gtk_vbox_new(FALSE, 2); |
2403 | gtk_container_add(GTK_CONTAINER(appdata.window), appdata.vbox); |
2404 | #ifndef USE_STACKABLE_WINDOW |
2405 | menu_create(&appdata); |
2406 | #else |
2407 | hildon_window_set_app_menu(HILDON_WINDOW(appdata.window), |
2408 | menu_create(&appdata, MENU_GPXLIST)); |
2409 | #endif |
2410 | |
2411 | #ifdef USE_BREAD_CRUMB_TRAIL |
2412 | appdata.bct = hildon_bread_crumb_trail_new(); |
2413 | |
2414 | gtk_box_pack_start(GTK_BOX(appdata.vbox), appdata.bct, FALSE,FALSE,0); |
2415 | |
2416 | hildon_bread_crumb_trail_clear(HILDON_BREAD_CRUMB_TRAIL(appdata.bct)); |
2417 | #else |
2418 | #ifdef BCT |
2419 | /* on non-hildon machines we use some custom made breadcrumbtrail */ |
2420 | /* replacement */ |
2421 | appdata.bct = bct_new(); |
2422 | gtk_box_pack_start(GTK_BOX(appdata.vbox), appdata.bct, FALSE,FALSE,0); |
2423 | #endif |
2424 | #endif |
2425 | |
2426 | #if defined(USE_BREAD_CRUMB_TRAIL) || defined(BCT) |
2427 | crumb_add(&appdata, "GPX", CRUMB_GPXLIST, NULL); |
2428 | #endif |
2429 | |
2430 | /* wait for main gui to appear */ |
2431 | gtk_widget_show_all(GTK_WIDGET(appdata.window)); |
2432 | while(gtk_events_pending()) |
2433 | gtk_main_iteration(); |
2434 | |
2435 | appdata.gconf_client = gconf_client_get_default(); |
2436 | gconf_load_state(&appdata); |
2437 | gps_init(&appdata); |
2438 | |
2439 | appdata.cur_view = gpxlist_create_view_and_model(&appdata, NULL); |
2440 | gtk_box_pack_start_defaults(GTK_BOX(appdata.vbox), appdata.cur_view); |
2441 | |
2442 | gtk_widget_show_all(GTK_WIDGET(appdata.window)); |
2443 | gtk_main(); |
2444 | |
2445 | cleanup(&appdata); |
2446 | |
2447 | return 0; |
2448 | } |