1 /* vi: set et sw=4 ts=8 cino=t0,(0: */
2 /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4; tab-width: 8 -*- */
3 /*
4  * This file is part of mission-control
5  *
6  * Copyright © 2010 Nokia Corporation.
7  * Copyright © 2010 Collabora Ltd.
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License
11  * version 2.1 as published by the Free Software Foundation.
12  *
13  * This library is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21  * 02110-1301 USA
22  *
23  */
24 #include "config.h"
25 
26 #include "mcd-account-addressing.h"
27 
28 #include <telepathy-glib/telepathy-glib.h>
29 #include <telepathy-glib/telepathy-glib-dbus.h>
30 
31 #include "mcd-account.h"
32 #include "mcd-account-priv.h"
33 #include "_gen/interfaces.h"
34 
35 static void
addressing_set_uri_scheme_association(TpSvcAccountInterfaceAddressing * iface,const gchar * uri_scheme,gboolean association,DBusGMethodInvocation * context)36 addressing_set_uri_scheme_association (TpSvcAccountInterfaceAddressing *iface,
37     const gchar *uri_scheme,
38     gboolean association,
39     DBusGMethodInvocation *context)
40 {
41   McdAccount *self = MCD_ACCOUNT (iface);
42   const gchar *account = mcd_account_get_unique_name (self);
43   McdStorage *storage = _mcd_account_get_storage (self);
44   GValue value = G_VALUE_INIT;
45   gchar **schemes = NULL;
46   gboolean old_association = FALSE;
47 
48   g_value_init (&value, G_TYPE_STRV);
49 
50   if (mcd_storage_get_attribute (storage, account, MC_ACCOUNTS_KEY_URI_SCHEMES,
51                                  &value, NULL))
52     {
53       schemes = g_value_get_boxed (&value);
54       old_association = tp_strv_contains ((const gchar * const *) schemes,
55           uri_scheme);
56     }
57 
58   if (old_association != association)
59     {
60       GPtrArray *new_schemes = g_ptr_array_new ();
61       gchar **s;
62       GHashTable *changed;
63 
64       if (association)
65         {
66           /* Prepend this new scheme to the existing list of schemes */
67           g_ptr_array_add (new_schemes, (gchar *) uri_scheme);
68 
69           for (s = schemes; s != NULL && *s != NULL; s++)
70             g_ptr_array_add (new_schemes, *s);
71         }
72       else
73         {
74           /* Remove this scheme from the existing list of schemes */
75           for (s = schemes; s != NULL && *s != NULL; s++)
76             if (tp_strdiff (*s, uri_scheme))
77               g_ptr_array_add (new_schemes, *s);
78         }
79 
80       g_ptr_array_add (new_schemes, NULL);
81       mcd_storage_set_strv (storage, account, MC_ACCOUNTS_KEY_URI_SCHEMES,
82           (const gchar * const *) new_schemes->pdata);
83 
84       changed = tp_asv_new (
85           "URISchemes", G_TYPE_STRV, new_schemes->pdata,
86           NULL);
87 
88       tp_svc_dbus_properties_emit_properties_changed (self,
89           TP_IFACE_ACCOUNT_INTERFACE_ADDRESSING, changed, NULL);
90 
91       g_hash_table_unref (changed);
92       g_ptr_array_unref (new_schemes);
93     }
94 
95   g_value_unset (&value);
96   tp_svc_account_interface_addressing_return_from_set_uri_scheme_association (
97       context);
98 }
99 
100 static void
addressing_get_uri_schemes(TpSvcDBusProperties * iface,const gchar * name,GValue * value)101 addressing_get_uri_schemes (TpSvcDBusProperties *iface,
102     const gchar *name,
103     GValue *value)
104 {
105   McdAccount *self = MCD_ACCOUNT (iface);
106   const gchar *account = mcd_account_get_unique_name (self);
107   McdStorage *storage = _mcd_account_get_storage (self);
108 
109   g_value_init (value, G_TYPE_STRV);
110 
111   if (!mcd_storage_get_attribute (storage, account, MC_ACCOUNTS_KEY_URI_SCHEMES,
112                                   value, NULL))
113     {
114       g_value_set_boxed (value, NULL);
115     }
116 }
117 
118 const McdDBusProp account_addressing_properties[] = {
119   { "URISchemes", NULL, addressing_get_uri_schemes },
120   { 0 }
121 };
122 
123 void
account_addressing_iface_init(TpSvcAccountInterfaceAddressingClass * iface,gpointer data)124 account_addressing_iface_init (TpSvcAccountInterfaceAddressingClass *iface,
125     gpointer data)
126 {
127   #define IMPLEMENT(x) tp_svc_account_interface_addressing_implement_##x (\
128     iface, addressing_##x)
129     IMPLEMENT(set_uri_scheme_association);
130 #undef IMPLEMENT
131 }
132