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