evolution-3.6.4/calendar/gui/ea-gnome-calendar.c

No issues found

  1 /*
  2  *
  3  * This program is free software; you can redistribute it and/or
  4  * modify it under the terms of the GNU Lesser General Public
  5  * License as published by the Free Software Foundation; either
  6  * version 2 of the License, or (at your option) version 3.
  7  *
  8  * This program is distributed in the hope that it will be useful,
  9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 11  * Lesser General Public License for more details.
 12  *
 13  * You should have received a copy of the GNU Lesser General Public
 14  * License along with the program; if not, see <http://www.gnu.org/licenses/>
 15  *
 16  *
 17  * Authors:
 18  *		Bolian Yin <bolian.yin@sun.com>
 19  *
 20  * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 21  *
 22  */
 23 
 24 #ifdef HAVE_CONFIG_H
 25 #include <config.h>
 26 #endif
 27 
 28 #include "ea-gnome-calendar.h"
 29 #include "e-calendar-view.h"
 30 #include <string.h>
 31 #include <gtk/gtk.h>
 32 #include <glib/gi18n.h>
 33 
 34 static void ea_gnome_calendar_class_init (EaGnomeCalendarClass *klass);
 35 
 36 static gint ea_gnome_calendar_get_n_children (AtkObject * obj);
 37 static AtkObject * ea_gnome_calendar_ref_child (AtkObject *obj, gint i);
 38 
 39 static void ea_gcal_dates_change_cb (GnomeCalendar *gcal, gpointer data);
 40 
 41 static gpointer parent_class = NULL;
 42 
 43 static const gchar *
 44 ea_gnome_calendar_get_name (AtkObject *accessible)
 45 {
 46 	if (accessible->name)
 47 		return accessible->name;
 48 	return _("Gnome Calendar");
 49 }
 50 
 51 static const gchar *
 52 ea_gnome_calendar_get_description (AtkObject *accessible)
 53 {
 54 	if (accessible->description)
 55 		return accessible->description;
 56 	return _("Gnome Calendar");
 57 }
 58 
 59 GType
 60 ea_gnome_calendar_get_type (void)
 61 {
 62 	static GType type = 0;
 63 	AtkObjectFactory *factory;
 64 	GTypeQuery query;
 65 	GType derived_atk_type;
 66 
 67 	if (!type) {
 68 		static GTypeInfo tinfo = {
 69 			sizeof (EaGnomeCalendarClass),
 70 			(GBaseInitFunc) NULL, /* base init */
 71 			(GBaseFinalizeFunc) NULL, /* base finalize */
 72 			(GClassInitFunc) ea_gnome_calendar_class_init, /* class init */
 73 			(GClassFinalizeFunc) NULL, /* class finalize */
 74 			NULL, /* class data */
 75 			sizeof (EaGnomeCalendar), /* instance size */
 76 			0, /* nb preallocs */
 77 			(GInstanceInitFunc) NULL, /* instance init */
 78 			NULL /* value table */
 79 		};
 80 
 81 		/*
 82 		 * Figure out the size of the class and instance
 83 		 * we are run-time deriving from (GailWidget, in this case)
 84 		 */
 85 
 86 		factory = atk_registry_get_factory (
 87 			atk_get_default_registry (),
 88 			GTK_TYPE_WIDGET);
 89 		derived_atk_type = atk_object_factory_get_accessible_type (factory);
 90 		g_type_query (derived_atk_type, &query);
 91 		tinfo.class_size = query.class_size;
 92 		tinfo.instance_size = query.instance_size;
 93 
 94 		type = g_type_register_static (
 95 			derived_atk_type,
 96 			"EaGnomeCalendar", &tinfo, 0);
 97 
 98 	}
 99 
100 	return type;
101 }
102 
103 static void
104 ea_gnome_calendar_class_init (EaGnomeCalendarClass *klass)
105 {
106 	AtkObjectClass *class = ATK_OBJECT_CLASS (klass);
107 
108 	parent_class = g_type_class_peek_parent (klass);
109 
110 	class->get_name = ea_gnome_calendar_get_name;
111 	class->get_description = ea_gnome_calendar_get_description;
112 
113 	class->get_n_children = ea_gnome_calendar_get_n_children;
114 	class->ref_child = ea_gnome_calendar_ref_child;
115 }
116 
117 AtkObject *
118 ea_gnome_calendar_new (GtkWidget *widget)
119 {
120 	GObject *object;
121 	AtkObject *accessible;
122 
123 	g_return_val_if_fail (GNOME_IS_CALENDAR (widget), NULL);
124 
125 	object = g_object_new (EA_TYPE_GNOME_CALENDAR, NULL);
126 
127 	accessible = ATK_OBJECT (object);
128 	atk_object_initialize (accessible, widget);
129 
130 	accessible->role = ATK_ROLE_FILLER;
131 
132 	/* listen on view type change
133 	 */
134 	g_signal_connect (
135 		widget, "dates_shown_changed",
136 		G_CALLBACK (ea_gcal_dates_change_cb), accessible);
137 
138 #ifdef ACC_DEBUG
139 	printf ("EvoAcc: ea-gnome-calendar created: %p\n", (gpointer) accessible);
140 #endif
141 
142 	return accessible;
143 }
144 
145 const gchar *
146 ea_gnome_calendar_get_label_description (GnomeCalendar *gcal)
147 {
148 	GnomeCalendarViewType view_type;
149 	ECalendarView *calendar_view;
150 	ECalModel *model;
151 	icaltimezone *zone;
152 	struct icaltimetype start_tt, end_tt;
153 	time_t start_time, end_time;
154 	struct tm start_tm, end_tm;
155 	static gchar buffer[512];
156 	gchar end_buffer[256];
157 	GnomeCalendarViewType view;
158 
159 	model = gnome_calendar_get_model (gcal);
160 	zone = e_cal_model_get_timezone (model);
161 
162 	view_type = gnome_calendar_get_view (gcal);
163 	calendar_view = gnome_calendar_get_calendar_view (gcal, view_type);
164 
165 	e_calendar_view_get_visible_time_range (
166 		calendar_view, &start_time, &end_time);
167 
168 	start_tt = icaltime_from_timet_with_zone (start_time, FALSE, zone);
169 	start_tm.tm_year = start_tt.year - 1900;
170 	start_tm.tm_mon = start_tt.month - 1;
171 	start_tm.tm_mday = start_tt.day;
172 	start_tm.tm_hour = start_tt.hour;
173 	start_tm.tm_min = start_tt.minute;
174 	start_tm.tm_sec = start_tt.second;
175 	start_tm.tm_isdst = -1;
176 	start_tm.tm_wday = time_day_of_week (
177 		start_tt.day, start_tt.month - 1,
178 		start_tt.year);
179 
180 	/* Take one off end_time so we don't get an extra day. */
181 	end_tt = icaltime_from_timet_with_zone (end_time - 1, FALSE, zone);
182 	end_tm.tm_year = end_tt.year - 1900;
183 	end_tm.tm_mon = end_tt.month - 1;
184 	end_tm.tm_mday = end_tt.day;
185 	end_tm.tm_hour = end_tt.hour;
186 	end_tm.tm_min = end_tt.minute;
187 	end_tm.tm_sec = end_tt.second;
188 	end_tm.tm_isdst = -1;
189 	end_tm.tm_wday = time_day_of_week (
190 		end_tt.day, end_tt.month - 1,
191 		end_tt.year);
192 
193 	view = gnome_calendar_get_view (gcal);
194 
195 	switch (view) {
196 	case GNOME_CAL_DAY_VIEW:
197 	case GNOME_CAL_WORK_WEEK_VIEW:
198 	case GNOME_CAL_WEEK_VIEW:
199 		if (start_tm.tm_year == end_tm.tm_year
200 		    && start_tm.tm_mon == end_tm.tm_mon
201 		    && start_tm.tm_mday == end_tm.tm_mday) {
202 			e_utf8_strftime (
203 				buffer, sizeof (buffer),
204 				_("%A %d %b %Y"), &start_tm);
205 		} else if (start_tm.tm_year == end_tm.tm_year) {
206 			e_utf8_strftime (
207 				buffer, sizeof (buffer),
208 				_("%a %d %b"), &start_tm);
209 			e_utf8_strftime (
210 				end_buffer, sizeof (end_buffer),
211 				_("%a %d %b %Y"), &end_tm);
212 			strcat (buffer, " - ");
213 			strcat (buffer, end_buffer);
214 		} else {
215 			e_utf8_strftime (
216 				buffer, sizeof (buffer),
217 				_("%a %d %b %Y"), &start_tm);
218 			e_utf8_strftime (
219 				end_buffer, sizeof (end_buffer),
220 				_("%a %d %b %Y"), &end_tm);
221 			strcat (buffer, " - ");
222 			strcat (buffer, end_buffer);
223 		}
224 		break;
225 	case GNOME_CAL_MONTH_VIEW:
226 	case GNOME_CAL_LIST_VIEW:
227 		if (start_tm.tm_year == end_tm.tm_year) {
228 			if (start_tm.tm_mon == end_tm.tm_mon) {
229 				if (start_tm.tm_mday == end_tm.tm_mday) {
230 					buffer[0] = '\0';
231 				} else {
232 					e_utf8_strftime (
233 						buffer, sizeof (buffer),
234 						"%d", &start_tm);
235 					strcat (buffer, " - ");
236 				}
237 				e_utf8_strftime (
238 					end_buffer, sizeof (end_buffer),
239 					_("%d %b %Y"), &end_tm);
240 				strcat (buffer, end_buffer);
241 			} else {
242 				e_utf8_strftime (
243 					buffer, sizeof (buffer),
244 					_("%d %b"), &start_tm);
245 				e_utf8_strftime (
246 					end_buffer, sizeof (end_buffer),
247 					_("%d %b %Y"), &end_tm);
248 				strcat (buffer, " - ");
249 				strcat (buffer, end_buffer);
250 			}
251 		} else {
252 			e_utf8_strftime (
253 				buffer, sizeof (buffer),
254 				_("%d %b %Y"), &start_tm);
255 			e_utf8_strftime (
256 				end_buffer, sizeof (end_buffer),
257 				_("%d %b %Y"), &end_tm);
258 			strcat (buffer, " - ");
259 			strcat (buffer, end_buffer);
260 		}
261 		break;
262 	default:
263 		g_return_val_if_reached (NULL);
264 	}
265 	return buffer;
266 }
267 
268 static gint
269 ea_gnome_calendar_get_n_children (AtkObject *obj)
270 {
271 	g_return_val_if_fail (EA_IS_GNOME_CALENDAR (obj), 0);
272 
273 	if (gtk_accessible_get_widget (GTK_ACCESSIBLE (obj)) == NULL)
274 		return -1;
275 
276 	return 2;
277 }
278 
279 static AtkObject *
280 ea_gnome_calendar_ref_child (AtkObject *obj,
281                              gint i)
282 {
283 	AtkObject * child = NULL;
284 	GnomeCalendar * calendarWidget;
285 	GnomeCalendarViewType view_type;
286 	ECalendarView *view;
287 	ECalendar *date_navigator;
288 	GtkWidget *childWidget;
289 	GtkWidget *widget;
290 
291 	g_return_val_if_fail (EA_IS_GNOME_CALENDAR (obj), NULL);
292 	/* valid child index range is [0-3] */
293 	if (i < 0 || i >3)
294 		return NULL;
295 
296 	widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (obj));
297 	if (widget == NULL)
298 		return NULL;
299 
300 	calendarWidget = GNOME_CALENDAR (widget);
301 
302 	switch (i) {
303 	case 0:
304 		/* for the day/week view */
305 		view_type = gnome_calendar_get_view (calendarWidget);
306 		view = gnome_calendar_get_calendar_view (calendarWidget, view_type);
307 		childWidget = GTK_WIDGET (view);
308 		child = gtk_widget_get_accessible (childWidget);
309 		atk_object_set_parent (child, obj);
310 		break;
311 	case 1:
312 		/* for calendar */
313 		date_navigator = gnome_calendar_get_date_navigator (calendarWidget);
314 		childWidget = GTK_WIDGET (date_navigator);
315 		child = gtk_widget_get_accessible (childWidget);
316 		break;
317 	default:
318 		break;
319 	}
320 	if (child)
321 		g_object_ref (child);
322 	return child;
323 }
324 
325 static void
326 ea_gcal_dates_change_cb (GnomeCalendar *gcal,
327                          gpointer data)
328 {
329 	const gchar *new_name;
330 
331 	g_return_if_fail (GNOME_IS_CALENDAR (gcal));
332 	g_return_if_fail (data);
333 	g_return_if_fail (EA_IS_GNOME_CALENDAR (data));
334 
335 	new_name = ea_gnome_calendar_get_label_description (gcal);
336 	atk_object_set_name (ATK_OBJECT (data), new_name);
337 	g_signal_emit_by_name (data, "visible_data_changed");
338 
339 #ifdef ACC_DEBUG
340 	printf ("AccDebug: calendar dates changed, label=%s\n", new_name);
341 #endif
342 }