Parent Directory | Revision Log
Work on pannable area/finger friendliness
1 | /* |
2 | * Copyright (C) 2008-2009 Till Harbaum <till@harbaum.org>. |
3 | * |
4 | * This file is part of OSM2Go. |
5 | * |
6 | * OSM2Go 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 | * OSM2Go 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 OSM2Go. If not, see <http://www.gnu.org/licenses/>. |
18 | */ |
19 | |
20 | /* |
21 | * TODO: |
22 | */ |
23 | |
24 | #include "appdata.h" |
25 | #include "banner.h" |
26 | |
27 | #include <sys/stat.h> |
28 | |
29 | #include <libxml/parser.h> |
30 | #include <libxml/tree.h> |
31 | |
32 | #if !defined(LIBXML_TREE_ENABLED) || !defined(LIBXML_OUTPUT_ENABLED) |
33 | #error "libxml doesn't support required tree or output" |
34 | #endif |
35 | |
36 | /* there shouldn't be a reason to changes the servers url */ |
37 | #undef SERVER_EDITABLE |
38 | |
39 | typedef struct { |
40 | project_t *project; |
41 | settings_t *settings; |
42 | GtkWidget *dialog, *fsize, *diff_stat, *diff_remove; |
43 | GtkWidget *desc, *download; |
44 | GtkWidget *minlat, *minlon, *maxlat, *maxlon; |
45 | gboolean is_new; |
46 | #ifdef SERVER_EDITABLE |
47 | GtkWidget *server; |
48 | #endif |
49 | area_edit_t area_edit; |
50 | } project_context_t; |
51 | |
52 | static gboolean project_edit(appdata_t *appdata, GtkWidget *parent, |
53 | settings_t *settings, project_t *project, |
54 | gboolean is_new); |
55 | |
56 | |
57 | /* ------------ project file io ------------- */ |
58 | |
59 | static gboolean project_read(appdata_t *appdata, |
60 | char *project_file, project_t *project) { |
61 | |
62 | LIBXML_TEST_VERSION; |
63 | |
64 | xmlDoc *doc = NULL; |
65 | xmlNode *root_element = NULL; |
66 | |
67 | /* parse the file and get the DOM */ |
68 | if((doc = xmlReadFile(project_file, NULL, 0)) == NULL) { |
69 | printf("error: could not parse file %s\n", project_file); |
70 | return FALSE; |
71 | } |
72 | |
73 | /* Get the root element node */ |
74 | root_element = xmlDocGetRootElement(doc); |
75 | |
76 | xmlNode *cur_node = NULL; |
77 | for (cur_node = root_element; cur_node; cur_node = cur_node->next) { |
78 | if (cur_node->type == XML_ELEMENT_NODE) { |
79 | if(strcasecmp((char*)cur_node->name, "proj") == 0) { |
80 | char *str; |
81 | |
82 | if((str = (char*)xmlGetProp(cur_node, BAD_CAST "dirty"))) { |
83 | project->data_dirty = (strcasecmp(str, "true") == 0); |
84 | xmlFree(str); |
85 | } else |
86 | project->data_dirty = FALSE; |
87 | |
88 | xmlNode *node = cur_node->children; |
89 | |
90 | while(node != NULL) { |
91 | if(node->type == XML_ELEMENT_NODE) { |
92 | |
93 | if(strcasecmp((char*)node->name, "desc") == 0) { |
94 | str = (char*)xmlNodeListGetString(doc, node->children, 1); |
95 | project->desc = g_strdup(str); |
96 | printf("desc = %s\n", project->desc); |
97 | xmlFree(str); |
98 | |
99 | } else if(strcasecmp((char*)node->name, "server") == 0) { |
100 | str = (char*)xmlNodeListGetString(doc, node->children, 1); |
101 | project->server = g_strdup(str); |
102 | printf("server = %s\n", project->server); |
103 | xmlFree(str); |
104 | |
105 | } else if(project->map_state && |
106 | strcasecmp((char*)node->name, "map") == 0) { |
107 | |
108 | if((str = (char*)xmlGetProp(node, BAD_CAST "zoom"))) { |
109 | project->map_state->zoom = g_ascii_strtod(str, NULL); |
110 | xmlFree(str); |
111 | } |
112 | if((str = (char*)xmlGetProp(node, BAD_CAST "detail"))) { |
113 | project->map_state->detail = g_ascii_strtod(str, NULL); |
114 | xmlFree(str); |
115 | } |
116 | if((str = (char*)xmlGetProp(node, BAD_CAST "scroll-offset-x"))) { |
117 | project->map_state->scroll_offset.x = strtoul(str, NULL, 10); |
118 | xmlFree(str); |
119 | } |
120 | if((str = (char*)xmlGetProp(node, BAD_CAST "scroll-offset-y"))) { |
121 | project->map_state->scroll_offset.y = strtoul(str, NULL, 10); |
122 | xmlFree(str); |
123 | } |
124 | |
125 | } else if(strcasecmp((char*)node->name, "wms") == 0) { |
126 | |
127 | if((str = (char*)xmlGetProp(node, BAD_CAST "server"))) { |
128 | project->wms_server = g_strdup(str); |
129 | xmlFree(str); |
130 | } |
131 | if((str = (char*)xmlGetProp(node, BAD_CAST "path"))) { |
132 | project->wms_path = g_strdup(str); |
133 | xmlFree(str); |
134 | } |
135 | if((str = (char*)xmlGetProp(node, BAD_CAST "x-offset"))) { |
136 | project->wms_offset.x = strtoul(str, NULL, 10); |
137 | xmlFree(str); |
138 | } |
139 | if((str = (char*)xmlGetProp(node, BAD_CAST "y-offset"))) { |
140 | project->wms_offset.y = strtoul(str, NULL, 10); |
141 | xmlFree(str); |
142 | } |
143 | |
144 | } else if(strcasecmp((char*)node->name, "osm") == 0) { |
145 | str = (char*)xmlNodeListGetString(doc, node->children, 1); |
146 | printf("osm = %s\n", str); |
147 | |
148 | /* make this a relative path if possible */ |
149 | /* if the project path actually is a prefix of this, */ |
150 | /* then just remove this prefix */ |
151 | if((str[0] == '/') && |
152 | (strlen(str) > strlen(project->path)) && |
153 | !strncmp(str, project->path, strlen(project->path))) { |
154 | |
155 | project->osm = g_strdup(str + strlen(project->path)); |
156 | printf("osm name converted to relative %s\n", project->osm); |
157 | } else |
158 | project->osm = g_strdup(str); |
159 | |
160 | xmlFree(str); |
161 | |
162 | } else if(strcasecmp((char*)node->name, "min") == 0) { |
163 | if((str = (char*)xmlGetProp(node, BAD_CAST "lat"))) { |
164 | project->min.lat = g_ascii_strtod(str, NULL); |
165 | xmlFree(str); |
166 | } |
167 | if((str = (char*)xmlGetProp(node, BAD_CAST "lon"))) { |
168 | project->min.lon = g_ascii_strtod(str, NULL); |
169 | xmlFree(str); |
170 | } |
171 | |
172 | } else if(strcasecmp((char*)node->name, "max") == 0) { |
173 | if((str = (char*)xmlGetProp(node, BAD_CAST "lat"))) { |
174 | project->max.lat = g_ascii_strtod(str, NULL); |
175 | xmlFree(str); |
176 | } |
177 | if((str = (char*)xmlGetProp(node, BAD_CAST "lon"))) { |
178 | project->max.lon = g_ascii_strtod(str, NULL); |
179 | xmlFree(str); |
180 | } |
181 | } |
182 | } |
183 | node = node->next; |
184 | } |
185 | } |
186 | } |
187 | } |
188 | |
189 | xmlFreeDoc(doc); |
190 | xmlCleanupParser(); |
191 | |
192 | return TRUE; |
193 | } |
194 | |
195 | gboolean project_save(GtkWidget *parent, project_t *project) { |
196 | char str[32]; |
197 | char *project_file = g_strdup_printf("%s%s.proj", |
198 | project->path, project->name); |
199 | |
200 | printf("saving project to %s\n", project_file); |
201 | |
202 | /* check if project path exists */ |
203 | if(!g_file_test(project->path, G_FILE_TEST_IS_DIR)) { |
204 | /* make sure project base path exists */ |
205 | if(g_mkdir_with_parents(project->path, S_IRWXU) != 0) { |
206 | errorf(GTK_WIDGET(parent), |
207 | _("Unable to create project path %s"), project->path); |
208 | return FALSE; |
209 | } |
210 | } |
211 | |
212 | LIBXML_TEST_VERSION; |
213 | |
214 | xmlDocPtr doc = xmlNewDoc(BAD_CAST "1.0"); |
215 | xmlNodePtr node, root_node = xmlNewNode(NULL, BAD_CAST "proj"); |
216 | xmlNewProp(root_node, BAD_CAST "name", BAD_CAST project->name); |
217 | if(project->data_dirty) |
218 | xmlNewProp(root_node, BAD_CAST "dirty", BAD_CAST "true"); |
219 | |
220 | xmlDocSetRootElement(doc, root_node); |
221 | |
222 | if(project->server) |
223 | xmlNewChild(root_node, NULL, BAD_CAST "server", |
224 | BAD_CAST project->server); |
225 | |
226 | if(project->desc) |
227 | xmlNewChild(root_node, NULL, BAD_CAST "desc", BAD_CAST project->desc); |
228 | |
229 | xmlNewChild(root_node, NULL, BAD_CAST "osm", BAD_CAST project->osm); |
230 | |
231 | node = xmlNewChild(root_node, NULL, BAD_CAST "min", NULL); |
232 | g_ascii_formatd(str, sizeof(str), LL_FORMAT, project->min.lat); |
233 | xmlNewProp(node, BAD_CAST "lat", BAD_CAST str); |
234 | g_ascii_formatd(str, sizeof(str), LL_FORMAT, project->min.lon); |
235 | xmlNewProp(node, BAD_CAST "lon", BAD_CAST str); |
236 | |
237 | node = xmlNewChild(root_node, NULL, BAD_CAST "max", NULL); |
238 | g_ascii_formatd(str, sizeof(str), LL_FORMAT, project->max.lat); |
239 | xmlNewProp(node, BAD_CAST "lat", BAD_CAST str); |
240 | g_ascii_formatd(str, sizeof(str), LL_FORMAT, project->max.lon); |
241 | xmlNewProp(node, BAD_CAST "lon", BAD_CAST str); |
242 | |
243 | if(project->map_state) { |
244 | node = xmlNewChild(root_node, NULL, BAD_CAST "map", BAD_CAST NULL); |
245 | g_ascii_formatd(str, sizeof(str), "%.04f", project->map_state->zoom); |
246 | xmlNewProp(node, BAD_CAST "zoom", BAD_CAST str); |
247 | g_ascii_formatd(str, sizeof(str), "%.04f", project->map_state->detail); |
248 | xmlNewProp(node, BAD_CAST "detail", BAD_CAST str); |
249 | snprintf(str, sizeof(str), "%d", project->map_state->scroll_offset.x); |
250 | xmlNewProp(node, BAD_CAST "scroll-offset-x", BAD_CAST str); |
251 | snprintf(str, sizeof(str), "%d", project->map_state->scroll_offset.y); |
252 | xmlNewProp(node, BAD_CAST "scroll-offset-y", BAD_CAST str); |
253 | } |
254 | |
255 | node = xmlNewChild(root_node, NULL, BAD_CAST "wms", NULL); |
256 | if(project->wms_server) |
257 | xmlNewProp(node, BAD_CAST "server", BAD_CAST project->wms_server); |
258 | if(project->wms_path) |
259 | xmlNewProp(node, BAD_CAST "path", BAD_CAST project->wms_path); |
260 | snprintf(str, sizeof(str), "%d", project->wms_offset.x); |
261 | xmlNewProp(node, BAD_CAST "x-offset", BAD_CAST str); |
262 | snprintf(str, sizeof(str), "%d", project->wms_offset.y); |
263 | xmlNewProp(node, BAD_CAST "y-offset", BAD_CAST str); |
264 | |
265 | xmlSaveFormatFileEnc(project_file, doc, "UTF-8", 1); |
266 | xmlFreeDoc(doc); |
267 | xmlCleanupParser(); |
268 | |
269 | g_free(project_file); |
270 | |
271 | return TRUE; |
272 | } |
273 | |
274 | /* ------------ freeing projects --------------------- */ |
275 | |
276 | void project_free(project_t *project) { |
277 | if(!project) return; |
278 | |
279 | if(project->name) g_free(project->name); |
280 | if(project->desc) g_free(project->desc); |
281 | if(project->server) g_free(project->server); |
282 | |
283 | if(project->wms_server) g_free(project->wms_server); |
284 | if(project->wms_path) g_free(project->wms_path); |
285 | |
286 | if(project->path) g_free(project->path); |
287 | if(project->osm) g_free(project->osm); |
288 | |
289 | map_state_free(project->map_state); |
290 | |
291 | g_free(project); |
292 | } |
293 | |
294 | /* ------------ project selection dialog ------------- */ |
295 | |
296 | static char *project_fullname(settings_t *settings, const char *name) { |
297 | return g_strdup_printf("%s%s/%s.proj", settings->base_path, name, name); |
298 | } |
299 | |
300 | gboolean project_exists(settings_t *settings, const char *name) { |
301 | gboolean ok = FALSE; |
302 | char *fulldir = g_strdup_printf("%s%s", settings->base_path, name); |
303 | |
304 | if(g_file_test(fulldir, G_FILE_TEST_IS_DIR)) { |
305 | |
306 | /* check for project file */ |
307 | char *fullname = project_fullname(settings, name); |
308 | |
309 | if(g_file_test(fullname, G_FILE_TEST_IS_REGULAR)) |
310 | ok = TRUE; |
311 | |
312 | g_free(fullname); |
313 | } |
314 | g_free(fulldir); |
315 | |
316 | return ok; |
317 | } |
318 | |
319 | static project_t *project_scan(appdata_t *appdata) { |
320 | project_t *projects = NULL, **current = &projects; |
321 | |
322 | /* scan for projects */ |
323 | GDir *dir = g_dir_open(appdata->settings->base_path, 0, NULL); |
324 | const char *name = NULL; |
325 | do { |
326 | if((name = g_dir_read_name(dir))) { |
327 | if(project_exists(appdata->settings, name)) { |
328 | printf("found project %s\n", name); |
329 | |
330 | /* try to read project and append it to chain */ |
331 | *current = g_new0(project_t, 1); |
332 | (*current)->name = g_strdup(name); |
333 | (*current)->path = g_strdup_printf("%s%s/", |
334 | appdata->settings->base_path, name); |
335 | |
336 | char *fullname = project_fullname(appdata->settings, name); |
337 | if(project_read(appdata, fullname, *current)) |
338 | current = &((*current)->next); |
339 | else { |
340 | g_free(*current); |
341 | *current = NULL; |
342 | } |
343 | g_free(fullname); |
344 | } |
345 | } |
346 | } while(name); |
347 | |
348 | g_dir_close(dir); |
349 | |
350 | return projects; |
351 | } |
352 | |
353 | typedef struct { |
354 | appdata_t *appdata; |
355 | project_t *project; |
356 | GtkWidget *dialog, *list; |
357 | settings_t *settings; |
358 | } select_context_t; |
359 | |
360 | enum { |
361 | PROJECT_COL_NAME = 0, |
362 | PROJECT_COL_STATUS, |
363 | PROJECT_COL_DESCRIPTION, |
364 | PROJECT_COL_DATA, |
365 | PROJECT_NUM_COLS |
366 | }; |
367 | |
368 | static gboolean osm_file_exists(char *path, char *name) { |
369 | gboolean exists = FALSE; |
370 | |
371 | if(name[0] == '/') |
372 | exists = g_file_test(name, G_FILE_TEST_IS_REGULAR); |
373 | else { |
374 | char *full = g_strjoin(NULL, path, name, NULL); |
375 | exists = g_file_test(full, G_FILE_TEST_IS_REGULAR); |
376 | g_free(full); |
377 | } |
378 | return exists; |
379 | } |
380 | |
381 | static void view_selected(select_context_t *context, project_t *project) { |
382 | /* check if the selected project also has a valid osm file */ |
383 | gtk_dialog_set_response_sensitive(GTK_DIALOG(context->dialog), |
384 | GTK_RESPONSE_ACCEPT, |
385 | project && osm_file_exists(project->path, project->osm)); |
386 | } |
387 | |
388 | static gboolean |
389 | view_selection_func(GtkTreeSelection *selection, GtkTreeModel *model, |
390 | GtkTreePath *path, gboolean path_currently_selected, |
391 | gpointer userdata) { |
392 | select_context_t *context = (select_context_t*)userdata; |
393 | GtkTreeIter iter; |
394 | |
395 | if(gtk_tree_model_get_iter(model, &iter, path)) { |
396 | project_t *project = NULL; |
397 | gtk_tree_model_get(model, &iter, PROJECT_COL_DATA, &project, -1); |
398 | g_assert(gtk_tree_path_get_depth(path) == 1); |
399 | |
400 | view_selected(context, project); |
401 | } |
402 | |
403 | return TRUE; /* allow selection state to change */ |
404 | } |
405 | |
406 | /* get the currently selected project in the list, NULL if none */ |
407 | static project_t *project_get_selected(GtkWidget *list) { |
408 | project_t *project = NULL; |
409 | GtkTreeModel *model; |
410 | GtkTreeIter iter; |
411 | |
412 | g_assert(list_get_selected(list, &model, &iter)); |
413 | gtk_tree_model_get(model, &iter, PROJECT_COL_DATA, &project, -1); |
414 | |
415 | return project; |
416 | } |
417 | |
418 | /* ------------------------- create a new project ---------------------- */ |
419 | |
420 | /* returns true of str contains one of the characters in chars */ |
421 | static gboolean strchrs(char *str, char *chars) { |
422 | while(*chars) { |
423 | char *p = str; |
424 | while(*p) { |
425 | if(*p == *chars) |
426 | return TRUE; |
427 | |
428 | p++; |
429 | } |
430 | chars++; |
431 | } |
432 | return FALSE; |
433 | } |
434 | |
435 | typedef struct { |
436 | GtkWidget *dialog; |
437 | settings_t *settings; |
438 | } name_callback_context_t; |
439 | |
440 | static void callback_modified_name(GtkWidget *widget, gpointer data) { |
441 | name_callback_context_t *context = (name_callback_context_t*)data; |
442 | |
443 | char *name = (char*)gtk_entry_get_text(GTK_ENTRY(widget)); |
444 | |
445 | /* name must not contain some special chars */ |
446 | gboolean ok = FALSE; |
447 | |
448 | /* check if there's a name */ |
449 | if(name && strlen(name) > 0) { |
450 | /* check if it consists of valid characters */ |
451 | if(!strchrs(name, "\\*?()\n\t\r")) { |
452 | /* check if such a project already exists */ |
453 | if(!project_exists(context->settings, name)) |
454 | ok = TRUE; |
455 | } |
456 | } |
457 | |
458 | gtk_dialog_set_response_sensitive(GTK_DIALOG(context->dialog), |
459 | GTK_RESPONSE_ACCEPT, ok); |
460 | } |
461 | |
462 | |
463 | gboolean project_delete(select_context_t *context, project_t *project) { |
464 | |
465 | printf("deleting project \"%s\"\n", project->name); |
466 | |
467 | /* check if we are to delete the currently open project */ |
468 | if(context->appdata->project && |
469 | !strcmp(context->appdata->project->name, project->name)) { |
470 | |
471 | if(!yes_no_f(context->dialog, NULL, 0, 0, |
472 | _("Delete current project?"), |
473 | _("The project you are about to delete is the one " |
474 | "you are currently working on!\n\n" |
475 | "Do you want to delete it anyway?"))) |
476 | return FALSE; |
477 | |
478 | project_close(context->appdata); |
479 | } |
480 | |
481 | /* remove entire directory from disk */ |
482 | GDir *dir = g_dir_open(project->path, 0, NULL); |
483 | const char *name = NULL; |
484 | do { |
485 | if((name = g_dir_read_name(dir))) { |
486 | char *fullname = g_strdup_printf("%s/%s", project->path, name); |
487 | g_remove(fullname); |
488 | g_free(fullname); |
489 | } |
490 | } while(name); |
491 | |
492 | /* remove the projects directory */ |
493 | g_remove(project->path); |
494 | |
495 | /* remove from view */ |
496 | GtkTreeIter iter; |
497 | GtkTreeModel *model = list_get_model(context->list); |
498 | gboolean deleted = FALSE; |
499 | if(gtk_tree_model_get_iter_first(model, &iter)) { |
500 | do { |
501 | project_t *prj = NULL; |
502 | gtk_tree_model_get(model, &iter, PROJECT_COL_DATA, &prj, -1); |
503 | if(prj && (prj == project)) { |
504 | printf("found %s to remove\n", prj->name); |
505 | /* and remove from store */ |
506 | gtk_list_store_remove(GTK_LIST_STORE(model), &iter); |
507 | deleted = TRUE; |
508 | } |
509 | } while(!deleted && gtk_tree_model_iter_next(model, &iter)); |
510 | } |
511 | |
512 | /* de-chain entry from project list */ |
513 | project_t **project_list = &context->project; |
514 | while(*project_list) { |
515 | if(*project_list == project) |
516 | *project_list = (*project_list)->next; |
517 | else |
518 | project_list = &((*project_list)->next); |
519 | } |
520 | |
521 | /* free project structure */ |
522 | project_free(project); |
523 | |
524 | /* disable edit/remove buttons */ |
525 | view_selected(context, NULL); |
526 | |
527 | return TRUE; |
528 | } |
529 | |
530 | project_t *project_new(select_context_t *context) { |
531 | printf("creating project with default values\n"); |
532 | |
533 | /* -------------- first choose a name for the project --------------- */ |
534 | GtkWidget *dialog = |
535 | misc_dialog_new(MISC_DIALOG_NOSIZE, _("Project name"), |
536 | GTK_WINDOW(context->dialog), |
537 | GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT, |
538 | GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, |
539 | NULL); |
540 | |
541 | GtkWidget *hbox = gtk_hbox_new(FALSE, 8); |
542 | gtk_box_pack_start_defaults(GTK_BOX(hbox), gtk_label_new(_("Name:"))); |
543 | |
544 | name_callback_context_t name_context = { dialog, context->settings }; |
545 | GtkWidget *entry = gtk_entry_new(); |
546 | gtk_box_pack_start_defaults(GTK_BOX(hbox), entry); |
547 | g_signal_connect(G_OBJECT(entry), "changed", |
548 | G_CALLBACK(callback_modified_name), &name_context); |
549 | |
550 | gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(dialog)->vbox), hbox); |
551 | |
552 | /* don't all user to click ok until something useful has been entered */ |
553 | gtk_dialog_set_response_sensitive(GTK_DIALOG(dialog), |
554 | GTK_RESPONSE_ACCEPT, FALSE); |
555 | |
556 | gtk_widget_show_all(dialog); |
557 | if(GTK_RESPONSE_ACCEPT != gtk_dialog_run(GTK_DIALOG(dialog))) { |
558 | gtk_widget_destroy(dialog); |
559 | return NULL; |
560 | } |
561 | |
562 | project_t *project = g_new0(project_t, 1); |
563 | project->name = g_strdup(gtk_entry_get_text(GTK_ENTRY(entry))); |
564 | gtk_widget_destroy(dialog); |
565 | |
566 | |
567 | project->path = g_strdup_printf("%s%s/", |
568 | context->settings->base_path, project->name); |
569 | project->desc = NULL; |
570 | |
571 | /* no data downloaded yet */ |
572 | project->data_dirty = TRUE; |
573 | |
574 | /* adjust default server stored in settings if required */ |
575 | if(strstr(context->settings->server, "0.5") != NULL) { |
576 | strstr(context->settings->server, "0.5")[2] = '6'; |
577 | printf("adjusting server path in settings to 0.6\n"); |
578 | } |
579 | |
580 | /* use global server/access settings */ |
581 | project->server = g_strdup(context->settings->server); |
582 | |
583 | /* build project osm file name */ |
584 | project->osm = g_strdup_printf("%s.osm", project->name); |
585 | |
586 | /* around the castle in karlsruhe, germany ... */ |
587 | project->min.lat = NAN; project->min.lon = NAN; |
588 | project->max.lat = NAN; project->max.lon = NAN; |
589 | |
590 | /* create project file on disk */ |
591 | project_save(context->dialog, project); |
592 | |
593 | if(!project_edit(context->appdata, context->dialog, |
594 | context->settings, project, TRUE)) { |
595 | printf("new/edit cancelled!!\n"); |
596 | |
597 | project_delete(context, project); |
598 | |
599 | project = NULL; |
600 | } |
601 | |
602 | /* enable/disable edit/remove buttons */ |
603 | view_selected(context, project); |
604 | |
605 | return project; |
606 | } |
607 | |
608 | // predecs |
609 | void project_get_status_icon_stock_id(project_t *project, gchar **stock_id); |
610 | |
611 | static void on_project_new(GtkButton *button, gpointer data) { |
612 | select_context_t *context = (select_context_t*)data; |
613 | project_t **project = &context->project; |
614 | *project = project_new(context); |
615 | if(*project) { |
616 | |
617 | GtkTreeModel *model = list_get_model(context->list); |
618 | |
619 | GtkTreeIter iter; |
620 | gchar *status_stock_id = NULL; |
621 | project_get_status_icon_stock_id(*project, &status_stock_id); |
622 | gtk_list_store_append(GTK_LIST_STORE(model), &iter); |
623 | gtk_list_store_set(GTK_LIST_STORE(model), &iter, |
624 | PROJECT_COL_NAME, (*project)->name, |
625 | PROJECT_COL_STATUS, status_stock_id, |
626 | PROJECT_COL_DESCRIPTION, (*project)->desc, |
627 | PROJECT_COL_DATA, *project, |
628 | -1); |
629 | |
630 | GtkTreeSelection *selection = list_get_selection(context->list); |
631 | gtk_tree_selection_select_iter(selection, &iter); |
632 | } |
633 | } |
634 | |
635 | static void on_project_delete(GtkButton *button, gpointer data) { |
636 | select_context_t *context = (select_context_t*)data; |
637 | project_t *project = project_get_selected(context->list); |
638 | |
639 | char *str = g_strdup_printf(_("Do you really want to delete the " |
640 | "project \"%s\"?"), project->name); |
641 | GtkWidget *dialog = gtk_message_dialog_new( |
642 | GTK_WINDOW(context->dialog), |
643 | GTK_DIALOG_DESTROY_WITH_PARENT, |
644 | GTK_MESSAGE_QUESTION, GTK_BUTTONS_YES_NO, str); |
645 | g_free(str); |
646 | |
647 | gtk_window_set_title(GTK_WINDOW(dialog), _("Delete project?")); |
648 | |
649 | /* set the active flag again if the user answered "no" */ |
650 | if(GTK_RESPONSE_NO == gtk_dialog_run(GTK_DIALOG(dialog))) { |
651 | gtk_widget_destroy(dialog); |
652 | return; |
653 | } |
654 | |
655 | gtk_widget_destroy(dialog); |
656 | |
657 | if(!project_delete(context, project)) |
658 | printf("unable to delete project\n"); |
659 | } |
660 | |
661 | static void on_project_edit(GtkButton *button, gpointer data) { |
662 | select_context_t *context = (select_context_t*)data; |
663 | project_t *project = project_get_selected(context->list); |
664 | g_assert(project); |
665 | |
666 | if(project_edit(context->appdata, context->dialog, |
667 | context->settings, project, FALSE)) { |
668 | GtkTreeModel *model; |
669 | GtkTreeIter iter; |
670 | |
671 | /* description etc. may have changed, so update list */ |
672 | GtkTreeSelection *selection = list_get_selection(context->list); |
673 | g_assert(gtk_tree_selection_get_selected(selection, &model, &iter)); |
674 | |
675 | // gtk_tree_model_get(model, &iter, PROJECT_COL_DATA, &project, -1); |
676 | gchar *status_stock_id = NULL; |
677 | project_get_status_icon_stock_id(project, &status_stock_id); |
678 | gtk_list_store_set(GTK_LIST_STORE(model), &iter, |
679 | PROJECT_COL_NAME, project->name, |
680 | PROJECT_COL_STATUS, status_stock_id, |
681 | PROJECT_COL_DESCRIPTION, project->desc, |
682 | -1); |
683 | |
684 | |
685 | /* check if we have actually editing the currently open project */ |
686 | if(context->appdata->project && |
687 | !strcmp(context->appdata->project->name, project->name)) { |
688 | project_t *cur = context->appdata->project; |
689 | |
690 | printf("edited project was actually the active one!\n"); |
691 | |
692 | /* update the currently active project also */ |
693 | |
694 | /* update description */ |
695 | if(cur->desc) { free(cur->desc); cur->desc = NULL; } |
696 | if(project->desc) cur->desc = g_strdup(project->desc); |
697 | |
698 | /* update server */ |
699 | if(cur->server) { free(cur->server); cur->server = NULL; } |
700 | if(project->server) cur->server = g_strdup(project->server); |
701 | |
702 | /* update coordinates */ |
703 | if((cur->min.lat != project->min.lat) || |
704 | (cur->max.lat != project->max.lat) || |
705 | (cur->min.lon != project->min.lon) || |
706 | (cur->max.lon != project->max.lon)) { |
707 | appdata_t *appdata = context->appdata; |
708 | |
709 | /* save modified coordinates */ |
710 | cur->min.lat = project->min.lat; |
711 | cur->max.lat = project->max.lat; |
712 | cur->min.lon = project->min.lon; |
713 | cur->max.lon = project->max.lon; |
714 | |
715 | /* try to do this automatically */ |
716 | |
717 | /* if we have valid osm data loaded: save state first */ |
718 | if(appdata->osm) { |
719 | /* redraw the entire map by destroying all map items */ |
720 | diff_save(appdata->project, appdata->osm); |
721 | map_clear(appdata, MAP_LAYER_ALL); |
722 | osm_free(&appdata->icon, appdata->osm); |
723 | |
724 | appdata->osm = NULL; |
725 | } |
726 | |
727 | /* and load the (hopefully) new file */ |
728 | appdata->osm = osm_parse(appdata->project->path, |
729 | appdata->project->osm); |
730 | diff_restore(appdata, appdata->project, appdata->osm); |
731 | map_paint(appdata); |
732 | |
733 | main_ui_enable(appdata); |
734 | } |
735 | } |
736 | } |
737 | |
738 | /* enable/disable edit/remove buttons */ |
739 | view_selected(context, project); |
740 | } |
741 | |
742 | |
743 | gboolean project_osm_present(project_t *project) { |
744 | char *osm_name = g_strdup_printf("%s/%s.osm", project->path, project->name); |
745 | gboolean is_present = g_file_test(osm_name, G_FILE_TEST_EXISTS); |
746 | g_free(osm_name); |
747 | return is_present; |
748 | } |
749 | |
750 | void project_get_status_icon_stock_id(project_t *project, gchar **stock_id) { |
751 | *stock_id = (! project_osm_present(project)) ? GTK_STOCK_DIALOG_WARNING |
752 | : diff_present(project) ? GTK_STOCK_PROPERTIES |
753 | : GTK_STOCK_FILE; |
754 | // TODO: check for outdatedness too. Which icon to use? |
755 | } |
756 | |
757 | static GtkWidget *project_list_widget(select_context_t *context) { |
758 | context->list = list_new(LIST_HILDON_WITHOUT_HEADERS); |
759 | |
760 | list_set_selection_function(context->list, view_selection_func, context); |
761 | |
762 | list_set_columns(context->list, |
763 | _("Name"), PROJECT_COL_NAME, 0, |
764 | _("State"), PROJECT_COL_STATUS, LIST_FLAG_STOCK_ICON, |
765 | _("Description"), PROJECT_COL_DESCRIPTION, LIST_FLAG_ELLIPSIZE, |
766 | NULL); |
767 | |
768 | |
769 | /* build the store */ |
770 | GtkListStore *store = gtk_list_store_new(PROJECT_NUM_COLS, |
771 | G_TYPE_STRING, // name |
772 | G_TYPE_STRING, // status |
773 | G_TYPE_STRING, // desc |
774 | G_TYPE_POINTER); // data |
775 | |
776 | GtkTreeIter iter; |
777 | project_t *project = context->project; |
778 | while(project) { |
779 | gchar *status_stock_id = NULL; |
780 | project_get_status_icon_stock_id(project, &status_stock_id); |
781 | /* Append a row and fill in some data */ |
782 | gtk_list_store_append(store, &iter); |
783 | gtk_list_store_set(store, &iter, |
784 | PROJECT_COL_NAME, project->name, |
785 | PROJECT_COL_STATUS, status_stock_id, |
786 | PROJECT_COL_DESCRIPTION, project->desc, |
787 | PROJECT_COL_DATA, project, |
788 | -1); |
789 | project = project->next; |
790 | } |
791 | |
792 | list_set_store(context->list, store); |
793 | g_object_unref(store); |
794 | |
795 | list_set_static_buttons(context->list, LIST_BTN_NEW | LIST_BTN_BIG, |
796 | G_CALLBACK(on_project_new), G_CALLBACK(on_project_edit), |
797 | G_CALLBACK(on_project_delete), context); |
798 | |
799 | return context->list; |
800 | } |
801 | |
802 | static char *project_select(appdata_t *appdata) { |
803 | char *name = NULL; |
804 | |
805 | select_context_t *context = g_new0(select_context_t, 1); |
806 | context->appdata = appdata; |
807 | context->settings = appdata->settings; |
808 | context->project = project_scan(appdata); |
809 | |
810 | /* create project selection dialog */ |
811 | context->dialog = |
812 | misc_dialog_new(MISC_DIALOG_MEDIUM,_("Project selection"), |
813 | GTK_WINDOW(appdata->window), |
814 | GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT, |
815 | GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT, |
816 | NULL); |
817 | |
818 | /* under fremantle the dialog does not have an "Open" button */ |
819 | /* as it's closed when a project is being selected */ |
820 | gtk_dialog_set_default_response(GTK_DIALOG(context->dialog), |
821 | GTK_RESPONSE_ACCEPT); |
822 | |
823 | gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(context->dialog)->vbox), |
824 | project_list_widget(context)); |
825 | |
826 | /* don't all user to click ok until something is selected */ |
827 | gtk_dialog_set_response_sensitive(GTK_DIALOG(context->dialog), |
828 | GTK_RESPONSE_ACCEPT, FALSE); |
829 | |
830 | gtk_widget_show_all(context->dialog); |
831 | if(GTK_RESPONSE_ACCEPT == gtk_dialog_run(GTK_DIALOG(context->dialog))) |
832 | name = g_strdup(project_get_selected(context->list)->name); |
833 | |
834 | gtk_widget_destroy(context->dialog); |
835 | |
836 | /* free all entries */ |
837 | project_t *project = context->project; |
838 | while(project) { |
839 | project_t *next = project->next; |
840 | project_free(project); |
841 | project = next; |
842 | } |
843 | |
844 | g_free(context); |
845 | |
846 | return name; |
847 | } |
848 | |
849 | /* ---------------------------------------------------- */ |
850 | |
851 | /* return file length or -1 on error */ |
852 | static gsize file_length(char *path, char *name) { |
853 | char *str = NULL; |
854 | |
855 | if(name[0] == '/') str = g_strdup(name); |
856 | else str = g_strjoin(NULL, path, name, NULL); |
857 | |
858 | GMappedFile *gmap = g_mapped_file_new(str, FALSE, NULL); |
859 | g_free(str); |
860 | |
861 | if(!gmap) return -1; |
862 | gsize size = g_mapped_file_get_length(gmap); |
863 | g_mapped_file_free(gmap); |
864 | return size; |
865 | } |
866 | |
867 | void project_filesize(project_context_t *context) { |
868 | char *str = NULL; |
869 | |
870 | printf("Checking size of %s\n", context->project->osm); |
871 | |
872 | if(!osm_file_exists(context->project->path, context->project->osm)) { |
873 | GdkColor color; |
874 | gdk_color_parse("red", &color); |
875 | gtk_widget_modify_fg(context->fsize, GTK_STATE_NORMAL, &color); |
876 | |
877 | str = g_strdup(_("Not downloaded!")); |
878 | |
879 | gtk_dialog_set_response_sensitive(GTK_DIALOG(context->dialog), |
880 | GTK_RESPONSE_ACCEPT, !context->is_new); |
881 | |
882 | } else { |
883 | gtk_widget_modify_fg(context->fsize, GTK_STATE_NORMAL, NULL); |
884 | |
885 | if(!context->project->data_dirty) |
886 | str = g_strdup_printf(_("%d bytes present"), |
887 | file_length(context->project->path, |
888 | context->project->osm)); |
889 | else |
890 | str = g_strdup_printf(_("Outdated, please download!")); |
891 | |
892 | gtk_dialog_set_response_sensitive(GTK_DIALOG(context->dialog), |
893 | GTK_RESPONSE_ACCEPT, !context->is_new || |
894 | !context->project->data_dirty); |
895 | } |
896 | |
897 | if(str) { |
898 | gtk_label_set_text(GTK_LABEL(context->fsize), str); |
899 | g_free(str); |
900 | } |
901 | } |
902 | |
903 | void project_diffstat(project_context_t *context) { |
904 | char *str = NULL; |
905 | |
906 | if(diff_present(context->project)) { |
907 | /* this should prevent the user from changing the area */ |
908 | str = g_strdup(_("unsaved changes pending")); |
909 | } else |
910 | str = g_strdup(_("no pending changes")); |
911 | |
912 | gtk_label_set_text(GTK_LABEL(context->diff_stat), str); |
913 | g_free(str); |
914 | } |
915 | |
916 | static gboolean |
917 | project_pos_is_valid(project_t *project) { |
918 | return(!isnan(project->min.lat) && |
919 | !isnan(project->min.lon) && |
920 | !isnan(project->max.lat) && |
921 | !isnan(project->max.lon)); |
922 | } |
923 | |
924 | static void on_edit_clicked(GtkButton *button, gpointer data) { |
925 | project_context_t *context = (project_context_t*)data; |
926 | |
927 | if(diff_present(context->project)) { |
928 | if(!yes_no_f(context->dialog, NULL, 0, 0, |
929 | _("Discard pending changes?"), |
930 | _("You have pending changes in this project. Changing " |
931 | "the area will discard these changes.\n\nDo you want to " |
932 | "discard all your changes?"))) |
933 | return; |
934 | |
935 | diff_remove(context->project); |
936 | project_diffstat(context); |
937 | gtk_widget_set_sensitive(context->diff_remove, FALSE); |
938 | } |
939 | |
940 | if(area_edit(&context->area_edit)) { |
941 | printf("coordinates changed!!\n"); |
942 | |
943 | pos_lon_label_set(context->minlat, context->project->min.lat); |
944 | pos_lon_label_set(context->minlon, context->project->min.lon); |
945 | pos_lon_label_set(context->maxlat, context->project->max.lat); |
946 | pos_lon_label_set(context->maxlon, context->project->max.lon); |
947 | |
948 | gtk_widget_set_sensitive(context->download, |
949 | project_pos_is_valid(context->project)); |
950 | |
951 | /* (re-) download area */ |
952 | if(osm_download(GTK_WIDGET(context->dialog), |
953 | context->area_edit.appdata->settings, context->project)) |
954 | context->project->data_dirty = FALSE; |
955 | |
956 | project_filesize(context); |
957 | } |
958 | } |
959 | |
960 | static void on_download_clicked(GtkButton *button, gpointer data) { |
961 | project_context_t *context = (project_context_t*)data; |
962 | |
963 | printf("download %s\n", context->project->osm); |
964 | |
965 | if(osm_download(context->dialog, context->settings, context->project)) |
966 | context->project->data_dirty = FALSE; |
967 | else |
968 | printf("download failed\n"); |
969 | |
970 | project_filesize(context); |
971 | } |
972 | |
973 | static void on_diff_remove_clicked(GtkButton *button, gpointer data) { |
974 | project_context_t *context = (project_context_t*)data; |
975 | |
976 | printf("clicked diff remove\n"); |
977 | |
978 | GtkWidget *dialog = gtk_message_dialog_new( |
979 | GTK_WINDOW(context->dialog), |
980 | GTK_DIALOG_DESTROY_WITH_PARENT, |
981 | GTK_MESSAGE_QUESTION, GTK_BUTTONS_YES_NO, |
982 | _("Do you really want to discard your changes? This " |
983 | "permanently undo all changes you've made so far and which " |
984 | "you didn't upload yet.")); |
985 | |
986 | gtk_window_set_title(GTK_WINDOW(dialog), _("Discard changes?")); |
987 | |
988 | /* set the active flag again if the user answered "no" */ |
989 | if(GTK_RESPONSE_YES == gtk_dialog_run(GTK_DIALOG(dialog))) { |
990 | diff_remove(context->project); |
991 | project_diffstat(context); |
992 | gtk_widget_set_sensitive(context->diff_remove, FALSE); |
993 | } |
994 | |
995 | gtk_widget_destroy(dialog); |
996 | } |
997 | |
998 | gboolean project_check_demo(GtkWidget *parent, project_t *project) { |
999 | if(!project->server) |
1000 | messagef(parent, "Demo project", |
1001 | "This is a preinstalled demo project. This means that the " |
1002 | "basic project parameters cannot be changed and no data can " |
1003 | "be up- or downloaded via the OSM servers.\n\n" |
1004 | "Please setup a new project to do these things."); |
1005 | |
1006 | return !project->server; |
1007 | } |
1008 | |
1009 | /* create a left aligned label (normal ones are centered) */ |
1010 | static GtkWidget *gtk_label_left_new(char *str) { |
1011 | GtkWidget *label = gtk_label_new(str); |
1012 | gtk_misc_set_alignment(GTK_MISC(label), 0.f, .5f); |
1013 | return label; |
1014 | } |
1015 | |
1016 | static gboolean |
1017 | project_edit(appdata_t *appdata, GtkWidget *parent, settings_t *settings, |
1018 | project_t *project, gboolean is_new) { |
1019 | gboolean ok = FALSE; |
1020 | |
1021 | if(project_check_demo(parent, project)) |
1022 | return ok; |
1023 | |
1024 | /* ------------ project edit dialog ------------- */ |
1025 | |
1026 | project_context_t *context = g_new0(project_context_t, 1); |
1027 | context->project = project; |
1028 | context->area_edit.settings = context->settings = settings; |
1029 | context->area_edit.appdata = appdata; |
1030 | context->is_new = is_new; |
1031 | |
1032 | context->area_edit.min = &project->min; |
1033 | context->area_edit.max = &project->max; |
1034 | |
1035 | /* cancel is enabled for "new" projects only */ |
1036 | if(is_new) { |
1037 | char *str = g_strdup_printf(_("New project - %s"), project->name); |
1038 | |
1039 | context->area_edit.parent = |
1040 | context->dialog = misc_dialog_new(MISC_DIALOG_WIDE, str, |
1041 | GTK_WINDOW(parent), |
1042 | GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT, |
1043 | GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, NULL); |
1044 | g_free(str); |
1045 | } else { |
1046 | char *str = g_strdup_printf(_("Edit project - %s"), project->name); |
1047 | |
1048 | context->area_edit.parent = |
1049 | context->dialog = misc_dialog_new(MISC_DIALOG_WIDE, str, |
1050 | GTK_WINDOW(parent), |
1051 | GTK_STOCK_CLOSE, GTK_RESPONSE_ACCEPT, NULL); |
1052 | g_free(str); |
1053 | } |
1054 | |
1055 | gtk_dialog_set_default_response(GTK_DIALOG(context->dialog), |
1056 | GTK_RESPONSE_ACCEPT); |
1057 | |
1058 | GtkWidget *label; |
1059 | GtkWidget *table = gtk_table_new(5, 5, FALSE); // x, y |
1060 | gtk_table_set_col_spacing(GTK_TABLE(table), 0, 8); |
1061 | gtk_table_set_col_spacing(GTK_TABLE(table), 3, 8); |
1062 | |
1063 | label = gtk_label_left_new(_("Description:")); |
1064 | gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, 0, 1); |
1065 | context->desc = gtk_entry_new(); |
1066 | gtk_entry_set_activates_default(GTK_ENTRY(context->desc), TRUE); |
1067 | if(project->desc) |
1068 | gtk_entry_set_text(GTK_ENTRY(context->desc), project->desc); |
1069 | gtk_table_attach_defaults(GTK_TABLE(table), context->desc, 1, 4, 0, 1); |
1070 | gtk_table_set_row_spacing(GTK_TABLE(table), 0, 4); |
1071 | |
1072 | label = gtk_label_left_new(_("Latitude:")); |
1073 | gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, 1, 2); |
1074 | context->minlat = pos_lat_label_new(project->min.lat); |
1075 | gtk_table_attach_defaults(GTK_TABLE(table), context->minlat, 1, 2, 1, 2); |
1076 | label = gtk_label_new(_("to")); |
1077 | gtk_table_attach_defaults(GTK_TABLE(table), label, 2, 3, 1, 2); |
1078 | context->maxlat = pos_lon_label_new(project->max.lat); |
1079 | gtk_table_attach_defaults(GTK_TABLE(table), context->maxlat, 3, 4, 1, 2); |
1080 | |
1081 | label = gtk_label_left_new(_("Longitude:")); |
1082 | gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, 2, 3); |
1083 | context->minlon = pos_lat_label_new(project->min.lon); |
1084 | gtk_table_attach_defaults(GTK_TABLE(table), context->minlon, 1, 2, 2, 3); |
1085 | label = gtk_label_new(_("to")); |
1086 | gtk_table_attach_defaults(GTK_TABLE(table), label, 2, 3, 2, 3); |
1087 | context->maxlon = pos_lon_label_new(project->max.lon); |
1088 | gtk_table_attach_defaults(GTK_TABLE(table), context->maxlon, 3, 4, 2, 3); |
1089 | |
1090 | GtkWidget *edit = gtk_button_new_with_label(_("Edit")); |
1091 | gtk_signal_connect(GTK_OBJECT(edit), "clicked", |
1092 | (GtkSignalFunc)on_edit_clicked, context); |
1093 | gtk_table_attach(GTK_TABLE(table), edit, 4, 5, 1, 3, |
1094 | GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL,0,0); |
1095 | |
1096 | gtk_table_set_row_spacing(GTK_TABLE(table), 2, 4); |
1097 | |
1098 | #ifdef SERVER_EDITABLE |
1099 | label = gtk_label_left_new(_("Server:")); |
1100 | gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, 3, 4); |
1101 | context->server = gtk_entry_new(); |
1102 | gtk_entry_set_activates_default(GTK_ENTRY(context->server), TRUE); |
1103 | HILDON_ENTRY_NO_AUTOCAP(context->server); |
1104 | gtk_entry_set_text(GTK_ENTRY(context->server), project->server); |
1105 | gtk_table_attach_defaults(GTK_TABLE(table), context->server, 1, 4, 3, 4); |
1106 | |
1107 | gtk_table_set_row_spacing(GTK_TABLE(table), 3, 4); |
1108 | #endif |
1109 | |
1110 | label = gtk_label_left_new(_("Map data:")); |
1111 | gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, 4, 5); |
1112 | context->fsize = gtk_label_left_new(_("")); |
1113 | project_filesize(context); |
1114 | gtk_table_attach_defaults(GTK_TABLE(table), context->fsize, 1, 4, 4, 5); |
1115 | context->download = gtk_button_new_with_label(_("Download")); |
1116 | gtk_signal_connect(GTK_OBJECT(context->download), "clicked", |
1117 | (GtkSignalFunc)on_download_clicked, context); |
1118 | gtk_widget_set_sensitive(context->download, project_pos_is_valid(project)); |
1119 | |
1120 | gtk_table_attach_defaults(GTK_TABLE(table), context->download, 4, 5, 4, 5); |
1121 | |
1122 | gtk_table_set_row_spacing(GTK_TABLE(table), 4, 4); |
1123 | |
1124 | label = gtk_label_left_new(_("Changes:")); |
1125 | gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, 5, 6); |
1126 | context->diff_stat = gtk_label_left_new(_("")); |
1127 | project_diffstat(context); |
1128 | gtk_table_attach_defaults(GTK_TABLE(table), context->diff_stat, 1, 4, 5, 6); |
1129 | context->diff_remove = gtk_button_new_with_label(_("Undo all")); |
1130 | if(!diff_present(project)) |
1131 | gtk_widget_set_sensitive(context->diff_remove, FALSE); |
1132 | gtk_signal_connect(GTK_OBJECT(context->diff_remove), "clicked", |
1133 | (GtkSignalFunc)on_diff_remove_clicked, context); |
1134 | gtk_table_attach_defaults(GTK_TABLE(table), context->diff_remove, 4, 5, 5, 6); |
1135 | |
1136 | /* ---------------------------------------------------------------- */ |
1137 | |
1138 | gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(context->dialog)->vbox), |
1139 | table); |
1140 | |
1141 | /* disable "ok" if there's no valid file downloaded */ |
1142 | if(is_new) |
1143 | gtk_dialog_set_response_sensitive(GTK_DIALOG(context->dialog), |
1144 | GTK_RESPONSE_ACCEPT, |
1145 | osm_file_exists(project->path, project->name)); |
1146 | |
1147 | gtk_widget_show_all(context->dialog); |
1148 | |
1149 | /* the return value may actually be != ACCEPT, but only if the editor */ |
1150 | /* is run for a new project which is completely removed afterwards if */ |
1151 | /* cancel has been selected */ |
1152 | ok = (GTK_RESPONSE_ACCEPT == gtk_dialog_run(GTK_DIALOG(context->dialog))); |
1153 | |
1154 | /* transfer values from edit dialog into project structure */ |
1155 | |
1156 | /* fetch values from dialog */ |
1157 | if(context->project->desc) g_free(context->project->desc); |
1158 | context->project->desc = g_strdup(gtk_entry_get_text( |
1159 | GTK_ENTRY(context->desc))); |
1160 | if(strlen(context->project->desc) == 0) { |
1161 | g_free(context->project->desc); |
1162 | context->project->desc = NULL; |
1163 | } |
1164 | |
1165 | #ifdef SERVER_EDITABLE |
1166 | if(context->project->server) g_free(context->project->server); |
1167 | context->project->server = g_strdup(gtk_entry_get_text( |
1168 | GTK_ENTRY(context->server))); |
1169 | if(strlen(context->project->server) == 0) { |
1170 | g_free(context->project->server); |
1171 | context->project->server = NULL; |
1172 | } |
1173 | #endif |
1174 | |
1175 | /* save project */ |
1176 | project_save(context->dialog, project); |
1177 | |
1178 | gtk_widget_destroy(context->dialog); |
1179 | g_free(context); |
1180 | |
1181 | return ok; |
1182 | } |
1183 | |
1184 | gboolean project_open(appdata_t *appdata, char *name) { |
1185 | project_t *project = g_new0(project_t, 1); |
1186 | |
1187 | /* link to map state if a map already exists */ |
1188 | if(appdata->map) { |
1189 | printf("Project: Using map state\n"); |
1190 | project->map_state = appdata->map->state; |
1191 | } else { |
1192 | printf("Project: Creating new map_state\n"); |
1193 | project->map_state = map_state_new(); |
1194 | } |
1195 | |
1196 | map_state_reset(project->map_state); |
1197 | project->map_state->refcount++; |
1198 | |
1199 | /* build project path */ |
1200 | project->path = g_strdup_printf("%s%s/", |
1201 | appdata->settings->base_path, name); |
1202 | project->name = g_strdup(name); |
1203 | |
1204 | char *project_file = g_strdup_printf("%s%s.proj", project->path, name); |
1205 | |
1206 | printf("project file = %s\n", project_file); |
1207 | if(!g_file_test(project_file, G_FILE_TEST_IS_REGULAR)) { |
1208 | printf("requested project file doesn't exist\n"); |
1209 | project_free(project); |
1210 | g_free(project_file); |
1211 | return FALSE; |
1212 | } |
1213 | |
1214 | if(!project_read(appdata, project_file, project)) { |
1215 | printf("error reading project file\n"); |
1216 | project_free(project); |
1217 | g_free(project_file); |
1218 | return FALSE; |
1219 | } |
1220 | |
1221 | g_free(project_file); |
1222 | |
1223 | /* --------- project structure ok: load its OSM file --------- */ |
1224 | appdata->project = project; |
1225 | |
1226 | printf("project_open: loading osm %s\n", project->osm); |
1227 | appdata->osm = osm_parse(project->path, project->osm); |
1228 | if(!appdata->osm) { |
1229 | printf("OSM parsing failed\n"); |
1230 | return FALSE; |
1231 | } |
1232 | |
1233 | printf("parsing ok\n"); |
1234 | |
1235 | return TRUE; |
1236 | } |
1237 | |
1238 | gboolean project_close(appdata_t *appdata) { |
1239 | if(!appdata->project) return FALSE; |
1240 | |
1241 | printf("closing current project\n"); |
1242 | |
1243 | /* redraw the entire map by destroying all map items and redrawing them */ |
1244 | if(appdata->osm) |
1245 | diff_save(appdata->project, appdata->osm); |
1246 | |
1247 | /* Save track and turn off the handler callback */ |
1248 | track_save(appdata->project, appdata->track.track); |
1249 | track_clear(appdata, appdata->track.track); |
1250 | appdata->track.track = NULL; |
1251 | |
1252 | map_clear(appdata, MAP_LAYER_ALL); |
1253 | |
1254 | if(appdata->osm) { |
1255 | osm_free(&appdata->icon, appdata->osm); |
1256 | appdata->osm = NULL; |
1257 | } |
1258 | |
1259 | /* update project file on disk */ |
1260 | project_save(GTK_WIDGET(appdata->window), appdata->project); |
1261 | |
1262 | project_free(appdata->project); |
1263 | appdata->project = NULL; |
1264 | |
1265 | return TRUE; |
1266 | } |
1267 | |
1268 | #define _PROJECT_LOAD_BUF_SIZ 64 |
1269 | |
1270 | gboolean project_load(appdata_t *appdata, char *name) { |
1271 | char *proj_name = NULL; |
1272 | |
1273 | if(!name) { |
1274 | /* make user select a project */ |
1275 | proj_name = project_select(appdata); |
1276 | if(!proj_name) { |
1277 | printf("no project selected\n"); |
1278 | return FALSE; |
1279 | } |
1280 | } |
1281 | else { |
1282 | proj_name = g_strdup(name); |
1283 | } |
1284 | |
1285 | char banner_txt[_PROJECT_LOAD_BUF_SIZ]; |
1286 | memset(banner_txt, 0, _PROJECT_LOAD_BUF_SIZ); |
1287 | |
1288 | snprintf(banner_txt, _PROJECT_LOAD_BUF_SIZ, _("Loading %s"), proj_name); |
1289 | banner_busy_start(appdata, TRUE, banner_txt); |
1290 | |
1291 | /* close current project */ |
1292 | banner_busy_tick(); |
1293 | if(appdata->project) |
1294 | project_close(appdata); |
1295 | |
1296 | /* open project itself */ |
1297 | banner_busy_tick(); |
1298 | if(!project_open(appdata, proj_name)) { |
1299 | printf("error opening requested project\n"); |
1300 | |
1301 | if(appdata->project) { |
1302 | project_free(appdata->project); |
1303 | appdata->project = NULL; |
1304 | } |
1305 | |
1306 | if(appdata->osm) { |
1307 | osm_free(&appdata->icon, appdata->osm); |
1308 | appdata->osm = NULL; |
1309 | } |
1310 | |
1311 | snprintf(banner_txt, _PROJECT_LOAD_BUF_SIZ, |
1312 | _("Error opening %s"), proj_name); |
1313 | banner_busy_stop(appdata); |
1314 | banner_show_info(appdata, banner_txt); |
1315 | |
1316 | g_free(proj_name); |
1317 | return FALSE; |
1318 | } |
1319 | |
1320 | /* check if OSM data is valid */ |
1321 | banner_busy_tick(); |
1322 | if(!osm_sanity_check(GTK_WIDGET(appdata->window), appdata->osm)) { |
1323 | printf("project/osm sanity checks failed, unloading project\n"); |
1324 | |
1325 | if(appdata->project) { |
1326 | project_free(appdata->project); |
1327 | appdata->project = NULL; |
1328 | } |
1329 | |
1330 | if(appdata->osm) { |
1331 | osm_free(&appdata->icon, appdata->osm); |
1332 | appdata->osm = NULL; |
1333 | } |
1334 | |
1335 | snprintf(banner_txt, _PROJECT_LOAD_BUF_SIZ, |
1336 | _("Error opening %s"), proj_name); |
1337 | banner_busy_stop(appdata); |
1338 | banner_show_info(appdata, banner_txt); |
1339 | |
1340 | g_free(proj_name); |
1341 | return FALSE; |
1342 | } |
1343 | |
1344 | /* load diff possibly preset */ |
1345 | banner_busy_tick(); |
1346 | diff_restore(appdata, appdata->project, appdata->osm); |
1347 | |
1348 | /* prepare colors etc, draw data and adjust scroll/zoom settings */ |
1349 | banner_busy_tick(); |
1350 | map_init(appdata); |
1351 | |
1352 | /* restore a track */ |
1353 | banner_busy_tick(); |
1354 | appdata->track.track = track_restore(appdata, appdata->project); |
1355 | if(appdata->track.track) |
1356 | map_track_draw(appdata->map, appdata->track.track); |
1357 | |
1358 | /* finally load a background if present */ |
1359 | banner_busy_tick(); |
1360 | wms_load(appdata); |
1361 | |
1362 | /* save the name of the project for the perferences */ |
1363 | if(appdata->settings->project) |
1364 | g_free(appdata->settings->project); |
1365 | appdata->settings->project = g_strdup(appdata->project->name); |
1366 | |
1367 | banner_busy_stop(appdata); |
1368 | |
1369 | #if 0 |
1370 | snprintf(banner_txt, _PROJECT_LOAD_BUF_SIZ, _("Loaded %s"), proj_name); |
1371 | banner_show_info(appdata, banner_txt); |
1372 | #endif |
1373 | |
1374 | statusbar_set(appdata, NULL, 0); |
1375 | |
1376 | g_free(proj_name); |
1377 | return TRUE; |
1378 | } |
1379 | |
1380 | /* ------------------- project setup wizard ----------------- */ |
1381 | |
1382 | struct wizard_s; |
1383 | |
1384 | typedef struct wizard_page_s { |
1385 | const gchar *title; |
1386 | GtkWidget* (*setup)(struct wizard_page_s *page); |
1387 | void (*update)(struct wizard_page_s *page); |
1388 | GtkAssistantPageType type; |
1389 | gboolean complete; |
1390 | /* everything before here is initialized statically */ |
1391 | |
1392 | struct wizard_s *wizard; |
1393 | GtkWidget *widget; |
1394 | gint index; |
1395 | |
1396 | union { |
1397 | struct { |
1398 | GtkWidget *check[4]; |
1399 | GtkWidget *label[4]; |
1400 | } source_selection; |
1401 | |
1402 | } state; |
1403 | |
1404 | } wizard_page_t; |
1405 | |
1406 | typedef struct wizard_s { |
1407 | gboolean running; |
1408 | |
1409 | int page_num; |
1410 | wizard_page_t *page; |
1411 | appdata_t *appdata; |
1412 | guint handler_id; |
1413 | GtkWidget *assistant; |
1414 | } wizard_t; |
1415 | |
1416 | |
1417 | static gint on_assistant_destroy(GtkWidget *widget, wizard_t *wizard) { |
1418 | printf("destroy callback\n"); |
1419 | wizard->running = FALSE; |
1420 | return FALSE; |
1421 | } |
1422 | |
1423 | static void on_assistant_cancel(GtkWidget *widget, wizard_t *wizard) { |
1424 | printf("cancel callback\n"); |
1425 | wizard->running = FALSE; |
1426 | } |
1427 | |
1428 | static void on_assistant_close(GtkWidget *widget, wizard_t *wizard) { |
1429 | printf("close callback\n"); |
1430 | wizard->running = FALSE; |
1431 | } |
1432 | |
1433 | static GtkWidget *wizard_text(const char *text) { |
1434 | GtkTextBuffer *buffer = gtk_text_buffer_new(NULL); |
1435 | gtk_text_buffer_set_text(buffer, text, -1); |
1436 | |
1437 | #ifndef USE_HILDON_TEXT_VIEW |
1438 | GtkWidget *view = gtk_text_view_new_with_buffer(buffer); |
1439 | #else |
1440 | GtkWidget *view = hildon_text_view_new(); |
1441 | hildon_text_view_set_buffer(HILDON_TEXT_VIEW(view), buffer); |
1442 | #endif |
1443 | |
1444 | gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(view), GTK_WRAP_WORD); |
1445 | gtk_text_view_set_editable(GTK_TEXT_VIEW(view), FALSE); |
1446 | gtk_text_view_set_left_margin(GTK_TEXT_VIEW(view), 2 ); |
1447 | gtk_text_view_set_right_margin(GTK_TEXT_VIEW(view), 2 ); |
1448 | gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(view), FALSE ); |
1449 | |
1450 | return view; |
1451 | } |
1452 | |
1453 | /* ---------------- page 1: intro ----------------- */ |
1454 | static GtkWidget *wizard_create_intro_page(wizard_page_t *page) { |
1455 | static const char *text = |
1456 | "This wizard will guide you through the setup of a new project.\n\n" |
1457 | "An osm2go project covers a certain area of the world as seen " |
1458 | "by openstreetmap.org. The wizard will help you downloading " |
1459 | "the data describing that area and will enable you to make changes " |
1460 | "to it using osm2go."; |
1461 | |
1462 | return wizard_text(text); |
1463 | } |
1464 | |
1465 | /* ---------------- page 2: source selection ----------------- */ |
1466 | static gboolean gtk_widget_get_sensitive(GtkWidget *widget) { |
1467 | GValue is_sensitive= { 0, }; |
1468 | g_value_init(&is_sensitive, G_TYPE_BOOLEAN); |
1469 | g_object_get_property(G_OBJECT(widget), "sensitive", &is_sensitive); |
1470 | return g_value_get_boolean(&is_sensitive); |
1471 | } |
1472 | |
1473 | static void wizard_update_source_selection_page(wizard_page_t *page) { |
1474 | |
1475 | gboolean gps_on = page->wizard->appdata && |
1476 | page->wizard->appdata->settings && |
1477 | page->wizard->appdata->settings->enable_gps; |
1478 | gboolean gps_fix = gps_on && gps_get_pos(page->wizard->appdata, NULL, NULL); |
1479 | |
1480 | gtk_widget_set_sensitive(page->state.source_selection.check[0], gps_fix); |
1481 | if(gps_fix) |
1482 | gtk_label_set_text(GTK_LABEL(page->state.source_selection.label[0]), |
1483 | "(GPS has a valid position)"); |
1484 | else if(gps_on) |
1485 | gtk_label_set_text(GTK_LABEL(page->state.source_selection.label[0]), |
1486 | "(GPS has no valid position)"); |
1487 | else |
1488 | gtk_label_set_text(GTK_LABEL(page->state.source_selection.label[0]), |
1489 | "(GPS is disabled)"); |
1490 | |
1491 | #ifndef USE_HILDON |
1492 | gtk_widget_set_sensitive(page->state.source_selection.check[1], FALSE); |
1493 | gtk_label_set_text(GTK_LABEL(page->state.source_selection.label[1]), |
1494 | "(Maemo Mapper not available)"); |
1495 | |
1496 | #endif |
1497 | |
1498 | /* check if the user selected something that is actually selectable */ |
1499 | /* only allow him to continue then */ |
1500 | gboolean sel_ok = FALSE; |
1501 | int i; |
1502 | for(i=0;i<4;i++) { |
1503 | if(gtk_toggle_button_get_active( |
1504 | GTK_TOGGLE_BUTTON(page->state.source_selection.check[i]))) |
1505 | sel_ok = gtk_widget_get_sensitive(page->state.source_selection.check[i]); |
1506 | } |
1507 | |
1508 | /* set page to "completed" if a valid entry is selected */ |
1509 | gtk_assistant_set_page_complete( |
1510 | GTK_ASSISTANT(page->wizard->assistant), page->widget, sel_ok); |
1511 | } |
1512 | |
1513 | /* the user has changed the selected source, update dialog */ |
1514 | static void on_wizard_source_selection_toggled(GtkToggleButton *togglebutton, |
1515 | gpointer user_data) { |
1516 | if(gtk_toggle_button_get_active(togglebutton)) |
1517 | wizard_update_source_selection_page((wizard_page_t*)user_data); |
1518 | } |
1519 | |
1520 | static GtkWidget *wizard_create_source_selection_page(wizard_page_t *page) { |
1521 | GtkWidget *vbox = gtk_vbox_new(FALSE, 0); |
1522 | |
1523 | gtk_box_pack_start_defaults(GTK_BOX(vbox), |
1524 | wizard_text("Please choose how to determine the area you " |
1525 | "are planning to work on.")); |
1526 | |
1527 | GtkWidget *hbox = gtk_hbox_new(FALSE, 0); |
1528 | GtkWidget *vbox2 = gtk_vbox_new(FALSE, 0); |
1529 | |
1530 | /* add selection buttons */ |
1531 | int i; |
1532 | for(i=0;i<4;i++) { |
1533 | static const char *labels[] = { |
1534 | "Select from map", |
1535 | "Use current GPS position", |
1536 | "Get from Maemo Mapper", |
1537 | "Specify area manually" |
1538 | }; |
1539 | |
1540 | page->state.source_selection.check[i] = |
1541 | gtk_radio_button_new_with_label_from_widget( |
1542 | i?GTK_RADIO_BUTTON(page->state.source_selection.check[0]):NULL, |
1543 | _(labels[i])); |
1544 | g_signal_connect(G_OBJECT(page->state.source_selection.check[i]), |
1545 | "toggled", G_CALLBACK(on_wizard_source_selection_toggled), page); |
1546 | gtk_box_pack_start(GTK_BOX(vbox2), page->state.source_selection.check[i], |
1547 | TRUE, TRUE, 2); |
1548 | page->state.source_selection.label[i] = gtk_label_new(""); |
1549 | gtk_box_pack_start(GTK_BOX(vbox2), page->state.source_selection.label[i], |
1550 | TRUE, TRUE, 2); |
1551 | } |
1552 | |
1553 | gtk_box_pack_start(GTK_BOX(hbox), vbox2, TRUE, FALSE, 0); |
1554 | gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, TRUE, 0); |
1555 | return vbox; |
1556 | } |
1557 | |
1558 | /* this is called once a second while the wizard is running and can be used */ |
1559 | /* to update pages etc */ |
1560 | static gboolean wizard_update(gpointer data) { |
1561 | wizard_t *wizard = (wizard_t*)data; |
1562 | gint page = gtk_assistant_get_current_page(GTK_ASSISTANT(wizard->assistant)); |
1563 | |
1564 | if(wizard->page[page].update) |
1565 | ; // wizard->page[page].update(&wizard->page[page]); |
1566 | else |
1567 | printf("nothing to animate on page %d\n", page); |
1568 | |
1569 | return TRUE; |
1570 | } |
1571 | |
1572 | void project_wizard(appdata_t *appdata) { |
1573 | wizard_page_t page[] = { |
1574 | { "Introduction", wizard_create_intro_page, NULL, |
1575 | GTK_ASSISTANT_PAGE_INTRO, TRUE}, |
1576 | { "Area source selection", wizard_create_source_selection_page, |
1577 | wizard_update_source_selection_page, |
1578 | GTK_ASSISTANT_PAGE_CONTENT, FALSE}, |
1579 | { "Click the Check Button", NULL, NULL, |
1580 | GTK_ASSISTANT_PAGE_CONTENT, FALSE}, |
1581 | { "Click the Button", NULL, NULL, |
1582 | GTK_ASSISTANT_PAGE_PROGRESS, FALSE}, |
1583 | { "Confirmation", NULL, NULL, |
1584 | GTK_ASSISTANT_PAGE_CONFIRM, TRUE}, |
1585 | }; |
1586 | |
1587 | wizard_t wizard = { |
1588 | TRUE, |
1589 | |
1590 | /* the pages themselves */ |
1591 | sizeof(page) / sizeof(wizard_page_t), page, |
1592 | appdata, 0, NULL |
1593 | }; |
1594 | |
1595 | wizard.assistant = gtk_assistant_new(); |
1596 | gtk_widget_set_size_request(wizard.assistant, 450, 300); |
1597 | |
1598 | /* Add five pages to the GtkAssistant dialog. */ |
1599 | int i; |
1600 | for (i = 0; i < wizard.page_num; i++) { |
1601 | wizard.page[i].wizard = &wizard; |
1602 | |
1603 | if(wizard.page[i].setup) |
1604 | wizard.page[i].widget = |
1605 | wizard.page[i].setup(&wizard.page[i]); |
1606 | else { |
1607 | char *str = g_strdup_printf("Page %d", i); |
1608 | wizard.page[i].widget = gtk_label_new(str); |
1609 | g_free(str); |
1610 | } |
1611 | |
1612 | page[i].index = gtk_assistant_append_page(GTK_ASSISTANT(wizard.assistant), |
1613 | wizard.page[i].widget); |
1614 | |
1615 | gtk_assistant_set_page_title(GTK_ASSISTANT(wizard.assistant), |
1616 | wizard.page[i].widget, wizard.page[i].title); |
1617 | gtk_assistant_set_page_type(GTK_ASSISTANT(wizard.assistant), |
1618 | wizard.page[i].widget, wizard.page[i].type); |
1619 | |
1620 | /* Set the introduction and conclusion pages as complete so they can be |
1621 | * incremented or closed. */ |
1622 | gtk_assistant_set_page_complete(GTK_ASSISTANT(wizard.assistant), |
1623 | wizard.page[i].widget, wizard.page[i].complete); |
1624 | |
1625 | if(wizard.page[i].update) |
1626 | wizard.page[i].update(&wizard.page[i]); |
1627 | } |
1628 | |
1629 | /* install handler for timed updates */ |
1630 | wizard.handler_id = gtk_timeout_add(1000, wizard_update, &wizard); |
1631 | |
1632 | /* make it a modal subdialog of the main window */ |
1633 | gtk_window_set_modal(GTK_WINDOW(wizard.assistant), TRUE); |
1634 | gtk_window_set_transient_for(GTK_WINDOW(wizard.assistant), |
1635 | GTK_WINDOW(appdata->window)); |
1636 | |
1637 | gtk_widget_show_all(wizard.assistant); |
1638 | |
1639 | g_signal_connect(G_OBJECT(wizard.assistant), "destroy", |
1640 | G_CALLBACK(on_assistant_destroy), &wizard); |
1641 | |
1642 | g_signal_connect(G_OBJECT(wizard.assistant), "cancel", |
1643 | G_CALLBACK(on_assistant_cancel), &wizard); |
1644 | |
1645 | g_signal_connect(G_OBJECT(wizard.assistant), "close", |
1646 | G_CALLBACK(on_assistant_close), &wizard); |
1647 | |
1648 | do { |
1649 | if(gtk_events_pending()) |
1650 | gtk_main_iteration(); |
1651 | else |
1652 | usleep(1000); |
1653 | |
1654 | } while(wizard.running); |
1655 | |
1656 | gtk_timeout_remove(wizard.handler_id); |
1657 | |
1658 | gtk_widget_destroy(wizard.assistant); |
1659 | } |
1660 | |
1661 | |
1662 | // vim:et:ts=8:sw=2:sts=2:ai |