1 /*
2 *
3 * This program is free software; you can redistribute it and/or modify it
4 * under the terms of the GNU Lesser General Public License as published by
5 * the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful, but
8 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
9 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
10 * for more details.
11 *
12 * You should have received a copy of the GNU Lesser General Public License
13 * along with this 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 #include "evolution-config.h"
24
25 #include "gal-view-instance.h"
26
27 #include <ctype.h>
28 #include <string.h>
29 #include <errno.h>
30 #include <unistd.h>
31 #include <sys/stat.h>
32
33 #include <gtk/gtk.h>
34 #include <libxml/parser.h>
35 #include <glib/gstdio.h>
36 #include <glib/gi18n.h>
37
38 #include <libedataserver/libedataserver.h>
39
40 #include "e-unicode.h"
41 #include "e-misc-utils.h"
42 #include "e-xml-utils.h"
43 #include "gal-view-instance-save-as-dialog.h"
44
45 G_DEFINE_TYPE (GalViewInstance, gal_view_instance, G_TYPE_OBJECT)
46
47 #define d(x)
48
49 enum {
50 DISPLAY_VIEW,
51 CHANGED,
52 LOADED,
53 LAST_SIGNAL
54 };
55
56 static guint gal_view_instance_signals[LAST_SIGNAL] = { 0, };
57
58 static void
gal_view_instance_changed(GalViewInstance * instance)59 gal_view_instance_changed (GalViewInstance *instance)
60 {
61 g_return_if_fail (instance != NULL);
62 g_return_if_fail (GAL_IS_VIEW_INSTANCE (instance));
63
64 g_signal_emit (
65 instance,
66 gal_view_instance_signals[CHANGED], 0);
67 }
68
69 static void
gal_view_instance_display_view(GalViewInstance * instance,GalView * view)70 gal_view_instance_display_view (GalViewInstance *instance,
71 GalView *view)
72 {
73 g_return_if_fail (instance != NULL);
74 g_return_if_fail (GAL_IS_VIEW_INSTANCE (instance));
75
76 g_signal_emit (
77 instance,
78 gal_view_instance_signals[DISPLAY_VIEW], 0,
79 view);
80 }
81
82 static void
save_current_view(GalViewInstance * instance)83 save_current_view (GalViewInstance *instance)
84 {
85 xmlDoc *doc;
86 xmlNode *root;
87
88 doc = xmlNewDoc ((const guchar *)"1.0");
89 root = xmlNewNode (NULL, (const guchar *)"GalViewCurrentView");
90 xmlDocSetRootElement (doc, root);
91
92 if (instance->current_id)
93 e_xml_set_string_prop_by_name (root, (const guchar *)"current_view", instance->current_id);
94 if (instance->current_type)
95 e_xml_set_string_prop_by_name (root, (const guchar *)"current_view_type", instance->current_type);
96
97 if (e_xml_save_file (instance->current_view_filename, doc) == -1)
98 g_warning ("Unable to save view to %s - %s", instance->current_view_filename, g_strerror (errno));
99 xmlFreeDoc (doc);
100 }
101
102 static void
view_changed(GalView * view,GalViewInstance * instance)103 view_changed (GalView *view,
104 GalViewInstance *instance)
105 {
106 if (instance->current_id != NULL) {
107 g_free (instance->current_id);
108 instance->current_id = NULL;
109 save_current_view (instance);
110 gal_view_instance_changed (instance);
111 }
112
113 gal_view_save (view, instance->custom_filename);
114 }
115
116 static void
disconnect_view(GalViewInstance * instance)117 disconnect_view (GalViewInstance *instance)
118 {
119 if (instance->current_view) {
120 if (instance->view_changed_id) {
121 g_signal_handler_disconnect (
122 instance->current_view,
123 instance->view_changed_id);
124 }
125
126 g_object_unref (instance->current_view);
127 }
128 g_free (instance->current_type);
129 g_free (instance->current_title);
130 instance->current_title = NULL;
131 instance->current_type = NULL;
132 instance->view_changed_id = 0;
133 instance->current_view = NULL;
134 }
135
136 static void
connect_view(GalViewInstance * instance,GalView * view)137 connect_view (GalViewInstance *instance,
138 GalView *view)
139 {
140 GalViewClass *view_class;
141
142 if (instance->current_view)
143 disconnect_view (instance);
144 instance->current_view = view;
145
146 view_class = GAL_VIEW_GET_CLASS (view);
147
148 instance->current_title = g_strdup (gal_view_get_title (view));
149 instance->current_type = g_strdup (view_class->type_code);
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
gal_view_instance_dispose(GObject * object)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
gal_view_instance_class_init(GalViewInstanceClass * class)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
gal_view_instance_init(GalViewInstance * instance)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
collection_changed(GalView * view,GalViewInstance * instance)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
load_current_view(GalViewInstance * instance)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 gchar *view_id = g_strdup (gal_view_instance_get_default_view (instance));
276 g_free (instance->current_id);
277 instance->current_id = view_id;
278
279 if (instance->current_id) {
280 gint index = gal_view_collection_get_view_index_by_id (
281 instance->collection,
282 instance->current_id);
283
284 if (index != -1) {
285 view = gal_view_collection_get_view (
286 instance->collection, index);
287 view = gal_view_clone (view);
288 connect_view (instance, view);
289 }
290 }
291 return;
292 }
293
294 root = xmlDocGetRootElement (doc);
295 g_free (instance->current_id);
296 instance->current_id = e_xml_get_string_prop_by_name_with_default (root, (const guchar *)"current_view", NULL);
297
298 if (instance->current_id != NULL) {
299 gint index = gal_view_collection_get_view_index_by_id (
300 instance->collection,
301 instance->current_id);
302
303 if (index != -1) {
304 view = gal_view_collection_get_view (
305 instance->collection, index);
306 view = gal_view_clone (view);
307 }
308 }
309 if (view == NULL) {
310 gchar *type;
311 type = e_xml_get_string_prop_by_name_with_default (root, (const guchar *)"current_view_type", NULL);
312 view = gal_view_collection_load_view_from_file (
313 instance->collection, type,
314 instance->custom_filename);
315 g_free (type);
316 }
317
318 connect_view (instance, view);
319
320 xmlFreeDoc (doc);
321 }
322
323 /**
324 * gal_view_instance_new:
325 * @collection: This %GalViewCollection should be loaded before being passed to this function.
326 * @instance_id: Which instance of this type of object is this (for most of evo, this is the folder id.)
327 *
328 * Create a new %GalViewInstance.
329 *
330 * Return value: The new %GalViewInstance.
331 **/
332 GalViewInstance *
gal_view_instance_new(GalViewCollection * collection,const gchar * instance_id)333 gal_view_instance_new (GalViewCollection *collection,
334 const gchar *instance_id)
335 {
336 GalViewInstance *instance = g_object_new (GAL_TYPE_VIEW_INSTANCE, NULL);
337 if (gal_view_instance_construct (instance, collection, instance_id))
338 return instance;
339 else {
340 g_object_unref (instance);
341 return NULL;
342 }
343 }
344
345 GalViewInstance *
gal_view_instance_construct(GalViewInstance * instance,GalViewCollection * collection,const gchar * instance_id)346 gal_view_instance_construct (GalViewInstance *instance,
347 GalViewCollection *collection,
348 const gchar *instance_id)
349 {
350 gchar *filename;
351 gchar *safe_id;
352 const gchar *user_directory;
353
354 instance->collection = collection;
355 if (collection)
356 g_object_ref (collection);
357 instance->collection_changed_id = g_signal_connect (
358 collection, "changed",
359 G_CALLBACK (collection_changed), instance);
360
361 if (instance_id)
362 instance->instance_id = g_strdup (instance_id);
363 else
364 instance->instance_id = g_strdup ("");
365
366 safe_id = g_strdup (instance->instance_id);
367 e_util_make_safe_filename (safe_id);
368
369 user_directory =
370 gal_view_collection_get_user_directory (instance->collection);
371
372 filename = g_strdup_printf ("custom_view-%s.xml", safe_id);
373 instance->custom_filename =
374 g_build_filename (user_directory, filename, NULL);
375 g_free (filename);
376
377 filename = g_strdup_printf ("current_view-%s.xml", safe_id);
378 instance->current_view_filename =
379 g_build_filename (user_directory, filename, NULL);
380 g_free (filename);
381
382 g_free (safe_id);
383
384 return instance;
385 }
386
387 /* Manipulate the current view. */
388 gchar *
gal_view_instance_get_current_view_id(GalViewInstance * instance)389 gal_view_instance_get_current_view_id (GalViewInstance *instance)
390 {
391 if (instance->current_id && gal_view_collection_get_view_index_by_id (instance->collection, instance->current_id) != -1)
392 return g_strdup (instance->current_id);
393 else
394 return NULL;
395 }
396
397 void
gal_view_instance_set_current_view_id(GalViewInstance * instance,const gchar * view_id)398 gal_view_instance_set_current_view_id (GalViewInstance *instance,
399 const gchar *view_id)
400 {
401 GalView *view;
402 gint index;
403
404 g_return_if_fail (instance != NULL);
405 g_return_if_fail (GAL_IS_VIEW_INSTANCE (instance));
406
407 d (g_print ("%s: view_id set to %s\n", G_STRFUNC, view_id));
408
409 if (instance->current_id && !strcmp (instance->current_id, view_id))
410 return;
411
412 g_free (instance->current_id);
413 instance->current_id = g_strdup (view_id);
414
415 index = gal_view_collection_get_view_index_by_id (instance->collection, view_id);
416 if (index != -1) {
417 view = gal_view_collection_get_view (instance->collection, index);
418 connect_view (instance, gal_view_clone (view));
419 }
420
421 if (instance->loaded)
422 save_current_view (instance);
423 gal_view_instance_changed (instance);
424 }
425
426 GalView *
gal_view_instance_get_current_view(GalViewInstance * instance)427 gal_view_instance_get_current_view (GalViewInstance *instance)
428 {
429 return instance->current_view;
430 }
431
432 void
gal_view_instance_set_custom_view(GalViewInstance * instance,GalView * view)433 gal_view_instance_set_custom_view (GalViewInstance *instance,
434 GalView *view)
435 {
436 g_free (instance->current_id);
437 instance->current_id = NULL;
438
439 view = gal_view_clone (view);
440 connect_view (instance, view);
441 gal_view_save (view, instance->custom_filename);
442 save_current_view (instance);
443 gal_view_instance_changed (instance);
444 }
445
446 static void
dialog_response(GtkWidget * dialog,gint id,GalViewInstance * instance)447 dialog_response (GtkWidget *dialog,
448 gint id,
449 GalViewInstance *instance)
450 {
451 if (id == GTK_RESPONSE_OK) {
452 gal_view_instance_save_as_dialog_save (GAL_VIEW_INSTANCE_SAVE_AS_DIALOG (dialog));
453 }
454 gtk_widget_destroy (dialog);
455 }
456
457 void
gal_view_instance_save_as(GalViewInstance * instance)458 gal_view_instance_save_as (GalViewInstance *instance)
459 {
460 GtkWidget *dialog;
461
462 g_return_if_fail (instance != NULL);
463
464 dialog = gal_view_instance_save_as_dialog_new (instance);
465 g_signal_connect (
466 dialog, "response",
467 G_CALLBACK (dialog_response), instance);
468 gtk_widget_show (dialog);
469 }
470
471 /* This is idempotent. Once it's been called once, the rest of the calls are ignored. */
472 void
gal_view_instance_load(GalViewInstance * instance)473 gal_view_instance_load (GalViewInstance *instance)
474 {
475 if (!instance->loaded) {
476 load_current_view (instance);
477 instance->loaded = TRUE;
478 g_signal_emit (instance, gal_view_instance_signals[LOADED], 0);
479 }
480 }
481
482 /* These only mean anything before gal_view_instance_load is called the first time. */
483 const gchar *
gal_view_instance_get_default_view(GalViewInstance * instance)484 gal_view_instance_get_default_view (GalViewInstance *instance)
485 {
486 if (instance->default_view)
487 return instance->default_view;
488 else
489 return gal_view_collection_get_default_view (instance->collection);
490 }
491
492 void
gal_view_instance_set_default_view(GalViewInstance * instance,const gchar * id)493 gal_view_instance_set_default_view (GalViewInstance *instance,
494 const gchar *id)
495 {
496 g_free (instance->default_view);
497 instance->default_view = g_strdup (id);
498 }
499
500 gboolean
gal_view_instance_exists(GalViewInstance * instance)501 gal_view_instance_exists (GalViewInstance *instance)
502 {
503 struct stat st;
504
505 if (instance->current_view_filename && g_stat (instance->current_view_filename, &st) == 0 && st.st_size > 0 && S_ISREG (st.st_mode))
506 return TRUE;
507 else
508 return FALSE;
509
510 }
511