aboutsummaryrefslogtreecommitdiffstats
path: root/addressbook/gui/widgets/e-addressbook-reflow-adapter.c
blob: 017b14ac1974f858f8ec9ab0d6480ab014c76254 (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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) version 3.
 *
 * 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with the program; if not, see <http://www.gnu.org/licenses/>
 *
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 */

#include <config.h>
#include <string.h>

#include <glib/gi18n.h>
#include "e-addressbook-reflow-adapter.h"
#include "e-addressbook-model.h"
#include "eab-gui-util.h"

#include "e-minicard.h"
#include <e-util/e-util.h>
#include "addressbook/printing/e-contact-print.h"

struct _EAddressbookReflowAdapterPrivate {
    EAddressbookModel *model;

    gboolean loading;

    gint create_contact_id, remove_contact_id, modify_contact_id, model_changed_id;
    gint search_started_id, search_result_id;
};

#define PARENT_TYPE e_reflow_model_get_type()
static EReflowModel *parent_class;

#define d(x)

enum {
    PROP_0,
    PROP_BOOK,
    PROP_QUERY,
    PROP_EDITABLE,
    PROP_MODEL
};

enum {
    DRAG_BEGIN,
    OPEN_CONTACT,
    LAST_SIGNAL
};

static guint signals[LAST_SIGNAL] = {0, };

static void
unlink_model (EAddressbookReflowAdapter *adapter)
{
    EAddressbookReflowAdapterPrivate *priv = adapter->priv;

    if (priv->model && priv->create_contact_id)
        g_signal_handler_disconnect (priv->model,
                         priv->create_contact_id);
    if (priv->model && priv->remove_contact_id)
        g_signal_handler_disconnect (priv->model,
                         priv->remove_contact_id);
    if (priv->model && priv->modify_contact_id)
        g_signal_handler_disconnect (priv->model,
                         priv->modify_contact_id);
    if (priv->model && priv->model_changed_id)
        g_signal_handler_disconnect (priv->model,
                         priv->model_changed_id);
    if (priv->model && priv->search_started_id)
        g_signal_handler_disconnect (priv->model,
                         priv->search_started_id);
    if (priv->model && priv->search_result_id)
        g_signal_handler_disconnect (priv->model,
                         priv->search_result_id);

    priv->create_contact_id = 0;
    priv->remove_contact_id = 0;
    priv->modify_contact_id = 0;
    priv->model_changed_id = 0;
    priv->search_started_id = 0;
    priv->search_result_id = 0;

    if (priv->model)
        g_object_unref (priv->model);

    priv->model = NULL;
}

static gint
text_height (PangoLayout *layout, const gchar *text)
{
    gint height;

    pango_layout_set_text (layout, text, -1);

    pango_layout_get_pixel_size (layout, NULL, &height);

    return height;
}

static void
addressbook_dispose (GObject *object)
{
    EAddressbookReflowAdapter *adapter = E_ADDRESSBOOK_REFLOW_ADAPTER (object);

    unlink_model (adapter);
}

static void
addressbook_set_width (EReflowModel *erm, gint width)
{
}

/* This function returns the number of items in our EReflowModel. */
static gint
addressbook_count (EReflowModel *erm)
{
    EAddressbookReflowAdapter *adapter = E_ADDRESSBOOK_REFLOW_ADAPTER (erm);
    EAddressbookReflowAdapterPrivate *priv = adapter->priv;

    return e_addressbook_model_contact_count (priv->model);
}

/* This function returns the height of the minicontact in question */
static gint
addressbook_height (EReflowModel *erm, gint i, GnomeCanvasGroup *parent)
{
    EAddressbookReflowAdapter *adapter = E_ADDRESSBOOK_REFLOW_ADAPTER (erm);
    EAddressbookReflowAdapterPrivate *priv = adapter->priv;
    EContactField field;
    gint count = 0;
    gchar *string;
    EContact *contact = (EContact*) e_addressbook_model_contact_at (priv->model, i);
    PangoLayout *layout;
    gint height;

    layout = gtk_widget_create_pango_layout (
        GTK_WIDGET (GNOME_CANVAS_ITEM (parent)->canvas), "");

    string = e_contact_get (contact, E_CONTACT_FILE_AS);
    height = text_height (layout, string ? string : "") + 10.0;
    g_free (string);

    for (field = E_CONTACT_FULL_NAME;
         field != E_CONTACT_LAST_SIMPLE_STRING && count < 5; field++) {

        if (field == E_CONTACT_FAMILY_NAME || field == E_CONTACT_GIVEN_NAME)
            continue;

        string = e_contact_get (contact, field);
        if (string && *string) {
            gint this_height;
            gint field_text_height;

            this_height = text_height (layout, e_contact_pretty_name (field));

            field_text_height = text_height (layout, string);
            if (this_height < field_text_height)
                this_height = field_text_height;

            this_height += 3;

            height += this_height;
            count++;
        }
        g_free (string);
    }
    height += 2;

    g_object_unref (layout);

    return height;
}

static GHashTable *
addressbook_create_cmp_cache (EReflowModel *erm)
{
    EAddressbookReflowAdapter *adapter = E_ADDRESSBOOK_REFLOW_ADAPTER (erm);
    EAddressbookReflowAdapterPrivate *priv = adapter->priv;
    GHashTable *cmp_cache;
    gint ii, count;

    count = e_reflow_model_count (erm);

    if (priv->loading || count <= 0)
        return NULL;

    cmp_cache = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, g_free);

    for (ii = 0; ii < count; ii++) {
        EContact *contact;

        contact  = (EContact *)
            e_addressbook_model_contact_at (priv->model, ii);
        if (contact != NULL) {
            const gchar *file_as;

            file_as = e_contact_get_const (
                contact, E_CONTACT_FILE_AS);
            if (file_as != NULL)
                g_hash_table_insert (
                    cmp_cache, GINT_TO_POINTER (ii),
                    g_utf8_collate_key (file_as, -1));
        }
    }

    return cmp_cache;
}

static gint
addressbook_compare (EReflowModel *erm, gint n1, gint n2, GHashTable *cmp_cache)
{
    EAddressbookReflowAdapter *adapter = E_ADDRESSBOOK_REFLOW_ADAPTER (erm);
    EAddressbookReflowAdapterPrivate *priv = adapter->priv;
    EContact *contact1, *contact2;

    if (priv->loading) {
        return n1-n2;
    }
    else {
        contact1 = (EContact*) e_addressbook_model_contact_at (priv->model, n1);
        contact2 = (EContact*) e_addressbook_model_contact_at (priv->model, n2);

        if (contact1 && contact2) {
            const gchar *file_as1, *file_as2;
            const gchar *uid1, *uid2;

            if (cmp_cache) {
                file_as1 = g_hash_table_lookup (cmp_cache, GINT_TO_POINTER (n1));
                file_as2 = g_hash_table_lookup (cmp_cache, GINT_TO_POINTER (n2));
                if (file_as1 && file_as2)
                    return strcmp (file_as1, file_as2);
            } else {
                file_as1 = e_contact_get_const (contact1, E_CONTACT_FILE_AS);
                file_as2 = e_contact_get_const (contact2, E_CONTACT_FILE_AS);
                if (file_as1 && file_as2)
                    return g_utf8_collate (file_as1, file_as2);
            }
            if (file_as1)
                return -1;
            if (file_as2)
                return 1;
            uid1 = e_contact_get_const (contact1, E_CONTACT_UID);
            uid2 = e_contact_get_const (contact2, E_CONTACT_UID);
            if (uid1 && uid2)
                return strcmp (uid1, uid2);
            if (uid1)
                return -1;
            if (uid2)
                return 1;
        }
        if (contact1)
            return -1;
        if (contact2)
            return 1;
        return 0;
    }
}

static gint
adapter_drag_begin (EMinicard *card,
                    GdkEvent *event,
                    EAddressbookReflowAdapter *adapter)
{
    gint ret_val = 0;

    g_signal_emit (adapter,
               signals[DRAG_BEGIN], 0,
               event, &ret_val);

    return ret_val;
}

static void
adapter_open_contact (EMinicard *card,
                      EContact *contact,
                      EAddressbookReflowAdapter *adapter)
{
    g_signal_emit (adapter, signals[OPEN_CONTACT], 0, contact);
}

static GnomeCanvasItem *
addressbook_incarnate (EReflowModel *erm, gint i, GnomeCanvasGroup *parent)
{
    EAddressbookReflowAdapter *adapter = E_ADDRESSBOOK_REFLOW_ADAPTER (erm);
    EAddressbookReflowAdapterPrivate *priv = adapter->priv;
    GnomeCanvasItem *item;

    item = gnome_canvas_item_new (parent,
                     e_minicard_get_type (),
                     "contact", e_addressbook_model_contact_at (priv->model, i),
                     "editable", e_addressbook_model_get_editable (priv->model),
                     NULL);

#if 0
    g_signal_connect (item, "selected",
              G_CALLBACK (card_selected), 0, emvm);
#endif

    g_signal_connect (item, "drag_begin",
              G_CALLBACK (adapter_drag_begin), adapter);

    g_signal_connect (item, "open-contact",
              G_CALLBACK (adapter_open_contact), adapter);

    return item;
}

static void
addressbook_reincarnate (EReflowModel *erm, gint i, GnomeCanvasItem *item)
{
    EAddressbookReflowAdapter *adapter = E_ADDRESSBOOK_REFLOW_ADAPTER (erm);
    EAddressbookReflowAdapterPrivate *priv = adapter->priv;

    gnome_canvas_item_set (item,
                  "contact", e_addressbook_model_contact_at (priv->model, i),
                  NULL);
}

static void
create_contact (EAddressbookModel *model,
        gint index, gint count,
        EAddressbookReflowAdapter *adapter)
{
    e_reflow_model_items_inserted (E_REFLOW_MODEL (adapter),
                       index,
                       count);
}

static void
remove_contacts (EAddressbookModel *model,
        gpointer data,
        EAddressbookReflowAdapter *adapter)
{
    GArray *indices = (GArray *) data;
    gint count = indices->len;

    if (count == 1)
        e_reflow_model_item_removed (
            E_REFLOW_MODEL (adapter),
            g_array_index (indices, gint, 0));
    else
        e_reflow_model_changed (E_REFLOW_MODEL (adapter));

}

static void
modify_contact (EAddressbookModel *model,
        gint index,
        EAddressbookReflowAdapter *adapter)
{
    e_reflow_model_item_changed (E_REFLOW_MODEL (adapter), index);
}

static void
model_changed (EAddressbookModel *model,
           EAddressbookReflowAdapter *adapter)
{
    e_reflow_model_changed (E_REFLOW_MODEL (adapter));
}

static void
search_started (EAddressbookModel *model,
        EAddressbookReflowAdapter *adapter)
{
    EAddressbookReflowAdapterPrivate *priv = adapter->priv;

    priv->loading = TRUE;
}

static void
search_result (EAddressbookModel *model,
           EBookViewStatus status,
           const gchar *error_msg,
           EAddressbookReflowAdapter *adapter)
{
    EAddressbookReflowAdapterPrivate *priv = adapter->priv;

    priv->loading = FALSE;

    e_reflow_model_comparison_changed (E_REFLOW_MODEL (adapter));
}

static void
addressbook_set_property (GObject *object,
                          guint prop_id,
                          const GValue *value,
                          GParamSpec *pspec)
{
    EAddressbookReflowAdapter *adapter = E_ADDRESSBOOK_REFLOW_ADAPTER (object);
    EAddressbookReflowAdapterPrivate *priv = adapter->priv;

    switch (prop_id) {
    case PROP_BOOK:
        g_object_set (priv->model,
                  "book", g_value_get_object (value),
                  NULL);
        break;
    case PROP_QUERY:
        g_object_set (priv->model,
                  "query", g_value_get_string (value),
                  NULL);
        break;
    case PROP_EDITABLE:
        g_object_set (priv->model,
                  "editable", g_value_get_boolean (value),
                  NULL);
        break;
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
        break;
    }
}

static void
addressbook_get_property (GObject *object,
                          guint prop_id,
                          GValue *value,
                          GParamSpec *pspec)
{
    EAddressbookReflowAdapter *adapter = E_ADDRESSBOOK_REFLOW_ADAPTER (object);
    EAddressbookReflowAdapterPrivate *priv = adapter->priv;

    switch (prop_id) {
    case PROP_BOOK: {
        g_object_get_property (G_OBJECT (priv->model),
                       "book", value);
        break;
    }
    case PROP_QUERY: {
        g_object_get_property (G_OBJECT (priv->model),
                       "query", value);
        break;
    }
    case PROP_EDITABLE: {
        g_object_get_property (G_OBJECT (priv->model),
                       "editable", value);
        break;
    }
    case PROP_MODEL:
        g_value_set_object (value, priv->model);
        break;
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
        break;
    }
}

static void
e_addressbook_reflow_adapter_class_init (GObjectClass *object_class)
{
    EReflowModelClass *model_class = (EReflowModelClass *) object_class;

    parent_class = g_type_class_peek_parent (object_class);

    object_class->set_property = addressbook_set_property;
    object_class->get_property = addressbook_get_property;
    object_class->dispose = addressbook_dispose;

    g_object_class_install_property (object_class, PROP_BOOK,
                     g_param_spec_object ("book",
                                  "Book",
                                  NULL,
                                  E_TYPE_BOOK,
                                  G_PARAM_READWRITE));

    g_object_class_install_property (object_class, PROP_QUERY,
                     g_param_spec_string ("query",
                                  "Query",
                                  NULL,
                                  NULL,
                                  G_PARAM_READWRITE));

    g_object_class_install_property (object_class, PROP_EDITABLE,
                     g_param_spec_boolean ("editable",
                                   "Editable",
                                   NULL,
                                   FALSE,
                                   G_PARAM_READWRITE));

    g_object_class_install_property (object_class, PROP_MODEL,
                     g_param_spec_object ("model",
                                  "Model",
                                  NULL,
                                  E_TYPE_ADDRESSBOOK_MODEL,
                                  G_PARAM_READABLE));

    signals[DRAG_BEGIN] =
        g_signal_new ("drag_begin",
                  G_OBJECT_CLASS_TYPE (object_class),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (EAddressbookReflowAdapterClass, drag_begin),
                  NULL, NULL,
                  e_marshal_INT__POINTER,
                  G_TYPE_INT, 1, G_TYPE_POINTER);

    signals[OPEN_CONTACT] =
        g_signal_new ("open-contact",
                  G_OBJECT_CLASS_TYPE (object_class),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (EAddressbookReflowAdapterClass, open_contact),
                  NULL, NULL,
                  g_cclosure_marshal_VOID__OBJECT,
                  G_TYPE_NONE, 1,
                  E_TYPE_CONTACT);

    model_class->set_width = addressbook_set_width;
    model_class->count = addressbook_count;
    model_class->height = addressbook_height;
    model_class->create_cmp_cache = addressbook_create_cmp_cache;
    model_class->compare = addressbook_compare;
    model_class->incarnate = addressbook_incarnate;
    model_class->reincarnate = addressbook_reincarnate;
}

static void
e_addressbook_reflow_adapter_init (GObject *object)
{
    EAddressbookReflowAdapter *adapter = E_ADDRESSBOOK_REFLOW_ADAPTER (object);
    EAddressbookReflowAdapterPrivate *priv;

    priv = adapter->priv = g_new0 (EAddressbookReflowAdapterPrivate, 1);

    priv->loading = FALSE;
    priv->create_contact_id = 0;
    priv->remove_contact_id = 0;
    priv->modify_contact_id = 0;
    priv->model_changed_id = 0;
    priv->search_started_id = 0;
    priv->search_result_id = 0;
}

GType
e_addressbook_reflow_adapter_get_type (void)
{
    static GType type = 0;

    if (!type) {
        static const GTypeInfo info =  {
            sizeof (EAddressbookReflowAdapterClass),
            NULL,           /* base_init */
            NULL,           /* base_finalize */
            (GClassInitFunc) e_addressbook_reflow_adapter_class_init,
            NULL,           /* class_finalize */
            NULL,           /* class_data */
            sizeof (EAddressbookReflowAdapter),
            0,             /* n_preallocs */
            (GInstanceInitFunc) e_addressbook_reflow_adapter_init,
        };

        type = g_type_register_static (PARENT_TYPE, "EAddressbookReflowAdapter", &info, 0);
    }

    return type;
}

void
e_addressbook_reflow_adapter_construct (EAddressbookReflowAdapter *adapter,
                    EAddressbookModel *model)
{
    EAddressbookReflowAdapterPrivate *priv = adapter->priv;

    priv->model = model;
    g_object_ref (priv->model);

    priv->create_contact_id = g_signal_connect (priv->model,
                        "contact_added",
                        G_CALLBACK (create_contact),
                        adapter);
    priv->remove_contact_id = g_signal_connect (priv->model,
                        "contacts_removed",
                        G_CALLBACK (remove_contacts),
                        adapter);
    priv->modify_contact_id = g_signal_connect (priv->model,
                        "contact_changed",
                        G_CALLBACK (modify_contact),
                        adapter);
    priv->model_changed_id = g_signal_connect (priv->model,
                          "model_changed",
                          G_CALLBACK (model_changed),
                          adapter);
    priv->search_started_id = g_signal_connect (priv->model,
                           "search_started",
                           G_CALLBACK (search_started),
                           adapter);
    priv->search_result_id = g_signal_connect (priv->model,
                          "search_result",
                          G_CALLBACK (search_result),
                          adapter);
}

EReflowModel *
e_addressbook_reflow_adapter_new (EAddressbookModel *model)
{
    EAddressbookReflowAdapter *et;

    et = g_object_new (E_TYPE_ADDRESSBOOK_REFLOW_ADAPTER, NULL);

    e_addressbook_reflow_adapter_construct (et, model);

    return E_REFLOW_MODEL (et);
}


EContact *
e_addressbook_reflow_adapter_get_contact (EAddressbookReflowAdapter *adapter,
                      gint index)
{
    EAddressbookReflowAdapterPrivate *priv = adapter->priv;

    return e_addressbook_model_get_contact (priv->model, index);
}