1 /*
2  * params-cm.h - source for TpTestsParamConnectionManager
3  *
4  * Copyright © 2007-2009 Collabora Ltd. <http://www.collabora.co.uk/>
5  * Copyright © 2007-2009 Nokia Corporation
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20  */
21 
22 #include "config.h"
23 
24 #include "params-cm.h"
25 
26 #include <dbus/dbus-glib.h>
27 
28 #include <telepathy-glib/telepathy-glib.h>
29 
30 G_DEFINE_TYPE (TpTestsParamConnectionManager,
31     tp_tests_param_connection_manager,
32     TP_TYPE_BASE_CONNECTION_MANAGER)
33 
34 struct _TpTestsParamConnectionManagerPrivate
35 {
36   int dummy;
37 };
38 
39 static void
tp_tests_param_connection_manager_init(TpTestsParamConnectionManager * self)40 tp_tests_param_connection_manager_init (
41     TpTestsParamConnectionManager *self)
42 {
43   self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
44       TP_TESTS_TYPE_PARAM_CONNECTION_MANAGER,
45       TpTestsParamConnectionManagerPrivate);
46 }
47 
48 enum {
49     TP_TESTS_PARAM_STRING,
50     TP_TESTS_PARAM_INT16,
51     TP_TESTS_PARAM_INT32,
52     TP_TESTS_PARAM_UINT16,
53     TP_TESTS_PARAM_UINT32,
54     TP_TESTS_PARAM_INT64,
55     TP_TESTS_PARAM_UINT64,
56     TP_TESTS_PARAM_BOOLEAN,
57     TP_TESTS_PARAM_DOUBLE,
58     TP_TESTS_PARAM_ARRAY_STRINGS,
59     TP_TESTS_PARAM_ARRAY_BYTES,
60     TP_TESTS_PARAM_OBJECT_PATH,
61     TP_TESTS_PARAM_LC_STRING,
62     TP_TESTS_PARAM_UC_STRING,
63     NUM_PARAM
64 };
65 
66 static gboolean
filter_string_ascii_case(const TpCMParamSpec * param_spec,GValue * value,GError ** error)67 filter_string_ascii_case (const TpCMParamSpec *param_spec,
68     GValue *value,
69     GError **error)
70 {
71   const gchar *s = g_value_get_string (value);
72   guint i;
73 
74   for (i = 0; s[i] != '\0'; i++)
75     {
76       int c = s[i];           /* just to avoid -Wtype-limits */
77 
78       if (c < 0 || c > 127)   /* char might be signed or unsigned */
79         {
80           g_set_error (error, TP_ERROR, TP_ERROR_INVALID_ARGUMENT,
81               "%s must be ASCII", param_spec->name);
82           return FALSE;
83         }
84     }
85 
86   if (GINT_TO_POINTER (param_spec->filter_data))
87     g_value_take_string (value, g_ascii_strup (s, -1));
88   else
89     g_value_take_string (value, g_ascii_strdown (s, -1));
90 
91   return TRUE;
92 }
93 
94 static TpCMParamSpec param_example_params[] = {
95   { "a-string", "s", G_TYPE_STRING,
96     TP_CONN_MGR_PARAM_FLAG_HAS_DEFAULT, "the default string",
97     G_STRUCT_OFFSET (TpTestsCMParams, a_string), NULL, NULL, NULL },
98   { "a-int16", "n", G_TYPE_INT,
99     TP_CONN_MGR_PARAM_FLAG_HAS_DEFAULT, GINT_TO_POINTER (42),
100     G_STRUCT_OFFSET (TpTestsCMParams, a_int16), NULL, NULL, NULL },
101   { "a-int32", "i", G_TYPE_INT,
102     TP_CONN_MGR_PARAM_FLAG_HAS_DEFAULT, GINT_TO_POINTER (42),
103     G_STRUCT_OFFSET (TpTestsCMParams, a_int32), NULL, NULL, NULL },
104   { "a-uint16", "q", G_TYPE_UINT, 0, NULL,
105     G_STRUCT_OFFSET (TpTestsCMParams, a_uint16), NULL, NULL, NULL },
106   { "a-uint32", "u", G_TYPE_UINT, 0, NULL,
107     G_STRUCT_OFFSET (TpTestsCMParams, a_uint32), NULL, NULL, NULL },
108   { "a-int64", "x", G_TYPE_INT64, 0, NULL,
109     G_STRUCT_OFFSET (TpTestsCMParams, a_int64), NULL, NULL, NULL },
110   { "a-uint64", "t", G_TYPE_UINT64, 0, NULL,
111     G_STRUCT_OFFSET (TpTestsCMParams, a_uint64), NULL, NULL, NULL },
112   { "a-boolean", "b", G_TYPE_BOOLEAN, TP_CONN_MGR_PARAM_FLAG_REQUIRED, NULL,
113     G_STRUCT_OFFSET (TpTestsCMParams, a_boolean), NULL, NULL, NULL },
114   { "a-double", "d", G_TYPE_DOUBLE, 0, NULL,
115     G_STRUCT_OFFSET (TpTestsCMParams, a_double), NULL, NULL, NULL },
116   { "a-array-of-strings", "as", 0, 0, NULL,
117     G_STRUCT_OFFSET (TpTestsCMParams, a_array_of_strings), NULL, NULL, NULL },
118   { "a-array-of-bytes", "ay", 0, 0, NULL,
119     G_STRUCT_OFFSET (TpTestsCMParams, a_array_of_bytes), NULL, NULL, NULL },
120   { "a-object-path", "o", 0, 0, NULL,
121     G_STRUCT_OFFSET (TpTestsCMParams, a_object_path), NULL, NULL, NULL },
122 
123   /* demo of a filter */
124   { "lc-string", "s", G_TYPE_STRING, 0, NULL,
125     G_STRUCT_OFFSET (TpTestsCMParams, lc_string),
126     filter_string_ascii_case, GINT_TO_POINTER (FALSE), NULL },
127   { "uc-string", "s", G_TYPE_STRING, 0, NULL,
128     G_STRUCT_OFFSET (TpTestsCMParams, uc_string),
129     filter_string_ascii_case, GINT_TO_POINTER (TRUE), NULL },
130   { NULL }
131 };
132 
133 static TpTestsCMParams *params = NULL;
134 
135 static gpointer
alloc_params(void)136 alloc_params (void)
137 {
138   params = g_slice_new0 (TpTestsCMParams);
139 
140   return params;
141 }
142 
143 static void
free_params(gpointer p)144 free_params (gpointer p)
145 {
146   /* CM user is responsible to free params so he can check their values */
147   params = (TpTestsCMParams *) p;
148   params->would_have_been_freed = TRUE;
149 }
150 
151 static const TpCMProtocolSpec example_protocols[] = {
152   { "example", param_example_params,
153     alloc_params, free_params },
154   { NULL, NULL }
155 };
156 
157 static TpBaseConnection *
new_connection(TpBaseConnectionManager * self,const gchar * proto,TpIntset * params_present,gpointer parsed_params,GError ** error)158 new_connection (TpBaseConnectionManager *self,
159                 const gchar *proto,
160                 TpIntset *params_present,
161                 gpointer parsed_params,
162                 GError **error)
163 {
164   g_set_error (error, TP_ERROR, TP_ERROR_NOT_IMPLEMENTED,
165       "No connection for you");
166   return NULL;
167 }
168 
169 static void
tp_tests_param_connection_manager_class_init(TpTestsParamConnectionManagerClass * klass)170 tp_tests_param_connection_manager_class_init (
171     TpTestsParamConnectionManagerClass *klass)
172 {
173   TpBaseConnectionManagerClass *base_class =
174       (TpBaseConnectionManagerClass *) klass;
175 
176   g_type_class_add_private (klass,
177       sizeof (TpTestsParamConnectionManagerPrivate));
178 
179   param_example_params[TP_TESTS_PARAM_ARRAY_STRINGS].gtype = G_TYPE_STRV;
180   param_example_params[TP_TESTS_PARAM_ARRAY_BYTES].gtype =
181       DBUS_TYPE_G_UCHAR_ARRAY;
182   param_example_params[TP_TESTS_PARAM_OBJECT_PATH].gtype =
183       DBUS_TYPE_G_OBJECT_PATH;
184 
185   base_class->new_connection = new_connection;
186   base_class->cm_dbus_name = "params_cm";
187   base_class->protocol_params = example_protocols;
188 }
189 
190 TpTestsCMParams *
tp_tests_param_connection_manager_steal_params_last_conn(void)191 tp_tests_param_connection_manager_steal_params_last_conn (void)
192 {
193   TpTestsCMParams *p = params;
194 
195   params = NULL;
196   return p;
197 }
198 
199 void
tp_tests_param_connection_manager_free_params(TpTestsCMParams * p)200 tp_tests_param_connection_manager_free_params (TpTestsCMParams *p)
201 {
202   g_free (p->a_string);
203   g_strfreev (p->a_array_of_strings);
204   if (p->a_array_of_bytes != NULL)
205     g_array_unref (p->a_array_of_bytes);
206   g_free (p->a_object_path);
207 
208   g_slice_free (TpTestsCMParams, p);
209 }
210