Contents of /trunk/src/main.c

Parent Directory Parent Directory | Revision Log Revision Log


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