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
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
*
* Author:
* Miguel de Icaza (miguel@gnu.org)
*
* (C) 1999 Helix Code, Inc.
*/
#include <config.h>
#include "e-test-model.h"
#include <gnome-xml/tree.h>
#include <gnome-xml/parser.h>
#include <gnome-xml/xmlmemory.h>
#include <gnome.h>
#define PARENT_TYPE e_table_model_get_type()
/*
* ETestModel callbacks
n * These are the callbacks that define the behavior of our custom model.
*/
static void
test_destroy(GtkObject *object)
{
ETestModel *model = E_TEST_MODEL(object);
int i;
if (model->book)
gtk_object_unref(GTK_OBJECT(model->book));
if (model->book_view)
gtk_object_unref(GTK_OBJECT(model->book_view));
for ( i = 0; i < model->data_count; i++ ) {
gtk_object_unref(GTK_OBJECT(model->data[i]));
}
g_free(model->data);
g_free(model->uri);
}
/* This function returns the number of columns in our ETableModel. */
static int
test_col_count (ETableModel *etc)
{
return LAST_COL;
}
/* This function returns the number of rows in our ETableModel. */
static int
test_row_count (ETableModel *etc)
{
ETestModel *test = E_TEST_MODEL(etc);
return test->data_count;
}
/* This function returns the value at a particular point in our ETableModel. */
static void *
test_value_at (ETableModel *etc, int col, int row)
{
ETestModel *test = E_TEST_MODEL(etc);
ECardList *list;
ECardIterator *iterator;
gchar *string;
if ( col >= LAST_COL || row >= test->data_count )
return NULL;
switch (col) {
case EMAIL:
gtk_object_get(GTK_OBJECT(test->data[row]),
"email", &list,
NULL);
iterator = e_card_list_get_iterator(list);
if (e_card_iterator_get(iterator))
return (void *) e_card_iterator_get(iterator);
else
return "";
gtk_object_unref(GTK_OBJECT(iterator));
break;
case FULL_NAME:
gtk_object_get(GTK_OBJECT(test->data[row]),
"full_name", &string,
NULL);
if (string)
return string;
else
return "";
break;
case STREET:
gtk_object_get(GTK_OBJECT(test->data[row]),
"street", &list,
NULL);
iterator = e_card_list_get_iterator(list);
if (e_card_iterator_get(iterator))
return ((ECardDeliveryAddress *)e_card_iterator_get(iterator))->street;
else
return "";
gtk_object_unref(GTK_OBJECT(iterator));
break;
case PHONE:
gtk_object_get(GTK_OBJECT(test->data[row]),
"phone", &list,
NULL);
iterator = e_card_list_get_iterator(list);
if (e_card_iterator_get(iterator))
return ((ECardPhone *)e_card_iterator_get(iterator))->number;
else
return "";
gtk_object_unref(GTK_OBJECT(iterator));
break;
default:
return NULL;
}
}
/* This function sets the value at a particular point in our ETableModel. */
static void
test_set_value_at (ETableModel *etc, int col, int row, const void *val)
{
ETestModel *test = E_TEST_MODEL(etc);
ECardList *list;
ECardIterator *iterator;
if ( col >= LAST_COL || row >= test->data_count )
return;
switch (col) {
case EMAIL:
gtk_object_get(GTK_OBJECT(test->data[row]),
"email", &list,
NULL);
iterator = e_card_list_get_iterator(list);
if (e_card_iterator_is_valid(iterator)) {
e_card_iterator_set(iterator, val);
} else {
e_card_list_append(list, val);
}
gtk_object_unref(GTK_OBJECT(iterator));
break;
case FULL_NAME:
gtk_object_set(GTK_OBJECT(test->data[row]),
"full_name", val,
NULL);
break;
case STREET:
gtk_object_get(GTK_OBJECT(test->data[row]),
"address", &list,
NULL);
iterator = e_card_list_get_iterator(list);
if (e_card_iterator_is_valid(iterator)) {
const ECardDeliveryAddress *address = e_card_iterator_get(iterator);
ECardDeliveryAddress *address_copy = e_card_delivery_address_copy(address);
g_free(address_copy->street);
address_copy->street = g_strdup(val);
e_card_iterator_set(iterator, address_copy);
e_card_delivery_address_free(address_copy);
} else {
ECardDeliveryAddress *address = g_new(ECardDeliveryAddress, 1);
address->po = NULL;
address->ext = NULL;
address->street = g_strdup(val);
address->city = NULL;
address->region = NULL;
address->code = NULL;
address->country = NULL;
address->flags = 0;
e_card_list_append(list, address);
e_card_delivery_address_free(address);
}
gtk_object_unref(GTK_OBJECT(iterator));
break;
case PHONE:
gtk_object_get(GTK_OBJECT(test->data[row]),
"phone", &list,
NULL);
iterator = e_card_list_get_iterator(list);
if (e_card_iterator_is_valid(iterator)) {
const ECardPhone *phone = e_card_iterator_get(iterator);
ECardPhone *phone_copy = e_card_phone_copy(phone);
g_free(phone_copy->number);
phone_copy->number = g_strdup(val);
e_card_iterator_set(iterator, phone_copy);
e_card_phone_free(phone_copy);
} else {
ECardPhone *phone = g_new(ECardPhone, 1);
phone->number = g_strdup(val);
phone->flags = 0;
e_card_list_append(list, phone);
e_card_phone_free(phone);
}
gtk_object_unref(GTK_OBJECT(iterator));
break;
default:
return;
}
e_book_commit_card(test->book, test->data[row], NULL, NULL);
if ( !etc->frozen )
e_table_model_cell_changed(etc, col, row);
}
/* This function returns whether a particular cell is editable. */
static gboolean
test_is_cell_editable (ETableModel *etc, int col, int row)
{
return TRUE;
}
/* This function duplicates the value passed to it. */
static void *
test_duplicate_value (ETableModel *etc, int col, const void *value)
{
return g_strdup(value);
}
/* This function frees the value passed to it. */
static void
test_free_value (ETableModel *etc, int col, void *value)
{
g_free(value);
}
/* This function is for when the model is unfrozen. This can mostly
be ignored for simple models. */
static void
test_thaw (ETableModel *etc)
{
e_table_model_changed(etc);
}
static void
e_test_model_class_init (GtkObjectClass *object_class)
{
ETableModelClass *model_class = (ETableModelClass *) object_class;
object_class->destroy = test_destroy;
model_class->column_count = test_col_count;
model_class->row_count = test_row_count;
model_class->value_at = test_value_at;
model_class->set_value_at = test_set_value_at;
model_class->is_cell_editable = test_is_cell_editable;
model_class->duplicate_value = test_duplicate_value;
model_class->free_value = test_free_value;
model_class->thaw = test_thaw;
}
static void
e_test_model_init (GtkObject *object)
{
ETestModel *model = E_TEST_MODEL(object);
model->data = NULL;
model->data_count = 0;
model->book = NULL;
model->book_view = NULL;
}
GtkType
e_test_model_get_type (void)
{
static GtkType type = 0;
if (!type){
GtkTypeInfo info = {
"ETestModel",
sizeof (ETestModel),
sizeof (ETestModelClass),
(GtkClassInitFunc) e_test_model_class_init,
(GtkObjectInitFunc) e_test_model_init,
NULL, /* reserved 1 */
NULL, /* reserved 2 */
(GtkClassInitFunc) NULL
};
type = gtk_type_unique (PARENT_TYPE, &info);
}
return type;
}
void
e_test_model_add_column (ETestModel *model, Address *newadd)
{
#if 0
model->data = g_realloc(model->data, (++model->data_count) * sizeof(Address *));
model->data[model->data_count - 1] = newadd;
e_test_model_queue_save(model);
if ( model && !E_TABLE_MODEL(model)->frozen )
e_table_model_changed(E_TABLE_MODEL(model));
#endif
}
static void
e_test_model_card_added(EBookView *book_view,
const GList *cards,
ETestModel *model)
{
model->data = g_realloc(model->data, (model->data_count + g_list_length((GList *)cards)) * sizeof(ECard *));
for ( ; cards; cards = cards->next) {
gtk_object_ref(GTK_OBJECT(cards->data));
model->data[model->data_count++] = E_CARD (cards->data);
}
e_table_model_changed(E_TABLE_MODEL(model));
}
static void
e_test_model_card_removed(EBookView *book_view,
const char *id,
ETestModel *model)
{
int i;
for ( i = 0; i < model->data_count; i++) {
if ( !strcmp(e_card_get_id(model->data[i]), id) ) {
gtk_object_unref(GTK_OBJECT(model->data[i]));
memmove(model->data + i, model->data + i + 1, (model->data_count - i - 1) * sizeof (ECard *));
}
}
e_table_model_changed(E_TABLE_MODEL(model));
}
static void
e_test_model_card_changed(EBookView *book_view,
const GList *cards,
ETestModel *model)
{
for ( ; cards; cards = cards->next) {
int i;
for ( i = 0; i < model->data_count; i++) {
if ( !strcmp(e_card_get_id(model->data[i]), e_card_get_id(E_CARD(cards->data))) ) {
gtk_object_unref(GTK_OBJECT(model->data[i]));
model->data[i] = E_CARD(cards->data);
gtk_object_ref(GTK_OBJECT(model->data[i]));
e_table_model_row_changed(E_TABLE_MODEL(model), i);
break;
}
}
}
}
static void
e_test_model_book_respond_get_view(EBook *book,
EBookStatus status,
EBookView *book_view,
ETestModel *model)
{
if (status == E_BOOK_STATUS_SUCCESS) {
model->book_view = book_view;
gtk_object_ref(GTK_OBJECT(book_view));
gtk_signal_connect(GTK_OBJECT(book_view),
"card_changed",
GTK_SIGNAL_FUNC(e_test_model_card_changed),
model);
gtk_signal_connect(GTK_OBJECT(book_view),
"card_removed",
GTK_SIGNAL_FUNC(e_test_model_card_removed),
model);
gtk_signal_connect(GTK_OBJECT(book_view),
"card_added",
GTK_SIGNAL_FUNC(e_test_model_card_added),
model);
}
}
static void
e_test_model_uri_loaded(EBook *book,
EBookStatus status,
ETestModel *model)
{
if (status == E_BOOK_STATUS_SUCCESS) {
e_book_get_book_view (book,
"",
(EBookBookViewCallback) e_test_model_book_respond_get_view,
model);
}
}
ETableModel *
e_test_model_new (gchar *uri)
{
ETestModel *et;
et = gtk_type_new (e_test_model_get_type ());
et->uri = g_strdup(uri);
et->book = e_book_new();
e_book_load_uri(et->book,
et->uri,
(EBookCallback) e_test_model_uri_loaded,
et);
return E_TABLE_MODEL(et);
}
|