1 /*
2  * This program is free software; you can redistribute it and/or modify it
3  * under the terms of the GNU Lesser General Public License as published by
4  * the Free Software Foundation.
5  *
6  * This program is distributed in the hope that it will be useful, but
7  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
8  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9  * for more details.
10  *
11  * You should have received a copy of the GNU Lesser General Public License
12  * along with this program; if not, see <http://www.gnu.org/licenses/>.
13  *
14  *
15  * Authors:
16  *		David Trowbridge <trowbrds@cs.colorado.edu>
17  *
18  * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
19  *
20  */
21 
22 #include "evolution-config.h"
23 
24 #include "eab-config.h"
25 
26 #define EAB_CONFIG_GET_PRIVATE(obj) \
27 	(G_TYPE_INSTANCE_GET_PRIVATE \
28 	((obj), EAB_TYPE_CONFIG, EABConfigPrivate))
29 
30 struct _EABConfigPrivate {
31 	guint source_changed_id;
32 };
33 
G_DEFINE_TYPE(EABConfig,eab_config,E_TYPE_CONFIG)34 G_DEFINE_TYPE (EABConfig, eab_config, E_TYPE_CONFIG)
35 
36 static void
37 eab_config_init (EABConfig *cfg)
38 {
39 	cfg->priv = EAB_CONFIG_GET_PRIVATE (cfg);
40 }
41 
42 static void
ecp_target_free(EConfig * ec,EConfigTarget * t)43 ecp_target_free (EConfig *ec,
44                  EConfigTarget *t)
45 {
46 	struct _EABConfigPrivate *p = EAB_CONFIG (ec)->priv;
47 
48 	if (ec->target == t) {
49 		switch (t->type) {
50 		case EAB_CONFIG_TARGET_SOURCE: {
51 			EABConfigTargetSource *s = (EABConfigTargetSource *) t;
52 
53 			if (p->source_changed_id) {
54 				g_signal_handler_disconnect (s->source, p->source_changed_id);
55 				p->source_changed_id = 0;
56 			}
57 			break; }
58 		case EAB_CONFIG_TARGET_PREFS:
59 			break;
60 		}
61 	}
62 
63 	switch (t->type) {
64 	case EAB_CONFIG_TARGET_SOURCE: {
65 		EABConfigTargetSource *s = (EABConfigTargetSource *) t;
66 
67 		if (s->source)
68 			g_object_unref (s->source);
69 		break; }
70 	case EAB_CONFIG_TARGET_PREFS: {
71 		EABConfigTargetPrefs *s = (EABConfigTargetPrefs *) t;
72 
73 		if (s->settings)
74 			g_object_unref (s->settings);
75 		break; }
76 	}
77 
78 	((EConfigClass *) eab_config_parent_class)->target_free (ec, t);
79 }
80 
81 static void
ecp_source_changed(ESource * source,EConfig * ec)82 ecp_source_changed (ESource *source,
83                     EConfig *ec)
84 {
85 	e_config_target_changed (ec, E_CONFIG_TARGET_CHANGED_STATE);
86 }
87 
88 static void
ecp_set_target(EConfig * ec,EConfigTarget * t)89 ecp_set_target (EConfig *ec,
90                 EConfigTarget *t)
91 {
92 	struct _EABConfigPrivate *p = EAB_CONFIG_GET_PRIVATE (ec);
93 
94 	((EConfigClass *) eab_config_parent_class)->set_target (ec, t);
95 
96 	if (t) {
97 		switch (t->type) {
98 		case EAB_CONFIG_TARGET_SOURCE: {
99 			EABConfigTargetSource *s = (EABConfigTargetSource *) t;
100 
101 			p->source_changed_id = g_signal_connect (
102 				s->source, "changed",
103 				G_CALLBACK (ecp_source_changed), ec);
104 			break; }
105 		case EAB_CONFIG_TARGET_PREFS:
106 			break;
107 		}
108 	}
109 }
110 
111 static void
eab_config_class_init(EABConfigClass * class)112 eab_config_class_init (EABConfigClass *class)
113 {
114 	EConfigClass *config_class;
115 
116 	g_type_class_add_private (class, sizeof (struct _EABConfigPrivate));
117 
118 	config_class = E_CONFIG_CLASS (class);
119 	config_class->set_target = ecp_set_target;
120 	config_class->target_free = ecp_target_free;
121 }
122 
123 EABConfig *
eab_config_new(const gchar * menuid)124 eab_config_new (const gchar *menuid)
125 {
126 	EABConfig *ecp = g_object_new (eab_config_get_type (), NULL);
127 	e_config_construct (&ecp->config, menuid);
128 	return ecp;
129 }
130 
131 EABConfigTargetSource *
eab_config_target_new_source(EABConfig * ecp,ESource * source)132 eab_config_target_new_source (EABConfig *ecp,
133                               ESource *source)
134 {
135 	EABConfigTargetSource *t = e_config_target_new (
136 		&ecp->config, EAB_CONFIG_TARGET_SOURCE, sizeof (*t));
137 
138 	t->source = source;
139 	g_object_ref (source);
140 
141 	return t;
142 }
143 
144 EABConfigTargetPrefs *
eab_config_target_new_prefs(EABConfig * ecp,GSettings * settings)145 eab_config_target_new_prefs (EABConfig *ecp,
146                              GSettings *settings)
147 {
148 	EABConfigTargetPrefs *t = e_config_target_new (
149 		&ecp->config, EAB_CONFIG_TARGET_PREFS, sizeof (*t));
150 
151 	if (settings)
152 		t->settings = g_object_ref (settings);
153 	else
154 		t->settings = NULL;
155 
156 	return t;
157 }
158