evolution-3.6.4/plugins/publish-calendar/publish-format-fb.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  *		David Trowbridge <trowbrds@cs.colorado.edu>
 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 <string.h>
 28 #include <time.h>
 29 #include <glib/gi18n.h>
 30 
 31 #include <shell/e-shell.h>
 32 
 33 #include "publish-format-fb.h"
 34 
 35 static void
 36 free_busy_data_cb (ECalClient *client,
 37                    const GSList *free_busy_ecalcomps,
 38                    GSList **pobjects)
 39 {
 40 	const GSList *iter;
 41 
 42 	g_return_if_fail (pobjects != NULL);
 43 
 44 	for (iter = free_busy_ecalcomps; iter != NULL; iter = iter->next) {
 45 		ECalComponent *comp = iter->data;
 46 
 47 		if (comp)
 48 			*pobjects = g_slist_prepend (*pobjects, g_object_ref (comp));
 49 	}
 50 }
 51 
 52 static gboolean
 53 write_calendar (const gchar *uid,
 54                 GOutputStream *stream,
 55                 gint dur_type,
 56                 gint dur_value,
 57                 GError **error)
 58 {
 59 	EShell *shell;
 60 	ESource *source;
 61 	ESourceRegistry *registry;
 62 	ECalClient *client = NULL;
 63 	GSList *objects = NULL;
 64 	icaltimezone *utc;
 65 	time_t start = time (NULL), end;
 66 	icalcomponent *top_level;
 67 	gchar *email = NULL;
 68 	GSList *users = NULL;
 69 	gboolean res = FALSE;
 70 
 71 	utc = icaltimezone_get_utc_timezone ();
 72 	start = time_day_begin_with_zone (start, utc);
 73 
 74 	switch (dur_type) {
 75 	case FB_DURATION_DAYS:
 76 		end = time_add_day_with_zone (start, dur_value, utc);
 77 		break;
 78 	default:
 79 	case FB_DURATION_WEEKS:
 80 		end = time_add_week_with_zone (start, dur_value, utc);
 81 		break;
 82 	case FB_DURATION_MONTHS:
 83 		end = time_add_month_with_zone (start, dur_value, utc);
 84 		break;
 85 	}
 86 
 87 	shell = e_shell_get_default ();
 88 	registry = e_shell_get_registry (shell);
 89 	source = e_source_registry_ref_source (registry, uid);
 90 
 91 	if (source != NULL) {
 92 		client = e_cal_client_new (source, E_CAL_CLIENT_SOURCE_TYPE_EVENTS, error);
 93 		g_object_unref (source);
 94 	}
 95 	if (!client) {
 96 		if (error && !*error)
 97 			*error = g_error_new (E_CAL_CLIENT_ERROR, E_CAL_CLIENT_ERROR_NO_SUCH_CALENDAR, _("Could not publish calendar: Calendar backend no longer exists"));
 98 		return FALSE;
 99 	}
100 
101 	if (!e_client_open_sync (E_CLIENT (client), TRUE, NULL, error)) {
102 		g_object_unref (client);
103 		return FALSE;
104 	}
105 
106 	if (e_client_get_backend_property_sync (E_CLIENT (client), CAL_BACKEND_PROPERTY_CAL_EMAIL_ADDRESS, &email, NULL, NULL)) {
107 		if (email && *email)
108 			users = g_slist_append (users, email);
109 	}
110 
111 	top_level = e_cal_util_new_top_level ();
112 
113 	g_signal_connect (
114 		client, "free-busy-data",
115 		G_CALLBACK (free_busy_data_cb), &objects);
116 
117 	if (e_cal_client_get_free_busy_sync (client, start, end, users, NULL, error)) {
118 		gchar *ical_string;
119 		GSList *iter;
120 
121 		for (iter = objects; iter; iter = iter->next) {
122 			ECalComponent *comp = iter->data;
123 			icalcomponent *icalcomp = icalcomponent_new_clone (e_cal_component_get_icalcomponent (comp));
124 			icalcomponent_add_component (top_level, icalcomp);
125 		}
126 
127 		ical_string = icalcomponent_as_ical_string_r (top_level);
128 		res = g_output_stream_write_all (stream, ical_string, strlen (ical_string), NULL, NULL, error);
129 
130 		e_cal_client_free_ecalcomp_slist (objects);
131 		g_free (ical_string);
132 	}
133 
134 	if (users)
135 		g_slist_free (users);
136 
137 	g_free (email);
138 	g_object_unref (client);
139 	icalcomponent_free (top_level);
140 
141 	return res;
142 }
143 
144 void
145 publish_calendar_as_fb (GOutputStream *stream,
146                         EPublishUri *uri,
147                         GError **error)
148 {
149 	GSList *l;
150 
151 	/* events */
152 	l = uri->events;
153 	while (l) {
154 		gchar *uid = l->data;
155 		if (!write_calendar (uid, stream, uri->fb_duration_type, uri->fb_duration_value, error))
156 			break;
157 		l = g_slist_next (l);
158 	}
159 }