updated changelog
[platform/upstream/evolution-data-server.git] / libedataserver / e-source-mail-account.c
1 /*
2  * e-source-mail-account.c
3  *
4  * This library is free software you can redistribute it and/or modify it
5  * under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation.
7  *
8  * This library is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
11  * for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this library; if not, see <http://www.gnu.org/licenses/>.
15  *
16  */
17
18 /**
19  * SECTION: e-source-mail-account
20  * @include: libedataserver/libedataserver.h
21  * @short_description: #ESource extension for an email account
22  *
23  * The #ESourceMailAccount extension identifies the #ESource as a
24  * mail account and also links to a default "mail identity" to use.
25  * See #ESourceMailIdentity for more information about identities.
26  *
27  * Access the extension as follows:
28  *
29  * |[
30  *   #include <libedataserver/libedataserver.h>
31  *
32  *   ESourceMailAccount *extension;
33  *
34  *   extension = e_source_get_extension (source, E_SOURCE_EXTENSION_MAIL_ACCOUNT);
35  * ]|
36  **/
37
38 #include "e-source-mail-account.h"
39
40 #include <libedataserver/e-source-enumtypes.h>
41 #include <libedataserver/e-source-mail-identity.h>
42
43 #define E_SOURCE_MAIL_ACCOUNT_GET_PRIVATE(obj) \
44         (G_TYPE_INSTANCE_GET_PRIVATE \
45         ((obj), E_TYPE_SOURCE_MAIL_ACCOUNT, ESourceMailAccountPrivate))
46
47 struct _ESourceMailAccountPrivate {
48         GMutex property_lock;
49         gchar *identity_uid;
50 };
51
52 enum {
53         PROP_0,
54         PROP_IDENTITY_UID
55 };
56
57 G_DEFINE_TYPE (
58         ESourceMailAccount,
59         e_source_mail_account,
60         E_TYPE_SOURCE_BACKEND)
61
62 static void
63 source_mail_account_set_property (GObject *object,
64                                   guint property_id,
65                                   const GValue *value,
66                                   GParamSpec *pspec)
67 {
68         switch (property_id) {
69                 case PROP_IDENTITY_UID:
70                         e_source_mail_account_set_identity_uid (
71                                 E_SOURCE_MAIL_ACCOUNT (object),
72                                 g_value_get_string (value));
73                         return;
74         }
75
76         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
77 }
78
79 static void
80 source_mail_account_get_property (GObject *object,
81                                   guint property_id,
82                                   GValue *value,
83                                   GParamSpec *pspec)
84 {
85         switch (property_id) {
86                 case PROP_IDENTITY_UID:
87                         g_value_take_string (
88                                 value,
89                                 e_source_mail_account_dup_identity_uid (
90                                 E_SOURCE_MAIL_ACCOUNT (object)));
91                         return;
92         }
93
94         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
95 }
96
97 static void
98 source_mail_account_finalize (GObject *object)
99 {
100         ESourceMailAccountPrivate *priv;
101
102         priv = E_SOURCE_MAIL_ACCOUNT_GET_PRIVATE (object);
103
104         g_mutex_clear (&priv->property_lock);
105
106         g_free (priv->identity_uid);
107
108         /* Chain up to parent's finalize() method. */
109         G_OBJECT_CLASS (e_source_mail_account_parent_class)->finalize (object);
110 }
111
112 static void
113 e_source_mail_account_class_init (ESourceMailAccountClass *class)
114 {
115         GObjectClass *object_class;
116         ESourceExtensionClass *extension_class;
117
118         g_type_class_add_private (class, sizeof (ESourceMailAccountPrivate));
119
120         object_class = G_OBJECT_CLASS (class);
121         object_class->set_property = source_mail_account_set_property;
122         object_class->get_property = source_mail_account_get_property;
123         object_class->finalize = source_mail_account_finalize;
124
125         extension_class = E_SOURCE_EXTENSION_CLASS (class);
126         extension_class->name = E_SOURCE_EXTENSION_MAIL_ACCOUNT;
127
128         g_object_class_install_property (
129                 object_class,
130                 PROP_IDENTITY_UID,
131                 g_param_spec_string (
132                         "identity-uid",
133                         "Identity UID",
134                         "ESource UID of a Mail Identity",
135                         "self",
136                         G_PARAM_READWRITE |
137                         G_PARAM_CONSTRUCT |
138                         G_PARAM_STATIC_STRINGS |
139                         E_SOURCE_PARAM_SETTING));
140 }
141
142 static void
143 e_source_mail_account_init (ESourceMailAccount *extension)
144 {
145         extension->priv = E_SOURCE_MAIL_ACCOUNT_GET_PRIVATE (extension);
146         g_mutex_init (&extension->priv->property_lock);
147 }
148
149 /**
150  * e_source_mail_account_get_identity_uid:
151  * @extension: an #ESourceMailAccount
152  *
153  * Returns the #ESource:uid of the #ESource that describes the mail
154  * identity to be used for this account.
155  *
156  * Returns: the mail identity #ESource:uid
157  *
158  * Since: 3.6
159  **/
160 const gchar *
161 e_source_mail_account_get_identity_uid (ESourceMailAccount *extension)
162 {
163         g_return_val_if_fail (E_IS_SOURCE_MAIL_ACCOUNT (extension), NULL);
164
165         return extension->priv->identity_uid;
166 }
167
168 /**
169  * e_source_mail_account_dup_identity_uid:
170  * @extension: an #ESourceMailAccount
171  *
172  * Thread-safe variation of e_source_mail_account_get_identity_uid().
173  * Use this function when accessing @extension from multiple threads.
174  *
175  * The returned string should be freed with g_free() when no longer needed.
176  *
177  * Returns: a newly-allocated copy of #ESourceMailAccount:identity-uid
178  *
179  * Since: 3.6
180  **/
181 gchar *
182 e_source_mail_account_dup_identity_uid (ESourceMailAccount *extension)
183 {
184         const gchar *protected;
185         gchar *duplicate;
186
187         g_return_val_if_fail (E_IS_SOURCE_MAIL_ACCOUNT (extension), NULL);
188
189         g_mutex_lock (&extension->priv->property_lock);
190
191         protected = e_source_mail_account_get_identity_uid (extension);
192         duplicate = g_strdup (protected);
193
194         g_mutex_unlock (&extension->priv->property_lock);
195
196         return duplicate;
197 }
198
199 /**
200  * e_source_mail_account_set_identity_uid:
201  * @extension: an #ESourceMailAccount
202  * @identity_uid: (allow-none): the mail identity #ESource:uid, or %NULL
203  *
204  * Sets the #ESource:uid of the #ESource that describes the mail
205  * identity to be used for this account.
206  *
207  * Since: 3.6
208  **/
209 void
210 e_source_mail_account_set_identity_uid (ESourceMailAccount *extension,
211                                         const gchar *identity_uid)
212 {
213         g_return_if_fail (E_IS_SOURCE_MAIL_ACCOUNT (extension));
214
215         g_mutex_lock (&extension->priv->property_lock);
216
217         if (g_strcmp0 (extension->priv->identity_uid, identity_uid) == 0) {
218                 g_mutex_unlock (&extension->priv->property_lock);
219                 return;
220         }
221
222         g_free (extension->priv->identity_uid);
223         extension->priv->identity_uid = g_strdup (identity_uid);
224
225         g_mutex_unlock (&extension->priv->property_lock);
226
227         g_object_notify (G_OBJECT (extension), "identity-uid");
228 }
229