core cleanup: sipe-csta module is purple free
[siplcs.git] / src / core / sipe-cal.c
blobce64f1637d9cf9aec5093b7fbbc1a795e4c34db4
1 /**
2 * @file sipe-cal.c
4 * pidgin-sipe
6 * Copyright (C) 2009 pier11 <pier11@operamail.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
28 #include <stdlib.h>
29 #include <string.h>
30 #include <time.h>
32 #include <glib.h>
34 #include "sip-sec.h"
35 #include "sipe-backend-debug.h"
36 #include "sipe-cal.h"
37 #include "sipe-nls.h"
38 #include "sipe-utils.h"
39 #include "sipe-xml.h"
40 #include "sipe.h"
42 #define TIME_NULL (time_t)-1
43 #define IS(time) (time != TIME_NULL)
46 http://msdn.microsoft.com/en-us/library/aa565001.aspx
48 <?xml version="1.0"?>
49 <WorkingHours xmlns="http://schemas.microsoft.com/exchange/services/2006/types">
50 <TimeZone>
51 <Bias>480</Bias>
52 <StandardTime>
53 <Bias>0</Bias>
54 <Time>02:00:00</Time>
55 <DayOrder>1</DayOrder>
56 <Month>11</Month>
57 <DayOfWeek>Sunday</DayOfWeek>
58 </StandardTime>
59 <DaylightTime>
60 <Bias>-60</Bias>
61 <Time>02:00:00</Time>
62 <DayOrder>2</DayOrder>
63 <Month>3</Month>
64 <DayOfWeek>Sunday</DayOfWeek>
65 </DaylightTime>
66 </TimeZone>
67 <WorkingPeriodArray>
68 <WorkingPeriod>
69 <DayOfWeek>Monday Tuesday Wednesday Thursday Friday</DayOfWeek>
70 <StartTimeInMinutes>600</StartTimeInMinutes>
71 <EndTimeInMinutes>1140</EndTimeInMinutes>
72 </WorkingPeriod>
73 </WorkingPeriodArray>
74 </WorkingHours>
76 Desc:
77 <StandardTime>
78 <Bias>int</Bias>
79 <Time>string</Time>
80 <DayOrder>short</DayOrder>
81 <Month>short</Month>
82 <DayOfWeek>Sunday or Monday or Tuesday or Wednesday or Thursday or Friday or Saturday</DayOfWeek>
83 <Year>string</Year>
84 </StandardTime>
87 struct sipe_cal_std_dst {
88 int bias; /* Ex.: -60 */
89 gchar *time; /* hh:mm:ss, 02:00:00 */
90 int day_order; /* 1..5 */
91 int month; /* 1..12 */
92 gchar *day_of_week; /* Sunday or Monday or Tuesday or Wednesday or Thursday or Friday or Saturday */
93 gchar *year; /* YYYY */
95 time_t switch_time;
98 struct sipe_cal_working_hours {
99 int bias; /* Ex.: 480 */
100 struct sipe_cal_std_dst std; /* StandardTime */
101 struct sipe_cal_std_dst dst; /* DaylightTime */
102 gchar *days_of_week; /* Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday separated by space */
103 int start_time; /* 0...1440 */
104 int end_time; /* 0...1440 */
106 gchar *tz; /* aggregated timezone string as in TZ environment variable.
107 Ex.: TST+8TDT+7,M3.2.0/02:00:00,M11.1.0/02:00:00 */
108 /** separate simple strings for Windows platform as the proper TZ does not work there.
109 * anyway, dynamic timezones would't work with just TZ
111 gchar *tz_std; /* Ex.: TST8 */
112 gchar *tz_dst; /* Ex.: TDT7 */
115 /* not for translation, a part of XML Schema definitions */
116 static const char *wday_names[] = {"Sunday",
117 "Monday",
118 "Tuesday",
119 "Wednesday",
120 "Thursday",
121 "Friday",
122 "Saturday"};
123 static int
124 sipe_cal_get_wday(char *wday_name)
126 int i;
128 if (!wday_name) return -1;
130 for (i = 0; i < 7; i++) {
131 if (sipe_strequal(wday_names[i], wday_name)) {
132 return i;
136 return -1;
139 void
140 sipe_cal_event_free(struct sipe_cal_event* cal_event)
142 if (!cal_event) return;
144 g_free(cal_event->subject);
145 g_free(cal_event->location);
146 g_free(cal_event);
149 char *
150 sipe_cal_event_describe(struct sipe_cal_event* cal_event)
152 GString* str = g_string_new(NULL);
153 const char *status = "";
155 switch(cal_event->cal_status) {
156 case SIPE_CAL_FREE: status = "SIPE_CAL_FREE"; break;
157 case SIPE_CAL_TENTATIVE: status = "SIPE_CAL_TENTATIVE"; break;
158 case SIPE_CAL_BUSY: status = "SIPE_CAL_BUSY"; break;
159 case SIPE_CAL_OOF: status = "SIPE_CAL_OOF"; break;
160 case SIPE_CAL_NO_DATA: status = "SIPE_CAL_NO_DATA"; break;
163 g_string_append_printf(str, "\t%s: %s", "start_time",
164 IS(cal_event->start_time) ? asctime(localtime(&cal_event->start_time)) : "\n");
165 g_string_append_printf(str, "\t%s: %s", "end_time ",
166 IS(cal_event->end_time) ? asctime(localtime(&cal_event->end_time)) : "\n");
167 g_string_append_printf(str, "\t%s: %s\n", "cal_status", status);
168 g_string_append_printf(str, "\t%s: %s\n", "subject ", cal_event->subject ? cal_event->subject : "");
169 g_string_append_printf(str, "\t%s: %s\n", "location ", cal_event->location ? cal_event->location : "");
170 g_string_append_printf(str, "\t%s: %s\n", "is_meeting", cal_event->is_meeting ? "TRUE" : "FALSE");
172 return g_string_free(str, FALSE);
175 char *
176 sipe_cal_event_hash(struct sipe_cal_event* event)
178 /* no end_time as it dos not get published */
179 /* no cal_status as it can change on publication */
180 return g_strdup_printf("<%d><%s><%s><%d>",
181 (int)event->start_time,
182 event->subject ? event->subject : "",
183 event->location ? event->location : "",
184 event->is_meeting);
187 static void
188 sipe_setenv(const char *name,
189 const char *value)
191 #ifdef HAVE_SETENV
192 setenv(name, value, 1);
193 #else
194 int len = strlen(name) + 1 + strlen(value) + 1;
195 char *str = g_malloc0(len);
196 sprintf(str, "%s=%s", name, value);
197 putenv(str);
198 #endif
201 static void
202 sipe_unsetenv(const char *name)
204 #ifdef HAVE_UNSETENV
205 unsetenv(name);
206 #else
207 int len = strlen(name) + 1 + 1;
208 char *str = g_malloc0(len);
209 sprintf(str, "%s=", name);
210 putenv(str);
211 #endif
215 * Converts struct tm to Epoch time_t considering timezone.
217 * @param tz as defined for TZ environment variable.
219 * Reference: see timegm(3) - Linux man page
221 time_t
222 sipe_mktime_tz(struct tm *tm,
223 const char* tz)
225 time_t ret;
226 char *tz_old;
228 tz_old = getenv("TZ");
229 sipe_setenv("TZ", tz);
230 tzset();
232 ret = mktime(tm);
234 if (tz_old) {
235 sipe_setenv("TZ", tz_old);
236 } else {
237 sipe_unsetenv("TZ");
239 tzset();
241 return ret;
245 * Converts Epoch time_t to struct tm considering timezone.
247 * @param tz as defined for TZ environment variable.
249 * Reference: see timegm(3) - Linux man page
251 static struct tm *
252 sipe_localtime_tz(const time_t *time,
253 const char* tz)
255 struct tm *ret;
256 char *tz_old;
258 tz_old = getenv("TZ");
259 sipe_setenv("TZ", tz);
260 tzset();
262 ret = localtime(time);
264 if (tz_old) {
265 sipe_setenv("TZ", tz_old);
266 } else {
267 sipe_unsetenv("TZ");
269 tzset();
271 return ret;
274 void
275 sipe_cal_free_working_hours(struct sipe_cal_working_hours *wh)
277 if (!wh) return;
279 g_free(wh->std.time);
280 g_free(wh->std.day_of_week);
281 g_free(wh->std.year);
283 g_free(wh->dst.time);
284 g_free(wh->dst.day_of_week);
285 g_free(wh->dst.year);
287 g_free(wh->days_of_week);
288 g_free(wh->tz);
289 g_free(wh->tz_std);
290 g_free(wh->tz_dst);
291 g_free(wh);
295 * Returns time_t of daylight savings time start/end
296 * in the provided timezone or otherwise
297 * (time_t)-1 if no daylight savings time.
299 static time_t
300 sipe_cal_get_std_dst_time(time_t now,
301 int bias,
302 struct sipe_cal_std_dst* std_dst,
303 struct sipe_cal_std_dst* dst_std)
305 struct tm switch_tm;
306 time_t res = TIME_NULL;
307 struct tm *gm_now_tm;
308 gchar **time_arr;
310 if (std_dst->month == 0) return TIME_NULL;
312 gm_now_tm = gmtime(&now);
313 time_arr = g_strsplit(std_dst->time, ":", 0);
315 switch_tm.tm_sec = atoi(time_arr[2]);
316 switch_tm.tm_min = atoi(time_arr[1]);
317 switch_tm.tm_hour = atoi(time_arr[0]);
318 g_strfreev(time_arr);
319 switch_tm.tm_mday = std_dst->year ? std_dst->day_order : 1 /* to adjust later */ ;
320 switch_tm.tm_mon = std_dst->month - 1;
321 switch_tm.tm_year = std_dst->year ? atoi(std_dst->year) - 1900 : gm_now_tm->tm_year;
322 switch_tm.tm_isdst = 0;
323 /* to set tm_wday */
324 res = sipe_mktime_tz(&switch_tm, "UTC");
326 /* if not dynamic, calculate right tm_mday */
327 if (!std_dst->year) {
328 int switch_wday = sipe_cal_get_wday(std_dst->day_of_week);
329 int needed_month;
330 /* get first desired wday in the month */
331 int delta = switch_wday >= switch_tm.tm_wday ? (switch_wday - switch_tm.tm_wday) : (switch_wday + 7 - switch_tm.tm_wday);
332 switch_tm.tm_mday = 1 + delta;
333 /* try nth order */
334 switch_tm.tm_mday += (std_dst->day_order - 1) * 7;
335 needed_month = switch_tm.tm_mon;
336 /* to set settle date if ahead of allowed month dates */
337 res = sipe_mktime_tz(&switch_tm, "UTC");
338 if (needed_month != switch_tm.tm_mon) {
339 /* moving 1 week back to stay within required month */
340 switch_tm.tm_mday -= 7;
341 /* to fix date again */
342 res = sipe_mktime_tz(&switch_tm, "UTC");
345 /* note: bias is taken from "switch to" structure */
346 return res + (bias + dst_std->bias)*60;
349 static void
350 sipe_cal_parse_std_dst(const sipe_xml *xn_std_dst_time,
351 struct sipe_cal_std_dst *std_dst)
353 const sipe_xml *node;
354 gchar *tmp;
356 if (!xn_std_dst_time) return;
357 if (!std_dst) return;
359 <StandardTime>
360 <Bias>0</Bias>
361 <Time>02:00:00</Time>
362 <DayOrder>1</DayOrder>
363 <Month>11</Month>
364 <DayOfWeek>Sunday</DayOfWeek>
365 </StandardTime>
368 if ((node = sipe_xml_child(xn_std_dst_time, "Bias"))) {
369 std_dst->bias = atoi(tmp = sipe_xml_data(node));
370 g_free(tmp);
373 if ((node = sipe_xml_child(xn_std_dst_time, "Time"))) {
374 std_dst->time = sipe_xml_data(node);
377 if ((node = sipe_xml_child(xn_std_dst_time, "DayOrder"))) {
378 std_dst->day_order = atoi(tmp = sipe_xml_data(node));
379 g_free(tmp);
382 if ((node = sipe_xml_child(xn_std_dst_time, "Month"))) {
383 std_dst->month = atoi(tmp = sipe_xml_data(node));
384 g_free(tmp);
387 if ((node = sipe_xml_child(xn_std_dst_time, "DayOfWeek"))) {
388 std_dst->day_of_week = sipe_xml_data(node);
391 if ((node = sipe_xml_child(xn_std_dst_time, "Year"))) {
392 std_dst->year = sipe_xml_data(node);
396 void
397 sipe_cal_parse_working_hours(const sipe_xml *xn_working_hours,
398 struct sipe_buddy *buddy)
400 const sipe_xml *xn_bias;
401 const sipe_xml *xn_timezone;
402 const sipe_xml *xn_working_period;
403 const sipe_xml *xn_standard_time;
404 const sipe_xml *xn_daylight_time;
405 gchar *tmp;
406 time_t now = time(NULL);
407 struct sipe_cal_std_dst* std;
408 struct sipe_cal_std_dst* dst;
410 if (!xn_working_hours) return;
412 <WorkingHours xmlns="http://schemas.microsoft.com/exchange/services/2006/types">
413 <TimeZone>
414 <Bias>480</Bias>
416 </TimeZone>
417 <WorkingPeriodArray>
418 <WorkingPeriod>
419 <DayOfWeek>Monday Tuesday Wednesday Thursday Friday</DayOfWeek>
420 <StartTimeInMinutes>600</StartTimeInMinutes>
421 <EndTimeInMinutes>1140</EndTimeInMinutes>
422 </WorkingPeriod>
423 </WorkingPeriodArray>
424 </WorkingHours>
426 sipe_cal_free_working_hours(buddy->cal_working_hours);
427 buddy->cal_working_hours = g_new0(struct sipe_cal_working_hours, 1);
429 xn_timezone = sipe_xml_child(xn_working_hours, "TimeZone");
430 xn_bias = sipe_xml_child(xn_timezone, "Bias");
431 if (xn_bias) {
432 buddy->cal_working_hours->bias = atoi(tmp = sipe_xml_data(xn_bias));
433 g_free(tmp);
436 xn_standard_time = sipe_xml_child(xn_timezone, "StandardTime");
437 xn_daylight_time = sipe_xml_child(xn_timezone, "DaylightTime");
439 std = &((*buddy->cal_working_hours).std);
440 dst = &((*buddy->cal_working_hours).dst);
441 sipe_cal_parse_std_dst(xn_standard_time, std);
442 sipe_cal_parse_std_dst(xn_daylight_time, dst);
444 xn_working_period = sipe_xml_child(xn_working_hours, "WorkingPeriodArray/WorkingPeriod");
445 if (xn_working_period) {
446 buddy->cal_working_hours->days_of_week =
447 sipe_xml_data(sipe_xml_child(xn_working_period, "DayOfWeek"));
449 buddy->cal_working_hours->start_time =
450 atoi(tmp = sipe_xml_data(sipe_xml_child(xn_working_period, "StartTimeInMinutes")));
451 g_free(tmp);
453 buddy->cal_working_hours->end_time =
454 atoi(tmp = sipe_xml_data(sipe_xml_child(xn_working_period, "EndTimeInMinutes")));
455 g_free(tmp);
458 std->switch_time = sipe_cal_get_std_dst_time(now, buddy->cal_working_hours->bias, std, dst);
459 dst->switch_time = sipe_cal_get_std_dst_time(now, buddy->cal_working_hours->bias, dst, std);
461 /* TST8TDT7,M3.2.0/02:00:00,M11.1.0/02:00:00 */
462 buddy->cal_working_hours->tz =
463 g_strdup_printf("TST%dTDT%d,M%d.%d.%d/%s,M%d.%d.%d/%s",
464 (buddy->cal_working_hours->bias + buddy->cal_working_hours->std.bias) / 60,
465 (buddy->cal_working_hours->bias + buddy->cal_working_hours->dst.bias) / 60,
467 buddy->cal_working_hours->dst.month,
468 buddy->cal_working_hours->dst.day_order,
469 sipe_cal_get_wday(buddy->cal_working_hours->dst.day_of_week),
470 buddy->cal_working_hours->dst.time,
472 buddy->cal_working_hours->std.month,
473 buddy->cal_working_hours->std.day_order,
474 sipe_cal_get_wday(buddy->cal_working_hours->std.day_of_week),
475 buddy->cal_working_hours->std.time
477 /* TST8 */
478 buddy->cal_working_hours->tz_std =
479 g_strdup_printf("TST%d",
480 (buddy->cal_working_hours->bias + buddy->cal_working_hours->std.bias) / 60);
481 /* TDT7 */
482 buddy->cal_working_hours->tz_dst =
483 g_strdup_printf("TDT%d",
484 (buddy->cal_working_hours->bias + buddy->cal_working_hours->dst.bias) / 60);
487 struct sipe_cal_event*
488 sipe_cal_get_event(GSList *cal_events,
489 time_t time_in_question)
491 GSList *entry = cal_events;
492 struct sipe_cal_event* cal_event;
493 struct sipe_cal_event* res = NULL;
495 if (!cal_events || !IS(time_in_question)) return NULL;
497 while (entry) {
498 cal_event = entry->data;
499 /* event is in the past or in the future */
500 if (cal_event->start_time > time_in_question ||
501 cal_event->end_time <= time_in_question)
503 entry = entry->next;
504 continue;
507 if (!res) {
508 res = cal_event;
509 } else {
510 int res_status = (res->cal_status == SIPE_CAL_NO_DATA) ? -1 : res->cal_status;
511 int cal_status = (cal_event->cal_status == SIPE_CAL_NO_DATA) ? -1 : cal_event->cal_status;
512 if (res_status < cal_status) {
513 res = cal_event;
516 entry = entry->next;
518 return res;
521 static int
522 sipe_cal_get_status0(const gchar *free_busy,
523 time_t cal_start,
524 int granularity,
525 time_t time_in_question,
526 int *index)
528 int res = SIPE_CAL_NO_DATA;
529 int shift;
530 time_t cal_end = cal_start + strlen(free_busy)*granularity*60 - 1;
532 if (!(time_in_question >= cal_start && time_in_question <= cal_end)) return res;
534 shift = (time_in_question - cal_start) / (granularity*60);
535 if (index) {
536 *index = shift;
539 res = free_busy[shift] - '0';
541 return res;
545 * Returns time when current calendar state started
547 static time_t
548 sipe_cal_get_since_time(const gchar *free_busy,
549 time_t calStart,
550 int granularity,
551 int index,
552 int current_state)
554 int i;
556 if ((index < 0) || ((size_t)(index + 1) > strlen(free_busy))) return 0;
558 for (i = index; i >= 0; i--) {
559 int temp_status = free_busy[i] - '0';
561 if (current_state != temp_status) {
562 return calStart + (i + 1)*granularity*60;
565 if (i == 0) return calStart;
568 return 0;
570 static char*
571 sipe_cal_get_free_busy(struct sipe_buddy *buddy);
574 sipe_cal_get_status(struct sipe_buddy *buddy,
575 time_t time_in_question,
576 time_t *since)
578 time_t cal_start;
579 const char* free_busy;
580 int ret = SIPE_CAL_NO_DATA;
581 time_t state_since;
582 int index;
584 if (!buddy || !buddy->cal_start_time || !buddy->cal_granularity) {
585 SIPE_DEBUG_INFO("sipe_cal_get_status: no calendar data1 for %s, exiting",
586 buddy ? (buddy->name ? buddy->name : "") : "");
587 return SIPE_CAL_NO_DATA;
590 if (!(free_busy = sipe_cal_get_free_busy(buddy))) {
591 SIPE_DEBUG_INFO("sipe_cal_get_status: no calendar data2 for %s, exiting", buddy->name);
592 return SIPE_CAL_NO_DATA;
594 SIPE_DEBUG_INFO("sipe_cal_get_description: buddy->cal_free_busy=\n%s", free_busy);
596 cal_start = sipe_utils_str_to_time(buddy->cal_start_time);
598 ret = sipe_cal_get_status0(free_busy,
599 cal_start,
600 buddy->cal_granularity,
601 time_in_question,
602 &index);
603 state_since = sipe_cal_get_since_time(free_busy,
604 cal_start,
605 buddy->cal_granularity,
606 index,
607 ret);
609 if (since) *since = state_since;
610 return ret;
613 static time_t
614 sipe_cal_get_switch_time(const gchar *free_busy,
615 time_t calStart,
616 int granularity,
617 int index,
618 int current_state,
619 int *to_state)
621 size_t i;
622 time_t ret = TIME_NULL;
624 if ((index < 0) || ((size_t) (index + 1) > strlen(free_busy))) {
625 *to_state = SIPE_CAL_NO_DATA;
626 return ret;
629 for (i = index + 1; i < strlen(free_busy); i++) {
630 int temp_status = free_busy[i] - '0';
632 if (current_state != temp_status) {
633 *to_state = temp_status;
634 return calStart + i*granularity*60;
638 return ret;
641 static const char*
642 sipe_cal_get_tz(struct sipe_cal_working_hours *wh,
643 time_t time_in_question)
645 time_t dst_switch_time = (*wh).dst.switch_time;
646 time_t std_switch_time = (*wh).std.switch_time;
647 gboolean is_dst = FALSE;
649 /* No daylight savings */
650 if (dst_switch_time == TIME_NULL) {
651 return wh->tz_std;
654 if (dst_switch_time < std_switch_time) { /* North hemosphere - Europe, US */
655 if (time_in_question >= dst_switch_time && time_in_question < std_switch_time) {
656 is_dst = TRUE;
658 } else { /* South hemisphere - Australia */
659 if (time_in_question >= dst_switch_time || time_in_question < std_switch_time) {
660 is_dst = TRUE;
664 if (is_dst) {
665 return wh->tz_dst;
666 } else {
667 return wh->tz_std;
671 static time_t
672 sipe_cal_mktime_of_day(struct tm *sample_today_tm,
673 const int shift_minutes,
674 const char *tz)
676 sample_today_tm->tm_sec = 0;
677 sample_today_tm->tm_min = shift_minutes % 60;
678 sample_today_tm->tm_hour = shift_minutes / 60;
680 return sipe_mktime_tz(sample_today_tm, tz);
684 * Returns work day start and end in Epoch time
685 * considering the initial values are provided
686 * in contact's local time zone.
688 static void
689 sipe_cal_get_today_work_hours(struct sipe_cal_working_hours *wh,
690 time_t *start,
691 time_t *end,
692 time_t *next_start)
694 time_t now = time(NULL);
695 const char *tz = sipe_cal_get_tz(wh, now);
696 struct tm *remote_now_tm = sipe_localtime_tz(&now, tz);
698 if (!strstr(wh->days_of_week, wday_names[remote_now_tm->tm_wday])) { /* not a work day */
699 *start = TIME_NULL;
700 *end = TIME_NULL;
701 *next_start = TIME_NULL;
702 return;
705 *end = sipe_cal_mktime_of_day(remote_now_tm, wh->end_time, tz);
707 if (now < *end) {
708 *start = sipe_cal_mktime_of_day(remote_now_tm, wh->start_time, tz);
709 *next_start = TIME_NULL;
710 } else { /* calculate start of tomorrow's work day if any */
711 time_t tom = now + 24*60*60;
712 struct tm *remote_tom_tm = sipe_localtime_tz(&tom, sipe_cal_get_tz(wh, tom));
714 if (!strstr(wh->days_of_week, wday_names[remote_tom_tm->tm_wday])) { /* not a work day */
715 *next_start = TIME_NULL;
718 *next_start = sipe_cal_mktime_of_day(remote_tom_tm, wh->start_time, sipe_cal_get_tz(wh, tom));
719 *start = TIME_NULL;
723 static int
724 sipe_cal_is_in_work_hours(const time_t time_in_question,
725 const time_t start,
726 const time_t end)
728 return !((time_in_question >= end) || (IS(start) && time_in_question < start));
732 * Returns time closest to now. Choses only from times ahead of now.
733 * Returns TIME_NULL otherwise.
735 static time_t
736 sipe_cal_get_until(const time_t now,
737 const time_t switch_time,
738 const time_t start,
739 const time_t end,
740 const time_t next_start)
742 time_t ret = TIME_NULL;
743 int min_diff = now - ret;
745 if (IS(switch_time) && switch_time > now && (switch_time - now) < min_diff) {
746 min_diff = switch_time - now;
747 ret = switch_time;
749 if (IS(start) && start > now && (start - now) < min_diff) {
750 min_diff = start - now;
751 ret = start;
753 if (IS(end) && end > now && (end - now) < min_diff) {
754 min_diff = end - now;
755 ret = end;
757 if (IS(next_start) && next_start > now && (next_start - now) < min_diff) {
758 min_diff = next_start - now;
759 ret = next_start;
761 return ret;
764 static char*
765 sipe_cal_get_free_busy(struct sipe_buddy *buddy)
767 /* do lazy decode if necessary */
768 if (!buddy->cal_free_busy && buddy->cal_free_busy_base64) {
769 gsize cal_dec64_len;
770 guchar *cal_dec64;
771 gsize i;
772 int j = 0;
774 cal_dec64 = g_base64_decode(buddy->cal_free_busy_base64, &cal_dec64_len);
776 buddy->cal_free_busy = g_malloc0(cal_dec64_len * 4 + 1);
778 http://msdn.microsoft.com/en-us/library/dd941537%28office.13%29.aspx
779 00, Free (Fr)
780 01, Tentative (Te)
781 10, Busy (Bu)
782 11, Out of facility (Oo)
784 http://msdn.microsoft.com/en-us/library/aa566048.aspx
785 0 Free
786 1 Tentative
787 2 Busy
788 3 Out of Office (OOF)
789 4 No data
791 for (i = 0; i < cal_dec64_len; i++) {
792 #define TWO_BIT_MASK 0x03
793 char tmp = cal_dec64[i];
794 buddy->cal_free_busy[j++] = (tmp & TWO_BIT_MASK) + '0';
795 buddy->cal_free_busy[j++] = ((tmp >> 2) & TWO_BIT_MASK) + '0';
796 buddy->cal_free_busy[j++] = ((tmp >> 4) & TWO_BIT_MASK) + '0';
797 buddy->cal_free_busy[j++] = ((tmp >> 6) & TWO_BIT_MASK) + '0';
799 buddy->cal_free_busy[j++] = '\0';
800 g_free(cal_dec64);
803 return buddy->cal_free_busy;
806 char *
807 sipe_cal_get_freebusy_base64(const char* freebusy_hex)
809 guint i = 0;
810 guint j = 0;
811 guint shift_factor = 0;
812 guint len, res_len;
813 guchar *res;
814 gchar *res_base64;
816 if (!freebusy_hex) return NULL;
818 len = strlen(freebusy_hex);
819 res_len = len / 4 + 1;
820 res = g_malloc0(res_len);
821 while (i < len) {
822 res[j] |= (freebusy_hex[i++] - '0') << shift_factor;
823 shift_factor += 2;
824 if (shift_factor == 8) {
825 shift_factor = 0;
826 j++;
830 res_base64 = g_base64_encode(res, shift_factor ? res_len : res_len - 1);
831 g_free(res);
832 return res_base64;
835 char *
836 sipe_cal_get_description(struct sipe_buddy *buddy)
838 time_t cal_start;
839 time_t cal_end;
840 int current_cal_state;
841 time_t now = time(NULL);
842 time_t start = TIME_NULL;
843 time_t end = TIME_NULL;
844 time_t next_start = TIME_NULL;
845 time_t switch_time;
846 int to_state = SIPE_CAL_NO_DATA;
847 time_t until = TIME_NULL;
848 int index = 0;
849 gboolean has_working_hours = (buddy->cal_working_hours != NULL);
850 const char *free_busy;
851 const char *cal_states[] = {_("Free"),
852 _("Tentative"),
853 _("Busy"),
854 _("Out of office"),
855 _("No data")};
857 if (buddy->cal_granularity != 15) {
858 SIPE_DEBUG_INFO("sipe_cal_get_description: granularity %d is unsupported, exiting.", buddy->cal_granularity);
859 return NULL;
862 /* to lazy load if needed */
863 free_busy = sipe_cal_get_free_busy(buddy);
864 SIPE_DEBUG_INFO("sipe_cal_get_description: buddy->cal_free_busy=\n%s", free_busy ? free_busy : "");
866 if (!buddy->cal_free_busy || !buddy->cal_granularity || !buddy->cal_start_time) {
867 SIPE_DEBUG_INFO("sipe_cal_get_description: no calendar data, exiting%s", "");
868 return NULL;
871 cal_start = sipe_utils_str_to_time(buddy->cal_start_time);
872 cal_end = cal_start + 60 * (buddy->cal_granularity) * strlen(buddy->cal_free_busy);
874 current_cal_state = sipe_cal_get_status0(free_busy, cal_start, buddy->cal_granularity, time(NULL), &index);
875 if (current_cal_state == SIPE_CAL_NO_DATA) {
876 SIPE_DEBUG_INFO("sipe_cal_get_description: calendar is undefined for present moment, exiting.%s", "");
877 return NULL;
880 switch_time = sipe_cal_get_switch_time(free_busy, cal_start, buddy->cal_granularity, index, current_cal_state, &to_state);
882 SIPE_DEBUG_INFO("\n* Calendar *%s", "");
883 if (buddy->cal_working_hours) {
884 sipe_cal_get_today_work_hours(buddy->cal_working_hours, &start, &end, &next_start);
886 SIPE_DEBUG_INFO("Remote now timezone : %s", sipe_cal_get_tz(buddy->cal_working_hours, now));
887 SIPE_DEBUG_INFO("std.switch_time(GMT): %s",
888 IS((*buddy->cal_working_hours).std.switch_time) ? asctime(gmtime(&((*buddy->cal_working_hours).std.switch_time))) : "");
889 SIPE_DEBUG_INFO("dst.switch_time(GMT): %s",
890 IS((*buddy->cal_working_hours).dst.switch_time) ? asctime(gmtime(&((*buddy->cal_working_hours).dst.switch_time))) : "");
891 SIPE_DEBUG_INFO("Remote now time : %s",
892 asctime(sipe_localtime_tz(&now, sipe_cal_get_tz(buddy->cal_working_hours, now))));
893 SIPE_DEBUG_INFO("Remote start time : %s",
894 IS(start) ? asctime(sipe_localtime_tz(&start, sipe_cal_get_tz(buddy->cal_working_hours, start))) : "");
895 SIPE_DEBUG_INFO("Remote end time : %s",
896 IS(end) ? asctime(sipe_localtime_tz(&end, sipe_cal_get_tz(buddy->cal_working_hours, end))) : "");
897 SIPE_DEBUG_INFO("Rem. next_start time: %s",
898 IS(next_start) ? asctime(sipe_localtime_tz(&next_start, sipe_cal_get_tz(buddy->cal_working_hours, next_start))) : "");
899 SIPE_DEBUG_INFO("Remote switch time : %s",
900 IS(switch_time) ? asctime(sipe_localtime_tz(&switch_time, sipe_cal_get_tz(buddy->cal_working_hours, switch_time))) : "");
901 } else {
902 SIPE_DEBUG_INFO("Local now time : %s",
903 asctime(localtime(&now)));
904 SIPE_DEBUG_INFO("Local switch time : %s",
905 IS(switch_time) ? asctime(localtime(&switch_time)) : "");
907 SIPE_DEBUG_INFO("Calendar End (GMT) : %s", asctime(gmtime(&cal_end)));
908 SIPE_DEBUG_INFO("current cal state : %s", cal_states[current_cal_state]);
909 SIPE_DEBUG_INFO("switch cal state : %s", cal_states[to_state] );
911 /* Calendar: string calculations */
914 ALGORITHM (don't delete)
915 (c)2009,2010 pier11 <pier11@operamail.com>
917 SOD = Start of Work Day
918 EOD = End of Work Day
919 NSOD = Start of tomorrow's Work Day
920 SW = Calendar status switch time
922 if current_cal_state == Free
923 until = min_t of SOD, EOD, NSOD, SW (min_t(x) = min(x-now) where x>now only)
924 else
925 until = SW
927 if (!until && (cal_period_end > now + 8H))
928 until = cal_period_end
930 if (!until)
931 return "Currently %", current_cal_state
933 if (until - now > 8H)
934 if (current_cal_state == Free && (work_hours && !in work_hours(now)))
935 return "Outside of working hours for next 8 hours"
936 else
937 return "%s for next 8 hours", current_cal_state
939 if (current_cal_state == Free)
940 if (work_hours && until !in work_hours(now))
941 "Not working"
942 else
943 "%s", current_cal_state
944 " until %.2d:%.2d", until
945 else
946 "Currently %", current_cal_state
947 if (work_hours && until !in work_hours(until))
948 ". Outside of working hours at at %.2d:%.2d", until
949 else
950 ". %s at %.2d:%.2d", to_state, until
953 if (current_cal_state < 1) { /* Free */
954 until = sipe_cal_get_until(now, switch_time, start, end, next_start);
955 } else {
956 until = switch_time;
959 if (!IS(until) && (cal_end - now > 8*60*60))
960 until = cal_end;
962 if (!IS(until)) {
963 return g_strdup_printf(_("Currently %s"), cal_states[current_cal_state]);
966 if (until - now > 8*60*60) {
967 /* Free & outside work hours */
968 if (current_cal_state < 1 && has_working_hours && !sipe_cal_is_in_work_hours(now, start, end)) {
969 return g_strdup(_("Outside of working hours for next 8 hours"));
970 } else {
971 return g_strdup_printf(_("%s for next 8 hours"), cal_states[current_cal_state]);
975 if (current_cal_state < 1) { /* Free */
976 const char *tmp;
977 struct tm *until_tm = localtime(&until);
979 if (has_working_hours && !sipe_cal_is_in_work_hours(now, start, end)) {
980 tmp = _("Not working");
981 } else {
982 tmp = cal_states[current_cal_state];
984 return g_strdup_printf(_("%s until %.2d:%.2d"), tmp, until_tm->tm_hour, until_tm->tm_min);
985 } else { /* Tentative or Busy or OOF */
986 char *tmp;
987 char *res;
988 struct tm *until_tm = localtime(&until);
990 tmp = g_strdup_printf(_("Currently %s"), cal_states[current_cal_state]);
991 if (has_working_hours && !sipe_cal_is_in_work_hours(until, start, end)) {
992 res = g_strdup_printf(_("%s. Outside of working hours at %.2d:%.2d"),
993 tmp, until_tm->tm_hour, until_tm->tm_min);
994 g_free(tmp);
995 return res;
996 } else {
997 res = g_strdup_printf(_("%s. %s at %.2d:%.2d"), tmp, cal_states[to_state], until_tm->tm_hour, until_tm->tm_min);
998 g_free(tmp);
999 return res;
1002 /* End of - Calendar: string calculations */
1006 Local Variables:
1007 mode: c
1008 c-file-style: "bsd"
1009 indent-tabs-mode: t
1010 tab-width: 8
1011 End: