Contents of /trunk/src/geotoad.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 195 - (hide annotations)
Wed Nov 18 20:09:39 2009 UTC (14 years, 6 months ago) by harbaum
File MIME type: text/plain
File size: 8606 byte(s)
geotoad handling
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    
27 harbaum 195 #define COLOR_ERR "red"
28     #define COLOR_OK "darkgreen"
29     #define COLOR_SYSTEM "darkblue"
30 harbaum 194
31 harbaum 195 #define BUFFER_SIZE 256
32 harbaum 193
33     typedef struct {
34 harbaum 194 appdata_t *appdata;
35    
36 harbaum 193 char buf[BUFFER_SIZE];
37     int bused;
38    
39     /** gdk input tag for async IO */
40     gint stdout_tag, stderr_tag;
41     gint stdin_fd, stdout_fd, stderr_fd;
42    
43 harbaum 194 struct log_s {
44     GtkTextBuffer *buffer;
45     GtkWidget *view;
46     } log;
47    
48 harbaum 193 } gt_context_t;
49    
50 harbaum 194 static void appendf(struct log_s *log, char *colname,
51     const char *fmt, ...) {
52     va_list args;
53     va_start( args, fmt );
54     char *buf = g_strdup_vprintf(fmt, args);
55     va_end( args );
56    
57 harbaum 195 printf("append: %s", buf);
58    
59 harbaum 194 GtkTextTag *tag = NULL;
60     if(colname)
61     tag = gtk_text_buffer_create_tag(log->buffer, NULL,
62     "foreground", colname,
63     NULL);
64    
65     GtkTextIter end;
66     gtk_text_buffer_get_end_iter(log->buffer, &end);
67     if(tag)
68     gtk_text_buffer_insert_with_tags(log->buffer, &end, buf, -1, tag, NULL);
69     else
70     gtk_text_buffer_insert(log->buffer, &end, buf, -1);
71    
72     g_free(buf);
73    
74 harbaum 195 gtk_text_buffer_get_end_iter(log->buffer, &end);
75 harbaum 194 gtk_text_view_scroll_to_iter(GTK_TEXT_VIEW(log->view),
76     &end, 0.0, FALSE, 0, 0);
77     }
78    
79 harbaum 193 /* watch child process and receive events */
80     static void child_state_cb(GPid pid, gint status, gpointer data) {
81 harbaum 195 gt_context_t *context = (gt_context_t*)data;
82 harbaum 193
83     puts("child state");
84    
85     if(WIFEXITED(status)) {
86 harbaum 194 printf("child exited with return code %d\n", WEXITSTATUS(status));
87     } else
88     printf("child failed\n");
89 harbaum 193
90     puts("gt exited");
91    
92 harbaum 195 appendf(&context->log, COLOR_SYSTEM, "GeoToad finished\n");
93    
94     appendf(&context->log, COLOR_SYSTEM, "TODO: free context!!!\n");
95     // printf("freeing context\n");
96     // g_free(context);
97    
98 harbaum 193 /* Reap child if needed. */
99     waitpid (pid, NULL, WNOHANG);
100     }
101    
102 harbaum 195 static gboolean child_input_cb(GIOChannel *source,
103     GIOCondition condition,
104     gpointer data) {
105 harbaum 193 gt_context_t *context = (gt_context_t*)data;
106 harbaum 195 int fd = g_io_channel_unix_get_fd(source);
107 harbaum 193 ssize_t bytes;
108    
109 harbaum 195 g_assert(condition == G_IO_IN);
110 harbaum 193
111     /* append to current buffer content */
112     while( (bytes = read(fd, context->buf+context->bused,
113     sizeof(context->buf) - context->bused - 1)) > 0) {
114     context->bused += bytes;
115     context->buf[context->bused]='\0';
116    
117     /* consume line by line */
118     gchar *ptr = context->buf;
119    
120     /* parse as long as the buffer contains a newline */
121     while( strchr(ptr, '\n') != NULL) {
122     char *p = strchr(ptr, '\n');
123     *p = '\0';
124    
125 harbaum 195 char *color = NULL;
126     if(strstr(ptr, "Saved to ") != NULL)
127     color = COLOR_OK;
128 harbaum 193
129 harbaum 195 appendf(&context->log, color, "%s\n", ptr);
130    
131 harbaum 193 ptr = p+1;
132     }
133    
134     /* move remaining bytes down the buffer */
135     memmove(context->buf, ptr, sizeof(context->buf)-(ptr-context->buf));
136     context->bused -= ptr - context->buf;
137    
138     /* check if buffer is full but doesn't contain any newline */
139     if(context->bused >= sizeof(context->buf)-1) {
140     printf("ERROR: reply buffer overflow\n");
141     context->bused = 0;
142     }
143     }
144    
145 harbaum 195 return TRUE;
146 harbaum 193 }
147    
148 harbaum 194 static void run(gt_context_t *context) {
149 harbaum 193 /* setup context */
150     context->bused = 0;
151     context->stdout_tag = -1;
152     context->stderr_tag = -1;
153     context->stdin_fd = -1;
154     context->stderr_fd = -1;
155     context->stderr_fd = -1;
156    
157 harbaum 194 /* build list of arguments to call geotoad */
158 harbaum 193 GPtrArray *gt_argv = g_ptr_array_new();
159 harbaum 194 g_ptr_array_add (gt_argv, "/usr/bin/geotoad");
160 harbaum 195 g_ptr_array_add (gt_argv, "--distanceMax=1.0");
161 harbaum 194 g_ptr_array_add (gt_argv, "--output=gtoad.gpx");
162     g_ptr_array_add (gt_argv, "--password=winterblume");
163     g_ptr_array_add (gt_argv, "--queryType=coord");
164     g_ptr_array_add (gt_argv, "--user=Tantil");
165    
166     /* check if we need to add proxy config */
167     char *proxy = NULL;
168     if(context->appdata->proxy && context->appdata->proxy->host) {
169     if(context->appdata->proxy->use_authentication &&
170     context->appdata->proxy->authentication_user &&
171     context->appdata->proxy->authentication_password)
172     proxy = g_strdup_printf("--proxy=http://%s:%s@%s:%d",
173     context->appdata->proxy->authentication_user,
174     context->appdata->proxy->authentication_password,
175     context->appdata->proxy->host,
176     context->appdata->proxy->port);
177     else
178     proxy = g_strdup_printf("--proxy=http://%s:%d",
179     context->appdata->proxy->host,
180     context->appdata->proxy->port);
181    
182 harbaum 195 appendf(&context->log, COLOR_SYSTEM, "Using proxy: %s\n", proxy);
183 harbaum 194 g_ptr_array_add (gt_argv, proxy);
184     }
185    
186     g_ptr_array_add (gt_argv, "N49 00.000 E008 23.000");
187 harbaum 193 g_ptr_array_add (gt_argv, NULL);
188 harbaum 194
189 harbaum 193 GError *error=NULL;
190     GPid pid;
191     GSource *gt_watch;
192    
193     if (!g_spawn_async_with_pipes (NULL, /* CWD */
194     (char **) gt_argv->pdata, /* argv */
195     NULL, /* envp */
196     G_SPAWN_DO_NOT_REAP_CHILD, /* flags */
197     NULL, /* child setup */
198     NULL, /* user data */
199     &pid,
200     &context->stdin_fd,
201     &context->stdout_fd,
202     &context->stderr_fd,
203     &error)) {
204     g_ptr_array_free(gt_argv, TRUE);
205 harbaum 194 if(proxy) g_free(proxy);
206     appendf(&context->log, COLOR_ERR,
207 harbaum 195 _("GeoToad failed to start!\n%s\n"), error->message);
208 harbaum 193 g_error_free(error);
209     return;
210     }
211 harbaum 195
212 harbaum 193 g_ptr_array_free (gt_argv, TRUE);
213 harbaum 195 if(proxy) g_free(proxy);
214 harbaum 193
215     gt_watch = g_child_watch_source_new(pid);
216 harbaum 195 g_source_set_callback(gt_watch, (GSourceFunc) child_state_cb, context, NULL);
217 harbaum 193
218     g_source_attach(gt_watch, NULL);
219     g_source_unref(gt_watch);
220    
221     /* make nonblocking */
222     if(fcntl(context->stdout_fd, F_SETFL, O_NONBLOCK) == -1)
223     perror("fcntl failed");
224    
225     if(fcntl(context->stderr_fd, F_SETFL, O_NONBLOCK) == -1)
226     perror("fcntl failed");
227    
228 harbaum 195 GIOChannel *ioc = g_io_channel_unix_new(context->stdout_fd);
229     g_io_channel_set_close_on_unref (ioc, TRUE);
230     g_io_channel_set_encoding (ioc, NULL, NULL);
231     g_io_add_watch(ioc, G_IO_IN, child_input_cb, context);
232     g_io_channel_unref(ioc);
233    
234     // ioc = g_io_channel_unix_new(context->stderr_fd);
235     // g_io_add_watch(ioc, G_IO_IN, child_input_cb, context);
236     // g_io_channel_unref(ioc);
237 harbaum 193 }
238 harbaum 194
239     void geotoad(appdata_t *appdata) {
240     gt_context_t *context = g_new0(gt_context_t, 1);
241     context->appdata = appdata;
242    
243     printf("geoToad\n");
244    
245     GtkWidget *dialog = gtk_dialog_new_with_buttons(_("GeoToad"),
246     GTK_WINDOW(appdata->window),
247     GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
248     GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE,
249     NULL);
250    
251     gtk_window_set_default_size(GTK_WINDOW(dialog), 640, 480);
252    
253     #ifndef USE_PANNABLE_AREA
254     GtkWidget *scrolled_window = gtk_scrolled_window_new(NULL, NULL);
255     gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window),
256     GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
257     #else
258     GtkWidget *pannable_area = hildon_pannable_area_new();
259     #endif
260    
261     context->log.buffer = gtk_text_buffer_new(NULL);
262    
263     #ifndef USE_HILDON_TEXT_VIEW
264     context->log.view = gtk_text_view_new_with_buffer(context->log.buffer);
265     #else
266     context->log.view = hildon_text_view_new();
267     hildon_text_view_set_buffer(HILDON_TEXT_VIEW(context->log.view),
268     context->log.buffer);
269     #endif
270    
271     #ifndef USE_PANNABLE_AREA
272     gtk_container_add(GTK_CONTAINER(scrolled_window), context->log.view);
273     gtk_scrolled_window_set_shadow_type( GTK_SCROLLED_WINDOW(scrolled_window),
274     GTK_SHADOW_IN);
275    
276     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox),
277     scrolled_window, TRUE, TRUE, 0);
278     #else
279     gtk_container_add(GTK_CONTAINER(pannable_area), context->log.view);
280     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox),
281     pannable_area, TRUE, TRUE, 0);
282     #endif
283    
284     gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_CLOSE);
285    
286     gtk_widget_show_all(dialog);
287    
288 harbaum 195 appendf(&context->log, COLOR_SYSTEM, "Running GeoToad\n");
289 harbaum 194 run(context);
290    
291     gtk_dialog_run(GTK_DIALOG(dialog));
292    
293     gtk_widget_destroy(dialog);
294     }