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