Contents of /trunk/src/main.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 113 - (show annotations)
Wed Sep 16 13:45:10 2009 UTC (14 years, 8 months ago) by harbaum
File MIME type: text/plain
File size: 69266 byte(s)
OSD balloon
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 GtkTreeIter iter;
981 gtk_list_store_append(appdata->gpxstore, &iter);
982 gpxlist_set(appdata->gpxstore, &iter, gpx);
983
984 if(gpx == sel_gpx) {
985 sel_iter = iter;
986 sel_iter_valid = TRUE;
987 }
988
989 gpx = gpx->next;
990 }
991
992 gtk_tree_view_set_model(GTK_TREE_VIEW(appdata->gpxview),
993 GTK_TREE_MODEL(appdata->gpxstore));
994
995 g_object_unref(appdata->gpxstore);
996
997 if(sel_iter_valid) {
998 gtk_tree_selection_select_iter(selection, &sel_iter);
999 path = gtk_tree_model_get_path(GTK_TREE_MODEL(appdata->gpxstore),
1000 &sel_iter);
1001 gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(appdata->gpxview),
1002 path, NULL, TRUE, 0.0, 0.0);
1003 gtk_tree_path_free(path);
1004 }
1005
1006 /* make list react on clicks */
1007 g_signal_connect(appdata->gpxview, "row-activated",
1008 (GCallback)gpxlist_view_onRowActivated, appdata);
1009
1010 /* put this inside a scrolled view */
1011 #ifndef USE_PANNABLE_AREA
1012 GtkWidget *scrolled_window = gtk_scrolled_window_new(NULL, NULL);
1013 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window),
1014 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
1015 gtk_container_add(GTK_CONTAINER(scrolled_window), appdata->gpxview);
1016
1017 return scrolled_window;
1018 #else
1019 GtkWidget *pannable_area = hildon_pannable_area_new();
1020 gtk_container_add(GTK_CONTAINER(pannable_area), appdata->gpxview);
1021
1022 return pannable_area;
1023 #endif
1024 }
1025
1026 /* add last entry in gpx list to visual representation */
1027 static void gpxlist_add(appdata_t *appdata, gpx_t *new) {
1028 GtkTreeIter iter;
1029
1030 gtk_list_store_append(appdata->gpxstore, &iter);
1031 gpxlist_set(appdata->gpxstore, &iter, new);
1032
1033 /* and attach entry to end of list */
1034 gpx_t **gpx = &appdata->gpx;
1035 while(*gpx) gpx = &((*gpx)->next);
1036 *gpx = new;
1037 }
1038
1039 /******************** end of gpxlist ********************/
1040
1041 /******************** begin of menu *********************/
1042
1043 typedef struct {
1044 appdata_t *appdata;
1045 GtkWidget *dialog;
1046 } about_context_t;
1047
1048 #ifdef ENABLE_BROWSER_INTERFACE
1049 void on_paypal_button_clicked(GtkButton *button, about_context_t *context) {
1050 gtk_dialog_response(GTK_DIALOG(context->dialog), GTK_RESPONSE_ACCEPT);
1051 browser_url(context->appdata,
1052 "https://www.paypal.com/cgi-bin/webscr"
1053 "?cmd=_s-xclick&hosted_button_id=7400558");
1054 }
1055 #endif
1056
1057 static void
1058 cb_menu_about(GtkWidget *window, gpointer data) {
1059 about_context_t context;
1060
1061 context.appdata = (appdata_t *)data;
1062
1063 #ifdef ENABLE_LIBLOCATION
1064 char *uses = "uses liblocation";
1065 #elif defined(ENABLE_GPSBT)
1066 char *uses = "uses gpsbt and gpsd";
1067 #else
1068 char *uses = "uses gpsd";
1069 #endif
1070
1071 const gchar *authors[] = {
1072 "Till Harbaum <till@harbaum.org>",
1073 "John Stowers <john.stowers@gmail.com>",
1074 NULL };
1075
1076 context.dialog = g_object_new(GTK_TYPE_ABOUT_DIALOG,
1077 "name", "GPXView",
1078 "version", VERSION,
1079 "copyright", _("Copyright 2008-2009"),
1080 "authors", authors,
1081 "website", _("http://www.harbaum.org/till/maemo"),
1082 "comments", _(uses),
1083 NULL);
1084
1085 #ifdef ENABLE_BROWSER_INTERFACE
1086 /* add a way to donate to the project */
1087 GtkWidget *alignment = gtk_alignment_new(0.5, 0, 0, 0);
1088
1089 GtkWidget *hbox = gtk_hbox_new(FALSE, 8);
1090 gtk_box_pack_start(GTK_BOX(hbox),
1091 gtk_label_new(_("Do you like GPXView?")),
1092 FALSE, FALSE, 0);
1093
1094 GtkWidget *button = gtk_button_new();
1095 gtk_button_set_image(GTK_BUTTON(button),
1096 icon_get_widget(ICON_MISC, 8));
1097 gtk_button_set_relief(GTK_BUTTON(button), GTK_RELIEF_NONE);
1098 g_signal_connect(button, "clicked",
1099 G_CALLBACK(on_paypal_button_clicked), &context);
1100 gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0);
1101
1102 gtk_container_add(GTK_CONTAINER(alignment), hbox);
1103 gtk_box_pack_start_defaults(GTK_BOX((GTK_DIALOG(context.dialog))->vbox),
1104 alignment);
1105
1106 gtk_widget_show_all(alignment);
1107 #endif
1108
1109 gtk_dialog_run(GTK_DIALOG(context.dialog));
1110 gtk_widget_destroy(context.dialog);
1111 }
1112
1113 #if defined(USE_MAEMO) && defined(HILDON_HELP)
1114 static void
1115 cb_menu_help(GtkWidget *window, gpointer data) {
1116 appdata_t *appdata = (appdata_t*)data;
1117
1118 hildon_help_show(appdata->osso_context, HELP_ID_INTRO, 0);
1119 }
1120 #endif
1121
1122 static void
1123 cb_menu_add(GtkWidget *window, gpointer data) {
1124 appdata_t *appdata = (appdata_t *)data;
1125
1126 gpx_t *new = choose_file(appdata, FALSE);
1127 if(new) gpxlist_add(appdata, new);
1128 }
1129
1130 static void
1131 cb_menu_adddir(GtkWidget *window, gpointer data) {
1132 appdata_t *appdata = (appdata_t *)data;
1133
1134 gpx_t *new = choose_file(appdata, TRUE);
1135 if(new) gpxlist_add(appdata, new);
1136 }
1137
1138 #ifndef USE_PANNABLE_AREA
1139 static void
1140 cb_menu_close(GtkWidget *window, gpointer data) {
1141 appdata_t *appdata = (appdata_t *)data;
1142 GtkTreeSelection *selection;
1143 GtkTreeModel *model;
1144 GtkTreeIter iter;
1145
1146 printf("selected close\n");
1147
1148 /* the entry cannot be closed again */
1149 gtk_widget_set_sensitive(appdata->menu_close, FALSE);
1150
1151 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(appdata->gpxview));
1152
1153 printf("gpxlist close\n");
1154
1155 if (gtk_tree_selection_get_selected(selection, &model, &iter)) {
1156 gpx_t *gpx = NULL;
1157 gtk_tree_model_get(model, &iter, GPXLIST_COL_DATA, &gpx, -1);
1158
1159 if(gpx) gpxlist_close(appdata, GTK_LIST_STORE(model), &iter, gpx);
1160 } else {
1161 g_print ("no row selected.\n");
1162 }
1163 }
1164
1165 static void
1166 cb_menu_remove(GtkWidget *window, gpointer data) {
1167 appdata_t *appdata = (appdata_t *)data;
1168
1169 /* disable menu item */
1170 gtk_widget_set_sensitive(appdata->menu_remove, FALSE);
1171 gtk_widget_set_sensitive(appdata->menu_close, FALSE);
1172
1173 GtkTreeModel *model;
1174 GtkTreeIter iter;
1175 GtkTreeSelection *selection =
1176 gtk_tree_view_get_selection(GTK_TREE_VIEW(appdata->gpxview));
1177
1178 printf("gpxlist remove\n");
1179
1180 if(gtk_tree_selection_get_selected(selection, &model, &iter)) {
1181 gpx_t *gpx = NULL;
1182 gtk_tree_model_get(model, &iter, GPXLIST_COL_DATA, &gpx, -1);
1183
1184 if(gpx) gpxlist_remove(appdata, GTK_LIST_STORE(model), &iter, gpx);
1185 } else {
1186 g_print ("no row selected.\n");
1187 }
1188 }
1189
1190 #endif // !USE_PANNABLE_AREA
1191
1192 static void search_result_free(gpx_t *result) {
1193 printf("freeing search results\n");
1194
1195 /* free found chain */
1196 cache_t *cache = result->cache;
1197 while(cache) {
1198 cache_t *next = cache->next;
1199 free(cache);
1200 cache = next;
1201 }
1202 free(result->name);
1203 free(result);
1204 }
1205
1206 #define MAX_HITS 50
1207
1208 static time_t localize_time(time_t in) {
1209 time_t ret;
1210 char *tz;
1211 struct tm *tm = localtime(&in);
1212
1213 tz = getenv("TZ");
1214 setenv("TZ", "", 1);
1215 tzset();
1216 ret = mktime(tm);
1217 if (tz)
1218 setenv("TZ", tz, 1);
1219 else
1220 unsetenv("TZ");
1221 tzset();
1222 return ret;
1223 }
1224
1225 static int days_ago(time_t in) {
1226 int day_in = localize_time(in) / (60*60*24);
1227 int day_now = localize_time(time(NULL)) / (60*60*24);
1228
1229 return day_now - day_in;
1230 }
1231
1232 gpx_t *search_do(appdata_t *appdata, gpx_t *gpx, char *phrase,
1233 int what, gboolean local) {
1234 /* walk through all caches */
1235
1236 int hits = 0;
1237 gpx_t *found = malloc(sizeof(gpx_t));
1238 memset(found, 0, sizeof(gpx_t));
1239 cache_t **cacheP = &(found->cache);
1240
1241 if(what & SEARCH_FINDS) {
1242 time_t loc_now = localize_time(time(NULL));
1243 printf("now: %ld days since 1/1/1970, days hour is %ld\n",
1244 loc_now/(60*60*24), loc_now%(60*60*24)/(60*60));
1245 }
1246
1247 while(gpx && hits < MAX_HITS) {
1248
1249 /* we need all notes ... */
1250 if(what & SEARCH_FINDS) {
1251 notes_load_all(appdata, gpx);
1252 gpx->notes_loaded = TRUE;
1253 }
1254
1255 cache_t *cache = gpx->cache;
1256
1257 while(cache && hits < MAX_HITS) {
1258 gboolean hit = FALSE;
1259
1260 if(what & SEARCH_FINDS) {
1261 if(cache->notes && cache->notes->found ) {
1262 int days = days_ago(cache->notes->ftime);
1263
1264 if(cache->id)
1265 printf("find of %s is %d days ago\n", cache->id, days);
1266
1267 if(days <= appdata->search_days)
1268 hit = 1;
1269 }
1270 } else if(cache->id && (what & SEARCH_ID) &&
1271 strcasestr(cache->id, phrase))
1272 hit = 1;
1273 else if(cache->name && (what & SEARCH_NAME) &&
1274 strcasestr(cache->name, phrase))
1275 hit = 1;
1276 else if(cache->short_description && (what & SEARCH_DESC) &&
1277 strcasestr(cache->short_description, phrase))
1278 hit = 1;
1279 else if(cache->long_description && (what & SEARCH_DESC) &&
1280 strcasestr(cache->long_description, phrase))
1281 hit = 1;
1282 else if(cache->owner && (what & SEARCH_OWNER) &&
1283 strcasestr(cache->owner, phrase))
1284 hit = 1;
1285
1286 if(hit) {
1287 /* chain a copy of this cache structure into the found list */
1288 *cacheP = malloc(sizeof(cache_t));
1289 memcpy(*cacheP, cache, sizeof(cache_t));
1290 (*cacheP)->next = NULL;
1291 cacheP = &((*cacheP)->next);
1292 hits++;
1293 }
1294 cache = cache->next;
1295 }
1296
1297 if(!local) gpx = gpx->next;
1298 else gpx = NULL; /* local search within one gpx only */
1299 }
1300
1301 found->name = strdup(_("Search results"));
1302
1303 return found;
1304 }
1305
1306 typedef struct {
1307 appdata_t *appdata;
1308 GtkWidget *entry, *spinner;
1309 GtkWidget *in_id, *in_name, *in_desc, *in_owner, *in_finds;
1310 } search_context_t;
1311
1312 static void callback_finds_toggled(GtkWidget *widget, gpointer data ) {
1313 search_context_t *context = (search_context_t*)data;
1314
1315 gboolean in_finds = gtk_toggle_button_get_active(
1316 GTK_TOGGLE_BUTTON(context->in_finds));
1317
1318 gtk_widget_set_sensitive(context->entry, !in_finds);
1319 gtk_widget_set_sensitive(context->in_id, !in_finds);
1320 gtk_widget_set_sensitive(context->in_name, !in_finds);
1321 gtk_widget_set_sensitive(context->in_desc, !in_finds);
1322 gtk_widget_set_sensitive(context->in_owner, !in_finds);
1323 gtk_widget_set_sensitive(context->spinner, in_finds);
1324 }
1325
1326 static void
1327 cb_menu_search(GtkWidget *window, gpointer data) {
1328 appdata_t *appdata = (appdata_t *)data;
1329
1330 search_context_t context;
1331 memset(&context, 0, sizeof(search_context_t));
1332 context.appdata = appdata;
1333
1334 GtkWidget *dialog = gtk_dialog_new_with_buttons(_("Enter search phrase"),
1335 GTK_WINDOW(appdata->window), GTK_DIALOG_MODAL,
1336 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
1337 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
1338 NULL);
1339
1340 gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(dialog)->vbox),
1341 gtk_label_new(_("Search in:")));
1342
1343 GtkWidget *table = gtk_table_new(2, 2, TRUE);
1344 gtk_table_set_col_spacing(GTK_TABLE(table), 0, 8);
1345
1346 context.in_id = gtk_check_button_new_with_label(_("Waypoint IDs"));
1347 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(context.in_id),
1348 appdata->search & SEARCH_ID);
1349 gtk_table_attach_defaults(GTK_TABLE(table), context.in_id, 0, 1, 0, 1);
1350
1351 context.in_name = gtk_check_button_new_with_label(_("Names"));
1352 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(context.in_name),
1353 appdata->search & SEARCH_NAME);
1354 gtk_table_attach_defaults(GTK_TABLE(table), context.in_name, 1, 2, 0, 1);
1355
1356 context.in_desc = gtk_check_button_new_with_label(_("Descriptions"));
1357 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(context.in_desc),
1358 appdata->search & SEARCH_DESC);
1359 gtk_table_attach_defaults(GTK_TABLE(table), context.in_desc, 0, 1, 1, 2);
1360
1361 context.in_owner = gtk_check_button_new_with_label(_("Owner"));
1362 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(context.in_owner),
1363 appdata->search & SEARCH_OWNER);
1364 gtk_table_attach_defaults(GTK_TABLE(table), context.in_owner, 1, 2, 1, 2);
1365
1366 gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(dialog)->vbox), table);
1367
1368 /* -------------------------------------------------------------- */
1369
1370 gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(dialog)->vbox),
1371 gtk_label_new(_("Search for:")));
1372 gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(dialog)->vbox),
1373 context.entry = gtk_entry_new());
1374 if(appdata->search_str)
1375 gtk_entry_set_text(GTK_ENTRY(context.entry), appdata->search_str);
1376
1377 /* -------------------------------------------------------------- */
1378
1379 gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(dialog)->vbox),
1380 gtk_hseparator_new());
1381
1382 GtkWidget *hbox = gtk_hbox_new(FALSE, 5);
1383
1384 context.in_finds = gtk_check_button_new_with_label(_("Search finds for"));
1385 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(context.in_finds),
1386 appdata->search & SEARCH_FINDS);
1387 gtk_box_pack_start_defaults(GTK_BOX(hbox), context.in_finds);
1388 g_signal_connect(G_OBJECT(context.in_finds), "toggled",
1389 G_CALLBACK(callback_finds_toggled), &context);
1390
1391 #ifndef USE_MAEMO
1392 GtkObject *adj = gtk_adjustment_new(appdata->search_days, 0, 99, 1, 10, 10);
1393 context.spinner = gtk_spin_button_new(GTK_ADJUSTMENT(adj), 1, 0);
1394 #else
1395 context.spinner = hildon_number_editor_new(0, 99);
1396 hildon_number_editor_set_value(HILDON_NUMBER_EDITOR(context.spinner),
1397 appdata->search_days);
1398 #endif
1399 gtk_box_pack_start_defaults(GTK_BOX(hbox), context.spinner);
1400
1401 gtk_box_pack_start_defaults(GTK_BOX(hbox), gtk_label_new(_("days")));
1402
1403 gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(dialog)->vbox), hbox);
1404
1405 /* -------------------------------------------------------------- */
1406
1407 gtk_widget_show_all(dialog);
1408 callback_finds_toggled(NULL, &context);
1409
1410 if(GTK_RESPONSE_ACCEPT == gtk_dialog_run(GTK_DIALOG(dialog))) {
1411 char *p = strdup(gtk_entry_get_text(GTK_ENTRY(context.entry)));
1412
1413 /* update saved search string */
1414 if(appdata->search_str) free(appdata->search_str);
1415 if(strlen(p) > 0)
1416 appdata->search_str = strdup(p);
1417
1418 #ifndef USE_MAEMO
1419 appdata->search_days = gtk_spin_button_get_value_as_int(
1420 GTK_SPIN_BUTTON(context.spinner));
1421 #else
1422 appdata->search_days = hildon_number_editor_get_value(
1423 HILDON_NUMBER_EDITOR(context.spinner));
1424 #endif
1425
1426 if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(context.in_finds)))
1427 appdata->search |= SEARCH_FINDS;
1428 else
1429 appdata->search &= ~SEARCH_FINDS;
1430
1431 if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(context.in_id)))
1432 appdata->search |= SEARCH_ID;
1433 else
1434 appdata->search &= ~SEARCH_ID;
1435
1436 if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(context.in_name)))
1437 appdata->search |= SEARCH_NAME;
1438 else
1439 appdata->search &= ~SEARCH_NAME;
1440
1441 if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(context.in_desc)))
1442 appdata->search |= SEARCH_DESC;
1443 else
1444 appdata->search &= ~SEARCH_DESC;
1445
1446 if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(context.in_owner)))
1447 appdata->search |= SEARCH_OWNER;
1448 else
1449 appdata->search &= ~SEARCH_OWNER;
1450
1451 gtk_widget_destroy(dialog);
1452
1453 /* don't search if we are asked to search for nothing */
1454 if(((appdata->search & (SEARCH_ID|SEARCH_NAME|SEARCH_DESC|SEARCH_OWNER)) &&
1455 strlen(p) > 0) || (appdata->search & SEARCH_FINDS)) {
1456
1457 printf("Search for %s (flags = %x)...\n", p, appdata->search);
1458
1459 #ifndef USE_BREAD_CRUMB_TRAIL
1460 gpx_t *found =
1461 search_do(appdata, appdata->gpx, p, appdata->search, FALSE);
1462
1463 /* do search result dialog here ... */
1464 cachelist_dialog(appdata, found);
1465 #ifndef USE_STACKABLE_WINDOW
1466 search_result_free(found);
1467 #else
1468 appdata->search_results = found;
1469 #endif
1470 #else
1471 gpx_t *found = NULL;
1472
1473 if(appdata->cur_gpx)
1474 found = search_do(appdata, appdata->cur_gpx, p, appdata->search, TRUE);
1475 else
1476 found = search_do(appdata, appdata->gpx, p, appdata->search, FALSE);
1477
1478 gtk_container_remove(GTK_CONTAINER(appdata->vbox), appdata->cur_view);
1479 appdata->cur_view = cachelist_create(appdata, found, NULL);
1480 gtk_box_pack_start_defaults(GTK_BOX(appdata->vbox), appdata->cur_view);
1481 gtk_widget_show_all(appdata->vbox);
1482 crumb_add(appdata, found->name,
1483 appdata->cur_gpx?CRUMB_SEARCH_GPX:CRUMB_SEARCH_GLOBAL, found);
1484 #endif
1485 } else
1486 printf("No valid search: \"%s\" with flags %x!\n", p, appdata->search);
1487
1488 free(p);
1489 } else
1490 gtk_widget_destroy(dialog);
1491 }
1492
1493 static void on_window_destroy (GtkWidget *widget, gpointer data);
1494
1495 #ifndef USE_MAEMO
1496 static void
1497 cb_menu_quit(GtkWidget *window, gpointer data) {
1498 on_window_destroy(window, data);
1499 }
1500 #endif
1501
1502 #ifndef NO_COPY_N_PASTE
1503 static void
1504 cb_menu_cut(GtkWidget *widget, gpointer data) {
1505 appdata_t *appdata = (appdata_t*)data;
1506
1507 if(appdata->active_buffer) {
1508 if(GTK_WIDGET_TYPE(appdata->active_buffer) == GTK_TYPE_TEXT_BUFFER) {
1509 gtk_text_buffer_cut_clipboard(GTK_TEXT_BUFFER(appdata->active_buffer),
1510 appdata->clipboard, TRUE);
1511 } else
1512 printf("cut: ERROR, not a text buffer\n");
1513 } else
1514 printf("cut: ERROR, no active buffer\n");
1515 }
1516
1517 static void
1518 cb_menu_copy(GtkWidget *widget, gpointer data) {
1519 appdata_t *appdata = (appdata_t*)data;
1520
1521 if(appdata->active_buffer) {
1522 if(GTK_WIDGET_TYPE(appdata->active_buffer) == GTK_TYPE_TEXT_BUFFER) {
1523 gtk_text_buffer_copy_clipboard(GTK_TEXT_BUFFER(appdata->active_buffer),
1524 appdata->clipboard);
1525 } else if(GTK_WIDGET_TYPE(appdata->active_buffer) == gtk_html_get_type()) {
1526 printf("copy from html buffer\n");
1527 html_copy_to_clipboard(appdata);
1528 } else
1529 printf("copy: ERROR, not a text nor a html buffer\n");
1530 } else
1531 printf("copy: ERROR, no active buffer\n");
1532 }
1533
1534 static void
1535 cb_menu_paste(GtkWidget *widget, gpointer data) {
1536 appdata_t *appdata = (appdata_t*)data;
1537
1538 if(appdata->active_buffer) {
1539 if(GTK_WIDGET_TYPE(appdata->active_buffer) == GTK_TYPE_TEXT_BUFFER) {
1540 gtk_text_buffer_paste_clipboard(GTK_TEXT_BUFFER(appdata->active_buffer),
1541 appdata->clipboard, NULL, TRUE);
1542 } else
1543 printf("paste: ERROR, not a text buffer\n");
1544 } else
1545 printf("paste: ERROR, no active buffer\n");
1546 }
1547 #endif
1548
1549 static void
1550 cb_menu_export_log(GtkWidget *widget, gpointer data) {
1551 appdata_t *appdata = (appdata_t*)data;
1552 notes_log_export(appdata);
1553 }
1554
1555 #ifdef USE_MAEMO
1556 static void
1557 cb_menu_export_mmpoi(GtkWidget *widget, gpointer data) {
1558 appdata_t *appdata = (appdata_t*)data;
1559 mmpoi_export(appdata);
1560 }
1561 #endif
1562
1563 static void
1564 cb_menu_export_garmin(GtkWidget *widget, gpointer data) {
1565 appdata_t *appdata = (appdata_t*)data;
1566 garmin_export(appdata);
1567 }
1568
1569 #ifdef ENABLE_OSM_GPS_MAP
1570 static void
1571 cb_menu_map(GtkWidget *window, gpointer data) {
1572 map((appdata_t *)data);
1573 }
1574 #endif
1575
1576 static void
1577 cb_menu_geomath(GtkWidget *window, gpointer data) {
1578 geomath_dialog((appdata_t *)data);
1579 }
1580
1581 static void
1582 cb_menu_geotext(GtkWidget *window, gpointer data) {
1583 geotext_dialog((appdata_t *)data);
1584 }
1585
1586 static void
1587 cb_menu_precpos(GtkWidget *window, gpointer data) {
1588 precise_position((appdata_t *)data);
1589 }
1590
1591 #ifdef USE_STACKABLE_WINDOW
1592 typedef struct {
1593 char *label, *desc;
1594 GtkSignalFunc activate_cb;
1595 } menu_entry_t;
1596
1597 typedef struct {
1598 const char *title;
1599 const menu_entry_t *menu;
1600 int len;
1601 } submenu_t;
1602
1603 #define COLUMNS 1
1604
1605 void on_submenu_entry_clicked(GtkButton *button, GtkWidget *menu) {
1606
1607 /* force closing of submenu dialog */
1608 gtk_dialog_response(GTK_DIALOG(menu), GTK_RESPONSE_NONE);
1609 gtk_widget_hide(menu);
1610
1611 /* let gtk clean up */
1612 while(gtk_events_pending())
1613 gtk_main_iteration();
1614 }
1615
1616 static GtkWidget *app_submenu_create(appdata_t *appdata,
1617 const submenu_t *submenu) {
1618
1619 /* create a oridinary dialog box */
1620 GtkWidget *dialog = gtk_dialog_new();
1621 gtk_window_set_title(GTK_WINDOW(dialog), _(submenu->title));
1622 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
1623 gtk_window_set_transient_for(GTK_WINDOW(dialog),
1624 GTK_WINDOW(appdata->window));
1625 gtk_dialog_set_has_separator(GTK_DIALOG(dialog), FALSE);
1626
1627 GtkWidget *table = gtk_table_new(submenu->len/COLUMNS, COLUMNS, TRUE);
1628 int x = 0, y = 0;
1629
1630 const menu_entry_t *menu_entries = submenu->menu;
1631 while(menu_entries->label) {
1632 GtkWidget *button = NULL;
1633
1634 button = hildon_button_new_with_text(
1635 HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH,
1636 HILDON_BUTTON_ARRANGEMENT_VERTICAL,
1637 _(menu_entries->label), _(menu_entries->desc));
1638
1639 /* try to center both texts */
1640 hildon_button_set_title_alignment(HILDON_BUTTON(button), 0.5, 0.5);
1641 hildon_button_set_value_alignment(HILDON_BUTTON(button), 0.5, 0.5);
1642
1643 g_signal_connect(button, "clicked",
1644 G_CALLBACK(on_submenu_entry_clicked), dialog);
1645
1646 g_signal_connect(button, "clicked",
1647 menu_entries->activate_cb, appdata);
1648
1649 gtk_table_attach_defaults(GTK_TABLE(table), button, x, x+1, y, y+1);
1650
1651 x++;
1652 if(x == COLUMNS) { x = 0; y++; }
1653
1654 menu_entries++;
1655 }
1656
1657 gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(dialog)->vbox), table);
1658
1659 return dialog;
1660 }
1661
1662 /* popup the dialog shaped submenu */
1663 static void submenu_popup(GtkWidget *menu) {
1664 gtk_widget_show_all(menu);
1665 gtk_dialog_run(GTK_DIALOG(menu));
1666 gtk_widget_hide(menu);
1667 }
1668
1669 static void submenu_cleanup(GtkWidget *menu) {
1670 gtk_widget_destroy(menu);
1671 }
1672
1673 static const menu_entry_t submenu_export_entries[] = {
1674 { "Export to Maemo Mapper" , "Save a Maemo Mapper POI file",
1675 G_CALLBACK(cb_menu_export_mmpoi) },
1676 { "Export Field Notes", "Save a Garmin Field Notes file",
1677 G_CALLBACK(cb_menu_export_log) },
1678 { "Export Garmin GPX", "Save modified waypoints in GPX file",
1679 G_CALLBACK(cb_menu_export_garmin) },
1680 { NULL, NULL, NULL }
1681 };
1682
1683 static const submenu_t submenu_export = {
1684 "Export", submenu_export_entries,
1685 sizeof(submenu_export_entries)/sizeof(menu_entry_t)-1
1686 };
1687
1688 /* the export submenu */
1689 void on_export_clicked(GtkButton *button, appdata_t *appdata) {
1690 if(!appdata->export_menu)
1691 appdata->export_menu = app_submenu_create(appdata, &submenu_export);
1692
1693 submenu_popup(appdata->export_menu);
1694 }
1695
1696 static const menu_entry_t submenu_tools_entries[] = {
1697 { "Geomath", "Geocoordinate calculation",
1698 G_CALLBACK(cb_menu_geomath) },
1699 { "Geotext", "Text analysis",
1700 G_CALLBACK(cb_menu_geotext) },
1701 { "Precise Position", "Calculate a precise GPS position",
1702 G_CALLBACK(cb_menu_precpos) },
1703 { NULL, NULL, NULL }
1704 };
1705
1706 static const submenu_t submenu_tools = {
1707 "Tools", submenu_tools_entries,
1708 sizeof(submenu_tools_entries)/sizeof(menu_entry_t)-1
1709 };
1710
1711 /* the tools submenu */
1712 void on_tools_clicked(GtkButton *button, appdata_t *appdata) {
1713 if(!appdata->tools_menu)
1714 appdata->tools_menu = app_submenu_create(appdata, &submenu_tools);
1715
1716 submenu_popup(appdata->tools_menu);
1717 }
1718
1719 HildonAppMenu *menu_create(appdata_t *appdata, int mode) {
1720 GtkWidget *button;
1721 HildonAppMenu *menu = HILDON_APP_MENU(hildon_app_menu_new());
1722
1723 /* ------- */
1724 button = gtk_button_new_with_label(_("About"));
1725 g_signal_connect_after(button, "clicked",
1726 G_CALLBACK(cb_menu_about), appdata);
1727 hildon_app_menu_append(menu, GTK_BUTTON(button));
1728
1729 button = gtk_button_new_with_label(_("Settings"));
1730 g_signal_connect_after(button, "clicked", G_CALLBACK(cb_menu_settings),
1731 appdata);
1732 hildon_app_menu_append(menu, GTK_BUTTON(button));
1733
1734 if(mode == MENU_GPXLIST) {
1735 button = gtk_button_new_with_label(_("Import file"));
1736 g_signal_connect_after(button, "clicked",
1737 G_CALLBACK(cb_menu_add), appdata);
1738 hildon_app_menu_append(menu, GTK_BUTTON(button));
1739
1740 button = gtk_button_new_with_label(_("Import directory"));
1741 g_signal_connect_after(button, "clicked",
1742 G_CALLBACK(cb_menu_adddir), appdata);
1743 hildon_app_menu_append(menu, GTK_BUTTON(button));
1744
1745 button = gtk_button_new_with_label(_("Export"));
1746 g_signal_connect_after(button, "clicked",
1747 G_CALLBACK(on_export_clicked), appdata);
1748 hildon_app_menu_append(menu, GTK_BUTTON(button));
1749
1750 button = gtk_button_new_with_label(_("Search"));
1751 g_signal_connect_after(button, "clicked",
1752 G_CALLBACK(cb_menu_search), appdata);
1753 hildon_app_menu_append(menu, GTK_BUTTON(button));
1754 }
1755
1756 button = gtk_button_new_with_label(_("Tools"));
1757 g_signal_connect_after(button, "clicked",
1758 G_CALLBACK(on_tools_clicked), appdata);
1759 hildon_app_menu_append(menu, GTK_BUTTON(button));
1760
1761 #ifdef ENABLE_OSM_GPS_MAP
1762 if((mode == MENU_GPXLIST) || (mode == MENU_CACHELIST)) {
1763 button = gtk_button_new_with_label(_("Map"));
1764 g_signal_connect_after(button, "clicked",
1765 G_CALLBACK(cb_menu_map), appdata);
1766 hildon_app_menu_append(menu, GTK_BUTTON(button));
1767 }
1768 #endif
1769
1770 #ifdef HILDON_HELP
1771 button = gtk_button_new_with_label(_("Help"));
1772 g_signal_connect_after(button, "clicked",
1773 G_CALLBACK(cb_menu_help), appdata);
1774 hildon_app_menu_append(menu, GTK_BUTTON(button));
1775 #endif
1776
1777 gtk_widget_show_all(GTK_WIDGET(menu));
1778
1779 return menu;
1780 }
1781 #else
1782
1783 void menu_create(appdata_t *appdata) {
1784 GtkWidget *menu, *item;
1785 menu = gtk_menu_new();
1786
1787 #ifdef USE_BREAD_CRUMB_TRAIL
1788 appdata->menu_import =
1789 #endif
1790 item = gtk_menu_item_new_with_label(_("Import"));
1791 gtk_menu_append(GTK_MENU_SHELL(menu), item);
1792 GtkWidget *submenu = gtk_menu_new();
1793 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
1794
1795 item = gtk_menu_item_new_with_label( _("File") );
1796 gtk_menu_append(GTK_MENU_SHELL(submenu), item);
1797 g_signal_connect(item, "activate", GTK_SIGNAL_FUNC(cb_menu_add), appdata);
1798
1799 item = gtk_menu_item_new_with_label( _("Directory") );
1800 gtk_menu_append(GTK_MENU_SHELL(submenu), item);
1801 g_signal_connect(item, "activate", GTK_SIGNAL_FUNC(cb_menu_adddir), appdata);
1802
1803 #ifndef USE_PANNABLE_AREA
1804 gtk_menu_append(GTK_MENU_SHELL(submenu), gtk_separator_menu_item_new());
1805
1806 appdata->menu_close =
1807 item = gtk_menu_item_new_with_label( _("Close") );
1808 gtk_menu_append(GTK_MENU_SHELL(submenu), item);
1809 g_signal_connect(item, "activate", GTK_SIGNAL_FUNC(cb_menu_close), appdata);
1810
1811 appdata->menu_remove =
1812 item = gtk_menu_item_new_with_label( _("Remove") );
1813 gtk_menu_append(GTK_MENU_SHELL(submenu), item);
1814 g_signal_connect(item, "activate", GTK_SIGNAL_FUNC(cb_menu_remove), appdata);
1815 #endif
1816
1817 #ifdef USE_BREAD_CRUMB_TRAIL
1818 appdata->menu_export =
1819 #endif
1820 item = gtk_menu_item_new_with_label(_("Export"));
1821 gtk_menu_append(GTK_MENU_SHELL(menu), item);
1822 submenu = gtk_menu_new();
1823 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
1824
1825 #ifdef USE_MAEMO
1826 item = gtk_menu_item_new_with_label( _("Maemo Mapper POI") );
1827 gtk_menu_append(GTK_MENU_SHELL(submenu), item);
1828 g_signal_connect(item, "activate",
1829 GTK_SIGNAL_FUNC(cb_menu_export_mmpoi), appdata);
1830 #endif
1831
1832 item = gtk_menu_item_new_with_label( _("Garmin Field Notes") );
1833 gtk_menu_append(GTK_MENU_SHELL(submenu), item);
1834 g_signal_connect(item, "activate",
1835 GTK_SIGNAL_FUNC(cb_menu_export_log), appdata);
1836
1837 item = gtk_menu_item_new_with_label( _("Garmin GPX") );
1838 gtk_menu_append(GTK_MENU_SHELL(submenu), item);
1839 g_signal_connect(item, "activate",
1840 GTK_SIGNAL_FUNC(cb_menu_export_garmin), appdata);
1841
1842 #ifdef USE_BREAD_CRUMB_TRAIL
1843 appdata->menu_search =
1844 #endif
1845 item = gtk_menu_item_new_with_label( _("Search") );
1846 gtk_menu_append(GTK_MENU_SHELL(menu), item);
1847 g_signal_connect(item, "activate", GTK_SIGNAL_FUNC(cb_menu_search), appdata);
1848
1849 gtk_menu_append(GTK_MENU_SHELL(menu), gtk_separator_menu_item_new());
1850
1851 #ifndef NO_COPY_N_PASTE
1852 /* ----------- copy'n paste submenu ----------------- */
1853 appdata->clipboard = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
1854 gtk_clipboard_set_can_store(appdata->clipboard, NULL, 0);
1855
1856 item = gtk_menu_item_new_with_label(_("Edit"));
1857 gtk_menu_append(GTK_MENU_SHELL(menu), item);
1858 submenu = gtk_menu_new();
1859 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
1860
1861 appdata->menu_cut = item = gtk_menu_item_new_with_label( _("Cut") );
1862 gtk_widget_set_sensitive(item, FALSE);
1863 gtk_menu_append(GTK_MENU_SHELL(submenu), item);
1864 g_signal_connect(item, "activate", GTK_SIGNAL_FUNC(cb_menu_cut), appdata);
1865 appdata->menu_copy = item = gtk_menu_item_new_with_label( _("Copy") );
1866 gtk_widget_set_sensitive(item, FALSE);
1867 gtk_menu_append(GTK_MENU_SHELL(submenu), item);
1868 g_signal_connect(item, "activate", GTK_SIGNAL_FUNC(cb_menu_copy), appdata);
1869 appdata->menu_paste = item = gtk_menu_item_new_with_label( _("Paste") );
1870 gtk_widget_set_sensitive(item, FALSE);
1871 gtk_menu_append(GTK_MENU_SHELL(submenu), item);
1872 g_signal_connect(item, "activate", GTK_SIGNAL_FUNC(cb_menu_paste), appdata);
1873 #endif
1874
1875 item = gtk_menu_item_new_with_label( _("Settings") );
1876 gtk_menu_append(GTK_MENU_SHELL(menu), item);
1877 g_signal_connect(item, "activate", GTK_SIGNAL_FUNC(cb_menu_settings),
1878 appdata);
1879
1880 gtk_menu_append(GTK_MENU_SHELL(menu), gtk_separator_menu_item_new());
1881
1882 #ifdef ENABLE_OSM_GPS_MAP
1883 item = gtk_menu_item_new_with_label( _("Map") );
1884 gtk_menu_append(GTK_MENU_SHELL(menu), item);
1885 g_signal_connect(item, "activate",
1886 GTK_SIGNAL_FUNC(cb_menu_map), appdata);
1887 #endif
1888
1889 item = gtk_menu_item_new_with_label(_("Tools"));
1890 gtk_menu_append(GTK_MENU_SHELL(menu), item);
1891 submenu = gtk_menu_new();
1892 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
1893
1894 item = gtk_menu_item_new_with_label( _("Geomath") );
1895 gtk_menu_append(GTK_MENU_SHELL(submenu), item);
1896 g_signal_connect(item, "activate",
1897 GTK_SIGNAL_FUNC(cb_menu_geomath), appdata);
1898
1899 item = gtk_menu_item_new_with_label( _("Geotext") );
1900 gtk_menu_append(GTK_MENU_SHELL(submenu), item);
1901 g_signal_connect(item, "activate",
1902 GTK_SIGNAL_FUNC(cb_menu_geotext), appdata);
1903
1904 item = gtk_menu_item_new_with_label( _("Precise Position") );
1905 gtk_menu_append(GTK_MENU_SHELL(submenu), item);
1906 g_signal_connect(item, "activate",
1907 GTK_SIGNAL_FUNC(cb_menu_precpos), appdata);
1908
1909 gtk_menu_append(GTK_MENU_SHELL(menu), gtk_separator_menu_item_new());
1910
1911 #if defined(USE_MAEMO) && defined(HILDON_HELP)
1912 item = gtk_menu_item_new_with_label( _("Help") );
1913 gtk_menu_append(GTK_MENU_SHELL(menu), item);
1914 g_signal_connect(item, "activate", GTK_SIGNAL_FUNC(cb_menu_help), appdata);
1915 #endif
1916
1917 item = gtk_menu_item_new_with_label( _("About") );
1918 gtk_menu_append(GTK_MENU_SHELL(menu), item);
1919 g_signal_connect(item, "activate", GTK_SIGNAL_FUNC(cb_menu_about), appdata);
1920
1921 #ifndef USE_MAEMO
1922 item = gtk_menu_item_new_with_label( _("Quit") );
1923 gtk_menu_append(GTK_MENU_SHELL(menu), item);
1924 g_signal_connect(item, "activate", GTK_SIGNAL_FUNC(cb_menu_quit), appdata);
1925 #endif
1926
1927 #ifdef USE_MAEMO
1928 hildon_window_set_menu(appdata->window, GTK_MENU(menu));
1929 #else
1930 /* attach ordinary gtk menu */
1931 GtkWidget *menu_bar = gtk_menu_bar_new();
1932
1933 GtkWidget *root_menu = gtk_menu_item_new_with_label (_("Menu"));
1934 gtk_widget_show(root_menu);
1935
1936 gtk_menu_bar_append(menu_bar, root_menu);
1937 gtk_menu_item_set_submenu(GTK_MENU_ITEM (root_menu), menu);
1938
1939 gtk_widget_show(menu_bar);
1940 gtk_box_pack_start(GTK_BOX(appdata->vbox), menu_bar, 0, 0, 0);
1941 #endif
1942 }
1943 #endif
1944
1945 /********************* end of menu **********************/
1946
1947 void cleanup(appdata_t *appdata) {
1948 gpx_free_all(appdata->gpx);
1949 if(appdata->path) free(appdata->path);
1950 if(appdata->image_path) free(appdata->image_path);
1951 if(appdata->search_str) free(appdata->search_str);
1952
1953 #ifdef USE_STACKABLE_WINDOW
1954 if(appdata->export_menu) submenu_cleanup(appdata->export_menu);
1955 if(appdata->tools_menu) submenu_cleanup(appdata->tools_menu);
1956 #endif
1957
1958 gnome_vfs_shutdown();
1959 icons_free();
1960 gps_release(appdata);
1961
1962 #ifdef USE_MAEMO
1963 if(appdata->search_results) {
1964 printf("freeing pending search\n");
1965 search_result_free(appdata->search_results);
1966 }
1967
1968 if(appdata->osso_context)
1969 osso_deinitialize(appdata->osso_context);
1970
1971 appdata->program = NULL;
1972 #endif
1973
1974 /* free chain of locations */
1975 location_t *loc = appdata->location;
1976 while(loc) {
1977 location_t *next = loc->next;
1978 if(loc->name) free(loc->name);
1979 free(loc);
1980 loc = next;
1981 }
1982
1983 puts("everything is gone");
1984 }
1985
1986 static void on_window_destroy (GtkWidget *widget, gpointer data) {
1987 appdata_t *appdata = (appdata_t*)data;
1988
1989 gconf_save_state(appdata);
1990 gtk_main_quit();
1991 appdata->window = NULL;
1992 }
1993
1994 gboolean on_window_key_press(GtkWidget *widget,
1995 GdkEventKey *event, appdata_t *appdata) {
1996 int handled = FALSE;
1997
1998 // printf("key event %d\n", event->keyval);
1999
2000 switch(event->keyval) {
2001 #ifdef USE_MAEMO
2002
2003 #ifdef HILDON_HARDKEY_INCREASE
2004 case HILDON_HARDKEY_INCREASE:
2005 html_zoom(appdata, TRUE);
2006 handled = TRUE;
2007 break;
2008 #endif
2009
2010 #ifdef HILDON_HARDKEY_DECREASE
2011 case HILDON_HARDKEY_DECREASE:
2012 html_zoom(appdata, FALSE);
2013 handled = TRUE;
2014 break;
2015 #endif
2016
2017 #ifdef HILDON_HARDKEY_FULLSCREEN
2018 case HILDON_HARDKEY_FULLSCREEN:
2019 {
2020 appdata->fullscreen = !appdata->fullscreen;
2021
2022 if(appdata->fullscreen)
2023 gtk_window_fullscreen(GTK_WINDOW(appdata->window));
2024 else
2025 gtk_window_unfullscreen(GTK_WINDOW(appdata->window));
2026
2027 handled = TRUE;
2028 }
2029 break;
2030 #endif
2031
2032 #else
2033 case '+':
2034 printf("zoom+\n");
2035 html_zoom(appdata, TRUE);
2036 handled = TRUE;
2037 break;
2038 case '-':
2039 printf("zoom-\n");
2040 html_zoom(appdata, FALSE);
2041 handled = TRUE;
2042 break;
2043 #endif
2044 }
2045
2046 return handled;
2047 }
2048
2049 #ifdef USE_BREAD_CRUMB_TRAIL
2050 typedef struct {
2051 int level;
2052 appdata_t *appdata;
2053 gpointer data;
2054 } crumb_t;
2055
2056 static void
2057 crumb_back(gpointer data) {
2058 crumb_t *crumb = (crumb_t*)data;
2059 printf("crumb_back called with %d\n", crumb->level);
2060
2061 /* don't do anything if main window has already been destroyed */
2062 if(!crumb->appdata->window) {
2063 printf("Main window gone ...\n");
2064 return;
2065 }
2066
2067 /* whatever is being displayed: we don't need it anymore */
2068 gtk_container_remove(GTK_CONTAINER(crumb->appdata->vbox),
2069 crumb->appdata->cur_view);
2070
2071 /* back from cache to cachelist */
2072 if(crumb->level == CRUMB_CACHE) {
2073 gpx_t *gpx = crumb->appdata->search_results;
2074
2075 if(!gpx) {
2076 gtk_widget_set_sensitive(crumb->appdata->menu_search, TRUE);
2077 gtk_widget_set_sensitive(crumb->appdata->menu_export, TRUE);
2078 printf("no search data found, return to gpx\n");
2079 gpx = crumb->appdata->cur_gpx;
2080 } else
2081 printf("returning to search result\n");
2082
2083 g_assert(gpx != NULL);
2084
2085 crumb->appdata->cur_view = cachelist_create(crumb->appdata, gpx,
2086 crumb->appdata->cur_cache);
2087
2088 /* returning from cache view: invalidate cache reference */
2089 crumb->appdata->cur_cache = NULL;
2090
2091 gtk_box_pack_start_defaults(GTK_BOX(crumb->appdata->vbox),
2092 crumb->appdata->cur_view);
2093 }
2094
2095 if(crumb->level == CRUMB_SEARCH_GPX) {
2096 printf("returning from a local search!\n");
2097
2098 g_assert((gpx_t*)crumb->data == crumb->appdata->search_results);
2099
2100 search_result_free((gpx_t*)crumb->data);
2101 crumb->appdata->search_results = NULL;
2102
2103 gtk_widget_set_sensitive(crumb->appdata->menu_search, TRUE);
2104
2105 crumb->appdata->cur_view = cachelist_create(crumb->appdata,
2106 crumb->appdata->cur_gpx, NULL);
2107 gtk_box_pack_start_defaults(GTK_BOX(crumb->appdata->vbox),
2108 crumb->appdata->cur_view);
2109 }
2110
2111 /* back from cachelist to gpxlist */
2112 if((crumb->level == CRUMB_CACHELIST) ||
2113 (crumb->level == CRUMB_SEARCH_GLOBAL)) {
2114
2115 crumb->appdata->cur_view = gpxlist_create_view_and_model(
2116 crumb->appdata, crumb->appdata->cur_gpx);
2117
2118 /* returning from cachelist/global search view: */
2119 /* invalidate gpx reference */
2120 crumb->appdata->cur_gpx = NULL;
2121
2122 gtk_box_pack_start_defaults(GTK_BOX(crumb->appdata->vbox),
2123 crumb->appdata->cur_view);
2124
2125 if((crumb->level == CRUMB_SEARCH_GLOBAL) ||
2126 (crumb->level == CRUMB_SEARCH_GPX)) {
2127 g_assert((gpx_t*)crumb->data == crumb->appdata->search_results);
2128
2129 search_result_free((gpx_t*)crumb->data);
2130 crumb->appdata->search_results = NULL;
2131 }
2132
2133 /* enable gpxlist related menu entries */
2134 gtk_widget_set_sensitive(crumb->appdata->menu_import, TRUE);
2135 gtk_widget_set_sensitive(crumb->appdata->menu_search, TRUE);
2136 gtk_widget_set_sensitive(crumb->appdata->menu_export, TRUE);
2137 }
2138
2139 gtk_widget_show_all(crumb->appdata->vbox);
2140 g_free(data);
2141 }
2142
2143 static void crumb_add(appdata_t *appdata, char *name, int level,
2144 gpointer user_data) {
2145 crumb_t *crumb = malloc(sizeof(crumb_t));
2146 crumb->level = level;
2147 crumb->appdata = appdata;
2148 crumb->data = user_data;
2149
2150 printf("crumb_add with level %d\n", level);
2151
2152 /* save that we are working on search results */
2153 if((level == CRUMB_SEARCH_GLOBAL) ||
2154 (level == CRUMB_SEARCH_GPX)) {
2155 appdata->search_results = (gpx_t*)user_data;
2156
2157 /* searches cannot be nested */
2158 gtk_widget_set_sensitive(appdata->menu_search, FALSE);
2159 }
2160
2161 /* save "path" pointers in appdata */
2162 if(crumb->level == CRUMB_CACHELIST)
2163 appdata->cur_gpx = (gpx_t*)user_data;
2164
2165 if(crumb->level == CRUMB_CACHE) {
2166 appdata->cur_cache = (cache_t*)user_data;
2167 /* the cache view cannot be searched */
2168 gtk_widget_set_sensitive(appdata->menu_search, FALSE);
2169 gtk_widget_set_sensitive(appdata->menu_export, FALSE);
2170 }
2171
2172 if(level != CRUMB_GPXLIST) {
2173 /* disable gpxlist related menu entries */
2174 gtk_widget_set_sensitive(appdata->menu_import, FALSE);
2175 #ifndef USE_PANNABLE_AREA
2176 gtk_widget_set_sensitive(appdata->menu_remove, FALSE);
2177 gtk_widget_set_sensitive(appdata->menu_close, FALSE);
2178 #endif
2179 }
2180
2181 hildon_bread_crumb_trail_push_text(HILDON_BREAD_CRUMB_TRAIL(appdata->bct),
2182 name, crumb, (GDestroyNotify)crumb_back);
2183 }
2184 #endif // USE_BREAD_CRUMB_TRAIL
2185
2186 void main_after_settings_redraw(appdata_t *appdata, int flags) {
2187 printf("main after settings redraw\n");
2188
2189 if(!appdata->cur_view) {
2190 printf("no active view\n");
2191 return;
2192 }
2193
2194 #ifndef USE_MAEMO
2195 // in non-maemo setup this can only affect the main screen as
2196 // the menu is blocked while a dialog is open. also the main
2197 // screen is always present
2198 if(appdata->gpxlist_items != appdata->cur_items) {
2199 /* re-do the main screen */
2200 gtk_container_remove(GTK_CONTAINER(appdata->vbox), appdata->cur_view);
2201 appdata->cur_view = gpxlist_create_view_and_model(appdata, NULL);
2202 gtk_box_pack_start_defaults(GTK_BOX(appdata->vbox), appdata->cur_view);
2203 gtk_widget_show_all(appdata->vbox);
2204 }
2205 #else
2206 /* a cache screen cannot be changed from the settings and thus doesn't */
2207 /* need to be redrawn */
2208 if(appdata->cur_cache) {
2209 printf("No redraw in cache view required\n");
2210 return;
2211 }
2212
2213 int redraw = 0; // nothing to redraw
2214
2215 if(appdata->search_results) {
2216 if((appdata->cur_items != appdata->cachelist_items) || flags)
2217 redraw = 1;
2218 } else {
2219 if(!appdata->cur_gpx) {
2220 if(appdata->cur_items != appdata->gpxlist_items)
2221 redraw = 2; // redraw gpxlist
2222 } else {
2223 if((appdata->cur_items != appdata->cachelist_items) || flags)
2224 redraw = 3; // redraw cachelist
2225 }
2226 }
2227
2228 if(redraw) {
2229 GtkWidget *container = appdata->vbox;
2230
2231 #ifdef USE_STACKABLE_WINDOW
2232 HildonWindowStack *stack = hildon_window_stack_get_default();
2233 container = hildon_window_stack_peek(stack);
2234 #endif
2235
2236 gtk_container_remove(GTK_CONTAINER(container), appdata->cur_view);
2237 switch(redraw) {
2238 case 1:
2239 appdata->cur_view = cachelist_create(appdata,
2240 appdata->search_results, NULL);
2241 break;
2242 case 2:
2243 appdata->cur_view = gpxlist_create_view_and_model(appdata, NULL);
2244 break;
2245 case 3:
2246 appdata->cur_view = cachelist_create(appdata,
2247 appdata->cur_gpx, NULL);
2248 break;
2249 }
2250
2251 #ifdef USE_STACKABLE_WINDOW
2252 if(container != appdata->vbox)
2253 gtk_container_add(GTK_CONTAINER(container), appdata->cur_view);
2254 else
2255 #endif
2256 gtk_box_pack_start_defaults(GTK_BOX(container), appdata->cur_view);
2257
2258 gtk_widget_show_all(container);
2259 }
2260 #endif // USE_MAEMO
2261 }
2262
2263 int main(int argc, char *argv[]) {
2264 appdata_t appdata;
2265
2266 /* init appdata */
2267 memset(&appdata, 0, sizeof(appdata));
2268
2269 printf("Using locale for %s in %s\n", PACKAGE, LOCALEDIR);
2270
2271 setlocale(LC_ALL, "");
2272 bindtextdomain(PACKAGE, LOCALEDIR);
2273 bind_textdomain_codeset(PACKAGE, "UTF-8");
2274 textdomain(PACKAGE);
2275
2276 /* prepare thread system */
2277 g_thread_init(NULL);
2278
2279 gtk_init (&argc, &argv);
2280
2281 #ifdef USE_MAEMO
2282 printf("Installing osso context for \"org.harbaum." APP "\"\n");
2283 appdata.osso_context = osso_initialize("org.harbaum."APP,
2284 VERSION, TRUE, NULL);
2285 if(appdata.osso_context == NULL) {
2286 fprintf(stderr, "error initiating osso context\n");
2287 }
2288
2289 dbus_register(&appdata);
2290 #endif
2291
2292 icons_init();
2293
2294 if(!gnome_vfs_init()) {
2295 g_error("Gnome VFS init failed\n");
2296 }
2297
2298 #ifdef USE_MAEMO
2299 /* Create the hildon program and setup the title */
2300 appdata.program = HILDON_PROGRAM(hildon_program_get_instance());
2301 g_set_application_name("GPXView");
2302
2303 /* Create HildonWindow and set it to HildonProgram */
2304 #ifdef USE_STACKABLE_WINDOW
2305 appdata.window = HILDON_WINDOW(hildon_stackable_window_new());
2306 #else
2307 appdata.window = HILDON_WINDOW(hildon_window_new());
2308 #endif
2309 hildon_program_add_window(appdata.program, appdata.window);
2310 #else
2311 /* Create a Window. */
2312 appdata.window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
2313 /* Set a decent default size for the window. */
2314 gtk_window_set_default_size(GTK_WINDOW(appdata.window), 500, 300);
2315 #endif
2316
2317 #if MAEMO_VERSION_MAJOR == 5
2318 gtk_window_set_title(GTK_WINDOW(appdata.window), "GPXView");
2319 #endif
2320
2321 g_signal_connect(G_OBJECT(appdata.window), "destroy",
2322 G_CALLBACK(on_window_destroy), &appdata);
2323
2324 g_signal_connect(G_OBJECT(appdata.window), "key_press_event",
2325 G_CALLBACK(on_window_key_press), &appdata);
2326
2327 appdata.vbox = gtk_vbox_new(FALSE, 2);
2328 gtk_container_add(GTK_CONTAINER(appdata.window), appdata.vbox);
2329 #ifndef USE_STACKABLE_WINDOW
2330 menu_create(&appdata);
2331 #else
2332 hildon_window_set_app_menu(HILDON_WINDOW(appdata.window),
2333 menu_create(&appdata, MENU_GPXLIST));
2334 #endif
2335
2336 #ifdef USE_BREAD_CRUMB_TRAIL
2337 appdata.bct = hildon_bread_crumb_trail_new();
2338
2339 gtk_box_pack_start(GTK_BOX(appdata.vbox), appdata.bct, FALSE,FALSE,0);
2340
2341 hildon_bread_crumb_trail_clear(HILDON_BREAD_CRUMB_TRAIL(appdata.bct));
2342 crumb_add(&appdata, "GPX", CRUMB_GPXLIST, NULL);
2343 #endif
2344
2345 /* wait for main gui to appear */
2346 gtk_widget_show_all(GTK_WIDGET(appdata.window));
2347 while(gtk_events_pending())
2348 gtk_main_iteration();
2349
2350 appdata.gconf_client = gconf_client_get_default();
2351 gconf_load_state(&appdata);
2352 gps_init(&appdata);
2353
2354 appdata.cur_view = gpxlist_create_view_and_model(&appdata, NULL);
2355 gtk_box_pack_start_defaults(GTK_BOX(appdata.vbox), appdata.cur_view);
2356
2357 gtk_widget_show_all(GTK_WIDGET(appdata.window));
2358 gtk_main();
2359
2360 cleanup(&appdata);
2361
2362 return 0;
2363 }