Fix FSF address (Tobias Mueller, #470445)
[platform/upstream/evolution-data-server.git] / calendar / libecal / e-cal-view-listener.c
1 /* Evolution calendar - Live search query listener convenience object
2  *
3  * Copyright (C) 2001 Ximian, Inc.
4  *
5  * Author: Federico Mena-Quintero <federico@ximian.com>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of version 2 of the GNU Lesser General Public
9  * License as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24
25 #include "e-cal-marshal.h"
26 #include "e-cal-view-listener.h"
27
28 \f
29
30 /* Private part of the CalViewListener structure */
31
32 struct _ECalViewListenerPrivate {
33         int dummy;
34 };
35
36 /* Signal IDs */
37 enum {
38         OBJECTS_ADDED,
39         OBJECTS_MODIFIED,
40         OBJECTS_REMOVED,
41         VIEW_PROGRESS,
42         VIEW_DONE,
43         LAST_SIGNAL
44 };
45
46 static guint signals[LAST_SIGNAL] = { 0 };
47
48 static BonoboObjectClass *parent_class;
49
50 /* CORBA method implementations */
51 /* FIXME This is duplicated from cal-listener.c */
52 static ECalendarStatus
53 convert_status (const GNOME_Evolution_Calendar_CallStatus status)
54 {
55         switch (status) {
56         case GNOME_Evolution_Calendar_Success:
57                 return E_CALENDAR_STATUS_OK;
58         case GNOME_Evolution_Calendar_RepositoryOffline:
59                 return E_CALENDAR_STATUS_REPOSITORY_OFFLINE;
60         case GNOME_Evolution_Calendar_PermissionDenied:
61                 return E_CALENDAR_STATUS_PERMISSION_DENIED;
62         case GNOME_Evolution_Calendar_ObjectNotFound:
63                 return E_CALENDAR_STATUS_OBJECT_NOT_FOUND;
64         case GNOME_Evolution_Calendar_ObjectIdAlreadyExists:
65                 return E_CALENDAR_STATUS_OBJECT_ID_ALREADY_EXISTS;
66         case GNOME_Evolution_Calendar_AuthenticationFailed:
67                 return E_CALENDAR_STATUS_AUTHENTICATION_FAILED;
68         case GNOME_Evolution_Calendar_AuthenticationRequired:
69                 return E_CALENDAR_STATUS_AUTHENTICATION_REQUIRED;
70         case GNOME_Evolution_Calendar_UnknownUser:
71                 return E_CALENDAR_STATUS_UNKNOWN_USER;
72         case GNOME_Evolution_Calendar_OtherError:
73         default:
74                 return E_CALENDAR_STATUS_OTHER_ERROR;
75         }
76 }
77
78 /* FIXME This is duplicated from cal-listener.c */
79 static GList *
80 build_object_list (const GNOME_Evolution_Calendar_stringlist *seq)
81 {
82         GList *list;
83         int i;
84
85         list = NULL;
86         for (i = 0; i < seq->_length; i++) {
87                 icalcomponent *comp;
88                 
89                 comp = icalcomponent_new_from_string (seq->_buffer[i]);
90                 if (!comp)
91                         continue;
92                 
93                 list = g_list_prepend (list, comp);
94         }
95
96         return list;
97 }
98
99 static GList *
100 build_id_list (const GNOME_Evolution_Calendar_CalObjIDSeq *seq)
101 {
102         GList *list;
103         int i;
104
105         list = NULL;
106         for (i = 0; i < seq->_length; i++) {
107                 GNOME_Evolution_Calendar_CalObjID *corba_id;
108                 ECalComponentId *id;
109
110                 corba_id = &seq->_buffer[i];
111                 id = g_new (ECalComponentId, 1);
112                 
113                 id->uid = g_strdup (corba_id->uid);
114                 id->rid = g_strdup (corba_id->rid);
115
116                 list = g_list_prepend (list, id);
117         }
118
119         return list;
120 }
121
122 static void
123 impl_notifyObjectsAdded (PortableServer_Servant servant,
124                          const GNOME_Evolution_Calendar_stringlist *objects,
125                          CORBA_Environment *ev)
126 {
127         ECalViewListener *ql;
128         ECalViewListenerPrivate *priv;
129         GList *object_list, *l;
130         
131         ql = E_CAL_VIEW_LISTENER (bonobo_object_from_servant (servant));
132         priv = ql->priv;
133
134         object_list = build_object_list (objects);
135         
136         g_signal_emit (G_OBJECT (ql), signals[OBJECTS_ADDED], 0, object_list);
137
138         for (l = object_list; l; l = l->next)
139                 icalcomponent_free (l->data);
140         g_list_free (object_list);
141 }
142
143 static void
144 impl_notifyObjectsModified (PortableServer_Servant servant,
145                             const GNOME_Evolution_Calendar_stringlist *objects,
146                             CORBA_Environment *ev)
147 {
148         ECalViewListener *ql;
149         ECalViewListenerPrivate *priv;
150         GList *object_list, *l;
151         
152         ql = E_CAL_VIEW_LISTENER (bonobo_object_from_servant (servant));
153         priv = ql->priv;
154
155         object_list = build_object_list (objects);
156         
157         g_signal_emit (G_OBJECT (ql), signals[OBJECTS_MODIFIED], 0, object_list);
158
159         for (l = object_list; l; l = l->next)
160                 icalcomponent_free (l->data);
161         g_list_free (object_list);
162 }
163
164 static void
165 impl_notifyObjectsRemoved (PortableServer_Servant servant,
166                            const GNOME_Evolution_Calendar_CalObjIDSeq *ids,
167                            CORBA_Environment *ev)
168 {
169         ECalViewListener *ql;
170         ECalViewListenerPrivate *priv;
171         GList *id_list, *l;
172         
173         ql = E_CAL_VIEW_LISTENER (bonobo_object_from_servant (servant));
174         priv = ql->priv;
175
176         id_list = build_id_list (ids);
177         
178         g_signal_emit (G_OBJECT (ql), signals[OBJECTS_REMOVED], 0, id_list);
179
180         for (l = id_list; l; l = l->next)
181                 e_cal_component_free_id (l->data);
182         g_list_free (id_list);
183 }
184
185 static void
186 impl_notifyQueryProgress (PortableServer_Servant servant,
187                           const CORBA_char *message,
188                           const CORBA_short percent,
189                           CORBA_Environment *ev)
190 {
191         ECalViewListener *ql;
192         ECalViewListenerPrivate *priv;
193
194         ql = E_CAL_VIEW_LISTENER (bonobo_object_from_servant (servant));
195         priv = ql->priv;
196         
197         g_signal_emit (G_OBJECT (ql), signals[VIEW_PROGRESS], 0, message, percent);
198 }
199
200 static void
201 impl_notifyQueryDone (PortableServer_Servant servant,
202                       const GNOME_Evolution_Calendar_CallStatus status,
203                       CORBA_Environment *ev)
204 {
205         ECalViewListener *ql;
206         ECalViewListenerPrivate *priv;
207
208         ql = E_CAL_VIEW_LISTENER (bonobo_object_from_servant (servant));
209         priv = ql->priv;
210         
211         g_signal_emit (G_OBJECT (ql), signals[VIEW_DONE], 0, convert_status (status));
212 }
213
214 /* Object initialization function for the live search query listener */
215 static void
216 e_cal_view_listener_init (ECalViewListener *ql, ECalViewListenerClass *class)
217 {
218         ECalViewListenerPrivate *priv;
219
220         priv = g_new0 (ECalViewListenerPrivate, 1);
221         ql->priv = priv;
222 }
223
224 /* Finalize handler for the live search query listener */
225 static void
226 e_cal_view_listener_finalize (GObject *object)
227 {
228         ECalViewListener *ql;
229         ECalViewListenerPrivate *priv;
230
231         g_return_if_fail (object != NULL);
232         g_return_if_fail (E_IS_CAL_VIEW_LISTENER (object));
233
234         ql = E_CAL_VIEW_LISTENER (object);
235         priv = ql->priv;
236
237         g_free (priv);
238
239         if (G_OBJECT_CLASS (parent_class)->finalize)
240                 (* G_OBJECT_CLASS (parent_class)->finalize) (object);
241 }
242
243 /* Class initialization function for the live search query listener */
244 static void
245 e_cal_view_listener_class_init (ECalViewListenerClass *klass)
246 {
247         GObjectClass *object_class;
248
249         object_class = (GObjectClass *) klass;
250
251         parent_class = g_type_class_peek_parent (klass);
252
253         object_class->finalize = e_cal_view_listener_finalize;
254
255         klass->epv.notifyObjectsAdded = impl_notifyObjectsAdded;
256         klass->epv.notifyObjectsModified = impl_notifyObjectsModified;
257         klass->epv.notifyObjectsRemoved = impl_notifyObjectsRemoved;
258         klass->epv.notifyQueryProgress = impl_notifyQueryProgress;
259         klass->epv.notifyQueryDone = impl_notifyQueryDone;
260
261         signals[OBJECTS_ADDED] =
262                 g_signal_new ("objects_added",
263                               G_TYPE_FROM_CLASS (klass),
264                               G_SIGNAL_RUN_FIRST,
265                               G_STRUCT_OFFSET (ECalViewListenerClass, objects_added),
266                               NULL, NULL,
267                               g_cclosure_marshal_VOID__POINTER,
268                               G_TYPE_NONE, 1, G_TYPE_POINTER);
269         signals[OBJECTS_MODIFIED] =
270                 g_signal_new ("objects_modified",
271                               G_TYPE_FROM_CLASS (klass),
272                               G_SIGNAL_RUN_FIRST,
273                               G_STRUCT_OFFSET (ECalViewListenerClass, objects_modified),
274                               NULL, NULL,
275                               g_cclosure_marshal_VOID__POINTER,
276                               G_TYPE_NONE, 1, G_TYPE_POINTER);
277         signals[OBJECTS_REMOVED] =
278                 g_signal_new ("objects_removed",
279                               G_TYPE_FROM_CLASS (klass),
280                               G_SIGNAL_RUN_FIRST,
281                               G_STRUCT_OFFSET (ECalViewListenerClass, objects_removed),
282                               NULL, NULL,
283                               g_cclosure_marshal_VOID__POINTER,
284                               G_TYPE_NONE, 1, G_TYPE_POINTER);
285         signals[VIEW_PROGRESS] =
286                 g_signal_new ("view_progress",
287                               G_TYPE_FROM_CLASS (klass),
288                               G_SIGNAL_RUN_FIRST,
289                               G_STRUCT_OFFSET (ECalViewListenerClass, view_progress),
290                               NULL, NULL,
291                               e_cal_marshal_VOID__STRING_INT,
292                               G_TYPE_NONE, 2, G_TYPE_STRING, G_TYPE_INT);
293         signals[VIEW_DONE] =
294                 g_signal_new ("view_done",
295                               G_TYPE_FROM_CLASS (klass),
296                               G_SIGNAL_RUN_FIRST,
297                               G_STRUCT_OFFSET (ECalViewListenerClass, view_done),
298                               NULL, NULL,
299                               g_cclosure_marshal_VOID__INT,
300                               G_TYPE_NONE, 1, G_TYPE_INT);
301 }
302
303 BONOBO_TYPE_FUNC_FULL (ECalViewListener,
304                        GNOME_Evolution_Calendar_CalViewListener,
305                        BONOBO_TYPE_OBJECT,
306                        e_cal_view_listener);
307
308 /**
309  * e_cal_view_listener_new:
310  *
311  * Creates a new ECalViewListener object.
312  *
313  * Return value: the newly created ECalViewListener.
314  */
315 ECalViewListener *
316 e_cal_view_listener_new (void)
317 {
318         ECalViewListener *ql;
319
320         ql = g_object_new (E_TYPE_CAL_VIEW_LISTENER, NULL);
321
322         return ql;
323 }