Contents of /trunk/src/main.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 30 - (hide annotations)
Fri Jul 24 19:24:42 2009 UTC (14 years, 9 months ago) by harbaum
File MIME type: text/plain
File size: 66745 byte(s)
Fixed fremantle submenues
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     char *title = g_strdup_printf("GPXView - %s", gpx->name);
591     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     void on_window_destroy (GtkWidget *widget, gpointer data);
1437    
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 6 static void
1513     cb_menu_geomath(GtkWidget *window, gpointer data) {
1514     geomath_dialog((appdata_t *)data);
1515     }
1516 harbaum 5
1517 harbaum 6 static void
1518     cb_menu_geotext(GtkWidget *window, gpointer data) {
1519     geotext_dialog((appdata_t *)data);
1520 harbaum 1 }
1521    
1522 harbaum 6 static void
1523     cb_menu_precpos(GtkWidget *window, gpointer data) {
1524     precise_position((appdata_t *)data);
1525 harbaum 1 }
1526    
1527 harbaum 6 #ifdef USE_STACKABLE_WINDOW
1528 harbaum 30 typedef struct {
1529     char *label, *desc;
1530     GtkSignalFunc activate_cb;
1531     } menu_entry_t;
1532 harbaum 6
1533 harbaum 30 typedef struct {
1534     const char *title;
1535     const menu_entry_t *menu;
1536     int len;
1537     } submenu_t;
1538 harbaum 3
1539 harbaum 30 #define COLUMNS 1
1540 harbaum 4
1541 harbaum 30 void on_submenu_entry_clicked(GtkButton *button, GtkWidget *menu) {
1542 harbaum 5
1543 harbaum 30 /* force closing of submenu dialog */
1544     gtk_dialog_response(GTK_DIALOG(menu), GTK_RESPONSE_NONE);
1545     gtk_widget_hide(menu);
1546    
1547     /* let gtk clean up */
1548     while(gtk_events_pending())
1549     gtk_main_iteration();
1550 harbaum 11 }
1551    
1552 harbaum 30 static GtkWidget *app_submenu_create(appdata_t *appdata,
1553     const submenu_t *submenu) {
1554 harbaum 5
1555 harbaum 30 /* create a oridinary dialog box */
1556     GtkWidget *dialog = gtk_dialog_new();
1557     gtk_window_set_title(GTK_WINDOW(dialog), _(submenu->title));
1558     gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
1559     gtk_window_set_transient_for(GTK_WINDOW(dialog),
1560     GTK_WINDOW(appdata->window));
1561     gtk_dialog_set_has_separator(GTK_DIALOG(dialog), FALSE);
1562 harbaum 5
1563 harbaum 30 GtkWidget *table = gtk_table_new(submenu->len/COLUMNS, COLUMNS, TRUE);
1564     int x = 0, y = 0;
1565 harbaum 11
1566 harbaum 30 const menu_entry_t *menu_entries = submenu->menu;
1567     while(menu_entries->label) {
1568     GtkWidget *button = NULL;
1569 harbaum 21
1570 harbaum 30 button = hildon_button_new_with_text(
1571 harbaum 6 HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH,
1572     HILDON_BUTTON_ARRANGEMENT_VERTICAL,
1573 harbaum 30 _(menu_entries->label), _(menu_entries->desc));
1574 harbaum 5
1575 harbaum 30 g_signal_connect(button, "clicked",
1576     G_CALLBACK(on_submenu_entry_clicked), dialog);
1577 harbaum 6
1578 harbaum 30 g_signal_connect(button, "clicked",
1579     menu_entries->activate_cb, appdata);
1580 harbaum 4
1581 harbaum 30 gtk_table_attach_defaults(GTK_TABLE(table), button, x, x+1, y, y+1);
1582    
1583     x++;
1584     if(x == COLUMNS) { x = 0; y++; }
1585 harbaum 3
1586 harbaum 30 menu_entries++;
1587     }
1588 harbaum 20
1589 harbaum 30 gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(dialog)->vbox), table);
1590    
1591     return dialog;
1592 harbaum 3 }
1593    
1594 harbaum 30 /* popup the dialog shaped submenu */
1595     static void submenu_popup(GtkWidget *menu) {
1596     gtk_widget_show_all(menu);
1597     gtk_dialog_run(GTK_DIALOG(menu));
1598     gtk_widget_hide(menu);
1599     }
1600    
1601     static void submenu_cleanup(GtkWidget *menu) {
1602     gtk_widget_destroy(menu);
1603     }
1604    
1605     static const menu_entry_t submenu_export_entries[] = {
1606     { "Export to Maemo Mapper" , "Save a Maemo Mapper POI file",
1607     G_CALLBACK(cb_menu_export_mmpoi) },
1608     { "Export Field Notes", "Save a Garmin Field Notes file",
1609     G_CALLBACK(cb_menu_export_log) },
1610     { "Export Garmin GPX", "Save modified waypoints in GPX file",
1611     G_CALLBACK(cb_menu_export_garmin) },
1612     { NULL, NULL, NULL }
1613     };
1614    
1615     static const submenu_t submenu_export = {
1616     "Export", submenu_export_entries,
1617     sizeof(submenu_export_entries)/sizeof(menu_entry_t)-1
1618     };
1619    
1620     /* the export submenu */
1621     void on_export_clicked(GtkButton *button, appdata_t *appdata) {
1622     if(!appdata->export_menu)
1623     appdata->export_menu = app_submenu_create(appdata, &submenu_export);
1624    
1625     submenu_popup(appdata->export_menu);
1626     }
1627    
1628     static const menu_entry_t submenu_tools_entries[] = {
1629     { "Geomath", "Geocoordinate calculation",
1630     G_CALLBACK(cb_menu_geomath) },
1631     { "Geotext", "Text analysis",
1632     G_CALLBACK(cb_menu_geotext) },
1633     { "Precise Position", "Calculate a precise GPS position",
1634     G_CALLBACK(cb_menu_precpos) },
1635     { NULL, NULL, NULL }
1636     };
1637    
1638     static const submenu_t submenu_tools = {
1639     "Tools", submenu_tools_entries,
1640     sizeof(submenu_tools_entries)/sizeof(menu_entry_t)-1
1641     };
1642    
1643 harbaum 20 /* the tools submenu */
1644     void on_tools_clicked(GtkButton *button, appdata_t *appdata) {
1645     if(!appdata->tools_menu)
1646 harbaum 30 appdata->tools_menu = app_submenu_create(appdata, &submenu_tools);
1647 harbaum 20
1648 harbaum 30 submenu_popup(appdata->tools_menu);
1649 harbaum 20 }
1650    
1651 harbaum 3 HildonAppMenu *menu_create(appdata_t *appdata, int mode) {
1652     GtkWidget *button;
1653     HildonAppMenu *menu = HILDON_APP_MENU(hildon_app_menu_new());
1654    
1655     /* ------- */
1656 harbaum 7 button = gtk_button_new_with_label(_("About"));
1657     g_signal_connect_after(button, "clicked",
1658     G_CALLBACK(cb_menu_about), appdata);
1659     hildon_app_menu_append(menu, GTK_BUTTON(button));
1660    
1661 harbaum 3 button = gtk_button_new_with_label(_("Settings"));
1662     g_signal_connect_after(button, "clicked", G_CALLBACK(cb_menu_settings),
1663     appdata);
1664     hildon_app_menu_append(menu, GTK_BUTTON(button));
1665    
1666     if(mode == MENU_GPXLIST) {
1667     button = gtk_button_new_with_label(_("Import file"));
1668     g_signal_connect_after(button, "clicked",
1669     G_CALLBACK(cb_menu_add), appdata);
1670     hildon_app_menu_append(menu, GTK_BUTTON(button));
1671    
1672 harbaum 7 button = gtk_button_new_with_label(_("Import directory"));
1673 harbaum 3 g_signal_connect_after(button, "clicked",
1674     G_CALLBACK(cb_menu_adddir), appdata);
1675     hildon_app_menu_append(menu, GTK_BUTTON(button));
1676     }
1677    
1678     if(mode == MENU_GPXLIST) {
1679 harbaum 4 button = gtk_button_new_with_label(_("Export"));
1680     g_signal_connect_after(button, "clicked",
1681     G_CALLBACK(on_export_clicked), appdata);
1682     hildon_app_menu_append(menu, GTK_BUTTON(button));
1683    
1684     button = gtk_button_new_with_label(_("Search"));
1685     g_signal_connect_after(button, "clicked",
1686     G_CALLBACK(cb_menu_search), appdata);
1687     hildon_app_menu_append(menu, GTK_BUTTON(button));
1688     }
1689    
1690 harbaum 5 button = gtk_button_new_with_label(_("Tools"));
1691     g_signal_connect_after(button, "clicked",
1692     G_CALLBACK(on_tools_clicked), appdata);
1693     hildon_app_menu_append(menu, GTK_BUTTON(button));
1694 harbaum 4
1695 harbaum 15 #ifdef HILDON_HELP
1696     button = gtk_button_new_with_label(_("Help"));
1697     g_signal_connect_after(button, "clicked",
1698     G_CALLBACK(cb_menu_help), appdata);
1699     hildon_app_menu_append(menu, GTK_BUTTON(button));
1700     #endif
1701 harbaum 3
1702 harbaum 15 gtk_widget_show_all(GTK_WIDGET(menu));
1703    
1704 harbaum 3 return menu;
1705     }
1706     #else
1707 harbaum 5
1708 harbaum 1 void menu_create(appdata_t *appdata) {
1709     GtkWidget *menu, *item;
1710     menu = gtk_menu_new();
1711    
1712 harbaum 3 #ifdef USE_BREAD_CRUMB_TRAIL
1713 harbaum 1 appdata->menu_import =
1714     #endif
1715     item = gtk_menu_item_new_with_label(_("Import"));
1716     gtk_menu_append(GTK_MENU_SHELL(menu), item);
1717     GtkWidget *submenu = gtk_menu_new();
1718     gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
1719    
1720 harbaum 7 item = gtk_menu_item_new_with_label( _("File") );
1721 harbaum 1 gtk_menu_append(GTK_MENU_SHELL(submenu), item);
1722     g_signal_connect(item, "activate", GTK_SIGNAL_FUNC(cb_menu_add), appdata);
1723    
1724 harbaum 7 item = gtk_menu_item_new_with_label( _("Directory") );
1725 harbaum 1 gtk_menu_append(GTK_MENU_SHELL(submenu), item);
1726     g_signal_connect(item, "activate", GTK_SIGNAL_FUNC(cb_menu_adddir), appdata);
1727    
1728     #ifndef USE_PANNABLE_AREA
1729     gtk_menu_append(GTK_MENU_SHELL(submenu), gtk_separator_menu_item_new());
1730    
1731     appdata->menu_close =
1732     item = gtk_menu_item_new_with_label( _("Close") );
1733     gtk_menu_append(GTK_MENU_SHELL(submenu), item);
1734     g_signal_connect(item, "activate", GTK_SIGNAL_FUNC(cb_menu_close), appdata);
1735    
1736     appdata->menu_remove =
1737     item = gtk_menu_item_new_with_label( _("Remove") );
1738     gtk_menu_append(GTK_MENU_SHELL(submenu), item);
1739     g_signal_connect(item, "activate", GTK_SIGNAL_FUNC(cb_menu_remove), appdata);
1740     #endif
1741    
1742 harbaum 3 #ifdef USE_BREAD_CRUMB_TRAIL
1743 harbaum 1 appdata->menu_export =
1744     #endif
1745     item = gtk_menu_item_new_with_label(_("Export"));
1746     gtk_menu_append(GTK_MENU_SHELL(menu), item);
1747     submenu = gtk_menu_new();
1748     gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
1749    
1750     #ifdef USE_MAEMO
1751 harbaum 7 item = gtk_menu_item_new_with_label( _("Maemo Mapper POI") );
1752 harbaum 1 gtk_menu_append(GTK_MENU_SHELL(submenu), item);
1753     g_signal_connect(item, "activate",
1754     GTK_SIGNAL_FUNC(cb_menu_export_mmpoi), appdata);
1755     #endif
1756    
1757 harbaum 7 item = gtk_menu_item_new_with_label( _("Garmin Field Notes") );
1758 harbaum 1 gtk_menu_append(GTK_MENU_SHELL(submenu), item);
1759     g_signal_connect(item, "activate",
1760     GTK_SIGNAL_FUNC(cb_menu_export_log), appdata);
1761    
1762 harbaum 7 item = gtk_menu_item_new_with_label( _("Garmin GPX") );
1763 harbaum 1 gtk_menu_append(GTK_MENU_SHELL(submenu), item);
1764     g_signal_connect(item, "activate",
1765     GTK_SIGNAL_FUNC(cb_menu_export_garmin), appdata);
1766    
1767 harbaum 3 #ifdef USE_BREAD_CRUMB_TRAIL
1768 harbaum 1 appdata->menu_search =
1769     #endif
1770 harbaum 7 item = gtk_menu_item_new_with_label( _("Search") );
1771 harbaum 1 gtk_menu_append(GTK_MENU_SHELL(menu), item);
1772     g_signal_connect(item, "activate", GTK_SIGNAL_FUNC(cb_menu_search), appdata);
1773    
1774     gtk_menu_append(GTK_MENU_SHELL(menu), gtk_separator_menu_item_new());
1775 harbaum 2
1776     #ifndef NO_COPY_N_PASTE
1777 harbaum 1 /* ----------- copy'n paste submenu ----------------- */
1778     appdata->clipboard = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
1779     gtk_clipboard_set_can_store(appdata->clipboard, NULL, 0);
1780    
1781     item = gtk_menu_item_new_with_label(_("Edit"));
1782     gtk_menu_append(GTK_MENU_SHELL(menu), item);
1783     submenu = gtk_menu_new();
1784     gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
1785    
1786     appdata->menu_cut = item = gtk_menu_item_new_with_label( _("Cut") );
1787     gtk_widget_set_sensitive(item, FALSE);
1788     gtk_menu_append(GTK_MENU_SHELL(submenu), item);
1789     g_signal_connect(item, "activate", GTK_SIGNAL_FUNC(cb_menu_cut), appdata);
1790     appdata->menu_copy = item = gtk_menu_item_new_with_label( _("Copy") );
1791     gtk_widget_set_sensitive(item, FALSE);
1792     gtk_menu_append(GTK_MENU_SHELL(submenu), item);
1793     g_signal_connect(item, "activate", GTK_SIGNAL_FUNC(cb_menu_copy), appdata);
1794     appdata->menu_paste = item = gtk_menu_item_new_with_label( _("Paste") );
1795     gtk_widget_set_sensitive(item, FALSE);
1796     gtk_menu_append(GTK_MENU_SHELL(submenu), item);
1797     g_signal_connect(item, "activate", GTK_SIGNAL_FUNC(cb_menu_paste), appdata);
1798 harbaum 2 #endif
1799 harbaum 1
1800 harbaum 3 item = gtk_menu_item_new_with_label( _("Settings") );
1801 harbaum 1 gtk_menu_append(GTK_MENU_SHELL(menu), item);
1802     g_signal_connect(item, "activate", GTK_SIGNAL_FUNC(cb_menu_settings),
1803     appdata);
1804    
1805     gtk_menu_append(GTK_MENU_SHELL(menu), gtk_separator_menu_item_new());
1806    
1807     item = gtk_menu_item_new_with_label(_("Tools"));
1808     gtk_menu_append(GTK_MENU_SHELL(menu), item);
1809     submenu = gtk_menu_new();
1810     gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
1811    
1812 harbaum 7 item = gtk_menu_item_new_with_label( _("Geomath") );
1813 harbaum 1 gtk_menu_append(GTK_MENU_SHELL(submenu), item);
1814     g_signal_connect(item, "activate",
1815     GTK_SIGNAL_FUNC(cb_menu_geomath), appdata);
1816    
1817 harbaum 7 item = gtk_menu_item_new_with_label( _("Geotext") );
1818 harbaum 1 gtk_menu_append(GTK_MENU_SHELL(submenu), item);
1819     g_signal_connect(item, "activate",
1820     GTK_SIGNAL_FUNC(cb_menu_geotext), appdata);
1821    
1822 harbaum 7 item = gtk_menu_item_new_with_label( _("Precise Position") );
1823 harbaum 1 gtk_menu_append(GTK_MENU_SHELL(submenu), item);
1824     g_signal_connect(item, "activate",
1825     GTK_SIGNAL_FUNC(cb_menu_precpos), appdata);
1826    
1827     gtk_menu_append(GTK_MENU_SHELL(menu), gtk_separator_menu_item_new());
1828    
1829     #if defined(USE_MAEMO) && defined(HILDON_HELP)
1830 harbaum 7 item = gtk_menu_item_new_with_label( _("Help") );
1831 harbaum 1 gtk_menu_append(GTK_MENU_SHELL(menu), item);
1832     g_signal_connect(item, "activate", GTK_SIGNAL_FUNC(cb_menu_help), appdata);
1833     #endif
1834    
1835 harbaum 7 item = gtk_menu_item_new_with_label( _("About") );
1836 harbaum 1 gtk_menu_append(GTK_MENU_SHELL(menu), item);
1837     g_signal_connect(item, "activate", GTK_SIGNAL_FUNC(cb_menu_about), appdata);
1838    
1839     #ifndef USE_MAEMO
1840     item = gtk_menu_item_new_with_label( _("Quit") );
1841     gtk_menu_append(GTK_MENU_SHELL(menu), item);
1842     g_signal_connect(item, "activate", GTK_SIGNAL_FUNC(cb_menu_quit), appdata);
1843     #endif
1844    
1845     #ifdef USE_MAEMO
1846     hildon_window_set_menu(appdata->window, GTK_MENU(menu));
1847     #else
1848     /* attach ordinary gtk menu */
1849     GtkWidget *menu_bar = gtk_menu_bar_new();
1850    
1851     GtkWidget *root_menu = gtk_menu_item_new_with_label (_("Menu"));
1852     gtk_widget_show(root_menu);
1853    
1854     gtk_menu_bar_append(menu_bar, root_menu);
1855     gtk_menu_item_set_submenu(GTK_MENU_ITEM (root_menu), menu);
1856    
1857     gtk_widget_show(menu_bar);
1858     gtk_box_pack_start(GTK_BOX(appdata->vbox), menu_bar, 0, 0, 0);
1859     #endif
1860     }
1861 harbaum 3 #endif
1862 harbaum 1
1863     /********************* end of menu **********************/
1864    
1865     void cleanup(appdata_t *appdata) {
1866     gpx_free_all(appdata->gpx);
1867     if(appdata->path) free(appdata->path);
1868     if(appdata->image_path) free(appdata->image_path);
1869     if(appdata->search_str) free(appdata->search_str);
1870    
1871 harbaum 30 #ifdef USE_STACKABLE_WINDOW
1872     if(appdata->export_menu) submenu_cleanup(appdata->export_menu);
1873     if(appdata->tools_menu) submenu_cleanup(appdata->tools_menu);
1874     #endif
1875    
1876 harbaum 1 gnome_vfs_shutdown();
1877     icons_free();
1878     gps_release(appdata);
1879    
1880     #ifdef USE_MAEMO
1881     if(appdata->search_results) {
1882     printf("freeing pending search\n");
1883     search_result_free(appdata->search_results);
1884     }
1885    
1886     if(appdata->osso_context)
1887     osso_deinitialize(appdata->osso_context);
1888    
1889     appdata->program = NULL;
1890     #endif
1891    
1892     /* free chain of locations */
1893     location_t *loc = appdata->location;
1894     while(loc) {
1895     location_t *next = loc->next;
1896     if(loc->name) free(loc->name);
1897     free(loc);
1898     loc = next;
1899     }
1900    
1901     puts("everything is gone");
1902     }
1903    
1904     void on_window_destroy (GtkWidget *widget, gpointer data) {
1905     appdata_t *appdata = (appdata_t*)data;
1906    
1907     gconf_save_state(appdata);
1908     gtk_main_quit();
1909     appdata->window = NULL;
1910     }
1911    
1912     gboolean on_window_key_press(GtkWidget *widget,
1913 harbaum 11 GdkEventKey *event, appdata_t *appdata) {
1914 harbaum 1 int handled = FALSE;
1915    
1916     // printf("key event %d\n", event->keyval);
1917    
1918     switch(event->keyval) {
1919     #ifdef USE_MAEMO
1920    
1921     #ifdef HILDON_HARDKEY_INCREASE
1922     case HILDON_HARDKEY_INCREASE:
1923     html_zoom(appdata, TRUE);
1924     handled = TRUE;
1925     break;
1926     #endif
1927    
1928     #ifdef HILDON_HARDKEY_DECREASE
1929     case HILDON_HARDKEY_DECREASE:
1930     html_zoom(appdata, FALSE);
1931     handled = TRUE;
1932     break;
1933     #endif
1934    
1935     #ifdef HILDON_HARDKEY_FULLSCREEN
1936     case HILDON_HARDKEY_FULLSCREEN:
1937     {
1938     appdata->fullscreen = !appdata->fullscreen;
1939    
1940     if(appdata->fullscreen)
1941     gtk_window_fullscreen(GTK_WINDOW(appdata->window));
1942     else
1943     gtk_window_unfullscreen(GTK_WINDOW(appdata->window));
1944    
1945     handled = TRUE;
1946     }
1947     break;
1948     #endif
1949    
1950     #else
1951     case '+':
1952     printf("zoom+\n");
1953     html_zoom(appdata, TRUE);
1954     handled = TRUE;
1955     break;
1956     case '-':
1957     printf("zoom-\n");
1958     html_zoom(appdata, FALSE);
1959     handled = TRUE;
1960     break;
1961     #endif
1962     }
1963    
1964     return handled;
1965     }
1966    
1967     #ifdef USE_BREAD_CRUMB_TRAIL
1968     typedef struct {
1969     int level;
1970     appdata_t *appdata;
1971     gpointer data;
1972     } crumb_t;
1973    
1974     static void
1975     crumb_back(gpointer data) {
1976     crumb_t *crumb = (crumb_t*)data;
1977     printf("crumb_back called with %d\n", crumb->level);
1978    
1979     /* don't do anything if main window has already been destroyed */
1980     if(!crumb->appdata->window) {
1981     printf("Main window gone ...\n");
1982     return;
1983     }
1984    
1985     /* whatever is being displayed: we don't need it anymore */
1986     gtk_container_remove(GTK_CONTAINER(crumb->appdata->vbox),
1987     crumb->appdata->cur_view);
1988    
1989     /* back from cache to cachelist */
1990     if(crumb->level == CRUMB_CACHE) {
1991     gpx_t *gpx = crumb->appdata->search_results;
1992    
1993     if(!gpx) {
1994     gtk_widget_set_sensitive(crumb->appdata->menu_search, TRUE);
1995     gtk_widget_set_sensitive(crumb->appdata->menu_export, TRUE);
1996     printf("no search data found, return to gpx\n");
1997     gpx = crumb->appdata->cur_gpx;
1998     } else
1999     printf("returning to search result\n");
2000    
2001     g_assert(gpx != NULL);
2002    
2003     crumb->appdata->cur_view = cachelist_create(crumb->appdata, gpx,
2004     crumb->appdata->cur_cache);
2005    
2006     /* returning from cache view: invalidate cache reference */
2007     crumb->appdata->cur_cache = NULL;
2008    
2009     gtk_box_pack_start_defaults(GTK_BOX(crumb->appdata->vbox),
2010     crumb->appdata->cur_view);
2011     }
2012    
2013     if(crumb->level == CRUMB_SEARCH_GPX) {
2014     printf("returning from a local search!\n");
2015    
2016     g_assert((gpx_t*)crumb->data == crumb->appdata->search_results);
2017    
2018     search_result_free((gpx_t*)crumb->data);
2019     crumb->appdata->search_results = NULL;
2020    
2021     gtk_widget_set_sensitive(crumb->appdata->menu_search, TRUE);
2022    
2023     crumb->appdata->cur_view = cachelist_create(crumb->appdata,
2024     crumb->appdata->cur_gpx, NULL);
2025     gtk_box_pack_start_defaults(GTK_BOX(crumb->appdata->vbox),
2026     crumb->appdata->cur_view);
2027     }
2028    
2029     /* back from cachelist to gpxlist */
2030     if((crumb->level == CRUMB_CACHELIST) ||
2031     (crumb->level == CRUMB_SEARCH_GLOBAL)) {
2032    
2033     crumb->appdata->cur_view = gpxlist_create_view_and_model(
2034     crumb->appdata, crumb->appdata->cur_gpx);
2035    
2036     /* returning from cachelist/global search view: */
2037     /* invalidate gpx reference */
2038     crumb->appdata->cur_gpx = NULL;
2039    
2040     gtk_box_pack_start_defaults(GTK_BOX(crumb->appdata->vbox),
2041     crumb->appdata->cur_view);
2042    
2043     if((crumb->level == CRUMB_SEARCH_GLOBAL) ||
2044     (crumb->level == CRUMB_SEARCH_GPX)) {
2045     g_assert((gpx_t*)crumb->data == crumb->appdata->search_results);
2046    
2047     search_result_free((gpx_t*)crumb->data);
2048     crumb->appdata->search_results = NULL;
2049     }
2050    
2051     /* enable gpxlist related menu entries */
2052     gtk_widget_set_sensitive(crumb->appdata->menu_import, TRUE);
2053     gtk_widget_set_sensitive(crumb->appdata->menu_search, TRUE);
2054     gtk_widget_set_sensitive(crumb->appdata->menu_export, TRUE);
2055     }
2056    
2057     gtk_widget_show_all(crumb->appdata->vbox);
2058     g_free(data);
2059     }
2060    
2061     static void crumb_add(appdata_t *appdata, char *name, int level,
2062     gpointer user_data) {
2063     crumb_t *crumb = malloc(sizeof(crumb_t));
2064     crumb->level = level;
2065     crumb->appdata = appdata;
2066     crumb->data = user_data;
2067    
2068     printf("crumb_add with level %d\n", level);
2069    
2070     /* save that we are working on search results */
2071     if((level == CRUMB_SEARCH_GLOBAL) ||
2072     (level == CRUMB_SEARCH_GPX)) {
2073     appdata->search_results = (gpx_t*)user_data;
2074    
2075     /* searches cannot be nested */
2076     gtk_widget_set_sensitive(appdata->menu_search, FALSE);
2077     }
2078    
2079     /* save "path" pointers in appdata */
2080     if(crumb->level == CRUMB_CACHELIST)
2081     appdata->cur_gpx = (gpx_t*)user_data;
2082    
2083     if(crumb->level == CRUMB_CACHE) {
2084     appdata->cur_cache = (cache_t*)user_data;
2085     /* the cache view cannot be searched */
2086     gtk_widget_set_sensitive(appdata->menu_search, FALSE);
2087     gtk_widget_set_sensitive(appdata->menu_export, FALSE);
2088     }
2089    
2090     if(level != CRUMB_GPXLIST) {
2091     /* disable gpxlist related menu entries */
2092     gtk_widget_set_sensitive(appdata->menu_import, FALSE);
2093     #ifndef USE_PANNABLE_AREA
2094     gtk_widget_set_sensitive(appdata->menu_remove, FALSE);
2095     gtk_widget_set_sensitive(appdata->menu_close, FALSE);
2096     #endif
2097     }
2098    
2099     hildon_bread_crumb_trail_push_text(HILDON_BREAD_CRUMB_TRAIL(appdata->bct),
2100     name, crumb, (GDestroyNotify)crumb_back);
2101     }
2102     #endif // USE_BREAD_CRUMB_TRAIL
2103    
2104     void main_after_settings_redraw(appdata_t *appdata, int flags) {
2105 harbaum 11 printf("main after settings redraw\n");
2106    
2107     if(!appdata->cur_view) {
2108     printf("no active view\n");
2109     return;
2110     }
2111    
2112 harbaum 1 #ifndef USE_MAEMO
2113     // in non-maemo setup this can only affect the main screen as
2114     // the menu is blocked while a dialog is open. also the main
2115     // screen is always present
2116     if(appdata->gpxlist_items != appdata->cur_items) {
2117     /* re-do the main screen */
2118     gtk_container_remove(GTK_CONTAINER(appdata->vbox), appdata->cur_view);
2119     appdata->cur_view = gpxlist_create_view_and_model(appdata, NULL);
2120     gtk_box_pack_start_defaults(GTK_BOX(appdata->vbox), appdata->cur_view);
2121     gtk_widget_show_all(appdata->vbox);
2122     }
2123     #else
2124     /* a cache screen cannot be changed from the settings and thus doesn't */
2125     /* need to be redrawn */
2126     if(appdata->cur_cache) {
2127     printf("No redraw in cache view required\n");
2128     return;
2129     }
2130    
2131     int redraw = 0; // nothing to redraw
2132    
2133     if(appdata->search_results) {
2134     if((appdata->cur_items != appdata->cachelist_items) || flags)
2135     redraw = 1;
2136     } else {
2137     if(!appdata->cur_gpx) {
2138     if(appdata->cur_items != appdata->gpxlist_items)
2139     redraw = 2; // redraw gpxlist
2140     } else {
2141     if((appdata->cur_items != appdata->cachelist_items) || flags)
2142     redraw = 3; // redraw cachelist
2143     }
2144     }
2145    
2146     if(redraw) {
2147 harbaum 11 GtkWidget *container = appdata->vbox;
2148    
2149     #ifdef USE_STACKABLE_WINDOW
2150     HildonWindowStack *stack = hildon_window_stack_get_default();
2151     container = hildon_window_stack_peek(stack);
2152     #endif
2153    
2154     gtk_container_remove(GTK_CONTAINER(container), appdata->cur_view);
2155 harbaum 1 switch(redraw) {
2156     case 1:
2157     appdata->cur_view = cachelist_create(appdata,
2158     appdata->search_results, NULL);
2159     break;
2160     case 2:
2161     appdata->cur_view = gpxlist_create_view_and_model(appdata, NULL);
2162     break;
2163     case 3:
2164     appdata->cur_view = cachelist_create(appdata,
2165     appdata->cur_gpx, NULL);
2166     break;
2167     }
2168    
2169 harbaum 11 #ifdef USE_STACKABLE_WINDOW
2170     if(container != appdata->vbox)
2171     gtk_container_add(GTK_CONTAINER(container), appdata->cur_view);
2172     else
2173     #endif
2174     gtk_box_pack_start_defaults(GTK_BOX(container), appdata->cur_view);
2175    
2176     gtk_widget_show_all(container);
2177 harbaum 1 }
2178 harbaum 11 #endif // USE_MAEMO
2179 harbaum 1 }
2180    
2181     int main(int argc, char *argv[]) {
2182     appdata_t appdata;
2183    
2184     /* init appdata */
2185     memset(&appdata, 0, sizeof(appdata));
2186    
2187     printf("Using locale for %s in %s\n", PACKAGE, LOCALEDIR);
2188    
2189     setlocale(LC_ALL, "");
2190     bindtextdomain(PACKAGE, LOCALEDIR);
2191     bind_textdomain_codeset(PACKAGE, "UTF-8");
2192     textdomain(PACKAGE);
2193    
2194     /* prepare thread system */
2195     g_thread_init(NULL);
2196    
2197     gtk_init (&argc, &argv);
2198    
2199     #ifdef USE_MAEMO
2200     printf("Installing osso context for \"org.harbaum." APP "\"\n");
2201     appdata.osso_context = osso_initialize("org.harbaum."APP,
2202     VERSION, TRUE, NULL);
2203     if(appdata.osso_context == NULL) {
2204     fprintf(stderr, "error initiating osso context\n");
2205     }
2206    
2207     dbus_register(&appdata);
2208     #endif
2209    
2210     icons_init();
2211    
2212     if(!gnome_vfs_init()) {
2213     g_error("Gnome VFS init failed\n");
2214     }
2215    
2216     #ifdef USE_MAEMO
2217     /* Create the hildon program and setup the title */
2218     appdata.program = HILDON_PROGRAM(hildon_program_get_instance());
2219     g_set_application_name("GPXView");
2220    
2221     /* Create HildonWindow and set it to HildonProgram */
2222     #ifdef USE_STACKABLE_WINDOW
2223     appdata.window = HILDON_WINDOW(hildon_stackable_window_new());
2224     #else
2225     appdata.window = HILDON_WINDOW(hildon_window_new());
2226     #endif
2227     hildon_program_add_window(appdata.program, appdata.window);
2228     #else
2229     /* Create a Window. */
2230     appdata.window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
2231     /* Set a decent default size for the window. */
2232     gtk_window_set_default_size(GTK_WINDOW(appdata.window), 500, 300);
2233     #endif
2234    
2235 harbaum 12 #if MAEMO_VERSION_MAJOR == 5
2236 harbaum 1 gtk_window_set_title(GTK_WINDOW(appdata.window), "GPXView");
2237 harbaum 12 #endif
2238    
2239 harbaum 1 g_signal_connect(G_OBJECT(appdata.window), "destroy",
2240     G_CALLBACK(on_window_destroy), &appdata);
2241    
2242     g_signal_connect(G_OBJECT(appdata.window), "key_press_event",
2243     G_CALLBACK(on_window_key_press), &appdata);
2244    
2245     appdata.vbox = gtk_vbox_new(FALSE, 2);
2246     gtk_container_add(GTK_CONTAINER(appdata.window), appdata.vbox);
2247 harbaum 3 #ifndef USE_STACKABLE_WINDOW
2248 harbaum 1 menu_create(&appdata);
2249 harbaum 3 #else
2250     hildon_window_set_app_menu(HILDON_WINDOW(appdata.window),
2251     menu_create(&appdata, MENU_GPXLIST));
2252     #endif
2253 harbaum 1
2254     #ifdef USE_BREAD_CRUMB_TRAIL
2255     appdata.bct = hildon_bread_crumb_trail_new();
2256    
2257     gtk_box_pack_start(GTK_BOX(appdata.vbox), appdata.bct, FALSE,FALSE,0);
2258    
2259     hildon_bread_crumb_trail_clear(HILDON_BREAD_CRUMB_TRAIL(appdata.bct));
2260     crumb_add(&appdata, "GPX", CRUMB_GPXLIST, NULL);
2261     #endif
2262    
2263     /* wait for main gui to appear */
2264     gtk_widget_show_all(GTK_WIDGET(appdata.window));
2265     while(gtk_events_pending())
2266     gtk_main_iteration();
2267    
2268     appdata.gconf_client = gconf_client_get_default();
2269     gconf_load_state(&appdata);
2270     gps_init(&appdata);
2271    
2272     appdata.cur_view = gpxlist_create_view_and_model(&appdata, NULL);
2273     gtk_box_pack_start_defaults(GTK_BOX(appdata.vbox), appdata.cur_view);
2274    
2275     gtk_widget_show_all(GTK_WIDGET(appdata.window));
2276     gtk_main();
2277    
2278     cleanup(&appdata);
2279    
2280     return 0;
2281     }