Contents of /trunk/src/main.c

Parent Directory Parent Directory | Revision Log Revision Log


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