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