1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
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 Lesser 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 
17 #include <stdlib.h>
18 #include <libecal/libecal.h>
19 
20 #include "e-test-server-utils.h"
21 
22 static ETestServerClosure cal_closure_sync =
23 	{ E_TEST_SERVER_CALENDAR, NULL, E_CAL_CLIENT_SOURCE_TYPE_EVENTS, FALSE, NULL, FALSE };
24 static ETestServerClosure cal_closure_async =
25 	{ E_TEST_SERVER_CALENDAR, NULL, E_CAL_CLIENT_SOURCE_TYPE_EVENTS, FALSE, NULL, TRUE };
26 
27 static void
print_ecomp(ECalComponent * ecalcomp)28 print_ecomp (ECalComponent *ecalcomp)
29 {
30 	const gchar *uid;
31 	ECalComponentText *summary;
32 
33 	g_return_if_fail (ecalcomp != NULL);
34 
35 	uid = e_cal_component_get_uid (ecalcomp);
36 	summary = e_cal_component_get_summary (ecalcomp);
37 
38 	g_print ("   Component: %s\n", uid ? uid : "no-uid");
39 	g_print ("   Summary: %s\n", summary && e_cal_component_text_get_value (summary) ? e_cal_component_text_get_value (summary) : "NULL");
40 	g_print ("\n");
41 
42 	e_cal_component_text_free (summary);
43 }
44 
45 static void
print_icomp(ICalComponent * icomp)46 print_icomp (ICalComponent *icomp)
47 {
48 	ECalComponent *ecomp;
49 
50 	g_assert_nonnull (icomp);
51 
52 	ecomp = e_cal_component_new_from_icalcomponent (i_cal_component_clone (icomp));
53 	g_assert_nonnull (ecomp);
54 
55 	print_ecomp (ecomp);
56 
57 	g_object_unref (ecomp);
58 }
59 
60 static ICalComponent *
create_object(void)61 create_object (void)
62 {
63 	ICalComponent *icomp;
64 	ICalTime *dtstart, *dtend;
65 
66 	dtstart = i_cal_time_new_current_with_zone (i_cal_timezone_get_utc_timezone ());
67 	dtend = i_cal_time_clone (dtstart);
68 	i_cal_time_adjust (dtend, 0, 1, 0, 0);
69 
70 	icomp = i_cal_component_new (I_CAL_VEVENT_COMPONENT);
71 	i_cal_component_set_summary (icomp, "To-be-sent event summary");
72 	i_cal_component_set_dtstart (icomp, dtstart);
73 	i_cal_component_set_dtend (icomp, dtend);
74 
75 	g_clear_object (&dtstart);
76 	g_clear_object (&dtend);
77 
78 	return icomp;
79 }
80 
81 static void
manage_result(GSList * users,ICalComponent * modified_icomp)82 manage_result (GSList *users,
83 	       ICalComponent *modified_icomp)
84 {
85 	g_print ("Wishes to send to %d users", g_slist_length (users));
86 	if (users) {
87 		GSList *u;
88 
89 		g_print (": ");
90 
91 		for (u = users; u; u = u->next)
92 			g_print ("%s%s", u == users ? "" : ", ", (const gchar *) u->data);
93 	}
94 	g_print ("\n");
95 
96 	if (!modified_icomp)
97 		g_print ("No modified iCalendar component, would send the same\n");
98 	else
99 		print_icomp (modified_icomp);
100 
101 	e_client_util_free_string_slist (users);
102 	g_clear_object (&modified_icomp);
103 }
104 
105 static void
test_send_objects_sync(ETestServerFixture * fixture,gconstpointer user_data)106 test_send_objects_sync (ETestServerFixture *fixture,
107                         gconstpointer user_data)
108 {
109 	ECalClient *cal_client;
110 	GError *error = NULL;
111 	ICalComponent *icomp, *modified_icomp = NULL;
112 	GSList *users = NULL;
113 
114 	cal_client = E_TEST_SERVER_UTILS_SERVICE (fixture, ECalClient);
115 
116 	icomp = create_object ();
117 	if (!e_cal_client_send_objects_sync (cal_client, icomp, E_CAL_OPERATION_FLAG_NONE, &users, &modified_icomp, NULL, &error))
118 		g_error ("send objects sync: %s", error->message);
119 
120 	g_object_unref (icomp);
121 	manage_result (users, modified_icomp);
122 }
123 
124 static void
async_send_result_ready(GObject * source_object,GAsyncResult * result,gpointer user_data)125 async_send_result_ready (GObject *source_object,
126                          GAsyncResult *result,
127                          gpointer user_data)
128 {
129 	ECalClient *cal_client;
130 	GError *error = NULL;
131 	GSList *users = NULL;
132 	ICalComponent *modified_icomp = NULL;
133 	GMainLoop *loop = (GMainLoop *) user_data;
134 
135 	cal_client = E_CAL_CLIENT (source_object);
136 
137 	if (!e_cal_client_send_objects_finish (cal_client, result, &users, &modified_icomp, &error))
138 		g_error ("send objects finish: %s", error->message);
139 
140 	manage_result (users, modified_icomp);
141 	g_main_loop_quit (loop);
142 }
143 
144 static void
test_send_objects_async(ETestServerFixture * fixture,gconstpointer user_data)145 test_send_objects_async (ETestServerFixture *fixture,
146                         gconstpointer user_data)
147 {
148 	ECalClient *cal_client;
149 	ICalComponent *icomp;
150 
151 	cal_client = E_TEST_SERVER_UTILS_SERVICE (fixture, ECalClient);
152 
153 	icomp = create_object ();
154 	g_assert_nonnull (icomp);
155 
156 	e_cal_client_send_objects (cal_client, icomp, E_CAL_OPERATION_FLAG_NONE, NULL, async_send_result_ready, fixture->loop);
157 	g_object_unref (icomp);
158 
159 	g_main_loop_run (fixture->loop);
160 }
161 
162 gint
main(gint argc,gchar ** argv)163 main (gint argc,
164       gchar **argv)
165 {
166 	g_test_init (&argc, &argv, NULL);
167 	g_test_bug_base ("http://bugzilla.gnome.org/");
168 
169 	g_test_add (
170 		"/ECalClient/SendObjects/Sync",
171 		ETestServerFixture,
172 		&cal_closure_sync,
173 		e_test_server_utils_setup,
174 		test_send_objects_sync,
175 		e_test_server_utils_teardown);
176 	g_test_add (
177 		"/ECalClient/SendObjects/Async",
178 		ETestServerFixture,
179 		&cal_closure_async,
180 		e_test_server_utils_setup,
181 		test_send_objects_async,
182 		e_test_server_utils_teardown);
183 
184 	return e_test_server_utils_run (argc, argv);
185 }
186