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
|
/* vi: set et sw=4 ts=8 cino=t0,(0: */
/* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4; tab-width: 8 -*- */
/*
* This file is part of mission-control
*
* Copyright (C) 2008-2009 Nokia Corporation.
* Copyright (C) 2009 Collabora Ltd.
*
* Contact: Alberto Mardegan <alberto.mardegan@nokia.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* version 2.1 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
*
*/
#include <stdio.h>
#include <string.h>
#include <glib/gstdio.h>
#include <glib/gi18n.h>
#include <config.h>
#include <telepathy-glib/svc-generic.h>
#include <telepathy-glib/gtypes.h>
#include <telepathy-glib/util.h>
#include "mcd-account.h"
#include "mcd-account-priv.h"
#include "mcd-account-conditions.h"
#include "mcd-account-manager.h"
#include "_gen/interfaces.h"
static void
store_condition (gpointer key, gpointer value, gpointer userdata)
{
McdAccount *account = userdata;
const gchar *name = key, *condition = value;
const gchar *unique_name;
gchar condition_key[256];
GKeyFile *keyfile;
keyfile = _mcd_account_get_keyfile (account);
unique_name = mcd_account_get_unique_name (account);
g_snprintf (condition_key, sizeof (condition_key), "condition-%s", name);
g_key_file_set_string (keyfile, unique_name, condition_key, condition);
}
static gboolean
set_condition (TpSvcDBusProperties *self, const gchar *name,
const GValue *value, GError **error)
{
McdAccount *account = MCD_ACCOUNT (self);
const gchar *unique_name;
GKeyFile *keyfile;
gchar **keys, **key;
GHashTable *conditions;
/* FIXME: some sort of validation beyond just the type? */
if (!G_VALUE_HOLDS (value, TP_HASH_TYPE_STRING_STRING_MAP))
{
g_set_error (error, TP_ERRORS, TP_ERROR_INVALID_ARGUMENT,
"Expected a{s:s} for Condition, but got %s",
G_VALUE_TYPE_NAME (value));
return FALSE;
}
keyfile = _mcd_account_get_keyfile (account);
unique_name = mcd_account_get_unique_name (account);
conditions = g_value_get_boxed (value);
/* first, delete existing conditions */
keys = g_key_file_get_keys (keyfile, unique_name, NULL, NULL);
for (key = keys; *key != NULL; key++)
{
if (strncmp (*key, "condition-", 10) != 0) continue;
g_key_file_remove_key (keyfile, unique_name,
*key, NULL);
}
g_strfreev (keys);
g_hash_table_foreach (conditions, store_condition, account);
_mcd_account_write_conf (account);
return TRUE;
}
static void
get_condition (TpSvcDBusProperties *self, const gchar *name, GValue *value)
{
GHashTable *conditions;
conditions = mcd_account_get_conditions (MCD_ACCOUNT (self));
g_value_init (value, DBUS_TYPE_G_STRING_STRING_HASHTABLE);
g_value_take_boxed (value, conditions);
}
const McdDBusProp account_conditions_properties[] = {
{ "Condition", set_condition, get_condition },
{ 0 },
};
void
account_conditions_iface_init (McSvcAccountInterfaceConditionsClass *iface,
gpointer iface_data)
{
}
GHashTable *mcd_account_get_conditions (McdAccount *account)
{
const gchar *unique_name;
GKeyFile *keyfile;
gchar **keys, **key, *condition;
GHashTable *conditions;
keyfile = _mcd_account_get_keyfile (account);
unique_name = mcd_account_get_unique_name (account);
conditions = g_hash_table_new_full (g_str_hash, g_str_equal,
g_free, g_free);
keys = g_key_file_get_keys (keyfile, unique_name, NULL, NULL);
for (key = keys; *key != NULL; key++)
{
if (strncmp (*key, "condition-", 10) != 0) continue;
condition = g_key_file_get_string (keyfile, unique_name, *key, NULL);
DEBUG ("Condition: %s = %s", *key, condition);
g_hash_table_insert (conditions, g_strdup (*key + 10), condition);
}
g_strfreev (keys);
return conditions;
}
|