Contents of /trunk/src/main.c

Parent Directory Parent Directory | Revision Log Revision Log


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