From 508cff9c057a40f40d882d58dc165645825f4204 Mon Sep 17 00:00:00 2001 From: Chris Toshok Date: Thu, 29 Jan 2004 19:07:34 +0000 Subject: use the MigrationContext struct. (dialog_close): same. 2004-01-29 Chris Toshok * gui/component/addressbook-migrate.c (setup_progress_dialog): use the MigrationContext struct. (dialog_close): same. (dialog_set_folder_name): same. (dialog_set_progress): same. (migrate_contacts): same, and pass it along to other functions. (migrate_contact_folder_to_source): same. (migrate_contact_folder): same. also, add the old path/uid to the folder_uid_map. (migrate_local_folders): same. (create_groups): use the MigrationContext struct. (migrate_completion_folders): if it's a file uri, look up the old path in our hash table for a uid to use. otherwise use the old behavior. (migration_context_new): new function, initialize the MigrationContext. (migration_context_free): free up everything. (addressbook_migrate): create the context, pass it to everything, destroy the context, shine toshok's shoes, etc. svn path=/trunk/; revision=24524 --- addressbook/ChangeLog | 22 +++ addressbook/gui/component/addressbook-migrate.c | 214 +++++++++++++----------- 2 files changed, 139 insertions(+), 97 deletions(-) diff --git a/addressbook/ChangeLog b/addressbook/ChangeLog index 7fe313da48..7a44793d8e 100644 --- a/addressbook/ChangeLog +++ b/addressbook/ChangeLog @@ -1,3 +1,25 @@ +2004-01-29 Chris Toshok + + * gui/component/addressbook-migrate.c (setup_progress_dialog): use + the MigrationContext struct. + (dialog_close): same. + (dialog_set_folder_name): same. + (dialog_set_progress): same. + (migrate_contacts): same, and pass it along to other functions. + (migrate_contact_folder_to_source): same. + (migrate_contact_folder): same. also, add the old path/uid to the + folder_uid_map. + (migrate_local_folders): same. + (create_groups): use the MigrationContext struct. + (migrate_completion_folders): if it's a file uri, look up the old + path in our hash table for a uid to use. otherwise use the old + behavior. + (migration_context_new): new function, initialize the + MigrationContext. + (migration_context_free): free up everything. + (addressbook_migrate): create the context, pass it to everything, + destroy the context, shine toshok's shoes, etc. + 2004-01-29 Radek Doulik * gui/component/addressbook-config.c (dialog_to_temp_source): set diff --git a/addressbook/gui/component/addressbook-migrate.c b/addressbook/gui/component/addressbook-migrate.c index 77481f76b0..3ea90d0495 100644 --- a/addressbook/gui/component/addressbook-migrate.c +++ b/addressbook/gui/component/addressbook-migrate.c @@ -33,76 +33,88 @@ #include #include -static GtkWidget *window; -static GtkLabel *label; -static GtkProgressBar *progress; +typedef struct { + /* this hash table maps old folder uris to new uids. It's + build in migrate_contact_folder and it's used in + migrate_completion_folders. */ + GHashTable *folder_uid_map; + + ESourceList *source_list; + + AddressbookComponent *component; + + GtkWidget *window; + GtkLabel *label; + GtkProgressBar *progress; +} MigrationContext; static void -setup_progress_dialog (void) +setup_progress_dialog (MigrationContext *context) { GtkWidget *vbox, *hbox, *w; - - window = gtk_window_new (GTK_WINDOW_TOPLEVEL); - gtk_window_set_title ((GtkWindow *) window, _("Migrating...")); - gtk_window_set_modal ((GtkWindow *) window, TRUE); - gtk_container_set_border_width ((GtkContainer *) window, 6); + + context->window = gtk_window_new (GTK_WINDOW_TOPLEVEL); + gtk_window_set_title (GTK_WINDOW (context->window), _("Migrating...")); + gtk_window_set_modal (GTK_WINDOW (context->window), TRUE); + gtk_container_set_border_width (GTK_CONTAINER (context->window), 6); vbox = gtk_vbox_new (FALSE, 6); gtk_widget_show (vbox); - gtk_container_add ((GtkContainer *) window, vbox); + gtk_container_add (GTK_CONTAINER (context->window), vbox); w = gtk_label_new (_("The location and hierarchy of the Evolution contact " "folders has changed since Evolution 1.x.\n\nPlease be " "patient while Evolution migrates your folders...")); - gtk_label_set_line_wrap ((GtkLabel *) w, TRUE); + gtk_label_set_line_wrap (GTK_LABEL (w), TRUE); gtk_widget_show (w); - gtk_box_pack_start_defaults ((GtkBox *) vbox, w); + gtk_box_pack_start_defaults (GTK_BOX (vbox), w); hbox = gtk_hbox_new (FALSE, 6); gtk_widget_show (hbox); - gtk_box_pack_start_defaults ((GtkBox *) vbox, hbox); + gtk_box_pack_start_defaults (GTK_BOX (vbox), hbox); - label = (GtkLabel *) gtk_label_new (""); - gtk_widget_show ((GtkWidget *) label); - gtk_box_pack_start_defaults ((GtkBox *) hbox, (GtkWidget *) label); + context->label = GTK_LABEL (gtk_label_new ("")); + gtk_widget_show (GTK_WIDGET (context->label)); + gtk_box_pack_start_defaults (GTK_BOX (hbox), GTK_WIDGET (context->label)); - progress = (GtkProgressBar *) gtk_progress_bar_new (); - gtk_widget_show ((GtkWidget *) progress); - gtk_box_pack_start_defaults ((GtkBox *) hbox, (GtkWidget *) progress); + context->progress = GTK_PROGRESS_BAR (gtk_progress_bar_new ()); + gtk_widget_show (GTK_WIDGET (context->progress)); + gtk_box_pack_start_defaults (GTK_BOX (hbox), GTK_WIDGET (context->progress)); - gtk_widget_show (window); + gtk_widget_show (context->window); + } static void -dialog_close (void) +dialog_close (MigrationContext *context) { - gtk_widget_destroy ((GtkWidget *) window); + gtk_widget_destroy (context->window); } static void -dialog_set_folder_name (const char *folder_name) +dialog_set_folder_name (MigrationContext *context, const char *folder_name) { char *text; text = g_strdup_printf (_("Migrating `%s':"), folder_name); - gtk_label_set_text (label, text); + gtk_label_set_text (context->label, text); g_free (text); - gtk_progress_bar_set_fraction (progress, 0.0); + gtk_progress_bar_set_fraction (context->progress, 0.0); while (gtk_events_pending ()) gtk_main_iteration (); } static void -dialog_set_progress (double percent) +dialog_set_progress (MigrationContext *context, double percent) { char text[5]; snprintf (text, sizeof (text), "%d%%", (int) (percent * 100.0f)); - gtk_progress_bar_set_fraction (progress, percent); - gtk_progress_bar_set_text (progress, text); + gtk_progress_bar_set_fraction (context->progress, percent); + gtk_progress_bar_set_text (context->progress, text); while (gtk_events_pending ()) gtk_main_iteration (); @@ -170,7 +182,7 @@ get_source_name (ESourceGroup *group, const char *path) } static void -migrate_contacts (EBook *old_book, EBook *new_book) +migrate_contacts (MigrationContext *context, EBook *old_book, EBook *new_book) { EBookQuery *query = e_book_query_any_field_contains (""); GList *l, *contacts; @@ -277,12 +289,12 @@ migrate_contacts (EBook *old_book, EBook *new_book) num_added ++; - dialog_set_progress ((double)num_added / num_contacts); + dialog_set_progress (context, (double)num_added / num_contacts); } } static void -migrate_contact_folder_to_source (char *old_path, ESource *new_source) +migrate_contact_folder_to_source (MigrationContext *context, char *old_path, ESource *new_source) { char *old_uri = g_strdup_printf ("file://%s", old_path); GError *e = NULL; @@ -295,7 +307,7 @@ migrate_contact_folder_to_source (char *old_path, ESource *new_source) old_source = e_source_new ("", ""); e_source_group_add_source (group, old_source, -1); - dialog_set_folder_name (e_source_peek_name (new_source)); + dialog_set_folder_name (context, e_source_peek_name (new_source)); old_book = e_book_new (); if (!e_book_load_source (old_book, old_source, TRUE, &e)) { @@ -309,7 +321,7 @@ migrate_contact_folder_to_source (char *old_path, ESource *new_source) goto finish; } - migrate_contacts (old_book, new_book); + migrate_contacts (context, old_book, new_book); finish: g_object_unref (old_source); @@ -322,7 +334,7 @@ migrate_contact_folder_to_source (char *old_path, ESource *new_source) } static void -migrate_contact_folder (char *old_path, ESourceGroup *dest_group, char *source_name) +migrate_contact_folder (MigrationContext *context, char *old_path, ESourceGroup *dest_group, char *source_name) { ESource *new_source; @@ -330,7 +342,9 @@ migrate_contact_folder (char *old_path, ESourceGroup *dest_group, char *source_n e_source_set_relative_uri (new_source, e_source_peek_uid (new_source)); e_source_group_add_source (dest_group, new_source, -1); - migrate_contact_folder_to_source (old_path, new_source); + g_hash_table_insert (context->folder_uid_map, g_strdup (old_path), g_strdup (e_source_peek_uid (new_source))); + + migrate_contact_folder_to_source (context, old_path, new_source); g_object_unref (new_source); } @@ -339,8 +353,7 @@ migrate_contact_folder (char *old_path, ESourceGroup *dest_group, char *source_n #define PERSONAL_RELATIVE_URI "system" static void -create_groups (AddressbookComponent *component, - ESourceList *source_list, +create_groups (MigrationContext *context, ESourceGroup **on_this_computer, ESourceGroup **on_ldap_servers, ESource **personal_source) @@ -353,13 +366,13 @@ create_groups (AddressbookComponent *component, *on_this_computer = NULL; *on_ldap_servers = NULL; - base_uri = g_build_filename (addressbook_component_peek_base_directory (component), + base_uri = g_build_filename (addressbook_component_peek_base_directory (context->component), "/addressbook/local/", NULL); base_uri_proto = g_strconcat ("file://", base_uri, NULL); - groups = e_source_list_peek_groups (source_list); + groups = e_source_list_peek_groups (context->source_list); if (groups) { /* groups are already there, we need to search for things... */ GSList *g; @@ -391,7 +404,7 @@ create_groups (AddressbookComponent *component, else { /* create the local source group */ group = e_source_group_new (_("On This Computer"), base_uri_proto); - e_source_list_add_group (source_list, group, -1); + e_source_list_add_group (context->source_list, group, -1); *on_this_computer = group; } @@ -407,7 +420,7 @@ create_groups (AddressbookComponent *component, if (!*on_ldap_servers) { /* Create the LDAP source group */ group = e_source_group_new (_("On LDAP Servers"), LDAP_BASE_URI); - e_source_list_add_group (source_list, group, -1); + e_source_list_add_group (context->source_list, group, -1); *on_ldap_servers = group; } @@ -417,7 +430,7 @@ create_groups (AddressbookComponent *component, } static gboolean -migrate_local_folders (AddressbookComponent *component, ESourceGroup *on_this_computer, ESource *personal_source) +migrate_local_folders (MigrationContext *context, ESourceGroup *on_this_computer, ESource *personal_source) { char *old_path = NULL; GSList *dirs, *l; @@ -431,17 +444,19 @@ migrate_local_folders (AddressbookComponent *component, ESourceGroup *on_this_co local_contact_folder = g_build_filename (g_get_home_dir (), "/evolution/local/Contacts", NULL); if (personal_source) - migrate_contact_folder_to_source (local_contact_folder, personal_source); + for (l = dirs; l; l = l->next) { char *source_name; - /* skip the local contact folder, since we handle that - specifically, mapping it to Personal */ - if (personal_source && !strcmp ((char*)l->data, local_contact_folder)) + /* we handle the system folder differently */ + if (personal_source && !strcmp ((char*)l->data, local_contact_folder)) { + g_hash_table_insert (context->folder_uid_map, g_strdup (l->data), g_strdup (e_source_peek_uid (personal_source))); + migrate_contact_folder_to_source (context, local_contact_folder, personal_source); continue; + } source_name = get_source_name (on_this_computer, (char*)l->data); - migrate_contact_folder (l->data, on_this_computer, source_name); + migrate_contact_folder (context, l->data, on_this_computer, source_name); g_free (source_name); } @@ -501,7 +516,7 @@ get_integer_child (xmlNode *node, } static gboolean -migrate_ldap_servers (AddressbookComponent *component, ESourceGroup *on_ldap_servers) +migrate_ldap_servers (MigrationContext *context, ESourceGroup *on_ldap_servers) { char *sources_xml = g_strdup_printf ("%s/evolution/addressbook-sources.xml", g_get_home_dir ()); @@ -533,7 +548,7 @@ migrate_ldap_servers (AddressbookComponent *component, ESourceGroup *on_ldap_ser } printf ("found %d contact servers to migrate\n", num_contactservers); - dialog_set_folder_name (_("LDAP Servers")); + dialog_set_folder_name (context, _("LDAP Servers")); servernum = 0; for (child = root->children; child; child = child->next) { @@ -588,7 +603,7 @@ migrate_ldap_servers (AddressbookComponent *component, ESourceGroup *on_ldap_ser g_free (description); servernum++; - dialog_set_progress ((double)servernum/num_contactservers); + dialog_set_progress (context, (double)servernum/num_contactservers); } } @@ -637,9 +652,9 @@ get_source_by_uri (ESourceList *source_list, const char *uri) } static gboolean -migrate_completion_folders (AddressbookComponent *component, ESourceList *source_list) +migrate_completion_folders (MigrationContext *context) { - char *uris_xml = gconf_client_get_string (addressbook_component_peek_gconf_client (component), + char *uris_xml = gconf_client_get_string (addressbook_component_peek_gconf_client (context->component), "/apps/evolution/addressbook/completion/uris", NULL); @@ -653,7 +668,7 @@ migrate_completion_folders (AddressbookComponent *component, ESourceList *source if (!doc) return FALSE; - dialog_set_folder_name (_("Autocompletion Settings")); + dialog_set_folder_name (context, _("Autocompletion Settings")); root = xmlDocGetRootElement (doc); if (root == NULL || strcmp (root->name, "EvolutionFolderList") != 0) { @@ -664,13 +679,13 @@ migrate_completion_folders (AddressbookComponent *component, ESourceList *source for (child = root->children; child; child = child->next) { if (!strcmp (child->name, "folder")) { char *physical_uri = e_xml_get_string_prop_by_name (child, "physical-uri"); - char *uri; - ESource *source; + ESource *source = NULL; - /* if the physical uri is - file://... we need to convert the - path to the new directory - structure. + /* if the physical uri is file://... + we look it up in our folder_uid_map + hashtable. If it's a folder we + converted over, we should get back + a uid we can search for. if the physical_uri is anything else, we strip off the args @@ -678,45 +693,25 @@ migrate_completion_folders (AddressbookComponent *component, ESourceList *source for the uri. */ if (!strncmp (physical_uri, "file://", 7)) { - char *local_path = g_build_filename (g_get_home_dir (), - "/evolution/local/", - NULL); - - if (!strncmp (physical_uri + 7, local_path, strlen (local_path))) { - char *path_extra; - char *path; - - if (!strcmp (physical_uri + 7 + strlen (local_path), "Contacts")) - /* special case the ~/evolution/local/Contacts folder */ - path_extra = "Personal"; - else - path_extra = physical_uri + 7 + strlen (local_path); - - path = g_build_filename (g_get_home_dir (), - "/.evolution/addressbook/local/OnThisComputer", - path_extra, - NULL); - uri = g_strdup_printf ("file://%s", path); - g_free (path); - } - else { - /* if they somehow created a folder that lies - outside the evolution folder tree, just pass - the uri straight on */ - uri = g_strdup (physical_uri); - } + char *uid = g_hash_table_lookup (context->folder_uid_map, + physical_uri + 7); - g_free (local_path); + if (uid) + source = e_source_list_peek_source_by_uid (context->source_list, uid); } else { + char *uri; char *semi = strchr (physical_uri, ';'); if (semi) uri = g_strndup (physical_uri, semi - physical_uri); else uri = g_strdup (physical_uri); + + source = get_source_by_uri (context->source_list, uri); + + g_free (uri); } - source = get_source_by_uri (source_list, uri); if (source) { e_source_set_property (source, "completion", "true"); } @@ -726,7 +721,6 @@ migrate_completion_folders (AddressbookComponent *component, ESourceList *source } g_free (physical_uri); - g_free (uri); } } @@ -739,20 +733,47 @@ migrate_completion_folders (AddressbookComponent *component, ESourceList *source return TRUE; } +static MigrationContext* +migration_context_new (AddressbookComponent *component) +{ + MigrationContext *context = g_new (MigrationContext, 1); + + /* set up the mapping from old uris to new uids */ + context->folder_uid_map = g_hash_table_new_full (g_str_hash, g_str_equal, (GDestroyNotify)g_free, (GDestroyNotify)g_free); + + context->source_list = addressbook_component_peek_source_list (component); + + /* initialize our dialog */ + setup_progress_dialog (context); + + context->component = component; + + return context; +} + +static void +migration_context_free (MigrationContext *context) +{ + e_source_list_sync (context->source_list, NULL); + + g_hash_table_destroy (context->folder_uid_map); + g_free (context); +} + int addressbook_migrate (AddressbookComponent *component, int major, int minor, int revision) { - ESourceList *source_list = addressbook_component_peek_source_list (component); ESourceGroup *on_this_computer; ESourceGroup *on_ldap_servers; ESource *personal_source; + MigrationContext *context = migration_context_new (component); printf ("addressbook_migrate (%d.%d.%d)\n", major, minor, revision); /* we call this unconditionally now - create_groups either creates the groups/sources or it finds the necessary groups/sources. */ - create_groups (component, source_list, &on_this_computer, &on_ldap_servers, &personal_source); + create_groups (context, &on_this_computer, &on_ldap_servers, &personal_source); if (major <= 1) { @@ -764,25 +785,24 @@ addressbook_migrate (AddressbookComponent *component, int major, int minor, int /* we're 0.x */ (major == 0)) { - setup_progress_dialog (); if (on_this_computer) { - migrate_local_folders (component, on_this_computer, personal_source); + migrate_local_folders (context, on_this_computer, personal_source); g_object_unref (on_this_computer); } if (on_ldap_servers) { - migrate_ldap_servers (component, on_ldap_servers); + migrate_ldap_servers (context, on_ldap_servers); g_object_unref (on_ldap_servers); } if (personal_source) g_object_unref (personal_source); - migrate_completion_folders (component, source_list); + migrate_completion_folders (context); - dialog_close (); + dialog_close (context); } } - e_source_list_sync (source_list, NULL); + migration_context_free (context); return TRUE; } -- cgit v1.2.3