Contents of /trunk/src/geotoad.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 198 - (hide annotations)
Thu Nov 19 12:38:03 2009 UTC (14 years, 7 months ago) by harbaum
File MIME type: text/plain
File size: 15466 byte(s)
more geotoad
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     #include <fcntl.h>
23     #include <sys/types.h>
24     #include <sys/wait.h>
25     #include <errno.h>
26 harbaum 198 #include <math.h>
27 harbaum 193
28 harbaum 198 #if defined(USE_MAEMO) && (MAEMO_VERSION_MAJOR >= 5)
29     #include <hildon/hildon-entry.h>
30     #endif
31    
32 harbaum 196 #define GEOTOAD "/usr/bin/geotoad"
33    
34 harbaum 195 #define COLOR_ERR "red"
35     #define COLOR_OK "darkgreen"
36     #define COLOR_SYSTEM "darkblue"
37 harbaum 194
38 harbaum 195 #define BUFFER_SIZE 256
39 harbaum 193
40     typedef struct {
41 harbaum 194 appdata_t *appdata;
42    
43 harbaum 198 GtkWidget *dialog;
44    
45 harbaum 193 char buf[BUFFER_SIZE];
46     int bused;
47    
48     /** gdk input tag for async IO */
49     gint stdout_tag, stderr_tag;
50     gint stdin_fd, stdout_fd, stderr_fd;
51    
52 harbaum 194 struct log_s {
53     GtkTextBuffer *buffer;
54     GtkWidget *view;
55     } log;
56 harbaum 196
57 harbaum 198 GtkWidget *username, *password, *filename;
58     GtkWidget *lat, *lon, *dst;
59 harbaum 194
60 harbaum 193 } gt_context_t;
61    
62 harbaum 194 static void appendf(struct log_s *log, char *colname,
63     const char *fmt, ...) {
64     va_list args;
65     va_start( args, fmt );
66     char *buf = g_strdup_vprintf(fmt, args);
67     va_end( args );
68    
69 harbaum 195 printf("append: %s", buf);
70    
71 harbaum 194 GtkTextTag *tag = NULL;
72     if(colname)
73     tag = gtk_text_buffer_create_tag(log->buffer, NULL,
74     "foreground", colname,
75     NULL);
76    
77     GtkTextIter end;
78     gtk_text_buffer_get_end_iter(log->buffer, &end);
79     if(tag)
80     gtk_text_buffer_insert_with_tags(log->buffer, &end, buf, -1, tag, NULL);
81     else
82     gtk_text_buffer_insert(log->buffer, &end, buf, -1);
83    
84     g_free(buf);
85    
86 harbaum 195 gtk_text_buffer_get_end_iter(log->buffer, &end);
87 harbaum 194 gtk_text_view_scroll_to_iter(GTK_TEXT_VIEW(log->view),
88     &end, 0.0, FALSE, 0, 0);
89     }
90    
91 harbaum 193 /* watch child process and receive events */
92     static void child_state_cb(GPid pid, gint status, gpointer data) {
93 harbaum 195 gt_context_t *context = (gt_context_t*)data;
94 harbaum 193
95     puts("child state");
96    
97     if(WIFEXITED(status)) {
98 harbaum 194 printf("child exited with return code %d\n", WEXITSTATUS(status));
99     } else
100     printf("child failed\n");
101 harbaum 193
102     puts("gt exited");
103    
104 harbaum 195 appendf(&context->log, COLOR_SYSTEM, "GeoToad finished\n");
105    
106     appendf(&context->log, COLOR_SYSTEM, "TODO: free context!!!\n");
107     // printf("freeing context\n");
108     // g_free(context);
109    
110 harbaum 193 /* Reap child if needed. */
111     waitpid (pid, NULL, WNOHANG);
112     }
113    
114 harbaum 195 static gboolean child_input_cb(GIOChannel *source,
115     GIOCondition condition,
116     gpointer data) {
117 harbaum 193 gt_context_t *context = (gt_context_t*)data;
118 harbaum 195 int fd = g_io_channel_unix_get_fd(source);
119 harbaum 193 ssize_t bytes;
120    
121 harbaum 195 g_assert(condition == G_IO_IN);
122 harbaum 193
123     /* append to current buffer content */
124     while( (bytes = read(fd, context->buf+context->bused,
125     sizeof(context->buf) - context->bused - 1)) > 0) {
126     context->bused += bytes;
127     context->buf[context->bused]='\0';
128    
129     /* consume line by line */
130     gchar *ptr = context->buf;
131    
132     /* parse as long as the buffer contains a newline */
133     while( strchr(ptr, '\n') != NULL) {
134     char *p = strchr(ptr, '\n');
135     *p = '\0';
136    
137 harbaum 195 char *color = NULL;
138     if(strstr(ptr, "Saved to ") != NULL)
139     color = COLOR_OK;
140 harbaum 193
141 harbaum 195 appendf(&context->log, color, "%s\n", ptr);
142    
143 harbaum 193 ptr = p+1;
144     }
145    
146     /* move remaining bytes down the buffer */
147     memmove(context->buf, ptr, sizeof(context->buf)-(ptr-context->buf));
148     context->bused -= ptr - context->buf;
149    
150     /* check if buffer is full but doesn't contain any newline */
151     if(context->bused >= sizeof(context->buf)-1) {
152     printf("ERROR: reply buffer overflow\n");
153     context->bused = 0;
154     }
155     }
156    
157 harbaum 195 return TRUE;
158 harbaum 193 }
159    
160 harbaum 194 static void run(gt_context_t *context) {
161 harbaum 193 /* setup context */
162     context->bused = 0;
163     context->stdout_tag = -1;
164     context->stderr_tag = -1;
165     context->stdin_fd = -1;
166     context->stderr_fd = -1;
167     context->stderr_fd = -1;
168    
169 harbaum 194 /* build list of arguments to call geotoad */
170 harbaum 193 GPtrArray *gt_argv = g_ptr_array_new();
171 harbaum 196 g_ptr_array_add (gt_argv, GEOTOAD);
172 harbaum 195 g_ptr_array_add (gt_argv, "--distanceMax=1.0");
173 harbaum 194 g_ptr_array_add (gt_argv, "--output=gtoad.gpx");
174     g_ptr_array_add (gt_argv, "--password=winterblume");
175     g_ptr_array_add (gt_argv, "--queryType=coord");
176     g_ptr_array_add (gt_argv, "--user=Tantil");
177    
178     /* check if we need to add proxy config */
179     char *proxy = NULL;
180     if(context->appdata->proxy && context->appdata->proxy->host) {
181     if(context->appdata->proxy->use_authentication &&
182     context->appdata->proxy->authentication_user &&
183     context->appdata->proxy->authentication_password)
184     proxy = g_strdup_printf("--proxy=http://%s:%s@%s:%d",
185     context->appdata->proxy->authentication_user,
186     context->appdata->proxy->authentication_password,
187     context->appdata->proxy->host,
188     context->appdata->proxy->port);
189     else
190     proxy = g_strdup_printf("--proxy=http://%s:%d",
191     context->appdata->proxy->host,
192     context->appdata->proxy->port);
193    
194 harbaum 195 appendf(&context->log, COLOR_SYSTEM, "Using proxy: %s\n", proxy);
195 harbaum 194 g_ptr_array_add (gt_argv, proxy);
196     }
197    
198     g_ptr_array_add (gt_argv, "N49 00.000 E008 23.000");
199 harbaum 193 g_ptr_array_add (gt_argv, NULL);
200 harbaum 194
201 harbaum 193 GError *error=NULL;
202     GPid pid;
203     GSource *gt_watch;
204    
205     if (!g_spawn_async_with_pipes (NULL, /* CWD */
206     (char **) gt_argv->pdata, /* argv */
207     NULL, /* envp */
208     G_SPAWN_DO_NOT_REAP_CHILD, /* flags */
209     NULL, /* child setup */
210     NULL, /* user data */
211     &pid,
212     &context->stdin_fd,
213     &context->stdout_fd,
214     &context->stderr_fd,
215     &error)) {
216     g_ptr_array_free(gt_argv, TRUE);
217 harbaum 194 if(proxy) g_free(proxy);
218     appendf(&context->log, COLOR_ERR,
219 harbaum 195 _("GeoToad failed to start!\n%s\n"), error->message);
220 harbaum 193 g_error_free(error);
221     return;
222     }
223 harbaum 195
224 harbaum 193 g_ptr_array_free (gt_argv, TRUE);
225 harbaum 195 if(proxy) g_free(proxy);
226 harbaum 193
227     gt_watch = g_child_watch_source_new(pid);
228 harbaum 195 g_source_set_callback(gt_watch, (GSourceFunc) child_state_cb, context, NULL);
229 harbaum 193
230     g_source_attach(gt_watch, NULL);
231     g_source_unref(gt_watch);
232    
233     /* make nonblocking */
234     if(fcntl(context->stdout_fd, F_SETFL, O_NONBLOCK) == -1)
235     perror("fcntl failed");
236    
237     if(fcntl(context->stderr_fd, F_SETFL, O_NONBLOCK) == -1)
238     perror("fcntl failed");
239    
240 harbaum 195 GIOChannel *ioc = g_io_channel_unix_new(context->stdout_fd);
241     g_io_channel_set_close_on_unref (ioc, TRUE);
242     g_io_channel_set_encoding (ioc, NULL, NULL);
243     g_io_add_watch(ioc, G_IO_IN, child_input_cb, context);
244     g_io_channel_unref(ioc);
245    
246     // ioc = g_io_channel_unix_new(context->stderr_fd);
247     // g_io_add_watch(ioc, G_IO_IN, child_input_cb, context);
248     // g_io_channel_unref(ioc);
249 harbaum 193 }
250 harbaum 194
251 harbaum 196 /* show text window and display output of running geotoad */
252     static void gui_run(gt_context_t *context) {
253     GtkWidget *dialog = gtk_dialog_new_with_buttons(_("GeoToad - Run"),
254     GTK_WINDOW(context->appdata->window),
255 harbaum 194 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
256     GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE,
257     NULL);
258    
259     gtk_window_set_default_size(GTK_WINDOW(dialog), 640, 480);
260    
261     #ifndef USE_PANNABLE_AREA
262     GtkWidget *scrolled_window = gtk_scrolled_window_new(NULL, NULL);
263     gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window),
264     GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
265     #else
266     GtkWidget *pannable_area = hildon_pannable_area_new();
267     #endif
268    
269     context->log.buffer = gtk_text_buffer_new(NULL);
270    
271     #ifndef USE_HILDON_TEXT_VIEW
272     context->log.view = gtk_text_view_new_with_buffer(context->log.buffer);
273     #else
274     context->log.view = hildon_text_view_new();
275     hildon_text_view_set_buffer(HILDON_TEXT_VIEW(context->log.view),
276     context->log.buffer);
277     #endif
278    
279     #ifndef USE_PANNABLE_AREA
280     gtk_container_add(GTK_CONTAINER(scrolled_window), context->log.view);
281     gtk_scrolled_window_set_shadow_type( GTK_SCROLLED_WINDOW(scrolled_window),
282     GTK_SHADOW_IN);
283    
284     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox),
285     scrolled_window, TRUE, TRUE, 0);
286     #else
287     gtk_container_add(GTK_CONTAINER(pannable_area), context->log.view);
288     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox),
289     pannable_area, TRUE, TRUE, 0);
290     #endif
291    
292     gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_CLOSE);
293    
294     gtk_widget_show_all(dialog);
295    
296 harbaum 195 appendf(&context->log, COLOR_SYSTEM, "Running GeoToad\n");
297 harbaum 194 run(context);
298    
299     gtk_dialog_run(GTK_DIALOG(dialog));
300    
301     gtk_widget_destroy(dialog);
302     }
303 harbaum 196
304 harbaum 198 static void on_browse(GtkWidget *widget, gpointer data) {
305     gt_context_t *context = (gt_context_t*)data;
306    
307     printf("Browse %p\n", context->dialog);
308    
309     #ifdef USE_MAEMO
310     GtkWidget *dialog = hildon_file_chooser_dialog_new(GTK_WINDOW(context->dialog),
311     GTK_FILE_CHOOSER_ACTION_SAVE);
312     #else
313     GtkWidget *dialog = gtk_file_chooser_dialog_new(_("Save GPX file"),
314     GTK_WINDOW(context->dialog),
315     GTK_FILE_CHOOSER_ACTION_SAVE,
316     GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
317     GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
318     NULL);
319     #endif
320    
321     printf("set filename <%s>\n", context->appdata->gt.filename);
322    
323     if(!g_file_test(context->appdata->gt.filename, G_FILE_TEST_EXISTS)) {
324     char *last_sep = strrchr(context->appdata->gt.filename, '/');
325     if(last_sep) {
326     *last_sep = 0; // seperate path from file
327    
328     /* the user just created a new document */
329     gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog),
330     context->appdata->gt.filename);
331     gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), last_sep+1);
332    
333     /* restore full filename */
334     *last_sep = '/';
335     }
336     } else
337     gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dialog),
338     context->appdata->gt.filename);
339    
340     if (gtk_dialog_run (GTK_DIALOG(dialog)) == GTK_FM_OK) {
341     gchar *name = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
342     gtk_label_set_text(GTK_LABEL(context->filename), name);
343     }
344    
345     gtk_widget_destroy (dialog);
346     }
347    
348 harbaum 196 static gboolean gui_setup(gt_context_t *context) {
349 harbaum 198 appdata_t *appdata = context->appdata;
350 harbaum 196 gboolean ok = FALSE;
351    
352 harbaum 198 /* if no filename has been setup yet, create one */
353     if(!appdata->gt.filename && appdata->path) {
354     printf("creating path\n");
355     appdata->gt.filename =
356     g_strdup_printf("%s/gtoad.gpx", appdata->path);
357     }
358 harbaum 196
359 harbaum 198 context->dialog = gtk_dialog_new_with_buttons(_("GeoToad - Setup"),
360     GTK_WINDOW(appdata->window),
361     GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
362     GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
363     GTK_STOCK_OK, GTK_RESPONSE_OK,
364     NULL);
365    
366     /* ------------------- Coordinates ------------------------- */
367 harbaum 196 GtkWidget *hbox = gtk_hbox_new(FALSE, 0);
368 harbaum 198
369 harbaum 196 GtkWidget *vbox = gtk_vbox_new(FALSE, 0);
370 harbaum 198 gtk_box_pack_start_defaults(GTK_BOX(vbox), left_label_new(_("Latitude:")));
371     gtk_box_pack_start_defaults(GTK_BOX(vbox), left_label_new(_("Longitude:")));
372     gtk_box_pack_start_defaults(GTK_BOX(vbox), left_label_new(_("Distance:")));
373     gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE, FALSE, 0);
374    
375     /* setup default positions */
376     pos_t *refpos = get_pos(appdata);
377     if((isnan(appdata->gt.lat) || isnan(appdata->gt.lat)) && refpos) {
378     appdata->gt.lat = refpos->lat;
379     appdata->gt.lon = refpos->lon;
380     }
381    
382     vbox = gtk_vbox_new(FALSE, 0);
383     context->lat = lat_entry_new(appdata->gt.lat);
384     gtk_box_pack_start_defaults(GTK_BOX(vbox), context->lat);
385     context->lon = lat_entry_new(appdata->gt.lon);
386     gtk_box_pack_start_defaults(GTK_BOX(vbox), context->lon);
387     context->dst = dist_entry_new(appdata->gt.distance, appdata->imperial);
388     gtk_box_pack_start_defaults(GTK_BOX(vbox), context->dst);
389 harbaum 196 gtk_box_pack_start_defaults(GTK_BOX(hbox), vbox);
390 harbaum 198
391     gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(context->dialog)->vbox), hbox);
392    
393     /* ------------------- file name ------------------------- */
394     hbox = gtk_hbox_new(FALSE, 0);
395    
396     context->filename = gtk_label_new(appdata->gt.filename);
397     gtk_misc_set_alignment(GTK_MISC(context->filename), 0.f, 0.5f);
398     gtk_label_set_ellipsize(GTK_LABEL(context->filename), PANGO_ELLIPSIZE_MIDDLE);
399     gtk_box_pack_start_defaults(GTK_BOX(hbox), context->filename);
400    
401     GtkWidget *button = gtk_button_new_with_label(_("Browse"));
402     gtk_signal_connect(GTK_OBJECT(button), "clicked",
403     GTK_SIGNAL_FUNC(on_browse), context);
404     gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0);
405    
406     gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(context->dialog)->vbox), hbox);
407    
408    
409     /* ------------------- Username/Password ------------------------- */
410     hbox = gtk_hbox_new(FALSE, 0);
411     vbox = gtk_vbox_new(FALSE, 0);
412     gtk_box_pack_start_defaults(GTK_BOX(vbox), left_label_new(_("Username:")));
413     gtk_box_pack_start_defaults(GTK_BOX(vbox), left_label_new(_("Password:")));
414     gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE, FALSE, 0);
415 harbaum 196
416     vbox = gtk_vbox_new(FALSE, 0);
417     #if !defined(USE_MAEMO) || (MAEMO_VERSION_MAJOR < 5)
418     context->username = gtk_entry_new();
419     context->password = gtk_entry_new();
420     #else
421     context->username = hildon_entry_new(HILDON_SIZE_AUTO);
422     hildon_gtk_entry_set_input_mode(GTK_ENTRY(context->username),
423     HILDON_GTK_INPUT_MODE_FULL);
424     context->password = hildon_entry_new(HILDON_SIZE_AUTO);
425     hildon_gtk_entry_set_input_mode(GTK_ENTRY(context->password),
426     HILDON_GTK_INPUT_MODE_FULL);
427     #endif
428     gtk_entry_set_visibility(GTK_ENTRY(context->password), FALSE);
429    
430     /* set saved defaults */
431 harbaum 198 if(appdata->gt.username)
432 harbaum 196 gtk_entry_set_text(GTK_ENTRY(context->username),
433 harbaum 198 appdata->gt.username);
434 harbaum 196
435 harbaum 198 if(appdata->gt.password)
436 harbaum 196 gtk_entry_set_text(GTK_ENTRY(context->password),
437 harbaum 198 appdata->gt.password);
438 harbaum 196
439     gtk_box_pack_start_defaults(GTK_BOX(vbox), context->username);
440     gtk_box_pack_start_defaults(GTK_BOX(vbox), context->password);
441     gtk_box_pack_start_defaults(GTK_BOX(hbox), vbox);
442    
443 harbaum 198 gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(context->dialog)->vbox), hbox);
444 harbaum 196
445 harbaum 198 gtk_dialog_set_default_response(GTK_DIALOG(context->dialog), GTK_RESPONSE_OK);
446 harbaum 196
447 harbaum 198 gtk_widget_show_all(context->dialog);
448 harbaum 196
449 harbaum 198 if(gtk_dialog_run(GTK_DIALOG(context->dialog)) == GTK_RESPONSE_OK) {
450 harbaum 196
451     /* save values */
452 harbaum 198 if(appdata->gt.username) g_free(appdata->gt.username);
453     appdata->gt.username =
454 harbaum 196 g_strdup(gtk_entry_get_text(GTK_ENTRY(context->username)));
455    
456 harbaum 198 if(appdata->gt.password) g_free(appdata->gt.password);
457     appdata->gt.password =
458 harbaum 196 g_strdup(gtk_entry_get_text(GTK_ENTRY(context->password)));
459    
460 harbaum 198 if(appdata->gt.filename) g_free(appdata->gt.filename);
461     appdata->gt.filename =
462     g_strdup(gtk_label_get_text(GTK_LABEL(context->filename)));
463    
464 harbaum 196 ok = TRUE;
465     }
466    
467 harbaum 198 gtk_widget_destroy(context->dialog);
468 harbaum 196
469     return ok;
470     }
471    
472     void geotoad(appdata_t *appdata) {
473     if(!geotoad_available()) {
474     errorf(_("GeoToad is not installed on this device.\n"
475     "You need to install it in order to be able to use it."));
476     return;
477     }
478    
479     gt_context_t *context = g_new0(gt_context_t, 1);
480     context->appdata = appdata;
481    
482     printf("geoToad\n");
483    
484     if(gui_setup(context))
485     gui_run(context);
486     }
487    
488     gboolean geotoad_available(void) {
489     /* before doing anything make sure geotoad is installed */
490     return g_file_test(GEOTOAD, G_FILE_TEST_IS_EXECUTABLE);
491     }
492    
493