Contents of /trunk/src/main.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 243 - (hide annotations)
Mon Dec 14 20:07:54 2009 UTC (14 years, 5 months ago) by harbaum
File MIME type: text/plain
File size: 72476 byte(s)
Various search related bug fixes
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     #ifdef USE_MAEMO
35     #include <hildon/hildon-banner.h>
36 harbaum 164 #if MAEMO_VERSION_MAJOR >= 5
37     #include <hildon/hildon-note.h>
38 harbaum 190 #include <hildon/hildon-entry.h>
39 harbaum 1 #endif
40 harbaum 164 #endif
41 harbaum 1
42     #include <locale.h>
43    
44     extern char *strcasestr (__const char *__haystack, __const char *__needle);
45    
46 harbaum 128 #if defined(USE_BREAD_CRUMB_TRAIL) || defined(BCT)
47 harbaum 1 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 harbaum 164 #if !defined(USE_MAEMO) || (MAEMO_VERSION_MAJOR < 5)
104 harbaum 1 GtkWidget *dialog = gtk_message_dialog_new(
105     GTK_WINDOW(NULL),
106     GTK_DIALOG_DESTROY_WITH_PARENT,
107 harbaum 123 GTK_MESSAGE_ERROR,
108     GTK_BUTTONS_CLOSE, buf);
109 harbaum 1
110     gtk_window_set_title(GTK_WINDOW(dialog), _("ERROR"));
111 harbaum 164 #else
112     GtkWidget *dialog =
113     hildon_note_new_information(GTK_WINDOW(NULL), buf);
114     #endif
115 harbaum 1
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 harbaum 11 dialog = gtk_file_chooser_dialog_new (whole_dir?_("Import directory"):
137     _("Import file"),
138 harbaum 1 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 harbaum 107
155     if(filename) {
156     gpx_dialog_t *dialog = gpx_busy_dialog_new(GTK_WIDGET(appdata->window));
157 harbaum 1
158 harbaum 107 if(!whole_dir)
159 harbaum 204 gpx = gpx_parse(dialog, filename, appdata->username);
160 harbaum 107 else {
161     /* cur trailing '/' if present */
162     if(strlastchr(filename) == '/')
163     filename[strlen(filename)] = 0;
164    
165 harbaum 204 gpx = gpx_parse_dir(dialog, filename, appdata->username);
166 harbaum 107 }
167 harbaum 204
168 harbaum 107 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 harbaum 1
188 harbaum 107 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 harbaum 1 }
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 harbaum 142 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 harbaum 214 crumb_add(appdata, cache->name, CRUMB_CACHE, cache);
226    
227 harbaum 142 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 harbaum 1 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 harbaum 45 #ifdef USE_MAEMO
243 harbaum 43 /* 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 harbaum 45 #endif
247 harbaum 43
248 harbaum 1 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 harbaum 142 cachelist_goto_cache(appdata, cache);
252 harbaum 1 }
253     }
254    
255     typedef struct {
256     appdata_t *appdata;
257     GtkTreePath *path;
258     gboolean done;
259 harbaum 238 } cachelist_context_t;
260 harbaum 1
261     static gboolean cachelist_expose(GtkWidget *widget, GdkEventExpose *event,
262     gpointer user_data) {
263 harbaum 238 cachelist_context_t *ce = (cachelist_context_t*)user_data;
264 harbaum 1
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 harbaum 238 guint handler_id =
280     (guint)g_object_get_data(G_OBJECT(user_data), "handler_id");
281 harbaum 1
282 harbaum 238 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 harbaum 1 }
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 harbaum 238 printf("redrawing cachelist\n");
303    
304 harbaum 11 if(!appdata->cur_view) {
305     printf("cachelist redraw: no active view\n");
306     return;
307     }
308    
309 harbaum 1 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 harbaum 11 #ifdef USE_STACKABLE_WINDOW
320     HildonWindowStack *stack = hildon_window_stack_get_default();
321 harbaum 237 GtkWidget *container = hildon_window_stack_peek(stack);
322     #else
323     GtkWidget *container = appdata->vbox;
324 harbaum 11 #endif
325    
326     gtk_container_remove(GTK_CONTAINER(container), appdata->cur_view);
327 harbaum 1 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 harbaum 11 #ifdef USE_STACKABLE_WINDOW
339 harbaum 237 gtk_container_add(GTK_CONTAINER(container), appdata->cur_view);
340     #else
341     gtk_box_pack_start_defaults(GTK_BOX(container), appdata->cur_view);
342 harbaum 11 #endif
343    
344     gtk_widget_show_all(container);
345 harbaum 1 }
346     }
347    
348    
349     static gboolean cachelist_update(gpointer data) {
350 harbaum 238 appdata_t *appdata =
351     (appdata_t*)g_object_get_data(G_OBJECT(data), "appdata");
352     g_assert(appdata);
353 harbaum 1
354     printf("cachelist timer fired!\n");
355    
356 harbaum 238 /* 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 harbaum 1
364 harbaum 11 if(appdata->cur_cache)
365     return TRUE;
366    
367 harbaum 129 #ifdef USE_MAEMO
368 harbaum 1 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 harbaum 129 #endif
372 harbaum 1
373     if(appdata->cachelist_update)
374     cachelist_redraw(appdata);
375 harbaum 238 else
376     printf("update disabled\n");
377 harbaum 122
378 harbaum 1 return TRUE;
379     }
380    
381 harbaum 238 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 harbaum 11 printf("cachelist timer reset\n");
391 harbaum 238 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 harbaum 1 }
395    
396     static gboolean cachelist_update_reset0(GtkWidget *widget,
397     GdkEventButton *event,
398     gpointer user_data) {
399 harbaum 238 cachelist_timer_reset(GTK_WIDGET(user_data));
400 harbaum 1 return FALSE;
401     }
402    
403     static void cachelist_update_reset1(GtkAdjustment *adj,
404     gpointer user_data) {
405 harbaum 238 cachelist_timer_reset(GTK_WIDGET(user_data));
406 harbaum 1 }
407    
408 harbaum 238 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 harbaum 1 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 harbaum 205 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 harbaum 1
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 harbaum 238 cachelist_context_t *ce = g_new0(cachelist_context_t, 1);
588 harbaum 1 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 harbaum 238 GtkWidget *container = gtk_scrolled_window_new (NULL, NULL);
598     gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(container),
599 harbaum 1 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
600 harbaum 238 gtk_container_add(GTK_CONTAINER(container), view);
601 harbaum 1 #else
602 harbaum 238 GtkWidget *container = hildon_pannable_area_new();
603     gtk_container_add(GTK_CONTAINER(container), view);
604 harbaum 1 #endif
605    
606 harbaum 238 g_signal_connect(view, "destroy",
607     (GCallback)cachelist_destroy, container);
608 harbaum 1
609     /* update timer is being reset if the user scrolls or selects */
610     g_signal_connect(view, "button-press-event",
611 harbaum 238 (GCallback)cachelist_update_reset0, container);
612 harbaum 1
613 harbaum 238 /* 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 harbaum 1 #ifndef USE_PANNABLE_AREA
634     g_signal_connect(gtk_scrolled_window_get_vadjustment(
635 harbaum 238 GTK_SCROLLED_WINDOW(container)),
636     "value-changed",
637     (GCallback)cachelist_update_reset1, container);
638 harbaum 1 #else
639     g_signal_connect(hildon_pannable_area_get_vadjustment(
640 harbaum 238 HILDON_PANNABLE_AREA(container)),
641     "value-changed",
642     (GCallback)cachelist_update_reset1, container);
643 harbaum 1
644     #endif
645 harbaum 238 return container;
646 harbaum 1 }
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 harbaum 11 static void search_result_free(gpx_t *result);
670    
671     void on_cachelist_destroy(GtkWidget *widget, appdata_t *appdata) {
672 harbaum 211 printf("cachelist destroy\n");
673    
674 harbaum 226 HildonWindowStack *stack = hildon_window_stack_get_default();
675     appdata->window = HILDON_WINDOW(hildon_window_stack_peek(stack));
676    
677 harbaum 11 if(appdata->search_results) {
678     search_result_free(appdata->search_results);
679     appdata->search_results = NULL;
680     }
681     appdata->cur_gpx = NULL;
682    
683 harbaum 130 #ifdef ENABLE_OSM_GPS_MAP
684     map_update(appdata);
685     #endif
686    
687 harbaum 11 /* restore cur_view */
688     appdata->cur_view = g_object_get_data(G_OBJECT(widget), "cur_view");
689     }
690    
691 harbaum 1 void cachelist_dialog(appdata_t *appdata, gpx_t *gpx) {
692     GtkWidget *window = hildon_stackable_window_new();
693 harbaum 226 appdata->window = HILDON_WINDOW(window);
694    
695 harbaum 11 /* store last "cur_view" in window */
696     g_object_set_data(G_OBJECT(window), "cur_view", appdata->cur_view);
697 harbaum 1
698 harbaum 11 appdata->cur_gpx = gpx;
699 harbaum 229 gtk_window_set_title(GTK_WINDOW(window), gpx->name);
700 harbaum 1
701 harbaum 11 appdata->cur_view = cachelist_create(appdata, gpx, NULL);
702     gtk_container_add(GTK_CONTAINER(window), appdata->cur_view);
703    
704    
705 harbaum 3 hildon_window_set_app_menu(HILDON_WINDOW(window),
706     menu_create(appdata, MENU_CACHELIST));
707    
708 harbaum 11 g_signal_connect(G_OBJECT(window), "destroy",
709     G_CALLBACK(on_cachelist_destroy), appdata);
710    
711 harbaum 1 gtk_widget_show_all(window);
712 harbaum 130
713     #ifdef ENABLE_OSM_GPS_MAP
714     map_update(appdata);
715     #endif
716 harbaum 1 }
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 harbaum 211 void gpxlist_set(GtkListStore *store, GtkTreeIter *iter, gpx_t *gpx) {
749 harbaum 1 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 harbaum 231 GPXLIST_COL_DELETE, icon_get(ICON_MISC, 2),
774 harbaum 1 #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 harbaum 142 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 harbaum 1 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 harbaum 45 #ifdef USE_MAEMO
848 harbaum 43 /* 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 harbaum 45 #endif
852 harbaum 43
853 harbaum 1 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 harbaum 164 #if !defined(USE_MAEMO) || (MAEMO_VERSION_MAJOR < 5)
866 harbaum 1 /* ask user what he wants */
867     GtkWidget *dialog = gtk_message_dialog_new(
868     GTK_WINDOW(appdata->window),
869     GTK_DIALOG_DESTROY_WITH_PARENT,
870 harbaum 133 GTK_MESSAGE_QUESTION,
871     GTK_BUTTONS_CANCEL,
872 harbaum 1 _("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 harbaum 164 gtk_window_set_title(GTK_WINDOW(dialog), _("Close or remove entry?"));
881     #else
882 harbaum 165
883 harbaum 164 GtkWidget *dialog =
884 harbaum 165 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 harbaum 164 _("Do you want to close this entry only or do "
899 harbaum 165 "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 harbaum 164 #endif
909    
910 harbaum 1 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 harbaum 204 new = gpx_parse_dir(dialog, gpx->filename, appdata->username);
943 harbaum 1 else
944 harbaum 204 new = gpx_parse(dialog, gpx->filename, appdata->username);
945 harbaum 1
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 harbaum 142
981     gpxlist_goto_cachelist(appdata, gpx);
982 harbaum 1 }
983     }
984     }
985    
986 harbaum 211 /* 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 harbaum 1 #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 harbaum 237 printf("building gpx list, items = %d\n", appdata->gpxlist_items);
1047    
1048 harbaum 1 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 harbaum 143 #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 harbaum 1 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 harbaum 143 // "sensitive", GPXLIST_COL_OPEN,
1121 harbaum 1 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 harbaum 113 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 harbaum 1 }
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 harbaum 223 void gpxlist_add(appdata_t *appdata, gpx_t *new) {
1185 harbaum 1 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 harbaum 223
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 harbaum 1 }
1205    
1206     /******************** end of gpxlist ********************/
1207    
1208     /******************** begin of menu *********************/
1209    
1210     static void
1211     cb_menu_about(GtkWidget *window, gpointer data) {
1212 harbaum 228 about_box((appdata_t*)data);
1213 harbaum 1 }
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 harbaum 156 else if(cache->owner && cache->owner->name && (what & SEARCH_OWNER) &&
1385     strcasestr(cache->owner->name, phrase))
1386 harbaum 1 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 harbaum 168
1415 harbaum 1 static void callback_finds_toggled(GtkWidget *widget, gpointer data ) {
1416     search_context_t *context = (search_context_t*)data;
1417    
1418 harbaum 168 gboolean in_finds = check_button_get_active(context->in_finds);
1419 harbaum 1
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 harbaum 168 context.in_id = check_button_new_with_label(_("Waypoint IDs"));
1449     check_button_set_active(context.in_id, appdata->search & SEARCH_ID);
1450 harbaum 1 gtk_table_attach_defaults(GTK_TABLE(table), context.in_id, 0, 1, 0, 1);
1451    
1452 harbaum 168 context.in_name = check_button_new_with_label(_("Names"));
1453     check_button_set_active(context.in_name, appdata->search & SEARCH_NAME);
1454 harbaum 1 gtk_table_attach_defaults(GTK_TABLE(table), context.in_name, 1, 2, 0, 1);
1455    
1456 harbaum 168 context.in_desc = check_button_new_with_label(_("Descriptions"));
1457     check_button_set_active(context.in_desc, appdata->search & SEARCH_DESC);
1458 harbaum 1 gtk_table_attach_defaults(GTK_TABLE(table), context.in_desc, 0, 1, 1, 2);
1459    
1460 harbaum 168 context.in_owner = check_button_new_with_label(_("Owner"));
1461     check_button_set_active(context.in_owner, appdata->search & SEARCH_OWNER);
1462 harbaum 1 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 harbaum 212 context.entry = entry_new();
1471 harbaum 1 gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(dialog)->vbox),
1472 harbaum 190 context.entry);
1473    
1474 harbaum 1 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 harbaum 168 context.in_finds = check_button_new_with_label(_("Search finds for"));
1485     check_button_set_active(context.in_finds, appdata->search & SEARCH_FINDS);
1486 harbaum 1 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     #ifndef USE_MAEMO
1491     GtkObject *adj = gtk_adjustment_new(appdata->search_days, 0, 99, 1, 10, 10);
1492     context.spinner = gtk_spin_button_new(GTK_ADJUSTMENT(adj), 1, 0);
1493     #else
1494     context.spinner = hildon_number_editor_new(0, 99);
1495     hildon_number_editor_set_value(HILDON_NUMBER_EDITOR(context.spinner),
1496     appdata->search_days);
1497     #endif
1498     gtk_box_pack_start_defaults(GTK_BOX(hbox), context.spinner);
1499    
1500     gtk_box_pack_start_defaults(GTK_BOX(hbox), gtk_label_new(_("days")));
1501    
1502     gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(dialog)->vbox), hbox);
1503    
1504     /* -------------------------------------------------------------- */
1505    
1506     gtk_widget_show_all(dialog);
1507     callback_finds_toggled(NULL, &context);
1508    
1509     if(GTK_RESPONSE_ACCEPT == gtk_dialog_run(GTK_DIALOG(dialog))) {
1510     char *p = strdup(gtk_entry_get_text(GTK_ENTRY(context.entry)));
1511    
1512     /* update saved search string */
1513     if(appdata->search_str) free(appdata->search_str);
1514     if(strlen(p) > 0)
1515     appdata->search_str = strdup(p);
1516    
1517     #ifndef USE_MAEMO
1518     appdata->search_days = gtk_spin_button_get_value_as_int(
1519     GTK_SPIN_BUTTON(context.spinner));
1520     #else
1521     appdata->search_days = hildon_number_editor_get_value(
1522     HILDON_NUMBER_EDITOR(context.spinner));
1523     #endif
1524    
1525 harbaum 168 if(check_button_get_active(context.in_finds))
1526 harbaum 1 appdata->search |= SEARCH_FINDS;
1527     else
1528     appdata->search &= ~SEARCH_FINDS;
1529    
1530 harbaum 168 if(check_button_get_active(context.in_id))
1531 harbaum 1 appdata->search |= SEARCH_ID;
1532     else
1533     appdata->search &= ~SEARCH_ID;
1534    
1535 harbaum 168 if(check_button_get_active(context.in_name))
1536 harbaum 1 appdata->search |= SEARCH_NAME;
1537     else
1538     appdata->search &= ~SEARCH_NAME;
1539    
1540 harbaum 168 if(check_button_get_active(context.in_desc))
1541 harbaum 1 appdata->search |= SEARCH_DESC;
1542     else
1543     appdata->search &= ~SEARCH_DESC;
1544    
1545 harbaum 168 if(check_button_get_active(context.in_owner))
1546 harbaum 1 appdata->search |= SEARCH_OWNER;
1547     else
1548     appdata->search &= ~SEARCH_OWNER;
1549    
1550     gtk_widget_destroy(dialog);
1551    
1552     /* don't search if we are asked to search for nothing */
1553     if(((appdata->search & (SEARCH_ID|SEARCH_NAME|SEARCH_DESC|SEARCH_OWNER)) &&
1554     strlen(p) > 0) || (appdata->search & SEARCH_FINDS)) {
1555    
1556     printf("Search for %s (flags = %x)...\n", p, appdata->search);
1557    
1558 harbaum 128 #if !defined(USE_BREAD_CRUMB_TRAIL) && !defined(BCT)
1559 harbaum 12 gpx_t *found =
1560 harbaum 11 search_do(appdata, appdata->gpx, p, appdata->search, FALSE);
1561 harbaum 1
1562     /* do search result dialog here ... */
1563 harbaum 12 cachelist_dialog(appdata, found);
1564 harbaum 11 #ifndef USE_STACKABLE_WINDOW
1565 harbaum 12 search_result_free(found);
1566     #else
1567     appdata->search_results = found;
1568 harbaum 11 #endif
1569 harbaum 1 #else
1570 harbaum 11 gpx_t *found = NULL;
1571    
1572 harbaum 1 if(appdata->cur_gpx)
1573     found = search_do(appdata, appdata->cur_gpx, p, appdata->search, TRUE);
1574     else
1575     found = search_do(appdata, appdata->gpx, p, appdata->search, FALSE);
1576    
1577     gtk_container_remove(GTK_CONTAINER(appdata->vbox), appdata->cur_view);
1578     appdata->cur_view = cachelist_create(appdata, found, NULL);
1579     gtk_box_pack_start_defaults(GTK_BOX(appdata->vbox), appdata->cur_view);
1580     gtk_widget_show_all(appdata->vbox);
1581     crumb_add(appdata, found->name,
1582     appdata->cur_gpx?CRUMB_SEARCH_GPX:CRUMB_SEARCH_GLOBAL, found);
1583     #endif
1584     } else
1585     printf("No valid search: \"%s\" with flags %x!\n", p, appdata->search);
1586    
1587     free(p);
1588     } else
1589     gtk_widget_destroy(dialog);
1590     }
1591    
1592 harbaum 40 static void on_window_destroy (GtkWidget *widget, gpointer data);
1593 harbaum 1
1594     #ifndef USE_MAEMO
1595     static void
1596     cb_menu_quit(GtkWidget *window, gpointer data) {
1597     on_window_destroy(window, data);
1598     }
1599     #endif
1600    
1601 harbaum 2 #ifndef NO_COPY_N_PASTE
1602 harbaum 1 static void
1603     cb_menu_cut(GtkWidget *widget, gpointer data) {
1604     appdata_t *appdata = (appdata_t*)data;
1605    
1606     if(appdata->active_buffer) {
1607     if(GTK_WIDGET_TYPE(appdata->active_buffer) == GTK_TYPE_TEXT_BUFFER) {
1608     gtk_text_buffer_cut_clipboard(GTK_TEXT_BUFFER(appdata->active_buffer),
1609     appdata->clipboard, TRUE);
1610     } else
1611     printf("cut: ERROR, not a text buffer\n");
1612     } else
1613     printf("cut: ERROR, no active buffer\n");
1614     }
1615    
1616     static void
1617     cb_menu_copy(GtkWidget *widget, gpointer data) {
1618     appdata_t *appdata = (appdata_t*)data;
1619    
1620     if(appdata->active_buffer) {
1621     if(GTK_WIDGET_TYPE(appdata->active_buffer) == GTK_TYPE_TEXT_BUFFER) {
1622     gtk_text_buffer_copy_clipboard(GTK_TEXT_BUFFER(appdata->active_buffer),
1623     appdata->clipboard);
1624     } else if(GTK_WIDGET_TYPE(appdata->active_buffer) == gtk_html_get_type()) {
1625     printf("copy from html buffer\n");
1626     html_copy_to_clipboard(appdata);
1627     } else
1628     printf("copy: ERROR, not a text nor a html buffer\n");
1629     } else
1630     printf("copy: ERROR, no active buffer\n");
1631     }
1632    
1633     static void
1634     cb_menu_paste(GtkWidget *widget, gpointer data) {
1635     appdata_t *appdata = (appdata_t*)data;
1636    
1637     if(appdata->active_buffer) {
1638     if(GTK_WIDGET_TYPE(appdata->active_buffer) == GTK_TYPE_TEXT_BUFFER) {
1639     gtk_text_buffer_paste_clipboard(GTK_TEXT_BUFFER(appdata->active_buffer),
1640     appdata->clipboard, NULL, TRUE);
1641     } else
1642     printf("paste: ERROR, not a text buffer\n");
1643     } else
1644     printf("paste: ERROR, no active buffer\n");
1645     }
1646 harbaum 2 #endif
1647 harbaum 1
1648 harbaum 6 static void
1649     cb_menu_export_log(GtkWidget *widget, gpointer data) {
1650     appdata_t *appdata = (appdata_t*)data;
1651     notes_log_export(appdata);
1652     }
1653 harbaum 5
1654 harbaum 122 #ifdef ENABLE_MAEMO_MAPPER
1655 harbaum 6 static void
1656     cb_menu_export_mmpoi(GtkWidget *widget, gpointer data) {
1657     appdata_t *appdata = (appdata_t*)data;
1658     mmpoi_export(appdata);
1659     }
1660     #endif
1661 harbaum 5
1662 harbaum 6 static void
1663     cb_menu_export_garmin(GtkWidget *widget, gpointer data) {
1664     appdata_t *appdata = (appdata_t*)data;
1665     garmin_export(appdata);
1666 harbaum 1 }
1667    
1668 harbaum 32 #ifdef ENABLE_OSM_GPS_MAP
1669 harbaum 6 static void
1670 harbaum 32 cb_menu_map(GtkWidget *window, gpointer data) {
1671     map((appdata_t *)data);
1672     }
1673     #endif
1674    
1675     static void
1676 harbaum 6 cb_menu_geomath(GtkWidget *window, gpointer data) {
1677     geomath_dialog((appdata_t *)data);
1678     }
1679 harbaum 5
1680 harbaum 6 static void
1681     cb_menu_geotext(GtkWidget *window, gpointer data) {
1682     geotext_dialog((appdata_t *)data);
1683 harbaum 1 }
1684    
1685 harbaum 6 static void
1686     cb_menu_precpos(GtkWidget *window, gpointer data) {
1687     precise_position((appdata_t *)data);
1688 harbaum 1 }
1689    
1690 harbaum 193 static void
1691     cb_menu_geotoad(GtkWidget *window, gpointer data) {
1692     geotoad((appdata_t *)data);
1693     }
1694    
1695 harbaum 6 #ifdef USE_STACKABLE_WINDOW
1696 harbaum 30 typedef struct {
1697     char *label, *desc;
1698     GtkSignalFunc activate_cb;
1699     } menu_entry_t;
1700 harbaum 6
1701 harbaum 30 typedef struct {
1702     const char *title;
1703     const menu_entry_t *menu;
1704     int len;
1705     } submenu_t;
1706 harbaum 3
1707 harbaum 30 #define COLUMNS 1
1708 harbaum 4
1709 harbaum 30 void on_submenu_entry_clicked(GtkButton *button, GtkWidget *menu) {
1710 harbaum 5
1711 harbaum 30 /* force closing of submenu dialog */
1712     gtk_dialog_response(GTK_DIALOG(menu), GTK_RESPONSE_NONE);
1713     gtk_widget_hide(menu);
1714    
1715     /* let gtk clean up */
1716     while(gtk_events_pending())
1717     gtk_main_iteration();
1718 harbaum 11 }
1719    
1720 harbaum 30 static GtkWidget *app_submenu_create(appdata_t *appdata,
1721     const submenu_t *submenu) {
1722 harbaum 5
1723 harbaum 30 /* create a oridinary dialog box */
1724     GtkWidget *dialog = gtk_dialog_new();
1725     gtk_window_set_title(GTK_WINDOW(dialog), _(submenu->title));
1726     gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
1727     gtk_window_set_transient_for(GTK_WINDOW(dialog),
1728     GTK_WINDOW(appdata->window));
1729     gtk_dialog_set_has_separator(GTK_DIALOG(dialog), FALSE);
1730 harbaum 5
1731 harbaum 30 GtkWidget *table = gtk_table_new(submenu->len/COLUMNS, COLUMNS, TRUE);
1732     int x = 0, y = 0;
1733 harbaum 11
1734 harbaum 30 const menu_entry_t *menu_entries = submenu->menu;
1735     while(menu_entries->label) {
1736     GtkWidget *button = NULL;
1737 harbaum 21
1738 harbaum 30 button = hildon_button_new_with_text(
1739 harbaum 6 HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH,
1740     HILDON_BUTTON_ARRANGEMENT_VERTICAL,
1741 harbaum 30 _(menu_entries->label), _(menu_entries->desc));
1742 harbaum 5
1743 harbaum 31 /* try to center both texts */
1744     hildon_button_set_title_alignment(HILDON_BUTTON(button), 0.5, 0.5);
1745     hildon_button_set_value_alignment(HILDON_BUTTON(button), 0.5, 0.5);
1746 harbaum 6
1747 harbaum 31 g_signal_connect(button, "clicked",
1748     G_CALLBACK(on_submenu_entry_clicked), dialog);
1749    
1750     g_signal_connect(button, "clicked",
1751     menu_entries->activate_cb, appdata);
1752    
1753 harbaum 30 gtk_table_attach_defaults(GTK_TABLE(table), button, x, x+1, y, y+1);
1754    
1755     x++;
1756     if(x == COLUMNS) { x = 0; y++; }
1757 harbaum 3
1758 harbaum 30 menu_entries++;
1759     }
1760 harbaum 20
1761 harbaum 30 gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(dialog)->vbox), table);
1762    
1763     return dialog;
1764 harbaum 3 }
1765    
1766 harbaum 30 /* popup the dialog shaped submenu */
1767     static void submenu_popup(GtkWidget *menu) {
1768     gtk_widget_show_all(menu);
1769     gtk_dialog_run(GTK_DIALOG(menu));
1770     gtk_widget_hide(menu);
1771     }
1772    
1773     static void submenu_cleanup(GtkWidget *menu) {
1774     gtk_widget_destroy(menu);
1775     }
1776    
1777     static const menu_entry_t submenu_export_entries[] = {
1778 harbaum 122 #ifdef ENABLE_MAEMO_MAPPER
1779 harbaum 30 { "Export to Maemo Mapper" , "Save a Maemo Mapper POI file",
1780     G_CALLBACK(cb_menu_export_mmpoi) },
1781 harbaum 122 #endif
1782 harbaum 30 { "Export Field Notes", "Save a Garmin Field Notes file",
1783     G_CALLBACK(cb_menu_export_log) },
1784     { "Export Garmin GPX", "Save modified waypoints in GPX file",
1785     G_CALLBACK(cb_menu_export_garmin) },
1786     { NULL, NULL, NULL }
1787     };
1788    
1789     static const submenu_t submenu_export = {
1790     "Export", submenu_export_entries,
1791     sizeof(submenu_export_entries)/sizeof(menu_entry_t)-1
1792     };
1793    
1794     /* the export submenu */
1795     void on_export_clicked(GtkButton *button, appdata_t *appdata) {
1796     if(!appdata->export_menu)
1797     appdata->export_menu = app_submenu_create(appdata, &submenu_export);
1798    
1799     submenu_popup(appdata->export_menu);
1800     }
1801    
1802     static const menu_entry_t submenu_tools_entries[] = {
1803     { "Geomath", "Geocoordinate calculation",
1804     G_CALLBACK(cb_menu_geomath) },
1805     { "Geotext", "Text analysis",
1806     G_CALLBACK(cb_menu_geotext) },
1807     { "Precise Position", "Calculate a precise GPS position",
1808     G_CALLBACK(cb_menu_precpos) },
1809 harbaum 193 { "GeoToad", "Use GeoToad online downloader",
1810     G_CALLBACK(cb_menu_geotoad) },
1811 harbaum 30 { NULL, NULL, NULL }
1812     };
1813    
1814     static const submenu_t submenu_tools = {
1815     "Tools", submenu_tools_entries,
1816     sizeof(submenu_tools_entries)/sizeof(menu_entry_t)-1
1817     };
1818    
1819 harbaum 20 /* the tools submenu */
1820     void on_tools_clicked(GtkButton *button, appdata_t *appdata) {
1821     if(!appdata->tools_menu)
1822 harbaum 30 appdata->tools_menu = app_submenu_create(appdata, &submenu_tools);
1823 harbaum 20
1824 harbaum 30 submenu_popup(appdata->tools_menu);
1825 harbaum 20 }
1826    
1827 harbaum 3 HildonAppMenu *menu_create(appdata_t *appdata, int mode) {
1828     GtkWidget *button;
1829     HildonAppMenu *menu = HILDON_APP_MENU(hildon_app_menu_new());
1830    
1831     /* ------- */
1832 harbaum 7 button = gtk_button_new_with_label(_("About"));
1833     g_signal_connect_after(button, "clicked",
1834     G_CALLBACK(cb_menu_about), appdata);
1835     hildon_app_menu_append(menu, GTK_BUTTON(button));
1836    
1837 harbaum 3 button = gtk_button_new_with_label(_("Settings"));
1838     g_signal_connect_after(button, "clicked", G_CALLBACK(cb_menu_settings),
1839     appdata);
1840     hildon_app_menu_append(menu, GTK_BUTTON(button));
1841    
1842     if(mode == MENU_GPXLIST) {
1843     button = gtk_button_new_with_label(_("Import file"));
1844     g_signal_connect_after(button, "clicked",
1845     G_CALLBACK(cb_menu_add), appdata);
1846     hildon_app_menu_append(menu, GTK_BUTTON(button));
1847    
1848 harbaum 7 button = gtk_button_new_with_label(_("Import directory"));
1849 harbaum 3 g_signal_connect_after(button, "clicked",
1850     G_CALLBACK(cb_menu_adddir), appdata);
1851     hildon_app_menu_append(menu, GTK_BUTTON(button));
1852    
1853 harbaum 4 button = gtk_button_new_with_label(_("Export"));
1854     g_signal_connect_after(button, "clicked",
1855     G_CALLBACK(on_export_clicked), appdata);
1856     hildon_app_menu_append(menu, GTK_BUTTON(button));
1857 harbaum 238 }
1858 harbaum 4
1859 harbaum 243 /* if search results exist, don't allow another search */
1860     if(!appdata->search_results &&
1861     ((mode == MENU_GPXLIST) || (mode == MENU_CACHELIST))) {
1862 harbaum 4 button = gtk_button_new_with_label(_("Search"));
1863     g_signal_connect_after(button, "clicked",
1864     G_CALLBACK(cb_menu_search), appdata);
1865     hildon_app_menu_append(menu, GTK_BUTTON(button));
1866     }
1867    
1868 harbaum 5 button = gtk_button_new_with_label(_("Tools"));
1869     g_signal_connect_after(button, "clicked",
1870     G_CALLBACK(on_tools_clicked), appdata);
1871     hildon_app_menu_append(menu, GTK_BUTTON(button));
1872 harbaum 4
1873 harbaum 44 #ifdef ENABLE_OSM_GPS_MAP
1874     button = gtk_button_new_with_label(_("Map"));
1875     g_signal_connect_after(button, "clicked",
1876     G_CALLBACK(cb_menu_map), appdata);
1877     hildon_app_menu_append(menu, GTK_BUTTON(button));
1878     #endif
1879    
1880 harbaum 15 #ifdef HILDON_HELP
1881     button = gtk_button_new_with_label(_("Help"));
1882     g_signal_connect_after(button, "clicked",
1883     G_CALLBACK(cb_menu_help), appdata);
1884     hildon_app_menu_append(menu, GTK_BUTTON(button));
1885     #endif
1886 harbaum 3
1887 harbaum 15 gtk_widget_show_all(GTK_WIDGET(menu));
1888    
1889 harbaum 3 return menu;
1890     }
1891     #else
1892 harbaum 5
1893 harbaum 1 void menu_create(appdata_t *appdata) {
1894     GtkWidget *menu, *item;
1895     menu = gtk_menu_new();
1896    
1897 harbaum 128 #if defined(USE_BREAD_CRUMB_TRAIL) || defined(BCT)
1898 harbaum 1 appdata->menu_import =
1899     #endif
1900     item = gtk_menu_item_new_with_label(_("Import"));
1901     gtk_menu_append(GTK_MENU_SHELL(menu), item);
1902     GtkWidget *submenu = gtk_menu_new();
1903     gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
1904    
1905 harbaum 7 item = gtk_menu_item_new_with_label( _("File") );
1906 harbaum 1 gtk_menu_append(GTK_MENU_SHELL(submenu), item);
1907     g_signal_connect(item, "activate", GTK_SIGNAL_FUNC(cb_menu_add), appdata);
1908    
1909 harbaum 7 item = gtk_menu_item_new_with_label( _("Directory") );
1910 harbaum 1 gtk_menu_append(GTK_MENU_SHELL(submenu), item);
1911     g_signal_connect(item, "activate", GTK_SIGNAL_FUNC(cb_menu_adddir), appdata);
1912    
1913     #ifndef USE_PANNABLE_AREA
1914     gtk_menu_append(GTK_MENU_SHELL(submenu), gtk_separator_menu_item_new());
1915    
1916     appdata->menu_close =
1917     item = gtk_menu_item_new_with_label( _("Close") );
1918     gtk_menu_append(GTK_MENU_SHELL(submenu), item);
1919     g_signal_connect(item, "activate", GTK_SIGNAL_FUNC(cb_menu_close), appdata);
1920    
1921     appdata->menu_remove =
1922     item = gtk_menu_item_new_with_label( _("Remove") );
1923     gtk_menu_append(GTK_MENU_SHELL(submenu), item);
1924     g_signal_connect(item, "activate", GTK_SIGNAL_FUNC(cb_menu_remove), appdata);
1925     #endif
1926    
1927 harbaum 128 #if defined(USE_BREAD_CRUMB_TRAIL) || defined(BCT)
1928 harbaum 1 appdata->menu_export =
1929     #endif
1930     item = gtk_menu_item_new_with_label(_("Export"));
1931     gtk_menu_append(GTK_MENU_SHELL(menu), item);
1932     submenu = gtk_menu_new();
1933     gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
1934    
1935 harbaum 122 #ifdef ENABLE_MAEMO_MAPPER
1936 harbaum 7 item = gtk_menu_item_new_with_label( _("Maemo Mapper POI") );
1937 harbaum 1 gtk_menu_append(GTK_MENU_SHELL(submenu), item);
1938     g_signal_connect(item, "activate",
1939     GTK_SIGNAL_FUNC(cb_menu_export_mmpoi), appdata);
1940     #endif
1941    
1942 harbaum 7 item = gtk_menu_item_new_with_label( _("Garmin Field Notes") );
1943 harbaum 1 gtk_menu_append(GTK_MENU_SHELL(submenu), item);
1944     g_signal_connect(item, "activate",
1945     GTK_SIGNAL_FUNC(cb_menu_export_log), appdata);
1946    
1947 harbaum 7 item = gtk_menu_item_new_with_label( _("Garmin GPX") );
1948 harbaum 1 gtk_menu_append(GTK_MENU_SHELL(submenu), item);
1949     g_signal_connect(item, "activate",
1950     GTK_SIGNAL_FUNC(cb_menu_export_garmin), appdata);
1951    
1952 harbaum 128 #if defined(USE_BREAD_CRUMB_TRAIL) || defined(BCT)
1953 harbaum 1 appdata->menu_search =
1954     #endif
1955 harbaum 7 item = gtk_menu_item_new_with_label( _("Search") );
1956 harbaum 1 gtk_menu_append(GTK_MENU_SHELL(menu), item);
1957     g_signal_connect(item, "activate", GTK_SIGNAL_FUNC(cb_menu_search), appdata);
1958    
1959     gtk_menu_append(GTK_MENU_SHELL(menu), gtk_separator_menu_item_new());
1960 harbaum 2
1961 harbaum 133 /* ----------- copy'n paste submenu ----------------- */
1962 harbaum 2 #ifndef NO_COPY_N_PASTE
1963 harbaum 1 item = gtk_menu_item_new_with_label(_("Edit"));
1964     gtk_menu_append(GTK_MENU_SHELL(menu), item);
1965     submenu = gtk_menu_new();
1966     gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
1967    
1968     appdata->menu_cut = item = gtk_menu_item_new_with_label( _("Cut") );
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_cut), appdata);
1972     appdata->menu_copy = item = gtk_menu_item_new_with_label( _("Copy") );
1973     gtk_widget_set_sensitive(item, FALSE);
1974     gtk_menu_append(GTK_MENU_SHELL(submenu), item);
1975     g_signal_connect(item, "activate", GTK_SIGNAL_FUNC(cb_menu_copy), appdata);
1976     appdata->menu_paste = item = gtk_menu_item_new_with_label( _("Paste") );
1977     gtk_widget_set_sensitive(item, FALSE);
1978     gtk_menu_append(GTK_MENU_SHELL(submenu), item);
1979     g_signal_connect(item, "activate", GTK_SIGNAL_FUNC(cb_menu_paste), appdata);
1980 harbaum 2 #endif
1981 harbaum 1
1982 harbaum 3 item = gtk_menu_item_new_with_label( _("Settings") );
1983 harbaum 1 gtk_menu_append(GTK_MENU_SHELL(menu), item);
1984     g_signal_connect(item, "activate", GTK_SIGNAL_FUNC(cb_menu_settings),
1985     appdata);
1986    
1987     gtk_menu_append(GTK_MENU_SHELL(menu), gtk_separator_menu_item_new());
1988    
1989 harbaum 32 #ifdef ENABLE_OSM_GPS_MAP
1990     item = gtk_menu_item_new_with_label( _("Map") );
1991 harbaum 63 gtk_menu_append(GTK_MENU_SHELL(menu), item);
1992 harbaum 32 g_signal_connect(item, "activate",
1993     GTK_SIGNAL_FUNC(cb_menu_map), appdata);
1994     #endif
1995    
1996 harbaum 63 item = gtk_menu_item_new_with_label(_("Tools"));
1997     gtk_menu_append(GTK_MENU_SHELL(menu), item);
1998     submenu = gtk_menu_new();
1999     gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
2000    
2001 harbaum 7 item = gtk_menu_item_new_with_label( _("Geomath") );
2002 harbaum 1 gtk_menu_append(GTK_MENU_SHELL(submenu), item);
2003     g_signal_connect(item, "activate",
2004     GTK_SIGNAL_FUNC(cb_menu_geomath), appdata);
2005    
2006 harbaum 7 item = gtk_menu_item_new_with_label( _("Geotext") );
2007 harbaum 1 gtk_menu_append(GTK_MENU_SHELL(submenu), item);
2008     g_signal_connect(item, "activate",
2009     GTK_SIGNAL_FUNC(cb_menu_geotext), appdata);
2010    
2011 harbaum 7 item = gtk_menu_item_new_with_label( _("Precise Position") );
2012 harbaum 1 gtk_menu_append(GTK_MENU_SHELL(submenu), item);
2013     g_signal_connect(item, "activate",
2014     GTK_SIGNAL_FUNC(cb_menu_precpos), appdata);
2015    
2016 harbaum 193 item = gtk_menu_item_new_with_label( _("GeoToad") );
2017     gtk_menu_append(GTK_MENU_SHELL(submenu), item);
2018     g_signal_connect(item, "activate",
2019     GTK_SIGNAL_FUNC(cb_menu_geotoad), appdata);
2020    
2021 harbaum 1 gtk_menu_append(GTK_MENU_SHELL(menu), gtk_separator_menu_item_new());
2022    
2023     #if defined(USE_MAEMO) && defined(HILDON_HELP)
2024 harbaum 7 item = gtk_menu_item_new_with_label( _("Help") );
2025 harbaum 1 gtk_menu_append(GTK_MENU_SHELL(menu), item);
2026     g_signal_connect(item, "activate", GTK_SIGNAL_FUNC(cb_menu_help), appdata);
2027     #endif
2028    
2029 harbaum 7 item = gtk_menu_item_new_with_label( _("About") );
2030 harbaum 1 gtk_menu_append(GTK_MENU_SHELL(menu), item);
2031     g_signal_connect(item, "activate", GTK_SIGNAL_FUNC(cb_menu_about), appdata);
2032    
2033     #ifndef USE_MAEMO
2034     item = gtk_menu_item_new_with_label( _("Quit") );
2035     gtk_menu_append(GTK_MENU_SHELL(menu), item);
2036     g_signal_connect(item, "activate", GTK_SIGNAL_FUNC(cb_menu_quit), appdata);
2037     #endif
2038    
2039     #ifdef USE_MAEMO
2040     hildon_window_set_menu(appdata->window, GTK_MENU(menu));
2041     #else
2042     /* attach ordinary gtk menu */
2043     GtkWidget *menu_bar = gtk_menu_bar_new();
2044    
2045     GtkWidget *root_menu = gtk_menu_item_new_with_label (_("Menu"));
2046     gtk_widget_show(root_menu);
2047    
2048     gtk_menu_bar_append(menu_bar, root_menu);
2049     gtk_menu_item_set_submenu(GTK_MENU_ITEM (root_menu), menu);
2050    
2051     gtk_widget_show(menu_bar);
2052     gtk_box_pack_start(GTK_BOX(appdata->vbox), menu_bar, 0, 0, 0);
2053     #endif
2054     }
2055 harbaum 3 #endif
2056 harbaum 1
2057     /********************* end of menu **********************/
2058    
2059     void cleanup(appdata_t *appdata) {
2060 harbaum 221 gconf_save_state(appdata);
2061 harbaum 243
2062 harbaum 1 gpx_free_all(appdata->gpx);
2063    
2064 harbaum 30 #ifdef USE_STACKABLE_WINDOW
2065     if(appdata->export_menu) submenu_cleanup(appdata->export_menu);
2066     if(appdata->tools_menu) submenu_cleanup(appdata->tools_menu);
2067     #endif
2068    
2069 harbaum 1 gnome_vfs_shutdown();
2070     icons_free();
2071     gps_release(appdata);
2072    
2073     if(appdata->search_results) {
2074     printf("freeing pending search\n");
2075     search_result_free(appdata->search_results);
2076     }
2077    
2078 harbaum 243 #ifdef USE_MAEMO
2079 harbaum 1 if(appdata->osso_context)
2080     osso_deinitialize(appdata->osso_context);
2081    
2082     appdata->program = NULL;
2083     #endif
2084    
2085     /* free chain of locations */
2086     location_t *loc = appdata->location;
2087     while(loc) {
2088     location_t *next = loc->next;
2089     if(loc->name) free(loc->name);
2090     free(loc);
2091     loc = next;
2092     }
2093    
2094     puts("everything is gone");
2095     }
2096    
2097 harbaum 40 static void on_window_destroy (GtkWidget *widget, gpointer data) {
2098 harbaum 1 appdata_t *appdata = (appdata_t*)data;
2099    
2100     gtk_main_quit();
2101     appdata->window = NULL;
2102     }
2103    
2104     gboolean on_window_key_press(GtkWidget *widget,
2105 harbaum 11 GdkEventKey *event, appdata_t *appdata) {
2106 harbaum 1 int handled = FALSE;
2107    
2108     // printf("key event %d\n", event->keyval);
2109    
2110     switch(event->keyval) {
2111     #ifdef USE_MAEMO
2112    
2113     #ifdef HILDON_HARDKEY_INCREASE
2114     case HILDON_HARDKEY_INCREASE:
2115     html_zoom(appdata, TRUE);
2116     handled = TRUE;
2117     break;
2118     #endif
2119    
2120     #ifdef HILDON_HARDKEY_DECREASE
2121     case HILDON_HARDKEY_DECREASE:
2122     html_zoom(appdata, FALSE);
2123     handled = TRUE;
2124     break;
2125     #endif
2126    
2127     #ifdef HILDON_HARDKEY_FULLSCREEN
2128     case HILDON_HARDKEY_FULLSCREEN:
2129     {
2130     appdata->fullscreen = !appdata->fullscreen;
2131    
2132     if(appdata->fullscreen)
2133     gtk_window_fullscreen(GTK_WINDOW(appdata->window));
2134     else
2135     gtk_window_unfullscreen(GTK_WINDOW(appdata->window));
2136    
2137     handled = TRUE;
2138     }
2139     break;
2140     #endif
2141    
2142     #else
2143     case '+':
2144     printf("zoom+\n");
2145     html_zoom(appdata, TRUE);
2146     handled = TRUE;
2147     break;
2148     case '-':
2149     printf("zoom-\n");
2150     html_zoom(appdata, FALSE);
2151     handled = TRUE;
2152     break;
2153     #endif
2154     }
2155    
2156     return handled;
2157     }
2158    
2159 harbaum 128 #if defined(USE_BREAD_CRUMB_TRAIL) || defined(BCT)
2160 harbaum 1 typedef struct {
2161     int level;
2162     appdata_t *appdata;
2163     gpointer data;
2164     } crumb_t;
2165    
2166     static void
2167     crumb_back(gpointer data) {
2168     crumb_t *crumb = (crumb_t*)data;
2169     printf("crumb_back called with %d\n", crumb->level);
2170    
2171     /* don't do anything if main window has already been destroyed */
2172     if(!crumb->appdata->window) {
2173     printf("Main window gone ...\n");
2174     return;
2175     }
2176    
2177     /* whatever is being displayed: we don't need it anymore */
2178     gtk_container_remove(GTK_CONTAINER(crumb->appdata->vbox),
2179     crumb->appdata->cur_view);
2180    
2181     /* back from cache to cachelist */
2182     if(crumb->level == CRUMB_CACHE) {
2183     gpx_t *gpx = crumb->appdata->search_results;
2184    
2185     if(!gpx) {
2186     gtk_widget_set_sensitive(crumb->appdata->menu_search, TRUE);
2187     gtk_widget_set_sensitive(crumb->appdata->menu_export, TRUE);
2188     printf("no search data found, return to gpx\n");
2189     gpx = crumb->appdata->cur_gpx;
2190     } else
2191     printf("returning to search result\n");
2192    
2193     g_assert(gpx != NULL);
2194    
2195     crumb->appdata->cur_view = cachelist_create(crumb->appdata, gpx,
2196     crumb->appdata->cur_cache);
2197    
2198     /* returning from cache view: invalidate cache reference */
2199     crumb->appdata->cur_cache = NULL;
2200    
2201     gtk_box_pack_start_defaults(GTK_BOX(crumb->appdata->vbox),
2202     crumb->appdata->cur_view);
2203     }
2204    
2205     if(crumb->level == CRUMB_SEARCH_GPX) {
2206     printf("returning from a local search!\n");
2207    
2208     g_assert((gpx_t*)crumb->data == crumb->appdata->search_results);
2209    
2210     search_result_free((gpx_t*)crumb->data);
2211     crumb->appdata->search_results = NULL;
2212    
2213     gtk_widget_set_sensitive(crumb->appdata->menu_search, TRUE);
2214    
2215     crumb->appdata->cur_view = cachelist_create(crumb->appdata,
2216     crumb->appdata->cur_gpx, NULL);
2217     gtk_box_pack_start_defaults(GTK_BOX(crumb->appdata->vbox),
2218     crumb->appdata->cur_view);
2219     }
2220    
2221     /* back from cachelist to gpxlist */
2222     if((crumb->level == CRUMB_CACHELIST) ||
2223     (crumb->level == CRUMB_SEARCH_GLOBAL)) {
2224    
2225     crumb->appdata->cur_view = gpxlist_create_view_and_model(
2226     crumb->appdata, crumb->appdata->cur_gpx);
2227    
2228     /* returning from cachelist/global search view: */
2229     /* invalidate gpx reference */
2230     crumb->appdata->cur_gpx = NULL;
2231    
2232     gtk_box_pack_start_defaults(GTK_BOX(crumb->appdata->vbox),
2233     crumb->appdata->cur_view);
2234    
2235     if((crumb->level == CRUMB_SEARCH_GLOBAL) ||
2236     (crumb->level == CRUMB_SEARCH_GPX)) {
2237     g_assert((gpx_t*)crumb->data == crumb->appdata->search_results);
2238    
2239     search_result_free((gpx_t*)crumb->data);
2240     crumb->appdata->search_results = NULL;
2241     }
2242    
2243     /* enable gpxlist related menu entries */
2244     gtk_widget_set_sensitive(crumb->appdata->menu_import, TRUE);
2245     gtk_widget_set_sensitive(crumb->appdata->menu_search, TRUE);
2246     gtk_widget_set_sensitive(crumb->appdata->menu_export, TRUE);
2247     }
2248    
2249     gtk_widget_show_all(crumb->appdata->vbox);
2250     g_free(data);
2251 harbaum 130
2252     #ifdef ENABLE_OSM_GPS_MAP
2253     map_update(crumb->appdata);
2254     #endif
2255 harbaum 1 }
2256    
2257     static void crumb_add(appdata_t *appdata, char *name, int level,
2258     gpointer user_data) {
2259 harbaum 242 crumb_t *crumb = g_new0(crumb_t, 1);
2260 harbaum 1 crumb->level = level;
2261     crumb->appdata = appdata;
2262     crumb->data = user_data;
2263    
2264     printf("crumb_add with level %d\n", level);
2265    
2266     /* save that we are working on search results */
2267     if((level == CRUMB_SEARCH_GLOBAL) ||
2268     (level == CRUMB_SEARCH_GPX)) {
2269     appdata->search_results = (gpx_t*)user_data;
2270    
2271     /* searches cannot be nested */
2272     gtk_widget_set_sensitive(appdata->menu_search, FALSE);
2273     }
2274    
2275     /* save "path" pointers in appdata */
2276     if(crumb->level == CRUMB_CACHELIST)
2277     appdata->cur_gpx = (gpx_t*)user_data;
2278    
2279     if(crumb->level == CRUMB_CACHE) {
2280     appdata->cur_cache = (cache_t*)user_data;
2281     /* the cache view cannot be searched */
2282     gtk_widget_set_sensitive(appdata->menu_search, FALSE);
2283     gtk_widget_set_sensitive(appdata->menu_export, FALSE);
2284     }
2285    
2286     if(level != CRUMB_GPXLIST) {
2287     /* disable gpxlist related menu entries */
2288     gtk_widget_set_sensitive(appdata->menu_import, FALSE);
2289     #ifndef USE_PANNABLE_AREA
2290     gtk_widget_set_sensitive(appdata->menu_remove, FALSE);
2291     gtk_widget_set_sensitive(appdata->menu_close, FALSE);
2292     #endif
2293     }
2294    
2295 harbaum 126 #ifdef USE_BREAD_CRUMB_TRAIL
2296 harbaum 1 hildon_bread_crumb_trail_push_text(HILDON_BREAD_CRUMB_TRAIL(appdata->bct),
2297     name, crumb, (GDestroyNotify)crumb_back);
2298 harbaum 126 #else
2299 harbaum 128 bct_push_text(appdata->bct, name, crumb, (GDestroyNotify)crumb_back);
2300 harbaum 126 #endif
2301 harbaum 130
2302     #ifdef ENABLE_OSM_GPS_MAP
2303     map_update(appdata);
2304     #endif
2305 harbaum 1 }
2306     #endif // USE_BREAD_CRUMB_TRAIL
2307    
2308     void main_after_settings_redraw(appdata_t *appdata, int flags) {
2309 harbaum 11 printf("main after settings redraw\n");
2310    
2311     if(!appdata->cur_view) {
2312     printf("no active view\n");
2313     return;
2314     }
2315    
2316 harbaum 1 /* a cache screen cannot be changed from the settings and thus doesn't */
2317     /* need to be redrawn */
2318     if(appdata->cur_cache) {
2319     printf("No redraw in cache view required\n");
2320     return;
2321     }
2322    
2323     int redraw = 0; // nothing to redraw
2324    
2325     if(appdata->search_results) {
2326     if((appdata->cur_items != appdata->cachelist_items) || flags)
2327     redraw = 1;
2328     } else {
2329     if(!appdata->cur_gpx) {
2330     if(appdata->cur_items != appdata->gpxlist_items)
2331     redraw = 2; // redraw gpxlist
2332     } else {
2333     if((appdata->cur_items != appdata->cachelist_items) || flags)
2334     redraw = 3; // redraw cachelist
2335     }
2336     }
2337    
2338     if(redraw) {
2339 harbaum 11
2340     #ifdef USE_STACKABLE_WINDOW
2341     HildonWindowStack *stack = hildon_window_stack_get_default();
2342 harbaum 237 GtkWidget *container = hildon_window_stack_peek(stack);
2343     #else
2344     GtkWidget *container = appdata->vbox;
2345 harbaum 11 #endif
2346    
2347     gtk_container_remove(GTK_CONTAINER(container), appdata->cur_view);
2348 harbaum 1 switch(redraw) {
2349     case 1:
2350     appdata->cur_view = cachelist_create(appdata,
2351     appdata->search_results, NULL);
2352     break;
2353     case 2:
2354     appdata->cur_view = gpxlist_create_view_and_model(appdata, NULL);
2355     break;
2356     case 3:
2357     appdata->cur_view = cachelist_create(appdata,
2358     appdata->cur_gpx, NULL);
2359     break;
2360     }
2361    
2362 harbaum 11 #ifdef USE_STACKABLE_WINDOW
2363     gtk_container_add(GTK_CONTAINER(container), appdata->cur_view);
2364 harbaum 237 #else
2365     gtk_box_pack_start_defaults(GTK_BOX(container), appdata->cur_view);
2366 harbaum 11 #endif
2367    
2368     gtk_widget_show_all(container);
2369 harbaum 1 }
2370     }
2371    
2372     int main(int argc, char *argv[]) {
2373     appdata_t appdata;
2374    
2375     /* init appdata */
2376     memset(&appdata, 0, sizeof(appdata));
2377    
2378     printf("Using locale for %s in %s\n", PACKAGE, LOCALEDIR);
2379    
2380     setlocale(LC_ALL, "");
2381     bindtextdomain(PACKAGE, LOCALEDIR);
2382     bind_textdomain_codeset(PACKAGE, "UTF-8");
2383     textdomain(PACKAGE);
2384    
2385     /* prepare thread system */
2386     g_thread_init(NULL);
2387    
2388     gtk_init (&argc, &argv);
2389    
2390 harbaum 223 misc_init();
2391    
2392 harbaum 157 curl_global_init(CURL_GLOBAL_ALL);
2393    
2394 harbaum 1 #ifdef USE_MAEMO
2395     printf("Installing osso context for \"org.harbaum." APP "\"\n");
2396     appdata.osso_context = osso_initialize("org.harbaum."APP,
2397     VERSION, TRUE, NULL);
2398     if(appdata.osso_context == NULL) {
2399     fprintf(stderr, "error initiating osso context\n");
2400     }
2401    
2402 harbaum 122 #ifdef ENABLE_MAEMO_MAPPER
2403 harbaum 1 dbus_register(&appdata);
2404     #endif
2405 harbaum 122 #endif
2406 harbaum 1
2407     icons_init();
2408    
2409     if(!gnome_vfs_init()) {
2410     g_error("Gnome VFS init failed\n");
2411     }
2412    
2413     #ifdef USE_MAEMO
2414     /* Create the hildon program and setup the title */
2415     appdata.program = HILDON_PROGRAM(hildon_program_get_instance());
2416     g_set_application_name("GPXView");
2417    
2418     /* Create HildonWindow and set it to HildonProgram */
2419     #ifdef USE_STACKABLE_WINDOW
2420     appdata.window = HILDON_WINDOW(hildon_stackable_window_new());
2421     #else
2422     appdata.window = HILDON_WINDOW(hildon_window_new());
2423     #endif
2424     hildon_program_add_window(appdata.program, appdata.window);
2425     #else
2426     /* Create a Window. */
2427     appdata.window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
2428     /* Set a decent default size for the window. */
2429 harbaum 129 gtk_window_set_default_size(GTK_WINDOW(appdata.window), 640, 480);
2430 harbaum 1 #endif
2431    
2432 harbaum 12 #if MAEMO_VERSION_MAJOR == 5
2433 harbaum 1 gtk_window_set_title(GTK_WINDOW(appdata.window), "GPXView");
2434 harbaum 12 #endif
2435    
2436 harbaum 1 g_signal_connect(G_OBJECT(appdata.window), "destroy",
2437     G_CALLBACK(on_window_destroy), &appdata);
2438    
2439     g_signal_connect(G_OBJECT(appdata.window), "key_press_event",
2440     G_CALLBACK(on_window_key_press), &appdata);
2441    
2442 harbaum 133 /* prepare clipboard */
2443     appdata.clipboard = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
2444     gtk_clipboard_set_can_store(appdata.clipboard, NULL, 0);
2445    
2446 harbaum 237 #ifndef USE_STACKABLE_WINDOW
2447 harbaum 1 appdata.vbox = gtk_vbox_new(FALSE, 2);
2448     gtk_container_add(GTK_CONTAINER(appdata.window), appdata.vbox);
2449     menu_create(&appdata);
2450 harbaum 3 #else
2451     hildon_window_set_app_menu(HILDON_WINDOW(appdata.window),
2452     menu_create(&appdata, MENU_GPXLIST));
2453     #endif
2454 harbaum 1
2455     #ifdef USE_BREAD_CRUMB_TRAIL
2456     appdata.bct = hildon_bread_crumb_trail_new();
2457    
2458     gtk_box_pack_start(GTK_BOX(appdata.vbox), appdata.bct, FALSE,FALSE,0);
2459    
2460     hildon_bread_crumb_trail_clear(HILDON_BREAD_CRUMB_TRAIL(appdata.bct));
2461 harbaum 126 #else
2462 harbaum 128 #ifdef BCT
2463 harbaum 126 /* on non-hildon machines we use some custom made breadcrumbtrail */
2464     /* replacement */
2465 harbaum 128 appdata.bct = bct_new();
2466 harbaum 126 gtk_box_pack_start(GTK_BOX(appdata.vbox), appdata.bct, FALSE,FALSE,0);
2467 harbaum 1 #endif
2468 harbaum 126 #endif
2469 harbaum 1
2470 harbaum 128 #if defined(USE_BREAD_CRUMB_TRAIL) || defined(BCT)
2471     crumb_add(&appdata, "GPX", CRUMB_GPXLIST, NULL);
2472     #endif
2473    
2474 harbaum 1 /* wait for main gui to appear */
2475     gtk_widget_show_all(GTK_WIDGET(appdata.window));
2476     while(gtk_events_pending())
2477     gtk_main_iteration();
2478    
2479     appdata.gconf_client = gconf_client_get_default();
2480     gconf_load_state(&appdata);
2481     gps_init(&appdata);
2482    
2483     appdata.cur_view = gpxlist_create_view_and_model(&appdata, NULL);
2484 harbaum 237 #ifndef USE_STACKABLE_WINDOW
2485 harbaum 1 gtk_box_pack_start_defaults(GTK_BOX(appdata.vbox), appdata.cur_view);
2486 harbaum 237 #else
2487     gtk_container_add(GTK_CONTAINER(appdata.window), appdata.cur_view);
2488     #endif
2489 harbaum 1
2490     gtk_widget_show_all(GTK_WIDGET(appdata.window));
2491     gtk_main();
2492    
2493     cleanup(&appdata);
2494    
2495     return 0;
2496     }