Contents of /trunk/src/cache.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 159 - (hide annotations)
Wed Nov 4 20:28:54 2009 UTC (14 years, 7 months ago) by harbaum
File MIME type: text/plain
File size: 28141 byte(s)
More gcvote work
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     printf("gcvote callback for failed request\n");
148    
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     printf("gcvote callback is being called with a %d/%d\n",
155     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 158 context->gcvote_request =
268     gcvote_request(appdata, gcvote_callback, cache->url, context);
269 harbaum 156
270 harbaum 159 context->votebox = gtk_vbox_new(FALSE, 0);
271     GtkWidget *align = gtk_alignment_new(0.5, 0.5, 0.0, 0.0);
272     gtk_container_add(GTK_CONTAINER(align), context->votebox);
273     gtk_box_pack_start_defaults(GTK_BOX(ratebox), align);
274 harbaum 156
275     gtk_table_attach_defaults(GTK_TABLE(table), ratebox, 2,3,0,2);
276    
277     /* ----------------------------------------------------- */
278    
279    
280 harbaum 1 /* ----------------- the two coordinates ----------------- */
281     /* ----------------- and the heading/distance ------------ */
282     pos_t *refpos = get_pos(appdata);
283    
284     ivbox = gtk_vbox_new(FALSE, 0);
285     int strike = (cache->notes && cache->notes->override)?
286     STRIKETHROUGH:STRIKETHROUGH_NONE;
287    
288     /* the original coordinate is being displayed */
289     gtk_box_pack_start_defaults(GTK_BOX(ivbox),
290     context->pos_lat_label = pos_lat(cache->pos.lat, SIZE_BIG, strike));
291     gtk_box_pack_start_defaults(GTK_BOX(ivbox),
292     context->pos_lon_label = pos_lon(cache->pos.lon, SIZE_BIG, strike));
293    
294     /* but calculations may be done with respect to the overriden one */
295 harbaum 13 if(!isnan(cache->pos.lat) && !isnan(cache->pos.lon)) {
296     context->bearing_hbox = gtk_hbox_new(FALSE, 0);
297     bearing_fill_hbox(context->bearing_hbox, appdata, *refpos,
298     gpx_cache_pos(cache));
299     gtk_box_pack_start_defaults(GTK_BOX(ivbox), context->bearing_hbox);
300     }
301 harbaum 1
302     gtk_table_attach_defaults(GTK_TABLE(table), ivbox, 3,4,0,2);
303    
304 harbaum 157 /* ----------------------------------------------------- */
305    
306 harbaum 1 gtk_box_pack_start(GTK_BOX(vbox), table, 0,0,0);
307     gtk_box_pack_start(GTK_BOX(vbox), gtk_hseparator_new(),FALSE,FALSE,0);
308    
309 harbaum 156 /* ----------------------------------------------------- */
310    
311 harbaum 1 if(cache->short_description)
312     gtk_box_pack_start_defaults(GTK_BOX(vbox),
313     html_view(appdata, cache->short_description,
314 harbaum 140 cache->short_is_html?HTML_HTML:HTML_PLAIN_TEXT, TRUE, cache, NULL));
315 harbaum 1
316     return vbox;
317     }
318    
319     /* slow but short, we don't need performance here ... */
320     static void rot13(char *t) {
321 harbaum 55 int braces = 0;
322    
323 harbaum 1 while(*t) {
324 harbaum 55 if(!braces) {
325     if(*t == '[')
326     braces++;
327     else if(((*t >= 'a') && (*t < 'n')) ||
328     ((*t >= 'A') && (*t < 'N'))) *t += 13;
329     else if(((*t >= 'n') && (*t <= 'z')) ||
330     ((*t >= 'N') && (*t <= 'Z'))) *t -= 13;
331     } else {
332     if(braces > 0 && *t == ']')
333     braces--;
334     }
335 harbaum 1
336     t++;
337     }
338     }
339    
340     static void on_decrypt(GtkWidget *widget, gpointer data) {
341     /* data is a link to the textview */
342     g_assert(GTK_IS_TEXT_VIEW(data));
343    
344     GtkTextIter start, end;
345     GtkTextBuffer *buffer = gtk_text_view_get_buffer((GtkTextView*)data);
346    
347     gtk_text_buffer_get_start_iter(buffer, &start);
348     gtk_text_buffer_get_end_iter(buffer, &end);
349     char *text = gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
350    
351     rot13(text);
352     gtk_text_buffer_set_text(buffer, text, -1);
353    
354     free(text);
355     }
356    
357     static GtkWidget *cache_hint(appdata_t *appdata, cache_t *cache) {
358     /* encrypting/decrypting html is nothing we want to do */
359     if(cache->hint_is_html)
360 harbaum 140 return html_view(appdata, cache->hint, HTML_HTML, TRUE, NULL, NULL);
361 harbaum 1
362     /* we can now be sure that we are talking about pain text */
363     GtkWidget *vbox = gtk_vbox_new(FALSE, 0);
364    
365     char *hint = strdup(cache->hint);
366     rot13(hint);
367 harbaum 140 GtkWidget *view =
368     html_view(appdata, hint, HTML_PLAIN_TEXT, TRUE, NULL, NULL);
369 harbaum 1 gtk_box_pack_start_defaults(GTK_BOX(vbox), view);
370     free(hint);
371    
372     GtkWidget *button = gtk_button_new_with_label(_("Encrypt/Decrypt"));
373 harbaum 66 #if defined(USE_MAEMO) && (MAEMO_VERSION_MAJOR == 5)
374     hildon_gtk_widget_set_theme_size(button,
375     (HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH));
376     #endif
377 harbaum 1 gtk_box_pack_start(GTK_BOX(vbox), button, FALSE, FALSE, 0);
378     gtk_signal_connect(GTK_OBJECT(button), "clicked",
379     GTK_SIGNAL_FUNC(on_decrypt), gtk_bin_get_child(GTK_BIN(view)));
380    
381     return vbox;
382     }
383    
384     static GtkWidget *cache_wpts(appdata_t *appdata, wpt_t *wpt) {
385     pos_t *refpos = NULL;
386    
387     #ifndef USE_PANNABLE_AREA
388     GtkWidget *scrolled_window = gtk_scrolled_window_new(NULL, NULL);
389     gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window),
390     GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
391     #else
392     GtkWidget *pannable_area = hildon_pannable_area_new();
393     #endif
394    
395     GtkWidget *vbox = gtk_vbox_new(FALSE, 0);
396    
397     /* four rows per waypoint */
398     GtkWidget *table = gtk_table_new(4*gpx_number_of_waypoints(wpt)-1,4, FALSE);
399    
400     refpos = get_pos(appdata);
401    
402     int wpt_row=0;
403     while(wpt) {
404     GtkWidget *ihbox, *tip;
405     char str[32];
406    
407     /* ----------------------- icon/id ---------------------------- */
408     ihbox = gtk_hbox_new(FALSE, 0);
409    
410     if(wpt->sym != WPT_SYM_UNKNOWN) {
411     gtk_box_pack_start(GTK_BOX(ihbox),
412     tip = icon_get_widget(ICON_WPT, wpt->sym), 1,0,0);
413     }
414    
415     if(wpt->id)
416     gtk_box_pack_start(GTK_BOX(ihbox), GTK_LABEL_BIG(wpt->id), 1,0,0);
417    
418     gtk_table_attach_defaults(GTK_TABLE(table), ihbox, 0,1,wpt_row, wpt_row+1);
419    
420     /* ----------------- the two coordinates ----------------- */
421     /* ----------------- and the heading/distance ------------ */
422     gtk_table_attach_defaults(GTK_TABLE(table),
423     pos_lat(wpt->pos.lat, SIZE_BIG, STRIKETHROUGH_NONE),
424     1,2, wpt_row, wpt_row+1);
425     gtk_table_attach_defaults(GTK_TABLE(table),
426     pos_lon(wpt->pos.lon, SIZE_BIG, STRIKETHROUGH_NONE),
427     2,3, wpt_row, wpt_row+1);
428    
429     ihbox = gtk_hbox_new(FALSE, 0);
430     gtk_box_pack_start(GTK_BOX(ihbox), gtk_image_new_from_pixbuf(
431     icon_bearing(*refpos, wpt->pos)),1,0,0);
432     gtk_box_pack_start_defaults(GTK_BOX(ihbox),
433     GTK_LABEL_SMALL((char*)pos_get_bearing_str(*refpos, wpt->pos)));
434     snprintf(str, sizeof(str), _("%.1f°"),
435     gpx_pos_get_bearing(*refpos, wpt->pos));
436     gtk_box_pack_start_defaults(GTK_BOX(ihbox), GTK_LABEL_SMALL(str));
437     gpx_pos_get_distance_str(str, sizeof(str),
438     *refpos, wpt->pos, appdata->imperial);
439     gtk_box_pack_start(GTK_BOX(ihbox), GTK_LABEL_SMALL(str),1,0,0);
440    
441     gtk_table_attach_defaults(GTK_TABLE(table), ihbox, 3,4,
442     wpt_row+0, wpt_row+1);
443    
444     /* ------------------ description ------------------------- */
445     if(wpt->desc) {
446     GtkTextBuffer *buffer = gtk_text_buffer_new(NULL);
447     gtk_text_buffer_set_text(buffer, wpt->desc, strlen(wpt->desc));
448    
449 harbaum 8 #ifndef USE_HILDON_TEXT_VIEW
450 harbaum 1 GtkWidget *textview = gtk_text_view_new_with_buffer(buffer);
451     #else
452     GtkWidget *textview = hildon_text_view_new();
453     hildon_text_view_set_buffer(HILDON_TEXT_VIEW(textview), buffer);
454     #endif
455    
456     gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(textview), GTK_WRAP_WORD);
457     gtk_text_view_set_editable(GTK_TEXT_VIEW(textview), FALSE);
458     gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(textview), FALSE);
459    
460     gtk_table_attach_defaults(GTK_TABLE(table), textview, 0,4,
461     wpt_row+1, wpt_row+2);
462     }
463    
464     /* ------------------ comment ------------------------- */
465     if(wpt->cmt) {
466     GtkTextBuffer *buffer = gtk_text_buffer_new(NULL);
467     gtk_text_buffer_set_text(buffer, wpt->cmt, strlen(wpt->cmt));
468 harbaum 8 #ifndef USE_HILDON_TEXT_VIEW
469 harbaum 1 GtkWidget *textview = gtk_text_view_new_with_buffer(buffer);
470     #else
471     GtkWidget *textview = hildon_text_view_new();
472     hildon_text_view_set_buffer(HILDON_TEXT_VIEW(textview), buffer);
473     #endif
474     gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(textview), GTK_WRAP_WORD);
475     gtk_text_view_set_editable(GTK_TEXT_VIEW(textview), FALSE);
476     gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(textview), FALSE);
477    
478     gtk_table_attach_defaults(GTK_TABLE(table), textview, 0,4,
479     wpt_row+2, wpt_row+3);
480     }
481    
482     /* --------------------- seperator -------------------------*/
483     if(wpt->next)
484     gtk_table_attach_defaults(GTK_TABLE(table), gtk_hseparator_new(), 0,4,
485     wpt_row+3, wpt_row+4);
486    
487    
488     wpt_row+=4;
489     wpt = wpt->next;
490     }
491    
492     gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
493    
494     #ifndef USE_PANNABLE_AREA
495     gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scrolled_window),
496     vbox);
497     return scrolled_window;
498     #else
499     hildon_pannable_area_add_with_viewport(HILDON_PANNABLE_AREA(pannable_area),
500     vbox);
501     return pannable_area;
502     #endif
503     }
504    
505     static GtkWidget *cache_tbs(appdata_t *appdata, tb_t *tb) {
506     pos_t *refpos = NULL;
507    
508     #ifndef USE_PANNABLE_AREA
509     GtkWidget *scrolled_window = gtk_scrolled_window_new(NULL, NULL);
510     gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window),
511     GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
512     #else
513     GtkWidget *pannable_area = hildon_pannable_area_new();
514     #endif
515    
516     GtkWidget *vbox = gtk_vbox_new(FALSE, 0);
517    
518     /* four rows per waypoint */
519     GtkWidget *table = gtk_table_new(2*gpx_number_of_tbs(tb)-1,3, FALSE);
520    
521     refpos = get_pos(appdata);
522    
523     int tb_row=0;
524     while(tb) {
525 harbaum 137 static const char *tb_type = "track/details.aspx";
526    
527 harbaum 1 /* --------------------- icon/ref/name -------------------------*/
528     gtk_table_attach_defaults(GTK_TABLE(table), icon_get_widget(ICON_TB, 0),
529     0, 1, tb_row+0, tb_row+1);
530 harbaum 133
531 harbaum 137
532 harbaum 133 if(tb->ref) {
533 harbaum 137 GtkWidget *ref = link_button_by_id(appdata, tb->ref, tb_type, tb->id);
534 harbaum 133 gtk_table_attach_defaults(GTK_TABLE(table), ref,
535 harbaum 1 1, 2, tb_row+0, tb_row+1);
536 harbaum 133 }
537    
538 harbaum 1 if(tb->name)
539     gtk_table_attach_defaults(GTK_TABLE(table), GTK_LABEL_BIG(tb->name),
540     2, 3, tb_row+0, tb_row+1);
541    
542     /* --------------------- seperator -------------------------*/
543     if(tb->next)
544     gtk_table_attach_defaults(GTK_TABLE(table), gtk_hseparator_new(), 0, 3,
545     tb_row+1, tb_row+2);
546     tb_row+=2;
547     tb = tb->next;
548     }
549    
550     gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
551    
552     #ifndef USE_PANNABLE_AREA
553     gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scrolled_window),
554     vbox);
555     return scrolled_window;
556     #else
557     hildon_pannable_area_add_with_viewport(HILDON_PANNABLE_AREA(pannable_area),
558     vbox);
559     return pannable_area;
560     #endif
561     }
562    
563 harbaum 66 #ifdef ENABLE_BROWSER_INTERFACE
564     static void on_gclink_clicked(GtkButton *button, gpointer data) {
565     cache_context_t *context = (cache_context_t*)data;
566     char *url = g_strdup_printf("http://www.geocaching.com/seek/log.aspx?wp=%s", context->cache->id);
567     browser_url(context->appdata, url);
568     g_free(url);
569     }
570     #endif
571    
572     static GtkWidget *cache_logs(appdata_t *appdata, cache_context_t *context, log_t *log, int is_html) {
573 harbaum 1 #ifndef USE_PANNABLE_AREA
574     /* put this inside a scrolled view */
575     GtkWidget *scrolled_window = gtk_scrolled_window_new(NULL, NULL);
576     gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window),
577     GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
578     #else
579     GtkWidget *pannable_area = hildon_pannable_area_new();
580     #endif
581    
582 harbaum 66 #ifdef ENABLE_BROWSER_INTERFACE
583     gboolean gc_link = strncmp(context->cache->id, "GC", 2) == 0;
584     #else
585     #define gc_link (FALSE)
586     #endif
587    
588     GtkWidget *table = gtk_table_new(4*gpx_number_of_logs(log)+(gc_link?1:0), 3, FALSE);
589 harbaum 1 int cnt = 0;
590 harbaum 66
591     #ifdef ENABLE_BROWSER_INTERFACE
592     if(gc_link) {
593     GtkWidget *but = gtk_button_new_with_label(_("Post a new log entry for this geocache"));
594     #if defined(USE_MAEMO) && (MAEMO_VERSION_MAJOR == 5)
595     hildon_gtk_widget_set_theme_size(but,
596     (HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH));
597     #endif
598     gtk_signal_connect(GTK_OBJECT(but), "clicked",
599     GTK_SIGNAL_FUNC(on_gclink_clicked), context);
600    
601     gtk_table_attach_defaults(GTK_TABLE(table), but, 0, 3, 0, 1);
602     cnt++;
603     }
604     #endif
605 harbaum 1
606     /* add all logs to the vbox */
607     while(log) {
608     gtk_table_attach_defaults(GTK_TABLE(table), gtk_hseparator_new(),
609     0, 3, cnt+0, cnt+1);
610 harbaum 138 #if 0
611     static const char *log_type = "seek/log.aspx";
612     GtkWidget *log_but =
613     link_icon_button_by_id(appdata, icon_get_widget(ICON_LOG, log->type),
614     log_type, log->id);
615     gtk_table_attach(GTK_TABLE(table), log_but,
616     0, 1, cnt+1, cnt+2, FALSE, FALSE, 0, 0);
617     #else
618 harbaum 1 gtk_table_attach_defaults(GTK_TABLE(table),
619 harbaum 138 icon_get_widget(ICON_LOG, log->type), 0, 1, cnt+1, cnt+2);
620     #endif
621 harbaum 1
622     char date_str[32];
623     if(log->day && log->month && log->year) {
624     GDate *date = g_date_new_dmy(log->day, log->month, log->year);
625     g_date_strftime(date_str, sizeof(date_str), "%x", date);
626     g_date_free(date);
627     } else
628     strcpy(date_str, "---");
629    
630     gtk_table_attach_defaults(GTK_TABLE(table), gtk_label_new(date_str),
631     1, 2, cnt+1, cnt+2);
632    
633 harbaum 137 static const char *finder_type = "profile/";
634     GtkWidget *finder = link_button_by_id(appdata, log->finder->name,
635     finder_type, log->finder->id);
636    
637     gtk_table_attach(GTK_TABLE(table), finder,
638     2, 3, cnt+1, cnt+2, FALSE, FALSE, 0, 0);
639    
640 harbaum 1 gtk_table_attach_defaults(GTK_TABLE(table), gtk_hseparator_new(),
641     0, 3, cnt+2, cnt+3);
642    
643     if(log->text) {
644     gtk_table_attach_defaults(GTK_TABLE(table),
645 harbaum 140 html_view(appdata, log->text,
646     is_html?HTML_HTML:HTML_CUSTOM_MARKUP, FALSE, NULL, NULL),
647     0, 3, cnt+3, cnt+4);
648 harbaum 1 }
649 harbaum 140
650 harbaum 1 log = log->next;
651     cnt+=4;
652     }
653    
654     #ifndef USE_PANNABLE_AREA
655     gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scrolled_window),
656     table);
657     return scrolled_window;
658     #else
659     hildon_pannable_area_add_with_viewport(HILDON_PANNABLE_AREA(pannable_area),
660     table);
661     return pannable_area;
662     #endif
663     }
664    
665     #ifdef USE_MAEMO
666     /* this routine is called once a second as long as the "goto" tab is visible */
667     static gboolean screensaver_update(gpointer data) {
668     appdata_t *appdata = (appdata_t*)data;
669    
670     if(appdata->goto_disable_screensaver)
671     if (osso_display_blanking_pause(appdata->osso_context) != OSSO_OK)
672     fprintf(stderr, "error with display blank\n");
673    
674     return TRUE; // fire again
675     }
676     #endif
677    
678     static void on_notebook_page_change(GtkNotebook *notebook,
679     GtkNotebookPage *page,
680     guint page_num,
681     gpointer user_data) {
682    
683     cache_context_t *context = (cache_context_t*)user_data;
684     GtkWidget *w = gtk_notebook_get_nth_page(notebook, page_num);
685     const char *name = gtk_notebook_get_tab_label_text(notebook, w);
686    
687     #ifdef USE_MAEMO
688     if(context->handler_id)
689     gtk_timeout_remove(context->handler_id);
690     #endif
691    
692     /* this is a workaround, around some bug in the gtktextwidget or so ... */
693     /* i tried to get info on this and everybody agreed that this is a bug */
694 harbaum 136 /* in gtk but noone had a fix ready. so i came up with this. */
695 harbaum 1 /* seems to work ... */
696     if(strcasecmp(name, _("Logs")) == 0) {
697     gtk_widget_queue_resize(w);
698 harbaum 136 } else if(strcasecmp(name, _("TBs")) == 0) {
699     gtk_widget_queue_resize(w);
700     } else if(strcasecmp(name, _("Goto")) == 0) {
701 harbaum 1 #ifdef USE_MAEMO
702     context->handler_id = gtk_timeout_add(1000, screensaver_update,
703     context->appdata);
704     #endif
705     goto_coordinate_update(context);
706     }
707    
708     if(strcasecmp(name, _("Main")) == 0) {
709     /* the notes page may have changed its "override" setting, thus the */
710     /* striked out coordinate may need update */
711     overview_coordinate_update(context);
712     }
713     }
714    
715     static void on_notebook_destroy(GtkWidget *widget, gpointer user_data ) {
716     cache_context_t *context = (cache_context_t*)user_data;
717    
718     printf("destroying notebook\n");
719    
720 harbaum 159 /* cancel a pending gcvote request */
721     if(context->gcvote_request) {
722     gcvote_request_free(context->gcvote_request);
723     context->gcvote_request = NULL;
724     }
725    
726 harbaum 1 notes_destroy_event(NULL, context);
727     goto_destroy_event(NULL, context);
728    
729     #ifdef USE_MAEMO
730     if(context->handler_id)
731     gtk_timeout_remove(context->handler_id);
732     #endif
733    
734     free(user_data);
735     }
736    
737     GtkWidget *cache_view(appdata_t *appdata, cache_t *cache) {
738     GtkWidget *notebook;
739    
740     cache_context_t *cache_context = malloc(sizeof(cache_context_t));
741     memset(cache_context, 0, sizeof(cache_context_t));
742     cache_context->appdata = appdata;
743     cache_context->cache = cache;
744    
745     #ifdef USE_MAEMO
746     #define TAB_DESC _("Desc.")
747     #define TAB_WPTS _("Wpts")
748     #else
749     #define TAB_DESC _("Description")
750     #define TAB_WPTS _("Waypoints")
751     #endif
752    
753     notebook = gtk_notebook_new();
754 harbaum 133
755 harbaum 1 #ifdef USE_MAEMO
756 harbaum 11 #if MAEMO_VERSION_MAJOR >= 5
757 harbaum 1 /* prevents user from accidentially touching the breadcrumb trail */
758     gtk_notebook_set_tab_pos(GTK_NOTEBOOK(notebook), GTK_POS_BOTTOM);
759     #endif
760     #endif
761    
762     gtk_notebook_append_page(GTK_NOTEBOOK(notebook),
763     cache_overview(cache_context), gtk_label_new(_("Main")));
764    
765     if(cache->long_description)
766     gtk_notebook_append_page(GTK_NOTEBOOK(notebook),
767     cache_description(appdata, cache), gtk_label_new(TAB_DESC));
768    
769     if(cache->hint)
770     gtk_notebook_append_page(GTK_NOTEBOOK(notebook),
771     cache_hint(appdata, cache), gtk_label_new(_("Hint")));
772    
773     if(cache->log)
774     gtk_notebook_append_page(GTK_NOTEBOOK(notebook),
775 harbaum 66 cache_logs(appdata, cache_context, cache->log, cache->logs_are_html),
776 harbaum 1 gtk_label_new(_("Logs")));
777    
778     if(cache->wpt)
779     gtk_notebook_append_page(GTK_NOTEBOOK(notebook),
780     cache_wpts(appdata, cache->wpt), gtk_label_new(TAB_WPTS));
781    
782     if(cache->tb)
783     gtk_notebook_append_page(GTK_NOTEBOOK(notebook),
784     cache_tbs(appdata, cache->tb), gtk_label_new(_("TBs")));
785    
786     gtk_notebook_append_page(GTK_NOTEBOOK(notebook),
787     cache_notes(cache_context), gtk_label_new(_("Notes")));
788    
789     gtk_notebook_append_page(GTK_NOTEBOOK(notebook),
790     goto_cache(cache_context), gtk_label_new(_("Goto")));
791    
792     g_signal_connect(G_OBJECT(notebook), "switch-page",
793     G_CALLBACK(on_notebook_page_change), cache_context);
794    
795     g_signal_connect(G_OBJECT(notebook), "destroy",
796     G_CALLBACK(on_notebook_destroy), cache_context);
797    
798     return notebook;
799     }
800    
801     #ifndef USE_MAEMO
802     void cache_dialog(appdata_t *appdata, cache_t *cache) {
803     GtkWidget *dialog = gtk_dialog_new_with_buttons(cache->name,
804     GTK_WINDOW(appdata->window),
805     GTK_DIALOG_NO_SEPARATOR | GTK_DIALOG_MODAL |
806     GTK_DIALOG_DESTROY_WITH_PARENT,
807     GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE,
808     NULL);
809    
810     gtk_window_set_default_size(GTK_WINDOW(dialog), DIALOG_WIDTH, DIALOG_HEIGHT);
811    
812     /* create cache visualization widget */
813     gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox),
814     cache_view(appdata, cache));
815    
816     gtk_widget_show_all(dialog);
817     gtk_dialog_run(GTK_DIALOG(dialog));
818     gtk_widget_destroy(dialog);
819     }
820    
821     #else
822     #ifdef USE_STACKABLE_WINDOW
823 harbaum 11 static void on_cache_destroy (GtkWidget *widget, appdata_t *appdata) {
824     appdata->cur_cache = NULL;
825    
826     /* restore cur_view */
827     appdata->cur_view = g_object_get_data(G_OBJECT(widget), "cur_view");
828     }
829    
830 harbaum 1 void cache_dialog(appdata_t *appdata, cache_t *cache) {
831     GtkWidget *window = hildon_stackable_window_new();
832    
833 harbaum 11 /* store last "cur_view" in window */
834     g_object_set_data(G_OBJECT(window), "cur_view", appdata->cur_view);
835    
836     appdata->cur_cache = cache;
837 harbaum 34 char *title = g_strdup_printf("%s - GPXView", cache->name);
838 harbaum 6 gtk_window_set_title(GTK_WINDOW(window), title);
839     g_free(title);
840 harbaum 1
841     /* create cache visualization widget */
842 harbaum 11 appdata->cur_view = cache_view(appdata, cache);
843     gtk_container_add(GTK_CONTAINER(window), appdata->cur_view);
844 harbaum 1
845 harbaum 3 hildon_window_set_app_menu(HILDON_WINDOW(window),
846     menu_create(appdata, MENU_CACHE));
847    
848 harbaum 11 g_signal_connect(G_OBJECT(window), "destroy",
849     G_CALLBACK(on_cache_destroy), appdata);
850    
851 harbaum 1 gtk_widget_show_all(window);
852     }
853     #endif // USE_STACKABLE_WINDOW
854    
855     #endif // USE_MAEMO