buddy: add interface to iterate over buddies
[siplcs.git] / src / core / sipe-cal.c
blob72baa31cb11f63332545569bb9b926e0502b051c
1 /**
2 * @file sipe-cal.c
4 * pidgin-sipe
6 * Copyright (C) 2010-2013 SIPE Project <http://sipe.sourceforge.net/>
7 * Copyright (C) 2009 pier11 <pier11@operamail.com>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
29 #include <stdlib.h>
30 #include <string.h>
31 #include <time.h>
33 #include <glib.h>
35 #include "sipe-backend.h"
36 #include "sipe-buddy.h"
37 #include "sipe-core.h"
38 #include "sipe-core-private.h"
39 #include "sipe-cal.h"
40 #include "sipe-http.h"
41 #include "sipe-nls.h"
42 #include "sipe-ocs2005.h"
43 #include "sipe-ocs2007.h"
44 #include "sipe-schedule.h"
45 #include "sipe-utils.h"
46 #include "sipe-xml.h"
48 /* Calendar backends */
49 #ifdef _WIN32
50 #include "sipe-domino.h"
51 #endif
52 #include "sipe-ews.h"
54 #define TIME_NULL (time_t)-1
55 #define IS(time) (time != TIME_NULL)
58 http://msdn.microsoft.com/en-us/library/aa565001.aspx
60 <?xml version="1.0"?>
61 <WorkingHours xmlns="http://schemas.microsoft.com/exchange/services/2006/types">
62 <TimeZone>
63 <Bias>480</Bias>
64 <StandardTime>
65 <Bias>0</Bias>
66 <Time>02:00:00</Time>
67 <DayOrder>1</DayOrder>
68 <Month>11</Month>
69 <DayOfWeek>Sunday</DayOfWeek>
70 </StandardTime>
71 <DaylightTime>
72 <Bias>-60</Bias>
73 <Time>02:00:00</Time>
74 <DayOrder>2</DayOrder>
75 <Month>3</Month>
76 <DayOfWeek>Sunday</DayOfWeek>
77 </DaylightTime>
78 </TimeZone>
79 <WorkingPeriodArray>
80 <WorkingPeriod>
81 <DayOfWeek>Monday Tuesday Wednesday Thursday Friday</DayOfWeek>
82 <StartTimeInMinutes>600</StartTimeInMinutes>
83 <EndTimeInMinutes>1140</EndTimeInMinutes>
84 </WorkingPeriod>
85 </WorkingPeriodArray>
86 </WorkingHours>
88 Desc:
89 <StandardTime>
90 <Bias>int</Bias>
91 <Time>string</Time>
92 <DayOrder>short</DayOrder>
93 <Month>short</Month>
94 <DayOfWeek>Sunday or Monday or Tuesday or Wednesday or Thursday or Friday or Saturday</DayOfWeek>
95 <Year>string</Year>
96 </StandardTime>
99 struct sipe_cal_std_dst {
100 int bias; /* Ex.: -60 */
101 gchar *time; /* hh:mm:ss, 02:00:00 */
102 int day_order; /* 1..5 */
103 int month; /* 1..12 */
104 gchar *day_of_week; /* Sunday or Monday or Tuesday or Wednesday or Thursday or Friday or Saturday */
105 gchar *year; /* YYYY */
107 time_t switch_time;
110 struct sipe_cal_working_hours {
111 int bias; /* Ex.: 480 */
112 struct sipe_cal_std_dst std; /* StandardTime */
113 struct sipe_cal_std_dst dst; /* DaylightTime */
114 gchar *days_of_week; /* Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday separated by space */
115 int start_time; /* 0...1440 */
116 int end_time; /* 0...1440 */
118 gchar *tz; /* aggregated timezone string as in TZ environment variable.
119 Ex.: TST+8TDT+7,M3.2.0/02:00:00,M11.1.0/02:00:00 */
120 /** separate simple strings for Windows platform as the proper TZ does not work there.
121 * anyway, dynamic timezones would't work with just TZ
123 gchar *tz_std; /* Ex.: TST8 */
124 gchar *tz_dst; /* Ex.: TDT7 */
127 /* not for translation, a part of XML Schema definitions */
128 static const char *wday_names[] = {"Sunday",
129 "Monday",
130 "Tuesday",
131 "Wednesday",
132 "Thursday",
133 "Friday",
134 "Saturday"};
135 static int
136 sipe_cal_get_wday(char *wday_name)
138 int i;
140 if (!wday_name) return -1;
142 for (i = 0; i < 7; i++) {
143 if (sipe_strequal(wday_names[i], wday_name)) {
144 return i;
148 return -1;
151 void
152 sipe_cal_event_free(struct sipe_cal_event* cal_event)
154 if (!cal_event) return;
156 g_free(cal_event->subject);
157 g_free(cal_event->location);
158 g_free(cal_event);
161 void
162 sipe_cal_events_free(GSList *cal_events)
164 if (!cal_events) return;
165 sipe_utils_slist_free_full(cal_events,
166 (GDestroyNotify) sipe_cal_event_free);
169 void
170 sipe_cal_calendar_free(struct sipe_calendar *cal)
172 if (cal) {
173 g_free(cal->email);
174 g_free(cal->legacy_dn);
175 g_free(cal->as_url);
176 g_free(cal->oof_url);
177 g_free(cal->oab_url);
178 g_free(cal->domino_url);
179 g_free(cal->oof_state);
180 g_free(cal->oof_note);
181 g_free(cal->free_busy);
182 g_free(cal->working_hours_xml_str);
184 sipe_cal_events_free(cal->cal_events);
186 if (cal->request)
187 sipe_http_request_cancel(cal->request);
188 sipe_http_session_close(cal->session);
190 g_free(cal);
194 void
195 sipe_cal_calendar_init(struct sipe_core_private *sipe_private)
197 if (!sipe_private->calendar) {
198 struct sipe_calendar *cal;
199 const char *value;
201 sipe_private->calendar = cal = g_new0(struct sipe_calendar, 1);
202 cal->sipe_private = sipe_private;
204 cal->email = g_strdup(sipe_private->email);
206 /* user specified a service URL? */
207 value = sipe_backend_setting(SIPE_CORE_PUBLIC, SIPE_SETTING_EMAIL_URL);
208 if (!is_empty(value)) {
209 cal->as_url = g_strdup(value);
210 cal->oof_url = g_strdup(value);
211 cal->domino_url = g_strdup(value);
214 return;
218 char *
219 sipe_cal_event_describe(struct sipe_cal_event* cal_event)
221 GString* str = g_string_new(NULL);
222 const char *status = "";
224 switch(cal_event->cal_status) {
225 case SIPE_CAL_FREE: status = "SIPE_CAL_FREE"; break;
226 case SIPE_CAL_TENTATIVE: status = "SIPE_CAL_TENTATIVE"; break;
227 case SIPE_CAL_BUSY: status = "SIPE_CAL_BUSY"; break;
228 case SIPE_CAL_OOF: status = "SIPE_CAL_OOF"; break;
229 case SIPE_CAL_NO_DATA: status = "SIPE_CAL_NO_DATA"; break;
232 g_string_append_printf(str, "\t%s: %s", "start_time",
233 IS(cal_event->start_time) ? asctime(localtime(&cal_event->start_time)) : "\n");
234 g_string_append_printf(str, "\t%s: %s", "end_time ",
235 IS(cal_event->end_time) ? asctime(localtime(&cal_event->end_time)) : "\n");
236 g_string_append_printf(str, "\t%s: %s\n", "cal_status", status);
237 g_string_append_printf(str, "\t%s: %s\n", "subject ", cal_event->subject ? cal_event->subject : "");
238 g_string_append_printf(str, "\t%s: %s\n", "location ", cal_event->location ? cal_event->location : "");
239 g_string_append_printf(str, "\t%s: %s\n", "is_meeting", cal_event->is_meeting ? "TRUE" : "FALSE");
241 return g_string_free(str, FALSE);
244 char *
245 sipe_cal_event_hash(struct sipe_cal_event* event)
247 /* no end_time as it dos not get published */
248 /* no cal_status as it can change on publication */
249 return g_strdup_printf("<%d><%s><%s><%d>",
250 (int)event->start_time,
251 event->subject ? event->subject : "",
252 event->location ? event->location : "",
253 event->is_meeting);
256 #define ENVIRONMENT_TIMEZONE "TZ"
258 static gchar *
259 sipe_switch_tz(const char *tz)
261 gchar *tz_orig;
263 tz_orig = g_strdup(g_getenv(ENVIRONMENT_TIMEZONE));
264 g_setenv(ENVIRONMENT_TIMEZONE, tz, TRUE);
265 tzset();
266 return(tz_orig);
269 static void
270 sipe_reset_tz(gchar *tz_orig)
272 if (tz_orig) {
273 g_setenv(ENVIRONMENT_TIMEZONE, tz_orig, TRUE);
274 g_free(tz_orig);
275 } else {
276 g_unsetenv(ENVIRONMENT_TIMEZONE);
278 tzset();
282 * Converts struct tm to Epoch time_t considering timezone.
284 * @param tz as defined for TZ environment variable.
286 * Reference: see timegm(3) - Linux man page
288 time_t
289 sipe_mktime_tz(struct tm *tm,
290 const char* tz)
292 time_t ret;
293 gchar *tz_orig;
295 tz_orig = sipe_switch_tz(tz);
296 ret = mktime(tm);
297 sipe_reset_tz(tz_orig);
299 return ret;
303 * Converts Epoch time_t to struct tm considering timezone.
305 * @param tz as defined for TZ environment variable.
307 * Reference: see timegm(3) - Linux man page
309 static struct tm *
310 sipe_localtime_tz(const time_t *time,
311 const char* tz)
313 struct tm *ret;
314 gchar *tz_orig;
316 tz_orig = sipe_switch_tz(tz);
317 ret = localtime(time);
318 sipe_reset_tz(tz_orig);
320 return ret;
323 void
324 sipe_cal_free_working_hours(struct sipe_cal_working_hours *wh)
326 if (!wh) return;
328 g_free(wh->std.time);
329 g_free(wh->std.day_of_week);
330 g_free(wh->std.year);
332 g_free(wh->dst.time);
333 g_free(wh->dst.day_of_week);
334 g_free(wh->dst.year);
336 g_free(wh->days_of_week);
337 g_free(wh->tz);
338 g_free(wh->tz_std);
339 g_free(wh->tz_dst);
340 g_free(wh);
344 * Returns time_t of daylight savings time start/end
345 * in the provided timezone or otherwise
346 * (time_t)-1 if no daylight savings time.
348 static time_t
349 sipe_cal_get_std_dst_time(time_t now,
350 int bias,
351 struct sipe_cal_std_dst* std_dst,
352 struct sipe_cal_std_dst* dst_std)
354 struct tm switch_tm;
355 time_t res = TIME_NULL;
356 struct tm *gm_now_tm;
357 gchar **time_arr;
359 if (std_dst->month == 0) return TIME_NULL;
361 gm_now_tm = gmtime(&now);
362 time_arr = g_strsplit(std_dst->time, ":", 0);
364 switch_tm.tm_sec = atoi(time_arr[2]);
365 switch_tm.tm_min = atoi(time_arr[1]);
366 switch_tm.tm_hour = atoi(time_arr[0]);
367 g_strfreev(time_arr);
368 switch_tm.tm_mday = std_dst->year ? std_dst->day_order : 1 /* to adjust later */ ;
369 switch_tm.tm_mon = std_dst->month - 1;
370 switch_tm.tm_year = std_dst->year ? atoi(std_dst->year) - 1900 : gm_now_tm->tm_year;
371 switch_tm.tm_isdst = 0;
372 /* to set tm_wday */
373 res = sipe_mktime_tz(&switch_tm, "UTC");
375 /* if not dynamic, calculate right tm_mday */
376 if (!std_dst->year) {
377 int switch_wday = sipe_cal_get_wday(std_dst->day_of_week);
378 int needed_month;
379 /* get first desired wday in the month */
380 int delta = switch_wday >= switch_tm.tm_wday ? (switch_wday - switch_tm.tm_wday) : (switch_wday + 7 - switch_tm.tm_wday);
381 switch_tm.tm_mday = 1 + delta;
382 /* try nth order */
383 switch_tm.tm_mday += (std_dst->day_order - 1) * 7;
384 needed_month = switch_tm.tm_mon;
385 /* to set settle date if ahead of allowed month dates */
386 res = sipe_mktime_tz(&switch_tm, "UTC");
387 if (needed_month != switch_tm.tm_mon) {
388 /* moving 1 week back to stay within required month */
389 switch_tm.tm_mday -= 7;
390 /* to fix date again */
391 res = sipe_mktime_tz(&switch_tm, "UTC");
394 /* note: bias is taken from "switch to" structure */
395 return res + (bias + dst_std->bias)*60;
398 static void
399 sipe_cal_parse_std_dst(const sipe_xml *xn_std_dst_time,
400 struct sipe_cal_std_dst *std_dst)
402 const sipe_xml *node;
403 gchar *tmp;
405 if (!xn_std_dst_time) return;
406 if (!std_dst) return;
408 <StandardTime>
409 <Bias>0</Bias>
410 <Time>02:00:00</Time>
411 <DayOrder>1</DayOrder>
412 <Month>11</Month>
413 <Year>2009</Year>
414 <DayOfWeek>Sunday</DayOfWeek>
415 </StandardTime>
418 if ((node = sipe_xml_child(xn_std_dst_time, "Bias"))) {
419 std_dst->bias = atoi(tmp = sipe_xml_data(node));
420 g_free(tmp);
423 if ((node = sipe_xml_child(xn_std_dst_time, "Time"))) {
424 std_dst->time = sipe_xml_data(node);
427 if ((node = sipe_xml_child(xn_std_dst_time, "DayOrder"))) {
428 std_dst->day_order = atoi(tmp = sipe_xml_data(node));
429 g_free(tmp);
432 if ((node = sipe_xml_child(xn_std_dst_time, "Month"))) {
433 std_dst->month = atoi(tmp = sipe_xml_data(node));
434 g_free(tmp);
437 if ((node = sipe_xml_child(xn_std_dst_time, "DayOfWeek"))) {
438 std_dst->day_of_week = sipe_xml_data(node);
441 if ((node = sipe_xml_child(xn_std_dst_time, "Year"))) {
442 std_dst->year = sipe_xml_data(node);
446 void
447 sipe_cal_parse_working_hours(const sipe_xml *xn_working_hours,
448 struct sipe_buddy *buddy)
450 const sipe_xml *xn_bias;
451 const sipe_xml *xn_timezone;
452 const sipe_xml *xn_working_period;
453 const sipe_xml *xn_standard_time;
454 const sipe_xml *xn_daylight_time;
455 gchar *tmp;
456 time_t now = time(NULL);
457 struct sipe_cal_std_dst* std;
458 struct sipe_cal_std_dst* dst;
460 if (!xn_working_hours) return;
462 <WorkingHours xmlns="http://schemas.microsoft.com/exchange/services/2006/types">
463 <TimeZone>
464 <Bias>480</Bias>
466 </TimeZone>
467 <WorkingPeriodArray>
468 <WorkingPeriod>
469 <DayOfWeek>Monday Tuesday Wednesday Thursday Friday</DayOfWeek>
470 <StartTimeInMinutes>600</StartTimeInMinutes>
471 <EndTimeInMinutes>1140</EndTimeInMinutes>
472 </WorkingPeriod>
473 </WorkingPeriodArray>
474 </WorkingHours>
476 sipe_cal_free_working_hours(buddy->cal_working_hours);
477 buddy->cal_working_hours = g_new0(struct sipe_cal_working_hours, 1);
479 xn_timezone = sipe_xml_child(xn_working_hours, "TimeZone");
480 xn_bias = sipe_xml_child(xn_timezone, "Bias");
481 if (xn_bias) {
482 buddy->cal_working_hours->bias = atoi(tmp = sipe_xml_data(xn_bias));
483 g_free(tmp);
486 xn_standard_time = sipe_xml_child(xn_timezone, "StandardTime");
487 xn_daylight_time = sipe_xml_child(xn_timezone, "DaylightTime");
489 std = &((*buddy->cal_working_hours).std);
490 dst = &((*buddy->cal_working_hours).dst);
491 sipe_cal_parse_std_dst(xn_standard_time, std);
492 sipe_cal_parse_std_dst(xn_daylight_time, dst);
494 xn_working_period = sipe_xml_child(xn_working_hours, "WorkingPeriodArray/WorkingPeriod");
495 if (xn_working_period) {
496 /* NOTE: this can be NULL! */
497 buddy->cal_working_hours->days_of_week =
498 sipe_xml_data(sipe_xml_child(xn_working_period, "DayOfWeek"));
500 buddy->cal_working_hours->start_time =
501 atoi(tmp = sipe_xml_data(sipe_xml_child(xn_working_period, "StartTimeInMinutes")));
502 g_free(tmp);
504 buddy->cal_working_hours->end_time =
505 atoi(tmp = sipe_xml_data(sipe_xml_child(xn_working_period, "EndTimeInMinutes")));
506 g_free(tmp);
509 std->switch_time = sipe_cal_get_std_dst_time(now, buddy->cal_working_hours->bias, std, dst);
510 dst->switch_time = sipe_cal_get_std_dst_time(now, buddy->cal_working_hours->bias, dst, std);
512 /* TST8TDT7,M3.2.0/02:00:00,M11.1.0/02:00:00 */
513 buddy->cal_working_hours->tz =
514 g_strdup_printf("TST%dTDT%d,M%d.%d.%d/%s,M%d.%d.%d/%s",
515 (buddy->cal_working_hours->bias + buddy->cal_working_hours->std.bias) / 60,
516 (buddy->cal_working_hours->bias + buddy->cal_working_hours->dst.bias) / 60,
518 buddy->cal_working_hours->dst.month,
519 buddy->cal_working_hours->dst.day_order,
520 sipe_cal_get_wday(buddy->cal_working_hours->dst.day_of_week),
521 buddy->cal_working_hours->dst.time,
523 buddy->cal_working_hours->std.month,
524 buddy->cal_working_hours->std.day_order,
525 sipe_cal_get_wday(buddy->cal_working_hours->std.day_of_week),
526 buddy->cal_working_hours->std.time
528 /* TST8 */
529 buddy->cal_working_hours->tz_std =
530 g_strdup_printf("TST%d",
531 (buddy->cal_working_hours->bias + buddy->cal_working_hours->std.bias) / 60);
532 /* TDT7 */
533 buddy->cal_working_hours->tz_dst =
534 g_strdup_printf("TDT%d",
535 (buddy->cal_working_hours->bias + buddy->cal_working_hours->dst.bias) / 60);
538 struct sipe_cal_event*
539 sipe_cal_get_event(GSList *cal_events,
540 time_t time_in_question)
542 GSList *entry = cal_events;
543 struct sipe_cal_event* cal_event;
544 struct sipe_cal_event* res = NULL;
546 if (!cal_events || !IS(time_in_question)) return NULL;
548 while (entry) {
549 cal_event = entry->data;
550 /* event is in the past or in the future */
551 if (cal_event->start_time > time_in_question ||
552 cal_event->end_time <= time_in_question)
554 entry = entry->next;
555 continue;
558 if (!res) {
559 res = cal_event;
560 } else {
561 int res_status = (res->cal_status == SIPE_CAL_NO_DATA) ? -1 : res->cal_status;
562 int cal_status = (cal_event->cal_status == SIPE_CAL_NO_DATA) ? -1 : cal_event->cal_status;
563 if (res_status < cal_status) {
564 res = cal_event;
567 entry = entry->next;
569 return res;
572 static int
573 sipe_cal_get_status0(const gchar *free_busy,
574 time_t cal_start,
575 int granularity,
576 time_t time_in_question,
577 int *index)
579 int res = SIPE_CAL_NO_DATA;
580 int shift;
581 time_t cal_end = cal_start + strlen(free_busy)*granularity*60 - 1;
583 if (!(time_in_question >= cal_start && time_in_question <= cal_end)) return res;
585 shift = (time_in_question - cal_start) / (granularity*60);
586 if (index) {
587 *index = shift;
590 res = free_busy[shift] - '0';
592 return res;
596 * Returns time when current calendar state started
598 static time_t
599 sipe_cal_get_since_time(const gchar *free_busy,
600 time_t calStart,
601 int granularity,
602 int index,
603 int current_state)
605 int i;
607 if ((index < 0) || ((size_t)(index + 1) > strlen(free_busy))) return 0;
609 for (i = index; i >= 0; i--) {
610 int temp_status = free_busy[i] - '0';
612 if (current_state != temp_status) {
613 return calStart + (i + 1)*granularity*60;
616 if (i == 0) return calStart;
619 return 0;
621 static char*
622 sipe_cal_get_free_busy(struct sipe_buddy *buddy);
625 sipe_cal_get_status(struct sipe_buddy *buddy,
626 time_t time_in_question,
627 time_t *since)
629 time_t cal_start;
630 const char* free_busy;
631 int ret = SIPE_CAL_NO_DATA;
632 time_t state_since;
633 int index = -1;
635 if (!buddy || !buddy->cal_start_time || !buddy->cal_granularity) {
636 SIPE_DEBUG_INFO("sipe_cal_get_status: no calendar data1 for %s, exiting",
637 buddy ? (buddy->name ? buddy->name : "") : "");
638 return SIPE_CAL_NO_DATA;
641 if (!(free_busy = sipe_cal_get_free_busy(buddy))) {
642 SIPE_DEBUG_INFO("sipe_cal_get_status: no calendar data2 for %s, exiting", buddy->name);
643 return SIPE_CAL_NO_DATA;
645 SIPE_DEBUG_INFO("sipe_cal_get_description: buddy->cal_free_busy=\n%s", free_busy);
647 cal_start = sipe_utils_str_to_time(buddy->cal_start_time);
649 ret = sipe_cal_get_status0(free_busy,
650 cal_start,
651 buddy->cal_granularity,
652 time_in_question,
653 &index);
654 state_since = sipe_cal_get_since_time(free_busy,
655 cal_start,
656 buddy->cal_granularity,
657 index,
658 ret);
660 if (since) *since = state_since;
661 return ret;
664 static time_t
665 sipe_cal_get_switch_time(const gchar *free_busy,
666 time_t calStart,
667 int granularity,
668 int index,
669 int current_state,
670 int *to_state)
672 size_t i;
673 time_t ret = TIME_NULL;
675 if ((index < 0) || ((size_t) (index + 1) > strlen(free_busy))) {
676 *to_state = SIPE_CAL_NO_DATA;
677 return ret;
680 for (i = index + 1; i < strlen(free_busy); i++) {
681 int temp_status = free_busy[i] - '0';
683 if (current_state != temp_status) {
684 *to_state = temp_status;
685 return calStart + i*granularity*60;
689 return ret;
692 static const char*
693 sipe_cal_get_tz(struct sipe_cal_working_hours *wh,
694 time_t time_in_question)
696 time_t dst_switch_time = (*wh).dst.switch_time;
697 time_t std_switch_time = (*wh).std.switch_time;
698 gboolean is_dst = FALSE;
700 /* No daylight savings */
701 if (dst_switch_time == TIME_NULL) {
702 return wh->tz_std;
705 if (dst_switch_time < std_switch_time) { /* North hemosphere - Europe, US */
706 if (time_in_question >= dst_switch_time && time_in_question < std_switch_time) {
707 is_dst = TRUE;
709 } else { /* South hemisphere - Australia */
710 if (time_in_question >= dst_switch_time || time_in_question < std_switch_time) {
711 is_dst = TRUE;
715 if (is_dst) {
716 return wh->tz_dst;
717 } else {
718 return wh->tz_std;
722 static time_t
723 sipe_cal_mktime_of_day(struct tm *sample_today_tm,
724 const int shift_minutes,
725 const char *tz)
727 sample_today_tm->tm_sec = 0;
728 sample_today_tm->tm_min = shift_minutes % 60;
729 sample_today_tm->tm_hour = shift_minutes / 60;
731 return sipe_mktime_tz(sample_today_tm, tz);
735 * Returns work day start and end in Epoch time
736 * considering the initial values are provided
737 * in contact's local time zone.
739 static void
740 sipe_cal_get_today_work_hours(struct sipe_cal_working_hours *wh,
741 time_t *start,
742 time_t *end,
743 time_t *next_start)
745 time_t now = time(NULL);
746 const char *tz = sipe_cal_get_tz(wh, now);
747 struct tm *remote_now_tm = sipe_localtime_tz(&now, tz);
749 if (!(wh->days_of_week && strstr(wh->days_of_week, wday_names[remote_now_tm->tm_wday]))) {
750 /* not a work day */
751 *start = TIME_NULL;
752 *end = TIME_NULL;
753 *next_start = TIME_NULL;
754 return;
757 *end = sipe_cal_mktime_of_day(remote_now_tm, wh->end_time, tz);
759 if (now < *end) {
760 *start = sipe_cal_mktime_of_day(remote_now_tm, wh->start_time, tz);
761 *next_start = TIME_NULL;
762 } else { /* calculate start of tomorrow's work day if any */
763 time_t tom = now + 24*60*60;
764 struct tm *remote_tom_tm = sipe_localtime_tz(&tom, sipe_cal_get_tz(wh, tom));
766 if (!(wh->days_of_week && strstr(wh->days_of_week, wday_names[remote_tom_tm->tm_wday]))) {
767 /* not a work day */
768 *next_start = TIME_NULL;
771 *next_start = sipe_cal_mktime_of_day(remote_tom_tm, wh->start_time, sipe_cal_get_tz(wh, tom));
772 *start = TIME_NULL;
776 static int
777 sipe_cal_is_in_work_hours(const time_t time_in_question,
778 const time_t start,
779 const time_t end)
781 return !((time_in_question >= end) || (IS(start) && time_in_question < start));
785 * Returns time closest to now. Choses only from times ahead of now.
786 * Returns TIME_NULL otherwise.
788 static time_t
789 sipe_cal_get_until(const time_t now,
790 const time_t switch_time,
791 const time_t start,
792 const time_t end,
793 const time_t next_start)
795 time_t ret = TIME_NULL;
796 int min_diff = now - ret;
798 if (IS(switch_time) && switch_time > now && (switch_time - now) < min_diff) {
799 min_diff = switch_time - now;
800 ret = switch_time;
802 if (IS(start) && start > now && (start - now) < min_diff) {
803 min_diff = start - now;
804 ret = start;
806 if (IS(end) && end > now && (end - now) < min_diff) {
807 min_diff = end - now;
808 ret = end;
810 if (IS(next_start) && next_start > now && (next_start - now) < min_diff) {
811 min_diff = next_start - now;
812 ret = next_start;
814 return ret;
817 static char*
818 sipe_cal_get_free_busy(struct sipe_buddy *buddy)
820 /* do lazy decode if necessary */
821 if (!buddy->cal_free_busy && buddy->cal_free_busy_base64) {
822 gsize cal_dec64_len;
823 guchar *cal_dec64;
824 gsize i;
825 int j = 0;
827 cal_dec64 = g_base64_decode(buddy->cal_free_busy_base64, &cal_dec64_len);
829 buddy->cal_free_busy = g_malloc0(cal_dec64_len * 4 + 1);
831 http://msdn.microsoft.com/en-us/library/dd941537%28office.13%29.aspx
832 00, Free (Fr)
833 01, Tentative (Te)
834 10, Busy (Bu)
835 11, Out of facility (Oo)
837 http://msdn.microsoft.com/en-us/library/aa566048.aspx
838 0 Free
839 1 Tentative
840 2 Busy
841 3 Out of Office (OOF)
842 4 No data
844 for (i = 0; i < cal_dec64_len; i++) {
845 #define TWO_BIT_MASK 0x03
846 char tmp = cal_dec64[i];
847 buddy->cal_free_busy[j++] = (tmp & TWO_BIT_MASK) + '0';
848 buddy->cal_free_busy[j++] = ((tmp >> 2) & TWO_BIT_MASK) + '0';
849 buddy->cal_free_busy[j++] = ((tmp >> 4) & TWO_BIT_MASK) + '0';
850 buddy->cal_free_busy[j++] = ((tmp >> 6) & TWO_BIT_MASK) + '0';
852 buddy->cal_free_busy[j++] = '\0';
853 g_free(cal_dec64);
856 return buddy->cal_free_busy;
859 char *
860 sipe_cal_get_freebusy_base64(const char* freebusy_hex)
862 guint i = 0;
863 guint j = 0;
864 guint shift_factor = 0;
865 guint len, res_len;
866 guchar *res;
867 gchar *res_base64;
869 if (!freebusy_hex) return NULL;
871 len = strlen(freebusy_hex);
872 res_len = len / 4 + 1;
873 res = g_malloc0(res_len);
874 while (i < len) {
875 res[j] |= (freebusy_hex[i++] - '0') << shift_factor;
876 shift_factor += 2;
877 if (shift_factor == 8) {
878 shift_factor = 0;
879 j++;
883 res_base64 = g_base64_encode(res, shift_factor ? res_len : res_len - 1);
884 g_free(res);
885 return res_base64;
888 char *
889 sipe_cal_get_description(struct sipe_buddy *buddy)
891 time_t cal_start;
892 time_t cal_end;
893 int current_cal_state;
894 time_t now = time(NULL);
895 time_t start = TIME_NULL;
896 time_t end = TIME_NULL;
897 time_t next_start = TIME_NULL;
898 time_t switch_time;
899 int to_state = SIPE_CAL_NO_DATA;
900 time_t until = TIME_NULL;
901 int index = 0;
902 gboolean has_working_hours = (buddy->cal_working_hours != NULL);
903 const char *free_busy;
904 const char *cal_states[] = {_("Free"),
905 _("Tentative"),
906 _("Busy"),
907 _("Out of office"),
908 _("No data")};
910 if (buddy->cal_granularity != 15) {
911 SIPE_DEBUG_INFO("sipe_cal_get_description: granularity %d is unsupported, exiting.", buddy->cal_granularity);
912 return NULL;
915 /* to lazy load if needed */
916 free_busy = sipe_cal_get_free_busy(buddy);
917 SIPE_DEBUG_INFO("sipe_cal_get_description: buddy->cal_free_busy=\n%s", free_busy ? free_busy : "");
919 if (!buddy->cal_free_busy || !buddy->cal_granularity || !buddy->cal_start_time) {
920 SIPE_DEBUG_INFO_NOFORMAT("sipe_cal_get_description: no calendar data, exiting");
921 return NULL;
924 cal_start = sipe_utils_str_to_time(buddy->cal_start_time);
925 cal_end = cal_start + 60 * (buddy->cal_granularity) * strlen(buddy->cal_free_busy);
927 current_cal_state = sipe_cal_get_status0(free_busy, cal_start, buddy->cal_granularity, time(NULL), &index);
928 if (current_cal_state == SIPE_CAL_NO_DATA) {
929 SIPE_DEBUG_INFO_NOFORMAT("sipe_cal_get_description: calendar is undefined for present moment, exiting.");
930 return NULL;
933 switch_time = sipe_cal_get_switch_time(free_busy, cal_start, buddy->cal_granularity, index, current_cal_state, &to_state);
935 SIPE_DEBUG_INFO_NOFORMAT("\n* Calendar *");
936 if (buddy->cal_working_hours) {
937 sipe_cal_get_today_work_hours(buddy->cal_working_hours, &start, &end, &next_start);
939 SIPE_DEBUG_INFO("Remote now timezone : %s", sipe_cal_get_tz(buddy->cal_working_hours, now));
940 SIPE_DEBUG_INFO("std.switch_time(GMT): %s",
941 IS((*buddy->cal_working_hours).std.switch_time) ? asctime(gmtime(&((*buddy->cal_working_hours).std.switch_time))) : "");
942 SIPE_DEBUG_INFO("dst.switch_time(GMT): %s",
943 IS((*buddy->cal_working_hours).dst.switch_time) ? asctime(gmtime(&((*buddy->cal_working_hours).dst.switch_time))) : "");
944 SIPE_DEBUG_INFO("Remote now time : %s",
945 asctime(sipe_localtime_tz(&now, sipe_cal_get_tz(buddy->cal_working_hours, now))));
946 SIPE_DEBUG_INFO("Remote start time : %s",
947 IS(start) ? asctime(sipe_localtime_tz(&start, sipe_cal_get_tz(buddy->cal_working_hours, start))) : "");
948 SIPE_DEBUG_INFO("Remote end time : %s",
949 IS(end) ? asctime(sipe_localtime_tz(&end, sipe_cal_get_tz(buddy->cal_working_hours, end))) : "");
950 SIPE_DEBUG_INFO("Rem. next_start time: %s",
951 IS(next_start) ? asctime(sipe_localtime_tz(&next_start, sipe_cal_get_tz(buddy->cal_working_hours, next_start))) : "");
952 SIPE_DEBUG_INFO("Remote switch time : %s",
953 IS(switch_time) ? asctime(sipe_localtime_tz(&switch_time, sipe_cal_get_tz(buddy->cal_working_hours, switch_time))) : "");
954 } else {
955 SIPE_DEBUG_INFO("Local now time : %s",
956 asctime(localtime(&now)));
957 SIPE_DEBUG_INFO("Local switch time : %s",
958 IS(switch_time) ? asctime(localtime(&switch_time)) : "");
960 SIPE_DEBUG_INFO("Calendar End (GMT) : %s", asctime(gmtime(&cal_end)));
961 SIPE_DEBUG_INFO("current cal state : %s", cal_states[current_cal_state]);
962 SIPE_DEBUG_INFO("switch cal state : %s", cal_states[to_state] );
964 /* Calendar: string calculations */
967 ALGORITHM (don't delete)
968 (c)2009,2010 pier11 <pier11@operamail.com>
970 SOD = Start of Work Day
971 EOD = End of Work Day
972 NSOD = Start of tomorrow's Work Day
973 SW = Calendar status switch time
975 if current_cal_state == Free
976 until = min_t of SOD, EOD, NSOD, SW (min_t(x) = min(x-now) where x>now only)
977 else
978 until = SW
980 if (!until && (cal_period_end > now + 8H))
981 until = cal_period_end
983 if (!until)
984 return "Currently %", current_cal_state
986 if (until - now > 8H)
987 if (current_cal_state == Free && (work_hours && !in work_hours(now)))
988 return "Outside of working hours for next 8 hours"
989 else
990 return "%s for next 8 hours", current_cal_state
992 if (current_cal_state == Free)
993 if (work_hours && until !in work_hours(now))
994 "Not working"
995 else
996 "%s", current_cal_state
997 " until %.2d:%.2d", until
998 else
999 "Currently %", current_cal_state
1000 if (work_hours && until !in work_hours(until))
1001 ". Outside of working hours at at %.2d:%.2d", until
1002 else
1003 ". %s at %.2d:%.2d", to_state, until
1006 if (current_cal_state < 1) { /* Free */
1007 until = sipe_cal_get_until(now, switch_time, start, end, next_start);
1008 } else {
1009 until = switch_time;
1012 if (!IS(until) && (cal_end - now > 8*60*60))
1013 until = cal_end;
1015 if (!IS(until)) {
1016 return g_strdup_printf(_("Currently %s"), cal_states[current_cal_state]);
1019 if (until - now > 8*60*60) {
1020 /* Free & outside work hours */
1021 if (current_cal_state < 1 && has_working_hours && !sipe_cal_is_in_work_hours(now, start, end)) {
1022 return g_strdup(_("Outside of working hours for next 8 hours"));
1023 } else {
1024 return g_strdup_printf(_("%s for next 8 hours"), cal_states[current_cal_state]);
1028 if (current_cal_state < 1) { /* Free */
1029 const char *tmp;
1030 struct tm *until_tm = localtime(&until);
1032 if (has_working_hours && !sipe_cal_is_in_work_hours(now, start, end)) {
1033 tmp = _("Not working");
1034 } else {
1035 tmp = cal_states[current_cal_state];
1037 return g_strdup_printf(_("%s until %.2d:%.2d"), tmp, until_tm->tm_hour, until_tm->tm_min);
1038 } else { /* Tentative or Busy or OOF */
1039 char *tmp;
1040 char *res;
1041 struct tm *until_tm = localtime(&until);
1043 tmp = g_strdup_printf(_("Currently %s"), cal_states[current_cal_state]);
1044 if (has_working_hours && !sipe_cal_is_in_work_hours(until, start, end)) {
1045 res = g_strdup_printf(_("%s. Outside of working hours at %.2d:%.2d"),
1046 tmp, until_tm->tm_hour, until_tm->tm_min);
1047 g_free(tmp);
1048 return res;
1049 } else {
1050 res = g_strdup_printf(_("%s. %s at %.2d:%.2d"), tmp, cal_states[to_state], until_tm->tm_hour, until_tm->tm_min);
1051 g_free(tmp);
1052 return res;
1055 /* End of - Calendar: string calculations */
1058 #define UPDATE_CALENDAR_INTERVAL 30*60 /* 30 min */
1060 void sipe_core_update_calendar(struct sipe_core_public *sipe_public)
1062 SIPE_DEBUG_INFO_NOFORMAT("sipe_core_update_calendar: started.");
1064 /* Do in parallel.
1065 * If failed, the branch will be disabled for subsequent calls.
1066 * Can't rely that user turned the functionality on in account settings.
1068 sipe_ews_update_calendar(SIPE_CORE_PRIVATE);
1069 #ifdef _WIN32
1070 /* @TODO: UNIX integration missing */
1071 sipe_domino_update_calendar(SIPE_CORE_PRIVATE);
1072 #endif
1074 /* schedule repeat */
1075 sipe_schedule_seconds(SIPE_CORE_PRIVATE,
1076 "<+update-calendar>",
1077 NULL,
1078 UPDATE_CALENDAR_INTERVAL,
1079 (sipe_schedule_action)sipe_core_update_calendar,
1080 NULL);
1082 SIPE_DEBUG_INFO_NOFORMAT("sipe_core_update_calendar: finished.");
1085 void sipe_cal_presence_publish(struct sipe_core_private *sipe_private,
1086 gboolean do_publish_calendar)
1088 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
1089 if (do_publish_calendar)
1090 sipe_ocs2007_presence_publish(sipe_private, NULL);
1091 else
1092 sipe_ocs2007_category_publish(sipe_private);
1093 } else {
1094 sipe_ocs2005_presence_publish(sipe_private,
1095 do_publish_calendar);
1099 void sipe_cal_delayed_calendar_update(struct sipe_core_private *sipe_private)
1101 #define UPDATE_CALENDAR_DELAY 1*60 /* 1 min */
1103 /* only start periodic calendar updating if user hasn't disabled it */
1104 if (!SIPE_CORE_PUBLIC_FLAG_IS(DONT_PUBLISH))
1105 sipe_schedule_seconds(sipe_private,
1106 "<+update-calendar>",
1107 NULL,
1108 UPDATE_CALENDAR_DELAY,
1109 (sipe_schedule_action) sipe_core_update_calendar,
1110 NULL);
1114 Local Variables:
1115 mode: c
1116 c-file-style: "bsd"
1117 indent-tabs-mode: t
1118 tab-width: 8
1119 End: