Contents of /trunk/src/main.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 21 - (show annotations)
Sun Jul 5 17:38:06 2009 UTC (14 years, 9 months ago) by harbaum
File MIME type: text/plain
File size: 66975 byte(s)
Release 0.7.1
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 #ifdef USE_STACKABLE_WINDOW
1497 hildon_window_stack_pop_1 (hildon_window_stack_get_default());
1498 #endif
1499 }
1500
1501 #ifdef USE_MAEMO
1502 static void
1503 cb_menu_export_mmpoi(GtkWidget *widget, gpointer data) {
1504 appdata_t *appdata = (appdata_t*)data;
1505 mmpoi_export(appdata);
1506 #ifdef USE_STACKABLE_WINDOW
1507 hildon_window_stack_pop_1 (hildon_window_stack_get_default());
1508 #endif
1509 }
1510 #endif
1511
1512 static void
1513 cb_menu_export_garmin(GtkWidget *widget, gpointer data) {
1514 appdata_t *appdata = (appdata_t*)data;
1515 garmin_export(appdata);
1516 #ifdef USE_STACKABLE_WINDOW
1517 hildon_window_stack_pop_1 (hildon_window_stack_get_default());
1518 #endif
1519 }
1520
1521 static void
1522 cb_menu_geomath(GtkWidget *window, gpointer data) {
1523 geomath_dialog((appdata_t *)data);
1524 #ifdef USE_STACKABLE_WINDOW
1525 hildon_window_stack_pop_1 (hildon_window_stack_get_default());
1526 #endif
1527 }
1528
1529 static void
1530 cb_menu_geotext(GtkWidget *window, gpointer data) {
1531 geotext_dialog((appdata_t *)data);
1532 #ifdef USE_STACKABLE_WINDOW
1533 hildon_window_stack_pop_1 (hildon_window_stack_get_default());
1534 #endif
1535 }
1536
1537 static void
1538 cb_menu_precpos(GtkWidget *window, gpointer data) {
1539 precise_position((appdata_t *)data);
1540 #ifdef USE_STACKABLE_WINDOW
1541 hildon_window_stack_pop_1 (hildon_window_stack_get_default());
1542 #endif
1543 }
1544
1545 #ifdef USE_STACKABLE_WINDOW
1546 static GtkWidget *export_menu_create(appdata_t *appdata) {
1547 GtkWidget *button;
1548 HildonAppMenu *menu = HILDON_APP_MENU(hildon_app_menu_new());
1549
1550 button = hildon_button_new_with_text(
1551 HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH,
1552 HILDON_BUTTON_ARRANGEMENT_VERTICAL,
1553 _("Export to Maemo Mapper"),
1554 _("Save a Maemo Mapper POI file"));
1555 g_signal_connect(button, "clicked",
1556 G_CALLBACK(cb_menu_export_mmpoi), appdata);
1557 hildon_app_menu_append(menu, GTK_BUTTON(button));
1558
1559 button = hildon_button_new_with_text(
1560 HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH,
1561 HILDON_BUTTON_ARRANGEMENT_VERTICAL,
1562 _("Export Field Notes"),
1563 _("Save a Garmin Field Notes file"));
1564 g_signal_connect(button, "clicked",
1565 G_CALLBACK(cb_menu_export_log), appdata);
1566 hildon_app_menu_append(menu, GTK_BUTTON(button));
1567
1568 button = hildon_button_new_with_text(
1569 HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH,
1570 HILDON_BUTTON_ARRANGEMENT_VERTICAL,
1571 _("Export Garmin GPX"),
1572 _("Save modified waypoints in GPX file"));
1573 g_signal_connect(button, "clicked",
1574 G_CALLBACK(cb_menu_export_garmin), appdata);
1575 hildon_app_menu_append(menu, GTK_BUTTON(button));
1576
1577 gtk_widget_show_all(GTK_WIDGET(menu));
1578
1579 return GTK_WIDGET(menu);
1580 }
1581
1582 /* the export submenu */
1583 void on_export_clicked(GtkButton *button, appdata_t *appdata) {
1584 if(!appdata->export_menu)
1585 appdata->export_menu = export_menu_create(appdata);
1586
1587 /* draw a popup menu */
1588 hildon_app_menu_popup(HILDON_APP_MENU(appdata->export_menu),
1589 GTK_WINDOW(hildon_window_stack_peek(
1590 hildon_window_stack_get_default())));
1591 }
1592
1593 static GtkWidget *tools_menu_create(appdata_t *appdata) {
1594 GtkWidget *button;
1595 HildonAppMenu *menu = HILDON_APP_MENU(hildon_app_menu_new());
1596
1597 /* the following doesn't have an effect */
1598 // gtk_window_set_title(GTK_WINDOW(menu), "Tools");
1599
1600 button = hildon_button_new_with_text(
1601 HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH,
1602 HILDON_BUTTON_ARRANGEMENT_VERTICAL,
1603 _("Geomath"),
1604 _("Geocoordinate calculation"));
1605 g_signal_connect(button, "clicked",
1606 G_CALLBACK(cb_menu_geomath), appdata);
1607 hildon_app_menu_append(menu, GTK_BUTTON(button));
1608
1609
1610 button = hildon_button_new_with_text(
1611 HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH,
1612 HILDON_BUTTON_ARRANGEMENT_VERTICAL,
1613 _("Geotext"),
1614 _("Text analysis"));
1615 g_signal_connect(button, "clicked",
1616 G_CALLBACK(cb_menu_geotext), appdata);
1617 hildon_app_menu_append(menu, GTK_BUTTON(button));
1618
1619
1620 button = hildon_button_new_with_text(
1621 HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH,
1622 HILDON_BUTTON_ARRANGEMENT_VERTICAL,
1623 _("Precise Position"),
1624 _("Calculate a precise GPS position"));
1625 g_signal_connect(button, "clicked",
1626 G_CALLBACK(cb_menu_precpos), appdata);
1627 hildon_app_menu_append(menu, GTK_BUTTON(button));
1628
1629 gtk_widget_show_all(GTK_WIDGET(menu));
1630
1631 return GTK_WIDGET(menu);
1632 }
1633
1634 /* the tools submenu */
1635 void on_tools_clicked(GtkButton *button, appdata_t *appdata) {
1636 if(!appdata->tools_menu)
1637 appdata->tools_menu = tools_menu_create(appdata);
1638
1639 /* draw a popup menu */
1640 hildon_app_menu_popup(HILDON_APP_MENU(appdata->tools_menu),
1641 GTK_WINDOW(hildon_window_stack_peek(
1642 hildon_window_stack_get_default())));
1643 }
1644
1645
1646 HildonAppMenu *menu_create(appdata_t *appdata, int mode) {
1647 GtkWidget *button;
1648 HildonAppMenu *menu = HILDON_APP_MENU(hildon_app_menu_new());
1649
1650 /* ------- */
1651 button = gtk_button_new_with_label(_("About"));
1652 g_signal_connect_after(button, "clicked",
1653 G_CALLBACK(cb_menu_about), appdata);
1654 hildon_app_menu_append(menu, GTK_BUTTON(button));
1655
1656 button = gtk_button_new_with_label(_("Settings"));
1657 g_signal_connect_after(button, "clicked", G_CALLBACK(cb_menu_settings),
1658 appdata);
1659 hildon_app_menu_append(menu, GTK_BUTTON(button));
1660
1661 if(mode == MENU_GPXLIST) {
1662 button = gtk_button_new_with_label(_("Import file"));
1663 g_signal_connect_after(button, "clicked",
1664 G_CALLBACK(cb_menu_add), appdata);
1665 hildon_app_menu_append(menu, GTK_BUTTON(button));
1666
1667 button = gtk_button_new_with_label(_("Import directory"));
1668 g_signal_connect_after(button, "clicked",
1669 G_CALLBACK(cb_menu_adddir), appdata);
1670 hildon_app_menu_append(menu, GTK_BUTTON(button));
1671 }
1672
1673 if(mode == MENU_GPXLIST) {
1674 button = gtk_button_new_with_label(_("Export"));
1675 g_signal_connect_after(button, "clicked",
1676 G_CALLBACK(on_export_clicked), appdata);
1677 hildon_app_menu_append(menu, GTK_BUTTON(button));
1678
1679 button = gtk_button_new_with_label(_("Search"));
1680 g_signal_connect_after(button, "clicked",
1681 G_CALLBACK(cb_menu_search), appdata);
1682 hildon_app_menu_append(menu, GTK_BUTTON(button));
1683 }
1684
1685 button = gtk_button_new_with_label(_("Tools"));
1686 g_signal_connect_after(button, "clicked",
1687 G_CALLBACK(on_tools_clicked), appdata);
1688 hildon_app_menu_append(menu, GTK_BUTTON(button));
1689
1690 #ifdef HILDON_HELP
1691 button = gtk_button_new_with_label(_("Help"));
1692 g_signal_connect_after(button, "clicked",
1693 G_CALLBACK(cb_menu_help), appdata);
1694 hildon_app_menu_append(menu, GTK_BUTTON(button));
1695 #endif
1696
1697 gtk_widget_show_all(GTK_WIDGET(menu));
1698
1699 return menu;
1700 }
1701 #else
1702
1703 void menu_create(appdata_t *appdata) {
1704 GtkWidget *menu, *item;
1705 menu = gtk_menu_new();
1706
1707 #ifdef USE_BREAD_CRUMB_TRAIL
1708 appdata->menu_import =
1709 #endif
1710 item = gtk_menu_item_new_with_label(_("Import"));
1711 gtk_menu_append(GTK_MENU_SHELL(menu), item);
1712 GtkWidget *submenu = gtk_menu_new();
1713 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
1714
1715 item = gtk_menu_item_new_with_label( _("File") );
1716 gtk_menu_append(GTK_MENU_SHELL(submenu), item);
1717 g_signal_connect(item, "activate", GTK_SIGNAL_FUNC(cb_menu_add), appdata);
1718
1719 item = gtk_menu_item_new_with_label( _("Directory") );
1720 gtk_menu_append(GTK_MENU_SHELL(submenu), item);
1721 g_signal_connect(item, "activate", GTK_SIGNAL_FUNC(cb_menu_adddir), appdata);
1722
1723 #ifndef USE_PANNABLE_AREA
1724 gtk_menu_append(GTK_MENU_SHELL(submenu), gtk_separator_menu_item_new());
1725
1726 appdata->menu_close =
1727 item = gtk_menu_item_new_with_label( _("Close") );
1728 gtk_menu_append(GTK_MENU_SHELL(submenu), item);
1729 g_signal_connect(item, "activate", GTK_SIGNAL_FUNC(cb_menu_close), appdata);
1730
1731 appdata->menu_remove =
1732 item = gtk_menu_item_new_with_label( _("Remove") );
1733 gtk_menu_append(GTK_MENU_SHELL(submenu), item);
1734 g_signal_connect(item, "activate", GTK_SIGNAL_FUNC(cb_menu_remove), appdata);
1735 #endif
1736
1737 #ifdef USE_BREAD_CRUMB_TRAIL
1738 appdata->menu_export =
1739 #endif
1740 item = gtk_menu_item_new_with_label(_("Export"));
1741 gtk_menu_append(GTK_MENU_SHELL(menu), item);
1742 submenu = gtk_menu_new();
1743 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
1744
1745 #ifdef USE_MAEMO
1746 item = gtk_menu_item_new_with_label( _("Maemo Mapper POI") );
1747 gtk_menu_append(GTK_MENU_SHELL(submenu), item);
1748 g_signal_connect(item, "activate",
1749 GTK_SIGNAL_FUNC(cb_menu_export_mmpoi), appdata);
1750 #endif
1751
1752 item = gtk_menu_item_new_with_label( _("Garmin Field Notes") );
1753 gtk_menu_append(GTK_MENU_SHELL(submenu), item);
1754 g_signal_connect(item, "activate",
1755 GTK_SIGNAL_FUNC(cb_menu_export_log), appdata);
1756
1757 item = gtk_menu_item_new_with_label( _("Garmin GPX") );
1758 gtk_menu_append(GTK_MENU_SHELL(submenu), item);
1759 g_signal_connect(item, "activate",
1760 GTK_SIGNAL_FUNC(cb_menu_export_garmin), appdata);
1761
1762 #ifdef USE_BREAD_CRUMB_TRAIL
1763 appdata->menu_search =
1764 #endif
1765 item = gtk_menu_item_new_with_label( _("Search") );
1766 gtk_menu_append(GTK_MENU_SHELL(menu), item);
1767 g_signal_connect(item, "activate", GTK_SIGNAL_FUNC(cb_menu_search), appdata);
1768
1769 gtk_menu_append(GTK_MENU_SHELL(menu), gtk_separator_menu_item_new());
1770
1771 #ifndef NO_COPY_N_PASTE
1772 /* ----------- copy'n paste submenu ----------------- */
1773 appdata->clipboard = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
1774 gtk_clipboard_set_can_store(appdata->clipboard, NULL, 0);
1775
1776 item = gtk_menu_item_new_with_label(_("Edit"));
1777 gtk_menu_append(GTK_MENU_SHELL(menu), item);
1778 submenu = gtk_menu_new();
1779 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
1780
1781 appdata->menu_cut = item = gtk_menu_item_new_with_label( _("Cut") );
1782 gtk_widget_set_sensitive(item, FALSE);
1783 gtk_menu_append(GTK_MENU_SHELL(submenu), item);
1784 g_signal_connect(item, "activate", GTK_SIGNAL_FUNC(cb_menu_cut), appdata);
1785 appdata->menu_copy = item = gtk_menu_item_new_with_label( _("Copy") );
1786 gtk_widget_set_sensitive(item, FALSE);
1787 gtk_menu_append(GTK_MENU_SHELL(submenu), item);
1788 g_signal_connect(item, "activate", GTK_SIGNAL_FUNC(cb_menu_copy), appdata);
1789 appdata->menu_paste = item = gtk_menu_item_new_with_label( _("Paste") );
1790 gtk_widget_set_sensitive(item, FALSE);
1791 gtk_menu_append(GTK_MENU_SHELL(submenu), item);
1792 g_signal_connect(item, "activate", GTK_SIGNAL_FUNC(cb_menu_paste), appdata);
1793 #endif
1794
1795 item = gtk_menu_item_new_with_label( _("Settings") );
1796 gtk_menu_append(GTK_MENU_SHELL(menu), item);
1797 g_signal_connect(item, "activate", GTK_SIGNAL_FUNC(cb_menu_settings),
1798 appdata);
1799
1800 gtk_menu_append(GTK_MENU_SHELL(menu), gtk_separator_menu_item_new());
1801
1802 item = gtk_menu_item_new_with_label(_("Tools"));
1803 gtk_menu_append(GTK_MENU_SHELL(menu), item);
1804 submenu = gtk_menu_new();
1805 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
1806
1807 item = gtk_menu_item_new_with_label( _("Geomath") );
1808 gtk_menu_append(GTK_MENU_SHELL(submenu), item);
1809 g_signal_connect(item, "activate",
1810 GTK_SIGNAL_FUNC(cb_menu_geomath), appdata);
1811
1812 item = gtk_menu_item_new_with_label( _("Geotext") );
1813 gtk_menu_append(GTK_MENU_SHELL(submenu), item);
1814 g_signal_connect(item, "activate",
1815 GTK_SIGNAL_FUNC(cb_menu_geotext), appdata);
1816
1817 item = gtk_menu_item_new_with_label( _("Precise Position") );
1818 gtk_menu_append(GTK_MENU_SHELL(submenu), item);
1819 g_signal_connect(item, "activate",
1820 GTK_SIGNAL_FUNC(cb_menu_precpos), appdata);
1821
1822 gtk_menu_append(GTK_MENU_SHELL(menu), gtk_separator_menu_item_new());
1823
1824 #if defined(USE_MAEMO) && defined(HILDON_HELP)
1825 item = gtk_menu_item_new_with_label( _("Help") );
1826 gtk_menu_append(GTK_MENU_SHELL(menu), item);
1827 g_signal_connect(item, "activate", GTK_SIGNAL_FUNC(cb_menu_help), appdata);
1828 #endif
1829
1830 item = gtk_menu_item_new_with_label( _("About") );
1831 gtk_menu_append(GTK_MENU_SHELL(menu), item);
1832 g_signal_connect(item, "activate", GTK_SIGNAL_FUNC(cb_menu_about), appdata);
1833
1834 #ifndef USE_MAEMO
1835 item = gtk_menu_item_new_with_label( _("Quit") );
1836 gtk_menu_append(GTK_MENU_SHELL(menu), item);
1837 g_signal_connect(item, "activate", GTK_SIGNAL_FUNC(cb_menu_quit), appdata);
1838 #endif
1839
1840 #ifdef USE_MAEMO
1841 hildon_window_set_menu(appdata->window, GTK_MENU(menu));
1842 #else
1843 /* attach ordinary gtk menu */
1844 GtkWidget *menu_bar = gtk_menu_bar_new();
1845
1846 GtkWidget *root_menu = gtk_menu_item_new_with_label (_("Menu"));
1847 gtk_widget_show(root_menu);
1848
1849 gtk_menu_bar_append(menu_bar, root_menu);
1850 gtk_menu_item_set_submenu(GTK_MENU_ITEM (root_menu), menu);
1851
1852 gtk_widget_show(menu_bar);
1853 gtk_box_pack_start(GTK_BOX(appdata->vbox), menu_bar, 0, 0, 0);
1854 #endif
1855 }
1856 #endif
1857
1858 /********************* end of menu **********************/
1859
1860 void cleanup(appdata_t *appdata) {
1861 gpx_free_all(appdata->gpx);
1862 if(appdata->path) free(appdata->path);
1863 if(appdata->image_path) free(appdata->image_path);
1864 if(appdata->search_str) free(appdata->search_str);
1865
1866 gnome_vfs_shutdown();
1867 icons_free();
1868 gps_release(appdata);
1869
1870 #ifdef USE_MAEMO
1871 if(appdata->search_results) {
1872 printf("freeing pending search\n");
1873 search_result_free(appdata->search_results);
1874 }
1875
1876 if(appdata->osso_context)
1877 osso_deinitialize(appdata->osso_context);
1878
1879 appdata->program = NULL;
1880 #endif
1881
1882 /* free chain of locations */
1883 location_t *loc = appdata->location;
1884 while(loc) {
1885 location_t *next = loc->next;
1886 if(loc->name) free(loc->name);
1887 free(loc);
1888 loc = next;
1889 }
1890
1891 puts("everything is gone");
1892 }
1893
1894 void on_window_destroy (GtkWidget *widget, gpointer data) {
1895 appdata_t *appdata = (appdata_t*)data;
1896
1897 gconf_save_state(appdata);
1898 gtk_main_quit();
1899 appdata->window = NULL;
1900 }
1901
1902 gboolean on_window_key_press(GtkWidget *widget,
1903 GdkEventKey *event, appdata_t *appdata) {
1904 int handled = FALSE;
1905
1906 // printf("key event %d\n", event->keyval);
1907
1908 switch(event->keyval) {
1909 #ifdef USE_MAEMO
1910
1911 #ifdef HILDON_HARDKEY_INCREASE
1912 case HILDON_HARDKEY_INCREASE:
1913 html_zoom(appdata, TRUE);
1914 handled = TRUE;
1915 break;
1916 #endif
1917
1918 #ifdef HILDON_HARDKEY_DECREASE
1919 case HILDON_HARDKEY_DECREASE:
1920 html_zoom(appdata, FALSE);
1921 handled = TRUE;
1922 break;
1923 #endif
1924
1925 #ifdef HILDON_HARDKEY_FULLSCREEN
1926 case HILDON_HARDKEY_FULLSCREEN:
1927 {
1928 appdata->fullscreen = !appdata->fullscreen;
1929
1930 if(appdata->fullscreen)
1931 gtk_window_fullscreen(GTK_WINDOW(appdata->window));
1932 else
1933 gtk_window_unfullscreen(GTK_WINDOW(appdata->window));
1934
1935 handled = TRUE;
1936 }
1937 break;
1938 #endif
1939
1940 #else
1941 case '+':
1942 printf("zoom+\n");
1943 html_zoom(appdata, TRUE);
1944 handled = TRUE;
1945 break;
1946 case '-':
1947 printf("zoom-\n");
1948 html_zoom(appdata, FALSE);
1949 handled = TRUE;
1950 break;
1951 #endif
1952 }
1953
1954 return handled;
1955 }
1956
1957 #ifdef USE_BREAD_CRUMB_TRAIL
1958 typedef struct {
1959 int level;
1960 appdata_t *appdata;
1961 gpointer data;
1962 } crumb_t;
1963
1964 static void
1965 crumb_back(gpointer data) {
1966 crumb_t *crumb = (crumb_t*)data;
1967 printf("crumb_back called with %d\n", crumb->level);
1968
1969 /* don't do anything if main window has already been destroyed */
1970 if(!crumb->appdata->window) {
1971 printf("Main window gone ...\n");
1972 return;
1973 }
1974
1975 /* whatever is being displayed: we don't need it anymore */
1976 gtk_container_remove(GTK_CONTAINER(crumb->appdata->vbox),
1977 crumb->appdata->cur_view);
1978
1979 /* back from cache to cachelist */
1980 if(crumb->level == CRUMB_CACHE) {
1981 gpx_t *gpx = crumb->appdata->search_results;
1982
1983 if(!gpx) {
1984 gtk_widget_set_sensitive(crumb->appdata->menu_search, TRUE);
1985 gtk_widget_set_sensitive(crumb->appdata->menu_export, TRUE);
1986 printf("no search data found, return to gpx\n");
1987 gpx = crumb->appdata->cur_gpx;
1988 } else
1989 printf("returning to search result\n");
1990
1991 g_assert(gpx != NULL);
1992
1993 crumb->appdata->cur_view = cachelist_create(crumb->appdata, gpx,
1994 crumb->appdata->cur_cache);
1995
1996 /* returning from cache view: invalidate cache reference */
1997 crumb->appdata->cur_cache = NULL;
1998
1999 gtk_box_pack_start_defaults(GTK_BOX(crumb->appdata->vbox),
2000 crumb->appdata->cur_view);
2001 }
2002
2003 if(crumb->level == CRUMB_SEARCH_GPX) {
2004 printf("returning from a local search!\n");
2005
2006 g_assert((gpx_t*)crumb->data == crumb->appdata->search_results);
2007
2008 search_result_free((gpx_t*)crumb->data);
2009 crumb->appdata->search_results = NULL;
2010
2011 gtk_widget_set_sensitive(crumb->appdata->menu_search, TRUE);
2012
2013 crumb->appdata->cur_view = cachelist_create(crumb->appdata,
2014 crumb->appdata->cur_gpx, NULL);
2015 gtk_box_pack_start_defaults(GTK_BOX(crumb->appdata->vbox),
2016 crumb->appdata->cur_view);
2017 }
2018
2019 /* back from cachelist to gpxlist */
2020 if((crumb->level == CRUMB_CACHELIST) ||
2021 (crumb->level == CRUMB_SEARCH_GLOBAL)) {
2022
2023 crumb->appdata->cur_view = gpxlist_create_view_and_model(
2024 crumb->appdata, crumb->appdata->cur_gpx);
2025
2026 /* returning from cachelist/global search view: */
2027 /* invalidate gpx reference */
2028 crumb->appdata->cur_gpx = NULL;
2029
2030 gtk_box_pack_start_defaults(GTK_BOX(crumb->appdata->vbox),
2031 crumb->appdata->cur_view);
2032
2033 if((crumb->level == CRUMB_SEARCH_GLOBAL) ||
2034 (crumb->level == CRUMB_SEARCH_GPX)) {
2035 g_assert((gpx_t*)crumb->data == crumb->appdata->search_results);
2036
2037 search_result_free((gpx_t*)crumb->data);
2038 crumb->appdata->search_results = NULL;
2039 }
2040
2041 /* enable gpxlist related menu entries */
2042 gtk_widget_set_sensitive(crumb->appdata->menu_import, TRUE);
2043 gtk_widget_set_sensitive(crumb->appdata->menu_search, TRUE);
2044 gtk_widget_set_sensitive(crumb->appdata->menu_export, TRUE);
2045 }
2046
2047 gtk_widget_show_all(crumb->appdata->vbox);
2048 g_free(data);
2049 }
2050
2051 static void crumb_add(appdata_t *appdata, char *name, int level,
2052 gpointer user_data) {
2053 crumb_t *crumb = malloc(sizeof(crumb_t));
2054 crumb->level = level;
2055 crumb->appdata = appdata;
2056 crumb->data = user_data;
2057
2058 printf("crumb_add with level %d\n", level);
2059
2060 /* save that we are working on search results */
2061 if((level == CRUMB_SEARCH_GLOBAL) ||
2062 (level == CRUMB_SEARCH_GPX)) {
2063 appdata->search_results = (gpx_t*)user_data;
2064
2065 /* searches cannot be nested */
2066 gtk_widget_set_sensitive(appdata->menu_search, FALSE);
2067 }
2068
2069 /* save "path" pointers in appdata */
2070 if(crumb->level == CRUMB_CACHELIST)
2071 appdata->cur_gpx = (gpx_t*)user_data;
2072
2073 if(crumb->level == CRUMB_CACHE) {
2074 appdata->cur_cache = (cache_t*)user_data;
2075 /* the cache view cannot be searched */
2076 gtk_widget_set_sensitive(appdata->menu_search, FALSE);
2077 gtk_widget_set_sensitive(appdata->menu_export, FALSE);
2078 }
2079
2080 if(level != CRUMB_GPXLIST) {
2081 /* disable gpxlist related menu entries */
2082 gtk_widget_set_sensitive(appdata->menu_import, FALSE);
2083 #ifndef USE_PANNABLE_AREA
2084 gtk_widget_set_sensitive(appdata->menu_remove, FALSE);
2085 gtk_widget_set_sensitive(appdata->menu_close, FALSE);
2086 #endif
2087 }
2088
2089 hildon_bread_crumb_trail_push_text(HILDON_BREAD_CRUMB_TRAIL(appdata->bct),
2090 name, crumb, (GDestroyNotify)crumb_back);
2091 }
2092 #endif // USE_BREAD_CRUMB_TRAIL
2093
2094 void main_after_settings_redraw(appdata_t *appdata, int flags) {
2095 printf("main after settings redraw\n");
2096
2097 if(!appdata->cur_view) {
2098 printf("no active view\n");
2099 return;
2100 }
2101
2102 #ifndef USE_MAEMO
2103 // in non-maemo setup this can only affect the main screen as
2104 // the menu is blocked while a dialog is open. also the main
2105 // screen is always present
2106 if(appdata->gpxlist_items != appdata->cur_items) {
2107 /* re-do the main screen */
2108 gtk_container_remove(GTK_CONTAINER(appdata->vbox), appdata->cur_view);
2109 appdata->cur_view = gpxlist_create_view_and_model(appdata, NULL);
2110 gtk_box_pack_start_defaults(GTK_BOX(appdata->vbox), appdata->cur_view);
2111 gtk_widget_show_all(appdata->vbox);
2112 }
2113 #else
2114 /* a cache screen cannot be changed from the settings and thus doesn't */
2115 /* need to be redrawn */
2116 if(appdata->cur_cache) {
2117 printf("No redraw in cache view required\n");
2118 return;
2119 }
2120
2121 int redraw = 0; // nothing to redraw
2122
2123 if(appdata->search_results) {
2124 if((appdata->cur_items != appdata->cachelist_items) || flags)
2125 redraw = 1;
2126 } else {
2127 if(!appdata->cur_gpx) {
2128 if(appdata->cur_items != appdata->gpxlist_items)
2129 redraw = 2; // redraw gpxlist
2130 } else {
2131 if((appdata->cur_items != appdata->cachelist_items) || flags)
2132 redraw = 3; // redraw cachelist
2133 }
2134 }
2135
2136 if(redraw) {
2137 GtkWidget *container = appdata->vbox;
2138
2139 #ifdef USE_STACKABLE_WINDOW
2140 HildonWindowStack *stack = hildon_window_stack_get_default();
2141 container = hildon_window_stack_peek(stack);
2142 #endif
2143
2144 gtk_container_remove(GTK_CONTAINER(container), appdata->cur_view);
2145 switch(redraw) {
2146 case 1:
2147 appdata->cur_view = cachelist_create(appdata,
2148 appdata->search_results, NULL);
2149 break;
2150 case 2:
2151 appdata->cur_view = gpxlist_create_view_and_model(appdata, NULL);
2152 break;
2153 case 3:
2154 appdata->cur_view = cachelist_create(appdata,
2155 appdata->cur_gpx, NULL);
2156 break;
2157 }
2158
2159 #ifdef USE_STACKABLE_WINDOW
2160 if(container != appdata->vbox)
2161 gtk_container_add(GTK_CONTAINER(container), appdata->cur_view);
2162 else
2163 #endif
2164 gtk_box_pack_start_defaults(GTK_BOX(container), appdata->cur_view);
2165
2166 gtk_widget_show_all(container);
2167 }
2168 #endif // USE_MAEMO
2169 }
2170
2171 int main(int argc, char *argv[]) {
2172 appdata_t appdata;
2173
2174 /* init appdata */
2175 memset(&appdata, 0, sizeof(appdata));
2176
2177 printf("Using locale for %s in %s\n", PACKAGE, LOCALEDIR);
2178
2179 setlocale(LC_ALL, "");
2180 bindtextdomain(PACKAGE, LOCALEDIR);
2181 bind_textdomain_codeset(PACKAGE, "UTF-8");
2182 textdomain(PACKAGE);
2183
2184 /* prepare thread system */
2185 g_thread_init(NULL);
2186
2187 gtk_init (&argc, &argv);
2188
2189 #ifdef USE_MAEMO
2190 printf("Installing osso context for \"org.harbaum." APP "\"\n");
2191 appdata.osso_context = osso_initialize("org.harbaum."APP,
2192 VERSION, TRUE, NULL);
2193 if(appdata.osso_context == NULL) {
2194 fprintf(stderr, "error initiating osso context\n");
2195 }
2196
2197 dbus_register(&appdata);
2198 #endif
2199
2200 icons_init();
2201
2202 if(!gnome_vfs_init()) {
2203 g_error("Gnome VFS init failed\n");
2204 }
2205
2206 #ifdef USE_MAEMO
2207 /* Create the hildon program and setup the title */
2208 appdata.program = HILDON_PROGRAM(hildon_program_get_instance());
2209 g_set_application_name("GPXView");
2210
2211 /* Create HildonWindow and set it to HildonProgram */
2212 #ifdef USE_STACKABLE_WINDOW
2213 appdata.window = HILDON_WINDOW(hildon_stackable_window_new());
2214 #else
2215 appdata.window = HILDON_WINDOW(hildon_window_new());
2216 #endif
2217 hildon_program_add_window(appdata.program, appdata.window);
2218 #else
2219 /* Create a Window. */
2220 appdata.window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
2221 /* Set a decent default size for the window. */
2222 gtk_window_set_default_size(GTK_WINDOW(appdata.window), 500, 300);
2223 #endif
2224
2225 #if MAEMO_VERSION_MAJOR == 5
2226 gtk_window_set_title(GTK_WINDOW(appdata.window), "GPXView");
2227 #endif
2228
2229 g_signal_connect(G_OBJECT(appdata.window), "destroy",
2230 G_CALLBACK(on_window_destroy), &appdata);
2231
2232 g_signal_connect(G_OBJECT(appdata.window), "key_press_event",
2233 G_CALLBACK(on_window_key_press), &appdata);
2234
2235 appdata.vbox = gtk_vbox_new(FALSE, 2);
2236 gtk_container_add(GTK_CONTAINER(appdata.window), appdata.vbox);
2237
2238 #ifndef USE_STACKABLE_WINDOW
2239 menu_create(&appdata);
2240 #else
2241 hildon_window_set_app_menu(HILDON_WINDOW(appdata.window),
2242 menu_create(&appdata, MENU_GPXLIST));
2243 #endif
2244
2245 #ifdef USE_BREAD_CRUMB_TRAIL
2246 appdata.bct = hildon_bread_crumb_trail_new();
2247
2248 gtk_box_pack_start(GTK_BOX(appdata.vbox), appdata.bct, FALSE,FALSE,0);
2249
2250 hildon_bread_crumb_trail_clear(HILDON_BREAD_CRUMB_TRAIL(appdata.bct));
2251 crumb_add(&appdata, "GPX", CRUMB_GPXLIST, NULL);
2252 #endif
2253
2254 /* wait for main gui to appear */
2255 gtk_widget_show_all(GTK_WIDGET(appdata.window));
2256 while(gtk_events_pending())
2257 gtk_main_iteration();
2258
2259 appdata.gconf_client = gconf_client_get_default();
2260 gconf_load_state(&appdata);
2261 gps_init(&appdata);
2262
2263 appdata.cur_view = gpxlist_create_view_and_model(&appdata, NULL);
2264 gtk_box_pack_start_defaults(GTK_BOX(appdata.vbox), appdata.cur_view);
2265
2266 gtk_widget_show_all(GTK_WIDGET(appdata.window));
2267 gtk_main();
2268
2269 cleanup(&appdata);
2270
2271 return 0;
2272 }