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
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
* Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
* Copyright (C) 2006 OpenedHand Ltd
* Copyright (C) 2009 Intel Corporation
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of version 2.1 of the GNU Lesser General Public License as
* published by the Free Software Foundation.
*
* This library 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 library; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* Author: Ross Burton <ross@linux.intel.com>
*/
#include <config.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <glib-object.h>
#include <glib/gi18n.h>
#include <dbus/dbus-protocol.h>
#include <dbus/dbus-glib.h>
#include <dbus/dbus-glib-lowlevel.h>
#include <dbus/dbus-glib-bindings.h>
#include <libebackend/e-data-server-module.h>
#include <libebackend/e-offline-listener.h>
#include "e-book-backend-factory.h"
#include "e-data-book-factory.h"
#include "e-data-book.h"
#include "e-book-backend.h"
#include "e-book-backend-factory.h"
#ifdef G_OS_WIN32
#include <windows.h>
#include <conio.h>
#ifndef PROCESS_DEP_ENABLE
#define PROCESS_DEP_ENABLE 0x00000001
#endif
#ifndef PROCESS_DEP_DISABLE_ATL_THUNK_EMULATION
#define PROCESS_DEP_DISABLE_ATL_THUNK_EMULATION 0x00000002
#endif
#endif
#define d(x)
static void impl_BookFactory_getBook(EDataBookFactory *factory, const gchar *IN_uri, DBusGMethodInvocation *context);
#include "e-data-book-factory-glue.h"
static GMainLoop *loop;
static EDataBookFactory *factory;
extern DBusGConnection *connection;
/* Convenience macro to test and set a GError/return on failure */
#define g_set_error_val_if_fail(test, returnval, error, domain, code) G_STMT_START{ \
if G_LIKELY (test) {} else { \
g_set_error (error, domain, code, #test); \
g_warning(#test " failed"); \
return (returnval); \
} \
}G_STMT_END
G_DEFINE_TYPE(EDataBookFactory, e_data_book_factory, G_TYPE_OBJECT);
#define E_DATA_BOOK_FACTORY_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), E_TYPE_DATA_BOOK_FACTORY, EDataBookFactoryPrivate))
struct _EDataBookFactoryPrivate {
/* TODO: as the factory is not threaded these locks could be removed */
GMutex *backends_lock;
GHashTable *backends;
GMutex *books_lock;
/* A hash of object paths for book URIs to EDataBooks */
GHashTable *books;
GMutex *connections_lock;
/* This is a hash of client addresses to GList* of EDataBooks */
GHashTable *connections;
guint exit_timeout;
gint mode;
};
/* Create the EDataBookFactory error quark */
GQuark
e_data_book_factory_error_quark (void)
{
static GQuark quark = 0;
if (!quark)
quark = g_quark_from_static_string ("e_data_book_factory_error");
return quark;
}
/**
* e_data_book_factory_register_backend:
* @factory: an #EDataBookFactory
* @backend_factory: an #EBookBackendFactory
*
* Registers @backend_factory with @factory.
**/
static void
e_data_book_factory_register_backend (EDataBookFactory *book_factory,
EBookBackendFactory *backend_factory)
{
const gchar *proto;
g_return_if_fail (E_IS_DATA_BOOK_FACTORY (book_factory));
g_return_if_fail (E_IS_BOOK_BACKEND_FACTORY (backend_factory));
proto = E_BOOK_BACKEND_FACTORY_GET_CLASS (backend_factory)->get_protocol (backend_factory);
if (g_hash_table_lookup (book_factory->priv->backends, proto) != NULL) {
g_warning ("e_data_book_factory_register_backend: "
"Proto \"%s\" already registered!\n", proto);
}
g_hash_table_insert (book_factory->priv->backends,
g_strdup (proto), backend_factory);
}
/**
* e_data_book_factory_register_backends:
* @book_factory: an #EDataBookFactory
*
* Register the backends supported by the Evolution Data Server,
* with @book_factory.
**/
static void
e_data_book_factory_register_backends (EDataBookFactory *book_factory)
{
GList *factories, *f;
factories = e_data_server_get_extensions_for_type (E_TYPE_BOOK_BACKEND_FACTORY);
for (f = factories; f; f = f->next) {
EBookBackendFactory *backend_factory = f->data;
e_data_book_factory_register_backend (book_factory, g_object_ref (backend_factory));
}
e_data_server_extension_list_free (factories);
e_data_server_module_remove_unused ();
}
static void
set_backend_online_status (gpointer key, gpointer value, gpointer data)
{
GList *books;
EBookBackend *backend = NULL;
for (books = (GList *) value; books; books = g_list_next (books)) {
backend = E_BOOK_BACKEND (books->data);
e_book_backend_set_mode (backend, GPOINTER_TO_INT (data));
}
}
/**
* e_data_book_factory_set_backend_mode:
* @factory: A bookendar factory.
* @mode: Online mode to set.
*
* Sets the online mode for all backends created by the given factory.
*/
void
e_data_book_factory_set_backend_mode (EDataBookFactory *factory, gint mode)
{
EDataBookFactoryPrivate *priv = factory->priv;
priv->mode = mode;
g_mutex_lock (priv->connections_lock);
g_hash_table_foreach (priv->connections, set_backend_online_status, GINT_TO_POINTER (priv->mode));
g_mutex_unlock (priv->connections_lock);
}
static void
e_data_book_factory_class_init (EDataBookFactoryClass *e_data_book_factory_class)
{
g_type_class_add_private (e_data_book_factory_class, sizeof (EDataBookFactoryPrivate));
dbus_g_object_type_install_info (G_TYPE_FROM_CLASS (e_data_book_factory_class), &dbus_glib_e_data_book_factory_object_info);
}
/* Instance init */
static void
e_data_book_factory_init (EDataBookFactory *factory)
{
factory->priv = E_DATA_BOOK_FACTORY_GET_PRIVATE (factory);
factory->priv->backends_lock = g_mutex_new ();
factory->priv->backends = g_hash_table_new (g_str_hash, g_str_equal);
factory->priv->books_lock = g_mutex_new ();
factory->priv->books = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
factory->priv->connections_lock = g_mutex_new ();
factory->priv->connections = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
e_data_server_module_init ();
e_data_book_factory_register_backends (factory);
}
/* TODO: write dispose to kill hash */
static gchar *
e_data_book_factory_extract_proto_from_uri (const gchar *uri)
{
gchar *proto, *p;
p = strchr (uri, ':');
if (p == NULL)
return NULL;
proto = g_malloc0 (p - uri + 1);
strncpy (proto, uri, p - uri);
return proto;
}
static EBookBackendFactory*
e_data_book_factory_lookup_backend_factory (EDataBookFactory *factory,
const gchar *uri)
{
EBookBackendFactory *backend_factory;
gchar *proto;
g_return_val_if_fail (E_IS_DATA_BOOK_FACTORY (factory), NULL);
g_return_val_if_fail (uri != NULL, NULL);
proto = e_data_book_factory_extract_proto_from_uri (uri);
if (proto == NULL) {
g_warning ("Cannot extract protocol from URI %s", uri);
return NULL;
}
backend_factory = g_hash_table_lookup (factory->priv->backends, proto);
g_free (proto);
return backend_factory;
}
static gchar *
construct_book_factory_path (void)
{
static volatile gint counter = 1;
return g_strdup_printf (
"/org/gnome/evolution/dataserver/addressbook/%d/%u",
getpid (),
g_atomic_int_exchange_and_add (&counter, 1));
}
static void
my_remove (gchar *key, GObject *dead)
{
EDataBookFactoryPrivate *priv = factory->priv;
GHashTableIter iter;
gpointer hkey, hvalue;
d (g_debug ("%s (%p) is dead", key, dead));
g_hash_table_remove (priv->books, key);
g_hash_table_iter_init (&iter, priv->connections);
while (g_hash_table_iter_next (&iter, &hkey, &hvalue)) {
GList *books = hvalue;
if (g_list_find (books, dead)) {
books = g_list_remove (books, dead);
if (books) {
g_hash_table_insert (priv->connections, g_strdup (hkey), books);
} else {
g_hash_table_remove (priv->connections, hkey);
}
break;
}
}
g_free (key);
/* If there are no open books, start a timer to quit */
if (priv->exit_timeout == 0 && g_hash_table_size (priv->books) == 0) {
priv->exit_timeout = g_timeout_add (10000, (GSourceFunc)g_main_loop_quit, loop);
}
}
static void
impl_BookFactory_getBook(EDataBookFactory *factory, const gchar *IN_source, DBusGMethodInvocation *context)
{
EDataBook *book;
EBookBackend *backend;
EDataBookFactoryPrivate *priv = factory->priv;
ESource *source;
gchar *uri, *path, *sender;
GList *list;
if (IN_source == NULL || IN_source[0] == '\0') {
dbus_g_method_return_error (context, g_error_new (E_DATA_BOOK_ERROR, E_DATA_BOOK_STATUS_NO_SUCH_BOOK, _("Empty URI")));
return;
}
/* Remove a pending exit */
if (priv->exit_timeout) {
g_source_remove (priv->exit_timeout);
priv->exit_timeout = 0;
}
g_mutex_lock (priv->backends_lock);
source = e_source_new_from_standalone_xml (IN_source);
if (!source) {
g_mutex_unlock (priv->backends_lock);
dbus_g_method_return_error (context, g_error_new (E_DATA_BOOK_ERROR, E_DATA_BOOK_STATUS_NO_SUCH_BOOK, _("Invalid source")));
return;
}
uri = e_source_get_uri (source);
g_mutex_lock (priv->books_lock);
backend = g_hash_table_lookup (priv->backends, uri);
if (!backend) {
EBookBackendFactory *backend_factory = e_data_book_factory_lookup_backend_factory (factory, uri);
if (backend_factory)
backend = e_book_backend_factory_new_backend (backend_factory);
if (backend) {
g_hash_table_insert (priv->backends, g_strdup (uri), backend);
e_book_backend_set_mode (backend, priv->mode);
}
}
if (!backend) {
g_free (uri);
g_object_unref (source);
g_mutex_unlock (priv->books_lock);
g_mutex_unlock (priv->backends_lock);
dbus_g_method_return_error (context, g_error_new (E_DATA_BOOK_ERROR, E_DATA_BOOK_STATUS_NO_SUCH_BOOK, _("Invalid source")));
return;
}
path = construct_book_factory_path ();
book = e_data_book_new (backend, source);
g_hash_table_insert (priv->books, g_strdup (path), book);
e_book_backend_add_client (backend, book);
dbus_g_connection_register_g_object (connection, path, G_OBJECT (book));
g_object_weak_ref (G_OBJECT (book), (GWeakNotify)my_remove, g_strdup (path));
/* Update the hash of open connections */
g_mutex_lock (priv->connections_lock);
sender = dbus_g_method_get_sender (context);
list = g_hash_table_lookup (priv->connections, sender);
list = g_list_prepend (list, book);
g_hash_table_insert (priv->connections, sender, list);
g_mutex_unlock (priv->connections_lock);
g_mutex_unlock (priv->books_lock);
g_mutex_unlock (priv->backends_lock);
g_object_unref (source);
g_free (uri);
dbus_g_method_return (context, path);
}
static void
remove_data_book_cb (gpointer data_bk, gpointer user_data)
{
EDataBook *data_book;
data_book = E_DATA_BOOK (data_bk);
g_return_if_fail (data_book != NULL);
e_book_backend_remove_client (e_data_book_get_backend (data_book), data_book);
g_object_unref (data_book);
}
static void
name_owner_changed (DBusGProxy *proxy,
const gchar *name,
const gchar *prev_owner,
const gchar *new_owner,
EDataBookFactory *factory)
{
if (strcmp (new_owner, "") == 0 && strcmp (name, prev_owner) == 0) {
gchar *key;
GList *list = NULL;
g_mutex_lock (factory->priv->connections_lock);
while (g_hash_table_lookup_extended (factory->priv->connections, prev_owner, (gpointer)&key, (gpointer)&list)) {
GList *copy = g_list_copy (list);
/* this should trigger the book's weak ref notify
* function, which will remove it from the list before
* it's freed, and will remove the connection from
* priv->connections once they're all gone */
g_list_foreach (copy, remove_data_book_cb, NULL);
g_list_free (copy);
}
g_mutex_unlock (factory->priv->connections_lock);
}
}
/* Convenience function to print an error and exit */
G_GNUC_NORETURN static void
die (const gchar *prefix, GError *error)
{
g_error("%s: %s", prefix, error->message);
g_error_free (error);
exit(1);
}
static void
offline_state_changed_cb (EOfflineListener *eol, EDataBookFactory *factory)
{
EOfflineListenerState state = e_offline_listener_get_state (eol);
g_return_if_fail (state == EOL_STATE_ONLINE || state == EOL_STATE_OFFLINE);
e_data_book_factory_set_backend_mode (factory, state == EOL_STATE_ONLINE ? E_DATA_BOOK_MODE_REMOTE : E_DATA_BOOK_MODE_LOCAL);
}
#define E_DATA_BOOK_FACTORY_SERVICE_NAME "org.gnome.evolution.dataserver.AddressBook"
gint
main (gint argc, gchar **argv)
{
GError *error = NULL;
DBusGProxy *bus_proxy;
guint32 request_name_ret;
EOfflineListener *eol;
#ifdef G_OS_WIN32
/* Reduce risks */
{
typedef BOOL (WINAPI *t_SetDllDirectoryA) (LPCSTR lpPathName);
t_SetDllDirectoryA p_SetDllDirectoryA;
p_SetDllDirectoryA = GetProcAddress (GetModuleHandle ("kernel32.dll"), "SetDllDirectoryA");
if (p_SetDllDirectoryA)
(*p_SetDllDirectoryA) ("");
}
#ifndef _WIN64
{
typedef BOOL (WINAPI *t_SetProcessDEPPolicy) (DWORD dwFlags);
t_SetProcessDEPPolicy p_SetProcessDEPPolicy;
p_SetProcessDEPPolicy = GetProcAddress (GetModuleHandle ("kernel32.dll"), "SetProcessDEPPolicy");
if (p_SetProcessDEPPolicy)
(*p_SetProcessDEPPolicy) (PROCESS_DEP_ENABLE|PROCESS_DEP_DISABLE_ATL_THUNK_EMULATION);
}
#endif
#endif
g_type_init ();
g_set_prgname (E_PRGNAME);
if (!g_thread_supported ()) g_thread_init (NULL);
dbus_g_thread_init ();
loop = g_main_loop_new (NULL, FALSE);
/* Obtain a connection to the session bus */
connection = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
if (connection == NULL)
die ("Failed to open connection to bus", error);
bus_proxy = dbus_g_proxy_new_for_name (connection,
DBUS_SERVICE_DBUS,
DBUS_PATH_DBUS,
DBUS_INTERFACE_DBUS);
if (!org_freedesktop_DBus_request_name (bus_proxy, E_DATA_BOOK_FACTORY_SERVICE_NAME,
0, &request_name_ret, &error))
die ("Failed to get name", error);
if (request_name_ret != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
g_error ("Got result code %u from requesting name", request_name_ret);
exit (1);
}
factory = g_object_new (E_TYPE_DATA_BOOK_FACTORY, NULL);
dbus_g_connection_register_g_object (connection,
"/org/gnome/evolution/dataserver/addressbook/BookFactory",
G_OBJECT (factory));
dbus_g_proxy_add_signal (bus_proxy, "NameOwnerChanged",
G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INVALID);
dbus_g_proxy_connect_signal (bus_proxy, "NameOwnerChanged", G_CALLBACK (name_owner_changed), factory, NULL);
eol = e_offline_listener_new ();
offline_state_changed_cb (eol, factory);
g_signal_connect (eol, "changed", G_CALLBACK (offline_state_changed_cb), factory);
printf ("Server is up and running...\n");
g_main_loop_run (loop);
g_object_unref (eol);
dbus_g_connection_unref (connection);
printf ("Bye.\n");
return 0;
}
|