Debian Codegraph @

src/goabackend/goamailconfig.c

raw symbols SCIP
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /*
3 * Copyright © 2024 GNOME Foundation Inc.
4 * Contributor: Andy Holmes <[email protected]>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General
17 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "config.h"
21
22 #include <stdint.h>
23
24 #include <glib-object.h>
25
26 #include "goabackendenums-priv.h"
27 #include "goabackendenumtypes-priv.h"
28 #include "goaserviceconfig.h"
29
30 #include "goamailconfig.h"
31
32 struct _GoaMailConfig
33 {
34 GoaServiceConfig parent_instance;
35
36 char *username;
37 char *hostname;
38 uint16_t port;
39 GoaTlsType encryption;
40 };
41
42 G_DEFINE_FINAL_TYPE (GoaMailConfig, goa_mail_config, GOA_TYPE_SERVICE_CONFIG)
43
44 typedef enum
45 {
46 PROP_ENCRYPTION = 1,
47 PROP_HOSTNAME,
48 PROP_PORT,
49 PROP_USERNAME,
50 } GoaMailConfigProperty;
51
52 static GParamSpec *properties[PROP_USERNAME + 1] = { NULL, };
53
54 /* ---------------------------------------------------------------------------------------------------- */
55
56 static void
57 goa_mail_config_finalize (GObject *object)
58 {
59 GoaMailConfig *self = GOA_MAIL_CONFIG (object);
60
61 g_clear_pointer (&self->hostname, g_free);
62 g_clear_pointer (&self->username, g_free);
63
64 G_OBJECT_CLASS (goa_mail_config_parent_class)->finalize (object);
65 }
66
67 static void
68 goa_mail_config_get_property (GObject *object,
69 guint prop_id,
70 GValue *value,
71 GParamSpec *pspec)
72 {
73 GoaMailConfig *self = GOA_MAIL_CONFIG (object);
74
75 switch ((GoaMailConfigProperty) prop_id)
76 {
77 case PROP_ENCRYPTION:
78 g_value_set_enum (value, self->encryption);
79 break;
80
81 case PROP_HOSTNAME:
82 g_value_set_string (value, self->hostname);
83 break;
84
85 case PROP_PORT:
86 g_value_set_uint (value, self->port);
87 break;
88
89 case PROP_USERNAME:
90 g_value_set_string (value, self->username);
91 break;
92
93 default:
94 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
95 break;
96 }
97 }
98
99 static void
100 goa_mail_config_set_property (GObject *object,
101 guint prop_id,
102 const GValue *value,
103 GParamSpec *pspec)
104 {
105 GoaMailConfig *self = GOA_MAIL_CONFIG (object);
106
107 switch ((GoaMailConfigProperty) prop_id)
108 {
109 case PROP_ENCRYPTION:
110 goa_mail_config_set_encryption (self, g_value_get_enum (value));
111 break;
112
113 case PROP_HOSTNAME:
114 goa_mail_config_set_hostname (self, g_value_get_string (value));
115 break;
116
117 case PROP_PORT:
118 goa_mail_config_set_port (self, g_value_get_uint (value));
119 break;
120
121 case PROP_USERNAME:
122 goa_mail_config_set_username (self, g_value_get_string (value));
123 break;
124
125 default:
126 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
127 break;
128 }
129 }
130
131 /* ---------------------------------------------------------------------------------------------------- */
132
133 static void
134 goa_mail_config_init (GoaMailConfig *self)
135 {
136 }
137
138 static void
139 goa_mail_config_class_init (GoaMailConfigClass *klass)
140 {
141 GObjectClass *object_class = G_OBJECT_CLASS (klass);
142
143 object_class->finalize = goa_mail_config_finalize;
144 object_class->get_property = goa_mail_config_get_property;
145 object_class->set_property = goa_mail_config_set_property;
146
147 /**
148 * GoaMailConfig:encryption: (getter get_encryption) (setter set_encryption)
149 *
150 * The encryption protocol (e.g. TLS or STARTTLS).
151 */
152 properties[PROP_ENCRYPTION] =
153 g_param_spec_enum ("encryption", NULL, NULL,
154 GOA_TYPE_TLS_TYPE,
155 GOA_TLS_TYPE_NONE,
156 (G_PARAM_READWRITE |
157 G_PARAM_STATIC_STRINGS |
158 G_PARAM_EXPLICIT_NOTIFY));
159
160 /**
161 * GoaMailConfig:hostname: (getter get_hostname) (setter set_hostname)
162 *
163 * The hostname.
164 */
165 properties[PROP_HOSTNAME] =
166 g_param_spec_string ("hostname", NULL, NULL,
167 NULL,
168 (G_PARAM_READWRITE |
169 G_PARAM_STATIC_STRINGS |
170 G_PARAM_EXPLICIT_NOTIFY));
171
172 /**
173 * GoaMailConfig:port: (getter get_port) (setter set_port)
174 *
175 * The port.
176 */
177 properties[PROP_PORT] =
178 g_param_spec_uint ("port", NULL, NULL,
179 0, G_MAXUINT16,
180 0,
181 (G_PARAM_READWRITE |
182 G_PARAM_STATIC_STRINGS |
183 G_PARAM_EXPLICIT_NOTIFY));
184
185 /**
186 * GoaMailConfig:uri: (getter get_username) (setter set_username)
187 *
188 * The username or email.
189 */
190 properties[PROP_USERNAME] =
191 g_param_spec_string ("username", NULL, NULL,
192 NULL,
193 (G_PARAM_READWRITE |
194 G_PARAM_STATIC_STRINGS |
195 G_PARAM_EXPLICIT_NOTIFY));
196
197 g_object_class_install_properties (object_class, G_N_ELEMENTS (properties), properties);
198 }
199
200 /* ---------------------------------------------------------------------------------------------------- */
201
202 /**
203 * goa_mail_config_new:
204 * @service: (not nullable): a service type
205 *
206 * Create a new, empty service configuration.
207 *
208 * Returns: (transfer full): a service configuration
209 */
210 GoaMailConfig *
211 goa_mail_config_new (const char *service)
212 {
213 g_return_val_if_fail (service != NULL, NULL);
214
215 return g_object_new (GOA_TYPE_MAIL_CONFIG,
216 "service", service,
217 NULL);
218 }
219
220 /* ---------------------------------------------------------------------------------------------------- */
221
222 /**
223 * goa_mail_config_get_encryption: (get-property encryption)
224 * @config: a `GoaMailConfig`
225 *
226 * Get the encryption.
227 *
228 * Returns: a `GoaTlsType` encryption.
229 */
230 GoaTlsType
231 goa_mail_config_get_encryption (GoaMailConfig *config)
232 {
233 g_return_val_if_fail (GOA_IS_MAIL_CONFIG (config), GOA_TLS_TYPE_NONE);
234
235 return config->encryption;
236 }
237
238 /**
239 * goa_mail_config_set_encryption: (set-property encryption)
240 * @config: a `GoaMailConfig`
241 * @encryption: a `GoaTlsType`
242 *
243 * Set the encryption.
244 */
245 void
246 goa_mail_config_set_encryption (GoaMailConfig *config,
247 GoaTlsType encryption)
248 {
249 g_return_if_fail (GOA_IS_MAIL_CONFIG (config));
250 g_return_if_fail (encryption >= GOA_TLS_TYPE_NONE && encryption <= GOA_TLS_TYPE_SSL);
251
252 if (config->encryption != encryption)
253 {
254 config->encryption = encryption;
255 g_object_notify_by_pspec (G_OBJECT (config), properties[PROP_ENCRYPTION]);
256 }
257 }
258
259 /* ---------------------------------------------------------------------------------------------------- */
260
261 /**
262 * goa_mail_config_get_hostname: (get-property hostname)
263 * @config: a `GoaMailConfig`
264 *
265 * Get the hostname.
266 *
267 * Returns: (transfer none) (nullable): a hostname, or %NULL if unknown
268 */
269 const char *
270 goa_mail_config_get_hostname (GoaMailConfig *config)
271 {
272 g_return_val_if_fail (GOA_IS_MAIL_CONFIG (config), NULL);
273
274 return config->hostname;
275 }
276
277 /**
278 * goa_mail_config_set_hostname: (set-property hostname)
279 * @config: a `GoaMailConfig`
280 * @hostname: (nullable): a hostname
281 *
282 * Set the hostname.
283 */
284 void
285 goa_mail_config_set_hostname (GoaMailConfig *config,
286 const char *hostname)
287 {
288 g_return_if_fail (GOA_IS_MAIL_CONFIG (config));
289
290 if (g_set_str (&config->hostname, hostname))
291 g_object_notify_by_pspec (G_OBJECT (config), properties[PROP_HOSTNAME]);
292 }
293
294 /* ---------------------------------------------------------------------------------------------------- */
295
296 /**
297 * goa_mail_config_get_port: (get-property port)
298 * @config: a `GoaMailConfig`
299 *
300 * Get the port.
301 *
302 * If the port is unset and the service is IMAP or SMTP, a default port will
303 * be returned based on [[email protected]:encryption].
304 *
305 * Returns: a port
306 */
307 uint16_t
308 goa_mail_config_get_port (GoaMailConfig *config)
309 {
310 g_return_val_if_fail (GOA_IS_MAIL_CONFIG (config), 0);
311
312 if (config->port == 0)
313 {
314 const char *service = NULL;
315
316 service = goa_service_config_get_service (GOA_SERVICE_CONFIG (config));
317 if (g_ascii_strcasecmp (service, GOA_SERVICE_TYPE_IMAP) == 0)
318 {
319 return (config->encryption == GOA_TLS_TYPE_SSL) ? 993 : 143;
320 }
321 else if (g_ascii_strcasecmp (service, GOA_SERVICE_TYPE_SMTP) == 0)
322 {
323 return (config->encryption == GOA_TLS_TYPE_SSL) ? 465 : 587;
324 }
325 }
326
327 return config->port;
328 }
329
330 /**
331 * goa_mail_config_set_port: (set-property port)
332 * @config: a `GoaMailConfig`
333 * @port: a valid port
334 *
335 * Set the port.
336 */
337 void
338 goa_mail_config_set_port (GoaMailConfig *config,
339 uint16_t port)
340 {
341 g_return_if_fail (GOA_IS_MAIL_CONFIG (config));
342 g_return_if_fail (port <= G_MAXUINT16);
343
344 if (config->port != port)
345 {
346 config->port = port;
347 g_object_notify_by_pspec (G_OBJECT (config), properties[PROP_PORT]);
348 }
349 }
350
351 /* ---------------------------------------------------------------------------------------------------- */
352
353 /**
354 * goa_mail_config_get_username: (get-property username)
355 * @config: a `GoaMailConfig`
356 *
357 * Get the username, or %NULL if unknown.
358 *
359 * Returns: (transfer none) (nullable): a username
360 */
361 const char *
362 goa_mail_config_get_username (GoaMailConfig *config)
363 {
364 g_return_val_if_fail (GOA_IS_MAIL_CONFIG (config), NULL);
365
366 return config->username;
367 }
368
369 /**
370 * goa_mail_config_set_username: (set-property username)
371 * @config: a `GoaMailConfig`
372 * @username: (nullable): a username or email
373 *
374 * Set the username.
375 */
376 void
377 goa_mail_config_set_username (GoaMailConfig *config,
378 const char *username)
379 {
380 g_return_if_fail (GOA_IS_MAIL_CONFIG (config));
381
382 if (g_set_str (&config->username, username))
383 g_object_notify_by_pspec (G_OBJECT (config), properties[PROP_USERNAME]);
384 }
385