daemon initial implementation
[platform/core/appfw/message-port-dbus.git] / lib / msgport-manager.c
1 #include "msgport-manager.h"
2 #include "message-port.h"
3 #include "common/log.h"
4 #include "dbus-manager-glue.h"
5 #include <gio/gio.h>
6
7 struct _MsgPortManager
8 {
9     MsgPortGlueManagerPorxy *proxy;
10     GHashTable *services; /* {gchar*:MsgPortService*} */
11     GList *local_service_list;
12     int n_local_services;
13 };
14
15 static MsgPortManager __manager;
16
17 MsgPortManager * msgport_manager_new ()
18 {
19     GError          *error = NULL;
20     GDBusConnection *connection = NULL;
21     MsgPortManager  *manager = g_slice_new0(MsgPortManager);
22     gchar           *bus_address = g_strdup_printf (MESSAGEPORT_BUS_ADDRESS, g_get_user_runtime_dir());
23
24     if (!manager) {
25         return NULL;
26     }
27
28     connection = g_dbus_connection_new_for_address_sync (bus_address,
29                                 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
30                                 NULL, NULL, &error);
31
32     if (error) {
33         ERR ("Unable to get bus connection for address %s: %s", bus_address, error->message);
34         g_error_free (error);
35         goto fail;
36     }
37
38     manager->services = g_hash_table_new_full (g_str_hash, g_str_equal,
39                                g_free, (GDestroyNotify)g_object_unref);
40     manager->local_servcie_list = NULL;
41     manager->n_local_services = 0;
42     manager->proxy = msgport_dbus_glue_manager_proxy_new_sync (
43             connection, G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES, NULL, "/", NULL, &error);
44
45     if (!manager->proxy) {
46         ERR ("Unable to get manager proxy : %s", error->message);
47         g_error_free (error);
48         goto fail;
49     }
50
51     return manager;
52
53 fail:
54     if (manager) msgport_manager_unref (manager);
55     return NULL;
56 }
57
58 MsgPortManager * msgport_get_manager () {
59     if (!__manager) 
60         __manager = magport_manager_new ();
61
62     return __manager;
63 }
64
65 int msgport_manager_register_service (MsgPortManager *manger, const gchar *port_name, gboolean is_trusted)
66 {
67     GError *error = NULL;
68     g_return_val_if_fail (manager && MSGPORT_IS_MANAGER (manager), MESSAGEPORT_ERROR_IO_ERROR);
69
70     if (!port_name) return MESSAGEPORT_ERROR_INVALID_PARAMETER;
71
72     msgport_dbus_glue_manager_call_register_service_sync (manager->proxy,
73             port_name, is_trusted, &object_path, NULL, &error);
74
75     if (error) {
76         WARN ("unable to register service (%s): %s", port_name, error->message);
77         g_error_free (error);
78         return MESSAGEPORT_ERROR_IO_ERROR;
79     }
80
81     MsgPortService *service = msgport_service_new (
82             g_dbus_proxy_get_connection (G_DBUS_PROXY(manager->proxy)),
83             object_path, &error);
84     if (!service) {
85         WARN ("unable to get service proxy for path %s: %s", object_path, error->message);
86         g_error_free (error);
87         g_free (object_path);
88         return MESSAGEPORT_ERROR_IO_ERROR;
89     }
90
91     g_hash_table_insert (manager->services, object_path, service);
92     manager->local_service_list = g_list_append (manager->local_service_list, object_path);
93     manager->n_local_services++;
94
95     return manager->n_local_services;
96 }
97
98 MsgPortService *
99 msgport_service_new (GDBusConnection *connection, const gchar *path, GError **error)
100 {
101     MsgPortService *service = g_object_new (MSGPORT_TYPE_SERVICE, NULL);
102
103     if (!service) {
104         /* FIXME: return no merory error */
105         return NULL
106     }
107
108     service->proxy = msgport_dbus_glue_service_proxy_new_sync (connection,
109                 g_dbus_proxy_get_connection (G_DBUS_PROXY(manager->proxy)),
110                 G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES, NULL, object_path, NULL, error);
111     if (!service->proxy) {
112         g_object_unref (service);
113         return NULL;
114     }
115
116     return service;
117 }
118