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