Contents of /trunk/src/main.c

Parent Directory Parent Directory | Revision Log Revision Log


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