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

No issues found

  1 /*
  2  *
  3  * This program is free software; you can redistribute it and/or
  4  * modify it under the terms of the GNU Lesser General Public
  5  * License as published by the Free Software Foundation; either
  6  * version 2 of the License, or (at your option) version 3.
  7  *
  8  * This program is distributed in the hope that it will be useful,
  9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 11  * Lesser General Public License for more details.
 12  *
 13  * You should have received a copy of the GNU Lesser General Public
 14  * License along with the program; if not, see <http://www.gnu.org/licenses/>
 15  *
 16  *
 17  * Authors:
 18  *		Chris Lahey <clahey@ximian.com>
 19  *
 20  * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 21  *
 22  */
 23 
 24 #ifdef HAVE_CONFIG_H
 25 #include <config.h>
 26 #endif
 27 
 28 #include <ctype.h>
 29 #include <string.h>
 30 #include <errno.h>
 31 #include <unistd.h>
 32 #include <sys/stat.h>
 33 
 34 #include <gtk/gtk.h>
 35 #include <libxml/parser.h>
 36 #include <glib/gstdio.h>
 37 #include <glib/gi18n.h>
 38 
 39 #include <libedataserver/libedataserver.h>
 40 
 41 #include "e-util/e-util.h"
 42 #include "libevolution-utils/e-xml-utils.h"
 43 #include "e-util/e-unicode.h"
 44 
 45 #include "gal-define-views-dialog.h"
 46 #include "gal-view-instance.h"
 47 #include "gal-view-instance-save-as-dialog.h"
 48 
 49 G_DEFINE_TYPE (GalViewInstance, gal_view_instance, G_TYPE_OBJECT)
 50 
 51 #define d(x)
 52 
 53 enum {
 54 	DISPLAY_VIEW,
 55 	CHANGED,
 56 	LOADED,
 57 	LAST_SIGNAL
 58 };
 59 
 60 static guint gal_view_instance_signals[LAST_SIGNAL] = { 0, };
 61 
 62 static void
 63 gal_view_instance_changed (GalViewInstance *instance)
 64 {
 65 	g_return_if_fail (instance != NULL);
 66 	g_return_if_fail (GAL_IS_VIEW_INSTANCE (instance));
 67 
 68 	g_signal_emit (
 69 		instance,
 70 		gal_view_instance_signals[CHANGED], 0);
 71 }
 72 
 73 static void
 74 gal_view_instance_display_view (GalViewInstance *instance,
 75                                 GalView *view)
 76 {
 77 	g_return_if_fail (instance != NULL);
 78 	g_return_if_fail (GAL_IS_VIEW_INSTANCE (instance));
 79 
 80 	g_signal_emit (
 81 		instance,
 82 		gal_view_instance_signals[DISPLAY_VIEW], 0,
 83 		view);
 84 }
 85 
 86 static void
 87 save_current_view (GalViewInstance *instance)
 88 {
 89 	xmlDoc *doc;
 90 	xmlNode *root;
 91 
 92 	doc = xmlNewDoc ((const guchar *)"1.0");
 93 	root = xmlNewNode (NULL, (const guchar *)"GalViewCurrentView");
 94 	xmlDocSetRootElement (doc, root);
 95 
 96 	if (instance->current_id)
 97 		e_xml_set_string_prop_by_name (root, (const guchar *)"current_view", instance->current_id);
 98 	if (instance->current_type)
 99 		e_xml_set_string_prop_by_name (root, (const guchar *)"current_view_type", instance->current_type);
100 
101 	if (e_xml_save_file (instance->current_view_filename, doc) == -1)
102 		g_warning ("Unable to save view to %s - %s", instance->current_view_filename, g_strerror (errno));
103 	xmlFreeDoc (doc);
104 }
105 
106 static void
107 view_changed (GalView *view,
108               GalViewInstance *instance)
109 {
110 	if (instance->current_id != NULL) {
111 		g_free (instance->current_id);
112 		instance->current_id = NULL;
113 		save_current_view (instance);
114 		gal_view_instance_changed (instance);
115 	}
116 
117 	gal_view_save (view, instance->custom_filename);
118 }
119 
120 static void
121 disconnect_view (GalViewInstance *instance)
122 {
123 	if (instance->current_view) {
124 		if (instance->view_changed_id) {
125 			g_signal_handler_disconnect (
126 				instance->current_view,
127 				instance->view_changed_id);
128 		}
129 
130 		g_object_unref (instance->current_view);
131 	}
132 	g_free (instance->current_type);
133 	g_free (instance->current_title);
134 	instance->current_title = NULL;
135 	instance->current_type = NULL;
136 	instance->view_changed_id = 0;
137 	instance->current_view = NULL;
138 }
139 
140 static void
141 connect_view (GalViewInstance *instance,
142               GalView *view)
143 {
144 	if (instance->current_view)
145 		disconnect_view (instance);
146 	instance->current_view = view;
147 
148 	instance->current_title = g_strdup (gal_view_get_title (view));
149 	instance->current_type = g_strdup (gal_view_get_type_code (view));
150 	instance->view_changed_id = g_signal_connect (
151 		instance->current_view, "changed",
152 		G_CALLBACK (view_changed), instance);
153 
154 	gal_view_instance_display_view (instance, instance->current_view);
155 }
156 
157 static void
158 gal_view_instance_dispose (GObject *object)
159 {
160 	GalViewInstance *instance = GAL_VIEW_INSTANCE (object);
161 
162 	if (instance->collection) {
163 		if (instance->collection_changed_id) {
164 			g_signal_handler_disconnect (
165 				instance->collection,
166 				instance->collection_changed_id);
167 		}
168 		g_object_unref (instance->collection);
169 	}
170 
171 	g_free (instance->instance_id);
172 	g_free (instance->custom_filename);
173 	g_free (instance->current_view_filename);
174 
175 	g_free (instance->current_id);
176 	disconnect_view (instance);
177 
178 	g_free (instance->default_view);
179 
180 	/* Chain up to parent's dispose() method. */
181 	G_OBJECT_CLASS (gal_view_instance_parent_class)->dispose (object);
182 }
183 
184 static void
185 gal_view_instance_class_init (GalViewInstanceClass *class)
186 {
187 	GObjectClass *object_class = G_OBJECT_CLASS (class);
188 
189 	object_class->dispose = gal_view_instance_dispose;
190 
191 	gal_view_instance_signals[DISPLAY_VIEW] = g_signal_new (
192 		"display_view",
193 		G_OBJECT_CLASS_TYPE (object_class),
194 		G_SIGNAL_RUN_LAST,
195 		G_STRUCT_OFFSET (GalViewInstanceClass, display_view),
196 		NULL, NULL,
197 		g_cclosure_marshal_VOID__OBJECT,
198 		G_TYPE_NONE, 1,
199 		GAL_TYPE_VIEW);
200 
201 	gal_view_instance_signals[CHANGED] = g_signal_new (
202 		"changed",
203 		G_OBJECT_CLASS_TYPE (object_class),
204 		G_SIGNAL_RUN_LAST,
205 		G_STRUCT_OFFSET (GalViewInstanceClass, changed),
206 		NULL, NULL,
207 		g_cclosure_marshal_VOID__VOID,
208 		G_TYPE_NONE, 0);
209 
210 	gal_view_instance_signals[LOADED] = g_signal_new (
211 		"loaded",
212 		G_OBJECT_CLASS_TYPE (object_class),
213 		G_SIGNAL_RUN_FIRST,
214 		G_STRUCT_OFFSET (GalViewInstanceClass, loaded),
215 		NULL, NULL,
216 		g_cclosure_marshal_VOID__VOID,
217 		G_TYPE_NONE, 0);
218 
219 	class->display_view = NULL;
220 	class->changed      = NULL;
221 }
222 
223 static void
224 gal_view_instance_init (GalViewInstance *instance)
225 {
226 	instance->collection            = NULL;
227 
228 	instance->instance_id           = NULL;
229 	instance->custom_filename       = NULL;
230 	instance->current_view_filename = NULL;
231 
232 	instance->current_title         = NULL;
233 	instance->current_type          = NULL;
234 	instance->current_id            = NULL;
235 	instance->current_view          = NULL;
236 
237 	instance->view_changed_id       = 0;
238 	instance->collection_changed_id       = 0;
239 
240 	instance->loaded = FALSE;
241 	instance->default_view = NULL;
242 }
243 
244 static void
245 collection_changed (GalView *view,
246                     GalViewInstance *instance)
247 {
248 	if (instance->current_id) {
249 		gchar *view_id = instance->current_id;
250 		instance->current_id = NULL;
251 		gal_view_instance_set_current_view_id (instance, view_id);
252 		g_free (view_id);
253 	}
254 }
255 
256 static void
257 load_current_view (GalViewInstance *instance)
258 {
259 	xmlDoc *doc = NULL;
260 	xmlNode *root;
261 	GalView *view = NULL;
262 
263 	if (g_file_test (instance->current_view_filename, G_FILE_TEST_IS_REGULAR)) {
264 #ifdef G_OS_WIN32
265 		gchar *locale_filename = g_win32_locale_filename_from_utf8 (instance->current_view_filename);
266 		if (locale_filename != NULL)
267 			doc = xmlParseFile (locale_filename);
268 		g_free (locale_filename);
269 #else
270 		doc = xmlParseFile (instance->current_view_filename);
271 #endif
272 	}
273 
274 	if (doc == NULL) {
275 		instance->current_id = g_strdup (gal_view_instance_get_default_view (instance));
276 
277 		if (instance->current_id) {
278 			gint index = gal_view_collection_get_view_index_by_id (
279 				instance->collection,
280 				instance->current_id);
281 
282 			if (index != -1) {
283 				view = gal_view_collection_get_view (
284 					instance->collection, index);
285 				view = gal_view_clone (view);
286 				connect_view (instance, view);
287 			}
288 		}
289 		return;
290 	}
291 
292 	root = xmlDocGetRootElement (doc);
293 	instance->current_id = e_xml_get_string_prop_by_name_with_default (root, (const guchar *)"current_view", NULL);
294 
295 	if (instance->current_id != NULL) {
296 		gint index = gal_view_collection_get_view_index_by_id (
297 			instance->collection,
298 			instance->current_id);
299 
300 		if (index != -1) {
301 			view = gal_view_collection_get_view (
302 				instance->collection, index);
303 			view = gal_view_clone (view);
304 		}
305 	}
306 	if (view == NULL) {
307 		gchar *type;
308 		type = e_xml_get_string_prop_by_name_with_default (root, (const guchar *)"current_view_type", NULL);
309 		view = gal_view_collection_load_view_from_file (
310 			instance->collection, type,
311 			instance->custom_filename);
312 		g_free (type);
313 	}
314 
315 	connect_view (instance, view);
316 
317 	xmlFreeDoc (doc);
318 }
319 
320 /**
321  * gal_view_instance_new:
322  * @collection: This %GalViewCollection should be loaded before being passed to this function.
323  * @instance_id: Which instance of this type of object is this (for most of evo, this is the folder id.)
324  *
325  * Create a new %GalViewInstance.
326  *
327  * Return value: The new %GalViewInstance.
328  **/
329 GalViewInstance *
330 gal_view_instance_new (GalViewCollection *collection,
331                        const gchar *instance_id)
332 {
333 	GalViewInstance *instance = g_object_new (GAL_VIEW_INSTANCE_TYPE, NULL);
334 	if (gal_view_instance_construct (instance, collection, instance_id))
335 		return instance;
336 	else {
337 		g_object_unref (instance);
338 		return NULL;
339 	}
340 }
341 
342 GalViewInstance *
343 gal_view_instance_construct (GalViewInstance *instance,
344                              GalViewCollection *collection,
345                              const gchar *instance_id)
346 {
347 	gchar *filename;
348 	gchar *safe_id;
349 
350 	g_return_val_if_fail (gal_view_collection_loaded (collection), NULL);
351 
352 	instance->collection = collection;
353 	if (collection)
354 		g_object_ref (collection);
355 	instance->collection_changed_id = g_signal_connect (
356 		collection, "changed",
357 		G_CALLBACK (collection_changed), instance);
358 
359 	if (instance_id)
360 		instance->instance_id = g_strdup (instance_id);
361 	else
362 		instance->instance_id = g_strdup ("");
363 
364 	safe_id = g_strdup (instance->instance_id);
365 	e_filename_make_safe (safe_id);
366 
367 	filename = g_strdup_printf ("custom_view-%s.xml", safe_id);
368 	instance->custom_filename = g_build_filename (instance->collection->local_dir, filename, NULL);
369 	g_free (filename);
370 
371 	filename = g_strdup_printf ("current_view-%s.xml", safe_id);
372 	instance->current_view_filename = g_build_filename (instance->collection->local_dir, filename, NULL);
373 	g_free (filename);
374 
375 	g_free (safe_id);
376 
377 	return instance;
378 }
379 
380 /* Manipulate the current view. */
381 gchar *
382 gal_view_instance_get_current_view_id (GalViewInstance *instance)
383 {
384 	if (instance->current_id && gal_view_collection_get_view_index_by_id (instance->collection, instance->current_id) != -1)
385 		return g_strdup (instance->current_id);
386 	else
387 		return NULL;
388 }
389 
390 void
391 gal_view_instance_set_current_view_id (GalViewInstance *instance,
392                                        const gchar *view_id)
393 {
394 	GalView *view;
395 	gint index;
396 
397 	g_return_if_fail (instance != NULL);
398 	g_return_if_fail (GAL_IS_VIEW_INSTANCE (instance));
399 
400 	d (g_print ("%s: view_id set to %s\n", G_STRFUNC, view_id));
401 
402 	if (instance->current_id && !strcmp (instance->current_id, view_id))
403 		return;
404 
405 	g_free (instance->current_id);
406 	instance->current_id = g_strdup (view_id);
407 
408 	index = gal_view_collection_get_view_index_by_id (instance->collection, view_id);
409 	if (index != -1) {
410 		view = gal_view_collection_get_view (instance->collection, index);
411 		connect_view (instance, gal_view_clone (view));
412 	}
413 
414 	if (instance->loaded)
415 		save_current_view (instance);
416 	gal_view_instance_changed (instance);
417 }
418 
419 GalView *
420 gal_view_instance_get_current_view (GalViewInstance *instance)
421 {
422 	return instance->current_view;
423 }
424 
425 void
426 gal_view_instance_set_custom_view (GalViewInstance *instance,
427                                    GalView *view)
428 {
429 	g_free (instance->current_id);
430 	instance->current_id = NULL;
431 
432 	view = gal_view_clone (view);
433 	connect_view (instance, view);
434 	gal_view_save (view, instance->custom_filename);
435 	save_current_view (instance);
436 	gal_view_instance_changed (instance);
437 }
438 
439 static void
440 dialog_response (GtkWidget *dialog,
441                  gint id,
442                  GalViewInstance *instance)
443 {
444 	if (id == GTK_RESPONSE_OK) {
445 		gal_view_instance_save_as_dialog_save (GAL_VIEW_INSTANCE_SAVE_AS_DIALOG (dialog));
446 	}
447 	gtk_widget_destroy (dialog);
448 }
449 
450 void
451 gal_view_instance_save_as (GalViewInstance *instance)
452 {
453 	GtkWidget *dialog;
454 
455 	g_return_if_fail (instance != NULL);
456 
457 	dialog = gal_view_instance_save_as_dialog_new (instance);
458 	g_signal_connect (
459 		dialog, "response",
460 		G_CALLBACK (dialog_response), instance);
461 	gtk_widget_show (dialog);
462 }
463 
464 /* This is idempotent.  Once it's been called once, the rest of the calls are ignored. */
465 void
466 gal_view_instance_load (GalViewInstance *instance)
467 {
468 	if (!instance->loaded) {
469 		load_current_view (instance);
470 		instance->loaded = TRUE;
471 		g_signal_emit (instance, gal_view_instance_signals[LOADED], 0);
472 	}
473 }
474 
475 /* These only mean anything before gal_view_instance_load is called the first time.  */
476 const gchar *
477 gal_view_instance_get_default_view (GalViewInstance *instance)
478 {
479 	if (instance->default_view)
480 		return instance->default_view;
481 	else
482 		return gal_view_collection_get_default_view (instance->collection);
483 }
484 
485 void
486 gal_view_instance_set_default_view (GalViewInstance *instance,
487                                     const gchar *id)
488 {
489 	g_free (instance->default_view);
490 	instance->default_view = g_strdup (id);
491 }
492 
493 gboolean
494 gal_view_instance_exists (GalViewInstance *instance)
495 {
496 	struct stat st;
497 
498 	if (instance->current_view_filename && g_stat (instance->current_view_filename, &st) == 0 && st.st_size > 0 && S_ISREG (st.st_mode))
499 		return TRUE;
500 	else
501 		return FALSE;
502 
503 }