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
|
Index: addressbook/libedata-book-dbus/e-data-book.c
===================================================================
--- addressbook/libedata-book-dbus/e-data-book.c (revision 622)
+++ addressbook/libedata-book-dbus/e-data-book.c (working copy)
@@ -55,8 +55,7 @@
static void return_status_and_list (guint32 opid, EDataBookStatus status, GList *list, gboolean free_data);
-enum
-{
+enum {
WRITABLE,
CONNECTION,
AUTH_REQUIRED,
@@ -65,6 +64,121 @@
static guint signals[LAST_SIGNAL] = { 0 };
+static GThreadPool *op_pool = NULL;
+
+typedef enum {
+ OP_OPEN,
+ OP_AUTHENTICATE,
+ OP_ADD_CONTACT,
+ OP_GET_CONTACT,
+ OP_GET_CONTACTS,
+ OP_MODIFY_CONTACT,
+ OP_MODIFY_CONTACTS,
+ OP_REMOVE_CONTACTS,
+ OP_GET_CHANGES,
+} OperationID;
+
+typedef struct {
+ OperationID op;
+ guint32 id; /* operation id */
+ EDataBook *book; /* book */
+ union {
+ /* OP_OPEN */
+ gboolean only_if_exists;
+ /* OP_AUTHENTICATE */
+ struct {
+ char *username;
+ char *password;
+ char *method;
+ } auth;
+ /* OP_ADD_CONTACT */
+ /* OP_MODIFY_CONTACT */
+ char *vcard;
+ /* OP_GET_CONTACT */
+ char *uid;
+ /* OP_GET_CONTACTS */
+ char *query;
+ /* OP_MODIFY_CONTACT */
+ char **vcards;
+ /* OP_REMOVE_CONTACTS */
+ GList *ids;
+ /* OP_GET_CHANGES */
+ char *change_id;
+ };
+} OperationData;
+
+
+static void
+operation_thread (gpointer data, gpointer user_data)
+{
+ OperationData *op = data;
+ EBookBackend *backend;
+
+ backend = e_data_book_get_backend (op->book);
+
+ switch (op->op) {
+ case OP_OPEN:
+ e_book_backend_open (backend, op->book, op->id, op->only_if_exists);
+ break;
+ case OP_AUTHENTICATE:
+ e_book_backend_authenticate_user (backend, op->book, op->id,
+ op->auth.username,
+ op->auth.password,
+ op->auth.method);
+ g_free (op->auth.username);
+ g_free (op->auth.password);
+ g_free (op->auth.method);
+ break;
+ case OP_ADD_CONTACT:
+ e_book_backend_create_contact (backend, op->book, op->id, op->vcard);
+ g_free (op->vcard);
+ break;
+ case OP_GET_CONTACT:
+ e_book_backend_get_contact (backend, op->book, op->id, op->uid);
+ g_free (op->uid);
+ break;
+ case OP_GET_CONTACTS:
+ e_book_backend_get_contact_list (backend, op->book, op->id, op->query);
+ g_free (op->query);
+ break;
+ case OP_MODIFY_CONTACT:
+ e_book_backend_modify_contact (backend, op->book, op->id, op->vcard);
+ g_free (op->vcard);
+ break;
+ case OP_MODIFY_CONTACTS:
+ /* C is weird at times, need to cast to const char** */
+ e_book_backend_modify_contacts (backend, op->book, op->id, (const char**)op->vcards);
+ g_strfreev (op->vcards);
+ break;
+ case OP_REMOVE_CONTACTS:
+ e_book_backend_remove_contacts (backend, op->book, op->id, op->ids);
+ g_list_foreach (op->ids, (GFunc)g_free, NULL);
+ g_list_free (op->ids);
+ break;
+ case OP_GET_CHANGES:
+ e_book_backend_get_changes (backend, op->book, op->id, op->change_id);
+ g_free (op->change_id);
+ break;
+ }
+
+ g_object_unref (op->book);
+ g_slice_free (OperationData, op);
+}
+
+static OperationData *
+op_new (OperationID op, EDataBook *book, DBusGMethodInvocation *context)
+{
+ OperationData *data;
+
+ data = g_slice_new0 (OperationData);
+ data->op = op;
+ data->book = g_object_ref (book);
+ data->id = opid_store (context);
+
+ return data;
+}
+
+
/* Create the EDataBook error quark */
GQuark
e_data_book_error_quark (void)
@@ -134,6 +248,10 @@
dbus_g_object_type_install_info (G_TYPE_FROM_CLASS (e_data_book_class), &dbus_glib_e_data_book_object_info);
dbus_g_error_domain_register (E_DATA_BOOK_ERROR, NULL, E_TYPE_DATA_BOOK_STATUS);
+
+ op_pool = g_thread_pool_new (operation_thread, NULL, 10, FALSE, NULL);
+ /* Kill threads which don't do anything for 10 seconds */
+ g_thread_pool_set_max_idle_time (10 * 1000);
}
/* Instance init */
@@ -172,7 +290,11 @@
static void
impl_AddressBook_Book_open(EDataBook *book, gboolean only_if_exists, DBusGMethodInvocation *context)
{
- e_book_backend_open (book->backend, book, opid_store (context), only_if_exists);
+ OperationData *op;
+
+ op = op_new (OP_OPEN, book, context);
+ op->only_if_exists = only_if_exists;
+ g_thread_pool_push (op_pool, op, NULL);
}
void
@@ -208,12 +330,16 @@
static void
impl_AddressBook_Book_getContact (EDataBook *book, const char *IN_uid, DBusGMethodInvocation *context)
{
+ OperationData *op;
+
if (IN_uid == NULL) {
dbus_g_method_return_error (context, g_error_new (E_DATA_BOOK_ERROR, ContactNotFound, _("Cannot get contact")));
return;
}
- e_book_backend_get_contact (book->backend, book, opid_store (context), IN_uid);
+ op = op_new (OP_GET_CONTACT, book, context);
+ op->uid = g_strdup (IN_uid);
+ g_thread_pool_push (op_pool, op, NULL);
}
void
@@ -229,14 +355,18 @@
}
static void
-impl_AddressBook_Book_getContactList(EDataBook *book, const char *query, DBusGMethodInvocation *context)
+impl_AddressBook_Book_getContactList (EDataBook *book, const char *query, DBusGMethodInvocation *context)
{
- if (query == NULL) {
+ OperationData *op;
+
+ if (query == NULL || query[0] == '\0') {
dbus_g_method_return_error (context, g_error_new (E_DATA_BOOK_ERROR, InvalidQuery, _("Empty query")));
return;
}
- e_book_backend_get_contact_list (book->backend, book, opid_store (context), query);
+ op = op_new (OP_GET_CONTACTS, book, context);
+ op->query = g_strdup (query);
+ g_thread_pool_push (op_pool, op, NULL);
}
void
@@ -248,8 +378,13 @@
static void
impl_AddressBook_Book_authenticateUser(EDataBook *book, const char *IN_user, const char *IN_passwd, const char *IN_auth_method, DBusGMethodInvocation *context)
{
- e_book_backend_authenticate_user (e_data_book_get_backend (book), book,
- opid_store (context), IN_user, IN_passwd, IN_auth_method);
+ OperationData *op;
+
+ op = op_new (OP_AUTHENTICATE, book, context);
+ op->auth.username = g_strdup (IN_user);
+ op->auth.password = g_strdup (IN_passwd);
+ op->auth.method = g_strdup (IN_auth_method);
+ g_thread_pool_push (op_pool, op, NULL);
}
void
@@ -267,13 +402,16 @@
static void
impl_AddressBook_Book_addContact (EDataBook *book, const char *IN_vcard, DBusGMethodInvocation *context)
{
- if (IN_vcard == NULL) {
+ OperationData *op;
+
+ if (IN_vcard == NULL || IN_vcard[0] == '\0') {
dbus_g_method_return_error (context, g_error_new (E_DATA_BOOK_ERROR, InvalidQuery, _("Cannot add contact")));
return;
}
- e_book_backend_create_contact (e_data_book_get_backend (book), book,
- opid_store (context), IN_vcard);
+ op = op_new (OP_ADD_CONTACT, book, context);
+ op->vcard = g_strdup (IN_vcard);
+ g_thread_pool_push (op_pool, op, NULL);
}
void
@@ -294,13 +432,16 @@
static void
impl_AddressBook_Book_modifyContact (EDataBook *book, const char *IN_vcard, DBusGMethodInvocation *context)
{
+ OperationData *op;
+
if (IN_vcard == NULL) {
dbus_g_method_return_error (context, g_error_new (E_DATA_BOOK_ERROR, InvalidQuery, _("Cannot modify contact")));
return;
}
- e_book_backend_modify_contact (e_data_book_get_backend (book), book,
- opid_store (context), IN_vcard);
+ op = op_new (OP_MODIFY_CONTACT, book, context);
+ op->vcard = g_strdup (IN_vcard);
+ g_thread_pool_push (op_pool, op, NULL);
}
void
@@ -321,13 +462,16 @@
static void
impl_AddressBook_Book_modifyContacts(EDataBook *book, const char **IN_vcards, DBusGMethodInvocation *context)
{
- if (IN_vcards == NULL) {
- dbus_g_method_return_error (context, g_error_new (E_DATA_BOOK_ERROR, InvalidQuery, _("Cannot modify contact")));
+ OperationData *op;
+
+ if (IN_vcards == NULL || IN_vcards[0] == NULL) {
+ dbus_g_method_return_error (context, g_error_new (E_DATA_BOOK_ERROR, InvalidQuery, _("Cannot modify contacts")));
return;
}
- e_book_backend_modify_contacts (e_data_book_get_backend (book), book,
- opid_store (context), IN_vcards);
+ op = op_new (OP_MODIFY_CONTACTS, book, context);
+ op->vcards = g_strdupv ((char**)IN_vcards);
+ g_thread_pool_push (op_pool, op, NULL);
}
void
@@ -350,8 +494,7 @@
static void
impl_AddressBook_Book_removeContacts(EDataBook *book, const char **IN_uids, DBusGMethodInvocation *context)
{
- GList *id_list = NULL;
- int i = 0;
+ OperationData *op;
/* Allow an empty array to be removed */
if (IN_uids == NULL) {
@@ -359,14 +502,13 @@
return;
}
- while (IN_uids[i] != NULL) {
- id_list = g_list_prepend (id_list, (gpointer) IN_uids[i]);
- i++;
+ op = op_new (OP_REMOVE_CONTACTS, book, context);
+
+ for (; *IN_uids; IN_uids++) {
+ op->ids = g_list_prepend (op->ids, g_strdup (*IN_uids));
}
- e_book_backend_remove_contacts (e_data_book_get_backend (book), book,
- opid_store (context), id_list);
- g_list_free (id_list);
+ g_thread_pool_push (op_pool, op, NULL);
}
void
@@ -466,7 +608,11 @@
static void
impl_AddressBook_Book_getChanges(EDataBook *book, const char *IN_change_id, DBusGMethodInvocation *context)
{
- e_book_backend_get_changes (e_data_book_get_backend (book), book, opid_store (context), IN_change_id);
+ OperationData *op;
+
+ op = op_new (OP_GET_CHANGES, book, context);
+ op->change_id = g_strdup (IN_change_id);
+ g_thread_pool_push (op_pool, op, NULL);
}
void
|