Extending test-client-custom-summary to try e_book_client_get_contacts_uids()
[platform/upstream/evolution-data-server.git] / camel / camel-settings.c
1 /*
2  * camel-settings.c
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) version 3.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with the program; if not, see <http://www.gnu.org/licenses/>
16  *
17  */
18
19 #include "camel-settings.h"
20
21 #include <stdlib.h>
22
23 /* Needed for CamelSettings <--> CamelURL conversions. */
24 #include "camel-local-settings.h"
25 #include "camel-network-settings.h"
26
27 G_DEFINE_TYPE (CamelSettings, camel_settings, G_TYPE_OBJECT)
28
29 static GParamSpec **
30 settings_list_settings (CamelSettingsClass *class,
31                         guint *n_settings)
32 {
33         GObjectClass *object_class = G_OBJECT_CLASS (class);
34
35         return g_object_class_list_properties (object_class, n_settings);
36 }
37
38 static CamelSettings *
39 settings_clone (CamelSettings *settings)
40 {
41         CamelSettingsClass *class;
42         GParamSpec **properties;
43         GParameter *parameters;
44         CamelSettings *clone;
45         guint ii, n_properties;
46
47         class = CAMEL_SETTINGS_GET_CLASS (settings);
48         properties = camel_settings_class_list_settings (class, &n_properties);
49
50         parameters = g_new0 (GParameter, n_properties);
51
52         for (ii = 0; ii < n_properties; ii++) {
53                 parameters[ii].name = properties[ii]->name;
54                 g_value_init (
55                         &parameters[ii].value,
56                         properties[ii]->value_type);
57                 g_object_get_property (
58                         G_OBJECT (settings),
59                         parameters[ii].name,
60                         &parameters[ii].value);
61         }
62
63         clone = g_object_newv (
64                 G_OBJECT_TYPE (settings),
65                 n_properties, parameters);
66
67         for (ii = 0; ii < n_properties; ii++)
68                 g_value_unset (&parameters[ii].value);
69
70         g_free (parameters);
71         g_free (properties);
72
73         return clone;
74 }
75
76 static gboolean
77 settings_equal (CamelSettings *settings_a,
78                 CamelSettings *settings_b)
79 {
80         CamelSettingsClass *class;
81         GParamSpec **properties;
82         GValue *value_a;
83         GValue *value_b;
84         guint ii, n_properties;
85         gboolean equal = TRUE;
86
87         /* Make sure both instances are of the same type. */
88         if (G_OBJECT_TYPE (settings_a) != G_OBJECT_TYPE (settings_b))
89                 return FALSE;
90
91         value_a = g_slice_new0 (GValue);
92         value_b = g_slice_new0 (GValue);
93
94         class = CAMEL_SETTINGS_GET_CLASS (settings_a);
95         properties = camel_settings_class_list_settings (class, &n_properties);
96
97         for (ii = 0; equal && ii < n_properties; ii++) {
98                 GParamSpec *pspec = properties[ii];
99
100                 g_value_init (value_a, pspec->value_type);
101                 g_value_init (value_b, pspec->value_type);
102
103                 g_object_get_property (
104                         G_OBJECT (settings_a),
105                         pspec->name, value_a);
106
107                 g_object_get_property (
108                         G_OBJECT (settings_b),
109                         pspec->name, value_b);
110
111                 equal = (g_param_values_cmp (pspec, value_a, value_b) == 0);
112
113                 g_value_unset (value_a);
114                 g_value_unset (value_b);
115         }
116
117         g_free (properties);
118
119         g_slice_free (GValue, value_a);
120         g_slice_free (GValue, value_b);
121
122         return equal;
123 }
124
125 static void
126 camel_settings_class_init (CamelSettingsClass *class)
127 {
128         class->list_settings = settings_list_settings;
129         class->clone = settings_clone;
130         class->equal = settings_equal;
131 }
132
133 static void
134 camel_settings_init (CamelSettings *settings)
135 {
136 }
137
138 /**
139  * camel_settings_class_list_settings:
140  * @settings_class: a #CamelSettingsClass
141  * @n_settings: return location for the length of the returned array
142  *
143  * Returns an array of #GParamSpec for properties of @class which are
144  * considered to be settings.  By default all properties are considered
145  * to be settings, but subclasses may wish to exclude certain properties.
146  * Free the returned array with g_free().
147  *
148  * Returns: an array of #GParamSpec which should be freed after use
149  *
150  * Since: 3.2
151  **/
152 GParamSpec **
153 camel_settings_class_list_settings (CamelSettingsClass *settings_class,
154                                     guint *n_settings)
155 {
156         g_return_val_if_fail (CAMEL_IS_SETTINGS_CLASS (settings_class), NULL);
157         g_return_val_if_fail (settings_class->list_settings != NULL, NULL);
158
159         return settings_class->list_settings (settings_class, n_settings);
160 }
161
162 /**
163  * camel_settings_clone:
164  * @settings: a #CamelSettings
165  *
166  * Creates an copy of @settings, such that passing @settings and the
167  * copied instance to camel_settings_equal() would return %TRUE.
168  *
169  * By default, this creates a new settings instance with the same #GType
170  * as @settings, and copies all #GObject property values from @settings
171  * to the new instance.
172  *
173  * Returns: a newly-created copy of @settings
174  *
175  * Since: 3.2
176  **/
177 CamelSettings *
178 camel_settings_clone (CamelSettings *settings)
179 {
180         CamelSettingsClass *class;
181         CamelSettings *clone;
182
183         g_return_val_if_fail (CAMEL_IS_SETTINGS (settings), NULL);
184
185         class = CAMEL_SETTINGS_GET_CLASS (settings);
186         g_return_val_if_fail (class->clone != NULL, NULL);
187
188         clone = class->clone (settings);
189
190         /* Make sure the documented invariant is satisfied. */
191         g_warn_if_fail (camel_settings_equal (settings, clone));
192
193         return clone;
194 }
195
196 /**
197  * camel_settings_equal:
198  * @settings_a: a #CamelSettings
199  * @settings_b: another #CamelSettings
200  *
201  * Returns %TRUE if @settings_a and @settings_b are equal.
202  *
203  * By default, equality requires both instances to have the same #GType
204  * with the same set of #GObject properties, and each property value in
205  * @settings_a is equal to the corresponding value in @settings_b.
206  *
207  * Returns: %TRUE if @settings_a and @settings_b are equal
208  *
209  * Since: 3.2
210  **/
211 gboolean
212 camel_settings_equal (CamelSettings *settings_a,
213                       CamelSettings *settings_b)
214 {
215         CamelSettingsClass *class;
216
217         g_return_val_if_fail (CAMEL_IS_SETTINGS (settings_a), FALSE);
218         g_return_val_if_fail (CAMEL_IS_SETTINGS (settings_b), FALSE);
219
220         class = CAMEL_SETTINGS_GET_CLASS (settings_a);
221         g_return_val_if_fail (class->equal != NULL, FALSE);
222
223         return class->equal (settings_a, settings_b);
224 }
225