Contents of /trunk/src/project.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 175 - (show annotations)
Wed Jun 10 09:24:47 2009 UTC (14 years, 11 months ago) by harbaum
File MIME type: text/plain
File size: 37982 byte(s)
Adjusted default path
1 /*
2 * Copyright (C) 2008 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 #include "appdata.h"
21 #include "banner.h"
22
23 #include <sys/stat.h>
24
25 #include <libxml/parser.h>
26 #include <libxml/tree.h>
27
28 #if !defined(LIBXML_TREE_ENABLED) || !defined(LIBXML_OUTPUT_ENABLED)
29 #error "libxml doesn't support required tree or output"
30 #endif
31
32 typedef struct {
33 project_t *project;
34 settings_t *settings;
35 GtkWidget *dialog, *fsize, *diff_stat, *diff_remove;
36 GtkWidget *desc, *server;
37 GtkWidget *minlat, *minlon, *maxlat, *maxlon;
38 area_edit_t area_edit;
39 } project_context_t;
40
41 /* ------------ project file io ------------- */
42
43 static gboolean project_read(appdata_t *appdata,
44 char *project_file, project_t *project) {
45
46 LIBXML_TEST_VERSION;
47
48 xmlDoc *doc = NULL;
49 xmlNode *root_element = NULL;
50
51 /* parse the file and get the DOM */
52 if((doc = xmlReadFile(project_file, NULL, 0)) == NULL) {
53 printf("error: could not parse file %s\n", project_file);
54 return FALSE;
55 }
56
57 /* Get the root element node */
58 root_element = xmlDocGetRootElement(doc);
59
60 xmlNode *cur_node = NULL;
61 for (cur_node = root_element; cur_node; cur_node = cur_node->next) {
62 if (cur_node->type == XML_ELEMENT_NODE) {
63 if(strcasecmp((char*)cur_node->name, "proj") == 0) {
64 char *str;
65
66 if((str = (char*)xmlGetProp(cur_node, BAD_CAST "dirty"))) {
67 project->data_dirty = (strcasecmp(str, "true") == 0);
68 xmlFree(str);
69 } else
70 project->data_dirty = FALSE;
71
72 xmlNode *node = cur_node->children;
73
74 while(node != NULL) {
75 if(node->type == XML_ELEMENT_NODE) {
76
77 if(strcasecmp((char*)node->name, "desc") == 0) {
78 str = (char*)xmlNodeListGetString(doc, node->children, 1);
79 project->desc = g_strdup(str);
80 printf("desc = %s\n", project->desc);
81 xmlFree(str);
82
83 } else if(strcasecmp((char*)node->name, "server") == 0) {
84 str = (char*)xmlNodeListGetString(doc, node->children, 1);
85 project->server = g_strdup(str);
86 printf("server = %s\n", project->server);
87 xmlFree(str);
88
89 } else if(project->map_state &&
90 strcasecmp((char*)node->name, "map") == 0) {
91 if((str = (char*)xmlGetProp(node, BAD_CAST "zoom"))) {
92 project->map_state->zoom = g_ascii_strtod(str, NULL);
93 xmlFree(str);
94 }
95 if((str = (char*)xmlGetProp(node, BAD_CAST "detail"))) {
96 project->map_state->detail = g_ascii_strtod(str, NULL);
97 xmlFree(str);
98 }
99 if((str = (char*)xmlGetProp(node, BAD_CAST "scroll-offset-x"))) {
100 project->map_state->scroll_offset.x = strtoul(str, NULL, 10);
101 xmlFree(str);
102 }
103 if((str = (char*)xmlGetProp(node, BAD_CAST "scroll-offset-y"))) {
104 project->map_state->scroll_offset.y = strtoul(str, NULL, 10);
105 xmlFree(str);
106 }
107
108 } else if(strcasecmp((char*)node->name, "wms") == 0) {
109
110 if((str = (char*)xmlGetProp(node, BAD_CAST "server"))) {
111 project->wms_server = g_strdup(str);
112 xmlFree(str);
113 }
114 if((str = (char*)xmlGetProp(node, BAD_CAST "path"))) {
115 project->wms_path = g_strdup(str);
116 xmlFree(str);
117 }
118 if((str = (char*)xmlGetProp(node, BAD_CAST "x-offset"))) {
119 project->wms_offset.x = strtoul(str, NULL, 10);
120 xmlFree(str);
121 }
122 if((str = (char*)xmlGetProp(node, BAD_CAST "y-offset"))) {
123 project->wms_offset.y = strtoul(str, NULL, 10);
124 xmlFree(str);
125 }
126
127 } else if(strcasecmp((char*)node->name, "osm") == 0) {
128 str = (char*)xmlNodeListGetString(doc, node->children, 1);
129 printf("osm = %s\n", str);
130
131 /* make this a relative path if possible */
132 /* if the project path actually is a prefix of this, */
133 /* then just remove this prefix */
134 if((str[0] == '/') &&
135 (strlen(str) > strlen(project->path)) &&
136 !strncmp(str, project->path, strlen(project->path))) {
137
138 project->osm = g_strdup(str + strlen(project->path));
139 project->dirty = TRUE;
140 printf("osm name converted to relative %s\n", project->osm);
141 } else
142 project->osm = g_strdup(str);
143
144 xmlFree(str);
145
146 } else if(strcasecmp((char*)node->name, "min") == 0) {
147 if((str = (char*)xmlGetProp(node, BAD_CAST "lat"))) {
148 project->min.lat = g_ascii_strtod(str, NULL);
149 xmlFree(str);
150 }
151 if((str = (char*)xmlGetProp(node, BAD_CAST "lon"))) {
152 project->min.lon = g_ascii_strtod(str, NULL);
153 xmlFree(str);
154 }
155
156 } else if(strcasecmp((char*)node->name, "max") == 0) {
157 if((str = (char*)xmlGetProp(node, BAD_CAST "lat"))) {
158 project->max.lat = g_ascii_strtod(str, NULL);
159 xmlFree(str);
160 }
161 if((str = (char*)xmlGetProp(node, BAD_CAST "lon"))) {
162 project->max.lon = g_ascii_strtod(str, NULL);
163 xmlFree(str);
164 }
165 }
166 }
167 node = node->next;
168 }
169 }
170 }
171 }
172
173 xmlFreeDoc(doc);
174 xmlCleanupParser();
175
176 return TRUE;
177 }
178
179 gboolean project_save(GtkWidget *parent, project_t *project) {
180 char str[32];
181 char *project_file = g_strdup_printf("%s%s.proj",
182 project->path, project->name);
183
184 printf("saving project to %s\n", project_file);
185
186 /* check if project path exists */
187 if(!g_file_test(project->path, G_FILE_TEST_IS_DIR)) {
188 /* make sure project base path exists */
189 if(g_mkdir_with_parents(project->path, S_IRWXU) != 0) {
190 errorf(GTK_WIDGET(parent),
191 _("Unable to create project path %s"), project->path);
192 return FALSE;
193 }
194 }
195
196 LIBXML_TEST_VERSION;
197
198 xmlDocPtr doc = xmlNewDoc(BAD_CAST "1.0");
199 xmlNodePtr node, root_node = xmlNewNode(NULL, BAD_CAST "proj");
200 xmlNewProp(root_node, BAD_CAST "name", BAD_CAST project->name);
201 if(project->data_dirty)
202 xmlNewProp(root_node, BAD_CAST "dirty", BAD_CAST "true");
203
204 xmlDocSetRootElement(doc, root_node);
205
206 node = xmlNewChild(root_node, NULL, BAD_CAST "server",
207 BAD_CAST project->server);
208
209 xmlNewChild(root_node, NULL, BAD_CAST "desc", BAD_CAST project->desc);
210 xmlNewChild(root_node, NULL, BAD_CAST "osm", BAD_CAST project->osm);
211
212 node = xmlNewChild(root_node, NULL, BAD_CAST "min", NULL);
213 g_ascii_formatd(str, sizeof(str), LL_FORMAT, project->min.lat);
214 xmlNewProp(node, BAD_CAST "lat", BAD_CAST str);
215 g_ascii_formatd(str, sizeof(str), LL_FORMAT, project->min.lon);
216 xmlNewProp(node, BAD_CAST "lon", BAD_CAST str);
217
218 node = xmlNewChild(root_node, NULL, BAD_CAST "max", NULL);
219 g_ascii_formatd(str, sizeof(str), LL_FORMAT, project->max.lat);
220 xmlNewProp(node, BAD_CAST "lat", BAD_CAST str);
221 g_ascii_formatd(str, sizeof(str), LL_FORMAT, project->max.lon);
222 xmlNewProp(node, BAD_CAST "lon", BAD_CAST str);
223
224 if(project->map_state) {
225 node = xmlNewChild(root_node, NULL, BAD_CAST "map", BAD_CAST NULL);
226 g_ascii_formatd(str, sizeof(str), "%.04f", project->map_state->zoom);
227 xmlNewProp(node, BAD_CAST "zoom", BAD_CAST str);
228 g_ascii_formatd(str, sizeof(str), "%.04f", project->map_state->detail);
229 xmlNewProp(node, BAD_CAST "detail", BAD_CAST str);
230 snprintf(str, sizeof(str), "%d", project->map_state->scroll_offset.x);
231 xmlNewProp(node, BAD_CAST "scroll-offset-x", BAD_CAST str);
232 snprintf(str, sizeof(str), "%d", project->map_state->scroll_offset.y);
233 xmlNewProp(node, BAD_CAST "scroll-offset-y", BAD_CAST str);
234 }
235
236 node = xmlNewChild(root_node, NULL, BAD_CAST "wms", NULL);
237 if(project->wms_server)
238 xmlNewProp(node, BAD_CAST "server", BAD_CAST project->wms_server);
239 if(project->wms_path)
240 xmlNewProp(node, BAD_CAST "path", BAD_CAST project->wms_path);
241 snprintf(str, sizeof(str), "%d", project->wms_offset.x);
242 xmlNewProp(node, BAD_CAST "x-offset", BAD_CAST str);
243 snprintf(str, sizeof(str), "%d", project->wms_offset.y);
244 xmlNewProp(node, BAD_CAST "y-offset", BAD_CAST str);
245
246 xmlSaveFormatFileEnc(project_file, doc, "UTF-8", 1);
247 xmlFreeDoc(doc);
248 xmlCleanupParser();
249
250 g_free(project_file);
251
252 return TRUE;
253 }
254
255 /* ------------ freeing projects --------------------- */
256
257 void project_free(project_t *project) {
258 if(!project) return;
259
260 if(project->name) g_free(project->name);
261 if(project->desc) g_free(project->desc);
262 if(project->server) g_free(project->server);
263
264 if(project->wms_server) g_free(project->wms_server);
265 if(project->wms_path) g_free(project->wms_path);
266
267 if(project->path) g_free(project->path);
268 if(project->osm) g_free(project->osm);
269
270 map_state_free(project->map_state);
271
272 g_free(project);
273 }
274
275 /* ------------ project selection dialog ------------- */
276
277 static char *project_fullname(settings_t *settings, const char *name) {
278 return g_strdup_printf("%s%s/%s.proj", settings->base_path, name, name);
279 }
280
281 static gboolean project_exists(settings_t *settings, const char *name) {
282 gboolean ok = FALSE;
283 char *fulldir = g_strdup_printf("%s%s", settings->base_path, name);
284
285 if(g_file_test(fulldir, G_FILE_TEST_IS_DIR)) {
286
287 /* check for project file */
288 char *fullname = project_fullname(settings, name);
289
290 if(g_file_test(fullname, G_FILE_TEST_IS_REGULAR))
291 ok = TRUE;
292
293 g_free(fullname);
294 }
295 g_free(fulldir);
296
297 return ok;
298 }
299
300 static project_t *project_scan(appdata_t *appdata) {
301 project_t *projects = NULL, **current = &projects;
302
303 /* scan for projects */
304 GDir *dir = g_dir_open(appdata->settings->base_path, 0, NULL);
305 const char *name = NULL;
306 do {
307 if((name = g_dir_read_name(dir))) {
308 if(project_exists(appdata->settings, name)) {
309 printf("found project %s\n", name);
310
311 /* try to read project and append it to chain */
312 *current = g_new0(project_t, 1);
313 (*current)->name = g_strdup(name);
314 (*current)->path = g_strdup_printf("%s%s/",
315 appdata->settings->base_path, name);
316
317 char *fullname = project_fullname(appdata->settings, name);
318 if(project_read(appdata, fullname, *current))
319 current = &((*current)->next);
320 else {
321 g_free(*current);
322 *current = NULL;
323 }
324 g_free(fullname);
325 }
326 }
327 } while(name);
328
329 g_dir_close(dir);
330
331 return projects;
332 }
333
334 typedef struct {
335 project_t *project;
336 GtkWidget *dialog, *list;
337 settings_t *settings;
338 #ifdef USE_HILDON
339 dbus_mm_pos_t *mmpos;
340 osso_context_t *osso_context;
341 #endif
342 } select_context_t;
343
344 enum {
345 PROJECT_COL_NAME = 0,
346 PROJECT_COL_STATUS,
347 PROJECT_COL_DESCRIPTION,
348 PROJECT_COL_DATA,
349 PROJECT_NUM_COLS
350 };
351
352 static gboolean osm_file_exists(char *path, char *name) {
353 gboolean exists = FALSE;
354
355 if(name[0] == '/')
356 exists = g_file_test(name, G_FILE_TEST_IS_REGULAR);
357 else {
358 char *full = g_strjoin(NULL, path, name, NULL);
359 exists = g_file_test(full, G_FILE_TEST_IS_REGULAR);
360 g_free(full);
361 }
362 return exists;
363 }
364
365 static void view_selected(select_context_t *context, project_t *project) {
366 list_button_enable(context->list, LIST_BUTTON_REMOVE, project != NULL);
367 list_button_enable(context->list, LIST_BUTTON_EDIT, project != NULL);
368
369 /* check if the selected project also has a valid osm file */
370 gtk_dialog_set_response_sensitive(GTK_DIALOG(context->dialog),
371 GTK_RESPONSE_ACCEPT,
372 project && osm_file_exists(project->path, project->osm));
373 }
374
375 static gboolean
376 view_selection_func(GtkTreeSelection *selection, GtkTreeModel *model,
377 GtkTreePath *path, gboolean path_currently_selected,
378 gpointer userdata) {
379 select_context_t *context = (select_context_t*)userdata;
380 GtkTreeIter iter;
381
382 if(gtk_tree_model_get_iter(model, &iter, path)) {
383 project_t *project = NULL;
384 gtk_tree_model_get(model, &iter, PROJECT_COL_DATA, &project, -1);
385 g_assert(gtk_tree_path_get_depth(path) == 1);
386
387 view_selected(context, project);
388 }
389
390 return TRUE; /* allow selection state to change */
391 }
392
393 /* get the currently selected project in the list, NULL if none */
394 static project_t *project_get_selected(GtkWidget *list) {
395 project_t *project = NULL;
396 GtkTreeModel *model;
397 GtkTreeIter iter;
398
399 GtkTreeSelection *selection = list_get_selection(list);
400 g_assert(gtk_tree_selection_get_selected(selection, &model, &iter));
401 gtk_tree_model_get(model, &iter, PROJECT_COL_DATA, &project, -1);
402
403 return project;
404 }
405
406 /* ------------------------- create a new project ---------------------- */
407
408 /* returns true of str contains one of the characters in chars */
409 static gboolean strchrs(char *str, char *chars) {
410 while(*chars) {
411 char *p = str;
412 while(*p) {
413 if(*p == *chars)
414 return TRUE;
415
416 p++;
417 }
418 chars++;
419 }
420 return FALSE;
421 }
422
423 typedef struct {
424 GtkWidget *dialog;
425 settings_t *settings;
426 } name_callback_context_t;
427
428 static void callback_modified_name(GtkWidget *widget, gpointer data) {
429 name_callback_context_t *context = (name_callback_context_t*)data;
430
431 char *name = (char*)gtk_entry_get_text(GTK_ENTRY(widget));
432
433 /* name must not contain some special chars */
434 gboolean ok = FALSE;
435
436 /* check if there's a name */
437 if(name && strlen(name) > 0) {
438 /* check if it consists of valid characters */
439 if(!strchrs(name, "\\*?()\n\t\r")) {
440 /* check if such a project already exists */
441 if(!project_exists(context->settings, name))
442 ok = TRUE;
443 }
444 }
445
446 gtk_dialog_set_response_sensitive(GTK_DIALOG(context->dialog),
447 GTK_RESPONSE_ACCEPT, ok);
448 }
449
450
451 gboolean project_delete(select_context_t *context, project_t *project) {
452
453 /* remove entire directory from disk */
454 GDir *dir = g_dir_open(project->path, 0, NULL);
455 const char *name = NULL;
456 do {
457 if((name = g_dir_read_name(dir))) {
458 char *fullname = g_strdup_printf("%s/%s", project->path, name);
459 g_remove(fullname);
460 g_free(fullname);
461 }
462 } while(name);
463
464 /* remove the projects directory */
465 g_remove(project->path);
466
467 /* remove from view */
468 GtkTreeIter iter;
469 GtkTreeModel *model = list_get_model(context->list);
470 gboolean deleted = FALSE;
471 if(gtk_tree_model_get_iter_first(model, &iter)) {
472 do {
473 project_t *prj = NULL;
474 gtk_tree_model_get(model, &iter, PROJECT_COL_DATA, &prj, -1);
475 if(prj && (prj == project)) {
476 printf("found %s to remove\n", prj->name);
477 /* and remove from store */
478 gtk_list_store_remove(GTK_LIST_STORE(model), &iter);
479 deleted = TRUE;
480 }
481 } while(!deleted && gtk_tree_model_iter_next(model, &iter));
482 }
483
484 /* de-chain entry from project list */
485 project_t **project_list = &context->project;
486 while(*project_list) {
487 if(*project_list == project)
488 *project_list = (*project_list)->next;
489 else
490 project_list = &((*project_list)->next);
491 }
492
493 /* free project structure */
494 project_free(project);
495
496 /* disable edit/remove buttons */
497 view_selected(context, NULL);
498
499 return TRUE;
500 }
501
502 project_t *project_new(select_context_t *context) {
503 printf("creating project with default values\n");
504
505 /* -------------- first choose a name for the project --------------- */
506 GtkWidget *dialog =
507 misc_dialog_new(MISC_DIALOG_NOSIZE, _("Project name"),
508 GTK_WINDOW(context->dialog),
509 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
510 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
511 NULL);
512
513 GtkWidget *hbox = gtk_hbox_new(FALSE, 8);
514 gtk_box_pack_start_defaults(GTK_BOX(hbox), gtk_label_new(_("Name:")));
515
516 name_callback_context_t name_context = { dialog, context->settings };
517 GtkWidget *entry = gtk_entry_new();
518 // gtk_entry_set_text(GTK_ENTRY(entry), "<enter name>");
519 gtk_box_pack_start_defaults(GTK_BOX(hbox), entry);
520 g_signal_connect(G_OBJECT(entry), "changed",
521 G_CALLBACK(callback_modified_name), &name_context);
522
523 gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(dialog)->vbox), hbox);
524
525 /* don't all user to click ok until something useful has been entered */
526 gtk_dialog_set_response_sensitive(GTK_DIALOG(dialog),
527 GTK_RESPONSE_ACCEPT, FALSE);
528
529 gtk_widget_show_all(dialog);
530 if(GTK_RESPONSE_ACCEPT != gtk_dialog_run(GTK_DIALOG(dialog))) {
531 gtk_widget_destroy(dialog);
532 return NULL;
533 }
534
535 project_t *project = g_new0(project_t, 1);
536 project->name = g_strdup(gtk_entry_get_text(GTK_ENTRY(entry)));
537 gtk_widget_destroy(dialog);
538
539
540 project->path = g_strdup_printf("%s%s/",
541 context->settings->base_path, project->name);
542 project->desc = g_strdup(_("<project description>"));
543
544 /* no data downloaded yet */
545 project->data_dirty = TRUE;
546
547 /* adjust default server stored in settings if required */
548 if(strstr(context->settings->server, "0.5") != NULL) {
549 strstr(context->settings->server, "0.5")[2] = '6';
550 printf("adjusting server path in settings to 0.6\n");
551 }
552
553 /* use global server/access settings */
554 project->server = g_strdup(context->settings->server);
555
556 /* build project osm file name */
557 project->osm = g_strdup_printf("%s.osm", project->name);
558
559 /* around the castle in karlsruhe, germany ... */
560 project->min.lat = 49.005; project->min.lon = 8.3911;
561 project->max.lat = 49.023; project->max.lon = 8.4185;
562
563 /* create project file on disk */
564 project_save(context->dialog, project);
565
566 #ifdef USE_HILDON
567 if(!project_edit(context->dialog, context->settings,
568 project, context->mmpos, context->osso_context))
569 #else
570 if(!project_edit(context->dialog, context->settings, project))
571 #endif
572 {
573 printf("edit cancelled!!\n");
574
575 project_delete(context, project);
576
577 project = NULL;
578 }
579
580 /* enable/disable edit/remove buttons */
581 view_selected(context, project);
582
583 return project;
584 }
585
586 // predecs
587 void project_get_status_icon_stock_id(project_t *project, gchar **stock_id);
588
589 static void on_project_new(GtkButton *button, gpointer data) {
590 select_context_t *context = (select_context_t*)data;
591 project_t **project = &context->project;
592 *project = project_new(context);
593 if(*project) {
594
595 GtkTreeModel *model = list_get_model(context->list);
596
597 GtkTreeIter iter;
598 gchar *status_stock_id = NULL;
599 project_get_status_icon_stock_id(*project, &status_stock_id);
600 gtk_list_store_append(GTK_LIST_STORE(model), &iter);
601 gtk_list_store_set(GTK_LIST_STORE(model), &iter,
602 PROJECT_COL_NAME, (*project)->name,
603 PROJECT_COL_STATUS, status_stock_id,
604 PROJECT_COL_DESCRIPTION, (*project)->desc,
605 PROJECT_COL_DATA, *project,
606 -1);
607
608 GtkTreeSelection *selection = list_get_selection(context->list);
609 gtk_tree_selection_select_iter(selection, &iter);
610 }
611 }
612
613 static void on_project_delete(GtkButton *button, gpointer data) {
614 select_context_t *context = (select_context_t*)data;
615 project_t *project = project_get_selected(context->list);
616
617 char *str = g_strdup_printf(_("Do you really want to delete the "
618 "project \"%s\"?"), project->name);
619 GtkWidget *dialog = gtk_message_dialog_new(
620 GTK_WINDOW(context->dialog),
621 GTK_DIALOG_DESTROY_WITH_PARENT,
622 GTK_MESSAGE_QUESTION, GTK_BUTTONS_YES_NO, str);
623 g_free(str);
624
625 gtk_window_set_title(GTK_WINDOW(dialog), _("Delete project?"));
626
627 /* set the active flag again if the user answered "no" */
628 if(GTK_RESPONSE_NO == gtk_dialog_run(GTK_DIALOG(dialog))) {
629 gtk_widget_destroy(dialog);
630 return;
631 }
632
633 gtk_widget_destroy(dialog);
634
635 if(!project_delete(context, project))
636 printf("unable to delete project\n");
637 }
638
639 static void on_project_edit(GtkButton *button, gpointer data) {
640 select_context_t *context = (select_context_t*)data;
641 project_t *project = project_get_selected(context->list);
642 g_assert(project);
643 #ifdef USE_HILDON
644 if(project_edit(context->dialog, context->settings, project,
645 context->mmpos, context->osso_context))
646 #else
647 if(project_edit(context->dialog, context->settings, project))
648 #endif
649 {
650 GtkTreeModel *model;
651 GtkTreeIter iter;
652
653 /* description etc. may have changed, so update list */
654 GtkTreeSelection *selection = list_get_selection(context->list);
655 g_assert(gtk_tree_selection_get_selected(selection, &model, &iter));
656
657 // gtk_tree_model_get(model, &iter, PROJECT_COL_DATA, &project, -1);
658 gchar *status_stock_id = NULL;
659 project_get_status_icon_stock_id(project, &status_stock_id);
660 gtk_list_store_set(GTK_LIST_STORE(model), &iter,
661 PROJECT_COL_NAME, project->name,
662 PROJECT_COL_STATUS, status_stock_id,
663 PROJECT_COL_DESCRIPTION, project->desc,
664 -1);
665
666
667 }
668
669 /* enable/disable edit/remove buttons */
670 view_selected(context, project);
671 }
672
673
674 gboolean project_osm_present(project_t *project) {
675 char *osm_name = g_strdup_printf("%s/%s.osm", project->path, project->name);
676 gboolean is_present = g_file_test(osm_name, G_FILE_TEST_EXISTS);
677 g_free(osm_name);
678 return is_present;
679 }
680
681 void project_get_status_icon_stock_id(project_t *project, gchar **stock_id) {
682 *stock_id = (! project_osm_present(project)) ? GTK_STOCK_DIALOG_WARNING
683 : diff_present(project) ? GTK_STOCK_PROPERTIES
684 : GTK_STOCK_FILE;
685 // TODO: check for outdatedness too. Which icon to use?
686 }
687
688 static GtkWidget *project_list_widget(select_context_t *context) {
689 context->list = list_new(LIST_HILDON_WITHOUT_HEADERS);
690
691 list_set_selection_function(context->list, view_selection_func, context);
692
693 list_set_columns(context->list,
694 _("Name"), PROJECT_COL_NAME, 0,
695 _("State"), PROJECT_COL_STATUS, LIST_FLAG_STOCK_ICON,
696 _("Description"), PROJECT_COL_DESCRIPTION, LIST_FLAG_ELLIPSIZE,
697 NULL);
698
699
700 /* build the store */
701 GtkListStore *store = gtk_list_store_new(PROJECT_NUM_COLS,
702 G_TYPE_STRING, // name
703 G_TYPE_STRING, // status
704 G_TYPE_STRING, // desc
705 G_TYPE_POINTER); // data
706
707 GtkTreeIter iter;
708 project_t *project = context->project;
709 while(project) {
710 gchar *status_stock_id = NULL;
711 project_get_status_icon_stock_id(project, &status_stock_id);
712 /* Append a row and fill in some data */
713 gtk_list_store_append(store, &iter);
714 gtk_list_store_set(store, &iter,
715 PROJECT_COL_NAME, project->name,
716 PROJECT_COL_STATUS, status_stock_id,
717 PROJECT_COL_DESCRIPTION, project->desc,
718 PROJECT_COL_DATA, project,
719 -1);
720 project = project->next;
721 }
722
723 list_set_store(context->list, store);
724 g_object_unref(store);
725
726 list_set_static_buttons(context->list, G_CALLBACK(on_project_new),
727 G_CALLBACK(on_project_edit), G_CALLBACK(on_project_delete), context);
728
729 return context->list;
730 }
731
732 char *project_select(appdata_t *appdata) {
733 char *name = NULL;
734
735 select_context_t *context = g_new0(select_context_t, 1);
736 #ifdef USE_HILDON
737 context->mmpos = &appdata->mmpos;
738 context->osso_context = appdata->osso_context;
739 #endif
740 context->settings = appdata->settings;
741 context->project = project_scan(appdata);
742
743 /* create project selection dialog */
744 context->dialog =
745 misc_dialog_new(MISC_DIALOG_MEDIUM,_("Project selection"),
746 GTK_WINDOW(appdata->window),
747 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
748 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
749 NULL);
750
751 gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(context->dialog)->vbox),
752 project_list_widget(context));
753
754 /* don't all user to click ok until something is selected */
755 gtk_dialog_set_response_sensitive(GTK_DIALOG(context->dialog),
756 GTK_RESPONSE_ACCEPT, FALSE);
757
758 gtk_widget_show_all(context->dialog);
759 if(GTK_RESPONSE_ACCEPT == gtk_dialog_run(GTK_DIALOG(context->dialog)))
760 name = g_strdup(project_get_selected(context->list)->name);
761
762 gtk_widget_destroy(context->dialog);
763
764 /* free all entries */
765 project_t *project = context->project;
766 while(project) {
767 project_t *next = project->next;
768 project_free(project);
769 project = next;
770 }
771
772 g_free(context);
773
774 return name;
775 }
776
777 /* ---------------------------------------------------- */
778
779 /* return file length or -1 on error */
780 static gsize file_length(char *path, char *name) {
781 char *str = NULL;
782
783 if(name[0] == '/') str = g_strdup(name);
784 else str = g_strjoin(NULL, path, name, NULL);
785
786 GMappedFile *gmap = g_mapped_file_new(str, FALSE, NULL);
787 g_free(str);
788
789 if(!gmap) return -1;
790 gsize size = g_mapped_file_get_length(gmap);
791 g_mapped_file_free(gmap);
792 return size;
793 }
794
795 void project_filesize(project_context_t *context) {
796 char *str = NULL;
797
798 printf("Checking size of %s\n", context->project->osm);
799
800 if(!osm_file_exists(context->project->path, context->project->osm)) {
801 GdkColor color;
802 gdk_color_parse("red", &color);
803 gtk_widget_modify_fg(context->fsize, GTK_STATE_NORMAL, &color);
804
805 str = g_strdup(_("Not downloaded!"));
806 gtk_dialog_set_response_sensitive(GTK_DIALOG(context->dialog),
807 GTK_RESPONSE_ACCEPT, 0);
808 } else {
809 gtk_widget_modify_fg(context->fsize, GTK_STATE_NORMAL, NULL);
810
811 if(!context->project->data_dirty)
812 str = g_strdup_printf(_("%d bytes present"),
813 file_length(context->project->path,
814 context->project->osm));
815 else
816 str = g_strdup_printf(_("Outdated, please download!"));
817
818 /* project also must not be dirty to proceed */
819 gtk_dialog_set_response_sensitive(GTK_DIALOG(context->dialog),
820 GTK_RESPONSE_ACCEPT, !context->project->data_dirty);
821 }
822
823 if(str) {
824 gtk_label_set_text(GTK_LABEL(context->fsize), str);
825 g_free(str);
826 }
827 }
828
829 void project_diffstat(project_context_t *context) {
830 char *str = NULL;
831
832 if(diff_present(context->project))
833 str = g_strdup(_("present"));
834 else
835 str = g_strdup(_("not present"));
836
837 gtk_label_set_text(GTK_LABEL(context->diff_stat), str);
838 g_free(str);
839 }
840
841 static void project_update(project_context_t *context) {
842
843 /* fetch values from dialog */
844 if(context->project->desc) g_free(context->project->desc);
845 context->project->desc = g_strdup(gtk_entry_get_text(
846 GTK_ENTRY(context->desc)));
847
848 if(context->project->server) g_free(context->project->server);
849 context->project->server = g_strdup(gtk_entry_get_text(
850 GTK_ENTRY(context->server)));
851 }
852
853 static void on_edit_clicked(GtkButton *button, gpointer data) {
854 project_context_t *context = (project_context_t*)data;
855
856 if(area_edit(&context->area_edit)) {
857 printf("coordinates changed!!\n");
858
859 pos_lon_label_set(context->minlat, context->project->min.lat);
860 pos_lon_label_set(context->minlon, context->project->min.lon);
861 pos_lon_label_set(context->maxlat, context->project->max.lat);
862 pos_lon_label_set(context->maxlon, context->project->max.lon);
863 }
864 }
865
866 static void on_download_clicked(GtkButton *button, gpointer data) {
867 project_context_t *context = (project_context_t*)data;
868
869 project_update(context);
870
871 printf("download %s\n", context->project->osm);
872
873 if(osm_download(context->dialog, context->settings, context->project)) {
874 context->project->data_dirty = FALSE;
875 project_filesize(context);
876 } else
877 printf("download failed\n");
878 }
879
880 static void on_diff_remove_clicked(GtkButton *button, gpointer data) {
881 project_context_t *context = (project_context_t*)data;
882
883 printf("clicked diff remove\n");
884
885 GtkWidget *dialog = gtk_message_dialog_new(
886 GTK_WINDOW(context->dialog),
887 GTK_DIALOG_DESTROY_WITH_PARENT,
888 GTK_MESSAGE_QUESTION, GTK_BUTTONS_YES_NO,
889 _("Do you really want to remove the diff file? This "
890 "will delete all changes you've made so far and which "
891 "you didn't upload yet."));
892
893 gtk_window_set_title(GTK_WINDOW(dialog), _("Remove diff?"));
894
895 /* set the active flag again if the user answered "no" */
896 if(GTK_RESPONSE_YES == gtk_dialog_run(GTK_DIALOG(dialog))) {
897 diff_remove(context->project);
898 project_diffstat(context);
899 gtk_widget_set_sensitive(context->diff_remove, FALSE);
900 }
901
902 gtk_widget_destroy(dialog);
903 }
904
905 gboolean project_edit(GtkWidget *parent, settings_t *settings,
906 project_t *project POS_PARM) {
907 gboolean ok = FALSE;
908
909 /* ------------ project edit dialog ------------- */
910
911 project_context_t *context = g_new0(project_context_t, 1);
912 context->project = project;
913 context->settings = settings;
914
915 context->area_edit.min = &project->min;
916 context->area_edit.max = &project->max;
917 #ifdef USE_HILDON
918 context->area_edit.mmpos = mmpos;
919 context->area_edit.osso_context = osso_context;
920 #endif
921
922 char *str = g_strdup_printf(_("Project - %s"), project->name);
923 context->area_edit.parent =
924 context->dialog = misc_dialog_new(MISC_DIALOG_WIDE, str,
925 GTK_WINDOW(parent),
926 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
927 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
928 NULL);
929 g_free(str);
930
931 GtkWidget *download, *label;
932 GtkWidget *table = gtk_table_new(4, 6, FALSE); // x, y
933
934 label = gtk_label_new(_("Description:"));
935 gtk_misc_set_alignment(GTK_MISC(label), 1.f, 0.5f);
936 gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, 0, 1);
937 context->desc = gtk_entry_new();
938 gtk_entry_set_text(GTK_ENTRY(context->desc), project->desc);
939 gtk_table_attach_defaults(GTK_TABLE(table), context->desc, 1, 4, 0, 1);
940
941 gtk_table_set_row_spacing(GTK_TABLE(table), 0, 4);
942
943 label = gtk_label_new(_("Latitude"));
944 gtk_table_attach_defaults(GTK_TABLE(table), label, 1, 2, 1, 2);
945 label = gtk_label_new(_("Longitude"));
946 gtk_table_attach_defaults(GTK_TABLE(table), label, 2, 3, 1, 2);
947
948 label = gtk_label_new(_("Min:"));
949 gtk_misc_set_alignment(GTK_MISC(label), 1.f, 0.5f);
950 gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, 2, 3);
951 context->minlat = pos_lat_label_new(project->min.lat);
952 gtk_table_attach_defaults(GTK_TABLE(table), context->minlat, 1, 2, 2, 3);
953 context->minlon = pos_lon_label_new(project->min.lon);
954 gtk_table_attach_defaults(GTK_TABLE(table), context->minlon, 2, 3, 2, 3);
955
956 label = gtk_label_new(_("Max:"));
957 gtk_misc_set_alignment(GTK_MISC(label), 1.f, 0.5f);
958 gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, 3, 4);
959 context->maxlat = pos_lat_label_new(project->max.lat);
960 gtk_table_attach_defaults(GTK_TABLE(table), context->maxlat, 1, 2, 3, 4);
961 context->maxlon = pos_lon_label_new(project->max.lon);
962 gtk_table_attach_defaults(GTK_TABLE(table), context->maxlon, 2, 3, 3, 4);
963
964 GtkWidget *edit = gtk_button_new_with_label(_("Edit..."));
965 gtk_signal_connect(GTK_OBJECT(edit), "clicked",
966 (GtkSignalFunc)on_edit_clicked, context);
967 gtk_table_attach(GTK_TABLE(table), edit, 3, 4, 2, 4,
968 GTK_EXPAND | GTK_FILL,0,0,0);
969
970 gtk_table_set_col_spacing(GTK_TABLE(table), 0, 4);
971 gtk_table_set_row_spacing(GTK_TABLE(table), 3, 4);
972
973 label = gtk_label_new(_("Server:"));
974 gtk_misc_set_alignment(GTK_MISC(label), 1.f, 0.5f);
975 gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, 4, 5);
976 context->server = gtk_entry_new();
977 HILDON_ENTRY_NO_AUTOCAP(context->server);
978 gtk_entry_set_text(GTK_ENTRY(context->server), project->server);
979 gtk_table_attach_defaults(GTK_TABLE(table), context->server, 1, 4, 4, 5);
980
981 label = gtk_label_new(_("OSM file:"));
982 gtk_misc_set_alignment(GTK_MISC(label), 1.f, 0.5f);
983 gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, 5, 6);
984 GtkWidget *hbox = gtk_hbox_new(FALSE, 0);
985 context->fsize = gtk_label_new(_(""));
986 gtk_misc_set_alignment(GTK_MISC(context->fsize), 0.f, 0.5f);
987 project_filesize(context);
988 gtk_box_pack_start_defaults(GTK_BOX(hbox), context->fsize);
989 download = gtk_button_new_with_label(_("Download..."));
990 gtk_signal_connect(GTK_OBJECT(download), "clicked",
991 (GtkSignalFunc)on_download_clicked, context);
992 gtk_box_pack_start(GTK_BOX(hbox), download, FALSE, FALSE, 0);
993 gtk_table_attach_defaults(GTK_TABLE(table), hbox, 1, 4, 5, 6);
994
995 label = gtk_label_new(_("Diff file:"));
996 gtk_misc_set_alignment(GTK_MISC(label), 1.f, 0.5f);
997 gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, 6, 7);
998 hbox = gtk_hbox_new(FALSE, 0);
999 context->diff_stat = gtk_label_new(_(""));
1000 gtk_misc_set_alignment(GTK_MISC(context->diff_stat), 0.f, 0.5f);
1001 project_diffstat(context);
1002 gtk_box_pack_start_defaults(GTK_BOX(hbox), context->diff_stat);
1003 context->diff_remove = gtk_button_new_with_label(_("Remove..."));
1004 if(!diff_present(project))
1005 gtk_widget_set_sensitive(context->diff_remove, FALSE);
1006 gtk_signal_connect(GTK_OBJECT(context->diff_remove), "clicked",
1007 (GtkSignalFunc)on_diff_remove_clicked, context);
1008 gtk_box_pack_start(GTK_BOX(hbox), context->diff_remove, FALSE, FALSE, 0);
1009 gtk_table_attach_defaults(GTK_TABLE(table), hbox, 1, 4, 6, 7);
1010
1011
1012 gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(context->dialog)->vbox),
1013 table);
1014 gtk_widget_show_all(context->dialog);
1015
1016 if(GTK_RESPONSE_ACCEPT == gtk_dialog_run(GTK_DIALOG(context->dialog))) {
1017 ok = TRUE;
1018
1019 /* transfer values from edit dialog into project structure */
1020 project_update(context);
1021
1022 /* save project */
1023 project_save(context->dialog, project);
1024 }
1025
1026 gtk_widget_destroy(context->dialog);
1027 g_free(context);
1028
1029 return ok;
1030 }
1031
1032 gboolean project_open(appdata_t *appdata, char *name) {
1033 project_t *project = g_new0(project_t, 1);
1034
1035 /* link to map state if a map already exists */
1036 if(appdata->map) {
1037 printf("Project: Using map state\n");
1038 project->map_state = appdata->map->state;
1039 } else {
1040 printf("Project: Creating new map_state\n");
1041 project->map_state = map_state_new();
1042 }
1043
1044 map_state_reset(project->map_state);
1045 project->map_state->refcount++;
1046
1047 /* build project path */
1048 project->path = g_strdup_printf("%s%s/",
1049 appdata->settings->base_path, name);
1050 project->name = g_strdup(name);
1051
1052 char *project_file = g_strdup_printf("%s%s.proj", project->path, name);
1053
1054 printf("project file = %s\n", project_file);
1055 if(!g_file_test(project_file, G_FILE_TEST_IS_REGULAR)) {
1056 printf("requested project file doesn't exist\n");
1057 project_free(project);
1058 g_free(project_file);
1059 return FALSE;
1060 }
1061
1062 if(!project_read(appdata, project_file, project)) {
1063 printf("error reading project file\n");
1064 project_free(project);
1065 g_free(project_file);
1066 return FALSE;
1067 }
1068
1069 g_free(project_file);
1070
1071 /* --------- project structure ok: load its OSM file --------- */
1072 appdata->project = project;
1073
1074 printf("project_open: loading osm %s\n", project->osm);
1075 appdata->osm = osm_parse(project->path, project->osm);
1076 if(!appdata->osm) {
1077 printf("OSM parsing failed\n");
1078 return FALSE;
1079 }
1080
1081 printf("parsing ok\n");
1082
1083 return TRUE;
1084 }
1085
1086 gboolean project_close(appdata_t *appdata) {
1087 if(!appdata->project) return FALSE;
1088
1089 printf("closing current project\n");
1090
1091 /* redraw the entire map by destroying all map items and redrawing them */
1092 if(appdata->osm)
1093 diff_save(appdata->project, appdata->osm);
1094
1095 /* Save track and turn off the handler callback */
1096 track_save(appdata->project, appdata->track.track);
1097 track_clear(appdata, appdata->track.track);
1098 appdata->track.track = NULL;
1099
1100 map_clear(appdata, MAP_LAYER_ALL);
1101
1102 if(appdata->osm) {
1103 osm_free(&appdata->icon, appdata->osm);
1104 appdata->osm = NULL;
1105 }
1106
1107 /* update project file on disk */
1108 project_save(GTK_WIDGET(appdata->window), appdata->project);
1109
1110 project_free(appdata->project);
1111 appdata->project = NULL;
1112
1113 return TRUE;
1114 }
1115
1116 #define _PROJECT_LOAD_BUF_SIZ 64
1117
1118 gboolean project_load(appdata_t *appdata, char *name) {
1119 char *proj_name = NULL;
1120
1121 if(!name) {
1122 /* make user select a project */
1123 proj_name = project_select(appdata);
1124 if(!proj_name) {
1125 printf("no project selected\n");
1126 return FALSE;
1127 }
1128 }
1129 else {
1130 proj_name = g_strdup(name);
1131 }
1132
1133 char banner_txt[_PROJECT_LOAD_BUF_SIZ];
1134 memset(banner_txt, 0, _PROJECT_LOAD_BUF_SIZ);
1135
1136 snprintf(banner_txt, _PROJECT_LOAD_BUF_SIZ, _("Loading %s"), proj_name);
1137 banner_busy_start(appdata, TRUE, banner_txt);
1138
1139 /* close current project */
1140 banner_busy_tick();
1141 if(appdata->project)
1142 project_close(appdata);
1143
1144 /* open project itself */
1145 banner_busy_tick();
1146 if(!project_open(appdata, proj_name)) {
1147 printf("error opening requested project\n");
1148
1149 if(appdata->project) {
1150 project_free(appdata->project);
1151 appdata->project = NULL;
1152 }
1153
1154 if(appdata->osm) {
1155 osm_free(&appdata->icon, appdata->osm);
1156 appdata->osm = NULL;
1157 }
1158
1159 snprintf(banner_txt, _PROJECT_LOAD_BUF_SIZ,
1160 _("Error opening %s"), proj_name);
1161 banner_busy_stop(appdata);
1162 banner_show_info(appdata, banner_txt);
1163
1164 g_free(proj_name);
1165 return FALSE;
1166 }
1167
1168 /* check if OSM data is valid */
1169 banner_busy_tick();
1170 if(!osm_sanity_check(GTK_WIDGET(appdata->window), appdata->osm)) {
1171 printf("project/osm sanity checks failed, unloading project\n");
1172
1173 if(appdata->project) {
1174 project_free(appdata->project);
1175 appdata->project = NULL;
1176 }
1177
1178 if(appdata->osm) {
1179 osm_free(&appdata->icon, appdata->osm);
1180 appdata->osm = NULL;
1181 }
1182
1183 snprintf(banner_txt, _PROJECT_LOAD_BUF_SIZ,
1184 _("Error opening %s"), proj_name);
1185 banner_busy_stop(appdata);
1186 banner_show_info(appdata, banner_txt);
1187
1188 g_free(proj_name);
1189 return FALSE;
1190 }
1191
1192 /* load diff possibly preset */
1193 banner_busy_tick();
1194 diff_restore(appdata, appdata->project, appdata->osm);
1195
1196 /* prepare colors etc, draw data and adjust scroll/zoom settings */
1197 banner_busy_tick();
1198 map_init(appdata);
1199
1200 /* restore a track */
1201 banner_busy_tick();
1202 appdata->track.track = track_restore(appdata, appdata->project);
1203 if(appdata->track.track)
1204 map_track_draw(appdata->map, appdata->track.track);
1205
1206 /* finally load a background if present */
1207 banner_busy_tick();
1208 wms_load(appdata);
1209
1210 /* save the name of the project for the perferences */
1211 if(appdata->settings->project)
1212 g_free(appdata->settings->project);
1213 appdata->settings->project = g_strdup(appdata->project->name);
1214
1215 snprintf(banner_txt, _PROJECT_LOAD_BUF_SIZ, _("Loaded %s"), proj_name);
1216 banner_busy_stop(appdata);
1217 banner_show_info(appdata, banner_txt);
1218 statusbar_set(appdata, NULL, 0);
1219
1220 g_free(proj_name);
1221 return TRUE;
1222 }
1223 // vim:et:ts=8:sw=2:sts=2:ai