core cleanup: xmlnode replacement in sipe-cal.c
[siplcs.git] / src / core / sipe-cal.c
blob3b5921edabcca8a509e843d74c1e218f1b242cbd
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>
31 #include <glib.h>
33 #include "debug.h"
35 #include "sipe.h"
36 #include "sipe-cal.h"
37 #include "sipe-utils.h"
38 #include "sipe-nls.h"
41 #define TIME_NULL (time_t)-1
42 #define IS(time) (time != TIME_NULL)
45 http://msdn.microsoft.com/en-us/library/aa565001.aspx
47 <?xml version="1.0"?>
48 <WorkingHours xmlns="http://schemas.microsoft.com/exchange/services/2006/types">
49 <TimeZone>
50 <Bias>480</Bias>
51 <StandardTime>
52 <Bias>0</Bias>
53 <Time>02:00:00</Time>
54 <DayOrder>1</DayOrder>
55 <Month>11</Month>
56 <DayOfWeek>Sunday</DayOfWeek>
57 </StandardTime>
58 <DaylightTime>
59 <Bias>-60</Bias>
60 <Time>02:00:00</Time>
61 <DayOrder>2</DayOrder>
62 <Month>3</Month>
63 <DayOfWeek>Sunday</DayOfWeek>
64 </DaylightTime>
65 </TimeZone>
66 <WorkingPeriodArray>
67 <WorkingPeriod>
68 <DayOfWeek>Monday Tuesday Wednesday Thursday Friday</DayOfWeek>
69 <StartTimeInMinutes>600</StartTimeInMinutes>
70 <EndTimeInMinutes>1140</EndTimeInMinutes>
71 </WorkingPeriod>
72 </WorkingPeriodArray>
73 </WorkingHours>
75 Desc:
76 <StandardTime>
77 <Bias>int</Bias>
78 <Time>string</Time>
79 <DayOrder>short</DayOrder>
80 <Month>short</Month>
81 <DayOfWeek>Sunday or Monday or Tuesday or Wednesday or Thursday or Friday or Saturday</DayOfWeek>
82 <Year>string</Year>
83 </StandardTime>
86 struct sipe_cal_std_dst {
87 int bias; /* Ex.: -60 */
88 gchar *time; /* hh:mm:ss, 02:00:00 */
89 int day_order; /* 1..5 */
90 int month; /* 1..12 */
91 gchar *day_of_week; /* Sunday or Monday or Tuesday or Wednesday or Thursday or Friday or Saturday */
92 gchar *year; /* YYYY */
94 time_t switch_time;
97 struct sipe_cal_working_hours {
98 int bias; /* Ex.: 480 */
99 struct sipe_cal_std_dst std; /* StandardTime */
100 struct sipe_cal_std_dst dst; /* DaylightTime */
101 gchar *days_of_week; /* Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday separated by space */
102 int start_time; /* 0...1440 */
103 int end_time; /* 0...1440 */
105 gchar *tz; /* aggregated timezone string as in TZ environment variable.
106 Ex.: TST+8TDT+7,M3.2.0/02:00:00,M11.1.0/02:00:00 */
107 /** separate simple strings for Windows platform as the proper TZ does not work there.
108 * anyway, dynamic timezones would't work with just TZ
110 gchar *tz_std; /* Ex.: TST8 */
111 gchar *tz_dst; /* Ex.: TDT7 */
114 /* not for translation, a part of XML Schema definitions */
115 static const char *wday_names[] = {"Sunday",
116 "Monday",
117 "Tuesday",
118 "Wednesday",
119 "Thursday",
120 "Friday",
121 "Saturday"};
122 static int
123 sipe_cal_get_wday(char *wday_name)
125 int i;
127 if (!wday_name) return -1;
129 for (i = 0; i < 7; i++) {
130 if (sipe_strequal(wday_names[i], wday_name)) {
131 return i;
135 return -1;
138 void
139 sipe_cal_event_free(struct sipe_cal_event* cal_event)
141 if (!cal_event) return;
143 g_free(cal_event->subject);
144 g_free(cal_event->location);
145 g_free(cal_event);
148 char *
149 sipe_cal_event_describe(struct sipe_cal_event* cal_event)
151 GString* str = g_string_new(NULL);
152 const char *status = "";
154 switch(cal_event->cal_status) {
155 case SIPE_CAL_FREE: status = "SIPE_CAL_FREE"; break;
156 case SIPE_CAL_TENTATIVE: status = "SIPE_CAL_TENTATIVE"; break;
157 case SIPE_CAL_BUSY: status = "SIPE_CAL_BUSY"; break;
158 case SIPE_CAL_OOF: status = "SIPE_CAL_OOF"; break;
159 case SIPE_CAL_NO_DATA: status = "SIPE_CAL_NO_DATA"; break;
162 g_string_append_printf(str, "\t%s: %s", "start_time",
163 IS(cal_event->start_time) ? asctime(localtime(&cal_event->start_time)) : "\n");
164 g_string_append_printf(str, "\t%s: %s", "end_time ",
165 IS(cal_event->end_time) ? asctime(localtime(&cal_event->end_time)) : "\n");
166 g_string_append_printf(str, "\t%s: %s\n", "cal_status", status);
167 g_string_append_printf(str, "\t%s: %s\n", "subject ", cal_event->subject ? cal_event->subject : "");
168 g_string_append_printf(str, "\t%s: %s\n", "location ", cal_event->location ? cal_event->location : "");
169 g_string_append_printf(str, "\t%s: %s\n", "is_meeting", cal_event->is_meeting ? "TRUE" : "FALSE");
171 return g_string_free(str, FALSE);
174 char *
175 sipe_cal_event_hash(struct sipe_cal_event* event)
177 /* no end_time as it dos not get published */
178 /* no cal_status as it can change on publication */
179 return g_strdup_printf("<%d><%s><%s><%d>",
180 (int)event->start_time,
181 event->subject ? event->subject : "",
182 event->location ? event->location : "",
183 event->is_meeting);
186 static void
187 sipe_setenv(const char *name,
188 const char *value)
190 #ifdef HAVE_SETENV
191 setenv(name, value, 1);
192 #else
193 int len = strlen(name) + 1 + strlen(value) + 1;
194 char *str = g_malloc0(len);
195 sprintf(str, "%s=%s", name, value);
196 putenv(str);
197 #endif
200 static void
201 sipe_unsetenv(const char *name)
203 #ifdef HAVE_UNSETENV
204 unsetenv(name);
205 #else
206 int len = strlen(name) + 1 + 1;
207 char *str = g_malloc0(len);
208 sprintf(str, "%s=", name);
209 putenv(str);
210 #endif
214 * Converts struct tm to Epoch time_t considering timezone.
216 * @param tz as defined for TZ environment variable.
218 * Reference: see timegm(3) - Linux man page
220 time_t
221 sipe_mktime_tz(struct tm *tm,
222 const char* tz)
224 time_t ret;
225 char *tz_old;
227 tz_old = getenv("TZ");
228 sipe_setenv("TZ", tz);
229 tzset();
231 ret = mktime(tm);
233 if (tz_old) {
234 sipe_setenv("TZ", tz_old);
235 } else {
236 sipe_unsetenv("TZ");
238 tzset();
240 return ret;
244 * Converts Epoch time_t to struct tm considering timezone.
246 * @param tz as defined for TZ environment variable.
248 * Reference: see timegm(3) - Linux man page
250 static struct tm *
251 sipe_localtime_tz(const time_t *time,
252 const char* tz)
254 struct tm *ret;
255 char *tz_old;
257 tz_old = getenv("TZ");
258 sipe_setenv("TZ", tz);
259 tzset();
261 ret = localtime(time);
263 if (tz_old) {
264 sipe_setenv("TZ", tz_old);
265 } else {
266 sipe_unsetenv("TZ");
268 tzset();
270 return ret;
273 void
274 sipe_cal_free_working_hours(struct sipe_cal_working_hours *wh)
276 if (!wh) return;
278 g_free(wh->std.time);
279 g_free(wh->std.day_of_week);
280 g_free(wh->std.year);
282 g_free(wh->dst.time);
283 g_free(wh->dst.day_of_week);
284 g_free(wh->dst.year);
286 g_free(wh->days_of_week);
287 g_free(wh->tz);
288 g_free(wh->tz_std);
289 g_free(wh->tz_dst);
290 g_free(wh);
294 * Returns time_t of daylight savings time start/end
295 * in the provided timezone or otherwise
296 * (time_t)-1 if no daylight savings time.
298 static time_t
299 sipe_cal_get_std_dst_time(time_t now,
300 int bias,
301 struct sipe_cal_std_dst* std_dst,
302 struct sipe_cal_std_dst* dst_std)
304 struct tm switch_tm;
305 time_t res = TIME_NULL;
306 struct tm *gm_now_tm;
307 gchar **time_arr;
309 if (std_dst->month == 0) return TIME_NULL;
311 gm_now_tm = gmtime(&now);
312 time_arr = g_strsplit(std_dst->time, ":", 0);
314 switch_tm.tm_sec = atoi(time_arr[2]);
315 switch_tm.tm_min = atoi(time_arr[1]);
316 switch_tm.tm_hour = atoi(time_arr[0]);
317 g_strfreev(time_arr);
318 switch_tm.tm_mday = std_dst->year ? std_dst->day_order : 1 /* to adjust later */ ;
319 switch_tm.tm_mon = std_dst->month - 1;
320 switch_tm.tm_year = std_dst->year ? atoi(std_dst->year) - 1900 : gm_now_tm->tm_year;
321 switch_tm.tm_isdst = 0;
322 /* to set tm_wday */
323 res = sipe_mktime_tz(&switch_tm, "UTC");
325 /* if not dynamic, calculate right tm_mday */
326 if (!std_dst->year) {
327 int switch_wday = sipe_cal_get_wday(std_dst->day_of_week);
328 int needed_month;
329 /* get first desired wday in the month */
330 int delta = switch_wday >= switch_tm.tm_wday ? (switch_wday - switch_tm.tm_wday) : (switch_wday + 7 - switch_tm.tm_wday);
331 switch_tm.tm_mday = 1 + delta;
332 /* try nth order */
333 switch_tm.tm_mday += (std_dst->day_order - 1) * 7;
334 needed_month = switch_tm.tm_mon;
335 /* to set settle date if ahead of allowed month dates */
336 res = sipe_mktime_tz(&switch_tm, "UTC");
337 if (needed_month != switch_tm.tm_mon) {
338 /* moving 1 week back to stay within required month */
339 switch_tm.tm_mday -= 7;
340 /* to fix date again */
341 res = sipe_mktime_tz(&switch_tm, "UTC");
344 /* note: bias is taken from "switch to" structure */
345 return res + (bias + dst_std->bias)*60;
348 static void
349 sipe_cal_parse_std_dst(const sipe_xml *xn_std_dst_time,
350 struct sipe_cal_std_dst *std_dst)
352 const sipe_xml *node;
353 gchar *tmp;
355 if (!xn_std_dst_time) return;
356 if (!std_dst) return;
358 <StandardTime>
359 <Bias>0</Bias>
360 <Time>02:00:00</Time>
361 <DayOrder>1</DayOrder>
362 <Month>11</Month>
363 <DayOfWeek>Sunday</DayOfWeek>
364 </StandardTime>
367 if ((node = sipe_xml_child(xn_std_dst_time, "Bias"))) {
368 std_dst->bias = atoi(tmp = sipe_xml_data(node));
369 g_free(tmp);
372 if ((node = sipe_xml_child(xn_std_dst_time, "Time"))) {
373 std_dst->time = sipe_xml_data(node);
376 if ((node = sipe_xml_child(xn_std_dst_time, "DayOrder"))) {
377 std_dst->day_order = atoi(tmp = sipe_xml_data(node));
378 g_free(tmp);
381 if ((node = sipe_xml_child(xn_std_dst_time, "Month"))) {
382 std_dst->month = atoi(tmp = sipe_xml_data(node));
383 g_free(tmp);
386 if ((node = sipe_xml_child(xn_std_dst_time, "DayOfWeek"))) {
387 std_dst->day_of_week = sipe_xml_data(node);
390 if ((node = sipe_xml_child(xn_std_dst_time, "Year"))) {
391 std_dst->year = sipe_xml_data(node);
395 void
396 sipe_cal_parse_working_hours(const sipe_xml *xn_working_hours,
397 struct sipe_buddy *buddy)
399 const sipe_xml *xn_bias;
400 const sipe_xml *xn_timezone;
401 const sipe_xml *xn_working_period;
402 const sipe_xml *xn_standard_time;
403 const sipe_xml *xn_daylight_time;
404 gchar *tmp;
405 time_t now = time(NULL);
406 struct sipe_cal_std_dst* std;
407 struct sipe_cal_std_dst* dst;
409 if (!xn_working_hours) return;
411 <WorkingHours xmlns="http://schemas.microsoft.com/exchange/services/2006/types">
412 <TimeZone>
413 <Bias>480</Bias>
415 </TimeZone>
416 <WorkingPeriodArray>
417 <WorkingPeriod>
418 <DayOfWeek>Monday Tuesday Wednesday Thursday Friday</DayOfWeek>
419 <StartTimeInMinutes>600</StartTimeInMinutes>
420 <EndTimeInMinutes>1140</EndTimeInMinutes>
421 </WorkingPeriod>
422 </WorkingPeriodArray>
423 </WorkingHours>
425 sipe_cal_free_working_hours(buddy->cal_working_hours);
426 buddy->cal_working_hours = g_new0(struct sipe_cal_working_hours, 1);
428 xn_timezone = sipe_xml_child(xn_working_hours, "TimeZone");
429 xn_bias = sipe_xml_child(xn_timezone, "Bias");
430 if (xn_bias) {
431 buddy->cal_working_hours->bias = atoi(tmp = sipe_xml_data(xn_bias));
432 g_free(tmp);
435 xn_standard_time = sipe_xml_child(xn_timezone, "StandardTime");
436 xn_daylight_time = sipe_xml_child(xn_timezone, "DaylightTime");
438 std = &((*buddy->cal_working_hours).std);
439 dst = &((*buddy->cal_working_hours).dst);
440 sipe_cal_parse_std_dst(xn_standard_time, std);
441 sipe_cal_parse_std_dst(xn_daylight_time, dst);
443 xn_working_period = sipe_xml_child(xn_working_hours, "WorkingPeriodArray/WorkingPeriod");
444 if (xn_working_period) {
445 buddy->cal_working_hours->days_of_week =
446 sipe_xml_data(sipe_xml_child(xn_working_period, "DayOfWeek"));
448 buddy->cal_working_hours->start_time =
449 atoi(tmp = sipe_xml_data(sipe_xml_child(xn_working_period, "StartTimeInMinutes")));
450 g_free(tmp);
452 buddy->cal_working_hours->end_time =
453 atoi(tmp = sipe_xml_data(sipe_xml_child(xn_working_period, "EndTimeInMinutes")));
454 g_free(tmp);
457 std->switch_time = sipe_cal_get_std_dst_time(now, buddy->cal_working_hours->bias, std, dst);
458 dst->switch_time = sipe_cal_get_std_dst_time(now, buddy->cal_working_hours->bias, dst, std);
460 /* TST8TDT7,M3.2.0/02:00:00,M11.1.0/02:00:00 */
461 buddy->cal_working_hours->tz =
462 g_strdup_printf("TST%dTDT%d,M%d.%d.%d/%s,M%d.%d.%d/%s",
463 (buddy->cal_working_hours->bias + buddy->cal_working_hours->std.bias) / 60,
464 (buddy->cal_working_hours->bias + buddy->cal_working_hours->dst.bias) / 60,
466 buddy->cal_working_hours->dst.month,
467 buddy->cal_working_hours->dst.day_order,
468 sipe_cal_get_wday(buddy->cal_working_hours->dst.day_of_week),
469 buddy->cal_working_hours->dst.time,
471 buddy->cal_working_hours->std.month,
472 buddy->cal_working_hours->std.day_order,
473 sipe_cal_get_wday(buddy->cal_working_hours->std.day_of_week),
474 buddy->cal_working_hours->std.time
476 /* TST8 */
477 buddy->cal_working_hours->tz_std =
478 g_strdup_printf("TST%d",
479 (buddy->cal_working_hours->bias + buddy->cal_working_hours->std.bias) / 60);
480 /* TDT7 */
481 buddy->cal_working_hours->tz_dst =
482 g_strdup_printf("TDT%d",
483 (buddy->cal_working_hours->bias + buddy->cal_working_hours->dst.bias) / 60);
486 struct sipe_cal_event*
487 sipe_cal_get_event(GSList *cal_events,
488 time_t time_in_question)
490 GSList *entry = cal_events;
491 struct sipe_cal_event* cal_event;
492 struct sipe_cal_event* res = NULL;
494 if (!cal_events || !IS(time_in_question)) return NULL;
496 while (entry) {
497 cal_event = entry->data;
498 /* event is in the past or in the future */
499 if (cal_event->start_time > time_in_question ||
500 cal_event->end_time <= time_in_question)
502 entry = entry->next;
503 continue;
506 if (!res) {
507 res = cal_event;
508 } else {
509 int res_status = (res->cal_status == SIPE_CAL_NO_DATA) ? -1 : res->cal_status;
510 int cal_status = (cal_event->cal_status == SIPE_CAL_NO_DATA) ? -1 : cal_event->cal_status;
511 if (res_status < cal_status) {
512 res = cal_event;
515 entry = entry->next;
517 return res;
520 static int
521 sipe_cal_get_status0(const gchar *free_busy,
522 time_t cal_start,
523 int granularity,
524 time_t time_in_question,
525 int *index)
527 int res = SIPE_CAL_NO_DATA;
528 int shift;
529 time_t cal_end = cal_start + strlen(free_busy)*granularity*60 - 1;
531 if (!(time_in_question >= cal_start && time_in_question <= cal_end)) return res;
533 shift = (time_in_question - cal_start) / (granularity*60);
534 if (index) {
535 *index = shift;
538 res = free_busy[shift] - '0';
540 return res;
544 * Returns time when current calendar state started
546 static time_t
547 sipe_cal_get_since_time(const gchar *free_busy,
548 time_t calStart,
549 int granularity,
550 int index,
551 int current_state)
553 int i;
555 if ((index < 0) || ((size_t)(index + 1) > strlen(free_busy))) return 0;
557 for (i = index; i >= 0; i--) {
558 int temp_status = free_busy[i] - '0';
560 if (current_state != temp_status) {
561 return calStart + (i + 1)*granularity*60;
564 if (i == 0) return calStart;
567 return 0;
569 static char*
570 sipe_cal_get_free_busy(struct sipe_buddy *buddy);
573 sipe_cal_get_status(struct sipe_buddy *buddy,
574 time_t time_in_question,
575 time_t *since)
577 time_t cal_start;
578 const char* free_busy;
579 int ret = SIPE_CAL_NO_DATA;
580 time_t state_since;
581 int index;
583 if (!buddy || !buddy->cal_start_time || !buddy->cal_granularity) {
584 purple_debug_info("sipe", "sipe_cal_get_status: no calendar data1 for %s, exiting\n",
585 buddy ? (buddy->name ? buddy->name : "") : "");
586 return SIPE_CAL_NO_DATA;
589 if (!(free_busy = sipe_cal_get_free_busy(buddy))) {
590 purple_debug_info("sipe", "sipe_cal_get_status: no calendar data2 for %s, exiting\n", buddy->name);
591 return SIPE_CAL_NO_DATA;
593 purple_debug_info("sipe", "sipe_cal_get_description: buddy->cal_free_busy=\n%s\n", free_busy);
595 cal_start = sipe_utils_str_to_time(buddy->cal_start_time);
597 ret = sipe_cal_get_status0(free_busy,
598 cal_start,
599 buddy->cal_granularity,
600 time_in_question,
601 &index);
602 state_since = sipe_cal_get_since_time(free_busy,
603 cal_start,
604 buddy->cal_granularity,
605 index,
606 ret);
608 if (since) *since = state_since;
609 return ret;
612 static time_t
613 sipe_cal_get_switch_time(const gchar *free_busy,
614 time_t calStart,
615 int granularity,
616 int index,
617 int current_state,
618 int *to_state)
620 size_t i;
621 time_t ret = TIME_NULL;
623 if ((index < 0) || ((size_t) (index + 1) > strlen(free_busy))) {
624 *to_state = SIPE_CAL_NO_DATA;
625 return ret;
628 for (i = index + 1; i < strlen(free_busy); i++) {
629 int temp_status = free_busy[i] - '0';
631 if (current_state != temp_status) {
632 *to_state = temp_status;
633 return calStart + i*granularity*60;
637 return ret;
640 static const char*
641 sipe_cal_get_tz(struct sipe_cal_working_hours *wh,
642 time_t time_in_question)
644 time_t dst_switch_time = (*wh).dst.switch_time;
645 time_t std_switch_time = (*wh).std.switch_time;
646 gboolean is_dst = FALSE;
648 /* No daylight savings */
649 if (dst_switch_time == TIME_NULL) {
650 return wh->tz_std;
653 if (dst_switch_time < std_switch_time) { /* North hemosphere - Europe, US */
654 if (time_in_question >= dst_switch_time && time_in_question < std_switch_time) {
655 is_dst = TRUE;
657 } else { /* South hemisphere - Australia */
658 if (time_in_question >= dst_switch_time || time_in_question < std_switch_time) {
659 is_dst = TRUE;
663 if (is_dst) {
664 return wh->tz_dst;
665 } else {
666 return wh->tz_std;
670 static time_t
671 sipe_cal_mktime_of_day(struct tm *sample_today_tm,
672 const int shift_minutes,
673 const char *tz)
675 sample_today_tm->tm_sec = 0;
676 sample_today_tm->tm_min = shift_minutes % 60;
677 sample_today_tm->tm_hour = shift_minutes / 60;
679 return sipe_mktime_tz(sample_today_tm, tz);
683 * Returns work day start and end in Epoch time
684 * considering the initial values are provided
685 * in contact's local time zone.
687 static void
688 sipe_cal_get_today_work_hours(struct sipe_cal_working_hours *wh,
689 time_t *start,
690 time_t *end,
691 time_t *next_start)
693 time_t now = time(NULL);
694 const char *tz = sipe_cal_get_tz(wh, now);
695 struct tm *remote_now_tm = sipe_localtime_tz(&now, tz);
697 if (!strstr(wh->days_of_week, wday_names[remote_now_tm->tm_wday])) { /* not a work day */
698 *start = TIME_NULL;
699 *end = TIME_NULL;
700 *next_start = TIME_NULL;
701 return;
704 *end = sipe_cal_mktime_of_day(remote_now_tm, wh->end_time, tz);
706 if (now < *end) {
707 *start = sipe_cal_mktime_of_day(remote_now_tm, wh->start_time, tz);
708 *next_start = TIME_NULL;
709 } else { /* calculate start of tomorrow's work day if any */
710 time_t tom = now + 24*60*60;
711 struct tm *remote_tom_tm = sipe_localtime_tz(&tom, sipe_cal_get_tz(wh, tom));
713 if (!strstr(wh->days_of_week, wday_names[remote_tom_tm->tm_wday])) { /* not a work day */
714 *next_start = TIME_NULL;
717 *next_start = sipe_cal_mktime_of_day(remote_tom_tm, wh->start_time, sipe_cal_get_tz(wh, tom));
718 *start = TIME_NULL;
722 static int
723 sipe_cal_is_in_work_hours(const time_t time_in_question,
724 const time_t start,
725 const time_t end)
727 return !((time_in_question >= end) || (IS(start) && time_in_question < start));
731 * Returns time closest to now. Choses only from times ahead of now.
732 * Returns TIME_NULL otherwise.
734 static time_t
735 sipe_cal_get_until(const time_t now,
736 const time_t switch_time,
737 const time_t start,
738 const time_t end,
739 const time_t next_start)
741 time_t ret = TIME_NULL;
742 int min_diff = now - ret;
744 if (IS(switch_time) && switch_time > now && (switch_time - now) < min_diff) {
745 min_diff = switch_time - now;
746 ret = switch_time;
748 if (IS(start) && start > now && (start - now) < min_diff) {
749 min_diff = start - now;
750 ret = start;
752 if (IS(end) && end > now && (end - now) < min_diff) {
753 min_diff = end - now;
754 ret = end;
756 if (IS(next_start) && next_start > now && (next_start - now) < min_diff) {
757 min_diff = next_start - now;
758 ret = next_start;
760 return ret;
763 static char*
764 sipe_cal_get_free_busy(struct sipe_buddy *buddy)
766 /* do lazy decode if necessary */
767 if (!buddy->cal_free_busy && buddy->cal_free_busy_base64) {
768 gsize cal_dec64_len;
769 guchar *cal_dec64;
770 gsize i;
771 int j = 0;
773 cal_dec64 = purple_base64_decode(buddy->cal_free_busy_base64, &cal_dec64_len);
775 buddy->cal_free_busy = g_malloc0(cal_dec64_len * 4 + 1);
777 http://msdn.microsoft.com/en-us/library/dd941537%28office.13%29.aspx
778 00, Free (Fr)
779 01, Tentative (Te)
780 10, Busy (Bu)
781 11, Out of facility (Oo)
783 http://msdn.microsoft.com/en-us/library/aa566048.aspx
784 0 Free
785 1 Tentative
786 2 Busy
787 3 Out of Office (OOF)
788 4 No data
790 for (i = 0; i < cal_dec64_len; i++) {
791 #define TWO_BIT_MASK 0x03
792 char tmp = cal_dec64[i];
793 buddy->cal_free_busy[j++] = (tmp & TWO_BIT_MASK) + '0';
794 buddy->cal_free_busy[j++] = ((tmp >> 2) & TWO_BIT_MASK) + '0';
795 buddy->cal_free_busy[j++] = ((tmp >> 4) & TWO_BIT_MASK) + '0';
796 buddy->cal_free_busy[j++] = ((tmp >> 6) & TWO_BIT_MASK) + '0';
798 buddy->cal_free_busy[j++] = '\0';
799 g_free(cal_dec64);
802 return buddy->cal_free_busy;
805 char *
806 sipe_cal_get_freebusy_base64(const char* freebusy_hex)
808 guint i = 0;
809 guint j = 0;
810 guint shift_factor = 0;
811 guint len, res_len;
812 guchar *res;
813 gchar *res_base64;
815 if (!freebusy_hex) return NULL;
817 len = strlen(freebusy_hex);
818 res_len = len / 4 + 1;
819 res = g_malloc0(res_len);
820 while (i < len) {
821 res[j] |= (freebusy_hex[i++] - '0') << shift_factor;
822 shift_factor += 2;
823 if (shift_factor == 8) {
824 shift_factor = 0;
825 j++;
829 res_base64 = purple_base64_encode(res, shift_factor ? res_len : res_len - 1);
830 g_free(res);
831 return res_base64;
834 char *
835 sipe_cal_get_description(struct sipe_buddy *buddy)
837 time_t cal_start;
838 time_t cal_end;
839 int current_cal_state;
840 time_t now = time(NULL);
841 time_t start = TIME_NULL;
842 time_t end = TIME_NULL;
843 time_t next_start = TIME_NULL;
844 time_t switch_time;
845 int to_state = SIPE_CAL_NO_DATA;
846 time_t until = TIME_NULL;
847 int index = 0;
848 gboolean has_working_hours = (buddy->cal_working_hours != NULL);
849 const char *free_busy;
850 const char *cal_states[] = {_("Free"),
851 _("Tentative"),
852 _("Busy"),
853 _("Out of office"),
854 _("No data")};
856 if (buddy->cal_granularity != 15) {
857 purple_debug_info("sipe", "sipe_cal_get_description: granularity %d is unsupported, exiting.\n", buddy->cal_granularity);
858 return NULL;
861 /* to lazy load if needed */
862 free_busy = sipe_cal_get_free_busy(buddy);
863 purple_debug_info("sipe", "sipe_cal_get_description: buddy->cal_free_busy=\n%s\n", free_busy ? free_busy : "");
865 if (!buddy->cal_free_busy || !buddy->cal_granularity || !buddy->cal_start_time) {
866 purple_debug_info("sipe", "sipe_cal_get_description: no calendar data, exiting");
867 return NULL;
870 cal_start = sipe_utils_str_to_time(buddy->cal_start_time);
871 cal_end = cal_start + 60 * (buddy->cal_granularity) * strlen(buddy->cal_free_busy);
873 current_cal_state = sipe_cal_get_status0(free_busy, cal_start, buddy->cal_granularity, time(NULL), &index);
874 if (current_cal_state == SIPE_CAL_NO_DATA) {
875 purple_debug_info("sipe", "sipe_cal_get_description: calendar is undefined for present moment, exiting.\n");
876 return NULL;
879 switch_time = sipe_cal_get_switch_time(free_busy, cal_start, buddy->cal_granularity, index, current_cal_state, &to_state);
881 purple_debug_info("sipe", "\n* Calendar *\n");
882 if (buddy->cal_working_hours) {
883 sipe_cal_get_today_work_hours(buddy->cal_working_hours, &start, &end, &next_start);
885 purple_debug_info("sipe", "Remote now timezone : %s\n", sipe_cal_get_tz(buddy->cal_working_hours, now));
886 purple_debug_info("sipe", "std.switch_time(GMT): %s",
887 IS((*buddy->cal_working_hours).std.switch_time) ? asctime(gmtime(&((*buddy->cal_working_hours).std.switch_time))) : "\n");
888 purple_debug_info("sipe", "dst.switch_time(GMT): %s",
889 IS((*buddy->cal_working_hours).dst.switch_time) ? asctime(gmtime(&((*buddy->cal_working_hours).dst.switch_time))) : "\n");
890 purple_debug_info("sipe", "Remote now time : %s",
891 asctime(sipe_localtime_tz(&now, sipe_cal_get_tz(buddy->cal_working_hours, now))));
892 purple_debug_info("sipe", "Remote start time : %s",
893 IS(start) ? asctime(sipe_localtime_tz(&start, sipe_cal_get_tz(buddy->cal_working_hours, start))) : "\n");
894 purple_debug_info("sipe", "Remote end time : %s",
895 IS(end) ? asctime(sipe_localtime_tz(&end, sipe_cal_get_tz(buddy->cal_working_hours, end))) : "\n");
896 purple_debug_info("sipe", "Rem. next_start time: %s",
897 IS(next_start) ? asctime(sipe_localtime_tz(&next_start, sipe_cal_get_tz(buddy->cal_working_hours, next_start))) : "\n");
898 purple_debug_info("sipe", "Remote switch time : %s",
899 IS(switch_time) ? asctime(sipe_localtime_tz(&switch_time, sipe_cal_get_tz(buddy->cal_working_hours, switch_time))) : "\n");
900 } else {
901 purple_debug_info("sipe", "Local now time : %s",
902 asctime(localtime(&now)));
903 purple_debug_info("sipe", "Local switch time : %s",
904 IS(switch_time) ? asctime(localtime(&switch_time)) : "\n");
906 purple_debug_info("sipe", "Calendar End (GMT) : %s", asctime(gmtime(&cal_end)));
907 purple_debug_info("sipe", "current cal state : %s\n", cal_states[current_cal_state]);
908 purple_debug_info("sipe", "switch cal state : %s\n", cal_states[to_state] );
910 /* Calendar: string calculations */
913 ALGORITHM (don't delete)
914 (c)2009,2010 pier11 <pier11@operamail.com>
916 SOD = Start of Work Day
917 EOD = End of Work Day
918 NSOD = Start of tomorrow's Work Day
919 SW = Calendar status switch time
921 if current_cal_state == Free
922 until = min_t of SOD, EOD, NSOD, SW (min_t(x) = min(x-now) where x>now only)
923 else
924 until = SW
926 if (!until && (cal_period_end > now + 8H))
927 until = cal_period_end
929 if (!until)
930 return "Currently %", current_cal_state
932 if (until - now > 8H)
933 if (current_cal_state == Free && (work_hours && !in work_hours(now)))
934 return "Outside of working hours for next 8 hours"
935 else
936 return "%s for next 8 hours", current_cal_state
938 if (current_cal_state == Free)
939 if (work_hours && until !in work_hours(now))
940 "Not working"
941 else
942 "%s", current_cal_state
943 " until %.2d:%.2d", until
944 else
945 "Currently %", current_cal_state
946 if (work_hours && until !in work_hours(until))
947 ". Outside of working hours at at %.2d:%.2d", until
948 else
949 ". %s at %.2d:%.2d", to_state, until
952 if (current_cal_state < 1) { /* Free */
953 until = sipe_cal_get_until(now, switch_time, start, end, next_start);
954 } else {
955 until = switch_time;
958 if (!IS(until) && (cal_end - now > 8*60*60))
959 until = cal_end;
961 if (!IS(until)) {
962 return g_strdup_printf(_("Currently %s"), cal_states[current_cal_state]);
965 if (until - now > 8*60*60) {
966 /* Free & outside work hours */
967 if (current_cal_state < 1 && has_working_hours && !sipe_cal_is_in_work_hours(now, start, end)) {
968 return g_strdup(_("Outside of working hours for next 8 hours"));
969 } else {
970 return g_strdup_printf(_("%s for next 8 hours"), cal_states[current_cal_state]);
974 if (current_cal_state < 1) { /* Free */
975 const char *tmp;
976 struct tm *until_tm = localtime(&until);
978 if (has_working_hours && !sipe_cal_is_in_work_hours(now, start, end)) {
979 tmp = _("Not working");
980 } else {
981 tmp = cal_states[current_cal_state];
983 return g_strdup_printf(_("%s until %.2d:%.2d"), tmp, until_tm->tm_hour, until_tm->tm_min);
984 } else { /* Tentative or Busy or OOF */
985 char *tmp;
986 char *res;
987 struct tm *until_tm = localtime(&until);
989 tmp = g_strdup_printf(_("Currently %s"), cal_states[current_cal_state]);
990 if (has_working_hours && !sipe_cal_is_in_work_hours(until, start, end)) {
991 res = g_strdup_printf(_("%s. Outside of working hours at %.2d:%.2d"),
992 tmp, until_tm->tm_hour, until_tm->tm_min);
993 g_free(tmp);
994 return res;
995 } else {
996 res = g_strdup_printf(_("%s. %s at %.2d:%.2d"), tmp, cal_states[to_state], until_tm->tm_hour, until_tm->tm_min);
997 g_free(tmp);
998 return res;
1001 /* End of - Calendar: string calculations */
1005 Local Variables:
1006 mode: c
1007 c-file-style: "bsd"
1008 indent-tabs-mode: t
1009 tab-width: 8
1010 End: