evolution-3.6.4/widgets/menus/gal-view-etable.c

No issues found

  1 /*
  2  * This program is free software; you can redistribute it and/or
  3  * modify it under the terms of the GNU Lesser General Public
  4  * License as published by the Free Software Foundation; either
  5  * version 2 of the License, or (at your option) version 3.
  6  *
  7  * This program is distributed in the hope that it will be useful,
  8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 10  * Lesser General Public License for more details.
 11  *
 12  * You should have received a copy of the GNU Lesser General Public
 13  * License along with the program; if not, see <http://www.gnu.org/licenses/>
 14  *
 15  *
 16  * Authors:
 17  *		Chris Lahey <clahey@ximian.com>
 18  *
 19  * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 20  *
 21  */
 22 
 23 #ifdef HAVE_CONFIG_H
 24 #include <config.h>
 25 #endif
 26 
 27 #include "table/e-table-config.h"
 28 
 29 #include "gal-view-etable.h"
 30 
 31 G_DEFINE_TYPE (GalViewEtable, gal_view_etable, GAL_TYPE_VIEW)
 32 
 33 static void
 34 detach_table (GalViewEtable *view)
 35 {
 36 	if (view->table == NULL)
 37 		return;
 38 	if (view->table_state_changed_id) {
 39 		g_signal_handler_disconnect (
 40 			view->table,
 41 			view->table_state_changed_id);
 42 		view->table_state_changed_id = 0;
 43 	}
 44 	g_object_unref (view->table);
 45 	view->table = NULL;
 46 }
 47 
 48 static void
 49 detach_tree (GalViewEtable *view)
 50 {
 51 	if (view->tree == NULL)
 52 		return;
 53 	if (view->tree_state_changed_id) {
 54 		g_signal_handler_disconnect (
 55 			view->tree,
 56 			view->tree_state_changed_id);
 57 		view->tree_state_changed_id = 0;
 58 	}
 59 	g_object_unref (view->tree);
 60 	view->tree = NULL;
 61 }
 62 
 63 static void
 64 config_changed (ETableConfig *config,
 65                 GalViewEtable *view)
 66 {
 67 	ETableState *state;
 68 	if (view->state)
 69 		g_object_unref (view->state);
 70 	g_object_get (
 71 		config,
 72 		"state", &state,
 73 		NULL);
 74 	view->state = e_table_state_duplicate (state);
 75 	g_object_unref (state);
 76 
 77 	gal_view_changed (GAL_VIEW (view));
 78 }
 79 
 80 static void
 81 gal_view_etable_edit (GalView *view,
 82                       GtkWindow *parent)
 83 {
 84 	GalViewEtable *etable_view = GAL_VIEW_ETABLE (view);
 85 	ETableConfig *config;
 86 
 87 	config = e_table_config_new (
 88 		etable_view->title,
 89 		etable_view->spec,
 90 		etable_view->state,
 91 		parent);
 92 
 93 	g_signal_connect (
 94 		config, "changed",
 95 		G_CALLBACK (config_changed), view);
 96 }
 97 
 98 static void
 99 gal_view_etable_load (GalView *view,
100                       const gchar *filename)
101 {
102 	e_table_state_load_from_file (GAL_VIEW_ETABLE (view)->state, filename);
103 }
104 
105 static void
106 gal_view_etable_save (GalView *view,
107                       const gchar *filename)
108 {
109 	e_table_state_save_to_file (GAL_VIEW_ETABLE (view)->state, filename);
110 }
111 
112 static const gchar *
113 gal_view_etable_get_title (GalView *view)
114 {
115 	return GAL_VIEW_ETABLE (view)->title;
116 }
117 
118 static void
119 gal_view_etable_set_title (GalView *view,
120                            const gchar *title)
121 {
122 	g_free (GAL_VIEW_ETABLE (view)->title);
123 	GAL_VIEW_ETABLE (view)->title = g_strdup (title);
124 }
125 
126 static const gchar *
127 gal_view_etable_get_type_code (GalView *view)
128 {
129 	return "etable";
130 }
131 
132 static GalView *
133 gal_view_etable_clone (GalView *view)
134 {
135 	GalViewEtable *gve, *new;
136 
137 	gve = GAL_VIEW_ETABLE (view);
138 
139 	new = g_object_new (GAL_TYPE_VIEW_ETABLE, NULL);
140 	new->spec  = gve->spec;
141 	new->title = g_strdup (gve->title);
142 	g_object_unref (new->state);
143 	new->state = e_table_state_duplicate (gve->state);
144 
145 	g_object_ref (new->spec);
146 
147 	return GAL_VIEW (new);
148 }
149 
150 static void
151 gal_view_etable_dispose (GObject *object)
152 {
153 	GalViewEtable *view = GAL_VIEW_ETABLE (object);
154 
155 	gal_view_etable_detach (view);
156 
157 	g_free (view->title);
158 	view->title = NULL;
159 
160 	if (view->spec)
161 		g_object_unref (view->spec);
162 	view->spec = NULL;
163 
164 	if (view->state)
165 		g_object_unref (view->state);
166 	view->state = NULL;
167 
168 	/* Chain up to parent's dispose() method. */
169 	G_OBJECT_CLASS (gal_view_etable_parent_class)->dispose (object);
170 }
171 
172 static void
173 gal_view_etable_class_init (GalViewEtableClass *class)
174 {
175 	GalViewClass *gal_view_class  = GAL_VIEW_CLASS (class);
176 	GObjectClass *object_class = G_OBJECT_CLASS (class);
177 
178 	gal_view_class->edit          = gal_view_etable_edit;
179 	gal_view_class->load          = gal_view_etable_load;
180 	gal_view_class->save          = gal_view_etable_save;
181 	gal_view_class->get_title     = gal_view_etable_get_title;
182 	gal_view_class->set_title     = gal_view_etable_set_title;
183 	gal_view_class->get_type_code = gal_view_etable_get_type_code;
184 	gal_view_class->clone         = gal_view_etable_clone;
185 
186 	object_class->dispose         = gal_view_etable_dispose;
187 }
188 
189 static void
190 gal_view_etable_init (GalViewEtable *gve)
191 {
192 	gve->spec  = NULL;
193 	gve->state = e_table_state_new ();
194 	gve->title = NULL;
195 }
196 
197 /**
198  * gal_view_etable_new
199  * @spec: The ETableSpecification that this view will be based upon.
200  * @title: The name of the new view.
201  *
202  * Returns a new GalViewEtable.  This is primarily for use by
203  * GalViewFactoryEtable.
204  *
205  * Returns: The new GalViewEtable.
206  */
207 GalView *
208 gal_view_etable_new (ETableSpecification *spec,
209                      const gchar *title)
210 {
211 	GalViewEtable *view;
212 
213 	g_return_val_if_fail (E_IS_TABLE_SPECIFICATION (spec), NULL);
214 
215 	view = g_object_new (GAL_TYPE_VIEW_ETABLE, NULL);
216 
217 	return gal_view_etable_construct (view, spec, title);
218 }
219 
220 /**
221  * gal_view_etable_construct
222  * @view: The view to construct.
223  * @spec: The ETableSpecification that this view will be based upon.
224  * @title: The name of the new view.
225  *
226  * constructs the GalViewEtable.  To be used by subclasses and
227  * language bindings.
228  *
229  * Returns: The GalViewEtable.
230  */
231 GalView *
232 gal_view_etable_construct (GalViewEtable *view,
233                            ETableSpecification *spec,
234                            const gchar *title)
235 {
236 	g_return_val_if_fail (GAL_IS_VIEW_ETABLE (view), NULL);
237 	g_return_val_if_fail (E_IS_TABLE_SPECIFICATION (spec), NULL);
238 
239 	if (spec)
240 		g_object_ref (spec);
241 	view->spec = spec;
242 
243 	if (view->state)
244 		g_object_unref (view->state);
245 	view->state = e_table_state_duplicate (spec->state);
246 
247 	view->title = g_strdup (title);
248 
249 	return GAL_VIEW (view);
250 }
251 
252 void
253 gal_view_etable_set_state (GalViewEtable *view,
254                            ETableState *state)
255 {
256 	g_return_if_fail (GAL_IS_VIEW_ETABLE (view));
257 	g_return_if_fail (E_IS_TABLE_STATE (state));
258 
259 	if (view->state)
260 		g_object_unref (view->state);
261 	view->state = e_table_state_duplicate (state);
262 
263 	gal_view_changed (GAL_VIEW (view));
264 }
265 
266 static void
267 table_state_changed (ETable *table,
268                      GalViewEtable *view)
269 {
270 	ETableState *state;
271 
272 	state = e_table_get_state_object (table);
273 	g_object_unref (view->state);
274 	view->state = state;
275 
276 	gal_view_changed (GAL_VIEW (view));
277 }
278 
279 static void
280 tree_state_changed (ETree *tree,
281                     GalViewEtable *view)
282 {
283 	ETableState *state;
284 
285 	state = e_tree_get_state_object (tree);
286 	g_object_unref (view->state);
287 	view->state = state;
288 
289 	gal_view_changed (GAL_VIEW (view));
290 }
291 
292 void
293 gal_view_etable_attach_table (GalViewEtable *view,
294                               ETable *table)
295 {
296 	g_return_if_fail (GAL_IS_VIEW_ETABLE (view));
297 	g_return_if_fail (E_IS_TABLE (table));
298 
299 	gal_view_etable_detach (view);
300 
301 	view->table = table;
302 
303 	e_table_set_state_object (view->table, view->state);
304 	g_object_ref (view->table);
305 	view->table_state_changed_id = g_signal_connect (
306 		view->table, "state_change",
307 		G_CALLBACK (table_state_changed), view);
308 }
309 
310 void
311 gal_view_etable_attach_tree (GalViewEtable *view,
312                              ETree *tree)
313 {
314 	g_return_if_fail (GAL_IS_VIEW_ETABLE (view));
315 	g_return_if_fail (E_IS_TREE (tree));
316 
317 	gal_view_etable_detach (view);
318 
319 	view->tree = tree;
320 
321 	e_tree_set_state_object (view->tree, view->state);
322 	g_object_ref (view->tree);
323 	view->tree_state_changed_id = g_signal_connect (
324 		view->tree, "state_change",
325 		G_CALLBACK (tree_state_changed), view);
326 }
327 
328 void
329 gal_view_etable_detach (GalViewEtable *view)
330 {
331 	g_return_if_fail (GAL_IS_VIEW_ETABLE (view));
332 
333 	if (view->table != NULL)
334 		detach_table (view);
335 	if (view->tree != NULL)
336 		detach_tree (view);
337 }