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
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* e-destination-store.c - EDestination store with GtkTreeModel interface.
*
* Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of version 2 of the GNU Lesser 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Authors: Hans Petter Jansson <hpj@novell.com>
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <string.h>
#include <glib/gi18n-lib.h>
#include "e-destination-store.h"
#define ITER_IS_VALID(destination_store, iter) \
((iter)->stamp == (destination_store)->priv->stamp)
#define ITER_GET(iter) \
GPOINTER_TO_INT (iter->user_data)
#define ITER_SET(destination_store, iter, index) \
G_STMT_START { \
(iter)->stamp = (destination_store)->priv->stamp; \
(iter)->user_data = GINT_TO_POINTER (index); \
} G_STMT_END
#define E_DESTINATION_STORE_GET_PRIVATE(obj) \
(G_TYPE_INSTANCE_GET_PRIVATE \
((obj), E_TYPE_DESTINATION_STORE, EDestinationStorePrivate))
struct _EDestinationStorePrivate {
GPtrArray *destinations;
gint stamp;
};
static GType column_types[E_DESTINATION_STORE_NUM_COLUMNS];
static void e_destination_store_tree_model_init (GtkTreeModelIface *iface);
G_DEFINE_TYPE_EXTENDED (
EDestinationStore, e_destination_store, G_TYPE_OBJECT, 0,
G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_MODEL, e_destination_store_tree_model_init);
column_types[E_DESTINATION_STORE_COLUMN_NAME] = G_TYPE_STRING;
column_types[E_DESTINATION_STORE_COLUMN_EMAIL] = G_TYPE_STRING;
column_types[E_DESTINATION_STORE_COLUMN_ADDRESS] = G_TYPE_STRING;
)
static GtkTreeModelFlags e_destination_store_get_flags (GtkTreeModel *tree_model);
static gint e_destination_store_get_n_columns (GtkTreeModel *tree_model);
static GType e_destination_store_get_column_type (GtkTreeModel *tree_model,
gint index);
static gboolean e_destination_store_get_iter (GtkTreeModel *tree_model,
GtkTreeIter *iter,
GtkTreePath *path);
static void e_destination_store_get_value (GtkTreeModel *tree_model,
GtkTreeIter *iter,
gint column,
GValue *value);
static gboolean e_destination_store_iter_next (GtkTreeModel *tree_model,
GtkTreeIter *iter);
static gboolean e_destination_store_iter_children (GtkTreeModel *tree_model,
GtkTreeIter *iter,
GtkTreeIter *parent);
static gboolean e_destination_store_iter_has_child (GtkTreeModel *tree_model,
GtkTreeIter *iter);
static gint e_destination_store_iter_n_children (GtkTreeModel *tree_model,
GtkTreeIter *iter);
static gboolean e_destination_store_iter_nth_child (GtkTreeModel *tree_model,
GtkTreeIter *iter,
GtkTreeIter *parent,
gint n);
static gboolean e_destination_store_iter_parent (GtkTreeModel *tree_model,
GtkTreeIter *iter,
GtkTreeIter *child);
static void destination_changed (EDestinationStore *destination_store, EDestination *destination);
static void stop_destination (EDestinationStore *destination_store, EDestination *destination);
static void
destination_store_dispose (GObject *object)
{
EDestinationStorePrivate *priv;
gint ii;
priv = E_DESTINATION_STORE_GET_PRIVATE (object);
for (ii = 0; ii < priv->destinations->len; ii++) {
EDestination *destination;
destination = g_ptr_array_index (priv->destinations, ii);
stop_destination (E_DESTINATION_STORE (object), destination);
g_object_unref (destination);
}
g_ptr_array_set_size (priv->destinations, 0);
/* Chain up to parent's dispose() method. */
G_OBJECT_CLASS (e_destination_store_parent_class)->dispose (object);
}
static void
destination_store_finalize (GObject *object)
{
EDestinationStorePrivate *priv;
priv = E_DESTINATION_STORE_GET_PRIVATE (object);
g_ptr_array_free (priv->destinations, TRUE);
/* Chain up to parent's finalize() method. */
G_OBJECT_CLASS (e_destination_store_parent_class)->finalize (object);
}
static void
e_destination_store_class_init (EDestinationStoreClass *class)
{
GObjectClass *object_class;
g_type_class_add_private (class, sizeof (EDestinationStorePrivate));
object_class = G_OBJECT_CLASS (class);
object_class->dispose = destination_store_dispose;
object_class->finalize = destination_store_finalize;
}
static void
e_destination_store_tree_model_init (GtkTreeModelIface *iface)
{
iface->get_flags = e_destination_store_get_flags;
iface->get_n_columns = e_destination_store_get_n_columns;
iface->get_column_type = e_destination_store_get_column_type;
iface->get_iter = e_destination_store_get_iter;
iface->get_path = e_destination_store_get_path;
iface->get_value = e_destination_store_get_value;
iface->iter_next = e_destination_store_iter_next;
iface->iter_children = e_destination_store_iter_children;
iface->iter_has_child = e_destination_store_iter_has_child;
iface->iter_n_children = e_destination_store_iter_n_children;
iface->iter_nth_child = e_destination_store_iter_nth_child;
iface->iter_parent = e_destination_store_iter_parent;
}
static void
e_destination_store_init (EDestinationStore *destination_store)
{
destination_store->priv =
E_DESTINATION_STORE_GET_PRIVATE (destination_store);
destination_store->priv->destinations = g_ptr_array_new ();
destination_store->priv->stamp = g_random_int ();
}
/**
* e_destination_store_new:
*
* Creates a new #EDestinationStore.
*
* Returns: A new #EDestinationStore.
**/
EDestinationStore *
e_destination_store_new (void)
{
return g_object_new (E_TYPE_DESTINATION_STORE, NULL);
}
/* ------------------ *
* Row update helpers *
* ------------------ */
static void
row_deleted (EDestinationStore *destination_store,
gint n)
{
GtkTreePath *path;
path = gtk_tree_path_new ();
gtk_tree_path_append_index (path, n);
gtk_tree_model_row_deleted (GTK_TREE_MODEL (destination_store), path);
gtk_tree_path_free (path);
}
static void
row_inserted (EDestinationStore *destination_store,
gint n)
{
GtkTreePath *path;
GtkTreeIter iter;
path = gtk_tree_path_new ();
gtk_tree_path_append_index (path, n);
if (gtk_tree_model_get_iter (GTK_TREE_MODEL (destination_store), &iter, path))
gtk_tree_model_row_inserted (GTK_TREE_MODEL (destination_store), path, &iter);
gtk_tree_path_free (path);
}
static void
row_changed (EDestinationStore *destination_store,
gint n)
{
GtkTreePath *path;
GtkTreeIter iter;
path = gtk_tree_path_new ();
gtk_tree_path_append_index (path, n);
if (gtk_tree_model_get_iter (GTK_TREE_MODEL (destination_store), &iter, path))
gtk_tree_model_row_changed (GTK_TREE_MODEL (destination_store), path, &iter);
gtk_tree_path_free (path);
}
/* ------------------- *
* Destination helpers *
* ------------------- */
static gint
find_destination_by_pointer (EDestinationStore *destination_store,
EDestination *destination)
{
GPtrArray *array;
gint i;
array = destination_store->priv->destinations;
for (i = 0; i < array->len; i++) {
EDestination *destination_here;
destination_here = g_ptr_array_index (array, i);
if (destination_here == destination)
return i;
}
return -1;
}
static gint
find_destination_by_email (EDestinationStore *destination_store,
EDestination *destination)
{
GPtrArray *array;
gint i;
const gchar *e_mail = e_destination_get_email (destination);
array = destination_store->priv->destinations;
for (i = 0; i < array->len; i++) {
EDestination *destination_here;
const gchar *mail;
destination_here = g_ptr_array_index (array, i);
mail = e_destination_get_email (destination_here);
if (g_str_equal (e_mail, mail))
return i;
}
return -1;
}
static void
start_destination (EDestinationStore *destination_store,
EDestination *destination)
{
g_signal_connect_swapped (
destination, "changed",
G_CALLBACK (destination_changed), destination_store);
}
static void
stop_destination (EDestinationStore *destination_store,
EDestination *destination)
{
g_signal_handlers_disconnect_matched (
destination, G_SIGNAL_MATCH_DATA,
0, 0, NULL, NULL, destination_store);
}
/* --------------- *
* Signal handlers *
* --------------- */
static void
destination_changed (EDestinationStore *destination_store,
EDestination *destination)
{
gint n;
n = find_destination_by_pointer (destination_store, destination);
if (n < 0) {
g_warning ("EDestinationStore got change from unknown EDestination!");
return;
}
row_changed (destination_store, n);
}
/* --------------------- *
* EDestinationStore API *
* --------------------- */
/**
* e_destination_store_get_destination:
* @destination_store: an #EDestinationStore
* @iter: a #GtkTreeIter
*
* Gets the #EDestination from @destination_store at @iter.
*
* Returns: An #EDestination.
**/
EDestination *
e_destination_store_get_destination (EDestinationStore *destination_store,
GtkTreeIter *iter)
{
GPtrArray *array;
gint index;
g_return_val_if_fail (E_IS_DESTINATION_STORE (destination_store), NULL);
g_return_val_if_fail (ITER_IS_VALID (destination_store, iter), NULL);
array = destination_store->priv->destinations;
index = ITER_GET (iter);
return g_ptr_array_index (array, index);
}
/**
* e_destination_store_list_destinations:
* @destination_store: an #EDestinationStore
*
* Gets a list of all the #EDestinations in @destination_store.
*
* Returns: A #GList of pointers to #EDestination. The list is owned
* by the caller, but the #EDestination elements aren't.
**/
GList *
e_destination_store_list_destinations (EDestinationStore *destination_store)
{
GList *destination_list = NULL;
GPtrArray *array;
gint i;
g_return_val_if_fail (E_IS_DESTINATION_STORE (destination_store), NULL);
array = destination_store->priv->destinations;
for (i = 0; i < array->len; i++) {
EDestination *destination;
destination = g_ptr_array_index (array, i);
destination_list = g_list_prepend (destination_list, destination);
}
destination_list = g_list_reverse (destination_list);
return destination_list;
}
/**
* e_destination_store_insert_destination:
* @destination_store: an #EDestinationStore
* @index: the index at which to insert
* @destination: an #EDestination to insert
*
* Inserts @destination into @destination_store at the position
* indicated by @index. @destination_store will ref @destination.
**/
void
e_destination_store_insert_destination (EDestinationStore *destination_store,
gint index,
EDestination *destination)
{
GPtrArray *array;
g_return_if_fail (E_IS_DESTINATION_STORE (destination_store));
g_return_if_fail (index >= 0);
if (find_destination_by_pointer (destination_store, destination) >= 0) {
g_warning ("Same destination added more than once to EDestinationStore!");
return;
}
g_object_ref (destination);
array = destination_store->priv->destinations;
index = MIN (index, array->len);
g_ptr_array_set_size (array, array->len + 1);
if (array->len - 1 - index > 0) {
memmove (
array->pdata + index + 1,
array->pdata + index,
(array->len - 1 - index) * sizeof (gpointer));
}
array->pdata[index] = destination;
start_destination (destination_store, destination);
row_inserted (destination_store, index);
}
/**
* e_destination_store_append_destination:
* @destination_store: an #EDestinationStore
* @destination: an #EDestination
*
* Appends @destination to the list of destinations in @destination_store.
* @destination_store will ref @destination.
**/
void
e_destination_store_append_destination (EDestinationStore *destination_store,
EDestination *destination)
{
GPtrArray *array;
g_return_if_fail (E_IS_DESTINATION_STORE (destination_store));
if (find_destination_by_email (destination_store, destination) >= 0 && !e_destination_is_evolution_list (destination)) {
g_warning ("Same destination added more than once to EDestinationStore!");
return;
}
array = destination_store->priv->destinations;
g_object_ref (destination);
g_ptr_array_add (array, destination);
start_destination (destination_store, destination);
row_inserted (destination_store, array->len - 1);
}
/**
* e_destination_store_remove_destination:
* @destination_store: an #EDestinationStore
* @destination: an #EDestination to remove
*
* Removes @destination from @destination_store. @destination_store will
* unref @destination.
**/
void
e_destination_store_remove_destination (EDestinationStore *destination_store,
EDestination *destination)
{
GPtrArray *array;
gint n;
g_return_if_fail (E_IS_DESTINATION_STORE (destination_store));
n = find_destination_by_pointer (destination_store, destination);
if (n < 0) {
g_warning ("Tried to remove unknown destination from EDestinationStore!");
return;
}
stop_destination (destination_store, destination);
g_object_unref (destination);
array = destination_store->priv->destinations;
g_ptr_array_remove_index (array, n);
row_deleted (destination_store, n);
}
void
e_destination_store_remove_destination_nth (EDestinationStore *destination_store,
gint n)
{
EDestination *destination;
GPtrArray *array;
g_return_if_fail (n >= 0);
array = destination_store->priv->destinations;
destination = g_ptr_array_index (array, n);
stop_destination (destination_store, destination);
g_object_unref (destination);
g_ptr_array_remove_index (array, n);
row_deleted (destination_store, n);
}
guint
e_destination_store_get_destination_count (EDestinationStore *destination_store)
{
return destination_store->priv->destinations->len;
}
/* ---------------- *
* GtkTreeModel API *
* ---------------- */
static GtkTreeModelFlags
e_destination_store_get_flags (GtkTreeModel *tree_model)
{
g_return_val_if_fail (E_IS_DESTINATION_STORE (tree_model), 0);
return GTK_TREE_MODEL_LIST_ONLY;
}
static gint
e_destination_store_get_n_columns (GtkTreeModel *tree_model)
{
g_return_val_if_fail (E_IS_DESTINATION_STORE (tree_model), 0);
return E_CONTACT_FIELD_LAST;
}
static GType
e_destination_store_get_column_type (GtkTreeModel *tree_model,
gint index)
{
g_return_val_if_fail (E_IS_DESTINATION_STORE (tree_model), G_TYPE_INVALID);
g_return_val_if_fail (index >= 0 && index < E_DESTINATION_STORE_NUM_COLUMNS, G_TYPE_INVALID);
return column_types[index];
}
static gboolean
e_destination_store_get_iter (GtkTreeModel *tree_model,
GtkTreeIter *iter,
GtkTreePath *path)
{
EDestinationStore *destination_store;
GPtrArray *array;
gint index;
g_return_val_if_fail (E_IS_DESTINATION_STORE (tree_model), FALSE);
g_return_val_if_fail (gtk_tree_path_get_depth (path) > 0, FALSE);
destination_store = E_DESTINATION_STORE (tree_model);
index = gtk_tree_path_get_indices (path)[0];
array = destination_store->priv->destinations;
if (index >= array->len)
return FALSE;
ITER_SET (destination_store, iter, index);
return TRUE;
}
GtkTreePath *
e_destination_store_get_path (GtkTreeModel *tree_model,
GtkTreeIter *iter)
{
EDestinationStore *destination_store = E_DESTINATION_STORE (tree_model);
GtkTreePath *path;
gint index;
g_return_val_if_fail (E_IS_DESTINATION_STORE (tree_model), NULL);
g_return_val_if_fail (ITER_IS_VALID (destination_store, iter), NULL);
index = ITER_GET (iter);
path = gtk_tree_path_new ();
gtk_tree_path_append_index (path, index);
return path;
}
static gboolean
e_destination_store_iter_next (GtkTreeModel *tree_model,
GtkTreeIter *iter)
{
EDestinationStore *destination_store = E_DESTINATION_STORE (tree_model);
gint index;
g_return_val_if_fail (E_IS_DESTINATION_STORE (tree_model), FALSE);
g_return_val_if_fail (ITER_IS_VALID (destination_store, iter), FALSE);
index = ITER_GET (iter);
if (index + 1 < destination_store->priv->destinations->len) {
ITER_SET (destination_store, iter, index + 1);
return TRUE;
}
return FALSE;
}
static gboolean
e_destination_store_iter_children (GtkTreeModel *tree_model,
GtkTreeIter *iter,
GtkTreeIter *parent)
{
EDestinationStore *destination_store = E_DESTINATION_STORE (tree_model);
g_return_val_if_fail (E_IS_DESTINATION_STORE (tree_model), FALSE);
/* This is a list, nodes have no children. */
if (parent)
return FALSE;
/* But if parent == NULL we return the list itself as children of the root. */
if (destination_store->priv->destinations->len <= 0)
return FALSE;
ITER_SET (destination_store, iter, 0);
return TRUE;
}
static gboolean
e_destination_store_iter_has_child (GtkTreeModel *tree_model,
GtkTreeIter *iter)
{
g_return_val_if_fail (E_IS_DESTINATION_STORE (tree_model), FALSE);
if (iter == NULL)
return TRUE;
return FALSE;
}
static gint
e_destination_store_iter_n_children (GtkTreeModel *tree_model,
GtkTreeIter *iter)
{
EDestinationStore *destination_store = E_DESTINATION_STORE (tree_model);
g_return_val_if_fail (E_IS_DESTINATION_STORE (tree_model), -1);
if (iter == NULL)
return destination_store->priv->destinations->len;
g_return_val_if_fail (ITER_IS_VALID (destination_store, iter), -1);
return 0;
}
static gboolean
e_destination_store_iter_nth_child (GtkTreeModel *tree_model,
GtkTreeIter *iter,
GtkTreeIter *parent,
gint n)
{
EDestinationStore *destination_store = E_DESTINATION_STORE (tree_model);
g_return_val_if_fail (E_IS_DESTINATION_STORE (tree_model), FALSE);
if (parent)
return FALSE;
if (n < destination_store->priv->destinations->len) {
ITER_SET (destination_store, iter, n);
return TRUE;
}
return FALSE;
}
static gboolean
e_destination_store_iter_parent (GtkTreeModel *tree_model,
GtkTreeIter *iter,
GtkTreeIter *child)
{
return FALSE;
}
static void
e_destination_store_get_value (GtkTreeModel *tree_model,
GtkTreeIter *iter,
gint column,
GValue *value)
{
EDestinationStore *destination_store = E_DESTINATION_STORE (tree_model);
EDestination *destination;
GString *string_new;
EContact *contact;
GPtrArray *array;
const gchar *string;
gint row;
g_return_if_fail (E_IS_DESTINATION_STORE (tree_model));
g_return_if_fail (column < E_DESTINATION_STORE_NUM_COLUMNS);
g_return_if_fail (ITER_IS_VALID (destination_store, iter));
g_value_init (value, column_types[column]);
array = destination_store->priv->destinations;
row = ITER_GET (iter);
if (row >= array->len)
return;
destination = g_ptr_array_index (array, row);
g_assert (destination);
switch (column) {
case E_DESTINATION_STORE_COLUMN_NAME:
string = e_destination_get_name (destination);
g_value_set_string (value, string);
break;
case E_DESTINATION_STORE_COLUMN_EMAIL:
string = e_destination_get_email (destination);
g_value_set_string (value, string);
break;
case E_DESTINATION_STORE_COLUMN_ADDRESS:
contact = e_destination_get_contact (destination);
if (contact && E_IS_CONTACT (contact)) {
if (e_contact_get (contact, E_CONTACT_IS_LIST)) {
string = e_destination_get_name (destination);
string_new = g_string_new (string);
string_new = g_string_append (string_new, " mailing list");
g_value_set_string (value, string_new->str);
g_string_free (string_new, TRUE);
}
else {
string = e_destination_get_address (destination);
g_value_set_string (value, string);
}
}
else {
string = e_destination_get_address (destination);
g_value_set_string (value, string);
}
break;
default:
g_assert_not_reached ();
break;
}
}
/**
* e_destination_store_get_stamp:
* @destination_store: an #EDestinationStore
*
* Since: 2.32
**/
gint
e_destination_store_get_stamp (EDestinationStore *destination_store)
{
g_return_val_if_fail (E_IS_DESTINATION_STORE (destination_store), 0);
return destination_store->priv->stamp;
}
|