aboutsummaryrefslogtreecommitdiffstats
path: root/addressbook/gui/component/addressbook-migrate.c
blob: 627660b9c10b3f916c2d28c4b9b61e208e22187f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* 
 * Copyright (C) 2004, Novell, Inc.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU General Public
 * License as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * Author: Chris Toshok (toshok@ximian.com)
 */

#include <glib.h>
#include <string.h>
#include "addressbook-migrate.h"
#include <libebook/e-book-async.h>
#include <libgnome/gnome-i18n.h>
#include <gal/util/e-util.h>
#include <gal/util/e-xml-utils.h>
#include <gtk/gtkwidget.h>
#include <gtk/gtkvbox.h>
#include <gtk/gtkmain.h>
#include <gtk/gtklabel.h>
#include <gtk/gtkprogressbar.h>
#include <e-util/e-folder-map.h>

static GtkWidget *window;
static GtkLabel *label;
static GtkProgressBar *progress;

static void
setup_progress_dialog (void)
{
    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);
    
    vbox = gtk_vbox_new (FALSE, 6);
    gtk_widget_show (vbox);
    gtk_container_add ((GtkContainer *) 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_widget_show (w);
    gtk_box_pack_start_defaults ((GtkBox *) vbox, w);
    
    hbox = gtk_hbox_new (FALSE, 6);
    gtk_widget_show (hbox);
    gtk_box_pack_start_defaults ((GtkBox *) vbox, hbox);
    
    label = (GtkLabel *) gtk_label_new ("");
    gtk_widget_show ((GtkWidget *) label);
    gtk_box_pack_start_defaults ((GtkBox *) hbox, (GtkWidget *) label);
    
    progress = (GtkProgressBar *) gtk_progress_bar_new ();
    gtk_widget_show ((GtkWidget *) progress);
    gtk_box_pack_start_defaults ((GtkBox *) hbox, (GtkWidget *) progress);
    
    gtk_widget_show (window);
}

static void
dialog_close (void)
{
    gtk_widget_destroy ((GtkWidget *) window);
}

static void
dialog_set_folder_name (const char *folder_name)
{
    char *text;
    
    text = g_strdup_printf (_("Migrating `%s':"), folder_name);
    gtk_label_set_text (label, text);
    g_free (text);
    
    gtk_progress_bar_set_fraction (progress, 0.0);
    
    while (gtk_events_pending ())
        gtk_main_iteration ();
}

static void
dialog_set_progress (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);
    
    while (gtk_events_pending ())
        gtk_main_iteration ();
}

static gboolean
check_for_conflict (ESourceGroup *group, char *name)
{
    GSList *sources;
    GSList *s;

    sources = e_source_group_peek_sources (group);

    for (s = sources; s; s = s->next) {
        ESource *source = E_SOURCE (s->data);

        if (!strcmp (e_source_peek_name (source), name))
            return TRUE;
    }

    return FALSE;
}

static char *
get_source_name (ESourceGroup *group, const char *path)
{
    char **p = g_strsplit (path, "/", 0);
    int i, j, starting_index;
    int num_elements;
    gboolean conflict;
    GString *s = g_string_new ("");

    for (i = 0; p[i]; i ++) ;

    num_elements = i;
    i--;

    /* p[i] is now the last path element */

    /* check if it conflicts */
    starting_index = i;
    do {
        g_string_assign (s, "");
        for (j = starting_index; j < num_elements; j += 2) {
            if (j != starting_index)
                g_string_append_c (s, '_');
            g_string_append (s, p[j]);
        }

        conflict = check_for_conflict (group, s->str);


        /* if there was a conflict back up 2 levels (skipping the /subfolder/ element) */
        if (conflict)
            starting_index -= 2;

        /* we always break out if we can't go any further,
           regardless of whether or not we conflict. */
        if (starting_index < 0)
            break;

    } while (conflict);

    return g_string_free (s, FALSE);
}

static void
migrate_contacts (EBook *old_book, EBook *new_book)
{
    EBookQuery *query = e_book_query_any_field_contains ("");
    GList *l, *contacts;
    int num_added = 0;
    int num_contacts;

    /* both books are loaded, start the actual migration */
    e_book_get_contacts (old_book, query, &contacts, NULL);

    num_contacts = g_list_length (contacts);
    for (l = contacts; l; l = l->next) {
        EContact *contact = l->data;
        GError *e = NULL;
        GList *attrs, *attr;

        /* do some last minute massaging of the contact's attributes */

        attrs = e_vcard_get_attributes (E_VCARD (contact));
        for (attr = attrs; attr;) {
            EVCardAttribute *a = attr->data;

            /* evo 1.4 used the non-standard X-EVOLUTION-OFFICE attribute,
               evo 1.5 uses the third element in the ORG list attribute. */
            if (!strcmp ("X-EVOLUTION-OFFICE", e_vcard_attribute_get_name (a))) {
                GList *v = e_vcard_attribute_get_values (a);
                GList *next_attr;

                if (v && v->data)
                    e_contact_set (contact, E_CONTACT_OFFICE, v->data);

                next_attr = attr->next;
                e_vcard_remove_attribute (E_VCARD (contact), a);
                attr = next_attr;
            }
            /* evo 1.4 didn't put TYPE=VOICE in for phone numbers.
               evo 1.5 does.

               so we search through the attribute params for
               either TYPE=VOICE or TYPE=FAX.  If we find
               either we do nothing.  If we find neither, we
               add TYPE=VOICE.
            */
            else if (!strcmp ("TEL", e_vcard_attribute_get_name (a))) {
                GList *params, *param;
                gboolean found = FALSE;

                params = e_vcard_attribute_get_params (a);
                for (param = params; param; param = param->next) {
                    EVCardAttributeParam *p = param->data;
                    if (!strcmp (EVC_TYPE, e_vcard_attribute_param_get_name (p))) {
                        GList *v = e_vcard_attribute_param_get_values (p);
                        if (v && v->data)
                            if (!strcmp ("VOICE", v->data)
                                || !strcmp ("FAX", v->data))
                                found = TRUE;
                    }
                }

                if (!found)
                    e_vcard_attribute_add_param_with_value (a,
                                        e_vcard_attribute_param_new (EVC_TYPE),
                                        "VOICE");
                attr = attr->next;
            }
            /* this is kinda gross.  The new vcard parser
               needs ';'s to be escaped by \'s.  but the
               1.4 vcard generator would put unescaped xml
               (including entities like &gt;) in the value
               of attributes, so we need to go through and
               escape those ';'s. */
            else if (!strcmp ("EMAIL", e_vcard_attribute_get_name (a))) {
                GList *v = e_vcard_attribute_get_values (a);

                if (v && v->data) {
                    if (!strncmp ((char*)v->data, "<?xml", 5)) {
                        /* k, this is the nasty part.  we glomb all the
                           value strings back together again (if there is
                           more than one), then work our magic */
                        GString *str = g_string_new ("");
                        while (v) {
                            g_string_append (str, v->data);
                            if (v->next)
                                g_string_append_c (str, ';');
                            v = v->next;
                        }

                        e_vcard_attribute_remove_values (a);
                        e_vcard_attribute_add_value (a, str->str);
                        g_string_free (str, TRUE);
                    }
                }

                attr = attr->next;
            }
            else {
                attr = attr->next;
            }
        }

        if (!e_book_add_contact (new_book,
                     contact,
                     &e))
            g_warning ("contact add failed: `%s'", e->message);

        num_added ++;

        dialog_set_progress ((double)num_added / num_contacts);
    }
}

static void
migrate_contact_folder_to_source (char *old_path, ESourceGroup *dest_group, ESource *new_source)
{
    char *old_uri = g_strdup_printf ("file://%s", old_path);
    GError *e = NULL;

    EBook *old_book = NULL, *new_book = NULL;
    ESource *old_source;
    ESourceGroup *group;

    group = e_source_group_new ("", old_uri);
    old_source = e_source_new ("", "");
    e_source_group_add_source (group, old_source, -1);

    dialog_set_folder_name (e_source_peek_name (new_source));

    old_book = e_book_new ();
    if (!e_book_load_source (old_book, old_source, TRUE, &e)) {
        g_warning ("failed to load source book for migration: `%s'", e->message);
        goto finish;
    }

    new_book = e_book_new ();
    if (!e_book_load_source (new_book, new_source, FALSE, &e)) {
        g_warning ("failed to load destination book for migration: `%s'", e->message);
        goto finish;
    }

    migrate_contacts (old_book, new_book);

 finish:
    g_object_unref (old_source);
    g_object_unref (group);
    if (old_book)
        g_object_unref (old_book);
    if (new_book)
        g_object_unref (new_book);
    g_free (old_uri);
}

static void
migrate_contact_folder (char *old_path, ESourceGroup *dest_group, char *source_name)
{
    ESource *new_source;

    new_source = e_source_new (source_name, source_name);
    e_source_group_add_source (dest_group, new_source, -1);

    migrate_contact_folder_to_source (old_path, dest_group, new_source);

    g_object_unref (new_source);
}

#define LDAP_BASE_URI "ldap://"
#define PERSONAL_RELATIVE_URI "Personal"

static void
create_groups (AddressbookComponent *component,
           ESourceList   *source_list,
           ESourceGroup **on_this_computer,
           ESourceGroup **on_ldap_servers,
           ESource      **personal_source)
{
    GSList *groups;
    ESourceGroup *group;
    ESource *source = NULL;
    char *base_uri, *base_uri_proto, *new_dir;

    *on_this_computer = NULL;
    *on_ldap_servers = NULL;

    base_uri = g_build_filename (addressbook_component_peek_base_directory (component),
                     "/addressbook/local/OnThisComputer/",
                     NULL);

    base_uri_proto = g_strconcat ("file://", base_uri, NULL);

    groups = e_source_list_peek_groups (source_list);
    if (groups) {
        /* groups are already there, we need to search for things... */
        GSList *g;

        for (g = groups; g; g = g->next) {

            group = E_SOURCE_GROUP (g->data);

            if (!*on_this_computer && !strcmp (base_uri_proto, e_source_group_peek_base_uri (group)))
                *on_this_computer = g_object_ref (group);
            else if (!*on_ldap_servers && !strcmp (LDAP_BASE_URI, e_source_group_peek_base_uri (group)))
                *on_ldap_servers = g_object_ref (group);
        }
    }

    if (*on_this_computer) {
        /* make sure "Personal" shows up as a source under
           this group */
        GSList *sources = e_source_group_peek_sources (*on_this_computer);
        GSList *s;
        for (s = sources; s; s = s->next) {
            ESource *source = E_SOURCE (s->data);
            if (!strcmp (PERSONAL_RELATIVE_URI, e_source_peek_relative_uri (source))) {
                *personal_source = g_object_ref (source);
                break;
            }
        }
    }
    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);

        *on_this_computer = group;
    }

    if (!source) {
        /* Create the default Person addressbook */
        new_dir = g_build_filename (base_uri, "Personal/", NULL);
        if (!e_mkdir_hier (new_dir, 0700)) {
            source = e_source_new (_("Personal"), PERSONAL_RELATIVE_URI);
            e_source_group_add_source (*on_this_computer, source, -1);

            *personal_source = source;
        }
        g_free (new_dir);
    }

    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);

        *on_ldap_servers = group;
    }

    g_free (base_uri_proto);
    g_free (base_uri);
}

static gboolean
migrate_local_folders (AddressbookComponent *component, ESourceGroup *on_this_computer, ESource *personal_source)
{
    char *old_path = NULL;
    GSList *dirs, *l;
    char *local_contact_folder = NULL;

    old_path = g_strdup_printf ("%s/evolution/local", g_get_home_dir ());

    dirs = e_folder_map_local_folders (old_path, "contacts");

    /* migrate the local addressbook first, to OnThisComputer/Personal */
    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, on_this_computer, 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))
            continue;

        source_name = get_source_name (on_this_computer, (char*)l->data);
        migrate_contact_folder (l->data, on_this_computer, source_name);
        g_free (source_name);
    }

    g_slist_foreach (dirs, (GFunc)g_free, NULL);
    g_slist_free (dirs);
    g_free (local_contact_folder);
    g_free (old_path);

    return TRUE;
}

static char *
get_string_child (xmlNode *node,
          const char *name)
{
    xmlNode *p;
    xmlChar *xml_string;
    char *retval;

    p = e_xml_get_child_by_name (node, (xmlChar *) name);
    if (p == NULL)
        return NULL;

    p = e_xml_get_child_by_name (p, (xmlChar *) "text");
    if (p == NULL) /* there's no text between the tags, return the empty string */
        return g_strdup("");

    xml_string = xmlNodeListGetString (node->doc, p, 1);
    retval = g_strdup ((char *) xml_string);
    xmlFree (xml_string);

    return retval;
}

static int
get_integer_child (xmlNode *node,
           const char *name,
           int defval)
{
    xmlNode *p;
    xmlChar *xml_string;
    int retval;

    p = e_xml_get_child_by_name (node, (xmlChar *) name);
    if (p == NULL)
        return defval;

    p = e_xml_get_child_by_name (p, (xmlChar *) "text");
    if (p == NULL) /* there's no text between the tags, return the default */
        return defval;

    xml_string = xmlNodeListGetString (node->doc, p, 1);
    retval = atoi (xml_string);
    xmlFree (xml_string);

    return retval;
}

static gboolean
migrate_ldap_servers (AddressbookComponent *component, ESourceGroup *on_ldap_servers)
{
    char *sources_xml = g_strdup_printf ("%s/evolution/addressbook-sources.xml",
                         g_get_home_dir ());

    printf ("trying to migrate from %s\n", sources_xml);

    if (g_file_test (sources_xml, G_FILE_TEST_EXISTS)) {
        xmlDoc  *doc = xmlParseFile (sources_xml);
        xmlNode *root;
        xmlNode *child;
        int num_contactservers;
        int servernum;

        if (!doc)
            return FALSE;

        root = xmlDocGetRootElement (doc);
        if (root == NULL || strcmp (root->name, "addressbooks") != 0) {
            xmlFreeDoc (doc);
            return FALSE;
        }

        /* count the number of servers, so we can give progress */
        num_contactservers = 0;
        for (child = root->children; child; child = child->next) {
            if (!strcmp (child->name, "contactserver")) {
                num_contactservers++;
            }
        }
        printf ("found %d contact servers to migrate\n", num_contactservers);

        dialog_set_folder_name (_("LDAP Servers"));

        servernum = 0;
        for (child = root->children; child; child = child->next) {
            if (!strcmp (child->name, "contactserver")) {
                char *port, *host, *rootdn, *scope, *authmethod, *ssl;
                char *emailaddr, *binddn, *limitstr;
                int limit;
                char *name, *description;
                GString *uri = g_string_new ("");
                ESource *source;

                name        = get_string_child (child, "name");
                description = get_string_child (child, "description");
                port        = get_string_child (child, "port");
                host        = get_string_child (child, "host");
                rootdn      = get_string_child (child, "rootdn");
                    scope       = get_string_child (child, "scope");
                authmethod  = get_string_child (child, "authmethod");
                ssl         = get_string_child (child, "ssl");
                emailaddr   = get_string_child (child, "emailaddr");
                binddn      = get_string_child (child, "binddn");
                limit       = get_integer_child (child, "limit", 100);
                limitstr    = g_strdup_printf ("%d", limit);

                g_string_append_printf (uri,
                            "%s:%s/%s?"/*trigraph prevention*/"?%s",
                            host, port, rootdn, scope);

                source = e_source_new (name, uri->str);
                e_source_set_property (source, "description", description);
                e_source_set_property (source, "limit", limitstr);
                e_source_set_property (source, "ssl", ssl);
                e_source_set_property (source, "auth", authmethod);
                if (emailaddr)
                    e_source_set_property (source, "email_addr", emailaddr);
                if (binddn)
                    e_source_set_property (source, "binddn", binddn);

                e_source_group_add_source (on_ldap_servers, source, -1);

                g_string_free (uri, TRUE);
                g_free (port);
                g_free (host);
                g_free (rootdn);
                g_free (scope);
                g_free (authmethod);
                g_free (ssl);
                g_free (emailaddr);
                g_free (binddn);
                g_free (limitstr);
                g_free (name);
                g_free (description);

                servernum++;
                dialog_set_progress ((double)servernum/num_contactservers);
            }
        }

        xmlFreeDoc (doc);
    }

    g_free (sources_xml);

    return TRUE;
}

static ESource*
get_source_by_uri (ESourceList *source_list, const char *uri)
{
    GSList *groups;
    GSList *g;

    groups = e_source_list_peek_groups (source_list);
    if (!groups)
        return NULL;

    for (g = groups; g; g = g->next) {
        GSList *sources;
        GSList *s;
        ESourceGroup *group = E_SOURCE_GROUP (g->data);

        sources = e_source_group_peek_sources (group);
        if (!sources)
            continue;

        for (s = sources; s; s = s->next) {
            ESource *source = E_SOURCE (s->data);
            char *source_uri = e_source_get_uri (source);
            gboolean found = FALSE;

            if (!strcmp (uri, source_uri))
                found = TRUE;

            g_free (source_uri);
            if (found)
                return source;
        }
    }

    return NULL;
}

static gboolean
migrate_completion_folders (AddressbookComponent *component, ESourceList *source_list)
{
    char *uris_xml = gconf_client_get_string (addressbook_component_peek_gconf_client (component),
                          "/apps/evolution/addressbook/completion/uris",
                          NULL);

    printf ("trying to migrate completion folders\n");

    if (uris_xml) {
        xmlDoc  *doc = xmlParseMemory (uris_xml, strlen (uris_xml));
        xmlNode *root;
        xmlNode *child;

        if (!doc)
            return FALSE;

        dialog_set_folder_name (_("Autocompletion Settings"));

        root = xmlDocGetRootElement (doc);
        if (root == NULL || strcmp (root->name, "EvolutionFolderList") != 0) {
            xmlFreeDoc (doc);
            return FALSE;
        }

        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;

                /* if the physical uri is
                   file://... we need to convert the
                   path to the new directory
                   structure.

                   if the physical_uri is anything
                   else, we strip off the args
                   (anything after ;) before searching
                   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);
                    }

                    g_free (local_path);
                }
                else {
                    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 (source_list, uri);
                if (source) {
                    e_source_set_property (source, "completion", "true");
                }
                else {
                    g_warning ("found completion folder with uri `%s' that "
                           "doesn't correspond to anything we migrated.", physical_uri);
                }

                g_free (physical_uri);
                g_free (uri);
            }
        }

        g_free (uris_xml);
    }
    else {
        g_message ("no completion folder settings to migrate");
    }

    return TRUE;
}

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;

    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);

    if (major <= 1) {
        
        if (/* we're <= 1.5.2 */
            (major == 1
             && ((minor == 5 && revision <= 2) 
             || (minor < 5)))
            ||
            /* we're 0.x */
            (major == 0)) {

            setup_progress_dialog ();
            if (on_this_computer) {
                migrate_local_folders (component, on_this_computer, personal_source);
                g_object_unref (on_this_computer);
            }
            if (on_ldap_servers) {
                migrate_ldap_servers (component, on_ldap_servers);
                g_object_unref (on_ldap_servers);
            }
            if (personal_source)
                g_object_unref (personal_source);

            migrate_completion_folders (component, source_list);

            dialog_close ();
        }
    }

    e_source_list_sync (source_list, NULL);

    return TRUE;
}