Contents of /trunk/src/main.c

Parent Directory Parent Directory | Revision Log Revision Log


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