aboutsummaryrefslogtreecommitdiffstats
path: root/addressbook/gui/component/addressbook.c
blob: f86a58a0055359c8550c303fb59f5b6b8ae29058 (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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * addressbook.c: 
 *
 * Author:
 *   Chris Lahey (clahey@helixcode.com)
 *
 * (C) 2000 Helix Code, Inc.
 */

#include <config.h>

#include <gnome.h>
#include <libgnorba/gnorba.h>
#include <bonobo.h>

#include "addressbook.h"

#include <ebook/e-book.h>
#include <e-util/e-canvas.h>
#include <e-util/e-util.h>
#include "e-minicard-view.h"
#include "e-contact-editor.h"
#include "e-ldap-server-dialog.h"

#ifdef USING_OAF
#define CONTROL_FACTORY_ID "OAFIID:control-factory:addressbook:3e10597b-0591-4d45-b082-d781b7aa6e17"
#else
#define CONTROL_FACTORY_ID "control-factory:addressbook"
#endif

#define PROPERTY_FOLDER_URI          "folder_uri"

#define PROPERTY_FOLDER_URI_IDX      1

static void
control_deactivate (BonoboControl *control, BonoboUIHandler *uih)
{
    /* how to remove a menu item */
    bonobo_ui_handler_menu_remove (uih, "/Actions/New Contact"); 
#ifdef HAVE_LDAP
    bonobo_ui_handler_menu_remove (uih, "/Actions/New Directory Server");
#endif
    /* remove our toolbar */
    bonobo_ui_handler_dock_remove (uih, "/Toolbar");
}

static void
do_nothing_cb (BonoboUIHandler *uih, void *user_data, const char *path)
{
    printf ("Yow! I am called back!\n");
}

static void
card_added_cb (EBook* book, EBookStatus status, const char *id,
        gpointer user_data)
{
    g_print ("%s: %s(): a card was added\n", __FILE__, __FUNCTION__);
}

static void
new_contact_cb (BonoboUIHandler *uih, void *user_data, const char *path)
{
    gint result;
    GtkWidget* contact_editor =
        e_contact_editor_new(e_card_new(""));
    EMinicardView *minicard_view = E_MINICARD_VIEW (user_data);
    EBook *book;

    GtkWidget* dlg = gnome_dialog_new ("Contact Editor", "Save", "Cancel", NULL);

    gtk_object_get(GTK_OBJECT(minicard_view), "book", &book, NULL);

    g_assert (E_IS_BOOK (book));

    gtk_box_pack_start (GTK_BOX (GNOME_DIALOG (dlg)->vbox),
                contact_editor, TRUE, TRUE, 0);

    gtk_widget_show_all (dlg);

    gnome_dialog_close_hides (GNOME_DIALOG (dlg), TRUE);
    result = gnome_dialog_run_and_close (GNOME_DIALOG (dlg));

    
    /* If the user clicks "okay"...*/
    if (result == 0) {
        ECard *card;
        g_assert (contact_editor);
        g_assert (GTK_IS_OBJECT (contact_editor));
        gtk_object_get(GTK_OBJECT(contact_editor),
                   "card", &card,
                   NULL);
        
        /* Add the card in the contact editor to our ebook */
        e_book_add_card (
            book,
            card,
            card_added_cb,
            NULL);
    }
    
}

#ifdef HAVE_LDAP
static void
null_cb (EBook *book, EBookStatus status, gpointer closure)
{
}

static void
new_server_cb (BonoboUIHandler *uih, void *user_data, const char *path)
{
    EMinicardView *minicard_view = E_MINICARD_VIEW (user_data);
    ELDAPServer server;
    char *uri;
    EBook *book;

    /* fill in the defaults */
    server.host = g_strdup("");
    server.port = 389;
    server.description = g_strdup("");
    server.rootdn = g_strdup("");

    e_ldap_server_editor_show (&server);

    gtk_object_get(GTK_OBJECT(minicard_view), "book", &book, NULL);
    g_assert (E_IS_BOOK (book));
    
    /* XXX write out the new server info */

    /* now update the view */
    uri = g_strdup_printf ("ldap://%s:%d/%s", server.host, server.port, server.rootdn);

    e_book_unload_uri (book);

    if (! e_book_load_uri (book, uri, null_cb, NULL)) {
        g_warning ("error calling load_uri!\n");
    }
}
#endif

static void
find_contact_cb (BonoboUIHandler *uih, void *user_data, const char *path)
{
    gint result;
    GtkWidget* search_entry = gtk_entry_new();
    EMinicardView *minicard_view = E_MINICARD_VIEW (user_data);
    gchar* search_text;

    GtkWidget* dlg = gnome_dialog_new ("Search Contacts", "Find", "Cancel", NULL);

    gtk_object_get (GTK_OBJECT(minicard_view), "query", &search_text, NULL);
    gtk_entry_set_text(GTK_ENTRY(search_entry), search_text);
    g_free (search_text);

    gtk_box_pack_start (GTK_BOX (GNOME_DIALOG (dlg)->vbox),
                search_entry, TRUE, TRUE, 0);

    gtk_widget_show_all (dlg);

    gnome_dialog_close_hides (GNOME_DIALOG (dlg), TRUE);
    result = gnome_dialog_run_and_close (GNOME_DIALOG (dlg));

    /* If the user clicks "okay"...*/
    if (result == 0) {
        search_text = gtk_entry_get_text(GTK_ENTRY(search_entry));
        gtk_object_set (GTK_OBJECT(minicard_view), "query", search_text, NULL);
    }
    
}

static void
card_deleted_cb (EBook* book, EBookStatus status, gpointer user_data)
{
    g_print ("%s: %s(): a card was deleted\n", __FILE__, __FUNCTION__);
}

static void
delete_contact_cb (BonoboUIHandler *uih, void *user_data, const char *path)
{
    EMinicardView *minicard_view = E_MINICARD_VIEW (user_data);

    e_minicard_view_remove_selection (minicard_view, card_deleted_cb, NULL);
}

static GnomeUIInfo gnome_toolbar [] = {
    GNOMEUIINFO_ITEM_STOCK (N_("New"), N_("Create a new contact"), new_contact_cb, GNOME_STOCK_PIXMAP_NEW),

    GNOMEUIINFO_SEPARATOR,

    GNOMEUIINFO_ITEM_STOCK (N_("Find"), N_("Find a contact"), find_contact_cb, GNOME_STOCK_PIXMAP_SEARCH),
    GNOMEUIINFO_ITEM_STOCK (N_("Print"), N_("Print contacts"), do_nothing_cb, GNOME_STOCK_PIXMAP_PRINT),
    GNOMEUIINFO_ITEM_STOCK (N_("Delete"), N_("Delete a contact"), delete_contact_cb, GNOME_STOCK_PIXMAP_TRASH),
    GNOMEUIINFO_SEPARATOR,
    GNOMEUIINFO_END
};

static void
search_entry_activated (GtkWidget* widget, EMinicardView* minicard_view)
{
    char* search_word = gtk_entry_get_text(GTK_ENTRY(widget));
    char* search_query;

    if (search_word && strlen (search_word))
        search_query = g_strdup_printf (
            "(contains \"full_name\" \"%s\")",
            search_word);
    else
        search_query = g_strdup (
            "(contains \"full_name\" \"\")");       
    
    gtk_object_set (GTK_OBJECT(minicard_view), "query",
            search_query, NULL);
    g_free (search_query);
}

static GtkWidget*
make_quick_search_widget (GtkSignalFunc start_search_func,
              gpointer user_data_for_search)
{
    GtkWidget *search_vbox = gtk_vbox_new (FALSE, 0);
    GtkWidget *search_entry = gtk_entry_new ();

    if (start_search_func) 
    {
        gtk_signal_connect (GTK_OBJECT (search_entry), "activate",
                    (GtkSignalFunc) search_entry_activated,
                    user_data_for_search);
    }
    
    /* add the search entry to the our search_vbox */
    gtk_box_pack_start (GTK_BOX (search_vbox), search_entry,
                FALSE, TRUE, 3);    
    gtk_box_pack_start (GTK_BOX (search_vbox),
                gtk_label_new("Quick Search"),
                FALSE, TRUE, 0);

    return search_vbox;
}

static void
control_activate (BonoboControl *control, BonoboUIHandler *uih,
          EMinicardView *minicard_view)
{
    Bonobo_UIHandler  remote_uih;
    GtkWidget *toolbar;
    BonoboControl *toolbar_control;
    GtkWidget *hbox = gtk_hbox_new (FALSE, 0);
    GtkWidget *quick_search_widget;

    remote_uih = bonobo_control_get_remote_ui_handler (control);
    bonobo_ui_handler_set_container (uih, remote_uih);      

    bonobo_ui_handler_menu_new_item (uih, "/Actions/New Contact",
                     N_("_New Contact"),       
                     NULL, -1,
                     BONOBO_UI_HANDLER_PIXMAP_NONE, NULL,
                     0, 0, new_contact_cb,
                     (gpointer)minicard_view);

#ifdef HAVE_LDAP
    bonobo_ui_handler_menu_new_item (uih, "/Actions/New Directory Server",
                     N_("N_ew Directory Server"),       
                     NULL, -1,
                     BONOBO_UI_HANDLER_PIXMAP_NONE, NULL,
                     0, 0, new_server_cb,
                     (gpointer)minicard_view);
#endif

    toolbar = gtk_toolbar_new (GTK_ORIENTATION_HORIZONTAL,
                   GTK_TOOLBAR_BOTH);

    gnome_app_fill_toolbar_with_data (GTK_TOOLBAR (toolbar),
                      gnome_toolbar, 
                      NULL, minicard_view);
    
    gtk_box_pack_start (GTK_BOX (hbox), toolbar, FALSE, TRUE, 0);


    /* add the search_vbox to the hbox which will be our toolbar */
    quick_search_widget = make_quick_search_widget (
        search_entry_activated, minicard_view);
    
    gtk_box_pack_start (GTK_BOX (hbox),
                quick_search_widget,
                FALSE, TRUE, 0);

    gtk_widget_show_all (hbox);

    toolbar_control = bonobo_control_new (hbox);
    bonobo_ui_handler_dock_add (
        uih, "/Toolbar",
        bonobo_object_corba_objref (BONOBO_OBJECT (toolbar_control)),
        GNOME_DOCK_ITEM_BEH_LOCKED |
        GNOME_DOCK_ITEM_BEH_EXCLUSIVE,
        GNOME_DOCK_TOP,
        1, 1, 0);
}

static void
control_activate_cb (BonoboControl *control, 
             gboolean activate, 
             EMinicardView* minicard_view)
{
    BonoboUIHandler  *uih;

    uih = bonobo_control_get_ui_handler (control);
    g_assert (uih);
    
    if (activate)
        control_activate (control, uih, minicard_view);
    else
        control_deactivate (control, uih);
}

typedef struct {
    GtkWidget *canvas;
    GnomeCanvasItem *view;
    GnomeCanvasItem *rect;
    GtkAllocation last_alloc;
    BonoboPropertyBag *properties;
    char *uri;
} AddressbookView;

static void
addressbook_view_free(AddressbookView *view)
{
    if (view->properties)
        bonobo_object_unref(BONOBO_OBJECT(view->properties));
    g_free(view->uri);
    g_free(view);
}

static void
book_open_cb (EBook *book, EBookStatus status, gpointer closure)
{
    AddressbookView *view = closure;
    if (status == E_BOOK_STATUS_SUCCESS)
        gnome_canvas_item_set(view->view,
                      "book", book,
                      NULL);
}

static EBook *
ebook_create (AddressbookView *view)
{
    EBook *book;
    
    book = e_book_new ();

    if (!book) {
        printf ("%s: %s(): Couldn't create EBook, bailing.\n",
            __FILE__,
            __FUNCTION__);
        return NULL;
    }
    


    if (! e_book_load_uri (book, "file:/tmp/test.db", book_open_cb, view))
    {
        printf ("error calling load_uri!\n");
    }


    return book;
}

static void destroy_callback(GtkWidget *widget, gpointer data)
{
    AddressbookView *view = data;
    addressbook_view_free(view);
}

static void allocate_callback(GtkWidget *canvas, GtkAllocation *allocation, gpointer data)
{
    double width;
    AddressbookView *view = data;
    view->last_alloc = *allocation;
    gnome_canvas_item_set( view->view,
                   "height", (double) allocation->height,
                   NULL );
    gnome_canvas_item_set( view->view,
                   "minimum_width", (double) allocation->width,
                   NULL );
    gtk_object_get(GTK_OBJECT(view->view),
               "width", &width,
               NULL);
    width = MAX(width, allocation->width);
    gnome_canvas_set_scroll_region(GNOME_CANVAS( view->canvas ), 0, 0, width, allocation->height );
    gnome_canvas_item_set( view->rect,
                   "x2", (double) width,
                   "y2", (double) allocation->height,
                   NULL );
}

static void resize(GnomeCanvas *canvas, gpointer data)
{
    double width;
    AddressbookView *view = data;
    gtk_object_get(GTK_OBJECT(view->view),
               "width", &width,
               NULL);
    width = MAX(width, view->last_alloc.width);
    gnome_canvas_set_scroll_region(GNOME_CANVAS(view->canvas), 0, 0, width, view->last_alloc.height );
    gnome_canvas_item_set( view->rect,
                   "x2", (double) width,
                   "y2", (double) view->last_alloc.height,
                   NULL );  
}

static void
get_prop (BonoboPropertyBag *bag,
      BonoboArg         *arg,
      guint              arg_id,
      gpointer           user_data)
{
    AddressbookView *view = user_data;

    switch (arg_id) {

    case PROPERTY_FOLDER_URI_IDX:
        if (view && view->uri)
            BONOBO_ARG_SET_STRING (arg, view->uri);
        else
            BONOBO_ARG_SET_STRING (arg, "");
        break;

    default:
        g_warning ("Unhandled arg %d\n", arg_id);
    }
}

static void
set_prop (BonoboPropertyBag *bag,
      const BonoboArg   *arg,
      guint              arg_id,
      gpointer           user_data)
{
    AddressbookView *view = user_data;

    EBook *book;
    char *uri_file;
    char *uri_data;
    
    switch (arg_id) {

    case PROPERTY_FOLDER_URI_IDX:
        view->uri = g_strdup(BONOBO_ARG_GET_STRING (arg));
        
        book = e_book_new ();
        
        if (!book) {
            printf ("%s: %s(): Couldn't create EBook, bailing.\n",
                __FILE__,
                __FUNCTION__);
            return;
        }
        
        uri_file = g_concat_dir_and_file(view->uri + 7, "uri");

        uri_data = e_read_file(uri_file);

        if (!uri_data)
            uri_data = g_concat_dir_and_file(view->uri + 7, "addressbook.db");
        
        if (! e_book_load_uri (book, uri_data, book_open_cb, view))
            {
                printf ("error calling load_uri!\n");
            }
        
        g_free(uri_data);
        g_free(uri_file);

        break;
        
    default:
        g_warning ("Unhandled arg %d\n", arg_id);
        break;
    }
}

static BonoboObject *
addressbook_factory (BonoboGenericFactory *Factory, void *closure)
{
    BonoboControl      *control;
    EBook *book;
    GtkWidget *vbox, *scrollbar;
    AddressbookView *view;

    gtk_widget_push_visual (gdk_rgb_get_visual ());
    gtk_widget_push_colormap (gdk_rgb_get_cmap ());
    
    view = g_new (AddressbookView, 1);
    
    vbox = gtk_vbox_new(FALSE, 0);
    
    view->canvas = e_canvas_new();
    view->rect = gnome_canvas_item_new(
        gnome_canvas_root( GNOME_CANVAS( view->canvas ) ),
        gnome_canvas_rect_get_type(),
        "x1", (double) 0,
        "y1", (double) 0,
        "x2", (double) 100,
        "y2", (double) 100,
        "fill_color", "white",
        NULL );

    view->view = gnome_canvas_item_new(
        gnome_canvas_root( GNOME_CANVAS( view->canvas ) ),
        e_minicard_view_get_type(),
        "height", (double) 100,
        "minimum_width", (double) 100,
        NULL );

    gtk_signal_connect( GTK_OBJECT( view->canvas ), "reflow",
                GTK_SIGNAL_FUNC( resize ),
                view);

    gnome_canvas_set_scroll_region ( GNOME_CANVAS( view->canvas ),
                     0, 0,
                     100, 100 );

    gtk_box_pack_start(GTK_BOX(vbox), view->canvas, TRUE, TRUE, 0);

    scrollbar = gtk_hscrollbar_new(
        gtk_layout_get_hadjustment(GTK_LAYOUT(view->canvas)));

    gtk_box_pack_start(GTK_BOX(vbox), scrollbar, FALSE, FALSE, 0);

    /* Connect the signals */
    gtk_signal_connect( GTK_OBJECT( vbox ), "destroy",
                GTK_SIGNAL_FUNC( destroy_callback ),
                ( gpointer ) view );

    gtk_signal_connect( GTK_OBJECT( view->canvas ), "size_allocate",
                GTK_SIGNAL_FUNC( allocate_callback ),
                ( gpointer ) view );

    gtk_widget_show_all( vbox );
#if 0
    gdk_window_set_back_pixmap(
        GTK_LAYOUT(view->canvas)->bin_window, NULL, FALSE);
#endif

    book = ebook_create(view);

    /* Create the control. */
    control = bonobo_control_new(vbox);

    view->properties = bonobo_property_bag_new (get_prop, set_prop, view);

    bonobo_property_bag_add (
        view->properties, PROPERTY_FOLDER_URI, PROPERTY_FOLDER_URI_IDX,
        BONOBO_ARG_STRING, NULL, _("The URI that the Folder Browser will display"), 0);

    bonobo_control_set_property_bag (control,
                     view->properties);

    view->uri = NULL;

    gtk_signal_connect (GTK_OBJECT (control), "activate",
                control_activate_cb, view->view);

    gtk_widget_pop_visual ();
    gtk_widget_pop_colormap ();

    return BONOBO_OBJECT (control);
}

void
addressbook_factory_init (void)
{
    static BonoboGenericFactory *addressbook_control_factory = NULL;

    if (addressbook_control_factory != NULL)
        return;

    addressbook_control_factory = bonobo_generic_factory_new (CONTROL_FACTORY_ID,
                                  addressbook_factory,
                                  NULL);

    if (addressbook_control_factory == NULL) {
        g_error ("I could not register a Addressbook factory.");
    }
}