source: trunk/third/evolution/shell/evolution-storage-set-view.c @ 18142

Revision 18142, 14.8 KB checked in by ghudson, 22 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r18141, which included commits to RCS files with non-trunk default branches.
Line 
1/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2/* evolution-storage-set-view.c
3 *
4 * Copyright (C) 2000, 2001, 2002  Ximian, Inc.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of version 2 of the GNU General Public
8 * License as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public
16 * License along with this program; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
19 *
20 * Author: Ettore Perazzoli
21 */
22
23#ifdef HAVE_CONFIG_H
24#include <config.h>
25#endif
26
27#include "evolution-storage-set-view.h"
28#include "e-shell-constants.h"
29
30#include <gal/util/e-util.h>
31
32
33#define PARENT_TYPE bonobo_object_get_type ()
34static BonoboObjectClass *parent_class = NULL;
35
36struct _EvolutionStorageSetViewPrivate {
37        GtkWidget *storage_set_view_widget;
38        GList *listeners;
39};
40
41
42/* EStorageSet widget callbacks.  */
43
44static void
45storage_set_view_widget_folder_selected_cb (EStorageSetView *storage_set_view_widget,
46                                            const char *uri,
47                                            void *data)
48{
49        EvolutionStorageSetView *storage_set_view;
50        EvolutionStorageSetViewPrivate *priv;
51        GList *p;
52
53        storage_set_view = EVOLUTION_STORAGE_SET_VIEW (data);
54        priv = storage_set_view->priv;
55
56        for (p = priv->listeners; p != NULL; p = p->next) {
57                CORBA_Environment ev;
58                GNOME_Evolution_StorageSetViewListener listener;
59
60                CORBA_exception_init (&ev);
61
62                listener = (GNOME_Evolution_StorageSetViewListener) p->data;
63                GNOME_Evolution_StorageSetViewListener_notifyFolderSelected (listener, uri, &ev);
64
65                /* FIXME: What if we fail?  */
66
67                CORBA_exception_free (&ev);
68        }
69}
70
71static void
72storage_set_view_widget_folder_toggled_cb (EStorageSetView *storage_set_view_widget,
73                                           void *data)
74{
75        EvolutionStorageSetView *storage_set_view;
76        EvolutionStorageSetViewPrivate *priv;
77        GList *p;
78
79        storage_set_view = EVOLUTION_STORAGE_SET_VIEW (data);
80        priv = storage_set_view->priv;
81
82        for (p = priv->listeners; p != NULL; p = p->next) {
83                CORBA_Environment ev;
84                GNOME_Evolution_StorageSetViewListener listener;
85
86                CORBA_exception_init (&ev);
87
88                listener = (GNOME_Evolution_StorageSetViewListener) p->data;
89                GNOME_Evolution_StorageSetViewListener_notifyFolderToggled (listener, &ev);
90
91                /* FIXME: What if we fail? */
92
93                CORBA_exception_free (&ev);
94        }
95}
96
97
98/* Listener handling.  */
99
100static GList *
101find_listener_in_list (GNOME_Evolution_StorageSetViewListener listener,
102                       GList *list)
103{
104        CORBA_Environment ev;
105        GList *p;
106
107        CORBA_exception_init (&ev);
108
109        for (p = list; p != NULL; p = p->next) {
110                GNOME_Evolution_StorageSetViewListener listener_item;
111
112                listener_item = (GNOME_Evolution_StorageSetViewListener) p->data;
113                if (CORBA_Object_is_equivalent (listener, listener_item, &ev))
114                        break;
115        }
116
117        CORBA_exception_free (&ev);
118
119        return p;
120}
121
122static gboolean
123add_listener (EvolutionStorageSetView *storage_set_view,
124              GNOME_Evolution_StorageSetViewListener listener)
125{
126        EvolutionStorageSetViewPrivate *priv;
127        CORBA_Environment ev;
128        const char *current_uri;
129        GNOME_Evolution_StorageSetViewListener copy_of_listener;
130
131        priv = storage_set_view->priv;
132
133        if (find_listener_in_list (listener, priv->listeners) != NULL)
134                return FALSE;
135
136        CORBA_exception_init (&ev);
137
138        copy_of_listener = CORBA_Object_duplicate (listener, &ev);
139        if (ev._major != CORBA_NO_EXCEPTION) {
140                CORBA_exception_free (&ev);
141                return FALSE;
142        }
143
144        priv->listeners = g_list_prepend (priv->listeners, copy_of_listener);
145
146        current_uri = e_storage_set_view_get_current_folder (E_STORAGE_SET_VIEW (priv->storage_set_view_widget));
147        if (current_uri != NULL)
148                GNOME_Evolution_StorageSetViewListener_notifyFolderSelected (listener, current_uri, &ev);
149
150        CORBA_exception_free (&ev);
151
152        return TRUE;
153}
154
155static gboolean
156remove_listener (EvolutionStorageSetView *storage_set_view,
157                 GNOME_Evolution_StorageSetViewListener listener)
158{
159        EvolutionStorageSetViewPrivate *priv;
160        GList *listener_node;
161        CORBA_Environment ev;
162
163        priv = storage_set_view->priv;
164
165        listener_node = find_listener_in_list (listener, priv->listeners);
166        if (listener_node == NULL)
167                return FALSE;
168
169        CORBA_exception_init (&ev);
170        CORBA_Object_release ((CORBA_Object) listener_node->data, &ev);
171        CORBA_exception_free (&ev);
172
173        priv->listeners = g_list_remove_link (priv->listeners, listener_node);
174
175        return TRUE;
176}
177
178
179/* CORBA interface implementation.  */
180
181static POA_GNOME_Evolution_StorageSetView__vepv StorageSetView_vepv;
182
183static POA_GNOME_Evolution_StorageSetView *
184create_servant (void)
185{
186        POA_GNOME_Evolution_StorageSetView *servant;
187        CORBA_Environment ev;
188
189        CORBA_exception_init (&ev);
190
191        servant = (POA_GNOME_Evolution_StorageSetView *) g_new0 (BonoboObjectServant, 1);
192        servant->vepv = &StorageSetView_vepv;
193
194        POA_GNOME_Evolution_StorageSetView__init ((PortableServer_Servant) servant, &ev);
195        if (ev._major != CORBA_NO_EXCEPTION) {
196                g_free (servant);
197                servant = NULL;
198        }
199
200        CORBA_exception_free (&ev);
201
202        return servant;
203}
204
205static void
206impl_StorageSetView_add_listener (PortableServer_Servant servant,
207                                  const GNOME_Evolution_StorageSetViewListener listener,
208                                  CORBA_Environment *ev)
209{
210        BonoboObject *bonobo_object;
211        EvolutionStorageSetView *storage_set_view;
212
213        bonobo_object = bonobo_object_from_servant (servant);
214        storage_set_view = EVOLUTION_STORAGE_SET_VIEW (bonobo_object);
215
216        if (! add_listener (storage_set_view, listener))
217                CORBA_exception_set (ev, CORBA_USER_EXCEPTION,
218                                     ex_GNOME_Evolution_StorageSetView_AlreadyListening, NULL);
219}
220
221static void
222impl_StorageSetView_remove_listener (PortableServer_Servant servant,
223                                     const GNOME_Evolution_StorageSetViewListener listener,
224                                     CORBA_Environment *ev)
225{
226        BonoboObject *bonobo_object;
227        EvolutionStorageSetView *storage_set_view;
228
229        bonobo_object = bonobo_object_from_servant (servant);
230        storage_set_view = EVOLUTION_STORAGE_SET_VIEW (bonobo_object);
231
232        if (! remove_listener (storage_set_view, listener))
233                CORBA_exception_set (ev, CORBA_USER_EXCEPTION,
234                                     ex_GNOME_Evolution_StorageSetView_NotFound, NULL);
235}
236
237static CORBA_boolean
238impl_StorageSetView__get_showFolders (PortableServer_Servant servant,
239                                      CORBA_Environment * ev)
240{
241        BonoboObject *bonobo_object;
242        EvolutionStorageSetView *storage_set_view;
243        EvolutionStorageSetViewPrivate *priv;
244
245        bonobo_object = bonobo_object_from_servant (servant);
246        storage_set_view = EVOLUTION_STORAGE_SET_VIEW (bonobo_object);
247        priv = storage_set_view->priv;
248
249        return e_storage_set_view_get_show_folders (E_STORAGE_SET_VIEW (priv->storage_set_view_widget));
250}
251
252static void
253impl_StorageSetView__set_showFolders (PortableServer_Servant servant,
254                                      const CORBA_boolean value,
255                                      CORBA_Environment * ev)
256{
257        BonoboObject *bonobo_object;
258        EvolutionStorageSetView *storage_set_view;
259        EvolutionStorageSetViewPrivate *priv;
260
261        bonobo_object = bonobo_object_from_servant (servant);
262        storage_set_view = EVOLUTION_STORAGE_SET_VIEW (bonobo_object);
263        priv = storage_set_view->priv;
264
265        e_storage_set_view_set_show_folders (E_STORAGE_SET_VIEW (priv->storage_set_view_widget), value);
266}
267
268static void
269impl_StorageSetView__set_showCheckboxes (PortableServer_Servant servant,
270                                         const CORBA_boolean value,
271                                         CORBA_Environment *ev)
272{
273        BonoboObject *bonobo_object;
274        EvolutionStorageSetView *storage_set_view;
275        EvolutionStorageSetViewPrivate *priv;
276
277        bonobo_object = bonobo_object_from_servant (servant);
278        storage_set_view = EVOLUTION_STORAGE_SET_VIEW (bonobo_object);
279        priv = storage_set_view->priv;
280
281        e_storage_set_view_set_show_checkboxes (E_STORAGE_SET_VIEW (priv->storage_set_view_widget),
282                                                value, NULL, NULL);
283}
284
285static CORBA_boolean
286impl_StorageSetView__get_showCheckboxes (PortableServer_Servant servant,
287                                         CORBA_Environment *ev)
288{
289        BonoboObject *bonobo_object;
290        EvolutionStorageSetView *storage_set_view;
291        EvolutionStorageSetViewPrivate *priv;
292
293        bonobo_object = bonobo_object_from_servant (servant);
294        storage_set_view = EVOLUTION_STORAGE_SET_VIEW (bonobo_object);
295        priv = storage_set_view->priv;
296
297        return e_storage_set_view_get_show_checkboxes (E_STORAGE_SET_VIEW (priv->storage_set_view_widget));
298}
299
300static void
301impl_StorageSetView__set_checkedFolders (PortableServer_Servant servant,
302                                         const GNOME_Evolution_FolderList *list,
303                                         CORBA_Environment *ev)
304{
305        BonoboObject *bonobo_object;
306        EvolutionStorageSetView *storage_set_view;
307        EvolutionStorageSetViewPrivate *priv;
308        GList *path_list = NULL;
309        int i;
310       
311        bonobo_object = bonobo_object_from_servant (servant);
312        storage_set_view = EVOLUTION_STORAGE_SET_VIEW (bonobo_object);
313        priv = storage_set_view->priv;
314
315        for (i = 0; i < list->_length; i++) {
316                if (strncmp (list->_buffer[i].evolutionUri, "evolution:", 10) != 0)
317                        continue;
318
319                path_list = g_list_append (path_list, g_strdup (list->_buffer[i].evolutionUri + 10));
320        }
321
322        e_storage_set_view_set_checkboxes_list (E_STORAGE_SET_VIEW (priv->storage_set_view_widget),
323                                                path_list);
324
325        e_free_string_list (path_list);
326}
327
328static GNOME_Evolution_FolderList *
329impl_StorageSetView__get_checkedFolders (PortableServer_Servant servant,
330                                         CORBA_Environment *ev)
331{
332        BonoboObject *bonobo_object;
333        EvolutionStorageSetView *storage_set_view;
334        EvolutionStorageSetViewPrivate *priv;
335        EStorageSet *storage_set;
336        GNOME_Evolution_FolderList *return_list;
337        GList *path_list;
338        GList *p;
339        int num_folders;
340        int i;
341
342        bonobo_object = bonobo_object_from_servant (servant);
343        storage_set_view = EVOLUTION_STORAGE_SET_VIEW (bonobo_object);
344        priv = storage_set_view->priv;
345
346        path_list = e_storage_set_view_get_checkboxes_list (E_STORAGE_SET_VIEW (priv->storage_set_view_widget));
347        num_folders = g_list_length (path_list);
348
349        return_list = GNOME_Evolution_FolderList__alloc ();
350        return_list->_maximum = num_folders;
351        return_list->_length  = num_folders;
352        return_list->_buffer  = CORBA_sequence_GNOME_Evolution_Folder_allocbuf (return_list->_maximum);
353
354        storage_set = e_storage_set_view_get_storage_set (E_STORAGE_SET_VIEW (priv->storage_set_view_widget));
355
356        for (p = path_list, i = 0; p != NULL; p = p->next, i ++) {
357                EFolder *folder;
358                const char *path;
359                char *evolution_uri;
360
361                path = (const char *) p->data;
362
363                folder = e_storage_set_get_folder (storage_set, path);
364                if (folder == NULL) {
365                        g_warning ("Cannot find folder -- %s", path);
366
367                        /* Subtract here so that we don't start putting
368                           ininitialised blanks into the CORBA list */
369                        return_list->_length--;
370                        i--;
371                        continue;
372                }
373
374                evolution_uri = g_strconcat (E_SHELL_URI_PREFIX, path, NULL);
375                e_folder_to_corba (folder, evolution_uri, return_list->_buffer + i);
376                g_free (evolution_uri);
377        }
378
379        e_free_string_list (path_list);
380
381        CORBA_sequence_set_release (return_list, TRUE);
382        return return_list;
383}
384
385
386/* GtkObject methods.  */
387
388static void
389impl_destroy (GtkObject *object)
390{
391        EvolutionStorageSetView *storage_set_view;
392        EvolutionStorageSetViewPrivate *priv;
393        CORBA_Environment ev;
394        GList *p;
395
396        storage_set_view = EVOLUTION_STORAGE_SET_VIEW (object);
397        priv = storage_set_view->priv;
398
399        CORBA_exception_init (&ev);
400
401        for (p = priv->listeners; p != NULL; p = p->next) {
402                GNOME_Evolution_StorageSetViewListener listener;
403
404                listener = (GNOME_Evolution_StorageSetViewListener) p->data;
405                CORBA_Object_release (listener, &ev);
406        }
407
408        CORBA_exception_free (&ev);
409
410        g_free (priv);
411
412        if (GTK_OBJECT_CLASS (parent_class)->destroy != NULL)
413                (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
414}
415
416
417static void
418corba_class_init (void)
419{
420        POA_GNOME_Evolution_StorageSetView__vepv *vepv;
421        POA_GNOME_Evolution_StorageSetView__epv *epv;
422        PortableServer_ServantBase__epv *base_epv;
423
424        base_epv = g_new0 (PortableServer_ServantBase__epv, 1);
425        base_epv->_private    = NULL;
426        base_epv->finalize    = NULL;
427        base_epv->default_POA = NULL;
428
429        epv = g_new0 (POA_GNOME_Evolution_StorageSetView__epv, 1);
430        epv->addListener         = impl_StorageSetView_add_listener;
431        epv->removeListener      = impl_StorageSetView_remove_listener;
432        epv->_set_showFolders    = impl_StorageSetView__set_showFolders;
433        epv->_get_showFolders    = impl_StorageSetView__get_showFolders;
434        epv->_set_showCheckboxes = impl_StorageSetView__set_showCheckboxes;
435        epv->_get_showCheckboxes = impl_StorageSetView__get_showCheckboxes;
436        epv->_set_checkedFolders = impl_StorageSetView__set_checkedFolders;
437        epv->_get_checkedFolders = impl_StorageSetView__get_checkedFolders;
438
439        vepv = &StorageSetView_vepv;
440        vepv->_base_epv                          = base_epv;
441        vepv->Bonobo_Unknown_epv                 = bonobo_object_get_epv ();
442        vepv->GNOME_Evolution_StorageSetView_epv = epv;
443}
444
445static void
446class_init (EvolutionStorageSetViewClass *klass)
447{
448        GtkObjectClass *object_class;
449
450        object_class = GTK_OBJECT_CLASS (klass);
451        object_class->destroy = impl_destroy;
452
453        parent_class = gtk_type_class (bonobo_object_get_type ());
454
455        corba_class_init ();
456}
457
458static void
459init (EvolutionStorageSetView *storage_set_view)
460{
461        EvolutionStorageSetViewPrivate *priv;
462
463        priv = g_new (EvolutionStorageSetViewPrivate, 1);
464        priv->storage_set_view_widget = NULL;
465        priv->listeners               = NULL;
466
467        storage_set_view->priv = priv;
468}
469
470
471void
472evolution_storage_set_view_construct (EvolutionStorageSetView *storage_set_view,
473                                      GNOME_Evolution_StorageSetView corba_object,
474                                      EStorageSetView *storage_set_view_widget)
475{
476        EvolutionStorageSetViewPrivate *priv;
477
478        g_return_if_fail (storage_set_view != NULL);
479        g_return_if_fail (EVOLUTION_IS_STORAGE_SET_VIEW (storage_set_view));
480        g_return_if_fail (corba_object != CORBA_OBJECT_NIL);
481        g_return_if_fail (storage_set_view_widget != NULL);
482        g_return_if_fail (E_IS_STORAGE_SET_VIEW (storage_set_view_widget));
483
484        priv = storage_set_view->priv;
485
486        bonobo_object_construct (BONOBO_OBJECT (storage_set_view), corba_object);
487
488        g_assert (priv->storage_set_view_widget == NULL);
489        priv->storage_set_view_widget = GTK_WIDGET (storage_set_view_widget);
490
491        gtk_signal_connect (GTK_OBJECT (priv->storage_set_view_widget), "folder_selected",
492                            GTK_SIGNAL_FUNC (storage_set_view_widget_folder_selected_cb), storage_set_view);
493        gtk_signal_connect (GTK_OBJECT (priv->storage_set_view_widget), "checkboxes_changed",
494                            GTK_SIGNAL_FUNC (storage_set_view_widget_folder_toggled_cb), storage_set_view);
495}
496
497EvolutionStorageSetView *
498evolution_storage_set_view_new (EStorageSetView *storage_set_view_widget)
499{
500        POA_GNOME_Evolution_StorageSetView *servant;
501        GNOME_Evolution_StorageSetView corba_object;
502        EvolutionStorageSetView *new;
503
504        g_return_val_if_fail (storage_set_view_widget != NULL, NULL);
505        g_return_val_if_fail (E_IS_STORAGE_SET_VIEW (storage_set_view_widget), NULL);
506
507        servant = create_servant ();
508        if (servant == NULL)
509                return NULL;
510
511        new = gtk_type_new (evolution_storage_set_view_get_type ());
512
513        corba_object = bonobo_object_activate_servant (BONOBO_OBJECT (new), servant);
514
515        evolution_storage_set_view_construct (new, corba_object, storage_set_view_widget);
516
517        return new;
518}
519
520
521E_MAKE_TYPE (evolution_storage_set_view, "EvolutionStorageSetView", EvolutionStorageSetView, class_init, init, PARENT_TYPE)
Note: See TracBrowser for help on using the repository browser.