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