1 /*
2  * e-source-contacts.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 Lesser 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 #include "evolution-data-server-config.h"
19 
20 #include "e-source-address-book.h"
21 
22 #include "e-source-contacts.h"
23 
24 struct _ESourceContactsPrivate {
25 	gboolean include_me;
26 };
27 
28 enum {
29 	PROP_0,
30 	PROP_INCLUDE_ME
31 };
32 
G_DEFINE_TYPE_WITH_PRIVATE(ESourceContacts,e_source_contacts,E_TYPE_SOURCE_EXTENSION)33 G_DEFINE_TYPE_WITH_PRIVATE (
34 	ESourceContacts,
35 	e_source_contacts,
36 	E_TYPE_SOURCE_EXTENSION)
37 
38 static void
39 source_contacts_set_property (GObject *object,
40                               guint property_id,
41                               const GValue *value,
42                               GParamSpec *pspec)
43 {
44 	switch (property_id) {
45 		case PROP_INCLUDE_ME:
46 			e_source_contacts_set_include_me (
47 				E_SOURCE_CONTACTS (object),
48 				g_value_get_boolean (value));
49 			return;
50 	}
51 
52 	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
53 }
54 
55 static void
source_contacts_get_property(GObject * object,guint property_id,GValue * value,GParamSpec * pspec)56 source_contacts_get_property (GObject *object,
57                               guint property_id,
58                               GValue *value,
59                               GParamSpec *pspec)
60 {
61 	switch (property_id) {
62 		case PROP_INCLUDE_ME:
63 			g_value_set_boolean (
64 				value,
65 				e_source_contacts_get_include_me (
66 				E_SOURCE_CONTACTS (object)));
67 			return;
68 	}
69 
70 	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
71 }
72 
73 static void
source_contacts_constructed(GObject * object)74 source_contacts_constructed (GObject *object)
75 {
76 	ESource *source;
77 	ESourceExtension *extension;
78 	ESourceBackend *backend_extension;
79 	ESourceContacts *contacts_extension;
80 	const gchar *backend_name;
81 	const gchar *extension_name;
82 	gboolean include_me;
83 
84 	/* Chain up to parent's constructed() method. */
85 	G_OBJECT_CLASS (e_source_contacts_parent_class)->constructed (object);
86 
87 	extension = E_SOURCE_EXTENSION (object);
88 	source = e_source_extension_ref_source (extension);
89 
90 	extension_name = E_SOURCE_EXTENSION_ADDRESS_BOOK;
91 	backend_extension = e_source_get_extension (source, extension_name);
92 	backend_name = e_source_backend_get_backend_name (backend_extension);
93 
94 	/* Only include local address books by default. */
95 	include_me = (g_strcmp0 (backend_name, "local") == 0);
96 
97 	contacts_extension = E_SOURCE_CONTACTS (extension);
98 	e_source_contacts_set_include_me (contacts_extension, include_me);
99 
100 	g_object_unref (source);
101 }
102 
103 static void
e_source_contacts_class_init(ESourceContactsClass * class)104 e_source_contacts_class_init (ESourceContactsClass *class)
105 {
106 	GObjectClass *object_class;
107 	ESourceExtensionClass *extension_class;
108 
109 	object_class = G_OBJECT_CLASS (class);
110 	object_class->set_property = source_contacts_set_property;
111 	object_class->get_property = source_contacts_get_property;
112 	object_class->constructed = source_contacts_constructed;
113 
114 	extension_class = E_SOURCE_EXTENSION_CLASS (class);
115 	extension_class->name = E_SOURCE_EXTENSION_CONTACTS_BACKEND;
116 
117 	g_object_class_install_property (
118 		object_class,
119 		PROP_INCLUDE_ME,
120 		g_param_spec_boolean (
121 			"include-me",
122 			"Include Me",
123 			"Include this address book in the contacts calendar",
124 			FALSE,  /* see constructed () */
125 			G_PARAM_READWRITE |
126 			G_PARAM_EXPLICIT_NOTIFY |
127 			E_SOURCE_PARAM_SETTING));
128 }
129 
130 static void
e_source_contacts_init(ESourceContacts * extension)131 e_source_contacts_init (ESourceContacts *extension)
132 {
133 	extension->priv = e_source_contacts_get_instance_private (extension);
134 }
135 
136 gboolean
e_source_contacts_get_include_me(ESourceContacts * extension)137 e_source_contacts_get_include_me (ESourceContacts *extension)
138 {
139 	g_return_val_if_fail (E_IS_SOURCE_CONTACTS (extension), FALSE);
140 
141 	return extension->priv->include_me;
142 }
143 
144 void
e_source_contacts_set_include_me(ESourceContacts * extension,gboolean include_me)145 e_source_contacts_set_include_me (ESourceContacts *extension,
146                                   gboolean include_me)
147 {
148 	g_return_if_fail (E_IS_SOURCE_CONTACTS (extension));
149 
150 	if (extension->priv->include_me == include_me)
151 		return;
152 
153 	extension->priv->include_me = include_me;
154 
155 	g_object_notify (G_OBJECT (extension), "include-me");
156 }
157