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