Contents of /trunk/src/main.c

Parent Directory Parent Directory | Revision Log Revision Log


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