core cleanup: replace purple_base64_encode/decode
[siplcs.git] / src / core / sipe-cal.c
blobd9d132478df8703f3fb52a628b738c8967839650
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 "debug.h"
36 #include "sip-sec.h"
37 #include "sipe-cal.h"
38 #include "sipe-nls.h"
39 #include "sipe-utils.h"
40 #include "sipe-xml.h"
41 #include "sipe.h"
43 #define TIME_NULL (time_t)-1
44 #define IS(time) (time != TIME_NULL)
47 http://msdn.microsoft.com/en-us/library/aa565001.aspx
49 <?xml version="1.0"?>
50 <WorkingHours xmlns="http://schemas.microsoft.com/exchange/services/2006/types">
51 <TimeZone>
52 <Bias>480</Bias>
53 <StandardTime>
54 <Bias>0</Bias>
55 <Time>02:00:00</Time>
56 <DayOrder>1</DayOrder>
57 <Month>11</Month>
58 <DayOfWeek>Sunday</DayOfWeek>
59 </StandardTime>
60 <DaylightTime>
61 <Bias>-60</Bias>
62 <Time>02:00:00</Time>
63 <DayOrder>2</DayOrder>
64 <Month>3</Month>
65 <DayOfWeek>Sunday</DayOfWeek>
66 </DaylightTime>
67 </TimeZone>
68 <WorkingPeriodArray>
69 <WorkingPeriod>
70 <DayOfWeek>Monday Tuesday Wednesday Thursday Friday</DayOfWeek>
71 <StartTimeInMinutes>600</StartTimeInMinutes>
72 <EndTimeInMinutes>1140</EndTimeInMinutes>
73 </WorkingPeriod>
74 </WorkingPeriodArray>
75 </WorkingHours>
77 Desc:
78 <StandardTime>
79 <Bias>int</Bias>
80 <Time>string</Time>
81 <DayOrder>short</DayOrder>
82 <Month>short</Month>
83 <DayOfWeek>Sunday or Monday or Tuesday or Wednesday or Thursday or Friday or Saturday</DayOfWeek>
84 <Year>string</Year>
85 </StandardTime>
88 struct sipe_cal_std_dst {
89 int bias; /* Ex.: -60 */
90 gchar *time; /* hh:mm:ss, 02:00:00 */
91 int day_order; /* 1..5 */
92 int month; /* 1..12 */
93 gchar *day_of_week; /* Sunday or Monday or Tuesday or Wednesday or Thursday or Friday or Saturday */
94 gchar *year; /* YYYY */
96 time_t switch_time;
99 struct sipe_cal_working_hours {
100 int bias; /* Ex.: 480 */
101 struct sipe_cal_std_dst std; /* StandardTime */
102 struct sipe_cal_std_dst dst; /* DaylightTime */
103 gchar *days_of_week; /* Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday separated by space */
104 int start_time; /* 0...1440 */
105 int end_time; /* 0...1440 */
107 gchar *tz; /* aggregated timezone string as in TZ environment variable.
108 Ex.: TST+8TDT+7,M3.2.0/02:00:00,M11.1.0/02:00:00 */
109 /** separate simple strings for Windows platform as the proper TZ does not work there.
110 * anyway, dynamic timezones would't work with just TZ
112 gchar *tz_std; /* Ex.: TST8 */
113 gchar *tz_dst; /* Ex.: TDT7 */
116 /* not for translation, a part of XML Schema definitions */
117 static const char *wday_names[] = {"Sunday",
118 "Monday",
119 "Tuesday",
120 "Wednesday",
121 "Thursday",
122 "Friday",
123 "Saturday"};
124 static int
125 sipe_cal_get_wday(char *wday_name)
127 int i;
129 if (!wday_name) return -1;
131 for (i = 0; i < 7; i++) {
132 if (sipe_strequal(wday_names[i], wday_name)) {
133 return i;
137 return -1;
140 void
141 sipe_cal_event_free(struct sipe_cal_event* cal_event)
143 if (!cal_event) return;
145 g_free(cal_event->subject);
146 g_free(cal_event->location);
147 g_free(cal_event);
150 char *
151 sipe_cal_event_describe(struct sipe_cal_event* cal_event)
153 GString* str = g_string_new(NULL);
154 const char *status = "";
156 switch(cal_event->cal_status) {
157 case SIPE_CAL_FREE: status = "SIPE_CAL_FREE"; break;
158 case SIPE_CAL_TENTATIVE: status = "SIPE_CAL_TENTATIVE"; break;
159 case SIPE_CAL_BUSY: status = "SIPE_CAL_BUSY"; break;
160 case SIPE_CAL_OOF: status = "SIPE_CAL_OOF"; break;
161 case SIPE_CAL_NO_DATA: status = "SIPE_CAL_NO_DATA"; break;
164 g_string_append_printf(str, "\t%s: %s", "start_time",
165 IS(cal_event->start_time) ? asctime(localtime(&cal_event->start_time)) : "\n");
166 g_string_append_printf(str, "\t%s: %s", "end_time ",
167 IS(cal_event->end_time) ? asctime(localtime(&cal_event->end_time)) : "\n");
168 g_string_append_printf(str, "\t%s: %s\n", "cal_status", status);
169 g_string_append_printf(str, "\t%s: %s\n", "subject ", cal_event->subject ? cal_event->subject : "");
170 g_string_append_printf(str, "\t%s: %s\n", "location ", cal_event->location ? cal_event->location : "");
171 g_string_append_printf(str, "\t%s: %s\n", "is_meeting", cal_event->is_meeting ? "TRUE" : "FALSE");
173 return g_string_free(str, FALSE);
176 char *
177 sipe_cal_event_hash(struct sipe_cal_event* event)
179 /* no end_time as it dos not get published */
180 /* no cal_status as it can change on publication */
181 return g_strdup_printf("<%d><%s><%s><%d>",
182 (int)event->start_time,
183 event->subject ? event->subject : "",
184 event->location ? event->location : "",
185 event->is_meeting);
188 static void
189 sipe_setenv(const char *name,
190 const char *value)
192 #ifdef HAVE_SETENV
193 setenv(name, value, 1);
194 #else
195 int len = strlen(name) + 1 + strlen(value) + 1;
196 char *str = g_malloc0(len);
197 sprintf(str, "%s=%s", name, value);
198 putenv(str);
199 #endif
202 static void
203 sipe_unsetenv(const char *name)
205 #ifdef HAVE_UNSETENV
206 unsetenv(name);
207 #else
208 int len = strlen(name) + 1 + 1;
209 char *str = g_malloc0(len);
210 sprintf(str, "%s=", name);
211 putenv(str);
212 #endif
216 * Converts struct tm to Epoch time_t considering timezone.
218 * @param tz as defined for TZ environment variable.
220 * Reference: see timegm(3) - Linux man page
222 time_t
223 sipe_mktime_tz(struct tm *tm,
224 const char* tz)
226 time_t ret;
227 char *tz_old;
229 tz_old = getenv("TZ");
230 sipe_setenv("TZ", tz);
231 tzset();
233 ret = mktime(tm);
235 if (tz_old) {
236 sipe_setenv("TZ", tz_old);
237 } else {
238 sipe_unsetenv("TZ");
240 tzset();
242 return ret;
246 * Converts Epoch time_t to struct tm considering timezone.
248 * @param tz as defined for TZ environment variable.
250 * Reference: see timegm(3) - Linux man page
252 static struct tm *
253 sipe_localtime_tz(const time_t *time,
254 const char* tz)
256 struct tm *ret;
257 char *tz_old;
259 tz_old = getenv("TZ");
260 sipe_setenv("TZ", tz);
261 tzset();
263 ret = localtime(time);
265 if (tz_old) {
266 sipe_setenv("TZ", tz_old);
267 } else {
268 sipe_unsetenv("TZ");
270 tzset();
272 return ret;
275 void
276 sipe_cal_free_working_hours(struct sipe_cal_working_hours *wh)
278 if (!wh) return;
280 g_free(wh->std.time);
281 g_free(wh->std.day_of_week);
282 g_free(wh->std.year);
284 g_free(wh->dst.time);
285 g_free(wh->dst.day_of_week);
286 g_free(wh->dst.year);
288 g_free(wh->days_of_week);
289 g_free(wh->tz);
290 g_free(wh->tz_std);
291 g_free(wh->tz_dst);
292 g_free(wh);
296 * Returns time_t of daylight savings time start/end
297 * in the provided timezone or otherwise
298 * (time_t)-1 if no daylight savings time.
300 static time_t
301 sipe_cal_get_std_dst_time(time_t now,
302 int bias,
303 struct sipe_cal_std_dst* std_dst,
304 struct sipe_cal_std_dst* dst_std)
306 struct tm switch_tm;
307 time_t res = TIME_NULL;
308 struct tm *gm_now_tm;
309 gchar **time_arr;
311 if (std_dst->month == 0) return TIME_NULL;
313 gm_now_tm = gmtime(&now);
314 time_arr = g_strsplit(std_dst->time, ":", 0);
316 switch_tm.tm_sec = atoi(time_arr[2]);
317 switch_tm.tm_min = atoi(time_arr[1]);
318 switch_tm.tm_hour = atoi(time_arr[0]);
319 g_strfreev(time_arr);
320 switch_tm.tm_mday = std_dst->year ? std_dst->day_order : 1 /* to adjust later */ ;
321 switch_tm.tm_mon = std_dst->month - 1;
322 switch_tm.tm_year = std_dst->year ? atoi(std_dst->year) - 1900 : gm_now_tm->tm_year;
323 switch_tm.tm_isdst = 0;
324 /* to set tm_wday */
325 res = sipe_mktime_tz(&switch_tm, "UTC");
327 /* if not dynamic, calculate right tm_mday */
328 if (!std_dst->year) {
329 int switch_wday = sipe_cal_get_wday(std_dst->day_of_week);
330 int needed_month;
331 /* get first desired wday in the month */
332 int delta = switch_wday >= switch_tm.tm_wday ? (switch_wday - switch_tm.tm_wday) : (switch_wday + 7 - switch_tm.tm_wday);
333 switch_tm.tm_mday = 1 + delta;
334 /* try nth order */
335 switch_tm.tm_mday += (std_dst->day_order - 1) * 7;
336 needed_month = switch_tm.tm_mon;
337 /* to set settle date if ahead of allowed month dates */
338 res = sipe_mktime_tz(&switch_tm, "UTC");
339 if (needed_month != switch_tm.tm_mon) {
340 /* moving 1 week back to stay within required month */
341 switch_tm.tm_mday -= 7;
342 /* to fix date again */
343 res = sipe_mktime_tz(&switch_tm, "UTC");
346 /* note: bias is taken from "switch to" structure */
347 return res + (bias + dst_std->bias)*60;
350 static void
351 sipe_cal_parse_std_dst(const sipe_xml *xn_std_dst_time,
352 struct sipe_cal_std_dst *std_dst)
354 const sipe_xml *node;
355 gchar *tmp;
357 if (!xn_std_dst_time) return;
358 if (!std_dst) return;
360 <StandardTime>
361 <Bias>0</Bias>
362 <Time>02:00:00</Time>
363 <DayOrder>1</DayOrder>
364 <Month>11</Month>
365 <DayOfWeek>Sunday</DayOfWeek>
366 </StandardTime>
369 if ((node = sipe_xml_child(xn_std_dst_time, "Bias"))) {
370 std_dst->bias = atoi(tmp = sipe_xml_data(node));
371 g_free(tmp);
374 if ((node = sipe_xml_child(xn_std_dst_time, "Time"))) {
375 std_dst->time = sipe_xml_data(node);
378 if ((node = sipe_xml_child(xn_std_dst_time, "DayOrder"))) {
379 std_dst->day_order = atoi(tmp = sipe_xml_data(node));
380 g_free(tmp);
383 if ((node = sipe_xml_child(xn_std_dst_time, "Month"))) {
384 std_dst->month = atoi(tmp = sipe_xml_data(node));
385 g_free(tmp);
388 if ((node = sipe_xml_child(xn_std_dst_time, "DayOfWeek"))) {
389 std_dst->day_of_week = sipe_xml_data(node);
392 if ((node = sipe_xml_child(xn_std_dst_time, "Year"))) {
393 std_dst->year = sipe_xml_data(node);
397 void
398 sipe_cal_parse_working_hours(const sipe_xml *xn_working_hours,
399 struct sipe_buddy *buddy)
401 const sipe_xml *xn_bias;
402 const sipe_xml *xn_timezone;
403 const sipe_xml *xn_working_period;
404 const sipe_xml *xn_standard_time;
405 const sipe_xml *xn_daylight_time;
406 gchar *tmp;
407 time_t now = time(NULL);
408 struct sipe_cal_std_dst* std;
409 struct sipe_cal_std_dst* dst;
411 if (!xn_working_hours) return;
413 <WorkingHours xmlns="http://schemas.microsoft.com/exchange/services/2006/types">
414 <TimeZone>
415 <Bias>480</Bias>
417 </TimeZone>
418 <WorkingPeriodArray>
419 <WorkingPeriod>
420 <DayOfWeek>Monday Tuesday Wednesday Thursday Friday</DayOfWeek>
421 <StartTimeInMinutes>600</StartTimeInMinutes>
422 <EndTimeInMinutes>1140</EndTimeInMinutes>
423 </WorkingPeriod>
424 </WorkingPeriodArray>
425 </WorkingHours>
427 sipe_cal_free_working_hours(buddy->cal_working_hours);
428 buddy->cal_working_hours = g_new0(struct sipe_cal_working_hours, 1);
430 xn_timezone = sipe_xml_child(xn_working_hours, "TimeZone");
431 xn_bias = sipe_xml_child(xn_timezone, "Bias");
432 if (xn_bias) {
433 buddy->cal_working_hours->bias = atoi(tmp = sipe_xml_data(xn_bias));
434 g_free(tmp);
437 xn_standard_time = sipe_xml_child(xn_timezone, "StandardTime");
438 xn_daylight_time = sipe_xml_child(xn_timezone, "DaylightTime");
440 std = &((*buddy->cal_working_hours).std);
441 dst = &((*buddy->cal_working_hours).dst);
442 sipe_cal_parse_std_dst(xn_standard_time, std);
443 sipe_cal_parse_std_dst(xn_daylight_time, dst);
445 xn_working_period = sipe_xml_child(xn_working_hours, "WorkingPeriodArray/WorkingPeriod");
446 if (xn_working_period) {
447 buddy->cal_working_hours->days_of_week =
448 sipe_xml_data(sipe_xml_child(xn_working_period, "DayOfWeek"));
450 buddy->cal_working_hours->start_time =
451 atoi(tmp = sipe_xml_data(sipe_xml_child(xn_working_period, "StartTimeInMinutes")));
452 g_free(tmp);
454 buddy->cal_working_hours->end_time =
455 atoi(tmp = sipe_xml_data(sipe_xml_child(xn_working_period, "EndTimeInMinutes")));
456 g_free(tmp);
459 std->switch_time = sipe_cal_get_std_dst_time(now, buddy->cal_working_hours->bias, std, dst);
460 dst->switch_time = sipe_cal_get_std_dst_time(now, buddy->cal_working_hours->bias, dst, std);
462 /* TST8TDT7,M3.2.0/02:00:00,M11.1.0/02:00:00 */
463 buddy->cal_working_hours->tz =
464 g_strdup_printf("TST%dTDT%d,M%d.%d.%d/%s,M%d.%d.%d/%s",
465 (buddy->cal_working_hours->bias + buddy->cal_working_hours->std.bias) / 60,
466 (buddy->cal_working_hours->bias + buddy->cal_working_hours->dst.bias) / 60,
468 buddy->cal_working_hours->dst.month,
469 buddy->cal_working_hours->dst.day_order,
470 sipe_cal_get_wday(buddy->cal_working_hours->dst.day_of_week),
471 buddy->cal_working_hours->dst.time,
473 buddy->cal_working_hours->std.month,
474 buddy->cal_working_hours->std.day_order,
475 sipe_cal_get_wday(buddy->cal_working_hours->std.day_of_week),
476 buddy->cal_working_hours->std.time
478 /* TST8 */
479 buddy->cal_working_hours->tz_std =
480 g_strdup_printf("TST%d",
481 (buddy->cal_working_hours->bias + buddy->cal_working_hours->std.bias) / 60);
482 /* TDT7 */
483 buddy->cal_working_hours->tz_dst =
484 g_strdup_printf("TDT%d",
485 (buddy->cal_working_hours->bias + buddy->cal_working_hours->dst.bias) / 60);
488 struct sipe_cal_event*
489 sipe_cal_get_event(GSList *cal_events,
490 time_t time_in_question)
492 GSList *entry = cal_events;
493 struct sipe_cal_event* cal_event;
494 struct sipe_cal_event* res = NULL;
496 if (!cal_events || !IS(time_in_question)) return NULL;
498 while (entry) {
499 cal_event = entry->data;
500 /* event is in the past or in the future */
501 if (cal_event->start_time > time_in_question ||
502 cal_event->end_time <= time_in_question)
504 entry = entry->next;
505 continue;
508 if (!res) {
509 res = cal_event;
510 } else {
511 int res_status = (res->cal_status == SIPE_CAL_NO_DATA) ? -1 : res->cal_status;
512 int cal_status = (cal_event->cal_status == SIPE_CAL_NO_DATA) ? -1 : cal_event->cal_status;
513 if (res_status < cal_status) {
514 res = cal_event;
517 entry = entry->next;
519 return res;
522 static int
523 sipe_cal_get_status0(const gchar *free_busy,
524 time_t cal_start,
525 int granularity,
526 time_t time_in_question,
527 int *index)
529 int res = SIPE_CAL_NO_DATA;
530 int shift;
531 time_t cal_end = cal_start + strlen(free_busy)*granularity*60 - 1;
533 if (!(time_in_question >= cal_start && time_in_question <= cal_end)) return res;
535 shift = (time_in_question - cal_start) / (granularity*60);
536 if (index) {
537 *index = shift;
540 res = free_busy[shift] - '0';
542 return res;
546 * Returns time when current calendar state started
548 static time_t
549 sipe_cal_get_since_time(const gchar *free_busy,
550 time_t calStart,
551 int granularity,
552 int index,
553 int current_state)
555 int i;
557 if ((index < 0) || ((size_t)(index + 1) > strlen(free_busy))) return 0;
559 for (i = index; i >= 0; i--) {
560 int temp_status = free_busy[i] - '0';
562 if (current_state != temp_status) {
563 return calStart + (i + 1)*granularity*60;
566 if (i == 0) return calStart;
569 return 0;
571 static char*
572 sipe_cal_get_free_busy(struct sipe_buddy *buddy);
575 sipe_cal_get_status(struct sipe_buddy *buddy,
576 time_t time_in_question,
577 time_t *since)
579 time_t cal_start;
580 const char* free_busy;
581 int ret = SIPE_CAL_NO_DATA;
582 time_t state_since;
583 int index;
585 if (!buddy || !buddy->cal_start_time || !buddy->cal_granularity) {
586 purple_debug_info("sipe", "sipe_cal_get_status: no calendar data1 for %s, exiting\n",
587 buddy ? (buddy->name ? buddy->name : "") : "");
588 return SIPE_CAL_NO_DATA;
591 if (!(free_busy = sipe_cal_get_free_busy(buddy))) {
592 purple_debug_info("sipe", "sipe_cal_get_status: no calendar data2 for %s, exiting\n", buddy->name);
593 return SIPE_CAL_NO_DATA;
595 purple_debug_info("sipe", "sipe_cal_get_description: buddy->cal_free_busy=\n%s\n", free_busy);
597 cal_start = sipe_utils_str_to_time(buddy->cal_start_time);
599 ret = sipe_cal_get_status0(free_busy,
600 cal_start,
601 buddy->cal_granularity,
602 time_in_question,
603 &index);
604 state_since = sipe_cal_get_since_time(free_busy,
605 cal_start,
606 buddy->cal_granularity,
607 index,
608 ret);
610 if (since) *since = state_since;
611 return ret;
614 static time_t
615 sipe_cal_get_switch_time(const gchar *free_busy,
616 time_t calStart,
617 int granularity,
618 int index,
619 int current_state,
620 int *to_state)
622 size_t i;
623 time_t ret = TIME_NULL;
625 if ((index < 0) || ((size_t) (index + 1) > strlen(free_busy))) {
626 *to_state = SIPE_CAL_NO_DATA;
627 return ret;
630 for (i = index + 1; i < strlen(free_busy); i++) {
631 int temp_status = free_busy[i] - '0';
633 if (current_state != temp_status) {
634 *to_state = temp_status;
635 return calStart + i*granularity*60;
639 return ret;
642 static const char*
643 sipe_cal_get_tz(struct sipe_cal_working_hours *wh,
644 time_t time_in_question)
646 time_t dst_switch_time = (*wh).dst.switch_time;
647 time_t std_switch_time = (*wh).std.switch_time;
648 gboolean is_dst = FALSE;
650 /* No daylight savings */
651 if (dst_switch_time == TIME_NULL) {
652 return wh->tz_std;
655 if (dst_switch_time < std_switch_time) { /* North hemosphere - Europe, US */
656 if (time_in_question >= dst_switch_time && time_in_question < std_switch_time) {
657 is_dst = TRUE;
659 } else { /* South hemisphere - Australia */
660 if (time_in_question >= dst_switch_time || time_in_question < std_switch_time) {
661 is_dst = TRUE;
665 if (is_dst) {
666 return wh->tz_dst;
667 } else {
668 return wh->tz_std;
672 static time_t
673 sipe_cal_mktime_of_day(struct tm *sample_today_tm,
674 const int shift_minutes,
675 const char *tz)
677 sample_today_tm->tm_sec = 0;
678 sample_today_tm->tm_min = shift_minutes % 60;
679 sample_today_tm->tm_hour = shift_minutes / 60;
681 return sipe_mktime_tz(sample_today_tm, tz);
685 * Returns work day start and end in Epoch time
686 * considering the initial values are provided
687 * in contact's local time zone.
689 static void
690 sipe_cal_get_today_work_hours(struct sipe_cal_working_hours *wh,
691 time_t *start,
692 time_t *end,
693 time_t *next_start)
695 time_t now = time(NULL);
696 const char *tz = sipe_cal_get_tz(wh, now);
697 struct tm *remote_now_tm = sipe_localtime_tz(&now, tz);
699 if (!strstr(wh->days_of_week, wday_names[remote_now_tm->tm_wday])) { /* not a work day */
700 *start = TIME_NULL;
701 *end = TIME_NULL;
702 *next_start = TIME_NULL;
703 return;
706 *end = sipe_cal_mktime_of_day(remote_now_tm, wh->end_time, tz);
708 if (now < *end) {
709 *start = sipe_cal_mktime_of_day(remote_now_tm, wh->start_time, tz);
710 *next_start = TIME_NULL;
711 } else { /* calculate start of tomorrow's work day if any */
712 time_t tom = now + 24*60*60;
713 struct tm *remote_tom_tm = sipe_localtime_tz(&tom, sipe_cal_get_tz(wh, tom));
715 if (!strstr(wh->days_of_week, wday_names[remote_tom_tm->tm_wday])) { /* not a work day */
716 *next_start = TIME_NULL;
719 *next_start = sipe_cal_mktime_of_day(remote_tom_tm, wh->start_time, sipe_cal_get_tz(wh, tom));
720 *start = TIME_NULL;
724 static int
725 sipe_cal_is_in_work_hours(const time_t time_in_question,
726 const time_t start,
727 const time_t end)
729 return !((time_in_question >= end) || (IS(start) && time_in_question < start));
733 * Returns time closest to now. Choses only from times ahead of now.
734 * Returns TIME_NULL otherwise.
736 static time_t
737 sipe_cal_get_until(const time_t now,
738 const time_t switch_time,
739 const time_t start,
740 const time_t end,
741 const time_t next_start)
743 time_t ret = TIME_NULL;
744 int min_diff = now - ret;
746 if (IS(switch_time) && switch_time > now && (switch_time - now) < min_diff) {
747 min_diff = switch_time - now;
748 ret = switch_time;
750 if (IS(start) && start > now && (start - now) < min_diff) {
751 min_diff = start - now;
752 ret = start;
754 if (IS(end) && end > now && (end - now) < min_diff) {
755 min_diff = end - now;
756 ret = end;
758 if (IS(next_start) && next_start > now && (next_start - now) < min_diff) {
759 min_diff = next_start - now;
760 ret = next_start;
762 return ret;
765 static char*
766 sipe_cal_get_free_busy(struct sipe_buddy *buddy)
768 /* do lazy decode if necessary */
769 if (!buddy->cal_free_busy && buddy->cal_free_busy_base64) {
770 gsize cal_dec64_len;
771 guchar *cal_dec64;
772 gsize i;
773 int j = 0;
775 cal_dec64 = g_base64_decode(buddy->cal_free_busy_base64, &cal_dec64_len);
777 buddy->cal_free_busy = g_malloc0(cal_dec64_len * 4 + 1);
779 http://msdn.microsoft.com/en-us/library/dd941537%28office.13%29.aspx
780 00, Free (Fr)
781 01, Tentative (Te)
782 10, Busy (Bu)
783 11, Out of facility (Oo)
785 http://msdn.microsoft.com/en-us/library/aa566048.aspx
786 0 Free
787 1 Tentative
788 2 Busy
789 3 Out of Office (OOF)
790 4 No data
792 for (i = 0; i < cal_dec64_len; i++) {
793 #define TWO_BIT_MASK 0x03
794 char tmp = cal_dec64[i];
795 buddy->cal_free_busy[j++] = (tmp & TWO_BIT_MASK) + '0';
796 buddy->cal_free_busy[j++] = ((tmp >> 2) & TWO_BIT_MASK) + '0';
797 buddy->cal_free_busy[j++] = ((tmp >> 4) & TWO_BIT_MASK) + '0';
798 buddy->cal_free_busy[j++] = ((tmp >> 6) & TWO_BIT_MASK) + '0';
800 buddy->cal_free_busy[j++] = '\0';
801 g_free(cal_dec64);
804 return buddy->cal_free_busy;
807 char *
808 sipe_cal_get_freebusy_base64(const char* freebusy_hex)
810 guint i = 0;
811 guint j = 0;
812 guint shift_factor = 0;
813 guint len, res_len;
814 guchar *res;
815 gchar *res_base64;
817 if (!freebusy_hex) return NULL;
819 len = strlen(freebusy_hex);
820 res_len = len / 4 + 1;
821 res = g_malloc0(res_len);
822 while (i < len) {
823 res[j] |= (freebusy_hex[i++] - '0') << shift_factor;
824 shift_factor += 2;
825 if (shift_factor == 8) {
826 shift_factor = 0;
827 j++;
831 res_base64 = g_base64_encode(res, shift_factor ? res_len : res_len - 1);
832 g_free(res);
833 return res_base64;
836 char *
837 sipe_cal_get_description(struct sipe_buddy *buddy)
839 time_t cal_start;
840 time_t cal_end;
841 int current_cal_state;
842 time_t now = time(NULL);
843 time_t start = TIME_NULL;
844 time_t end = TIME_NULL;
845 time_t next_start = TIME_NULL;
846 time_t switch_time;
847 int to_state = SIPE_CAL_NO_DATA;
848 time_t until = TIME_NULL;
849 int index = 0;
850 gboolean has_working_hours = (buddy->cal_working_hours != NULL);
851 const char *free_busy;
852 const char *cal_states[] = {_("Free"),
853 _("Tentative"),
854 _("Busy"),
855 _("Out of office"),
856 _("No data")};
858 if (buddy->cal_granularity != 15) {
859 purple_debug_info("sipe", "sipe_cal_get_description: granularity %d is unsupported, exiting.\n", buddy->cal_granularity);
860 return NULL;
863 /* to lazy load if needed */
864 free_busy = sipe_cal_get_free_busy(buddy);
865 purple_debug_info("sipe", "sipe_cal_get_description: buddy->cal_free_busy=\n%s\n", free_busy ? free_busy : "");
867 if (!buddy->cal_free_busy || !buddy->cal_granularity || !buddy->cal_start_time) {
868 purple_debug_info("sipe", "sipe_cal_get_description: no calendar data, exiting");
869 return NULL;
872 cal_start = sipe_utils_str_to_time(buddy->cal_start_time);
873 cal_end = cal_start + 60 * (buddy->cal_granularity) * strlen(buddy->cal_free_busy);
875 current_cal_state = sipe_cal_get_status0(free_busy, cal_start, buddy->cal_granularity, time(NULL), &index);
876 if (current_cal_state == SIPE_CAL_NO_DATA) {
877 purple_debug_info("sipe", "sipe_cal_get_description: calendar is undefined for present moment, exiting.\n");
878 return NULL;
881 switch_time = sipe_cal_get_switch_time(free_busy, cal_start, buddy->cal_granularity, index, current_cal_state, &to_state);
883 purple_debug_info("sipe", "\n* Calendar *\n");
884 if (buddy->cal_working_hours) {
885 sipe_cal_get_today_work_hours(buddy->cal_working_hours, &start, &end, &next_start);
887 purple_debug_info("sipe", "Remote now timezone : %s\n", sipe_cal_get_tz(buddy->cal_working_hours, now));
888 purple_debug_info("sipe", "std.switch_time(GMT): %s",
889 IS((*buddy->cal_working_hours).std.switch_time) ? asctime(gmtime(&((*buddy->cal_working_hours).std.switch_time))) : "\n");
890 purple_debug_info("sipe", "dst.switch_time(GMT): %s",
891 IS((*buddy->cal_working_hours).dst.switch_time) ? asctime(gmtime(&((*buddy->cal_working_hours).dst.switch_time))) : "\n");
892 purple_debug_info("sipe", "Remote now time : %s",
893 asctime(sipe_localtime_tz(&now, sipe_cal_get_tz(buddy->cal_working_hours, now))));
894 purple_debug_info("sipe", "Remote start time : %s",
895 IS(start) ? asctime(sipe_localtime_tz(&start, sipe_cal_get_tz(buddy->cal_working_hours, start))) : "\n");
896 purple_debug_info("sipe", "Remote end time : %s",
897 IS(end) ? asctime(sipe_localtime_tz(&end, sipe_cal_get_tz(buddy->cal_working_hours, end))) : "\n");
898 purple_debug_info("sipe", "Rem. next_start time: %s",
899 IS(next_start) ? asctime(sipe_localtime_tz(&next_start, sipe_cal_get_tz(buddy->cal_working_hours, next_start))) : "\n");
900 purple_debug_info("sipe", "Remote switch time : %s",
901 IS(switch_time) ? asctime(sipe_localtime_tz(&switch_time, sipe_cal_get_tz(buddy->cal_working_hours, switch_time))) : "\n");
902 } else {
903 purple_debug_info("sipe", "Local now time : %s",
904 asctime(localtime(&now)));
905 purple_debug_info("sipe", "Local switch time : %s",
906 IS(switch_time) ? asctime(localtime(&switch_time)) : "\n");
908 purple_debug_info("sipe", "Calendar End (GMT) : %s", asctime(gmtime(&cal_end)));
909 purple_debug_info("sipe", "current cal state : %s\n", cal_states[current_cal_state]);
910 purple_debug_info("sipe", "switch cal state : %s\n", cal_states[to_state] );
912 /* Calendar: string calculations */
915 ALGORITHM (don't delete)
916 (c)2009,2010 pier11 <pier11@operamail.com>
918 SOD = Start of Work Day
919 EOD = End of Work Day
920 NSOD = Start of tomorrow's Work Day
921 SW = Calendar status switch time
923 if current_cal_state == Free
924 until = min_t of SOD, EOD, NSOD, SW (min_t(x) = min(x-now) where x>now only)
925 else
926 until = SW
928 if (!until && (cal_period_end > now + 8H))
929 until = cal_period_end
931 if (!until)
932 return "Currently %", current_cal_state
934 if (until - now > 8H)
935 if (current_cal_state == Free && (work_hours && !in work_hours(now)))
936 return "Outside of working hours for next 8 hours"
937 else
938 return "%s for next 8 hours", current_cal_state
940 if (current_cal_state == Free)
941 if (work_hours && until !in work_hours(now))
942 "Not working"
943 else
944 "%s", current_cal_state
945 " until %.2d:%.2d", until
946 else
947 "Currently %", current_cal_state
948 if (work_hours && until !in work_hours(until))
949 ". Outside of working hours at at %.2d:%.2d", until
950 else
951 ". %s at %.2d:%.2d", to_state, until
954 if (current_cal_state < 1) { /* Free */
955 until = sipe_cal_get_until(now, switch_time, start, end, next_start);
956 } else {
957 until = switch_time;
960 if (!IS(until) && (cal_end - now > 8*60*60))
961 until = cal_end;
963 if (!IS(until)) {
964 return g_strdup_printf(_("Currently %s"), cal_states[current_cal_state]);
967 if (until - now > 8*60*60) {
968 /* Free & outside work hours */
969 if (current_cal_state < 1 && has_working_hours && !sipe_cal_is_in_work_hours(now, start, end)) {
970 return g_strdup(_("Outside of working hours for next 8 hours"));
971 } else {
972 return g_strdup_printf(_("%s for next 8 hours"), cal_states[current_cal_state]);
976 if (current_cal_state < 1) { /* Free */
977 const char *tmp;
978 struct tm *until_tm = localtime(&until);
980 if (has_working_hours && !sipe_cal_is_in_work_hours(now, start, end)) {
981 tmp = _("Not working");
982 } else {
983 tmp = cal_states[current_cal_state];
985 return g_strdup_printf(_("%s until %.2d:%.2d"), tmp, until_tm->tm_hour, until_tm->tm_min);
986 } else { /* Tentative or Busy or OOF */
987 char *tmp;
988 char *res;
989 struct tm *until_tm = localtime(&until);
991 tmp = g_strdup_printf(_("Currently %s"), cal_states[current_cal_state]);
992 if (has_working_hours && !sipe_cal_is_in_work_hours(until, start, end)) {
993 res = g_strdup_printf(_("%s. Outside of working hours at %.2d:%.2d"),
994 tmp, until_tm->tm_hour, until_tm->tm_min);
995 g_free(tmp);
996 return res;
997 } else {
998 res = g_strdup_printf(_("%s. %s at %.2d:%.2d"), tmp, cal_states[to_state], until_tm->tm_hour, until_tm->tm_min);
999 g_free(tmp);
1000 return res;
1003 /* End of - Calendar: string calculations */
1007 Local Variables:
1008 mode: c
1009 c-file-style: "bsd"
1010 indent-tabs-mode: t
1011 tab-width: 8
1012 End: