Add new ESource classes.
[platform/upstream/evolution-data-server.git] / libedataserver / e-system-source.c
1 /*
2  * e-system-source.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 "e-system-source.h"
20
21 #include <config.h>
22 #include <glib/gi18n-lib.h>
23
24 #include "e-data-server-util.h"
25
26 G_DEFINE_TYPE (
27         ESystemSource,
28         e_system_source,
29         E_TYPE_SOURCE)
30
31 static void
32 system_source_notify (GObject *object,
33                       GParamSpec *pspec)
34 {
35         ESource *source = E_SOURCE (object);
36
37         /* GObject does not allow subclasses to override property flags,
38          * so we'll keep the "backend-name" and "display-name" properties
39          * fixed by intercepting attempts to change them and setting them
40          * back to their proper values.  Hokey but works. */
41
42         if (g_strcmp0 (pspec->name, "backend-name") == 0) {
43                 if (e_source_get_backend_name (source) != NULL) {
44                         e_source_set_backend_name (source, NULL);
45                         return;
46                 }
47         }
48
49         if (g_strcmp0 (pspec->name, "display-name") == 0) {
50                 const gchar *display_name;
51                 const gchar *proper_value;
52
53                 display_name = e_source_get_display_name (source);
54                 proper_value = _("Personal");
55
56                 if (g_strcmp0 (display_name, proper_value) != 0) {
57                         e_source_set_display_name (source, proper_value);
58                         return;
59                 }
60         }
61
62         /* Chain up to parent's notify() method. */
63         G_OBJECT_CLASS (e_system_source_parent_class)->notify (object, pspec);
64 }
65
66 static void
67 e_system_source_class_init (ESystemSourceClass *class)
68 {
69         GObjectClass *object_class;
70
71         object_class = G_OBJECT_CLASS (class);
72         object_class->notify = system_source_notify;
73 }
74
75 static void
76 e_system_source_init (ESystemSource *source)
77 {
78 }
79
80 ESource *
81 e_system_source_new (void)
82 {
83         GSettings *settings;
84         ESource *source;
85         GFile *file;
86         const gchar *data_dir;
87         gchar *path;
88
89         data_dir = e_get_user_data_dir ();
90         path = g_build_filename (data_dir, "sources", "system", NULL);
91         file = g_file_new_for_path (path);
92         g_free (path);
93
94         /* This function must not fail, so if a "system" key file
95          * exists and fails to load, delete it and try again. */
96         source = g_initable_new (
97                 E_TYPE_SYSTEM_SOURCE, NULL, NULL, "file", file, NULL);
98         if (source == NULL) {
99                 g_file_delete (file, NULL, NULL);
100                 source = g_initable_new (
101                         E_TYPE_SYSTEM_SOURCE, NULL, NULL, "file", file, NULL);
102         }
103
104         g_object_unref (file);
105
106         g_return_val_if_fail (E_IS_SYSTEM_SOURCE (source), NULL);
107
108         /* Set the "parent" key directly through its GSettings.
109          *
110          * XXX To set this during object construction we would have
111          *     to override the GInitable interface.  Too much hassle
112          *     for now.  Maybe revisit this in the future. */
113         settings = e_source_get_settings (source);
114         g_settings_set_string (settings, "parent", "local");
115
116         return source;
117 }