Contents of /trunk/src/main.c

Parent Directory Parent Directory | Revision Log Revision Log


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