Parent Directory | Revision Log
Fremantle menu test
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 | list_button_enable(context->list, LIST_BUTTON_REMOVE, project != NULL); |
383 | list_button_enable(context->list, LIST_BUTTON_EDIT, project != NULL); |
384 | |
385 | /* check if the selected project also has a valid osm file */ |
386 | gtk_dialog_set_response_sensitive(GTK_DIALOG(context->dialog), |
387 | GTK_RESPONSE_ACCEPT, |
388 | project && osm_file_exists(project->path, project->osm)); |
389 | } |
390 | |
391 | static gboolean |
392 | view_selection_func(GtkTreeSelection *selection, GtkTreeModel *model, |
393 | GtkTreePath *path, gboolean path_currently_selected, |
394 | gpointer userdata) { |
395 | select_context_t *context = (select_context_t*)userdata; |
396 | GtkTreeIter iter; |
397 | |
398 | if(gtk_tree_model_get_iter(model, &iter, path)) { |
399 | project_t *project = NULL; |
400 | gtk_tree_model_get(model, &iter, PROJECT_COL_DATA, &project, -1); |
401 | g_assert(gtk_tree_path_get_depth(path) == 1); |
402 | |
403 | view_selected(context, project); |
404 | } |
405 | |
406 | return TRUE; /* allow selection state to change */ |
407 | } |
408 | |
409 | /* get the currently selected project in the list, NULL if none */ |
410 | static project_t *project_get_selected(GtkWidget *list) { |
411 | project_t *project = NULL; |
412 | GtkTreeModel *model; |
413 | GtkTreeIter iter; |
414 | |
415 | GtkTreeSelection *selection = list_get_selection(list); |
416 | g_assert(gtk_tree_selection_get_selected(selection, &model, &iter)); |
417 | gtk_tree_model_get(model, &iter, PROJECT_COL_DATA, &project, -1); |
418 | |
419 | return project; |
420 | } |
421 | |
422 | /* ------------------------- create a new project ---------------------- */ |
423 | |
424 | /* returns true of str contains one of the characters in chars */ |
425 | static gboolean strchrs(char *str, char *chars) { |
426 | while(*chars) { |
427 | char *p = str; |
428 | while(*p) { |
429 | if(*p == *chars) |
430 | return TRUE; |
431 | |
432 | p++; |
433 | } |
434 | chars++; |
435 | } |
436 | return FALSE; |
437 | } |
438 | |
439 | typedef struct { |
440 | GtkWidget *dialog; |
441 | settings_t *settings; |
442 | } name_callback_context_t; |
443 | |
444 | static void callback_modified_name(GtkWidget *widget, gpointer data) { |
445 | name_callback_context_t *context = (name_callback_context_t*)data; |
446 | |
447 | char *name = (char*)gtk_entry_get_text(GTK_ENTRY(widget)); |
448 | |
449 | /* name must not contain some special chars */ |
450 | gboolean ok = FALSE; |
451 | |
452 | /* check if there's a name */ |
453 | if(name && strlen(name) > 0) { |
454 | /* check if it consists of valid characters */ |
455 | if(!strchrs(name, "\\*?()\n\t\r")) { |
456 | /* check if such a project already exists */ |
457 | if(!project_exists(context->settings, name)) |
458 | ok = TRUE; |
459 | } |
460 | } |
461 | |
462 | gtk_dialog_set_response_sensitive(GTK_DIALOG(context->dialog), |
463 | GTK_RESPONSE_ACCEPT, ok); |
464 | } |
465 | |
466 | |
467 | gboolean project_delete(select_context_t *context, project_t *project) { |
468 | |
469 | printf("deleting project \"%s\"\n", project->name); |
470 | |
471 | /* check if we are to delete the currently open project */ |
472 | if(context->appdata->project && |
473 | !strcmp(context->appdata->project->name, project->name)) { |
474 | |
475 | if(!yes_no_f(context->dialog, NULL, 0, 0, |
476 | _("Delete current project?"), |
477 | _("The project you are about to delete is the one " |
478 | "you are currently working on!\n\n" |
479 | "Do you want to delete it anyway?"))) |
480 | return FALSE; |
481 | |
482 | project_close(context->appdata); |
483 | } |
484 | |
485 | /* remove entire directory from disk */ |
486 | GDir *dir = g_dir_open(project->path, 0, NULL); |
487 | const char *name = NULL; |
488 | do { |
489 | if((name = g_dir_read_name(dir))) { |
490 | char *fullname = g_strdup_printf("%s/%s", project->path, name); |
491 | g_remove(fullname); |
492 | g_free(fullname); |
493 | } |
494 | } while(name); |
495 | |
496 | /* remove the projects directory */ |
497 | g_remove(project->path); |
498 | |
499 | /* remove from view */ |
500 | GtkTreeIter iter; |
501 | GtkTreeModel *model = list_get_model(context->list); |
502 | gboolean deleted = FALSE; |
503 | if(gtk_tree_model_get_iter_first(model, &iter)) { |
504 | do { |
505 | project_t *prj = NULL; |
506 | gtk_tree_model_get(model, &iter, PROJECT_COL_DATA, &prj, -1); |
507 | if(prj && (prj == project)) { |
508 | printf("found %s to remove\n", prj->name); |
509 | /* and remove from store */ |
510 | gtk_list_store_remove(GTK_LIST_STORE(model), &iter); |
511 | deleted = TRUE; |
512 | } |
513 | } while(!deleted && gtk_tree_model_iter_next(model, &iter)); |
514 | } |
515 | |
516 | /* de-chain entry from project list */ |
517 | project_t **project_list = &context->project; |
518 | while(*project_list) { |
519 | if(*project_list == project) |
520 | *project_list = (*project_list)->next; |
521 | else |
522 | project_list = &((*project_list)->next); |
523 | } |
524 | |
525 | /* free project structure */ |
526 | project_free(project); |
527 | |
528 | /* disable edit/remove buttons */ |
529 | view_selected(context, NULL); |
530 | |
531 | return TRUE; |
532 | } |
533 | |
534 | project_t *project_new(select_context_t *context) { |
535 | printf("creating project with default values\n"); |
536 | |
537 | /* -------------- first choose a name for the project --------------- */ |
538 | GtkWidget *dialog = |
539 | misc_dialog_new(MISC_DIALOG_NOSIZE, _("Project name"), |
540 | GTK_WINDOW(context->dialog), |
541 | GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT, |
542 | GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, |
543 | NULL); |
544 | |
545 | GtkWidget *hbox = gtk_hbox_new(FALSE, 8); |
546 | gtk_box_pack_start_defaults(GTK_BOX(hbox), gtk_label_new(_("Name:"))); |
547 | |
548 | name_callback_context_t name_context = { dialog, context->settings }; |
549 | GtkWidget *entry = gtk_entry_new(); |
550 | gtk_box_pack_start_defaults(GTK_BOX(hbox), entry); |
551 | g_signal_connect(G_OBJECT(entry), "changed", |
552 | G_CALLBACK(callback_modified_name), &name_context); |
553 | |
554 | gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(dialog)->vbox), hbox); |
555 | |
556 | /* don't all user to click ok until something useful has been entered */ |
557 | gtk_dialog_set_response_sensitive(GTK_DIALOG(dialog), |
558 | GTK_RESPONSE_ACCEPT, FALSE); |
559 | |
560 | gtk_widget_show_all(dialog); |
561 | if(GTK_RESPONSE_ACCEPT != gtk_dialog_run(GTK_DIALOG(dialog))) { |
562 | gtk_widget_destroy(dialog); |
563 | return NULL; |
564 | } |
565 | |
566 | project_t *project = g_new0(project_t, 1); |
567 | project->name = g_strdup(gtk_entry_get_text(GTK_ENTRY(entry))); |
568 | gtk_widget_destroy(dialog); |
569 | |
570 | |
571 | project->path = g_strdup_printf("%s%s/", |
572 | context->settings->base_path, project->name); |
573 | project->desc = NULL; |
574 | |
575 | /* no data downloaded yet */ |
576 | project->data_dirty = TRUE; |
577 | |
578 | /* adjust default server stored in settings if required */ |
579 | if(strstr(context->settings->server, "0.5") != NULL) { |
580 | strstr(context->settings->server, "0.5")[2] = '6'; |
581 | printf("adjusting server path in settings to 0.6\n"); |
582 | } |
583 | |
584 | /* use global server/access settings */ |
585 | project->server = g_strdup(context->settings->server); |
586 | |
587 | /* build project osm file name */ |
588 | project->osm = g_strdup_printf("%s.osm", project->name); |
589 | |
590 | /* around the castle in karlsruhe, germany ... */ |
591 | project->min.lat = NAN; project->min.lon = NAN; |
592 | project->max.lat = NAN; project->max.lon = NAN; |
593 | |
594 | /* create project file on disk */ |
595 | project_save(context->dialog, project); |
596 | |
597 | if(!project_edit(context->appdata, context->dialog, |
598 | context->settings, project, TRUE)) { |
599 | printf("new/edit cancelled!!\n"); |
600 | |
601 | project_delete(context, project); |
602 | |
603 | project = NULL; |
604 | } |
605 | |
606 | /* enable/disable edit/remove buttons */ |
607 | view_selected(context, project); |
608 | |
609 | return project; |
610 | } |
611 | |
612 | // predecs |
613 | void project_get_status_icon_stock_id(project_t *project, gchar **stock_id); |
614 | |
615 | static void on_project_new(GtkButton *button, gpointer data) { |
616 | select_context_t *context = (select_context_t*)data; |
617 | project_t **project = &context->project; |
618 | *project = project_new(context); |
619 | if(*project) { |
620 | |
621 | GtkTreeModel *model = list_get_model(context->list); |
622 | |
623 | GtkTreeIter iter; |
624 | gchar *status_stock_id = NULL; |
625 | project_get_status_icon_stock_id(*project, &status_stock_id); |
626 | gtk_list_store_append(GTK_LIST_STORE(model), &iter); |
627 | gtk_list_store_set(GTK_LIST_STORE(model), &iter, |
628 | PROJECT_COL_NAME, (*project)->name, |
629 | PROJECT_COL_STATUS, status_stock_id, |
630 | PROJECT_COL_DESCRIPTION, (*project)->desc, |
631 | PROJECT_COL_DATA, *project, |
632 | -1); |
633 | |
634 | GtkTreeSelection *selection = list_get_selection(context->list); |
635 | gtk_tree_selection_select_iter(selection, &iter); |
636 | } |
637 | } |
638 | |
639 | static void on_project_delete(GtkButton *button, gpointer data) { |
640 | select_context_t *context = (select_context_t*)data; |
641 | project_t *project = project_get_selected(context->list); |
642 | |
643 | char *str = g_strdup_printf(_("Do you really want to delete the " |
644 | "project \"%s\"?"), project->name); |
645 | GtkWidget *dialog = gtk_message_dialog_new( |
646 | GTK_WINDOW(context->dialog), |
647 | GTK_DIALOG_DESTROY_WITH_PARENT, |
648 | GTK_MESSAGE_QUESTION, GTK_BUTTONS_YES_NO, str); |
649 | g_free(str); |
650 | |
651 | gtk_window_set_title(GTK_WINDOW(dialog), _("Delete project?")); |
652 | |
653 | /* set the active flag again if the user answered "no" */ |
654 | if(GTK_RESPONSE_NO == gtk_dialog_run(GTK_DIALOG(dialog))) { |
655 | gtk_widget_destroy(dialog); |
656 | return; |
657 | } |
658 | |
659 | gtk_widget_destroy(dialog); |
660 | |
661 | if(!project_delete(context, project)) |
662 | printf("unable to delete project\n"); |
663 | } |
664 | |
665 | static void on_project_edit(GtkButton *button, gpointer data) { |
666 | select_context_t *context = (select_context_t*)data; |
667 | project_t *project = project_get_selected(context->list); |
668 | g_assert(project); |
669 | |
670 | if(project_edit(context->appdata, context->dialog, |
671 | context->settings, project, FALSE)) { |
672 | GtkTreeModel *model; |
673 | GtkTreeIter iter; |
674 | |
675 | /* description etc. may have changed, so update list */ |
676 | GtkTreeSelection *selection = list_get_selection(context->list); |
677 | g_assert(gtk_tree_selection_get_selected(selection, &model, &iter)); |
678 | |
679 | // gtk_tree_model_get(model, &iter, PROJECT_COL_DATA, &project, -1); |
680 | gchar *status_stock_id = NULL; |
681 | project_get_status_icon_stock_id(project, &status_stock_id); |
682 | gtk_list_store_set(GTK_LIST_STORE(model), &iter, |
683 | PROJECT_COL_NAME, project->name, |
684 | PROJECT_COL_STATUS, status_stock_id, |
685 | PROJECT_COL_DESCRIPTION, project->desc, |
686 | -1); |
687 | |
688 | |
689 | /* check if we have actually editing the currently open project */ |
690 | if(context->appdata->project && |
691 | !strcmp(context->appdata->project->name, project->name)) { |
692 | project_t *cur = context->appdata->project; |
693 | |
694 | printf("edited project was actually the active one!\n"); |
695 | |
696 | /* update the currently active project also */ |
697 | |
698 | /* update description */ |
699 | if(cur->desc) { free(cur->desc); cur->desc = NULL; } |
700 | if(project->desc) cur->desc = g_strdup(project->desc); |
701 | |
702 | /* update server */ |
703 | if(cur->server) { free(cur->server); cur->server = NULL; } |
704 | if(project->server) cur->server = g_strdup(project->server); |
705 | |
706 | /* update coordinates */ |
707 | if((cur->min.lat != project->min.lat) || |
708 | (cur->max.lat != project->max.lat) || |
709 | (cur->min.lon != project->min.lon) || |
710 | (cur->max.lon != project->max.lon)) { |
711 | appdata_t *appdata = context->appdata; |
712 | |
713 | /* save modified coordinates */ |
714 | cur->min.lat = project->min.lat; |
715 | cur->max.lat = project->max.lat; |
716 | cur->min.lon = project->min.lon; |
717 | cur->max.lon = project->max.lon; |
718 | |
719 | /* try to do this automatically */ |
720 | |
721 | /* if we have valid osm data loaded: save state first */ |
722 | if(appdata->osm) { |
723 | /* redraw the entire map by destroying all map items */ |
724 | diff_save(appdata->project, appdata->osm); |
725 | map_clear(appdata, MAP_LAYER_ALL); |
726 | osm_free(&appdata->icon, appdata->osm); |
727 | |
728 | appdata->osm = NULL; |
729 | } |
730 | |
731 | /* and load the (hopefully) new file */ |
732 | appdata->osm = osm_parse(appdata->project->path, |
733 | appdata->project->osm); |
734 | diff_restore(appdata, appdata->project, appdata->osm); |
735 | map_paint(appdata); |
736 | |
737 | main_ui_enable(appdata); |
738 | } |
739 | } |
740 | } |
741 | |
742 | /* enable/disable edit/remove buttons */ |
743 | view_selected(context, project); |
744 | } |
745 | |
746 | |
747 | gboolean project_osm_present(project_t *project) { |
748 | char *osm_name = g_strdup_printf("%s/%s.osm", project->path, project->name); |
749 | gboolean is_present = g_file_test(osm_name, G_FILE_TEST_EXISTS); |
750 | g_free(osm_name); |
751 | return is_present; |
752 | } |
753 | |
754 | void project_get_status_icon_stock_id(project_t *project, gchar **stock_id) { |
755 | *stock_id = (! project_osm_present(project)) ? GTK_STOCK_DIALOG_WARNING |
756 | : diff_present(project) ? GTK_STOCK_PROPERTIES |
757 | : GTK_STOCK_FILE; |
758 | // TODO: check for outdatedness too. Which icon to use? |
759 | } |
760 | |
761 | static GtkWidget *project_list_widget(select_context_t *context) { |
762 | context->list = list_new(LIST_HILDON_WITHOUT_HEADERS); |
763 | |
764 | list_set_selection_function(context->list, view_selection_func, context); |
765 | |
766 | list_set_columns(context->list, |
767 | _("Name"), PROJECT_COL_NAME, 0, |
768 | _("State"), PROJECT_COL_STATUS, LIST_FLAG_STOCK_ICON, |
769 | _("Description"), PROJECT_COL_DESCRIPTION, LIST_FLAG_ELLIPSIZE, |
770 | NULL); |
771 | |
772 | |
773 | /* build the store */ |
774 | GtkListStore *store = gtk_list_store_new(PROJECT_NUM_COLS, |
775 | G_TYPE_STRING, // name |
776 | G_TYPE_STRING, // status |
777 | G_TYPE_STRING, // desc |
778 | G_TYPE_POINTER); // data |
779 | |
780 | GtkTreeIter iter; |
781 | project_t *project = context->project; |
782 | while(project) { |
783 | gchar *status_stock_id = NULL; |
784 | project_get_status_icon_stock_id(project, &status_stock_id); |
785 | /* Append a row and fill in some data */ |
786 | gtk_list_store_append(store, &iter); |
787 | gtk_list_store_set(store, &iter, |
788 | PROJECT_COL_NAME, project->name, |
789 | PROJECT_COL_STATUS, status_stock_id, |
790 | PROJECT_COL_DESCRIPTION, project->desc, |
791 | PROJECT_COL_DATA, project, |
792 | -1); |
793 | project = project->next; |
794 | } |
795 | |
796 | list_set_store(context->list, store); |
797 | g_object_unref(store); |
798 | |
799 | list_set_static_buttons(context->list, TRUE, G_CALLBACK(on_project_new), |
800 | G_CALLBACK(on_project_edit), G_CALLBACK(on_project_delete), context); |
801 | |
802 | return context->list; |
803 | } |
804 | |
805 | static char *project_select(appdata_t *appdata) { |
806 | char *name = NULL; |
807 | |
808 | select_context_t *context = g_new0(select_context_t, 1); |
809 | context->appdata = appdata; |
810 | context->settings = appdata->settings; |
811 | context->project = project_scan(appdata); |
812 | |
813 | /* create project selection dialog */ |
814 | context->dialog = |
815 | misc_dialog_new(MISC_DIALOG_MEDIUM,_("Project selection"), |
816 | GTK_WINDOW(appdata->window), |
817 | GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT, |
818 | GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT, |
819 | NULL); |
820 | |
821 | gtk_dialog_set_default_response(GTK_DIALOG(context->dialog), |
822 | GTK_RESPONSE_ACCEPT); |
823 | |
824 | gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(context->dialog)->vbox), |
825 | project_list_widget(context)); |
826 | |
827 | /* don't all user to click ok until something is selected */ |
828 | gtk_dialog_set_response_sensitive(GTK_DIALOG(context->dialog), |
829 | GTK_RESPONSE_ACCEPT, FALSE); |
830 | |
831 | gtk_widget_show_all(context->dialog); |
832 | if(GTK_RESPONSE_ACCEPT == gtk_dialog_run(GTK_DIALOG(context->dialog))) |
833 | name = g_strdup(project_get_selected(context->list)->name); |
834 | |
835 | gtk_widget_destroy(context->dialog); |
836 | |
837 | /* free all entries */ |
838 | project_t *project = context->project; |
839 | while(project) { |
840 | project_t *next = project->next; |
841 | project_free(project); |
842 | project = next; |
843 | } |
844 | |
845 | g_free(context); |
846 | |
847 | return name; |
848 | } |
849 | |
850 | /* ---------------------------------------------------- */ |
851 | |
852 | /* return file length or -1 on error */ |
853 | static gsize file_length(char *path, char *name) { |
854 | char *str = NULL; |
855 | |
856 | if(name[0] == '/') str = g_strdup(name); |
857 | else str = g_strjoin(NULL, path, name, NULL); |
858 | |
859 | GMappedFile *gmap = g_mapped_file_new(str, FALSE, NULL); |
860 | g_free(str); |
861 | |
862 | if(!gmap) return -1; |
863 | gsize size = g_mapped_file_get_length(gmap); |
864 | g_mapped_file_free(gmap); |
865 | return size; |
866 | } |
867 | |
868 | void project_filesize(project_context_t *context) { |
869 | char *str = NULL; |
870 | |
871 | printf("Checking size of %s\n", context->project->osm); |
872 | |
873 | if(!osm_file_exists(context->project->path, context->project->osm)) { |
874 | GdkColor color; |
875 | gdk_color_parse("red", &color); |
876 | gtk_widget_modify_fg(context->fsize, GTK_STATE_NORMAL, &color); |
877 | |
878 | str = g_strdup(_("Not downloaded!")); |
879 | |
880 | gtk_dialog_set_response_sensitive(GTK_DIALOG(context->dialog), |
881 | GTK_RESPONSE_ACCEPT, !context->is_new); |
882 | |
883 | } else { |
884 | gtk_widget_modify_fg(context->fsize, GTK_STATE_NORMAL, NULL); |
885 | |
886 | if(!context->project->data_dirty) |
887 | str = g_strdup_printf(_("%d bytes present"), |
888 | file_length(context->project->path, |
889 | context->project->osm)); |
890 | else |
891 | str = g_strdup_printf(_("Outdated, please download!")); |
892 | |
893 | gtk_dialog_set_response_sensitive(GTK_DIALOG(context->dialog), |
894 | GTK_RESPONSE_ACCEPT, !context->is_new || |
895 | !context->project->data_dirty); |
896 | } |
897 | |
898 | if(str) { |
899 | gtk_label_set_text(GTK_LABEL(context->fsize), str); |
900 | g_free(str); |
901 | } |
902 | } |
903 | |
904 | void project_diffstat(project_context_t *context) { |
905 | char *str = NULL; |
906 | |
907 | if(diff_present(context->project)) { |
908 | /* this should prevent the user from changing the area */ |
909 | str = g_strdup(_("unsaved changes pending")); |
910 | } else |
911 | str = g_strdup(_("no pending changes")); |
912 | |
913 | gtk_label_set_text(GTK_LABEL(context->diff_stat), str); |
914 | g_free(str); |
915 | } |
916 | |
917 | static gboolean |
918 | project_pos_is_valid(project_t *project) { |
919 | return(!isnan(project->min.lat) && |
920 | !isnan(project->min.lon) && |
921 | !isnan(project->max.lat) && |
922 | !isnan(project->max.lon)); |
923 | } |
924 | |
925 | static void on_edit_clicked(GtkButton *button, gpointer data) { |
926 | project_context_t *context = (project_context_t*)data; |
927 | |
928 | if(diff_present(context->project)) { |
929 | if(!yes_no_f(context->dialog, NULL, 0, 0, |
930 | _("Discard pending changes?"), |
931 | _("You have pending changes in this project. Changing " |
932 | "the area will discard these changes.\n\nDo you want to " |
933 | "discard all your changes?"))) |
934 | return; |
935 | |
936 | diff_remove(context->project); |
937 | project_diffstat(context); |
938 | gtk_widget_set_sensitive(context->diff_remove, FALSE); |
939 | } |
940 | |
941 | if(area_edit(&context->area_edit)) { |
942 | printf("coordinates changed!!\n"); |
943 | |
944 | pos_lon_label_set(context->minlat, context->project->min.lat); |
945 | pos_lon_label_set(context->minlon, context->project->min.lon); |
946 | pos_lon_label_set(context->maxlat, context->project->max.lat); |
947 | pos_lon_label_set(context->maxlon, context->project->max.lon); |
948 | |
949 | gtk_widget_set_sensitive(context->download, |
950 | project_pos_is_valid(context->project)); |
951 | |
952 | /* (re-) download area */ |
953 | if(osm_download(GTK_WIDGET(context->dialog), |
954 | context->area_edit.appdata->settings, context->project)) |
955 | context->project->data_dirty = FALSE; |
956 | |
957 | project_filesize(context); |
958 | } |
959 | } |
960 | |
961 | static void on_download_clicked(GtkButton *button, gpointer data) { |
962 | project_context_t *context = (project_context_t*)data; |
963 | |
964 | printf("download %s\n", context->project->osm); |
965 | |
966 | if(osm_download(context->dialog, context->settings, context->project)) |
967 | context->project->data_dirty = FALSE; |
968 | else |
969 | printf("download failed\n"); |
970 | |
971 | project_filesize(context); |
972 | } |
973 | |
974 | static void on_diff_remove_clicked(GtkButton *button, gpointer data) { |
975 | project_context_t *context = (project_context_t*)data; |
976 | |
977 | printf("clicked diff remove\n"); |
978 | |
979 | GtkWidget *dialog = gtk_message_dialog_new( |
980 | GTK_WINDOW(context->dialog), |
981 | GTK_DIALOG_DESTROY_WITH_PARENT, |
982 | GTK_MESSAGE_QUESTION, GTK_BUTTONS_YES_NO, |
983 | _("Do you really want to discard your changes? This " |
984 | "permanently undo all changes you've made so far and which " |
985 | "you didn't upload yet.")); |
986 | |
987 | gtk_window_set_title(GTK_WINDOW(dialog), _("Discard changes?")); |
988 | |
989 | /* set the active flag again if the user answered "no" */ |
990 | if(GTK_RESPONSE_YES == gtk_dialog_run(GTK_DIALOG(dialog))) { |
991 | diff_remove(context->project); |
992 | project_diffstat(context); |
993 | gtk_widget_set_sensitive(context->diff_remove, FALSE); |
994 | } |
995 | |
996 | gtk_widget_destroy(dialog); |
997 | } |
998 | |
999 | gboolean project_check_demo(GtkWidget *parent, project_t *project) { |
1000 | if(!project->server) |
1001 | messagef(parent, "Demo project", |
1002 | "This is a preinstalled demo project. This means that the " |
1003 | "basic project parameters cannot be changed and no data can " |
1004 | "be up- or downloaded via the OSM servers.\n\n" |
1005 | "Please setup a new project to do these things."); |
1006 | |
1007 | return !project->server; |
1008 | } |
1009 | |
1010 | /* create a left aligned label (normal ones are centered) */ |
1011 | static GtkWidget *gtk_label_left_new(char *str) { |
1012 | GtkWidget *label = gtk_label_new(str); |
1013 | gtk_misc_set_alignment(GTK_MISC(label), 0.f, .5f); |
1014 | return label; |
1015 | } |
1016 | |
1017 | static gboolean |
1018 | project_edit(appdata_t *appdata, GtkWidget *parent, settings_t *settings, |
1019 | project_t *project, gboolean is_new) { |
1020 | gboolean ok = FALSE; |
1021 | |
1022 | if(project_check_demo(parent, project)) |
1023 | return ok; |
1024 | |
1025 | /* ------------ project edit dialog ------------- */ |
1026 | |
1027 | project_context_t *context = g_new0(project_context_t, 1); |
1028 | context->project = project; |
1029 | context->area_edit.settings = context->settings = settings; |
1030 | context->area_edit.appdata = appdata; |
1031 | context->is_new = is_new; |
1032 | |
1033 | context->area_edit.min = &project->min; |
1034 | context->area_edit.max = &project->max; |
1035 | |
1036 | /* cancel is enabled for "new" projects only */ |
1037 | if(is_new) { |
1038 | char *str = g_strdup_printf(_("New project - %s"), project->name); |
1039 | |
1040 | context->area_edit.parent = |
1041 | context->dialog = misc_dialog_new(MISC_DIALOG_WIDE, str, |
1042 | GTK_WINDOW(parent), |
1043 | GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT, |
1044 | GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, NULL); |
1045 | g_free(str); |
1046 | } else { |
1047 | char *str = g_strdup_printf(_("Edit project - %s"), project->name); |
1048 | |
1049 | context->area_edit.parent = |
1050 | context->dialog = misc_dialog_new(MISC_DIALOG_WIDE, str, |
1051 | GTK_WINDOW(parent), |
1052 | GTK_STOCK_CLOSE, GTK_RESPONSE_ACCEPT, NULL); |
1053 | g_free(str); |
1054 | } |
1055 | |
1056 | gtk_dialog_set_default_response(GTK_DIALOG(context->dialog), |
1057 | GTK_RESPONSE_ACCEPT); |
1058 | |
1059 | GtkWidget *label; |
1060 | GtkWidget *table = gtk_table_new(5, 5, FALSE); // x, y |
1061 | gtk_table_set_col_spacing(GTK_TABLE(table), 0, 8); |
1062 | gtk_table_set_col_spacing(GTK_TABLE(table), 3, 8); |
1063 | |
1064 | label = gtk_label_left_new(_("Description:")); |
1065 | gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, 0, 1); |
1066 | context->desc = gtk_entry_new(); |
1067 | gtk_entry_set_activates_default(GTK_ENTRY(context->desc), TRUE); |
1068 | if(project->desc) |
1069 | gtk_entry_set_text(GTK_ENTRY(context->desc), project->desc); |
1070 | gtk_table_attach_defaults(GTK_TABLE(table), context->desc, 1, 4, 0, 1); |
1071 | gtk_table_set_row_spacing(GTK_TABLE(table), 0, 4); |
1072 | |
1073 | label = gtk_label_left_new(_("Latitude:")); |
1074 | gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, 1, 2); |
1075 | context->minlat = pos_lat_label_new(project->min.lat); |
1076 | gtk_table_attach_defaults(GTK_TABLE(table), context->minlat, 1, 2, 1, 2); |
1077 | label = gtk_label_new(_("to")); |
1078 | gtk_table_attach_defaults(GTK_TABLE(table), label, 2, 3, 1, 2); |
1079 | context->maxlat = pos_lon_label_new(project->max.lat); |
1080 | gtk_table_attach_defaults(GTK_TABLE(table), context->maxlat, 3, 4, 1, 2); |
1081 | |
1082 | label = gtk_label_left_new(_("Longitude:")); |
1083 | gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, 2, 3); |
1084 | context->minlon = pos_lat_label_new(project->min.lon); |
1085 | gtk_table_attach_defaults(GTK_TABLE(table), context->minlon, 1, 2, 2, 3); |
1086 | label = gtk_label_new(_("to")); |
1087 | gtk_table_attach_defaults(GTK_TABLE(table), label, 2, 3, 2, 3); |
1088 | context->maxlon = pos_lon_label_new(project->max.lon); |
1089 | gtk_table_attach_defaults(GTK_TABLE(table), context->maxlon, 3, 4, 2, 3); |
1090 | |
1091 | GtkWidget *edit = gtk_button_new_with_label(_("Edit")); |
1092 | gtk_signal_connect(GTK_OBJECT(edit), "clicked", |
1093 | (GtkSignalFunc)on_edit_clicked, context); |
1094 | gtk_table_attach(GTK_TABLE(table), edit, 4, 5, 1, 3, |
1095 | GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL,0,0); |
1096 | |
1097 | gtk_table_set_row_spacing(GTK_TABLE(table), 2, 4); |
1098 | |
1099 | #ifdef SERVER_EDITABLE |
1100 | label = gtk_label_left_new(_("Server:")); |
1101 | gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, 3, 4); |
1102 | context->server = gtk_entry_new(); |
1103 | gtk_entry_set_activates_default(GTK_ENTRY(context->server), TRUE); |
1104 | HILDON_ENTRY_NO_AUTOCAP(context->server); |
1105 | gtk_entry_set_text(GTK_ENTRY(context->server), project->server); |
1106 | gtk_table_attach_defaults(GTK_TABLE(table), context->server, 1, 4, 3, 4); |
1107 | |
1108 | gtk_table_set_row_spacing(GTK_TABLE(table), 3, 4); |
1109 | #endif |
1110 | |
1111 | label = gtk_label_left_new(_("Map data:")); |
1112 | gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, 4, 5); |
1113 | context->fsize = gtk_label_left_new(_("")); |
1114 | project_filesize(context); |
1115 | gtk_table_attach_defaults(GTK_TABLE(table), context->fsize, 1, 4, 4, 5); |
1116 | context->download = gtk_button_new_with_label(_("Download")); |
1117 | gtk_signal_connect(GTK_OBJECT(context->download), "clicked", |
1118 | (GtkSignalFunc)on_download_clicked, context); |
1119 | gtk_widget_set_sensitive(context->download, project_pos_is_valid(project)); |
1120 | |
1121 | gtk_table_attach_defaults(GTK_TABLE(table), context->download, 4, 5, 4, 5); |
1122 | |
1123 | gtk_table_set_row_spacing(GTK_TABLE(table), 4, 4); |
1124 | |
1125 | label = gtk_label_left_new(_("Changes:")); |
1126 | gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, 5, 6); |
1127 | context->diff_stat = gtk_label_left_new(_("")); |
1128 | project_diffstat(context); |
1129 | gtk_table_attach_defaults(GTK_TABLE(table), context->diff_stat, 1, 4, 5, 6); |
1130 | context->diff_remove = gtk_button_new_with_label(_("Undo all")); |
1131 | if(!diff_present(project)) |
1132 | gtk_widget_set_sensitive(context->diff_remove, FALSE); |
1133 | gtk_signal_connect(GTK_OBJECT(context->diff_remove), "clicked", |
1134 | (GtkSignalFunc)on_diff_remove_clicked, context); |
1135 | gtk_table_attach_defaults(GTK_TABLE(table), context->diff_remove, 4, 5, 5, 6); |
1136 | |
1137 | /* ---------------------------------------------------------------- */ |
1138 | |
1139 | gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(context->dialog)->vbox), |
1140 | table); |
1141 | |
1142 | /* disable "ok" if there's no valid file downloaded */ |
1143 | if(is_new) |
1144 | gtk_dialog_set_response_sensitive(GTK_DIALOG(context->dialog), |
1145 | GTK_RESPONSE_ACCEPT, |
1146 | osm_file_exists(project->path, project->name)); |
1147 | |
1148 | gtk_widget_show_all(context->dialog); |
1149 | |
1150 | /* the return value may actually be != ACCEPT, but only if the editor */ |
1151 | /* is run for a new project which is completely removed afterwards if */ |
1152 | /* cancel has been selected */ |
1153 | ok = (GTK_RESPONSE_ACCEPT == gtk_dialog_run(GTK_DIALOG(context->dialog))); |
1154 | |
1155 | /* transfer values from edit dialog into project structure */ |
1156 | |
1157 | /* fetch values from dialog */ |
1158 | if(context->project->desc) g_free(context->project->desc); |
1159 | context->project->desc = g_strdup(gtk_entry_get_text( |
1160 | GTK_ENTRY(context->desc))); |
1161 | if(strlen(context->project->desc) == 0) { |
1162 | g_free(context->project->desc); |
1163 | context->project->desc = NULL; |
1164 | } |
1165 | |
1166 | #ifdef SERVER_EDITABLE |
1167 | if(context->project->server) g_free(context->project->server); |
1168 | context->project->server = g_strdup(gtk_entry_get_text( |
1169 | GTK_ENTRY(context->server))); |
1170 | if(strlen(context->project->server) == 0) { |
1171 | g_free(context->project->server); |
1172 | context->project->server = NULL; |
1173 | } |
1174 | #endif |
1175 | |
1176 | /* save project */ |
1177 | project_save(context->dialog, project); |
1178 | |
1179 | gtk_widget_destroy(context->dialog); |
1180 | g_free(context); |
1181 | |
1182 | return ok; |
1183 | } |
1184 | |
1185 | gboolean project_open(appdata_t *appdata, char *name) { |
1186 | project_t *project = g_new0(project_t, 1); |
1187 | |
1188 | /* link to map state if a map already exists */ |
1189 | if(appdata->map) { |
1190 | printf("Project: Using map state\n"); |
1191 | project->map_state = appdata->map->state; |
1192 | } else { |
1193 | printf("Project: Creating new map_state\n"); |
1194 | project->map_state = map_state_new(); |
1195 | } |
1196 | |
1197 | map_state_reset(project->map_state); |
1198 | project->map_state->refcount++; |
1199 | |
1200 | /* build project path */ |
1201 | project->path = g_strdup_printf("%s%s/", |
1202 | appdata->settings->base_path, name); |
1203 | project->name = g_strdup(name); |
1204 | |
1205 | char *project_file = g_strdup_printf("%s%s.proj", project->path, name); |
1206 | |
1207 | printf("project file = %s\n", project_file); |
1208 | if(!g_file_test(project_file, G_FILE_TEST_IS_REGULAR)) { |
1209 | printf("requested project file doesn't exist\n"); |
1210 | project_free(project); |
1211 | g_free(project_file); |
1212 | return FALSE; |
1213 | } |
1214 | |
1215 | if(!project_read(appdata, project_file, project)) { |
1216 | printf("error reading project file\n"); |
1217 | project_free(project); |
1218 | g_free(project_file); |
1219 | return FALSE; |
1220 | } |
1221 | |
1222 | g_free(project_file); |
1223 | |
1224 | /* --------- project structure ok: load its OSM file --------- */ |
1225 | appdata->project = project; |
1226 | |
1227 | printf("project_open: loading osm %s\n", project->osm); |
1228 | appdata->osm = osm_parse(project->path, project->osm); |
1229 | if(!appdata->osm) { |
1230 | printf("OSM parsing failed\n"); |
1231 | return FALSE; |
1232 | } |
1233 | |
1234 | printf("parsing ok\n"); |
1235 | |
1236 | return TRUE; |
1237 | } |
1238 | |
1239 | gboolean project_close(appdata_t *appdata) { |
1240 | if(!appdata->project) return FALSE; |
1241 | |
1242 | printf("closing current project\n"); |
1243 | |
1244 | /* redraw the entire map by destroying all map items and redrawing them */ |
1245 | if(appdata->osm) |
1246 | diff_save(appdata->project, appdata->osm); |
1247 | |
1248 | /* Save track and turn off the handler callback */ |
1249 | track_save(appdata->project, appdata->track.track); |
1250 | track_clear(appdata, appdata->track.track); |
1251 | appdata->track.track = NULL; |
1252 | |
1253 | map_clear(appdata, MAP_LAYER_ALL); |
1254 | |
1255 | if(appdata->osm) { |
1256 | osm_free(&appdata->icon, appdata->osm); |
1257 | appdata->osm = NULL; |
1258 | } |
1259 | |
1260 | /* update project file on disk */ |
1261 | project_save(GTK_WIDGET(appdata->window), appdata->project); |
1262 | |
1263 | project_free(appdata->project); |
1264 | appdata->project = NULL; |
1265 | |
1266 | return TRUE; |
1267 | } |
1268 | |
1269 | #define _PROJECT_LOAD_BUF_SIZ 64 |
1270 | |
1271 | gboolean project_load(appdata_t *appdata, char *name) { |
1272 | char *proj_name = NULL; |
1273 | |
1274 | if(!name) { |
1275 | /* make user select a project */ |
1276 | proj_name = project_select(appdata); |
1277 | if(!proj_name) { |
1278 | printf("no project selected\n"); |
1279 | return FALSE; |
1280 | } |
1281 | } |
1282 | else { |
1283 | proj_name = g_strdup(name); |
1284 | } |
1285 | |
1286 | char banner_txt[_PROJECT_LOAD_BUF_SIZ]; |
1287 | memset(banner_txt, 0, _PROJECT_LOAD_BUF_SIZ); |
1288 | |
1289 | snprintf(banner_txt, _PROJECT_LOAD_BUF_SIZ, _("Loading %s"), proj_name); |
1290 | banner_busy_start(appdata, TRUE, banner_txt); |
1291 | |
1292 | /* close current project */ |
1293 | banner_busy_tick(); |
1294 | if(appdata->project) |
1295 | project_close(appdata); |
1296 | |
1297 | /* open project itself */ |
1298 | banner_busy_tick(); |
1299 | if(!project_open(appdata, proj_name)) { |
1300 | printf("error opening requested project\n"); |
1301 | |
1302 | if(appdata->project) { |
1303 | project_free(appdata->project); |
1304 | appdata->project = NULL; |
1305 | } |
1306 | |
1307 | if(appdata->osm) { |
1308 | osm_free(&appdata->icon, appdata->osm); |
1309 | appdata->osm = NULL; |
1310 | } |
1311 | |
1312 | snprintf(banner_txt, _PROJECT_LOAD_BUF_SIZ, |
1313 | _("Error opening %s"), proj_name); |
1314 | banner_busy_stop(appdata); |
1315 | banner_show_info(appdata, banner_txt); |
1316 | |
1317 | g_free(proj_name); |
1318 | return FALSE; |
1319 | } |
1320 | |
1321 | /* check if OSM data is valid */ |
1322 | banner_busy_tick(); |
1323 | if(!osm_sanity_check(GTK_WIDGET(appdata->window), appdata->osm)) { |
1324 | printf("project/osm sanity checks failed, unloading project\n"); |
1325 | |
1326 | if(appdata->project) { |
1327 | project_free(appdata->project); |
1328 | appdata->project = NULL; |
1329 | } |
1330 | |
1331 | if(appdata->osm) { |
1332 | osm_free(&appdata->icon, appdata->osm); |
1333 | appdata->osm = NULL; |
1334 | } |
1335 | |
1336 | snprintf(banner_txt, _PROJECT_LOAD_BUF_SIZ, |
1337 | _("Error opening %s"), proj_name); |
1338 | banner_busy_stop(appdata); |
1339 | banner_show_info(appdata, banner_txt); |
1340 | |
1341 | g_free(proj_name); |
1342 | return FALSE; |
1343 | } |
1344 | |
1345 | /* load diff possibly preset */ |
1346 | banner_busy_tick(); |
1347 | diff_restore(appdata, appdata->project, appdata->osm); |
1348 | |
1349 | /* prepare colors etc, draw data and adjust scroll/zoom settings */ |
1350 | banner_busy_tick(); |
1351 | map_init(appdata); |
1352 | |
1353 | /* restore a track */ |
1354 | banner_busy_tick(); |
1355 | appdata->track.track = track_restore(appdata, appdata->project); |
1356 | if(appdata->track.track) |
1357 | map_track_draw(appdata->map, appdata->track.track); |
1358 | |
1359 | /* finally load a background if present */ |
1360 | banner_busy_tick(); |
1361 | wms_load(appdata); |
1362 | |
1363 | /* save the name of the project for the perferences */ |
1364 | if(appdata->settings->project) |
1365 | g_free(appdata->settings->project); |
1366 | appdata->settings->project = g_strdup(appdata->project->name); |
1367 | |
1368 | banner_busy_stop(appdata); |
1369 | |
1370 | #if 0 |
1371 | snprintf(banner_txt, _PROJECT_LOAD_BUF_SIZ, _("Loaded %s"), proj_name); |
1372 | banner_show_info(appdata, banner_txt); |
1373 | #endif |
1374 | |
1375 | statusbar_set(appdata, NULL, 0); |
1376 | |
1377 | g_free(proj_name); |
1378 | return TRUE; |
1379 | } |
1380 | |
1381 | /* ------------------- project setup wizard ----------------- */ |
1382 | |
1383 | struct wizard_s; |
1384 | |
1385 | typedef struct wizard_page_s { |
1386 | const gchar *title; |
1387 | GtkWidget* (*setup)(struct wizard_page_s *page); |
1388 | void (*update)(struct wizard_page_s *page); |
1389 | GtkAssistantPageType type; |
1390 | gboolean complete; |
1391 | /* everything before here is initialized statically */ |
1392 | |
1393 | struct wizard_s *wizard; |
1394 | GtkWidget *widget; |
1395 | gint index; |
1396 | |
1397 | union { |
1398 | struct { |
1399 | GtkWidget *check[3]; |
1400 | GtkWidget *label[3]; |
1401 | } source_selection; |
1402 | |
1403 | } state; |
1404 | |
1405 | } wizard_page_t; |
1406 | |
1407 | typedef struct wizard_s { |
1408 | gboolean running; |
1409 | |
1410 | int page_num; |
1411 | wizard_page_t *page; |
1412 | appdata_t *appdata; |
1413 | guint handler_id; |
1414 | GtkWidget *assistant; |
1415 | } wizard_t; |
1416 | |
1417 | |
1418 | static gint on_assistant_destroy(GtkWidget *widget, wizard_t *wizard) { |
1419 | printf("destroy callback\n"); |
1420 | wizard->running = FALSE; |
1421 | return FALSE; |
1422 | } |
1423 | |
1424 | static void on_assistant_cancel(GtkWidget *widget, wizard_t *wizard) { |
1425 | printf("cancel callback\n"); |
1426 | wizard->running = FALSE; |
1427 | } |
1428 | |
1429 | static void on_assistant_close(GtkWidget *widget, wizard_t *wizard) { |
1430 | printf("close callback\n"); |
1431 | wizard->running = FALSE; |
1432 | } |
1433 | |
1434 | static GtkWidget *wizard_text(const char *text) { |
1435 | GtkTextBuffer *buffer = gtk_text_buffer_new(NULL); |
1436 | gtk_text_buffer_set_text(buffer, text, -1); |
1437 | |
1438 | #ifndef USE_HILDON_TEXT_VIEW |
1439 | GtkWidget *view = gtk_text_view_new_with_buffer(buffer); |
1440 | #else |
1441 | GtkWidget *view = hildon_text_view_new(); |
1442 | hildon_text_view_set_buffer(HILDON_TEXT_VIEW(view), buffer); |
1443 | #endif |
1444 | |
1445 | gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(view), GTK_WRAP_WORD); |
1446 | gtk_text_view_set_editable(GTK_TEXT_VIEW(view), FALSE); |
1447 | gtk_text_view_set_left_margin(GTK_TEXT_VIEW(view), 2 ); |
1448 | gtk_text_view_set_right_margin(GTK_TEXT_VIEW(view), 2 ); |
1449 | gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(view), FALSE ); |
1450 | |
1451 | return view; |
1452 | } |
1453 | |
1454 | /* ---------------- page 1: intro ----------------- */ |
1455 | static GtkWidget *wizard_create_intro_page(wizard_page_t *page) { |
1456 | static const char *text = |
1457 | "This wizard will guide you through the setup of a new project.\n\n" |
1458 | "An osm2go project covers a certain area of the world as seen " |
1459 | "by openstreetmap.org. The wizard will help you downloading " |
1460 | "the data describing that area and will enable you to make changes " |
1461 | "to it using osm2go."; |
1462 | |
1463 | return wizard_text(text); |
1464 | } |
1465 | |
1466 | /* ---------------- page 2: source selection ----------------- */ |
1467 | static gboolean gtk_widget_get_sensitive(GtkWidget *widget) { |
1468 | GValue is_sensitive= { 0, }; |
1469 | g_value_init(&is_sensitive, G_TYPE_BOOLEAN); |
1470 | g_object_get_property(G_OBJECT(widget), "sensitive", &is_sensitive); |
1471 | return g_value_get_boolean(&is_sensitive); |
1472 | } |
1473 | |
1474 | static void wizard_update_source_selection_page(wizard_page_t *page) { |
1475 | |
1476 | gboolean gps_on = page->wizard->appdata && |
1477 | page->wizard->appdata->settings && |
1478 | page->wizard->appdata->settings->enable_gps; |
1479 | gboolean gps_fix = gps_on && gps_get_pos(page->wizard->appdata, NULL, NULL); |
1480 | |
1481 | gtk_widget_set_sensitive(page->state.source_selection.check[0], gps_fix); |
1482 | if(gps_fix) |
1483 | gtk_label_set_text(GTK_LABEL(page->state.source_selection.label[0]), |
1484 | "(GPS has a valid position)"); |
1485 | else if(gps_on) |
1486 | gtk_label_set_text(GTK_LABEL(page->state.source_selection.label[0]), |
1487 | "(GPS has no valid position)"); |
1488 | else |
1489 | gtk_label_set_text(GTK_LABEL(page->state.source_selection.label[0]), |
1490 | "(GPS is disabled)"); |
1491 | |
1492 | #ifndef USE_HILDON |
1493 | gtk_widget_set_sensitive(page->state.source_selection.check[1], FALSE); |
1494 | gtk_label_set_text(GTK_LABEL(page->state.source_selection.label[1]), |
1495 | "(Maemo Mapper not available)"); |
1496 | |
1497 | #endif |
1498 | |
1499 | /* check if the user selected something that is actually selectable */ |
1500 | /* only allow him to continue then */ |
1501 | gboolean sel_ok = FALSE; |
1502 | int i; |
1503 | for(i=0;i<3;i++) { |
1504 | if(gtk_toggle_button_get_active( |
1505 | GTK_TOGGLE_BUTTON(page->state.source_selection.check[i]))) |
1506 | sel_ok = gtk_widget_get_sensitive(page->state.source_selection.check[i]); |
1507 | } |
1508 | |
1509 | /* set page to "completed" if a valid entry is selected */ |
1510 | gtk_assistant_set_page_complete( |
1511 | GTK_ASSISTANT(page->wizard->assistant), page->widget, sel_ok); |
1512 | } |
1513 | |
1514 | /* the user has changed the selected source, update dialog */ |
1515 | static void on_wizard_source_selection_toggled(GtkToggleButton *togglebutton, |
1516 | gpointer user_data) { |
1517 | if(gtk_toggle_button_get_active(togglebutton)) |
1518 | wizard_update_source_selection_page((wizard_page_t*)user_data); |
1519 | } |
1520 | |
1521 | static GtkWidget *wizard_create_source_selection_page(wizard_page_t *page) { |
1522 | GtkWidget *vbox = gtk_vbox_new(FALSE, 0); |
1523 | |
1524 | gtk_box_pack_start_defaults(GTK_BOX(vbox), |
1525 | wizard_text("Please choose how to determine the area you " |
1526 | "are planning to work on.")); |
1527 | |
1528 | GtkWidget *hbox = gtk_hbox_new(FALSE, 0); |
1529 | GtkWidget *vbox2 = gtk_vbox_new(FALSE, 0); |
1530 | |
1531 | /* add selection buttons */ |
1532 | int i; |
1533 | for(i=0;i<3;i++) { |
1534 | static const char *labels[] = { |
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 |