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
39 #include "sipe-utils.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
50 <WorkingHours xmlns="http://schemas.microsoft.com/exchange/services/2006/types">
56 <DayOrder>1</DayOrder>
58 <DayOfWeek>Sunday</DayOfWeek>
63 <DayOrder>2</DayOrder>
65 <DayOfWeek>Sunday</DayOfWeek>
70 <DayOfWeek>Monday Tuesday Wednesday Thursday Friday</DayOfWeek>
71 <StartTimeInMinutes>600</StartTimeInMinutes>
72 <EndTimeInMinutes>1140</EndTimeInMinutes>
81 <DayOrder>short</DayOrder>
83 <DayOfWeek>Sunday or Monday or Tuesday or Wednesday or Thursday or Friday or Saturday</DayOfWeek>
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 */
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",
125 sipe_cal_get_wday(char *wday_name
)
129 if (!wday_name
) return -1;
131 for (i
= 0; i
< 7; i
++) {
132 if (sipe_strequal(wday_names
[i
], wday_name
)) {
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
);
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
);
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
: "",
189 sipe_setenv(const char *name
,
193 setenv(name
, value
, 1);
195 int len
= strlen(name
) + 1 + strlen(value
) + 1;
196 char *str
= g_malloc0(len
);
197 sprintf(str
, "%s=%s", name
, value
);
203 sipe_unsetenv(const char *name
)
208 int len
= strlen(name
) + 1 + 1;
209 char *str
= g_malloc0(len
);
210 sprintf(str
, "%s=", name
);
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
223 sipe_mktime_tz(struct tm
*tm
,
229 tz_old
= getenv("TZ");
230 sipe_setenv("TZ", tz
);
236 sipe_setenv("TZ", tz_old
);
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
253 sipe_localtime_tz(const time_t *time
,
259 tz_old
= getenv("TZ");
260 sipe_setenv("TZ", tz
);
263 ret
= localtime(time
);
266 sipe_setenv("TZ", tz_old
);
276 sipe_cal_free_working_hours(struct sipe_cal_working_hours
*wh
)
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
);
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.
301 sipe_cal_get_std_dst_time(time_t now
,
303 struct sipe_cal_std_dst
* std_dst
,
304 struct sipe_cal_std_dst
* dst_std
)
307 time_t res
= TIME_NULL
;
308 struct tm
*gm_now_tm
;
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;
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
);
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
;
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;
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
;
357 if (!xn_std_dst_time
) return;
358 if (!std_dst
) return;
362 <Time>02:00:00</Time>
363 <DayOrder>1</DayOrder>
365 <DayOfWeek>Sunday</DayOfWeek>
369 if ((node
= sipe_xml_child(xn_std_dst_time
, "Bias"))) {
370 std_dst
->bias
= atoi(tmp
= sipe_xml_data(node
));
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
));
383 if ((node
= sipe_xml_child(xn_std_dst_time
, "Month"))) {
384 std_dst
->month
= atoi(tmp
= sipe_xml_data(node
));
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
);
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
;
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">
420 <DayOfWeek>Monday Tuesday Wednesday Thursday Friday</DayOfWeek>
421 <StartTimeInMinutes>600</StartTimeInMinutes>
422 <EndTimeInMinutes>1140</EndTimeInMinutes>
424 </WorkingPeriodArray>
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");
433 buddy
->cal_working_hours
->bias
= atoi(tmp
= sipe_xml_data(xn_bias
));
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")));
454 buddy
->cal_working_hours
->end_time
=
455 atoi(tmp
= sipe_xml_data(sipe_xml_child(xn_working_period
, "EndTimeInMinutes")));
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
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);
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
;
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
)
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
) {
523 sipe_cal_get_status0(const gchar
*free_busy
,
526 time_t time_in_question
,
529 int res
= SIPE_CAL_NO_DATA
;
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);
540 res
= free_busy
[shift
] - '0';
546 * Returns time when current calendar state started
549 sipe_cal_get_since_time(const gchar
*free_busy
,
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
;
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
,
580 const char* free_busy
;
581 int ret
= SIPE_CAL_NO_DATA
;
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
,
601 buddy
->cal_granularity
,
604 state_since
= sipe_cal_get_since_time(free_busy
,
606 buddy
->cal_granularity
,
610 if (since
) *since
= state_since
;
615 sipe_cal_get_switch_time(const gchar
*free_busy
,
623 time_t ret
= TIME_NULL
;
625 if ((index
< 0) || ((size_t) (index
+ 1) > strlen(free_busy
))) {
626 *to_state
= SIPE_CAL_NO_DATA
;
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;
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
) {
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
) {
659 } else { /* South hemisphere - Australia */
660 if (time_in_question
>= dst_switch_time
|| time_in_question
< std_switch_time
) {
673 sipe_cal_mktime_of_day(struct tm
*sample_today_tm
,
674 const int shift_minutes
,
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.
690 sipe_cal_get_today_work_hours(struct sipe_cal_working_hours
*wh
,
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 */
702 *next_start
= TIME_NULL
;
706 *end
= sipe_cal_mktime_of_day(remote_now_tm
, wh
->end_time
, tz
);
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
));
725 sipe_cal_is_in_work_hours(const time_t time_in_question
,
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.
737 sipe_cal_get_until(const time_t now
,
738 const time_t switch_time
,
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
;
750 if (IS(start
) && start
> now
&& (start
- now
) < min_diff
) {
751 min_diff
= start
- now
;
754 if (IS(end
) && end
> now
&& (end
- now
) < min_diff
) {
755 min_diff
= end
- now
;
758 if (IS(next_start
) && next_start
> now
&& (next_start
- now
) < min_diff
) {
759 min_diff
= next_start
- now
;
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
) {
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
783 11, Out of facility (Oo)
785 http://msdn.microsoft.com/en-us/library/aa566048.aspx
789 3 Out of Office (OOF)
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';
804 return buddy
->cal_free_busy
;
808 sipe_cal_get_freebusy_base64(const char* freebusy_hex
)
812 guint shift_factor
= 0;
817 if (!freebusy_hex
) return NULL
;
819 len
= strlen(freebusy_hex
);
820 res_len
= len
/ 4 + 1;
821 res
= g_malloc0(res_len
);
823 res
[j
] |= (freebusy_hex
[i
++] - '0') << shift_factor
;
825 if (shift_factor
== 8) {
831 res_base64
= g_base64_encode(res
, shift_factor
? res_len
: res_len
- 1);
837 sipe_cal_get_description(struct sipe_buddy
*buddy
)
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
;
847 int to_state
= SIPE_CAL_NO_DATA
;
848 time_t until
= TIME_NULL
;
850 gboolean has_working_hours
= (buddy
->cal_working_hours
!= NULL
);
851 const char *free_busy
;
852 const char *cal_states
[] = {_("Free"),
858 if (buddy
->cal_granularity
!= 15) {
859 purple_debug_info("sipe", "sipe_cal_get_description: granularity %d is unsupported, exiting.\n", buddy
->cal_granularity
);
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");
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");
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");
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)
928 if (!until && (cal_period_end > now + 8H))
929 until = cal_period_end
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"
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))
944 "%s", current_cal_state
945 " until %.2d:%.2d", until
947 "Currently %", current_cal_state
948 if (work_hours && until !in work_hours(until))
949 ". Outside of working hours at at %.2d:%.2d", until
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
);
960 if (!IS(until
) && (cal_end
- now
> 8*60*60))
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"));
972 return g_strdup_printf(_("%s for next 8 hours"), cal_states
[current_cal_state
]);
976 if (current_cal_state
< 1) { /* Free */
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");
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 */
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
);
998 res
= g_strdup_printf(_("%s. %s at %.2d:%.2d"), tmp
, cal_states
[to_state
], until_tm
->tm_hour
, until_tm
->tm_min
);
1003 /* End of - Calendar: string calculations */