Contents of /trunk/src/cache.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 160 - (hide annotations)
Thu Nov 5 07:00:52 2009 UTC (14 years, 6 months ago) by harbaum
File MIME type: text/plain
File size: 28301 byte(s)
GCVote disk cache
1 harbaum 1 /*
2     * Copyright (C) 2008 Till Harbaum <till@harbaum.org>.
3     *
4     * This file is part of GPXView.
5     *
6     * GPXView is free software: you can redistribute it and/or modify
7     * it under the terms of the GNU General Public License as published by
8     * the Free Software Foundation, either version 3 of the License, or
9     * (at your option) any later version.
10     *
11     * GPXView is distributed in the hope that it will be useful,
12     * but WITHOUT ANY WARRANTY; without even the implied warranty of
13     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14     * GNU General Public License for more details.
15     *
16     * You should have received a copy of the GNU General Public License
17     * along with GPXView. If not, see <http://www.gnu.org/licenses/>.
18     */
19    
20     #include "gpxview.h"
21     #include <math.h>
22    
23     static GtkWidget *cache_description(appdata_t *appdata, cache_t *cache) {
24     return html_view(appdata, cache->long_description,
25 harbaum 140 cache->long_is_html?HTML_HTML:HTML_PLAIN_TEXT, TRUE, cache, NULL);
26 harbaum 1 }
27    
28     #ifndef USE_MAEMO // maemos touchscreen doesn't support tooltips
29     static const char *cache_type_tip[] = {
30     "Traditional Cache", "Multicache", "Mystery/Unknown Cache",
31     "Virtual Cache", "Webcam Cache", "Event Cache",
32     "Letterbox Hybrid", "Earthcache", "Wherigo Cache",
33     "Mega-Event Cache", "Cache-In-Trash-Out Cache"
34     };
35    
36     static const char *cache_size_tip[] = {
37     "Regular Container", "Small Container", "Micro",
38     "Other Container", "Container not chosen", "Large Container",
39     "Virtual Container"
40     };
41     #endif
42    
43     static const char *cache_size_name[] = {
44     "Regular", "Small", "Micro", "Other",
45     "Not chosen", "Large", "Virtual"
46     };
47    
48     void bearing_fill_hbox(GtkWidget *hbox,
49     appdata_t *appdata, pos_t refpos, pos_t pos) {
50     char str[32];
51    
52     if(!isnan(pos.lat) && !isnan(pos.lon)) {
53     gtk_box_pack_start(GTK_BOX(hbox), gtk_image_new_from_pixbuf(
54     icon_bearing(refpos, pos)),1,0,0);
55    
56 harbaum 34 if(!isnan(refpos.lat) && !isnan(refpos.lon)) {
57 harbaum 1 gtk_box_pack_start_defaults(GTK_BOX(hbox),
58     GTK_LABEL_SMALL((char*)pos_get_bearing_str(refpos, pos)));
59     snprintf(str, sizeof(str), _("%.1f°"),
60     gpx_pos_get_bearing(refpos, pos));
61     gtk_box_pack_start_defaults(GTK_BOX(hbox), GTK_LABEL_SMALL(str));
62     gpx_pos_get_distance_str(str, sizeof(str),
63     refpos, pos, appdata->imperial);
64     gtk_box_pack_start(GTK_BOX(hbox),
65     gtk_label_attrib(str, SIZE_SMALL, STRIKETHROUGH_NONE),1,0,0);
66     } else
67     gtk_box_pack_start(GTK_BOX(hbox),
68     gtk_label_attrib(_("(no position)"),
69     SIZE_SMALL, STRIKETHROUGH_NONE),1,0,0);
70     } else
71     gtk_box_pack_start(GTK_BOX(hbox),
72     gtk_label_attrib(_("(invalid position in notes)"),
73     SIZE_SMALL, STRIKETHROUGH_NONE),1,0,0);
74     }
75    
76     /* this function sets everthing related to the coordinate. used to */
77     /* cope with the user setting a new "note coordinate" */
78     void overview_coordinate_update(cache_context_t *context) {
79     if(!context->notes.modified)
80     return;
81    
82     /* update position labels */
83     int strike = notes_get_override(context)?STRIKETHROUGH:STRIKETHROUGH_NONE;
84     char str[32];
85     pos_lat_str(str, sizeof(str), context->cache->pos.lat);
86     gtk_label_attrib_set(context->pos_lat_label, str, SIZE_BIG, strike);
87     pos_lon_str(str, sizeof(str), context->cache->pos.lon);
88     gtk_label_attrib_set(context->pos_lon_label, str, SIZE_BIG, strike);
89    
90     /* remove enire hbox and build a new one */
91     gtk_container_foreach(GTK_CONTAINER(context->bearing_hbox),
92     (GtkCallback)gtk_widget_destroy, NULL);
93    
94     /* update distance/etc */
95 harbaum 13 if(!isnan(context->cache->pos.lat) &&
96     !isnan(context->cache->pos.lon))
97     bearing_fill_hbox(context->bearing_hbox, context->appdata,
98     *get_pos(context->appdata), notes_get_pos(context));
99    
100 harbaum 1 gtk_widget_show_all(context->bearing_hbox);
101     }
102    
103 harbaum 159 static void gcvote_set(cache_context_t *context, vote_t *vote) {
104     if(!vote) return;
105 harbaum 158
106 harbaum 159 if(context->quality) {
107     gtk_widget_destroy(context->quality);
108     context->quality = NULL;
109     }
110 harbaum 158
111 harbaum 159 if(context->votes) {
112     gtk_widget_destroy(context->votes);
113     context->votes = NULL;
114     }
115 harbaum 158
116     /* update/draw the voting */
117     #ifndef USE_MAEMO
118 harbaum 159 GtkTooltips *tips = gtk_tooltips_new();
119 harbaum 158 #endif
120    
121 harbaum 159 char *votes_str = g_strdup_printf(_("Quality (%d %s):"), vote->votes,
122 harbaum 158 (vote->votes == 1)?_("vote"):_("votes"));
123 harbaum 159 context->votes = GTK_LABEL_SMALL(votes_str);
124     gtk_box_pack_start(GTK_BOX(context->votebox),
125     context->votes, FALSE, FALSE, 0);
126     g_free(votes_str);
127     context->quality = icon_get_widget(ICON_STARS, (int)(vote->quality*2-2));
128     gtk_box_pack_start(GTK_BOX(context->votebox), context->quality,
129     FALSE, FALSE, 0);
130    
131 harbaum 158 #ifndef USE_MAEMO
132 harbaum 159 char *str = g_strdup_printf(_("Quality: %d"), vote->quality);
133     gtk_tooltips_set_tip(tips, context->votebox, str, NULL);
134     g_free(str);
135 harbaum 158 #endif
136 harbaum 159
137     gtk_widget_show_all(context->votebox);
138 harbaum 158
139 harbaum 159 g_free(vote);
140     }
141 harbaum 158
142 harbaum 159 static void gcvote_callback(vote_t *vote, gpointer data) {
143     cache_context_t *context = (cache_context_t*)data;
144    
145     /* no vote returned: request failed, just cleanup */
146     if(!vote) {
147 harbaum 160 printf("gcvote: callback for failed request\n");
148 harbaum 159
149 harbaum 158 gcvote_request_free(context->gcvote_request);
150     context->gcvote_request = NULL;
151 harbaum 159 return;
152 harbaum 158 }
153 harbaum 159
154 harbaum 160 printf("gcvote: callback is being called with a %d/%d\n",
155 harbaum 159 vote->quality, vote->votes);
156    
157     gcvote_set(context, vote);
158    
159     gcvote_save(context->appdata, context->cache, &context->gcvote_request->mem);
160    
161     gcvote_request_free(context->gcvote_request);
162     context->gcvote_request = NULL;
163 harbaum 158 }
164    
165 harbaum 1 static GtkWidget *cache_overview(cache_context_t *context) {
166     GtkWidget *vbox, *ivbox;
167     GtkWidget *table, *tip;
168     char str[64];
169     #ifndef USE_MAEMO
170     GtkTooltips *tips = gtk_tooltips_new();
171     #endif
172     appdata_t *appdata = context->appdata;
173     cache_t *cache = context->cache;
174    
175     vbox = gtk_vbox_new(FALSE, 0);
176    
177     table = gtk_table_new(3,4, FALSE);
178    
179     if(cache->type != CACHE_TYPE_UNKNOWN) {
180 harbaum 137 gtk_table_attach(GTK_TABLE(table),
181     tip = icon_get_widget(ICON_CACHE_TYPE, cache->type), 0,1,0,1,
182     GTK_FILL, 0, GTK_FILL, 0);
183 harbaum 1 #ifndef USE_MAEMO
184     gtk_tooltips_set_tip(tips, tip, _(cache_type_tip[cache->type]), NULL);
185     #endif
186     }
187    
188     /* ------------ box containing container info ------------ */
189     if(cache->container != CACHE_CONT_UNKNOWN) {
190     ivbox = gtk_vbox_new(FALSE, 0);
191     sprintf(str, _("Size: %s"), _(cache_size_name[cache->container]));
192 harbaum 137 gtk_box_pack_start(GTK_BOX(ivbox), GTK_LABEL_SMALL(str),
193     FALSE, FALSE, 0);
194     gtk_box_pack_start(GTK_BOX(ivbox),
195     icon_get_widget(ICON_CACHE_SIZE, cache->container),
196     FALSE, FALSE, 0);
197     gtk_table_attach(GTK_TABLE(table), ivbox, 0,1,1,2,
198     GTK_EXPAND | GTK_FILL, 0, GTK_FILL, 0);
199 harbaum 1 #ifndef USE_MAEMO
200     gtk_tooltips_set_tip(tips, ivbox, _(cache_size_tip[cache->container]), NULL);
201     #endif
202     }
203    
204     /* ----------------------- id ---------------------------- */
205     if(cache->id) {
206     int strike = cache->archived?STRIKETHROUGH_RED:
207     (!cache->available?STRIKETHROUGH:STRIKETHROUGH_NONE);
208 harbaum 137 GtkWidget *lbl = link_button_attrib(context->appdata,
209     cache->id, context->cache->url,
210     SIZE_BIG, strike);
211 harbaum 1 gtk_table_attach(GTK_TABLE(table), lbl, 1,2,0,1, FALSE, FALSE, 0, 0);
212     }
213    
214     /* --------------- box containing owner info ------------- */
215     if(cache->owner) {
216     ivbox = gtk_vbox_new(FALSE, 0);
217     gtk_box_pack_start_defaults(GTK_BOX(ivbox), GTK_LABEL_SMALL(_("by")));
218 harbaum 137
219     static const char *owner_type = "profile/";
220     gtk_box_pack_start_defaults(GTK_BOX(ivbox),
221     link_button_by_id(appdata, cache->owner->name,
222     owner_type, cache->owner->id));
223     gtk_table_attach(GTK_TABLE(table), ivbox, 1,2,1,2, FALSE,FALSE,0,0);
224 harbaum 1 }
225    
226 harbaum 156 /* ----------- vbox containing all ratings ---------- */
227 harbaum 159 GtkWidget *ratebox = gtk_vbox_new(FALSE, 0);
228 harbaum 156
229 harbaum 1 /* ----------- box containing difficulty rating ---------- */
230     if(cache->difficulty != 0) {
231     ivbox = gtk_vbox_new(FALSE, 0);
232 harbaum 137 gtk_box_pack_start(GTK_BOX(ivbox),
233     GTK_LABEL_SMALL(_("Difficulty:")),
234     FALSE, FALSE, 0);
235     gtk_box_pack_start(GTK_BOX(ivbox),
236     icon_get_widget(ICON_STARS, (int)(cache->difficulty*2-2)),
237     FALSE, FALSE, 0);
238 harbaum 156
239     GtkWidget *align = gtk_alignment_new(0.5, 0.5, 0.0, 0.0);
240     gtk_container_add(GTK_CONTAINER(align), ivbox);
241     gtk_box_pack_start_defaults(GTK_BOX(ratebox), align);
242 harbaum 1 #ifndef USE_MAEMO
243     sprintf(str, _("Difficulty: %.1f"), cache->difficulty);
244     gtk_tooltips_set_tip(tips, ivbox, str, NULL);
245     #endif
246     }
247    
248     /* ------------ box containing terrain rating ------------ */
249     if(cache->terrain != 0) {
250     ivbox = gtk_vbox_new(FALSE, 0);
251 harbaum 137 gtk_box_pack_start(GTK_BOX(ivbox), GTK_LABEL_SMALL(_("Terrain:")),
252     FALSE, FALSE, 0);
253     gtk_box_pack_start(GTK_BOX(ivbox),
254     icon_get_widget(ICON_STARS, (int)(cache->terrain*2-2)),
255     FALSE, FALSE, 0);
256 harbaum 156 GtkWidget *align = gtk_alignment_new(0.5, 0.5, 0.0, 0.0);
257     gtk_container_add(GTK_CONTAINER(align), ivbox);
258     gtk_box_pack_start_defaults(GTK_BOX(ratebox), align);
259 harbaum 1 #ifndef USE_MAEMO
260     sprintf(str, _("Terrain: %.1f"), cache->terrain);
261     gtk_tooltips_set_tip(tips, ivbox, str, NULL);
262     #endif
263     }
264    
265 harbaum 156 /* --------------------- GCVote ------------------------ */
266 harbaum 159
267 harbaum 160 vote_t *vote = gcvote_restore(appdata, cache);
268    
269 harbaum 158 context->gcvote_request =
270     gcvote_request(appdata, gcvote_callback, cache->url, context);
271 harbaum 156
272 harbaum 159 context->votebox = gtk_vbox_new(FALSE, 0);
273     GtkWidget *align = gtk_alignment_new(0.5, 0.5, 0.0, 0.0);
274     gtk_container_add(GTK_CONTAINER(align), context->votebox);
275     gtk_box_pack_start_defaults(GTK_BOX(ratebox), align);
276 harbaum 156
277 harbaum 160 /* fill with vote if present on disk (will also free vote) */
278     if(vote)
279     gcvote_set(context, vote);
280    
281 harbaum 156 gtk_table_attach_defaults(GTK_TABLE(table), ratebox, 2,3,0,2);
282    
283     /* ----------------------------------------------------- */
284    
285    
286 harbaum 1 /* ----------------- the two coordinates ----------------- */
287     /* ----------------- and the heading/distance ------------ */
288     pos_t *refpos = get_pos(appdata);
289    
290     ivbox = gtk_vbox_new(FALSE, 0);
291     int strike = (cache->notes && cache->notes->override)?
292     STRIKETHROUGH:STRIKETHROUGH_NONE;
293    
294     /* the original coordinate is being displayed */
295     gtk_box_pack_start_defaults(GTK_BOX(ivbox),
296     context->pos_lat_label = pos_lat(cache->pos.lat, SIZE_BIG, strike));
297     gtk_box_pack_start_defaults(GTK_BOX(ivbox),
298     context->pos_lon_label = pos_lon(cache->pos.lon, SIZE_BIG, strike));
299    
300     /* but calculations may be done with respect to the overriden one */
301 harbaum 13 if(!isnan(cache->pos.lat) && !isnan(cache->pos.lon)) {
302     context->bearing_hbox = gtk_hbox_new(FALSE, 0);
303     bearing_fill_hbox(context->bearing_hbox, appdata, *refpos,
304     gpx_cache_pos(cache));
305     gtk_box_pack_start_defaults(GTK_BOX(ivbox), context->bearing_hbox);
306     }
307 harbaum 1
308     gtk_table_attach_defaults(GTK_TABLE(table), ivbox, 3,4,0,2);
309    
310 harbaum 157 /* ----------------------------------------------------- */
311    
312 harbaum 1 gtk_box_pack_start(GTK_BOX(vbox), table, 0,0,0);
313     gtk_box_pack_start(GTK_BOX(vbox), gtk_hseparator_new(),FALSE,FALSE,0);
314    
315 harbaum 156 /* ----------------------------------------------------- */
316    
317 harbaum 1 if(cache->short_description)
318     gtk_box_pack_start_defaults(GTK_BOX(vbox),
319     html_view(appdata, cache->short_description,
320 harbaum 140 cache->short_is_html?HTML_HTML:HTML_PLAIN_TEXT, TRUE, cache, NULL));
321 harbaum 1
322     return vbox;
323     }
324    
325     /* slow but short, we don't need performance here ... */
326     static void rot13(char *t) {
327 harbaum 55 int braces = 0;
328    
329 harbaum 1 while(*t) {
330 harbaum 55 if(!braces) {
331     if(*t == '[')
332     braces++;
333     else if(((*t >= 'a') && (*t < 'n')) ||
334     ((*t >= 'A') && (*t < 'N'))) *t += 13;
335     else if(((*t >= 'n') && (*t <= 'z')) ||
336     ((*t >= 'N') && (*t <= 'Z'))) *t -= 13;
337     } else {
338     if(braces > 0 && *t == ']')
339     braces--;
340     }
341 harbaum 1
342     t++;
343     }
344     }
345    
346     static void on_decrypt(GtkWidget *widget, gpointer data) {
347     /* data is a link to the textview */
348     g_assert(GTK_IS_TEXT_VIEW(data));
349    
350     GtkTextIter start, end;
351     GtkTextBuffer *buffer = gtk_text_view_get_buffer((GtkTextView*)data);
352    
353     gtk_text_buffer_get_start_iter(buffer, &start);
354     gtk_text_buffer_get_end_iter(buffer, &end);
355     char *text = gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
356    
357     rot13(text);
358     gtk_text_buffer_set_text(buffer, text, -1);
359    
360     free(text);
361     }
362    
363     static GtkWidget *cache_hint(appdata_t *appdata, cache_t *cache) {
364     /* encrypting/decrypting html is nothing we want to do */
365     if(cache->hint_is_html)
366 harbaum 140 return html_view(appdata, cache->hint, HTML_HTML, TRUE, NULL, NULL);
367 harbaum 1
368     /* we can now be sure that we are talking about pain text */
369     GtkWidget *vbox = gtk_vbox_new(FALSE, 0);
370    
371     char *hint = strdup(cache->hint);
372     rot13(hint);
373 harbaum 140 GtkWidget *view =
374     html_view(appdata, hint, HTML_PLAIN_TEXT, TRUE, NULL, NULL);
375 harbaum 1 gtk_box_pack_start_defaults(GTK_BOX(vbox), view);
376     free(hint);
377    
378     GtkWidget *button = gtk_button_new_with_label(_("Encrypt/Decrypt"));
379 harbaum 66 #if defined(USE_MAEMO) && (MAEMO_VERSION_MAJOR == 5)
380     hildon_gtk_widget_set_theme_size(button,
381     (HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH));
382     #endif
383 harbaum 1 gtk_box_pack_start(GTK_BOX(vbox), button, FALSE, FALSE, 0);
384     gtk_signal_connect(GTK_OBJECT(button), "clicked",
385     GTK_SIGNAL_FUNC(on_decrypt), gtk_bin_get_child(GTK_BIN(view)));
386    
387     return vbox;
388     }
389    
390     static GtkWidget *cache_wpts(appdata_t *appdata, wpt_t *wpt) {
391     pos_t *refpos = NULL;
392    
393     #ifndef USE_PANNABLE_AREA
394     GtkWidget *scrolled_window = gtk_scrolled_window_new(NULL, NULL);
395     gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window),
396     GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
397     #else
398     GtkWidget *pannable_area = hildon_pannable_area_new();
399     #endif
400    
401     GtkWidget *vbox = gtk_vbox_new(FALSE, 0);
402    
403     /* four rows per waypoint */
404     GtkWidget *table = gtk_table_new(4*gpx_number_of_waypoints(wpt)-1,4, FALSE);
405    
406     refpos = get_pos(appdata);
407    
408     int wpt_row=0;
409     while(wpt) {
410     GtkWidget *ihbox, *tip;
411     char str[32];
412    
413     /* ----------------------- icon/id ---------------------------- */
414     ihbox = gtk_hbox_new(FALSE, 0);
415    
416     if(wpt->sym != WPT_SYM_UNKNOWN) {
417     gtk_box_pack_start(GTK_BOX(ihbox),
418     tip = icon_get_widget(ICON_WPT, wpt->sym), 1,0,0);
419     }
420    
421     if(wpt->id)
422     gtk_box_pack_start(GTK_BOX(ihbox), GTK_LABEL_BIG(wpt->id), 1,0,0);
423    
424     gtk_table_attach_defaults(GTK_TABLE(table), ihbox, 0,1,wpt_row, wpt_row+1);
425    
426     /* ----------------- the two coordinates ----------------- */
427     /* ----------------- and the heading/distance ------------ */
428     gtk_table_attach_defaults(GTK_TABLE(table),
429     pos_lat(wpt->pos.lat, SIZE_BIG, STRIKETHROUGH_NONE),
430     1,2, wpt_row, wpt_row+1);
431     gtk_table_attach_defaults(GTK_TABLE(table),
432     pos_lon(wpt->pos.lon, SIZE_BIG, STRIKETHROUGH_NONE),
433     2,3, wpt_row, wpt_row+1);
434    
435     ihbox = gtk_hbox_new(FALSE, 0);
436     gtk_box_pack_start(GTK_BOX(ihbox), gtk_image_new_from_pixbuf(
437     icon_bearing(*refpos, wpt->pos)),1,0,0);
438     gtk_box_pack_start_defaults(GTK_BOX(ihbox),
439     GTK_LABEL_SMALL((char*)pos_get_bearing_str(*refpos, wpt->pos)));
440     snprintf(str, sizeof(str), _("%.1f°"),
441     gpx_pos_get_bearing(*refpos, wpt->pos));
442     gtk_box_pack_start_defaults(GTK_BOX(ihbox), GTK_LABEL_SMALL(str));
443     gpx_pos_get_distance_str(str, sizeof(str),
444     *refpos, wpt->pos, appdata->imperial);
445     gtk_box_pack_start(GTK_BOX(ihbox), GTK_LABEL_SMALL(str),1,0,0);
446    
447     gtk_table_attach_defaults(GTK_TABLE(table), ihbox, 3,4,
448     wpt_row+0, wpt_row+1);
449    
450     /* ------------------ description ------------------------- */
451     if(wpt->desc) {
452     GtkTextBuffer *buffer = gtk_text_buffer_new(NULL);
453     gtk_text_buffer_set_text(buffer, wpt->desc, strlen(wpt->desc));
454    
455 harbaum 8 #ifndef USE_HILDON_TEXT_VIEW
456 harbaum 1 GtkWidget *textview = gtk_text_view_new_with_buffer(buffer);
457     #else
458     GtkWidget *textview = hildon_text_view_new();
459     hildon_text_view_set_buffer(HILDON_TEXT_VIEW(textview), buffer);
460     #endif
461    
462     gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(textview), GTK_WRAP_WORD);
463     gtk_text_view_set_editable(GTK_TEXT_VIEW(textview), FALSE);
464     gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(textview), FALSE);
465    
466     gtk_table_attach_defaults(GTK_TABLE(table), textview, 0,4,
467     wpt_row+1, wpt_row+2);
468     }
469    
470     /* ------------------ comment ------------------------- */
471     if(wpt->cmt) {
472     GtkTextBuffer *buffer = gtk_text_buffer_new(NULL);
473     gtk_text_buffer_set_text(buffer, wpt->cmt, strlen(wpt->cmt));
474 harbaum 8 #ifndef USE_HILDON_TEXT_VIEW
475 harbaum 1 GtkWidget *textview = gtk_text_view_new_with_buffer(buffer);
476     #else
477     GtkWidget *textview = hildon_text_view_new();
478     hildon_text_view_set_buffer(HILDON_TEXT_VIEW(textview), buffer);
479     #endif
480     gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(textview), GTK_WRAP_WORD);
481     gtk_text_view_set_editable(GTK_TEXT_VIEW(textview), FALSE);
482     gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(textview), FALSE);
483    
484     gtk_table_attach_defaults(GTK_TABLE(table), textview, 0,4,
485     wpt_row+2, wpt_row+3);
486     }
487    
488     /* --------------------- seperator -------------------------*/
489     if(wpt->next)
490     gtk_table_attach_defaults(GTK_TABLE(table), gtk_hseparator_new(), 0,4,
491     wpt_row+3, wpt_row+4);
492    
493    
494     wpt_row+=4;
495     wpt = wpt->next;
496     }
497    
498     gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
499    
500     #ifndef USE_PANNABLE_AREA
501     gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scrolled_window),
502     vbox);
503     return scrolled_window;
504     #else
505     hildon_pannable_area_add_with_viewport(HILDON_PANNABLE_AREA(pannable_area),
506     vbox);
507     return pannable_area;
508     #endif
509     }
510    
511     static GtkWidget *cache_tbs(appdata_t *appdata, tb_t *tb) {
512     pos_t *refpos = NULL;
513    
514     #ifndef USE_PANNABLE_AREA
515     GtkWidget *scrolled_window = gtk_scrolled_window_new(NULL, NULL);
516     gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window),
517     GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
518     #else
519     GtkWidget *pannable_area = hildon_pannable_area_new();
520     #endif
521    
522     GtkWidget *vbox = gtk_vbox_new(FALSE, 0);
523    
524     /* four rows per waypoint */
525     GtkWidget *table = gtk_table_new(2*gpx_number_of_tbs(tb)-1,3, FALSE);
526    
527     refpos = get_pos(appdata);
528    
529     int tb_row=0;
530     while(tb) {
531 harbaum 137 static const char *tb_type = "track/details.aspx";
532    
533 harbaum 1 /* --------------------- icon/ref/name -------------------------*/
534     gtk_table_attach_defaults(GTK_TABLE(table), icon_get_widget(ICON_TB, 0),
535     0, 1, tb_row+0, tb_row+1);
536 harbaum 133
537 harbaum 137
538 harbaum 133 if(tb->ref) {
539 harbaum 137 GtkWidget *ref = link_button_by_id(appdata, tb->ref, tb_type, tb->id);
540 harbaum 133 gtk_table_attach_defaults(GTK_TABLE(table), ref,
541 harbaum 1 1, 2, tb_row+0, tb_row+1);
542 harbaum 133 }
543    
544 harbaum 1 if(tb->name)
545     gtk_table_attach_defaults(GTK_TABLE(table), GTK_LABEL_BIG(tb->name),
546     2, 3, tb_row+0, tb_row+1);
547    
548     /* --------------------- seperator -------------------------*/
549     if(tb->next)
550     gtk_table_attach_defaults(GTK_TABLE(table), gtk_hseparator_new(), 0, 3,
551     tb_row+1, tb_row+2);
552     tb_row+=2;
553     tb = tb->next;
554     }
555    
556     gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
557    
558     #ifndef USE_PANNABLE_AREA
559     gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scrolled_window),
560     vbox);
561     return scrolled_window;
562     #else
563     hildon_pannable_area_add_with_viewport(HILDON_PANNABLE_AREA(pannable_area),
564     vbox);
565     return pannable_area;
566     #endif
567     }
568    
569 harbaum 66 #ifdef ENABLE_BROWSER_INTERFACE
570     static void on_gclink_clicked(GtkButton *button, gpointer data) {
571     cache_context_t *context = (cache_context_t*)data;
572     char *url = g_strdup_printf("http://www.geocaching.com/seek/log.aspx?wp=%s", context->cache->id);
573     browser_url(context->appdata, url);
574     g_free(url);
575     }
576     #endif
577    
578     static GtkWidget *cache_logs(appdata_t *appdata, cache_context_t *context, log_t *log, int is_html) {
579 harbaum 1 #ifndef USE_PANNABLE_AREA
580     /* put this inside a scrolled view */
581     GtkWidget *scrolled_window = gtk_scrolled_window_new(NULL, NULL);
582     gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window),
583     GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
584     #else
585     GtkWidget *pannable_area = hildon_pannable_area_new();
586     #endif
587    
588 harbaum 66 #ifdef ENABLE_BROWSER_INTERFACE
589     gboolean gc_link = strncmp(context->cache->id, "GC", 2) == 0;
590     #else
591     #define gc_link (FALSE)
592     #endif
593    
594     GtkWidget *table = gtk_table_new(4*gpx_number_of_logs(log)+(gc_link?1:0), 3, FALSE);
595 harbaum 1 int cnt = 0;
596 harbaum 66
597     #ifdef ENABLE_BROWSER_INTERFACE
598     if(gc_link) {
599     GtkWidget *but = gtk_button_new_with_label(_("Post a new log entry for this geocache"));
600     #if defined(USE_MAEMO) && (MAEMO_VERSION_MAJOR == 5)
601     hildon_gtk_widget_set_theme_size(but,
602     (HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH));
603     #endif
604     gtk_signal_connect(GTK_OBJECT(but), "clicked",
605     GTK_SIGNAL_FUNC(on_gclink_clicked), context);
606    
607     gtk_table_attach_defaults(GTK_TABLE(table), but, 0, 3, 0, 1);
608     cnt++;
609     }
610     #endif
611 harbaum 1
612     /* add all logs to the vbox */
613     while(log) {
614     gtk_table_attach_defaults(GTK_TABLE(table), gtk_hseparator_new(),
615     0, 3, cnt+0, cnt+1);
616 harbaum 138 #if 0
617     static const char *log_type = "seek/log.aspx";
618     GtkWidget *log_but =
619     link_icon_button_by_id(appdata, icon_get_widget(ICON_LOG, log->type),
620     log_type, log->id);
621     gtk_table_attach(GTK_TABLE(table), log_but,
622     0, 1, cnt+1, cnt+2, FALSE, FALSE, 0, 0);
623     #else
624 harbaum 1 gtk_table_attach_defaults(GTK_TABLE(table),
625 harbaum 138 icon_get_widget(ICON_LOG, log->type), 0, 1, cnt+1, cnt+2);
626     #endif
627 harbaum 1
628     char date_str[32];
629     if(log->day && log->month && log->year) {
630     GDate *date = g_date_new_dmy(log->day, log->month, log->year);
631     g_date_strftime(date_str, sizeof(date_str), "%x", date);
632     g_date_free(date);
633     } else
634     strcpy(date_str, "---");
635    
636     gtk_table_attach_defaults(GTK_TABLE(table), gtk_label_new(date_str),
637     1, 2, cnt+1, cnt+2);
638    
639 harbaum 137 static const char *finder_type = "profile/";
640     GtkWidget *finder = link_button_by_id(appdata, log->finder->name,
641     finder_type, log->finder->id);
642    
643     gtk_table_attach(GTK_TABLE(table), finder,
644     2, 3, cnt+1, cnt+2, FALSE, FALSE, 0, 0);
645    
646 harbaum 1 gtk_table_attach_defaults(GTK_TABLE(table), gtk_hseparator_new(),
647     0, 3, cnt+2, cnt+3);
648    
649     if(log->text) {
650     gtk_table_attach_defaults(GTK_TABLE(table),
651 harbaum 140 html_view(appdata, log->text,
652     is_html?HTML_HTML:HTML_CUSTOM_MARKUP, FALSE, NULL, NULL),
653     0, 3, cnt+3, cnt+4);
654 harbaum 1 }
655 harbaum 140
656 harbaum 1 log = log->next;
657     cnt+=4;
658     }
659    
660     #ifndef USE_PANNABLE_AREA
661     gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scrolled_window),
662     table);
663     return scrolled_window;
664     #else
665     hildon_pannable_area_add_with_viewport(HILDON_PANNABLE_AREA(pannable_area),
666     table);
667     return pannable_area;
668     #endif
669     }
670    
671     #ifdef USE_MAEMO
672     /* this routine is called once a second as long as the "goto" tab is visible */
673     static gboolean screensaver_update(gpointer data) {
674     appdata_t *appdata = (appdata_t*)data;
675    
676     if(appdata->goto_disable_screensaver)
677     if (osso_display_blanking_pause(appdata->osso_context) != OSSO_OK)
678     fprintf(stderr, "error with display blank\n");
679    
680     return TRUE; // fire again
681     }
682     #endif
683    
684     static void on_notebook_page_change(GtkNotebook *notebook,
685     GtkNotebookPage *page,
686     guint page_num,
687     gpointer user_data) {
688    
689     cache_context_t *context = (cache_context_t*)user_data;
690     GtkWidget *w = gtk_notebook_get_nth_page(notebook, page_num);
691     const char *name = gtk_notebook_get_tab_label_text(notebook, w);
692    
693     #ifdef USE_MAEMO
694     if(context->handler_id)
695     gtk_timeout_remove(context->handler_id);
696     #endif
697    
698     /* this is a workaround, around some bug in the gtktextwidget or so ... */
699     /* i tried to get info on this and everybody agreed that this is a bug */
700 harbaum 136 /* in gtk but noone had a fix ready. so i came up with this. */
701 harbaum 1 /* seems to work ... */
702     if(strcasecmp(name, _("Logs")) == 0) {
703     gtk_widget_queue_resize(w);
704 harbaum 136 } else if(strcasecmp(name, _("TBs")) == 0) {
705     gtk_widget_queue_resize(w);
706     } else if(strcasecmp(name, _("Goto")) == 0) {
707 harbaum 1 #ifdef USE_MAEMO
708     context->handler_id = gtk_timeout_add(1000, screensaver_update,
709     context->appdata);
710     #endif
711     goto_coordinate_update(context);
712     }
713    
714     if(strcasecmp(name, _("Main")) == 0) {
715     /* the notes page may have changed its "override" setting, thus the */
716     /* striked out coordinate may need update */
717     overview_coordinate_update(context);
718     }
719     }
720    
721     static void on_notebook_destroy(GtkWidget *widget, gpointer user_data ) {
722     cache_context_t *context = (cache_context_t*)user_data;
723    
724     printf("destroying notebook\n");
725    
726 harbaum 159 /* cancel a pending gcvote request */
727     if(context->gcvote_request) {
728     gcvote_request_free(context->gcvote_request);
729     context->gcvote_request = NULL;
730     }
731    
732 harbaum 1 notes_destroy_event(NULL, context);
733     goto_destroy_event(NULL, context);
734    
735     #ifdef USE_MAEMO
736     if(context->handler_id)
737     gtk_timeout_remove(context->handler_id);
738     #endif
739    
740     free(user_data);
741     }
742    
743     GtkWidget *cache_view(appdata_t *appdata, cache_t *cache) {
744     GtkWidget *notebook;
745    
746     cache_context_t *cache_context = malloc(sizeof(cache_context_t));
747     memset(cache_context, 0, sizeof(cache_context_t));
748     cache_context->appdata = appdata;
749     cache_context->cache = cache;
750    
751     #ifdef USE_MAEMO
752     #define TAB_DESC _("Desc.")
753     #define TAB_WPTS _("Wpts")
754     #else
755     #define TAB_DESC _("Description")
756     #define TAB_WPTS _("Waypoints")
757     #endif
758    
759     notebook = gtk_notebook_new();
760 harbaum 133
761 harbaum 1 #ifdef USE_MAEMO
762 harbaum 11 #if MAEMO_VERSION_MAJOR >= 5
763 harbaum 1 /* prevents user from accidentially touching the breadcrumb trail */
764     gtk_notebook_set_tab_pos(GTK_NOTEBOOK(notebook), GTK_POS_BOTTOM);
765     #endif
766     #endif
767    
768     gtk_notebook_append_page(GTK_NOTEBOOK(notebook),
769     cache_overview(cache_context), gtk_label_new(_("Main")));
770    
771     if(cache->long_description)
772     gtk_notebook_append_page(GTK_NOTEBOOK(notebook),
773     cache_description(appdata, cache), gtk_label_new(TAB_DESC));
774    
775     if(cache->hint)
776     gtk_notebook_append_page(GTK_NOTEBOOK(notebook),
777     cache_hint(appdata, cache), gtk_label_new(_("Hint")));
778    
779     if(cache->log)
780     gtk_notebook_append_page(GTK_NOTEBOOK(notebook),
781 harbaum 66 cache_logs(appdata, cache_context, cache->log, cache->logs_are_html),
782 harbaum 1 gtk_label_new(_("Logs")));
783    
784     if(cache->wpt)
785     gtk_notebook_append_page(GTK_NOTEBOOK(notebook),
786     cache_wpts(appdata, cache->wpt), gtk_label_new(TAB_WPTS));
787    
788     if(cache->tb)
789     gtk_notebook_append_page(GTK_NOTEBOOK(notebook),
790     cache_tbs(appdata, cache->tb), gtk_label_new(_("TBs")));
791    
792     gtk_notebook_append_page(GTK_NOTEBOOK(notebook),
793     cache_notes(cache_context), gtk_label_new(_("Notes")));
794    
795     gtk_notebook_append_page(GTK_NOTEBOOK(notebook),
796     goto_cache(cache_context), gtk_label_new(_("Goto")));
797    
798     g_signal_connect(G_OBJECT(notebook), "switch-page",
799     G_CALLBACK(on_notebook_page_change), cache_context);
800    
801     g_signal_connect(G_OBJECT(notebook), "destroy",
802     G_CALLBACK(on_notebook_destroy), cache_context);
803    
804     return notebook;
805     }
806    
807     #ifndef USE_MAEMO
808     void cache_dialog(appdata_t *appdata, cache_t *cache) {
809     GtkWidget *dialog = gtk_dialog_new_with_buttons(cache->name,
810     GTK_WINDOW(appdata->window),
811     GTK_DIALOG_NO_SEPARATOR | GTK_DIALOG_MODAL |
812     GTK_DIALOG_DESTROY_WITH_PARENT,
813     GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE,
814     NULL);
815    
816     gtk_window_set_default_size(GTK_WINDOW(dialog), DIALOG_WIDTH, DIALOG_HEIGHT);
817    
818     /* create cache visualization widget */
819     gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox),
820     cache_view(appdata, cache));
821    
822     gtk_widget_show_all(dialog);
823     gtk_dialog_run(GTK_DIALOG(dialog));
824     gtk_widget_destroy(dialog);
825     }
826    
827     #else
828     #ifdef USE_STACKABLE_WINDOW
829 harbaum 11 static void on_cache_destroy (GtkWidget *widget, appdata_t *appdata) {
830     appdata->cur_cache = NULL;
831    
832     /* restore cur_view */
833     appdata->cur_view = g_object_get_data(G_OBJECT(widget), "cur_view");
834     }
835    
836 harbaum 1 void cache_dialog(appdata_t *appdata, cache_t *cache) {
837     GtkWidget *window = hildon_stackable_window_new();
838    
839 harbaum 11 /* store last "cur_view" in window */
840     g_object_set_data(G_OBJECT(window), "cur_view", appdata->cur_view);
841    
842     appdata->cur_cache = cache;
843 harbaum 34 char *title = g_strdup_printf("%s - GPXView", cache->name);
844 harbaum 6 gtk_window_set_title(GTK_WINDOW(window), title);
845     g_free(title);
846 harbaum 1
847     /* create cache visualization widget */
848 harbaum 11 appdata->cur_view = cache_view(appdata, cache);
849     gtk_container_add(GTK_CONTAINER(window), appdata->cur_view);
850 harbaum 1
851 harbaum 3 hildon_window_set_app_menu(HILDON_WINDOW(window),
852     menu_create(appdata, MENU_CACHE));
853    
854 harbaum 11 g_signal_connect(G_OBJECT(window), "destroy",
855     G_CALLBACK(on_cache_destroy), appdata);
856    
857 harbaum 1 gtk_widget_show_all(window);
858     }
859     #endif // USE_STACKABLE_WINDOW
860    
861     #endif // USE_MAEMO