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