Parent Directory | Revision Log
Some geotoad processing
1 | harbaum | 193 | /* |
2 | * Copyright (C) 2009 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 | |||
22 | harbaum | 206 | #define __USE_GNU |
23 | #include <string.h> | ||
24 | |||
25 | harbaum | 193 | #include <fcntl.h> |
26 | #include <sys/types.h> | ||
27 | #include <sys/wait.h> | ||
28 | #include <errno.h> | ||
29 | harbaum | 198 | #include <math.h> |
30 | harbaum | 193 | |
31 | harbaum | 198 | #if defined(USE_MAEMO) && (MAEMO_VERSION_MAJOR >= 5) |
32 | #include <hildon/hildon-entry.h> | ||
33 | #endif | ||
34 | |||
35 | harbaum | 196 | #define GEOTOAD "/usr/bin/geotoad" |
36 | |||
37 | harbaum | 195 | #define COLOR_ERR "red" |
38 | #define COLOR_OK "darkgreen" | ||
39 | #define COLOR_SYSTEM "darkblue" | ||
40 | harbaum | 194 | |
41 | harbaum | 195 | #define BUFFER_SIZE 256 |
42 | harbaum | 193 | |
43 | harbaum | 206 | typedef enum { |
44 | GT_STATE_ILLEGAL = 0, | ||
45 | GT_STATE_STARTED, | ||
46 | GT_STATE_PREMATURE_END, | ||
47 | GT_STATE_SAVE_FOUND, | ||
48 | } gt_state_t; | ||
49 | |||
50 | harbaum | 193 | typedef struct { |
51 | harbaum | 194 | appdata_t *appdata; |
52 | harbaum | 206 | |
53 | GPtrArray *argv; | ||
54 | gchar *info; | ||
55 | GMutex *update_mutex; | ||
56 | GCond *update_cond; | ||
57 | harbaum | 194 | |
58 | harbaum | 206 | gt_state_t state; |
59 | |||
60 | harbaum | 198 | GtkWidget *dialog; |
61 | |||
62 | harbaum | 206 | long pid; |
63 | harbaum | 193 | |
64 | harbaum | 194 | struct log_s { |
65 | GtkTextBuffer *buffer; | ||
66 | GtkWidget *view; | ||
67 | } log; | ||
68 | harbaum | 196 | |
69 | harbaum | 198 | GtkWidget *username, *password, *filename; |
70 | GtkWidget *lat, *lon, *dst; | ||
71 | harbaum | 194 | |
72 | harbaum | 193 | } gt_context_t; |
73 | |||
74 | harbaum | 206 | static void arg_free(gpointer data, gpointer user_data) { |
75 | if(data) g_free(data); | ||
76 | } | ||
77 | |||
78 | static void context_free(gt_context_t *context) { | ||
79 | printf("freeing context\n"); | ||
80 | |||
81 | if(context->info) g_free(context->info); | ||
82 | |||
83 | g_ptr_array_foreach(context->argv, arg_free, NULL); | ||
84 | g_ptr_array_free (context->argv, TRUE); | ||
85 | |||
86 | g_free(context); | ||
87 | } | ||
88 | |||
89 | harbaum | 194 | static void appendf(struct log_s *log, char *colname, |
90 | const char *fmt, ...) { | ||
91 | va_list args; | ||
92 | va_start( args, fmt ); | ||
93 | char *buf = g_strdup_vprintf(fmt, args); | ||
94 | va_end( args ); | ||
95 | |||
96 | harbaum | 206 | // printf("append: %s", buf); |
97 | harbaum | 195 | |
98 | harbaum | 194 | GtkTextTag *tag = NULL; |
99 | if(colname) | ||
100 | tag = gtk_text_buffer_create_tag(log->buffer, NULL, | ||
101 | "foreground", colname, | ||
102 | NULL); | ||
103 | |||
104 | GtkTextIter end; | ||
105 | gtk_text_buffer_get_end_iter(log->buffer, &end); | ||
106 | if(tag) | ||
107 | gtk_text_buffer_insert_with_tags(log->buffer, &end, buf, -1, tag, NULL); | ||
108 | else | ||
109 | gtk_text_buffer_insert(log->buffer, &end, buf, -1); | ||
110 | |||
111 | g_free(buf); | ||
112 | |||
113 | gtk_text_view_scroll_to_iter(GTK_TEXT_VIEW(log->view), | ||
114 | harbaum | 206 | &end, 0.0, TRUE, 0, 1.0); |
115 | harbaum | 194 | } |
116 | |||
117 | harbaum | 206 | // This function receives a requst from a worker thread asking to |
118 | // update the gui with the required info. | ||
119 | gboolean cb_update_job(gt_context_t *context) { | ||
120 | if (context->info) { | ||
121 | char *color = NULL; | ||
122 | if(strstr(context->info, "Saved to ") != NULL) { | ||
123 | context->state = GT_STATE_SAVE_FOUND; | ||
124 | color = COLOR_OK; | ||
125 | } | ||
126 | harbaum | 193 | |
127 | harbaum | 206 | if(strcasestr(context->info, "error") != NULL) |
128 | color = COLOR_ERR; | ||
129 | harbaum | 193 | |
130 | harbaum | 206 | appendf(&context->log, color, context->info); |
131 | |||
132 | g_free(context->info); | ||
133 | context->info = NULL; | ||
134 | } | ||
135 | |||
136 | // Indicate that the update is done | ||
137 | g_mutex_lock(context->update_mutex); | ||
138 | g_cond_signal(context->update_cond); | ||
139 | g_mutex_unlock(context->update_mutex); | ||
140 | |||
141 | return FALSE; | ||
142 | } | ||
143 | harbaum | 193 | |
144 | harbaum | 206 | // A helper function run in the job thread receiving the string that |
145 | // should be displayed in the textview. | ||
146 | void job_add_to_text_viewer(gt_context_t *context, | ||
147 | const char *info) | ||
148 | { | ||
149 | /* TODO: make this cope with format strings */ | ||
150 | /* TODO: make this cope with colors */ | ||
151 | context->info = g_strdup(info); | ||
152 | |||
153 | // Lock mutex to make sure that we will receive the condition signal | ||
154 | g_mutex_lock(context->update_mutex); | ||
155 | g_idle_add((GSourceFunc)cb_update_job, context); | ||
156 | |||
157 | // Wait for cb_update_job to tell me that the update is done | ||
158 | g_cond_wait(context->update_cond, | ||
159 | context->update_mutex); | ||
160 | g_mutex_unlock(context->update_mutex); | ||
161 | harbaum | 193 | } |
162 | |||
163 | harbaum | 206 | /* custom version of popen to get access to the pid and to be able */ |
164 | /* to slaughter the child process */ | ||
165 | static FILE *gt_popen(char **args, const char *type, long *tid) { | ||
166 | int p[2]; | ||
167 | FILE *fp; | ||
168 | |||
169 | if (*type != 'r' && *type != 'w') | ||
170 | return NULL; | ||
171 | |||
172 | if (pipe(p) < 0) | ||
173 | return NULL; | ||
174 | |||
175 | if ((*tid = fork()) > 0) { /* then we are the parent */ | ||
176 | if (*type == 'r') { | ||
177 | close(p[1]); | ||
178 | fp = fdopen(p[0], type); | ||
179 | } else { | ||
180 | close(p[0]); | ||
181 | fp = fdopen(p[1], type); | ||
182 | } | ||
183 | |||
184 | return fp; | ||
185 | } else if (*tid == 0) { /* we're the child */ | ||
186 | harbaum | 193 | |
187 | harbaum | 206 | /* make our thread id the process group leader */ |
188 | setpgid(0, 0); | ||
189 | harbaum | 193 | |
190 | harbaum | 206 | if (*type == 'r') { |
191 | fflush(stdout); | ||
192 | fflush(stderr); | ||
193 | close(1); | ||
194 | if (dup(p[1]) < 0) | ||
195 | perror("dup of write side of pipe failed"); | ||
196 | close(2); | ||
197 | if (dup(p[1]) < 0) | ||
198 | perror("dup of write side of pipe failed"); | ||
199 | } else { | ||
200 | close(0); | ||
201 | if (dup(p[0]) < 0) | ||
202 | perror("dup of read side of pipe failed"); | ||
203 | harbaum | 193 | } |
204 | harbaum | 206 | |
205 | close(p[0]); /* close since we dup()'ed what we needed */ | ||
206 | close(p[1]); | ||
207 | |||
208 | execve(args[0], args, NULL); | ||
209 | |||
210 | printf("gt_popen(): execve(%s) failed!\n", args[0]); | ||
211 | } else { /* we're having major problems... */ | ||
212 | close(p[0]); | ||
213 | close(p[1]); | ||
214 | printf("gt_popen(): fork() failure!\n"); | ||
215 | } | ||
216 | |||
217 | return NULL; | ||
218 | } | ||
219 | harbaum | 193 | |
220 | harbaum | 206 | // The thread entry point. It will do the job, send the data to the |
221 | // GUI and self destruct when it is done. | ||
222 | static gpointer thread_worker(gt_context_t *context) | ||
223 | { | ||
224 | FILE *fh; | ||
225 | GIOStatus status; | ||
226 | GError *error = NULL; | ||
227 | gsize length; | ||
228 | gsize terminator_pos; | ||
229 | gchar *str_return; | ||
230 | harbaum | 193 | |
231 | harbaum | 206 | fh = gt_popen((char**)(context->argv->pdata),"r", &context->pid); |
232 | if(!fh) { | ||
233 | appendf(&context->log, COLOR_ERR, _("Failed to run GeoToad")); | ||
234 | return NULL; | ||
235 | harbaum | 193 | } |
236 | |||
237 | harbaum | 206 | /* switch to line buffered mode */ |
238 | if(setvbuf(fh, NULL, _IOLBF, 0)) | ||
239 | perror("setvbuf(_IOLBF)"); | ||
240 | |||
241 | GIOChannel *gh = g_io_channel_unix_new(fileno(fh)); | ||
242 | harbaum | 193 | |
243 | harbaum | 206 | while( (status = g_io_channel_read_line(gh, |
244 | &str_return, | ||
245 | &length, | ||
246 | &terminator_pos, | ||
247 | &error)) == G_IO_STATUS_NORMAL) { | ||
248 | job_add_to_text_viewer(context, str_return); | ||
249 | g_free(str_return); | ||
250 | } | ||
251 | |||
252 | g_io_channel_unref(gh); | ||
253 | pclose(fh); | ||
254 | job_add_to_text_viewer(context, "Job done!"); | ||
255 | |||
256 | g_thread_exit(NULL); | ||
257 | context_free(context); | ||
258 | |||
259 | return NULL; | ||
260 | harbaum | 200 | } |
261 | |||
262 | harbaum | 206 | |
263 | harbaum | 200 | static void arg_dsp(gpointer data, gpointer user_data) { |
264 | gt_context_t *context = (gt_context_t*)user_data; | ||
265 | |||
266 | if(data) | ||
267 | appendf(&context->log, COLOR_SYSTEM, "%s\n", data); | ||
268 | } | ||
269 | |||
270 | harbaum | 194 | static void run(gt_context_t *context) { |
271 | harbaum | 206 | GError *error = NULL; |
272 | harbaum | 200 | char str[8]; |
273 | |||
274 | harbaum | 194 | /* build list of arguments to call geotoad */ |
275 | harbaum | 206 | context->argv = g_ptr_array_new(); |
276 | g_ptr_array_add (context->argv, g_strdup_printf(GEOTOAD "a")); | ||
277 | harbaum | 200 | g_ascii_dtostr(str, sizeof(str), context->appdata->gt.distance); |
278 | harbaum | 206 | g_ptr_array_add (context->argv, g_strdup_printf("--distanceMax=%s", str)); |
279 | g_ptr_array_add (context->argv, g_strdup_printf("--output=%s", context->appdata->gt.filename)); | ||
280 | g_ptr_array_add (context->argv, g_strdup_printf("--password=%s", context->appdata->gt.password)); | ||
281 | g_ptr_array_add (context->argv, g_strdup_printf("--queryType=coord")); | ||
282 | g_ptr_array_add (context->argv, g_strdup_printf("--user=%s", context->appdata->username)); | ||
283 | harbaum | 194 | |
284 | /* check if we need to add proxy config */ | ||
285 | char *proxy = NULL; | ||
286 | if(context->appdata->proxy && context->appdata->proxy->host) { | ||
287 | if(context->appdata->proxy->use_authentication && | ||
288 | context->appdata->proxy->authentication_user && | ||
289 | context->appdata->proxy->authentication_password) | ||
290 | proxy = g_strdup_printf("--proxy=http://%s:%s@%s:%d", | ||
291 | harbaum | 206 | context->appdata->proxy->authentication_user, |
292 | context->appdata->proxy->authentication_password, | ||
293 | context->appdata->proxy->host, | ||
294 | context->appdata->proxy->port); | ||
295 | harbaum | 194 | else |
296 | proxy = g_strdup_printf("--proxy=http://%s:%d", | ||
297 | harbaum | 206 | context->appdata->proxy->host, |
298 | context->appdata->proxy->port); | ||
299 | harbaum | 194 | |
300 | harbaum | 206 | g_ptr_array_add (context->argv, proxy); |
301 | harbaum | 194 | } |
302 | |||
303 | harbaum | 206 | /* convert coordinates into simple ascii format */ |
304 | char n = (context->appdata->gt.lat >= 0)?'N':'S'; | ||
305 | char e = (context->appdata->gt.lon >= 0)?'E':'W'; | ||
306 | float lat = fabs(context->appdata->gt.lat); | ||
307 | float lon = fabs(context->appdata->gt.lon); | ||
308 | float lat_mint, lat_int, lat_frac = modff(lat, &lat_int); | ||
309 | float lon_mint, lon_int, lon_frac = modff(lon, &lon_int); | ||
310 | lat_frac = modff(lat_frac*60.0, &lat_mint); | ||
311 | lon_frac = modff(lon_frac*60.0, &lon_mint); | ||
312 | harbaum | 194 | |
313 | harbaum | 206 | g_ptr_array_add (context->argv, |
314 | g_strdup_printf("%c%02u %02u.%03u %c%03u %02u.%03u", | ||
315 | n, (int)lat_int, (int)lat_mint, (int)(lat_frac*1000.0+0.5), | ||
316 | e, (int)lon_int, (int)lon_mint, (int)(lon_frac*1000.0+0.5))); | ||
317 | |||
318 | // g_ptr_array_add (context->argv, g_strdup_printf("2>&1")); | ||
319 | g_ptr_array_add (context->argv, NULL); | ||
320 | |||
321 | harbaum | 200 | /* show all entries */ |
322 | harbaum | 206 | g_ptr_array_foreach(context->argv, arg_dsp, context); |
323 | harbaum | 200 | |
324 | harbaum | 206 | g_thread_create((GThreadFunc)thread_worker, context, FALSE, &error); |
325 | if (error) { | ||
326 | appendf(&context->log, COLOR_ERR, "Error: %s\n", error->message); | ||
327 | harbaum | 193 | g_error_free(error); |
328 | } | ||
329 | } | ||
330 | harbaum | 194 | |
331 | harbaum | 196 | /* show text window and display output of running geotoad */ |
332 | static void gui_run(gt_context_t *context) { | ||
333 | GtkWidget *dialog = gtk_dialog_new_with_buttons(_("GeoToad - Run"), | ||
334 | GTK_WINDOW(context->appdata->window), | ||
335 | harbaum | 194 | GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT, |
336 | GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE, | ||
337 | NULL); | ||
338 | |||
339 | gtk_window_set_default_size(GTK_WINDOW(dialog), 640, 480); | ||
340 | |||
341 | #ifndef USE_PANNABLE_AREA | ||
342 | GtkWidget *scrolled_window = gtk_scrolled_window_new(NULL, NULL); | ||
343 | gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window), | ||
344 | GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC); | ||
345 | #else | ||
346 | GtkWidget *pannable_area = hildon_pannable_area_new(); | ||
347 | #endif | ||
348 | |||
349 | context->log.buffer = gtk_text_buffer_new(NULL); | ||
350 | |||
351 | #ifndef USE_HILDON_TEXT_VIEW | ||
352 | context->log.view = gtk_text_view_new_with_buffer(context->log.buffer); | ||
353 | #else | ||
354 | context->log.view = hildon_text_view_new(); | ||
355 | hildon_text_view_set_buffer(HILDON_TEXT_VIEW(context->log.view), | ||
356 | context->log.buffer); | ||
357 | #endif | ||
358 | |||
359 | #ifndef USE_PANNABLE_AREA | ||
360 | gtk_container_add(GTK_CONTAINER(scrolled_window), context->log.view); | ||
361 | gtk_scrolled_window_set_shadow_type( GTK_SCROLLED_WINDOW(scrolled_window), | ||
362 | GTK_SHADOW_IN); | ||
363 | |||
364 | gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), | ||
365 | scrolled_window, TRUE, TRUE, 0); | ||
366 | #else | ||
367 | gtk_container_add(GTK_CONTAINER(pannable_area), context->log.view); | ||
368 | gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), | ||
369 | pannable_area, TRUE, TRUE, 0); | ||
370 | #endif | ||
371 | |||
372 | gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_CLOSE); | ||
373 | |||
374 | gtk_widget_show_all(dialog); | ||
375 | |||
376 | harbaum | 195 | appendf(&context->log, COLOR_SYSTEM, "Running GeoToad\n"); |
377 | harbaum | 194 | run(context); |
378 | |||
379 | gtk_dialog_run(GTK_DIALOG(dialog)); | ||
380 | |||
381 | gtk_widget_destroy(dialog); | ||
382 | harbaum | 206 | |
383 | errorf("Fertig!\n"); | ||
384 | |||
385 | harbaum | 194 | } |
386 | harbaum | 196 | |
387 | harbaum | 198 | static void on_browse(GtkWidget *widget, gpointer data) { |
388 | gt_context_t *context = (gt_context_t*)data; | ||
389 | |||
390 | printf("Browse %p\n", context->dialog); | ||
391 | |||
392 | #ifdef USE_MAEMO | ||
393 | GtkWidget *dialog = hildon_file_chooser_dialog_new(GTK_WINDOW(context->dialog), | ||
394 | GTK_FILE_CHOOSER_ACTION_SAVE); | ||
395 | #else | ||
396 | GtkWidget *dialog = gtk_file_chooser_dialog_new(_("Save GPX file"), | ||
397 | GTK_WINDOW(context->dialog), | ||
398 | GTK_FILE_CHOOSER_ACTION_SAVE, | ||
399 | GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, | ||
400 | GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT, | ||
401 | NULL); | ||
402 | #endif | ||
403 | |||
404 | printf("set filename <%s>\n", context->appdata->gt.filename); | ||
405 | |||
406 | if(!g_file_test(context->appdata->gt.filename, G_FILE_TEST_EXISTS)) { | ||
407 | char *last_sep = strrchr(context->appdata->gt.filename, '/'); | ||
408 | if(last_sep) { | ||
409 | *last_sep = 0; // seperate path from file | ||
410 | |||
411 | /* the user just created a new document */ | ||
412 | gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), | ||
413 | context->appdata->gt.filename); | ||
414 | gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), last_sep+1); | ||
415 | |||
416 | /* restore full filename */ | ||
417 | *last_sep = '/'; | ||
418 | } | ||
419 | } else | ||
420 | gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dialog), | ||
421 | context->appdata->gt.filename); | ||
422 | |||
423 | if (gtk_dialog_run (GTK_DIALOG(dialog)) == GTK_FM_OK) { | ||
424 | gchar *name = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog)); | ||
425 | gtk_label_set_text(GTK_LABEL(context->filename), name); | ||
426 | } | ||
427 | |||
428 | gtk_widget_destroy (dialog); | ||
429 | } | ||
430 | |||
431 | harbaum | 196 | static gboolean gui_setup(gt_context_t *context) { |
432 | harbaum | 198 | appdata_t *appdata = context->appdata; |
433 | harbaum | 196 | gboolean ok = FALSE; |
434 | |||
435 | harbaum | 198 | /* if no filename has been setup yet, create one */ |
436 | if(!appdata->gt.filename && appdata->path) { | ||
437 | printf("creating path\n"); | ||
438 | appdata->gt.filename = | ||
439 | g_strdup_printf("%s/gtoad.gpx", appdata->path); | ||
440 | } | ||
441 | harbaum | 196 | |
442 | harbaum | 198 | context->dialog = gtk_dialog_new_with_buttons(_("GeoToad - Setup"), |
443 | GTK_WINDOW(appdata->window), | ||
444 | GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT, | ||
445 | GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, | ||
446 | GTK_STOCK_OK, GTK_RESPONSE_OK, | ||
447 | NULL); | ||
448 | |||
449 | /* ------------------- Coordinates ------------------------- */ | ||
450 | harbaum | 196 | GtkWidget *hbox = gtk_hbox_new(FALSE, 0); |
451 | harbaum | 198 | |
452 | harbaum | 196 | GtkWidget *vbox = gtk_vbox_new(FALSE, 0); |
453 | harbaum | 199 | gtk_box_pack_start_defaults(GTK_BOX(vbox), left_label_new(_("Position:"))); |
454 | harbaum | 198 | gtk_box_pack_start_defaults(GTK_BOX(vbox), left_label_new(_("Distance:"))); |
455 | gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE, FALSE, 0); | ||
456 | |||
457 | /* setup default positions */ | ||
458 | pos_t *refpos = get_pos(appdata); | ||
459 | if((isnan(appdata->gt.lat) || isnan(appdata->gt.lat)) && refpos) { | ||
460 | appdata->gt.lat = refpos->lat; | ||
461 | appdata->gt.lon = refpos->lon; | ||
462 | } | ||
463 | |||
464 | vbox = gtk_vbox_new(FALSE, 0); | ||
465 | harbaum | 199 | GtkWidget *ihbox = gtk_hbox_new(FALSE, 0); |
466 | harbaum | 198 | context->lat = lat_entry_new(appdata->gt.lat); |
467 | harbaum | 199 | gtk_box_pack_start_defaults(GTK_BOX(ihbox), context->lat); |
468 | context->lon = lon_entry_new(appdata->gt.lon); | ||
469 | gtk_box_pack_start_defaults(GTK_BOX(ihbox), context->lon); | ||
470 | gtk_box_pack_start_defaults(GTK_BOX(vbox), ihbox); | ||
471 | harbaum | 200 | float dst = appdata->gt.distance; // distance is given in kilometers |
472 | if(appdata->imperial) dst /= 1.609344; | ||
473 | context->dst = dist_entry_new(dst, appdata->imperial); | ||
474 | harbaum | 198 | gtk_box_pack_start_defaults(GTK_BOX(vbox), context->dst); |
475 | harbaum | 196 | gtk_box_pack_start_defaults(GTK_BOX(hbox), vbox); |
476 | harbaum | 198 | |
477 | gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(context->dialog)->vbox), hbox); | ||
478 | |||
479 | /* ------------------- file name ------------------------- */ | ||
480 | hbox = gtk_hbox_new(FALSE, 0); | ||
481 | |||
482 | context->filename = gtk_label_new(appdata->gt.filename); | ||
483 | gtk_misc_set_alignment(GTK_MISC(context->filename), 0.f, 0.5f); | ||
484 | gtk_label_set_ellipsize(GTK_LABEL(context->filename), PANGO_ELLIPSIZE_MIDDLE); | ||
485 | gtk_box_pack_start_defaults(GTK_BOX(hbox), context->filename); | ||
486 | |||
487 | GtkWidget *button = gtk_button_new_with_label(_("Browse")); | ||
488 | harbaum | 199 | #if defined(USE_MAEMO) && (MAEMO_VERSION_MAJOR == 5) |
489 | hildon_gtk_widget_set_theme_size(button, | ||
490 | (HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH)); | ||
491 | #endif | ||
492 | harbaum | 198 | gtk_signal_connect(GTK_OBJECT(button), "clicked", |
493 | GTK_SIGNAL_FUNC(on_browse), context); | ||
494 | gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0); | ||
495 | |||
496 | gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(context->dialog)->vbox), hbox); | ||
497 | |||
498 | |||
499 | /* ------------------- Username/Password ------------------------- */ | ||
500 | hbox = gtk_hbox_new(FALSE, 0); | ||
501 | vbox = gtk_vbox_new(FALSE, 0); | ||
502 | gtk_box_pack_start_defaults(GTK_BOX(vbox), left_label_new(_("Username:"))); | ||
503 | gtk_box_pack_start_defaults(GTK_BOX(vbox), left_label_new(_("Password:"))); | ||
504 | gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE, FALSE, 0); | ||
505 | harbaum | 196 | |
506 | vbox = gtk_vbox_new(FALSE, 0); | ||
507 | harbaum | 206 | #ifndef FREMANTLE |
508 | harbaum | 196 | context->username = gtk_entry_new(); |
509 | context->password = gtk_entry_new(); | ||
510 | #else | ||
511 | context->username = hildon_entry_new(HILDON_SIZE_AUTO); | ||
512 | hildon_gtk_entry_set_input_mode(GTK_ENTRY(context->username), | ||
513 | HILDON_GTK_INPUT_MODE_FULL); | ||
514 | context->password = hildon_entry_new(HILDON_SIZE_AUTO); | ||
515 | hildon_gtk_entry_set_input_mode(GTK_ENTRY(context->password), | ||
516 | HILDON_GTK_INPUT_MODE_FULL); | ||
517 | #endif | ||
518 | gtk_entry_set_visibility(GTK_ENTRY(context->password), FALSE); | ||
519 | |||
520 | /* set saved defaults */ | ||
521 | harbaum | 204 | if(appdata->username) |
522 | harbaum | 196 | gtk_entry_set_text(GTK_ENTRY(context->username), |
523 | harbaum | 204 | appdata->username); |
524 | harbaum | 196 | |
525 | harbaum | 198 | if(appdata->gt.password) |
526 | harbaum | 196 | gtk_entry_set_text(GTK_ENTRY(context->password), |
527 | harbaum | 198 | appdata->gt.password); |
528 | harbaum | 196 | |
529 | gtk_box_pack_start_defaults(GTK_BOX(vbox), context->username); | ||
530 | gtk_box_pack_start_defaults(GTK_BOX(vbox), context->password); | ||
531 | gtk_box_pack_start_defaults(GTK_BOX(hbox), vbox); | ||
532 | |||
533 | harbaum | 198 | gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(context->dialog)->vbox), hbox); |
534 | harbaum | 196 | |
535 | harbaum | 198 | gtk_dialog_set_default_response(GTK_DIALOG(context->dialog), GTK_RESPONSE_OK); |
536 | harbaum | 196 | |
537 | harbaum | 198 | gtk_widget_show_all(context->dialog); |
538 | harbaum | 196 | |
539 | harbaum | 198 | if(gtk_dialog_run(GTK_DIALOG(context->dialog)) == GTK_RESPONSE_OK) { |
540 | harbaum | 196 | |
541 | harbaum | 200 | /* parse coordinates */ |
542 | harbaum | 206 | appdata->gt.lat = lat_get(context->lat); |
543 | appdata->gt.lon = lon_get(context->lon); | ||
544 | harbaum | 200 | |
545 | harbaum | 196 | /* save values */ |
546 | harbaum | 204 | if(appdata->username) g_free(appdata->username); |
547 | appdata->username = | ||
548 | harbaum | 196 | g_strdup(gtk_entry_get_text(GTK_ENTRY(context->username))); |
549 | |||
550 | harbaum | 198 | if(appdata->gt.password) g_free(appdata->gt.password); |
551 | appdata->gt.password = | ||
552 | harbaum | 196 | g_strdup(gtk_entry_get_text(GTK_ENTRY(context->password))); |
553 | |||
554 | harbaum | 198 | if(appdata->gt.filename) g_free(appdata->gt.filename); |
555 | appdata->gt.filename = | ||
556 | g_strdup(gtk_label_get_text(GTK_LABEL(context->filename))); | ||
557 | |||
558 | harbaum | 200 | /* get distance in kilometers */ |
559 | appdata->gt.distance = dist_get(context->dst, FALSE); | ||
560 | |||
561 | harbaum | 206 | |
562 | /* check for valid entries */ | ||
563 | if(isnan(appdata->gt.lat) || isnan(appdata->gt.lon) || | ||
564 | isnan(appdata->gt.distance) || !appdata->gt.filename || | ||
565 | !appdata->username || !appdata->gt.password) | ||
566 | errorf(_("GeoToad setup not complete")); | ||
567 | else | ||
568 | ok = TRUE; | ||
569 | harbaum | 196 | } |
570 | |||
571 | harbaum | 198 | gtk_widget_destroy(context->dialog); |
572 | harbaum | 196 | |
573 | return ok; | ||
574 | } | ||
575 | |||
576 | void geotoad(appdata_t *appdata) { | ||
577 | if(!geotoad_available()) { | ||
578 | errorf(_("GeoToad is not installed on this device.\n" | ||
579 | "You need to install it in order to be able to use it.")); | ||
580 | return; | ||
581 | } | ||
582 | |||
583 | gt_context_t *context = g_new0(gt_context_t, 1); | ||
584 | context->appdata = appdata; | ||
585 | |||
586 | harbaum | 206 | context->update_mutex = g_mutex_new(); |
587 | context->update_cond = g_cond_new(); | ||
588 | // context->mutex_to_run = mutex_to_run; | ||
589 | |||
590 | harbaum | 196 | printf("geoToad\n"); |
591 | |||
592 | if(gui_setup(context)) | ||
593 | gui_run(context); | ||
594 | } | ||
595 | |||
596 | gboolean geotoad_available(void) { | ||
597 | /* before doing anything make sure geotoad is installed */ | ||
598 | return g_file_test(GEOTOAD, G_FILE_TEST_IS_EXECUTABLE); | ||
599 | } | ||
600 | |||
601 |