Contents of /trunk/src/main.c

Parent Directory Parent Directory | Revision Log Revision Log


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