aboutsummaryrefslogtreecommitdiffstats
path: root/importers/evolution-gnomecard-importer.c
blob: 8b90d69024cc57a7ed39b6ca9b434e92d17b348a (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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* evolution-gnomecard-intelligent-importer.c
 *
 * Copyright (C) 2001  Ximian, 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: Iain Holmes  <iain@ximian.com>
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>

#include <liboaf/liboaf.h>
#include <bonobo/bonobo-context.h>
#include <bonobo/bonobo-generic-factory.h>
#include <bonobo/bonobo-main.h>
#include <bonobo/bonobo-control.h>
#include <bonobo/bonobo-exception.h>
#include <bonobo/bonobo-moniker-util.h>
#include <bonobo-conf/bonobo-config-database.h>

#include <e-book.h>

#include <importer/evolution-intelligent-importer.h>
#include <importer/GNOME_Evolution_Importer.h>
#include <importer/evolution-importer-listener.h>

#define COMPONENT_FACTORY_IID "OAFIID:GNOME_Evolution_GnomeCard_Intelligent_ImporterFactory"
#define VCARD_IMPORTER_IID "OAFIID:GNOME_Evolution_Addressbook_VCard_Importer"

typedef struct {
    GNOME_Evolution_Importer importer;
    EvolutionImporterListener *listener;

    GtkWidget *addresses;
    gboolean do_addresses;

    Bonobo_ConfigDatabase db;
} GnomeCardImporter;

static void
gnomecard_store_settings (GnomeCardImporter *importer)
{
    bonobo_config_set_boolean (importer->db, 
                   "/Importer/Gnomecard/address", 
                   importer->do_addresses, NULL);
}

static void
gnomecard_restore_settings (GnomeCardImporter *importer)
{
    importer->do_addresses = bonobo_config_get_boolean_with_default (
        importer->db, "/Importer/Gnomecard/address", TRUE, NULL);
}

static gboolean
gnomecard_can_import (EvolutionIntelligentImporter *ii,
              void *closure)
{
    GnomeCardImporter *importer = closure;
    char *gnomecard;
    gboolean result, address;

    address = bonobo_config_get_boolean_with_default (importer->db, 
                "/Importer/Gnomecard/address-imported", FALSE, NULL);

    if (address == TRUE)
        return FALSE;

    gnomecard = gnome_util_home_file ("GnomeCard.gcrd");
    result = g_file_exists (gnomecard);
    g_free (gnomecard);

    return result;
}

static gboolean
importer_timeout_fn (gpointer data)
{
    GnomeCardImporter *gci = (GnomeCardImporter *) data;
    CORBA_Object objref;
    CORBA_Environment ev;

    CORBA_exception_init (&ev);
    objref = bonobo_object_corba_objref (BONOBO_OBJECT (gci->listener));
    GNOME_Evolution_Importer_processItem (gci->importer, objref, &ev);
    CORBA_exception_free (&ev);

    return FALSE;
}
                          
static void
importer_cb (EvolutionImporterListener *listener,
         EvolutionImporterResult result,
         gboolean more_items,
         void *data)
{
    GnomeCardImporter *gci = (GnomeCardImporter *) data;
    CORBA_Environment ev;

    if (result == EVOLUTION_IMPORTER_NOT_READY ||
        result == EVOLUTION_IMPORTER_BUSY) {
        gtk_timeout_add (5000, importer_timeout_fn, data);
        return;
    }

    if (more_items) {
        g_idle_add_full (G_PRIORITY_LOW, importer_timeout_fn, data, NULL);
        return;
    }

    CORBA_exception_init (&ev);
    bonobo_object_release_unref (gci->importer, &ev);
    bonobo_object_release_unref (gci->db, &ev);
    CORBA_exception_free (&ev);

    gtk_main_quit ();
}
                 
static void
gnomecard_import (EvolutionIntelligentImporter *ii,
          void *closure)
{
    CORBA_boolean result;
    GnomeCardImporter *gci = closure;
    CORBA_Object objref;
    CORBA_Environment ev;
    char *gnomecard;

    gnomecard = gnome_util_home_file ("GnomeCard.gcrd");

    /* Reference our object so when the shell release_unrefs us
       we will still exist and not go byebye */
    bonobo_object_ref (BONOBO_OBJECT (ii));

    gnomecard_store_settings (gci);

    if (gci->do_addresses == TRUE) {

        CORBA_exception_init (&ev);
        result = GNOME_Evolution_Importer_loadFile (gci->importer, 
                                gnomecard, 
                                "", &ev);
        if (ev._major != CORBA_NO_EXCEPTION || result == FALSE) {
            g_warning ("Exception here: %s",
                   CORBA_exception_id (&ev));
            CORBA_Object_release (gci->importer, &ev);
            CORBA_exception_free (&ev);
            return;
        }

        gci->listener = evolution_importer_listener_new (importer_cb, 
                                 gci);
        objref = bonobo_object_corba_objref (BONOBO_OBJECT (gci->listener));
        GNOME_Evolution_Importer_processItem (gci->importer, objref, &ev);
        if (ev._major != CORBA_NO_EXCEPTION) {
            g_warning ("Exception: %s", CORBA_exception_id (&ev));
            return;
        }

        CORBA_exception_free (&ev);

        return;
    } else {
        bonobo_object_unref (BONOBO_OBJECT (ii));
        return;
    }
}

static void
gnomecard_destroy_cb (GtkObject *object,
              GnomeCardImporter *importer)
{
    CORBA_Environment ev;

    /* save the state of the checkboxes */
    g_print ("\n---------Settings-------\n");
    g_print ("Addressbook - %s\n", importer->do_addresses? "Yes" : "No");

    gnomecard_store_settings (importer);

    CORBA_exception_init (&ev);
    Bonobo_ConfigDatabase_sync (importer->db, &ev);
    CORBA_exception_free (&ev);

    if (importer->importer != CORBA_OBJECT_NIL) {
        bonobo_object_release_unref (importer->importer, NULL);
    }

    if (importer->db != CORBA_OBJECT_NIL) {
        bonobo_object_release_unref (importer->db, NULL);
    }
    importer->db = CORBA_OBJECT_NIL;

    gtk_main_quit ();
}

/* Fun with aggregation */
static void
checkbox_toggle_cb (GtkToggleButton *tb,
            gboolean *do_item)
{
    *do_item = gtk_toggle_button_get_active (tb);
}

static BonoboControl *
create_checkboxes_control (GnomeCardImporter *importer)
{
    GtkWidget *hbox;
    BonoboControl *control;

    hbox = gtk_hbox_new (FALSE, 2);

    importer->addresses = gtk_check_button_new_with_label (_("Addressbook"));
    gtk_signal_connect (GTK_OBJECT (importer->addresses), "toggled",
                GTK_SIGNAL_FUNC (checkbox_toggle_cb),
                &importer->do_addresses);

    gtk_box_pack_start (GTK_BOX (hbox), importer->addresses, FALSE, FALSE, 0);

    gtk_widget_show_all (hbox);
    control = bonobo_control_new (hbox);
    return control;
}

static BonoboObject *
factory_fn (BonoboGenericFactory *_factory,
        void *closure)
{
    EvolutionIntelligentImporter *importer;
    GnomeCardImporter *gci;
    char *message = N_("Evolution has found GnomeCard files.\n"
               "Would you like them to be imported into Evolution?");
    CORBA_Environment ev;
    BonoboControl *control;

    gci = g_new (GnomeCardImporter, 1);

    CORBA_exception_init (&ev);

    gci->db = bonobo_get_object ("wombat:", "Bonobo/ConfigDatabase", &ev);

    if (BONOBO_EX (&ev) || gci->db == CORBA_OBJECT_NIL) {
        g_free (gci);
        CORBA_exception_free (&ev);
        return NULL;
    }

    gnomecard_restore_settings (gci);

    gci->importer = oaf_activate_from_id (VCARD_IMPORTER_IID, 0, NULL, &ev);
    if (ev._major != CORBA_NO_EXCEPTION) {
        g_warning ("Could not start VCard importer: %s",
               CORBA_exception_id (&ev));
        CORBA_exception_free (&ev);
        return NULL;
    }
    CORBA_exception_free (&ev);

    importer = evolution_intelligent_importer_new (gnomecard_can_import, 
                               gnomecard_import, 
                               "GnomeCard",
                               _(message), gci);
    
    gtk_signal_connect (GTK_OBJECT (importer), "destroy",
                GTK_SIGNAL_FUNC (gnomecard_destroy_cb), gci);

    control = create_checkboxes_control (gci);
    bonobo_object_add_interface (BONOBO_OBJECT (importer),
                     BONOBO_OBJECT (control));
    
    return BONOBO_OBJECT (importer);
}

static void
importer_init (void)
{
    BonoboGenericFactory *factory;

    factory = bonobo_generic_factory_new (COMPONENT_FACTORY_IID, 
                          factory_fn, NULL);
    if (factory == NULL) {
        g_error ("Unable to create factory");
        exit (0);
    }

    bonobo_running_context_auto_exit_unref (BONOBO_OBJECT (factory));
}

int
main (int argc,
      char **argv)
{
    CORBA_ORB orb;
    
    bindtextdomain(PACKAGE, EVOLUTION_LOCALEDIR);
    textdomain(PACKAGE);
    
    gnome_init_with_popt_table ("Evolution-GnomeCard-Intelligent-Importer",
                    VERSION, argc, argv, oaf_popt_options, 0,
                    NULL);
    orb = oaf_init (argc, argv);
    if (bonobo_init (orb, CORBA_OBJECT_NIL, CORBA_OBJECT_NIL) == FALSE) {
        g_error ("Could not initialize Bonobo.");
        exit (0);
    }

    importer_init ();
    bonobo_main ();

    return 0;
}