Contents of /trunk/src/main.c

Parent Directory Parent Directory | Revision Log Revision Log


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