Parent Directory | Revision Log
No copy'n paste on fremantle
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 | gtk_widget_show_all(window); |
555 | } |
556 | #endif |
557 | #endif |
558 | |
559 | /******************** end of cachelist ********************/ |
560 | |
561 | /******************** begin of gpxlist ********************/ |
562 | |
563 | enum { |
564 | GPXLIST_COL_ICON = 0, |
565 | GPXLIST_COL_FILENAME, |
566 | GPXLIST_COL_NAME, |
567 | GPXLIST_COL_DATE, |
568 | GPXLIST_COL_CACHES, |
569 | GPXLIST_COL_OPEN, |
570 | #ifdef USE_PANNABLE_AREA |
571 | GPXLIST_COL_DELETE, |
572 | #endif |
573 | GPXLIST_COL_DATA, |
574 | GPXLIST_NUM_COLS |
575 | } ; |
576 | |
577 | static GdkPixbuf *gpx_icon_get(gpx_t *gpx) { |
578 | if(gpx->filename && g_file_test(gpx->filename, G_FILE_TEST_IS_DIR)) |
579 | return icon_get(ICON_FILE, 1); |
580 | |
581 | if(gpx->filename&& !strcasecmp(gpx->filename+strlen(gpx->filename)-4,".zip")) |
582 | return icon_get(ICON_FILE, 2); |
583 | |
584 | return icon_get(ICON_FILE, 0); |
585 | } |
586 | |
587 | static void gpxlist_set(GtkListStore *store, GtkTreeIter *iter, gpx_t *gpx) { |
588 | char date_str[32], cnum[32]; |
589 | |
590 | if(gpx->year && gpx->month && gpx->day) { |
591 | GDate *date = g_date_new_dmy(gpx->day, gpx->month, gpx->year); |
592 | g_date_strftime(date_str, sizeof(date_str), "%x", date); |
593 | g_date_free(date); |
594 | } else |
595 | strcpy(date_str, "---"); |
596 | |
597 | char *fname = strrchr(gpx->filename, '/'); |
598 | if(!fname) fname = gpx->filename; |
599 | else fname++; /* skip '/' */ |
600 | |
601 | snprintf(cnum, sizeof(cnum), "%d", gpx_total_caches(gpx)); |
602 | |
603 | /* Append a row and fill in some data */ |
604 | gtk_list_store_set(store, iter, |
605 | GPXLIST_COL_ICON, gpx_icon_get(gpx), |
606 | GPXLIST_COL_FILENAME, fname, |
607 | GPXLIST_COL_NAME, gpx->name, |
608 | GPXLIST_COL_DATE, gpx->closed?NULL:date_str, |
609 | GPXLIST_COL_OPEN, !gpx->closed, |
610 | GPXLIST_COL_CACHES, gpx->closed?NULL:cnum, |
611 | #ifdef USE_PANNABLE_AREA |
612 | GPXLIST_COL_DELETE, icon_get(ICON_MISC, 7), |
613 | #endif |
614 | GPXLIST_COL_DATA, gpx, |
615 | -1); |
616 | } |
617 | |
618 | static void gpxlist_remove(appdata_t *appdata, |
619 | GtkListStore *store, GtkTreeIter *iter, |
620 | gpx_t *gpx) { |
621 | |
622 | printf("removing %s\n", gpx->name); |
623 | |
624 | /* de-chain */ |
625 | gpx_t **prev = &appdata->gpx; |
626 | while(*prev != gpx) prev = &((*prev)->next); |
627 | *prev = gpx->next; |
628 | |
629 | /* remove gconf entry if file was closed */ |
630 | gconf_remove_closed_name(appdata, gpx->filename); |
631 | |
632 | /* free gpx itself */ |
633 | gpx_free(gpx); |
634 | |
635 | /* and remove from store */ |
636 | gtk_list_store_remove(store, iter); |
637 | } |
638 | |
639 | static void gpxlist_close(appdata_t *appdata, |
640 | GtkListStore *store, GtkTreeIter *iter, |
641 | gpx_t *gpx) { |
642 | |
643 | printf("closing %s\n", gpx->name); |
644 | |
645 | g_assert(!gpx->closed); |
646 | gpx->closed = TRUE; |
647 | |
648 | /* free all associated caches */ |
649 | gpx_free_caches(gpx); |
650 | |
651 | /* update entry */ |
652 | gpxlist_set(store, iter, gpx); |
653 | |
654 | /* save name in gconf so we know this has been closed */ |
655 | gconf_save_closed_name(appdata, gpx->filename, gpx->name); |
656 | } |
657 | |
658 | static void gpxlist_view_onRowActivated(GtkTreeView *treeview, |
659 | GtkTreePath *path, |
660 | GtkTreeViewColumn *col, |
661 | gpointer userdata) { |
662 | appdata_t *appdata = (appdata_t*)userdata; |
663 | GtkTreeIter iter; |
664 | GtkTreeModel *model = gtk_tree_view_get_model(treeview); |
665 | |
666 | if (gtk_tree_model_get_iter(model, &iter, path)) { |
667 | gpx_t *gpx; |
668 | gtk_tree_model_get(model, &iter, GPXLIST_COL_DATA, &gpx, -1); |
669 | |
670 | #ifdef USE_PANNABLE_AREA |
671 | /* get name of column the user clicked on */ |
672 | const char *col_name = NULL; |
673 | if(col) col_name = gtk_tree_view_column_get_title(col); |
674 | |
675 | if(col_name && !strcmp(col_name, "Del")) { |
676 | printf("clicked delete\n"); |
677 | |
678 | /* ask user what he wants */ |
679 | GtkWidget *dialog = gtk_message_dialog_new( |
680 | GTK_WINDOW(appdata->window), |
681 | GTK_DIALOG_DESTROY_WITH_PARENT, |
682 | GTK_MESSAGE_QUESTION, GTK_BUTTONS_CANCEL, |
683 | _("Do you want to close this entry only or do " |
684 | "you want to remove it completely from the list?")); |
685 | |
686 | gtk_dialog_add_buttons(GTK_DIALOG(dialog), |
687 | _("Remove"), 1, |
688 | _("Close"), 2, |
689 | NULL); |
690 | |
691 | if(gpx->closed) |
692 | gtk_dialog_set_response_sensitive(GTK_DIALOG(dialog), 2, FALSE); |
693 | |
694 | gtk_window_set_title(GTK_WINDOW(dialog), _("Remove entry?")); |
695 | |
696 | /* set the active flag again if the user answered "no" */ |
697 | switch(gtk_dialog_run(GTK_DIALOG(dialog))) { |
698 | |
699 | case 1: |
700 | gpxlist_remove(appdata, GTK_LIST_STORE(model), &iter, gpx); |
701 | break; |
702 | |
703 | case 2: |
704 | gpxlist_close(appdata, GTK_LIST_STORE(model), &iter, gpx); |
705 | break; |
706 | |
707 | default: |
708 | break; |
709 | } |
710 | |
711 | gtk_widget_destroy(dialog); |
712 | |
713 | } else |
714 | #endif |
715 | { |
716 | |
717 | /* this gpx file may be closed. Since the user definitely wants */ |
718 | /* to use it, we just open it again */ |
719 | if(gpx->closed) { |
720 | gpx_dialog_t *dialog = |
721 | gpx_busy_dialog_new(GTK_WIDGET(appdata->window)); |
722 | gpx_t *new = NULL; |
723 | |
724 | if(g_file_test(gpx->filename, G_FILE_TEST_IS_DIR)) |
725 | new = gpx_parse_dir(dialog, gpx->filename); |
726 | else |
727 | new = gpx_parse(dialog, gpx->filename); |
728 | |
729 | if(new) { |
730 | gpx_t **prev = &(appdata->gpx); |
731 | while(*prev && *prev != gpx) |
732 | prev = &(*prev)->next; |
733 | |
734 | /* this entry _must_ be in the list */ |
735 | g_assert(*prev); |
736 | |
737 | /* replace gpx entry with the new "open" one */ |
738 | (*prev) = new; |
739 | new->next = gpx->next; |
740 | gpx->next = NULL; |
741 | |
742 | /* free old closed one */ |
743 | gpx_free(gpx); |
744 | |
745 | gpx = new; |
746 | |
747 | /* finally update the visible list */ |
748 | gpxlist_set(appdata->gpxstore, &iter, gpx); |
749 | |
750 | /* and remove gconf entry */ |
751 | gconf_remove_closed_name(appdata, gpx->filename); |
752 | |
753 | #ifndef USE_PANNABLE_AREA |
754 | gtk_widget_set_sensitive(appdata->menu_close, TRUE); |
755 | #endif |
756 | } else { |
757 | printf("unable to reopen file %s\n", gpx->filename); |
758 | return; |
759 | } |
760 | |
761 | gpx_busy_dialog_destroy(dialog); |
762 | } |
763 | #ifndef USE_BREAD_CRUMB_TRAIL |
764 | cachelist_dialog(appdata, gpx); |
765 | #else |
766 | gtk_container_remove(GTK_CONTAINER(appdata->vbox), appdata->cur_view); |
767 | appdata->cur_view = cachelist_create(appdata, gpx, NULL); |
768 | gtk_box_pack_start_defaults(GTK_BOX(appdata->vbox), appdata->cur_view); |
769 | gtk_widget_show_all(appdata->vbox); |
770 | |
771 | crumb_add(appdata, gpx->name, CRUMB_CACHELIST, gpx); |
772 | #endif |
773 | } |
774 | } |
775 | } |
776 | |
777 | #ifndef USE_PANNABLE_AREA |
778 | static gboolean |
779 | view_selection_func(GtkTreeSelection *selection, GtkTreeModel *model, |
780 | GtkTreePath *path, gboolean path_currently_selected, |
781 | gpointer userdata) { |
782 | appdata_t *appdata = (appdata_t*)userdata; |
783 | GtkTreeIter iter; |
784 | |
785 | if(gtk_tree_model_get_iter(model, &iter, path)) { |
786 | gpx_t *gpx; |
787 | gtk_tree_model_get(model, &iter, GPXLIST_COL_DATA, &gpx, -1); |
788 | |
789 | gtk_widget_set_sensitive(appdata->menu_remove, !path_currently_selected); |
790 | |
791 | if(!gpx->closed) |
792 | gtk_widget_set_sensitive(appdata->menu_close, !path_currently_selected); |
793 | } |
794 | |
795 | return TRUE; /* allow selection state to change */ |
796 | } |
797 | #endif |
798 | |
799 | static GtkWidget *gpxlist_create_view_and_model(appdata_t *appdata, |
800 | gpx_t *sel_gpx) { |
801 | gpx_t *gpx = appdata->gpx; |
802 | GtkCellRenderer *renderer; |
803 | |
804 | /* saved displayed items */ |
805 | appdata->cur_items = appdata->gpxlist_items; |
806 | |
807 | #ifndef USE_PANNABLE_AREA |
808 | /* nothing selected yet */ |
809 | gtk_widget_set_sensitive(appdata->menu_remove, FALSE); |
810 | gtk_widget_set_sensitive(appdata->menu_close, FALSE); |
811 | #endif |
812 | |
813 | appdata->gpxview = gtk_tree_view_new (); |
814 | |
815 | GtkTreeSelection *selection = |
816 | gtk_tree_view_get_selection(GTK_TREE_VIEW(appdata->gpxview)); |
817 | #ifndef USE_PANNABLE_AREA |
818 | gtk_tree_selection_set_select_function(selection, view_selection_func, |
819 | appdata, NULL); |
820 | #endif |
821 | |
822 | /* --- "Icon" column --- */ |
823 | renderer = gtk_cell_renderer_pixbuf_new(); |
824 | gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(appdata->gpxview), |
825 | -1, "Icon", renderer, |
826 | "pixbuf", GPXLIST_COL_ICON, |
827 | // "sensitive", GPXLIST_COL_OPEN, |
828 | NULL); |
829 | |
830 | /* --- "FileName" column --- */ |
831 | if(appdata->gpxlist_items & GPXLIST_ITEM_FILENAME) { |
832 | renderer = gtk_cell_renderer_text_new(); |
833 | gtk_tree_view_insert_column_with_attributes( |
834 | GTK_TREE_VIEW(appdata->gpxview), |
835 | -1, "Filename", renderer, |
836 | "text", GPXLIST_COL_FILENAME, |
837 | "sensitive", GPXLIST_COL_OPEN, |
838 | NULL); |
839 | } |
840 | |
841 | /* --- "Name" column --- */ |
842 | renderer = gtk_cell_renderer_text_new(); |
843 | g_object_set(renderer, "ellipsize", PANGO_ELLIPSIZE_END, NULL ); |
844 | |
845 | GtkTreeViewColumn *column = gtk_tree_view_column_new_with_attributes( |
846 | "Name", renderer, |
847 | "text", GPXLIST_COL_NAME, |
848 | "sensitive", GPXLIST_COL_OPEN, |
849 | NULL); |
850 | gtk_tree_view_column_set_expand(column, TRUE); |
851 | gtk_tree_view_insert_column(GTK_TREE_VIEW(appdata->gpxview), column, -1); |
852 | |
853 | /* --- "Date" column --- */ |
854 | if(appdata->gpxlist_items & GPXLIST_ITEM_DATE) { |
855 | renderer = gtk_cell_renderer_text_new(); |
856 | g_object_set(renderer, "xalign", 1.0, NULL ); |
857 | gtk_tree_view_insert_column_with_attributes( |
858 | GTK_TREE_VIEW(appdata->gpxview), |
859 | -1, "Date", renderer, |
860 | "text", GPXLIST_COL_DATE, |
861 | "sensitive", GPXLIST_COL_OPEN, |
862 | NULL); |
863 | } |
864 | |
865 | /* --- "Number of caches" column --- */ |
866 | if(appdata->gpxlist_items & GPXLIST_ITEM_CNUM) { |
867 | renderer = gtk_cell_renderer_text_new(); |
868 | g_object_set(renderer, "xalign", 1.0, NULL ); |
869 | gtk_tree_view_insert_column_with_attributes( |
870 | GTK_TREE_VIEW(appdata->gpxview), |
871 | -1, "#Caches", renderer, |
872 | "text", GPXLIST_COL_CACHES, |
873 | "sensitive", GPXLIST_COL_OPEN, |
874 | NULL); |
875 | } |
876 | |
877 | #ifdef USE_PANNABLE_AREA |
878 | /* --- "Delete" column --- */ |
879 | renderer = gtk_cell_renderer_pixbuf_new(); |
880 | gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(appdata->gpxview), |
881 | -1, "Del", renderer, |
882 | "pixbuf", GPXLIST_COL_DELETE, |
883 | "sensitive", GPXLIST_COL_OPEN, |
884 | NULL); |
885 | #endif |
886 | |
887 | /* build and fill the store */ |
888 | appdata->gpxstore = gtk_list_store_new(GPXLIST_NUM_COLS, GDK_TYPE_PIXBUF, |
889 | G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, |
890 | G_TYPE_STRING, G_TYPE_BOOLEAN, |
891 | #ifdef USE_PANNABLE_AREA |
892 | GDK_TYPE_PIXBUF, |
893 | #endif |
894 | G_TYPE_POINTER); |
895 | |
896 | GtkTreePath *path = NULL; |
897 | GtkTreeIter sel_iter; |
898 | gboolean sel_iter_valid = FALSE; |
899 | while(gpx) { |
900 | GtkTreeIter iter; |
901 | gtk_list_store_append(appdata->gpxstore, &iter); |
902 | gpxlist_set(appdata->gpxstore, &iter, gpx); |
903 | |
904 | if(gpx == sel_gpx) { |
905 | sel_iter = iter; |
906 | sel_iter_valid = TRUE; |
907 | } |
908 | |
909 | gpx = gpx->next; |
910 | } |
911 | |
912 | gtk_tree_view_set_model(GTK_TREE_VIEW(appdata->gpxview), |
913 | GTK_TREE_MODEL(appdata->gpxstore)); |
914 | |
915 | g_object_unref(appdata->gpxstore); |
916 | |
917 | if(sel_iter_valid) { |
918 | gtk_tree_selection_select_iter(selection, &sel_iter); |
919 | path = gtk_tree_model_get_path(GTK_TREE_MODEL(appdata->gpxstore), |
920 | &sel_iter); |
921 | gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(appdata->gpxview), |
922 | path, NULL, TRUE, 0.0, 0.0); |
923 | gtk_tree_path_free(path); |
924 | } |
925 | |
926 | /* make list react on clicks */ |
927 | g_signal_connect(appdata->gpxview, "row-activated", |
928 | (GCallback)gpxlist_view_onRowActivated, appdata); |
929 | |
930 | /* put this inside a scrolled view */ |
931 | #ifndef USE_PANNABLE_AREA |
932 | GtkWidget *scrolled_window = gtk_scrolled_window_new(NULL, NULL); |
933 | gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window), |
934 | GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC); |
935 | gtk_container_add(GTK_CONTAINER(scrolled_window), appdata->gpxview); |
936 | |
937 | return scrolled_window; |
938 | #else |
939 | GtkWidget *pannable_area = hildon_pannable_area_new(); |
940 | gtk_container_add(GTK_CONTAINER(pannable_area), appdata->gpxview); |
941 | |
942 | return pannable_area; |
943 | #endif |
944 | } |
945 | |
946 | /* add last entry in gpx list to visual representation */ |
947 | static void gpxlist_add(appdata_t *appdata, gpx_t *new) { |
948 | GtkTreeIter iter; |
949 | |
950 | gtk_list_store_append(appdata->gpxstore, &iter); |
951 | gpxlist_set(appdata->gpxstore, &iter, new); |
952 | |
953 | /* and attach entry to end of list */ |
954 | gpx_t **gpx = &appdata->gpx; |
955 | while(*gpx) gpx = &((*gpx)->next); |
956 | *gpx = new; |
957 | } |
958 | |
959 | /******************** end of gpxlist ********************/ |
960 | |
961 | /******************** begin of menu *********************/ |
962 | |
963 | static void |
964 | cb_menu_about(GtkWidget *window, gpointer data) { |
965 | GtkAboutDialog *about = GTK_ABOUT_DIALOG(gtk_about_dialog_new()); |
966 | |
967 | gtk_about_dialog_set_name(about, "GPXView"); |
968 | gtk_about_dialog_set_version(about, VERSION); |
969 | gtk_about_dialog_set_copyright(about, |
970 | _("GPXView (c) 2008 by\n" |
971 | "Till Harbaum <till@harbaum.org>\n" |
972 | "Geocaching.com: Tantil")); |
973 | |
974 | gtk_about_dialog_set_website(about, |
975 | _("http://www.harbaum.org/till/maemo")); |
976 | |
977 | gtk_about_dialog_set_comments(about, |
978 | _("Geocaching.com GPX file viewer")); |
979 | |
980 | gtk_widget_show_all(GTK_WIDGET(about)); |
981 | gtk_dialog_run(GTK_DIALOG(about)); |
982 | gtk_widget_destroy(GTK_WIDGET(about)); |
983 | } |
984 | |
985 | #if defined(USE_MAEMO) && defined(HILDON_HELP) |
986 | static void |
987 | cb_menu_help(GtkWidget *window, gpointer data) { |
988 | appdata_t *appdata = (appdata_t*)data; |
989 | |
990 | hildon_help_show(appdata->osso_context, HELP_ID_INTRO, 0); |
991 | } |
992 | #endif |
993 | |
994 | static void |
995 | cb_menu_add(GtkWidget *window, gpointer data) { |
996 | appdata_t *appdata = (appdata_t *)data; |
997 | |
998 | gpx_t *new = choose_file(appdata, FALSE); |
999 | if(new) gpxlist_add(appdata, new); |
1000 | } |
1001 | |
1002 | static void |
1003 | cb_menu_adddir(GtkWidget *window, gpointer data) { |
1004 | appdata_t *appdata = (appdata_t *)data; |
1005 | |
1006 | gpx_t *new = choose_file(appdata, TRUE); |
1007 | if(new) gpxlist_add(appdata, new); |
1008 | } |
1009 | |
1010 | static void |
1011 | cb_menu_geomath(GtkWidget *window, gpointer data) { |
1012 | geomath_dialog((appdata_t *)data); |
1013 | } |
1014 | |
1015 | static void |
1016 | cb_menu_geotext(GtkWidget *window, gpointer data) { |
1017 | geotext_dialog((appdata_t *)data); |
1018 | } |
1019 | |
1020 | static void |
1021 | cb_menu_precpos(GtkWidget *window, gpointer data) { |
1022 | precise_position((appdata_t *)data); |
1023 | } |
1024 | |
1025 | #ifndef USE_PANNABLE_AREA |
1026 | static void |
1027 | cb_menu_close(GtkWidget *window, gpointer data) { |
1028 | appdata_t *appdata = (appdata_t *)data; |
1029 | GtkTreeSelection *selection; |
1030 | GtkTreeModel *model; |
1031 | GtkTreeIter iter; |
1032 | |
1033 | printf("selected close\n"); |
1034 | |
1035 | /* the entry cannot be closed again */ |
1036 | gtk_widget_set_sensitive(appdata->menu_close, FALSE); |
1037 | |
1038 | selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(appdata->gpxview)); |
1039 | |
1040 | printf("gpxlist close\n"); |
1041 | |
1042 | if (gtk_tree_selection_get_selected(selection, &model, &iter)) { |
1043 | gpx_t *gpx = NULL; |
1044 | gtk_tree_model_get(model, &iter, GPXLIST_COL_DATA, &gpx, -1); |
1045 | |
1046 | if(gpx) gpxlist_close(appdata, GTK_LIST_STORE(model), &iter, gpx); |
1047 | } else { |
1048 | g_print ("no row selected.\n"); |
1049 | } |
1050 | } |
1051 | |
1052 | static void |
1053 | cb_menu_remove(GtkWidget *window, gpointer data) { |
1054 | appdata_t *appdata = (appdata_t *)data; |
1055 | |
1056 | /* disable menu item */ |
1057 | gtk_widget_set_sensitive(appdata->menu_remove, FALSE); |
1058 | gtk_widget_set_sensitive(appdata->menu_close, FALSE); |
1059 | |
1060 | GtkTreeModel *model; |
1061 | GtkTreeIter iter; |
1062 | GtkTreeSelection *selection = |
1063 | gtk_tree_view_get_selection(GTK_TREE_VIEW(appdata->gpxview)); |
1064 | |
1065 | printf("gpxlist remove\n"); |
1066 | |
1067 | if(gtk_tree_selection_get_selected(selection, &model, &iter)) { |
1068 | gpx_t *gpx = NULL; |
1069 | gtk_tree_model_get(model, &iter, GPXLIST_COL_DATA, &gpx, -1); |
1070 | |
1071 | if(gpx) gpxlist_remove(appdata, GTK_LIST_STORE(model), &iter, gpx); |
1072 | } else { |
1073 | g_print ("no row selected.\n"); |
1074 | } |
1075 | } |
1076 | |
1077 | #endif // !USE_PANNABLE_AREA |
1078 | |
1079 | static void search_result_free(gpx_t *result) { |
1080 | printf("freeing search results\n"); |
1081 | |
1082 | /* free found chain */ |
1083 | cache_t *cache = result->cache; |
1084 | while(cache) { |
1085 | cache_t *next = cache->next; |
1086 | free(cache); |
1087 | cache = next; |
1088 | } |
1089 | free(result->name); |
1090 | free(result); |
1091 | } |
1092 | |
1093 | #define MAX_HITS 50 |
1094 | |
1095 | static time_t localize_time(time_t in) { |
1096 | time_t ret; |
1097 | char *tz; |
1098 | struct tm *tm = localtime(&in); |
1099 | |
1100 | tz = getenv("TZ"); |
1101 | setenv("TZ", "", 1); |
1102 | tzset(); |
1103 | ret = mktime(tm); |
1104 | if (tz) |
1105 | setenv("TZ", tz, 1); |
1106 | else |
1107 | unsetenv("TZ"); |
1108 | tzset(); |
1109 | return ret; |
1110 | } |
1111 | |
1112 | static int days_ago(time_t in) { |
1113 | int day_in = localize_time(in) / (60*60*24); |
1114 | int day_now = localize_time(time(NULL)) / (60*60*24); |
1115 | |
1116 | return day_now - day_in; |
1117 | } |
1118 | |
1119 | gpx_t *search_do(appdata_t *appdata, gpx_t *gpx, char *phrase, |
1120 | int what, gboolean local) { |
1121 | /* walk through all caches */ |
1122 | |
1123 | int hits = 0; |
1124 | gpx_t *found = malloc(sizeof(gpx_t)); |
1125 | memset(found, 0, sizeof(gpx_t)); |
1126 | cache_t **cacheP = &(found->cache); |
1127 | |
1128 | if(what & SEARCH_FINDS) { |
1129 | time_t loc_now = localize_time(time(NULL)); |
1130 | printf("now: %ld days since 1/1/1970, days hour is %ld\n", |
1131 | loc_now/(60*60*24), loc_now%(60*60*24)/(60*60)); |
1132 | } |
1133 | |
1134 | while(gpx && hits < MAX_HITS) { |
1135 | |
1136 | /* we need all notes ... */ |
1137 | if(what & SEARCH_FINDS) { |
1138 | notes_load_all(appdata, gpx); |
1139 | gpx->notes_loaded = TRUE; |
1140 | } |
1141 | |
1142 | cache_t *cache = gpx->cache; |
1143 | |
1144 | while(cache && hits < MAX_HITS) { |
1145 | gboolean hit = FALSE; |
1146 | |
1147 | if(what & SEARCH_FINDS) { |
1148 | if(cache->notes && cache->notes->found ) { |
1149 | int days = days_ago(cache->notes->ftime); |
1150 | |
1151 | if(cache->id) |
1152 | printf("find of %s is %d days ago\n", cache->id, days); |
1153 | |
1154 | if(days <= appdata->search_days) |
1155 | hit = 1; |
1156 | } |
1157 | } else if(cache->id && (what & SEARCH_ID) && |
1158 | strcasestr(cache->id, phrase)) |
1159 | hit = 1; |
1160 | else if(cache->name && (what & SEARCH_NAME) && |
1161 | strcasestr(cache->name, phrase)) |
1162 | hit = 1; |
1163 | else if(cache->short_description && (what & SEARCH_DESC) && |
1164 | strcasestr(cache->short_description, phrase)) |
1165 | hit = 1; |
1166 | else if(cache->long_description && (what & SEARCH_DESC) && |
1167 | strcasestr(cache->long_description, phrase)) |
1168 | hit = 1; |
1169 | else if(cache->owner && (what & SEARCH_OWNER) && |
1170 | strcasestr(cache->owner, phrase)) |
1171 | hit = 1; |
1172 | |
1173 | if(hit) { |
1174 | /* chain a copy of this cache structure into the found list */ |
1175 | *cacheP = malloc(sizeof(cache_t)); |
1176 | memcpy(*cacheP, cache, sizeof(cache_t)); |
1177 | (*cacheP)->next = NULL; |
1178 | cacheP = &((*cacheP)->next); |
1179 | hits++; |
1180 | } |
1181 | cache = cache->next; |
1182 | } |
1183 | |
1184 | if(!local) gpx = gpx->next; |
1185 | else gpx = NULL; /* local search within one gpx only */ |
1186 | } |
1187 | |
1188 | found->name = strdup(_("Search results")); |
1189 | |
1190 | return found; |
1191 | } |
1192 | |
1193 | typedef struct { |
1194 | appdata_t *appdata; |
1195 | GtkWidget *entry, *spinner; |
1196 | GtkWidget *in_id, *in_name, *in_desc, *in_owner, *in_finds; |
1197 | } search_context_t; |
1198 | |
1199 | static void callback_finds_toggled(GtkWidget *widget, gpointer data ) { |
1200 | search_context_t *context = (search_context_t*)data; |
1201 | |
1202 | gboolean in_finds = gtk_toggle_button_get_active( |
1203 | GTK_TOGGLE_BUTTON(context->in_finds)); |
1204 | |
1205 | gtk_widget_set_sensitive(context->entry, !in_finds); |
1206 | gtk_widget_set_sensitive(context->in_id, !in_finds); |
1207 | gtk_widget_set_sensitive(context->in_name, !in_finds); |
1208 | gtk_widget_set_sensitive(context->in_desc, !in_finds); |
1209 | gtk_widget_set_sensitive(context->in_owner, !in_finds); |
1210 | gtk_widget_set_sensitive(context->spinner, in_finds); |
1211 | } |
1212 | |
1213 | static void |
1214 | cb_menu_search(GtkWidget *window, gpointer data) { |
1215 | appdata_t *appdata = (appdata_t *)data; |
1216 | |
1217 | search_context_t context; |
1218 | memset(&context, 0, sizeof(search_context_t)); |
1219 | context.appdata = appdata; |
1220 | |
1221 | GtkWidget *dialog = gtk_dialog_new_with_buttons(_("Enter search phrase"), |
1222 | GTK_WINDOW(appdata->window), GTK_DIALOG_MODAL, |
1223 | GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, |
1224 | GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT, |
1225 | NULL); |
1226 | |
1227 | gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(dialog)->vbox), |
1228 | gtk_label_new(_("Search in:"))); |
1229 | |
1230 | GtkWidget *table = gtk_table_new(2, 2, TRUE); |
1231 | gtk_table_set_col_spacing(GTK_TABLE(table), 0, 8); |
1232 | |
1233 | context.in_id = gtk_check_button_new_with_label(_("Waypoint IDs")); |
1234 | gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(context.in_id), |
1235 | appdata->search & SEARCH_ID); |
1236 | gtk_table_attach_defaults(GTK_TABLE(table), context.in_id, 0, 1, 0, 1); |
1237 | |
1238 | context.in_name = gtk_check_button_new_with_label(_("Names")); |
1239 | gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(context.in_name), |
1240 | appdata->search & SEARCH_NAME); |
1241 | gtk_table_attach_defaults(GTK_TABLE(table), context.in_name, 1, 2, 0, 1); |
1242 | |
1243 | context.in_desc = gtk_check_button_new_with_label(_("Descriptions")); |
1244 | gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(context.in_desc), |
1245 | appdata->search & SEARCH_DESC); |
1246 | gtk_table_attach_defaults(GTK_TABLE(table), context.in_desc, 0, 1, 1, 2); |
1247 | |
1248 | context.in_owner = gtk_check_button_new_with_label(_("Owner")); |
1249 | gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(context.in_owner), |
1250 | appdata->search & SEARCH_OWNER); |
1251 | gtk_table_attach_defaults(GTK_TABLE(table), context.in_owner, 1, 2, 1, 2); |
1252 | |
1253 | gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(dialog)->vbox), table); |
1254 | |
1255 | /* -------------------------------------------------------------- */ |
1256 | |
1257 | gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(dialog)->vbox), |
1258 | gtk_label_new(_("Search for:"))); |
1259 | gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(dialog)->vbox), |
1260 | context.entry = gtk_entry_new()); |
1261 | if(appdata->search_str) |
1262 | gtk_entry_set_text(GTK_ENTRY(context.entry), appdata->search_str); |
1263 | |
1264 | /* -------------------------------------------------------------- */ |
1265 | |
1266 | gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(dialog)->vbox), |
1267 | gtk_hseparator_new()); |
1268 | |
1269 | GtkWidget *hbox = gtk_hbox_new(FALSE, 5); |
1270 | |
1271 | context.in_finds = gtk_check_button_new_with_label(_("Search finds for")); |
1272 | gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(context.in_finds), |
1273 | appdata->search & SEARCH_FINDS); |
1274 | gtk_box_pack_start_defaults(GTK_BOX(hbox), context.in_finds); |
1275 | g_signal_connect(G_OBJECT(context.in_finds), "toggled", |
1276 | G_CALLBACK(callback_finds_toggled), &context); |
1277 | |
1278 | #ifndef USE_MAEMO |
1279 | GtkObject *adj = gtk_adjustment_new(appdata->search_days, 0, 99, 1, 10, 10); |
1280 | context.spinner = gtk_spin_button_new(GTK_ADJUSTMENT(adj), 1, 0); |
1281 | #else |
1282 | context.spinner = hildon_number_editor_new(0, 99); |
1283 | hildon_number_editor_set_value(HILDON_NUMBER_EDITOR(context.spinner), |
1284 | appdata->search_days); |
1285 | #endif |
1286 | gtk_box_pack_start_defaults(GTK_BOX(hbox), context.spinner); |
1287 | |
1288 | gtk_box_pack_start_defaults(GTK_BOX(hbox), gtk_label_new(_("days"))); |
1289 | |
1290 | gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(dialog)->vbox), hbox); |
1291 | |
1292 | /* -------------------------------------------------------------- */ |
1293 | |
1294 | gtk_widget_show_all(dialog); |
1295 | callback_finds_toggled(NULL, &context); |
1296 | |
1297 | if(GTK_RESPONSE_ACCEPT == gtk_dialog_run(GTK_DIALOG(dialog))) { |
1298 | char *p = strdup(gtk_entry_get_text(GTK_ENTRY(context.entry))); |
1299 | |
1300 | /* update saved search string */ |
1301 | if(appdata->search_str) free(appdata->search_str); |
1302 | if(strlen(p) > 0) |
1303 | appdata->search_str = strdup(p); |
1304 | |
1305 | #ifndef USE_MAEMO |
1306 | appdata->search_days = gtk_spin_button_get_value_as_int( |
1307 | GTK_SPIN_BUTTON(context.spinner)); |
1308 | #else |
1309 | appdata->search_days = hildon_number_editor_get_value( |
1310 | HILDON_NUMBER_EDITOR(context.spinner)); |
1311 | #endif |
1312 | |
1313 | if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(context.in_finds))) |
1314 | appdata->search |= SEARCH_FINDS; |
1315 | else |
1316 | appdata->search &= ~SEARCH_FINDS; |
1317 | |
1318 | if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(context.in_id))) |
1319 | appdata->search |= SEARCH_ID; |
1320 | else |
1321 | appdata->search &= ~SEARCH_ID; |
1322 | |
1323 | if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(context.in_name))) |
1324 | appdata->search |= SEARCH_NAME; |
1325 | else |
1326 | appdata->search &= ~SEARCH_NAME; |
1327 | |
1328 | if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(context.in_desc))) |
1329 | appdata->search |= SEARCH_DESC; |
1330 | else |
1331 | appdata->search &= ~SEARCH_DESC; |
1332 | |
1333 | if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(context.in_owner))) |
1334 | appdata->search |= SEARCH_OWNER; |
1335 | else |
1336 | appdata->search &= ~SEARCH_OWNER; |
1337 | |
1338 | gtk_widget_destroy(dialog); |
1339 | |
1340 | /* don't search if we are asked to search for nothing */ |
1341 | if(((appdata->search & (SEARCH_ID|SEARCH_NAME|SEARCH_DESC|SEARCH_OWNER)) && |
1342 | strlen(p) > 0) || (appdata->search & SEARCH_FINDS)) { |
1343 | gpx_t *found = NULL; |
1344 | |
1345 | printf("Search for %s (flags = %x)...\n", p, appdata->search); |
1346 | |
1347 | #ifndef USE_BREAD_CRUMB_TRAIL |
1348 | found = search_do(appdata, appdata->gpx, p, appdata->search, FALSE); |
1349 | |
1350 | /* do search result dialog here ... */ |
1351 | cachelist_dialog(appdata, found); |
1352 | search_result_free(found); |
1353 | #else |
1354 | if(appdata->cur_gpx) |
1355 | found = search_do(appdata, appdata->cur_gpx, p, appdata->search, TRUE); |
1356 | else |
1357 | found = search_do(appdata, appdata->gpx, p, appdata->search, FALSE); |
1358 | |
1359 | gtk_container_remove(GTK_CONTAINER(appdata->vbox), appdata->cur_view); |
1360 | appdata->cur_view = cachelist_create(appdata, found, NULL); |
1361 | gtk_box_pack_start_defaults(GTK_BOX(appdata->vbox), appdata->cur_view); |
1362 | gtk_widget_show_all(appdata->vbox); |
1363 | crumb_add(appdata, found->name, |
1364 | appdata->cur_gpx?CRUMB_SEARCH_GPX:CRUMB_SEARCH_GLOBAL, found); |
1365 | #endif |
1366 | } else |
1367 | printf("No valid search: \"%s\" with flags %x!\n", p, appdata->search); |
1368 | |
1369 | free(p); |
1370 | } else |
1371 | gtk_widget_destroy(dialog); |
1372 | } |
1373 | |
1374 | void on_window_destroy (GtkWidget *widget, gpointer data); |
1375 | |
1376 | #ifndef USE_MAEMO |
1377 | static void |
1378 | cb_menu_quit(GtkWidget *window, gpointer data) { |
1379 | on_window_destroy(window, data); |
1380 | } |
1381 | #endif |
1382 | |
1383 | #ifndef NO_COPY_N_PASTE |
1384 | static void |
1385 | cb_menu_cut(GtkWidget *widget, gpointer data) { |
1386 | appdata_t *appdata = (appdata_t*)data; |
1387 | |
1388 | if(appdata->active_buffer) { |
1389 | if(GTK_WIDGET_TYPE(appdata->active_buffer) == GTK_TYPE_TEXT_BUFFER) { |
1390 | gtk_text_buffer_cut_clipboard(GTK_TEXT_BUFFER(appdata->active_buffer), |
1391 | appdata->clipboard, TRUE); |
1392 | } else |
1393 | printf("cut: ERROR, not a text buffer\n"); |
1394 | } else |
1395 | printf("cut: ERROR, no active buffer\n"); |
1396 | } |
1397 | |
1398 | static void |
1399 | cb_menu_copy(GtkWidget *widget, gpointer data) { |
1400 | appdata_t *appdata = (appdata_t*)data; |
1401 | |
1402 | if(appdata->active_buffer) { |
1403 | if(GTK_WIDGET_TYPE(appdata->active_buffer) == GTK_TYPE_TEXT_BUFFER) { |
1404 | gtk_text_buffer_copy_clipboard(GTK_TEXT_BUFFER(appdata->active_buffer), |
1405 | appdata->clipboard); |
1406 | } else if(GTK_WIDGET_TYPE(appdata->active_buffer) == gtk_html_get_type()) { |
1407 | printf("copy from html buffer\n"); |
1408 | html_copy_to_clipboard(appdata); |
1409 | } else |
1410 | printf("copy: ERROR, not a text nor a html buffer\n"); |
1411 | } else |
1412 | printf("copy: ERROR, no active buffer\n"); |
1413 | } |
1414 | |
1415 | static void |
1416 | cb_menu_paste(GtkWidget *widget, gpointer data) { |
1417 | appdata_t *appdata = (appdata_t*)data; |
1418 | |
1419 | if(appdata->active_buffer) { |
1420 | if(GTK_WIDGET_TYPE(appdata->active_buffer) == GTK_TYPE_TEXT_BUFFER) { |
1421 | gtk_text_buffer_paste_clipboard(GTK_TEXT_BUFFER(appdata->active_buffer), |
1422 | appdata->clipboard, NULL, TRUE); |
1423 | } else |
1424 | printf("paste: ERROR, not a text buffer\n"); |
1425 | } else |
1426 | printf("paste: ERROR, no active buffer\n"); |
1427 | } |
1428 | #endif |
1429 | |
1430 | static void |
1431 | cb_menu_export_log(GtkWidget *widget, gpointer data) { |
1432 | appdata_t *appdata = (appdata_t*)data; |
1433 | notes_log_export(appdata); |
1434 | } |
1435 | |
1436 | #ifdef USE_MAEMO |
1437 | static void |
1438 | cb_menu_export_mmpoi(GtkWidget *widget, gpointer data) { |
1439 | appdata_t *appdata = (appdata_t*)data; |
1440 | mmpoi_export(appdata); |
1441 | } |
1442 | #endif |
1443 | |
1444 | static void |
1445 | cb_menu_export_garmin(GtkWidget *widget, gpointer data) { |
1446 | appdata_t *appdata = (appdata_t*)data; |
1447 | garmin_export(appdata); |
1448 | } |
1449 | |
1450 | void menu_create(appdata_t *appdata) { |
1451 | GtkWidget *menu, *item; |
1452 | menu = gtk_menu_new(); |
1453 | |
1454 | #ifdef USE_MAEMO |
1455 | appdata->menu_import = |
1456 | #endif |
1457 | item = gtk_menu_item_new_with_label(_("Import")); |
1458 | gtk_menu_append(GTK_MENU_SHELL(menu), item); |
1459 | GtkWidget *submenu = gtk_menu_new(); |
1460 | gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu); |
1461 | |
1462 | item = gtk_menu_item_new_with_label( _("File...") ); |
1463 | gtk_menu_append(GTK_MENU_SHELL(submenu), item); |
1464 | g_signal_connect(item, "activate", GTK_SIGNAL_FUNC(cb_menu_add), appdata); |
1465 | |
1466 | item = gtk_menu_item_new_with_label( _("Directory...") ); |
1467 | gtk_menu_append(GTK_MENU_SHELL(submenu), item); |
1468 | g_signal_connect(item, "activate", GTK_SIGNAL_FUNC(cb_menu_adddir), appdata); |
1469 | |
1470 | #ifndef USE_PANNABLE_AREA |
1471 | gtk_menu_append(GTK_MENU_SHELL(submenu), gtk_separator_menu_item_new()); |
1472 | |
1473 | appdata->menu_close = |
1474 | item = gtk_menu_item_new_with_label( _("Close") ); |
1475 | gtk_menu_append(GTK_MENU_SHELL(submenu), item); |
1476 | g_signal_connect(item, "activate", GTK_SIGNAL_FUNC(cb_menu_close), appdata); |
1477 | |
1478 | appdata->menu_remove = |
1479 | item = gtk_menu_item_new_with_label( _("Remove") ); |
1480 | gtk_menu_append(GTK_MENU_SHELL(submenu), item); |
1481 | g_signal_connect(item, "activate", GTK_SIGNAL_FUNC(cb_menu_remove), appdata); |
1482 | #endif |
1483 | |
1484 | #ifdef USE_MAEMO |
1485 | appdata->menu_export = |
1486 | #endif |
1487 | item = gtk_menu_item_new_with_label(_("Export")); |
1488 | gtk_menu_append(GTK_MENU_SHELL(menu), item); |
1489 | submenu = gtk_menu_new(); |
1490 | gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu); |
1491 | |
1492 | #ifdef USE_MAEMO |
1493 | item = gtk_menu_item_new_with_label( _("Maemo Mapper POI...") ); |
1494 | gtk_menu_append(GTK_MENU_SHELL(submenu), item); |
1495 | g_signal_connect(item, "activate", |
1496 | GTK_SIGNAL_FUNC(cb_menu_export_mmpoi), appdata); |
1497 | #endif |
1498 | |
1499 | item = gtk_menu_item_new_with_label( _("Garmin Field Notes...") ); |
1500 | gtk_menu_append(GTK_MENU_SHELL(submenu), item); |
1501 | g_signal_connect(item, "activate", |
1502 | GTK_SIGNAL_FUNC(cb_menu_export_log), appdata); |
1503 | |
1504 | item = gtk_menu_item_new_with_label( _("Garmin GPX...") ); |
1505 | gtk_menu_append(GTK_MENU_SHELL(submenu), item); |
1506 | g_signal_connect(item, "activate", |
1507 | GTK_SIGNAL_FUNC(cb_menu_export_garmin), appdata); |
1508 | |
1509 | #ifdef USE_MAEMO |
1510 | appdata->menu_search = |
1511 | #endif |
1512 | item = gtk_menu_item_new_with_label( _("Search...") ); |
1513 | gtk_menu_append(GTK_MENU_SHELL(menu), item); |
1514 | g_signal_connect(item, "activate", GTK_SIGNAL_FUNC(cb_menu_search), appdata); |
1515 | |
1516 | gtk_menu_append(GTK_MENU_SHELL(menu), gtk_separator_menu_item_new()); |
1517 | |
1518 | #ifndef NO_COPY_N_PASTE |
1519 | /* ----------- copy'n paste submenu ----------------- */ |
1520 | appdata->clipboard = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD); |
1521 | gtk_clipboard_set_can_store(appdata->clipboard, NULL, 0); |
1522 | |
1523 | item = gtk_menu_item_new_with_label(_("Edit")); |
1524 | gtk_menu_append(GTK_MENU_SHELL(menu), item); |
1525 | submenu = gtk_menu_new(); |
1526 | gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu); |
1527 | |
1528 | appdata->menu_cut = item = gtk_menu_item_new_with_label( _("Cut") ); |
1529 | gtk_widget_set_sensitive(item, FALSE); |
1530 | gtk_menu_append(GTK_MENU_SHELL(submenu), item); |
1531 | g_signal_connect(item, "activate", GTK_SIGNAL_FUNC(cb_menu_cut), appdata); |
1532 | appdata->menu_copy = item = gtk_menu_item_new_with_label( _("Copy") ); |
1533 | gtk_widget_set_sensitive(item, FALSE); |
1534 | gtk_menu_append(GTK_MENU_SHELL(submenu), item); |
1535 | g_signal_connect(item, "activate", GTK_SIGNAL_FUNC(cb_menu_copy), appdata); |
1536 | appdata->menu_paste = item = gtk_menu_item_new_with_label( _("Paste") ); |
1537 | gtk_widget_set_sensitive(item, FALSE); |
1538 | gtk_menu_append(GTK_MENU_SHELL(submenu), item); |
1539 | g_signal_connect(item, "activate", GTK_SIGNAL_FUNC(cb_menu_paste), appdata); |
1540 | #endif |
1541 | |
1542 | item = gtk_menu_item_new_with_label( _("Settings...") ); |
1543 | gtk_menu_append(GTK_MENU_SHELL(menu), item); |
1544 | g_signal_connect(item, "activate", GTK_SIGNAL_FUNC(cb_menu_settings), |
1545 | appdata); |
1546 | |
1547 | gtk_menu_append(GTK_MENU_SHELL(menu), gtk_separator_menu_item_new()); |
1548 | |
1549 | item = gtk_menu_item_new_with_label(_("Tools")); |
1550 | gtk_menu_append(GTK_MENU_SHELL(menu), item); |
1551 | submenu = gtk_menu_new(); |
1552 | gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu); |
1553 | |
1554 | item = gtk_menu_item_new_with_label( _("Geomath...") ); |
1555 | gtk_menu_append(GTK_MENU_SHELL(submenu), item); |
1556 | g_signal_connect(item, "activate", |
1557 | GTK_SIGNAL_FUNC(cb_menu_geomath), appdata); |
1558 | |
1559 | item = gtk_menu_item_new_with_label( _("Geotext...") ); |
1560 | gtk_menu_append(GTK_MENU_SHELL(submenu), item); |
1561 | g_signal_connect(item, "activate", |
1562 | GTK_SIGNAL_FUNC(cb_menu_geotext), appdata); |
1563 | |
1564 | item = gtk_menu_item_new_with_label( _("Precise Position...") ); |
1565 | gtk_menu_append(GTK_MENU_SHELL(submenu), item); |
1566 | g_signal_connect(item, "activate", |
1567 | GTK_SIGNAL_FUNC(cb_menu_precpos), appdata); |
1568 | |
1569 | gtk_menu_append(GTK_MENU_SHELL(menu), gtk_separator_menu_item_new()); |
1570 | |
1571 | #if defined(USE_MAEMO) && defined(HILDON_HELP) |
1572 | item = gtk_menu_item_new_with_label( _("Help...") ); |
1573 | gtk_menu_append(GTK_MENU_SHELL(menu), item); |
1574 | g_signal_connect(item, "activate", GTK_SIGNAL_FUNC(cb_menu_help), appdata); |
1575 | #endif |
1576 | |
1577 | item = gtk_menu_item_new_with_label( _("About...") ); |
1578 | gtk_menu_append(GTK_MENU_SHELL(menu), item); |
1579 | g_signal_connect(item, "activate", GTK_SIGNAL_FUNC(cb_menu_about), appdata); |
1580 | |
1581 | #ifndef USE_MAEMO |
1582 | item = gtk_menu_item_new_with_label( _("Quit") ); |
1583 | gtk_menu_append(GTK_MENU_SHELL(menu), item); |
1584 | g_signal_connect(item, "activate", GTK_SIGNAL_FUNC(cb_menu_quit), appdata); |
1585 | #endif |
1586 | |
1587 | #ifdef USE_MAEMO |
1588 | hildon_window_set_menu(appdata->window, GTK_MENU(menu)); |
1589 | #else |
1590 | /* attach ordinary gtk menu */ |
1591 | GtkWidget *menu_bar = gtk_menu_bar_new(); |
1592 | |
1593 | GtkWidget *root_menu = gtk_menu_item_new_with_label (_("Menu")); |
1594 | gtk_widget_show(root_menu); |
1595 | |
1596 | gtk_menu_bar_append(menu_bar, root_menu); |
1597 | gtk_menu_item_set_submenu(GTK_MENU_ITEM (root_menu), menu); |
1598 | |
1599 | gtk_widget_show(menu_bar); |
1600 | gtk_box_pack_start(GTK_BOX(appdata->vbox), menu_bar, 0, 0, 0); |
1601 | #endif |
1602 | } |
1603 | |
1604 | /********************* end of menu **********************/ |
1605 | |
1606 | void cleanup(appdata_t *appdata) { |
1607 | gpx_free_all(appdata->gpx); |
1608 | if(appdata->path) free(appdata->path); |
1609 | if(appdata->image_path) free(appdata->image_path); |
1610 | if(appdata->search_str) free(appdata->search_str); |
1611 | |
1612 | gnome_vfs_shutdown(); |
1613 | icons_free(); |
1614 | gps_release(appdata); |
1615 | |
1616 | #ifdef USE_MAEMO |
1617 | if(appdata->search_results) { |
1618 | printf("freeing pending search\n"); |
1619 | search_result_free(appdata->search_results); |
1620 | } |
1621 | |
1622 | if(appdata->osso_context) |
1623 | osso_deinitialize(appdata->osso_context); |
1624 | |
1625 | appdata->program = NULL; |
1626 | #endif |
1627 | |
1628 | /* free chain of locations */ |
1629 | location_t *loc = appdata->location; |
1630 | while(loc) { |
1631 | location_t *next = loc->next; |
1632 | if(loc->name) free(loc->name); |
1633 | free(loc); |
1634 | loc = next; |
1635 | } |
1636 | |
1637 | puts("everything is gone"); |
1638 | } |
1639 | |
1640 | void on_window_destroy (GtkWidget *widget, gpointer data) { |
1641 | appdata_t *appdata = (appdata_t*)data; |
1642 | |
1643 | gconf_save_state(appdata); |
1644 | gtk_main_quit(); |
1645 | appdata->window = NULL; |
1646 | } |
1647 | |
1648 | gboolean on_window_key_press(GtkWidget *widget, |
1649 | GdkEventKey *event, gpointer data) { |
1650 | appdata_t *appdata = (appdata_t*)data; |
1651 | int handled = FALSE; |
1652 | |
1653 | // printf("key event %d\n", event->keyval); |
1654 | |
1655 | switch(event->keyval) { |
1656 | #ifdef USE_MAEMO |
1657 | |
1658 | #if 0 /* don't do this as it interferes with gtkentry fields */ |
1659 | case HILDON_HARDKEY_SELECT: |
1660 | /* remove as long as we haven't reached the main gpx list */ |
1661 | if(appdata->cur_gpx) |
1662 | hildon_bread_crumb_trail_pop(HILDON_BREAD_CRUMB_TRAIL(appdata->bct)); |
1663 | handled = TRUE; |
1664 | break; |
1665 | #endif |
1666 | |
1667 | #ifdef HILDON_HARDKEY_INCREASE |
1668 | case HILDON_HARDKEY_INCREASE: |
1669 | html_zoom(appdata, TRUE); |
1670 | handled = TRUE; |
1671 | break; |
1672 | #endif |
1673 | |
1674 | #ifdef HILDON_HARDKEY_DECREASE |
1675 | case HILDON_HARDKEY_DECREASE: |
1676 | html_zoom(appdata, FALSE); |
1677 | handled = TRUE; |
1678 | break; |
1679 | #endif |
1680 | |
1681 | #ifdef HILDON_HARDKEY_FULLSCREEN |
1682 | case HILDON_HARDKEY_FULLSCREEN: |
1683 | { |
1684 | appdata->fullscreen = !appdata->fullscreen; |
1685 | |
1686 | if(appdata->fullscreen) |
1687 | gtk_window_fullscreen(GTK_WINDOW(appdata->window)); |
1688 | else |
1689 | gtk_window_unfullscreen(GTK_WINDOW(appdata->window)); |
1690 | |
1691 | handled = TRUE; |
1692 | } |
1693 | break; |
1694 | #endif |
1695 | |
1696 | #else |
1697 | case '+': |
1698 | printf("zoom+\n"); |
1699 | html_zoom(appdata, TRUE); |
1700 | handled = TRUE; |
1701 | break; |
1702 | case '-': |
1703 | printf("zoom-\n"); |
1704 | html_zoom(appdata, FALSE); |
1705 | handled = TRUE; |
1706 | break; |
1707 | #endif |
1708 | } |
1709 | |
1710 | return handled; |
1711 | } |
1712 | |
1713 | #ifdef USE_BREAD_CRUMB_TRAIL |
1714 | typedef struct { |
1715 | int level; |
1716 | appdata_t *appdata; |
1717 | gpointer data; |
1718 | } crumb_t; |
1719 | |
1720 | static void |
1721 | crumb_back(gpointer data) { |
1722 | crumb_t *crumb = (crumb_t*)data; |
1723 | printf("crumb_back called with %d\n", crumb->level); |
1724 | |
1725 | /* don't do anything if main window has already been destroyed */ |
1726 | if(!crumb->appdata->window) { |
1727 | printf("Main window gone ...\n"); |
1728 | return; |
1729 | } |
1730 | |
1731 | /* whatever is being displayed: we don't need it anymore */ |
1732 | gtk_container_remove(GTK_CONTAINER(crumb->appdata->vbox), |
1733 | crumb->appdata->cur_view); |
1734 | |
1735 | /* back from cache to cachelist */ |
1736 | if(crumb->level == CRUMB_CACHE) { |
1737 | gpx_t *gpx = crumb->appdata->search_results; |
1738 | |
1739 | if(!gpx) { |
1740 | gtk_widget_set_sensitive(crumb->appdata->menu_search, TRUE); |
1741 | gtk_widget_set_sensitive(crumb->appdata->menu_export, TRUE); |
1742 | printf("no search data found, return to gpx\n"); |
1743 | gpx = crumb->appdata->cur_gpx; |
1744 | } else |
1745 | printf("returning to search result\n"); |
1746 | |
1747 | g_assert(gpx != NULL); |
1748 | |
1749 | crumb->appdata->cur_view = cachelist_create(crumb->appdata, gpx, |
1750 | crumb->appdata->cur_cache); |
1751 | |
1752 | /* returning from cache view: invalidate cache reference */ |
1753 | crumb->appdata->cur_cache = NULL; |
1754 | |
1755 | gtk_box_pack_start_defaults(GTK_BOX(crumb->appdata->vbox), |
1756 | crumb->appdata->cur_view); |
1757 | } |
1758 | |
1759 | if(crumb->level == CRUMB_SEARCH_GPX) { |
1760 | printf("returning from a local search!\n"); |
1761 | |
1762 | g_assert((gpx_t*)crumb->data == crumb->appdata->search_results); |
1763 | |
1764 | search_result_free((gpx_t*)crumb->data); |
1765 | crumb->appdata->search_results = NULL; |
1766 | |
1767 | gtk_widget_set_sensitive(crumb->appdata->menu_search, TRUE); |
1768 | |
1769 | crumb->appdata->cur_view = cachelist_create(crumb->appdata, |
1770 | crumb->appdata->cur_gpx, NULL); |
1771 | gtk_box_pack_start_defaults(GTK_BOX(crumb->appdata->vbox), |
1772 | crumb->appdata->cur_view); |
1773 | } |
1774 | |
1775 | /* back from cachelist to gpxlist */ |
1776 | if((crumb->level == CRUMB_CACHELIST) || |
1777 | (crumb->level == CRUMB_SEARCH_GLOBAL)) { |
1778 | |
1779 | crumb->appdata->cur_view = gpxlist_create_view_and_model( |
1780 | crumb->appdata, crumb->appdata->cur_gpx); |
1781 | |
1782 | /* returning from cachelist/global search view: */ |
1783 | /* invalidate gpx reference */ |
1784 | crumb->appdata->cur_gpx = NULL; |
1785 | |
1786 | gtk_box_pack_start_defaults(GTK_BOX(crumb->appdata->vbox), |
1787 | crumb->appdata->cur_view); |
1788 | |
1789 | if((crumb->level == CRUMB_SEARCH_GLOBAL) || |
1790 | (crumb->level == CRUMB_SEARCH_GPX)) { |
1791 | g_assert((gpx_t*)crumb->data == crumb->appdata->search_results); |
1792 | |
1793 | search_result_free((gpx_t*)crumb->data); |
1794 | crumb->appdata->search_results = NULL; |
1795 | } |
1796 | |
1797 | /* enable gpxlist related menu entries */ |
1798 | gtk_widget_set_sensitive(crumb->appdata->menu_import, TRUE); |
1799 | gtk_widget_set_sensitive(crumb->appdata->menu_search, TRUE); |
1800 | gtk_widget_set_sensitive(crumb->appdata->menu_export, TRUE); |
1801 | } |
1802 | |
1803 | gtk_widget_show_all(crumb->appdata->vbox); |
1804 | g_free(data); |
1805 | } |
1806 | |
1807 | static void crumb_add(appdata_t *appdata, char *name, int level, |
1808 | gpointer user_data) { |
1809 | crumb_t *crumb = malloc(sizeof(crumb_t)); |
1810 | crumb->level = level; |
1811 | crumb->appdata = appdata; |
1812 | crumb->data = user_data; |
1813 | |
1814 | printf("crumb_add with level %d\n", level); |
1815 | |
1816 | /* save that we are working on search results */ |
1817 | if((level == CRUMB_SEARCH_GLOBAL) || |
1818 | (level == CRUMB_SEARCH_GPX)) { |
1819 | appdata->search_results = (gpx_t*)user_data; |
1820 | |
1821 | /* searches cannot be nested */ |
1822 | gtk_widget_set_sensitive(appdata->menu_search, FALSE); |
1823 | } |
1824 | |
1825 | /* save "path" pointers in appdata */ |
1826 | if(crumb->level == CRUMB_CACHELIST) |
1827 | appdata->cur_gpx = (gpx_t*)user_data; |
1828 | |
1829 | if(crumb->level == CRUMB_CACHE) { |
1830 | appdata->cur_cache = (cache_t*)user_data; |
1831 | /* the cache view cannot be searched */ |
1832 | gtk_widget_set_sensitive(appdata->menu_search, FALSE); |
1833 | gtk_widget_set_sensitive(appdata->menu_export, FALSE); |
1834 | } |
1835 | |
1836 | if(level != CRUMB_GPXLIST) { |
1837 | /* disable gpxlist related menu entries */ |
1838 | gtk_widget_set_sensitive(appdata->menu_import, FALSE); |
1839 | #ifndef USE_PANNABLE_AREA |
1840 | gtk_widget_set_sensitive(appdata->menu_remove, FALSE); |
1841 | gtk_widget_set_sensitive(appdata->menu_close, FALSE); |
1842 | #endif |
1843 | } |
1844 | |
1845 | hildon_bread_crumb_trail_push_text(HILDON_BREAD_CRUMB_TRAIL(appdata->bct), |
1846 | name, crumb, (GDestroyNotify)crumb_back); |
1847 | } |
1848 | #endif // USE_BREAD_CRUMB_TRAIL |
1849 | |
1850 | void main_after_settings_redraw(appdata_t *appdata, int flags) { |
1851 | #ifndef USE_MAEMO |
1852 | // in non-maemo setup this can only affect the main screen as |
1853 | // the menu is blocked while a dialog is open. also the main |
1854 | // screen is always present |
1855 | if(appdata->gpxlist_items != appdata->cur_items) { |
1856 | /* re-do the main screen */ |
1857 | gtk_container_remove(GTK_CONTAINER(appdata->vbox), appdata->cur_view); |
1858 | appdata->cur_view = gpxlist_create_view_and_model(appdata, NULL); |
1859 | gtk_box_pack_start_defaults(GTK_BOX(appdata->vbox), appdata->cur_view); |
1860 | gtk_widget_show_all(appdata->vbox); |
1861 | } |
1862 | #else |
1863 | /* a cache screen cannot be changed from the settings and thus doesn't */ |
1864 | /* need to be redrawn */ |
1865 | if(appdata->cur_cache) { |
1866 | printf("No redraw in cache view required\n"); |
1867 | return; |
1868 | } |
1869 | |
1870 | int redraw = 0; // nothing to redraw |
1871 | |
1872 | if(appdata->search_results) { |
1873 | if((appdata->cur_items != appdata->cachelist_items) || flags) |
1874 | redraw = 1; |
1875 | } else { |
1876 | if(!appdata->cur_gpx) { |
1877 | if(appdata->cur_items != appdata->gpxlist_items) |
1878 | redraw = 2; // redraw gpxlist |
1879 | } else { |
1880 | if((appdata->cur_items != appdata->cachelist_items) || flags) |
1881 | redraw = 3; // redraw cachelist |
1882 | } |
1883 | } |
1884 | |
1885 | if(redraw) { |
1886 | gtk_container_remove(GTK_CONTAINER(appdata->vbox), appdata->cur_view); |
1887 | switch(redraw) { |
1888 | case 1: |
1889 | appdata->cur_view = cachelist_create(appdata, |
1890 | appdata->search_results, NULL); |
1891 | break; |
1892 | case 2: |
1893 | appdata->cur_view = gpxlist_create_view_and_model(appdata, NULL); |
1894 | break; |
1895 | case 3: |
1896 | appdata->cur_view = cachelist_create(appdata, |
1897 | appdata->cur_gpx, NULL); |
1898 | break; |
1899 | } |
1900 | |
1901 | gtk_box_pack_start_defaults(GTK_BOX(appdata->vbox), appdata->cur_view); |
1902 | gtk_widget_show_all(appdata->vbox); |
1903 | } |
1904 | #endif |
1905 | } |
1906 | |
1907 | int main(int argc, char *argv[]) { |
1908 | appdata_t appdata; |
1909 | |
1910 | /* init appdata */ |
1911 | memset(&appdata, 0, sizeof(appdata)); |
1912 | |
1913 | printf("Using locale for %s in %s\n", PACKAGE, LOCALEDIR); |
1914 | |
1915 | setlocale(LC_ALL, ""); |
1916 | bindtextdomain(PACKAGE, LOCALEDIR); |
1917 | bind_textdomain_codeset(PACKAGE, "UTF-8"); |
1918 | textdomain(PACKAGE); |
1919 | |
1920 | /* prepare thread system */ |
1921 | g_thread_init(NULL); |
1922 | |
1923 | gtk_init (&argc, &argv); |
1924 | |
1925 | #ifdef USE_MAEMO |
1926 | printf("Installing osso context for \"org.harbaum." APP "\"\n"); |
1927 | appdata.osso_context = osso_initialize("org.harbaum."APP, |
1928 | VERSION, TRUE, NULL); |
1929 | if(appdata.osso_context == NULL) { |
1930 | fprintf(stderr, "error initiating osso context\n"); |
1931 | } |
1932 | |
1933 | dbus_register(&appdata); |
1934 | #endif |
1935 | |
1936 | icons_init(); |
1937 | |
1938 | if(!gnome_vfs_init()) { |
1939 | g_error("Gnome VFS init failed\n"); |
1940 | } |
1941 | |
1942 | #ifdef USE_MAEMO |
1943 | /* Create the hildon program and setup the title */ |
1944 | appdata.program = HILDON_PROGRAM(hildon_program_get_instance()); |
1945 | g_set_application_name("GPXView"); |
1946 | |
1947 | /* Create HildonWindow and set it to HildonProgram */ |
1948 | #ifdef USE_STACKABLE_WINDOW |
1949 | appdata.window = HILDON_WINDOW(hildon_stackable_window_new()); |
1950 | #else |
1951 | appdata.window = HILDON_WINDOW(hildon_window_new()); |
1952 | #endif |
1953 | hildon_program_add_window(appdata.program, appdata.window); |
1954 | #else |
1955 | /* Create a Window. */ |
1956 | appdata.window = gtk_window_new(GTK_WINDOW_TOPLEVEL); |
1957 | /* Set a decent default size for the window. */ |
1958 | gtk_window_set_default_size(GTK_WINDOW(appdata.window), 500, 300); |
1959 | #endif |
1960 | |
1961 | gtk_window_set_title(GTK_WINDOW(appdata.window), "GPXView"); |
1962 | g_signal_connect(G_OBJECT(appdata.window), "destroy", |
1963 | G_CALLBACK(on_window_destroy), &appdata); |
1964 | |
1965 | g_signal_connect(G_OBJECT(appdata.window), "key_press_event", |
1966 | G_CALLBACK(on_window_key_press), &appdata); |
1967 | |
1968 | appdata.vbox = gtk_vbox_new(FALSE, 2); |
1969 | gtk_container_add(GTK_CONTAINER(appdata.window), appdata.vbox); |
1970 | |
1971 | menu_create(&appdata); |
1972 | |
1973 | #ifdef USE_BREAD_CRUMB_TRAIL |
1974 | appdata.bct = hildon_bread_crumb_trail_new(); |
1975 | |
1976 | gtk_box_pack_start(GTK_BOX(appdata.vbox), appdata.bct, FALSE,FALSE,0); |
1977 | |
1978 | hildon_bread_crumb_trail_clear(HILDON_BREAD_CRUMB_TRAIL(appdata.bct)); |
1979 | crumb_add(&appdata, "GPX", CRUMB_GPXLIST, NULL); |
1980 | #endif |
1981 | |
1982 | /* wait for main gui to appear */ |
1983 | gtk_widget_show_all(GTK_WIDGET(appdata.window)); |
1984 | while(gtk_events_pending()) |
1985 | gtk_main_iteration(); |
1986 | |
1987 | appdata.gconf_client = gconf_client_get_default(); |
1988 | gconf_load_state(&appdata); |
1989 | gps_init(&appdata); |
1990 | |
1991 | appdata.cur_view = gpxlist_create_view_and_model(&appdata, NULL); |
1992 | gtk_box_pack_start_defaults(GTK_BOX(appdata.vbox), appdata.cur_view); |
1993 | |
1994 | gtk_widget_show_all(GTK_WIDGET(appdata.window)); |
1995 | gtk_main(); |
1996 | |
1997 | cleanup(&appdata); |
1998 | |
1999 | return 0; |
2000 | } |