Contents of /trunk/src/main.c

Parent Directory Parent Directory | Revision Log Revision Log


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