6 * Copyright (C) 2010 SIPE Project <http://sipe.sourceforge.net/>
7 * Copyright (C) 2009 pier11 <pier11@operamail.com>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
35 #include "http-conn.h"
36 #include "sipe-backend.h"
37 #include "sipe-buddy.h"
38 #include "sipe-core.h"
39 #include "sipe-core-private.h"
42 #include "sipe-utils.h"
46 #define TIME_NULL (time_t)-1
47 #define IS(time) (time != TIME_NULL)
50 http://msdn.microsoft.com/en-us/library/aa565001.aspx
53 <WorkingHours xmlns="http://schemas.microsoft.com/exchange/services/2006/types">
59 <DayOrder>1</DayOrder>
61 <DayOfWeek>Sunday</DayOfWeek>
66 <DayOrder>2</DayOrder>
68 <DayOfWeek>Sunday</DayOfWeek>
73 <DayOfWeek>Monday Tuesday Wednesday Thursday Friday</DayOfWeek>
74 <StartTimeInMinutes>600</StartTimeInMinutes>
75 <EndTimeInMinutes>1140</EndTimeInMinutes>
84 <DayOrder>short</DayOrder>
86 <DayOfWeek>Sunday or Monday or Tuesday or Wednesday or Thursday or Friday or Saturday</DayOfWeek>
91 struct sipe_cal_std_dst
{
92 int bias
; /* Ex.: -60 */
93 gchar
*time
; /* hh:mm:ss, 02:00:00 */
94 int day_order
; /* 1..5 */
95 int month
; /* 1..12 */
96 gchar
*day_of_week
; /* Sunday or Monday or Tuesday or Wednesday or Thursday or Friday or Saturday */
97 gchar
*year
; /* YYYY */
102 struct sipe_cal_working_hours
{
103 int bias
; /* Ex.: 480 */
104 struct sipe_cal_std_dst std
; /* StandardTime */
105 struct sipe_cal_std_dst dst
; /* DaylightTime */
106 gchar
*days_of_week
; /* Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday separated by space */
107 int start_time
; /* 0...1440 */
108 int end_time
; /* 0...1440 */
110 gchar
*tz
; /* aggregated timezone string as in TZ environment variable.
111 Ex.: TST+8TDT+7,M3.2.0/02:00:00,M11.1.0/02:00:00 */
112 /** separate simple strings for Windows platform as the proper TZ does not work there.
113 * anyway, dynamic timezones would't work with just TZ
115 gchar
*tz_std
; /* Ex.: TST8 */
116 gchar
*tz_dst
; /* Ex.: TDT7 */
119 /* not for translation, a part of XML Schema definitions */
120 static const char *wday_names
[] = {"Sunday",
128 sipe_cal_get_wday(char *wday_name
)
132 if (!wday_name
) return -1;
134 for (i
= 0; i
< 7; i
++) {
135 if (sipe_strequal(wday_names
[i
], wday_name
)) {
144 sipe_cal_event_free(struct sipe_cal_event
* cal_event
)
146 if (!cal_event
) return;
148 g_free(cal_event
->subject
);
149 g_free(cal_event
->location
);
154 sipe_cal_events_free(GSList
*cal_events
)
156 GSList
*entry
= cal_events
;
158 if (!cal_events
) return;
161 struct sipe_cal_event
*cal_event
= entry
->data
;
162 sipe_cal_event_free(cal_event
);
166 g_slist_free(cal_events
);
170 sipe_cal_calendar_free(struct sipe_calendar
*cal
)
173 g_free(cal
->legacy_dn
);
175 g_free(cal
->auth
->domain
);
176 g_free(cal
->auth
->user
);
177 g_free(cal
->auth
->password
);
181 g_free(cal
->oof_url
);
182 g_free(cal
->oab_url
);
183 g_free(cal
->domino_url
);
184 g_free(cal
->oof_state
);
185 g_free(cal
->oof_note
);
186 g_free(cal
->free_busy
);
187 g_free(cal
->working_hours_xml_str
);
189 sipe_cal_events_free(cal
->cal_events
);
191 if (cal
->http_conn
) {
192 http_conn_free(cal
->http_conn
);
195 if (cal
->http_session
) {
196 http_conn_session_free(cal
->http_session
);
203 sipe_cal_calendar_init(struct sipe_core_private
*sipe_private
,
206 struct sipe_account_data
*sip
= SIPE_ACCOUNT_DATA_PRIVATE
;
210 sip
->cal
= g_new0(struct sipe_calendar
, 1);
211 sip
->cal
->sipe_private
= sipe_private
;
213 sip
->cal
->email
= g_strdup(sip
->email
);
215 /* user specified a service URL? */
216 value
= sipe_backend_setting(SIPE_CORE_PUBLIC
, SIPE_SETTING_EMAIL_URL
);
217 if (has_url
) *has_url
= !is_empty(value
);
218 if (!is_empty(value
)) {
219 sip
->cal
->as_url
= g_strdup(value
);
220 sip
->cal
->oof_url
= g_strdup(value
);
221 sip
->cal
->domino_url
= g_strdup(value
);
224 sip
->cal
->auth
= g_new0(HttpConnAuth
, 1);
225 sip
->cal
->auth
->use_negotiate
= SIPE_CORE_PUBLIC_FLAG_IS(KRB5
);
227 /* user specified email login? */
228 value
= sipe_backend_setting(SIPE_CORE_PUBLIC
, SIPE_SETTING_EMAIL_LOGIN
);
229 if (!is_empty(value
)) {
231 /* user specified email login domain? */
232 const char *tmp
= strstr(value
, "\\");
234 sip
->cal
->auth
->domain
= g_strndup(value
, tmp
- value
);
235 sip
->cal
->auth
->user
= g_strdup(tmp
+ 1);
237 sip
->cal
->auth
->user
= g_strdup(value
);
239 sip
->cal
->auth
->password
= g_strdup(sipe_backend_setting(SIPE_CORE_PUBLIC
,
240 SIPE_SETTING_EMAIL_PASSWORD
));
243 /* re-use SIPE credentials */
244 sip
->cal
->auth
->domain
= g_strdup(sip
->authdomain
);
245 sip
->cal
->auth
->user
= g_strdup(sip
->authuser
);
246 sip
->cal
->auth
->password
= g_strdup(sip
->password
);
255 sipe_cal_event_describe(struct sipe_cal_event
* cal_event
)
257 GString
* str
= g_string_new(NULL
);
258 const char *status
= "";
260 switch(cal_event
->cal_status
) {
261 case SIPE_CAL_FREE
: status
= "SIPE_CAL_FREE"; break;
262 case SIPE_CAL_TENTATIVE
: status
= "SIPE_CAL_TENTATIVE"; break;
263 case SIPE_CAL_BUSY
: status
= "SIPE_CAL_BUSY"; break;
264 case SIPE_CAL_OOF
: status
= "SIPE_CAL_OOF"; break;
265 case SIPE_CAL_NO_DATA
: status
= "SIPE_CAL_NO_DATA"; break;
268 g_string_append_printf(str
, "\t%s: %s", "start_time",
269 IS(cal_event
->start_time
) ? asctime(localtime(&cal_event
->start_time
)) : "\n");
270 g_string_append_printf(str
, "\t%s: %s", "end_time ",
271 IS(cal_event
->end_time
) ? asctime(localtime(&cal_event
->end_time
)) : "\n");
272 g_string_append_printf(str
, "\t%s: %s\n", "cal_status", status
);
273 g_string_append_printf(str
, "\t%s: %s\n", "subject ", cal_event
->subject
? cal_event
->subject
: "");
274 g_string_append_printf(str
, "\t%s: %s\n", "location ", cal_event
->location
? cal_event
->location
: "");
275 g_string_append_printf(str
, "\t%s: %s\n", "is_meeting", cal_event
->is_meeting
? "TRUE" : "FALSE");
277 return g_string_free(str
, FALSE
);
281 sipe_cal_event_hash(struct sipe_cal_event
* event
)
283 /* no end_time as it dos not get published */
284 /* no cal_status as it can change on publication */
285 return g_strdup_printf("<%d><%s><%s><%d>",
286 (int)event
->start_time
,
287 event
->subject
? event
->subject
: "",
288 event
->location
? event
->location
: "",
292 #define ENVIRONMENT_TIMEZONE "TZ"
295 sipe_switch_tz(const char *tz
)
299 tz_orig
= g_strdup(g_getenv(ENVIRONMENT_TIMEZONE
));
300 g_setenv(ENVIRONMENT_TIMEZONE
, tz
, TRUE
);
306 sipe_reset_tz(gchar
*tz_orig
)
309 g_setenv(ENVIRONMENT_TIMEZONE
, tz_orig
, TRUE
);
312 g_unsetenv(ENVIRONMENT_TIMEZONE
);
318 * Converts struct tm to Epoch time_t considering timezone.
320 * @param tz as defined for TZ environment variable.
322 * Reference: see timegm(3) - Linux man page
325 sipe_mktime_tz(struct tm
*tm
,
331 tz_orig
= sipe_switch_tz(tz
);
333 sipe_reset_tz(tz_orig
);
339 * Converts Epoch time_t to struct tm considering timezone.
341 * @param tz as defined for TZ environment variable.
343 * Reference: see timegm(3) - Linux man page
346 sipe_localtime_tz(const time_t *time
,
352 tz_orig
= sipe_switch_tz(tz
);
353 ret
= localtime(time
);
354 sipe_reset_tz(tz_orig
);
360 sipe_cal_free_working_hours(struct sipe_cal_working_hours
*wh
)
364 g_free(wh
->std
.time
);
365 g_free(wh
->std
.day_of_week
);
366 g_free(wh
->std
.year
);
368 g_free(wh
->dst
.time
);
369 g_free(wh
->dst
.day_of_week
);
370 g_free(wh
->dst
.year
);
372 g_free(wh
->days_of_week
);
380 * Returns time_t of daylight savings time start/end
381 * in the provided timezone or otherwise
382 * (time_t)-1 if no daylight savings time.
385 sipe_cal_get_std_dst_time(time_t now
,
387 struct sipe_cal_std_dst
* std_dst
,
388 struct sipe_cal_std_dst
* dst_std
)
391 time_t res
= TIME_NULL
;
392 struct tm
*gm_now_tm
;
395 if (std_dst
->month
== 0) return TIME_NULL
;
397 gm_now_tm
= gmtime(&now
);
398 time_arr
= g_strsplit(std_dst
->time
, ":", 0);
400 switch_tm
.tm_sec
= atoi(time_arr
[2]);
401 switch_tm
.tm_min
= atoi(time_arr
[1]);
402 switch_tm
.tm_hour
= atoi(time_arr
[0]);
403 g_strfreev(time_arr
);
404 switch_tm
.tm_mday
= std_dst
->year
? std_dst
->day_order
: 1 /* to adjust later */ ;
405 switch_tm
.tm_mon
= std_dst
->month
- 1;
406 switch_tm
.tm_year
= std_dst
->year
? atoi(std_dst
->year
) - 1900 : gm_now_tm
->tm_year
;
407 switch_tm
.tm_isdst
= 0;
409 res
= sipe_mktime_tz(&switch_tm
, "UTC");
411 /* if not dynamic, calculate right tm_mday */
412 if (!std_dst
->year
) {
413 int switch_wday
= sipe_cal_get_wday(std_dst
->day_of_week
);
415 /* get first desired wday in the month */
416 int delta
= switch_wday
>= switch_tm
.tm_wday
? (switch_wday
- switch_tm
.tm_wday
) : (switch_wday
+ 7 - switch_tm
.tm_wday
);
417 switch_tm
.tm_mday
= 1 + delta
;
419 switch_tm
.tm_mday
+= (std_dst
->day_order
- 1) * 7;
420 needed_month
= switch_tm
.tm_mon
;
421 /* to set settle date if ahead of allowed month dates */
422 res
= sipe_mktime_tz(&switch_tm
, "UTC");
423 if (needed_month
!= switch_tm
.tm_mon
) {
424 /* moving 1 week back to stay within required month */
425 switch_tm
.tm_mday
-= 7;
426 /* to fix date again */
427 res
= sipe_mktime_tz(&switch_tm
, "UTC");
430 /* note: bias is taken from "switch to" structure */
431 return res
+ (bias
+ dst_std
->bias
)*60;
435 sipe_cal_parse_std_dst(const sipe_xml
*xn_std_dst_time
,
436 struct sipe_cal_std_dst
*std_dst
)
438 const sipe_xml
*node
;
441 if (!xn_std_dst_time
) return;
442 if (!std_dst
) return;
446 <Time>02:00:00</Time>
447 <DayOrder>1</DayOrder>
449 <DayOfWeek>Sunday</DayOfWeek>
453 if ((node
= sipe_xml_child(xn_std_dst_time
, "Bias"))) {
454 std_dst
->bias
= atoi(tmp
= sipe_xml_data(node
));
458 if ((node
= sipe_xml_child(xn_std_dst_time
, "Time"))) {
459 std_dst
->time
= sipe_xml_data(node
);
462 if ((node
= sipe_xml_child(xn_std_dst_time
, "DayOrder"))) {
463 std_dst
->day_order
= atoi(tmp
= sipe_xml_data(node
));
467 if ((node
= sipe_xml_child(xn_std_dst_time
, "Month"))) {
468 std_dst
->month
= atoi(tmp
= sipe_xml_data(node
));
472 if ((node
= sipe_xml_child(xn_std_dst_time
, "DayOfWeek"))) {
473 std_dst
->day_of_week
= sipe_xml_data(node
);
476 if ((node
= sipe_xml_child(xn_std_dst_time
, "Year"))) {
477 std_dst
->year
= sipe_xml_data(node
);
482 sipe_cal_parse_working_hours(const sipe_xml
*xn_working_hours
,
483 struct sipe_buddy
*buddy
)
485 const sipe_xml
*xn_bias
;
486 const sipe_xml
*xn_timezone
;
487 const sipe_xml
*xn_working_period
;
488 const sipe_xml
*xn_standard_time
;
489 const sipe_xml
*xn_daylight_time
;
491 time_t now
= time(NULL
);
492 struct sipe_cal_std_dst
* std
;
493 struct sipe_cal_std_dst
* dst
;
495 if (!xn_working_hours
) return;
497 <WorkingHours xmlns="http://schemas.microsoft.com/exchange/services/2006/types">
504 <DayOfWeek>Monday Tuesday Wednesday Thursday Friday</DayOfWeek>
505 <StartTimeInMinutes>600</StartTimeInMinutes>
506 <EndTimeInMinutes>1140</EndTimeInMinutes>
508 </WorkingPeriodArray>
511 sipe_cal_free_working_hours(buddy
->cal_working_hours
);
512 buddy
->cal_working_hours
= g_new0(struct sipe_cal_working_hours
, 1);
514 xn_timezone
= sipe_xml_child(xn_working_hours
, "TimeZone");
515 xn_bias
= sipe_xml_child(xn_timezone
, "Bias");
517 buddy
->cal_working_hours
->bias
= atoi(tmp
= sipe_xml_data(xn_bias
));
521 xn_standard_time
= sipe_xml_child(xn_timezone
, "StandardTime");
522 xn_daylight_time
= sipe_xml_child(xn_timezone
, "DaylightTime");
524 std
= &((*buddy
->cal_working_hours
).std
);
525 dst
= &((*buddy
->cal_working_hours
).dst
);
526 sipe_cal_parse_std_dst(xn_standard_time
, std
);
527 sipe_cal_parse_std_dst(xn_daylight_time
, dst
);
529 xn_working_period
= sipe_xml_child(xn_working_hours
, "WorkingPeriodArray/WorkingPeriod");
530 if (xn_working_period
) {
531 buddy
->cal_working_hours
->days_of_week
=
532 sipe_xml_data(sipe_xml_child(xn_working_period
, "DayOfWeek"));
534 buddy
->cal_working_hours
->start_time
=
535 atoi(tmp
= sipe_xml_data(sipe_xml_child(xn_working_period
, "StartTimeInMinutes")));
538 buddy
->cal_working_hours
->end_time
=
539 atoi(tmp
= sipe_xml_data(sipe_xml_child(xn_working_period
, "EndTimeInMinutes")));
543 std
->switch_time
= sipe_cal_get_std_dst_time(now
, buddy
->cal_working_hours
->bias
, std
, dst
);
544 dst
->switch_time
= sipe_cal_get_std_dst_time(now
, buddy
->cal_working_hours
->bias
, dst
, std
);
546 /* TST8TDT7,M3.2.0/02:00:00,M11.1.0/02:00:00 */
547 buddy
->cal_working_hours
->tz
=
548 g_strdup_printf("TST%dTDT%d,M%d.%d.%d/%s,M%d.%d.%d/%s",
549 (buddy
->cal_working_hours
->bias
+ buddy
->cal_working_hours
->std
.bias
) / 60,
550 (buddy
->cal_working_hours
->bias
+ buddy
->cal_working_hours
->dst
.bias
) / 60,
552 buddy
->cal_working_hours
->dst
.month
,
553 buddy
->cal_working_hours
->dst
.day_order
,
554 sipe_cal_get_wday(buddy
->cal_working_hours
->dst
.day_of_week
),
555 buddy
->cal_working_hours
->dst
.time
,
557 buddy
->cal_working_hours
->std
.month
,
558 buddy
->cal_working_hours
->std
.day_order
,
559 sipe_cal_get_wday(buddy
->cal_working_hours
->std
.day_of_week
),
560 buddy
->cal_working_hours
->std
.time
563 buddy
->cal_working_hours
->tz_std
=
564 g_strdup_printf("TST%d",
565 (buddy
->cal_working_hours
->bias
+ buddy
->cal_working_hours
->std
.bias
) / 60);
567 buddy
->cal_working_hours
->tz_dst
=
568 g_strdup_printf("TDT%d",
569 (buddy
->cal_working_hours
->bias
+ buddy
->cal_working_hours
->dst
.bias
) / 60);
572 struct sipe_cal_event
*
573 sipe_cal_get_event(GSList
*cal_events
,
574 time_t time_in_question
)
576 GSList
*entry
= cal_events
;
577 struct sipe_cal_event
* cal_event
;
578 struct sipe_cal_event
* res
= NULL
;
580 if (!cal_events
|| !IS(time_in_question
)) return NULL
;
583 cal_event
= entry
->data
;
584 /* event is in the past or in the future */
585 if (cal_event
->start_time
> time_in_question
||
586 cal_event
->end_time
<= time_in_question
)
595 int res_status
= (res
->cal_status
== SIPE_CAL_NO_DATA
) ? -1 : res
->cal_status
;
596 int cal_status
= (cal_event
->cal_status
== SIPE_CAL_NO_DATA
) ? -1 : cal_event
->cal_status
;
597 if (res_status
< cal_status
) {
607 sipe_cal_get_status0(const gchar
*free_busy
,
610 time_t time_in_question
,
613 int res
= SIPE_CAL_NO_DATA
;
615 time_t cal_end
= cal_start
+ strlen(free_busy
)*granularity
*60 - 1;
617 if (!(time_in_question
>= cal_start
&& time_in_question
<= cal_end
)) return res
;
619 shift
= (time_in_question
- cal_start
) / (granularity
*60);
624 res
= free_busy
[shift
] - '0';
630 * Returns time when current calendar state started
633 sipe_cal_get_since_time(const gchar
*free_busy
,
641 if ((index
< 0) || ((size_t)(index
+ 1) > strlen(free_busy
))) return 0;
643 for (i
= index
; i
>= 0; i
--) {
644 int temp_status
= free_busy
[i
] - '0';
646 if (current_state
!= temp_status
) {
647 return calStart
+ (i
+ 1)*granularity
*60;
650 if (i
== 0) return calStart
;
656 sipe_cal_get_free_busy(struct sipe_buddy
*buddy
);
659 sipe_cal_get_status(struct sipe_buddy
*buddy
,
660 time_t time_in_question
,
664 const char* free_busy
;
665 int ret
= SIPE_CAL_NO_DATA
;
669 if (!buddy
|| !buddy
->cal_start_time
|| !buddy
->cal_granularity
) {
670 SIPE_DEBUG_INFO("sipe_cal_get_status: no calendar data1 for %s, exiting",
671 buddy
? (buddy
->name
? buddy
->name
: "") : "");
672 return SIPE_CAL_NO_DATA
;
675 if (!(free_busy
= sipe_cal_get_free_busy(buddy
))) {
676 SIPE_DEBUG_INFO("sipe_cal_get_status: no calendar data2 for %s, exiting", buddy
->name
);
677 return SIPE_CAL_NO_DATA
;
679 SIPE_DEBUG_INFO("sipe_cal_get_description: buddy->cal_free_busy=\n%s", free_busy
);
681 cal_start
= sipe_utils_str_to_time(buddy
->cal_start_time
);
683 ret
= sipe_cal_get_status0(free_busy
,
685 buddy
->cal_granularity
,
688 state_since
= sipe_cal_get_since_time(free_busy
,
690 buddy
->cal_granularity
,
694 if (since
) *since
= state_since
;
699 sipe_cal_get_switch_time(const gchar
*free_busy
,
707 time_t ret
= TIME_NULL
;
709 if ((index
< 0) || ((size_t) (index
+ 1) > strlen(free_busy
))) {
710 *to_state
= SIPE_CAL_NO_DATA
;
714 for (i
= index
+ 1; i
< strlen(free_busy
); i
++) {
715 int temp_status
= free_busy
[i
] - '0';
717 if (current_state
!= temp_status
) {
718 *to_state
= temp_status
;
719 return calStart
+ i
*granularity
*60;
727 sipe_cal_get_tz(struct sipe_cal_working_hours
*wh
,
728 time_t time_in_question
)
730 time_t dst_switch_time
= (*wh
).dst
.switch_time
;
731 time_t std_switch_time
= (*wh
).std
.switch_time
;
732 gboolean is_dst
= FALSE
;
734 /* No daylight savings */
735 if (dst_switch_time
== TIME_NULL
) {
739 if (dst_switch_time
< std_switch_time
) { /* North hemosphere - Europe, US */
740 if (time_in_question
>= dst_switch_time
&& time_in_question
< std_switch_time
) {
743 } else { /* South hemisphere - Australia */
744 if (time_in_question
>= dst_switch_time
|| time_in_question
< std_switch_time
) {
757 sipe_cal_mktime_of_day(struct tm
*sample_today_tm
,
758 const int shift_minutes
,
761 sample_today_tm
->tm_sec
= 0;
762 sample_today_tm
->tm_min
= shift_minutes
% 60;
763 sample_today_tm
->tm_hour
= shift_minutes
/ 60;
765 return sipe_mktime_tz(sample_today_tm
, tz
);
769 * Returns work day start and end in Epoch time
770 * considering the initial values are provided
771 * in contact's local time zone.
774 sipe_cal_get_today_work_hours(struct sipe_cal_working_hours
*wh
,
779 time_t now
= time(NULL
);
780 const char *tz
= sipe_cal_get_tz(wh
, now
);
781 struct tm
*remote_now_tm
= sipe_localtime_tz(&now
, tz
);
783 if (!strstr(wh
->days_of_week
, wday_names
[remote_now_tm
->tm_wday
])) { /* not a work day */
786 *next_start
= TIME_NULL
;
790 *end
= sipe_cal_mktime_of_day(remote_now_tm
, wh
->end_time
, tz
);
793 *start
= sipe_cal_mktime_of_day(remote_now_tm
, wh
->start_time
, tz
);
794 *next_start
= TIME_NULL
;
795 } else { /* calculate start of tomorrow's work day if any */
796 time_t tom
= now
+ 24*60*60;
797 struct tm
*remote_tom_tm
= sipe_localtime_tz(&tom
, sipe_cal_get_tz(wh
, tom
));
799 if (!strstr(wh
->days_of_week
, wday_names
[remote_tom_tm
->tm_wday
])) { /* not a work day */
800 *next_start
= TIME_NULL
;
803 *next_start
= sipe_cal_mktime_of_day(remote_tom_tm
, wh
->start_time
, sipe_cal_get_tz(wh
, tom
));
809 sipe_cal_is_in_work_hours(const time_t time_in_question
,
813 return !((time_in_question
>= end
) || (IS(start
) && time_in_question
< start
));
817 * Returns time closest to now. Choses only from times ahead of now.
818 * Returns TIME_NULL otherwise.
821 sipe_cal_get_until(const time_t now
,
822 const time_t switch_time
,
825 const time_t next_start
)
827 time_t ret
= TIME_NULL
;
828 int min_diff
= now
- ret
;
830 if (IS(switch_time
) && switch_time
> now
&& (switch_time
- now
) < min_diff
) {
831 min_diff
= switch_time
- now
;
834 if (IS(start
) && start
> now
&& (start
- now
) < min_diff
) {
835 min_diff
= start
- now
;
838 if (IS(end
) && end
> now
&& (end
- now
) < min_diff
) {
839 min_diff
= end
- now
;
842 if (IS(next_start
) && next_start
> now
&& (next_start
- now
) < min_diff
) {
843 min_diff
= next_start
- now
;
850 sipe_cal_get_free_busy(struct sipe_buddy
*buddy
)
852 /* do lazy decode if necessary */
853 if (!buddy
->cal_free_busy
&& buddy
->cal_free_busy_base64
) {
859 cal_dec64
= g_base64_decode(buddy
->cal_free_busy_base64
, &cal_dec64_len
);
861 buddy
->cal_free_busy
= g_malloc0(cal_dec64_len
* 4 + 1);
863 http://msdn.microsoft.com/en-us/library/dd941537%28office.13%29.aspx
867 11, Out of facility (Oo)
869 http://msdn.microsoft.com/en-us/library/aa566048.aspx
873 3 Out of Office (OOF)
876 for (i
= 0; i
< cal_dec64_len
; i
++) {
877 #define TWO_BIT_MASK 0x03
878 char tmp
= cal_dec64
[i
];
879 buddy
->cal_free_busy
[j
++] = (tmp
& TWO_BIT_MASK
) + '0';
880 buddy
->cal_free_busy
[j
++] = ((tmp
>> 2) & TWO_BIT_MASK
) + '0';
881 buddy
->cal_free_busy
[j
++] = ((tmp
>> 4) & TWO_BIT_MASK
) + '0';
882 buddy
->cal_free_busy
[j
++] = ((tmp
>> 6) & TWO_BIT_MASK
) + '0';
884 buddy
->cal_free_busy
[j
++] = '\0';
888 return buddy
->cal_free_busy
;
892 sipe_cal_get_freebusy_base64(const char* freebusy_hex
)
896 guint shift_factor
= 0;
901 if (!freebusy_hex
) return NULL
;
903 len
= strlen(freebusy_hex
);
904 res_len
= len
/ 4 + 1;
905 res
= g_malloc0(res_len
);
907 res
[j
] |= (freebusy_hex
[i
++] - '0') << shift_factor
;
909 if (shift_factor
== 8) {
915 res_base64
= g_base64_encode(res
, shift_factor
? res_len
: res_len
- 1);
921 sipe_cal_get_description(struct sipe_buddy
*buddy
)
925 int current_cal_state
;
926 time_t now
= time(NULL
);
927 time_t start
= TIME_NULL
;
928 time_t end
= TIME_NULL
;
929 time_t next_start
= TIME_NULL
;
931 int to_state
= SIPE_CAL_NO_DATA
;
932 time_t until
= TIME_NULL
;
934 gboolean has_working_hours
= (buddy
->cal_working_hours
!= NULL
);
935 const char *free_busy
;
936 const char *cal_states
[] = {_("Free"),
942 if (buddy
->cal_granularity
!= 15) {
943 SIPE_DEBUG_INFO("sipe_cal_get_description: granularity %d is unsupported, exiting.", buddy
->cal_granularity
);
947 /* to lazy load if needed */
948 free_busy
= sipe_cal_get_free_busy(buddy
);
949 SIPE_DEBUG_INFO("sipe_cal_get_description: buddy->cal_free_busy=\n%s", free_busy
? free_busy
: "");
951 if (!buddy
->cal_free_busy
|| !buddy
->cal_granularity
|| !buddy
->cal_start_time
) {
952 SIPE_DEBUG_INFO_NOFORMAT("sipe_cal_get_description: no calendar data, exiting");
956 cal_start
= sipe_utils_str_to_time(buddy
->cal_start_time
);
957 cal_end
= cal_start
+ 60 * (buddy
->cal_granularity
) * strlen(buddy
->cal_free_busy
);
959 current_cal_state
= sipe_cal_get_status0(free_busy
, cal_start
, buddy
->cal_granularity
, time(NULL
), &index
);
960 if (current_cal_state
== SIPE_CAL_NO_DATA
) {
961 SIPE_DEBUG_INFO_NOFORMAT("sipe_cal_get_description: calendar is undefined for present moment, exiting.");
965 switch_time
= sipe_cal_get_switch_time(free_busy
, cal_start
, buddy
->cal_granularity
, index
, current_cal_state
, &to_state
);
967 SIPE_DEBUG_INFO_NOFORMAT("\n* Calendar *");
968 if (buddy
->cal_working_hours
) {
969 sipe_cal_get_today_work_hours(buddy
->cal_working_hours
, &start
, &end
, &next_start
);
971 SIPE_DEBUG_INFO("Remote now timezone : %s", sipe_cal_get_tz(buddy
->cal_working_hours
, now
));
972 SIPE_DEBUG_INFO("std.switch_time(GMT): %s",
973 IS((*buddy
->cal_working_hours
).std
.switch_time
) ? asctime(gmtime(&((*buddy
->cal_working_hours
).std
.switch_time
))) : "");
974 SIPE_DEBUG_INFO("dst.switch_time(GMT): %s",
975 IS((*buddy
->cal_working_hours
).dst
.switch_time
) ? asctime(gmtime(&((*buddy
->cal_working_hours
).dst
.switch_time
))) : "");
976 SIPE_DEBUG_INFO("Remote now time : %s",
977 asctime(sipe_localtime_tz(&now
, sipe_cal_get_tz(buddy
->cal_working_hours
, now
))));
978 SIPE_DEBUG_INFO("Remote start time : %s",
979 IS(start
) ? asctime(sipe_localtime_tz(&start
, sipe_cal_get_tz(buddy
->cal_working_hours
, start
))) : "");
980 SIPE_DEBUG_INFO("Remote end time : %s",
981 IS(end
) ? asctime(sipe_localtime_tz(&end
, sipe_cal_get_tz(buddy
->cal_working_hours
, end
))) : "");
982 SIPE_DEBUG_INFO("Rem. next_start time: %s",
983 IS(next_start
) ? asctime(sipe_localtime_tz(&next_start
, sipe_cal_get_tz(buddy
->cal_working_hours
, next_start
))) : "");
984 SIPE_DEBUG_INFO("Remote switch time : %s",
985 IS(switch_time
) ? asctime(sipe_localtime_tz(&switch_time
, sipe_cal_get_tz(buddy
->cal_working_hours
, switch_time
))) : "");
987 SIPE_DEBUG_INFO("Local now time : %s",
988 asctime(localtime(&now
)));
989 SIPE_DEBUG_INFO("Local switch time : %s",
990 IS(switch_time
) ? asctime(localtime(&switch_time
)) : "");
992 SIPE_DEBUG_INFO("Calendar End (GMT) : %s", asctime(gmtime(&cal_end
)));
993 SIPE_DEBUG_INFO("current cal state : %s", cal_states
[current_cal_state
]);
994 SIPE_DEBUG_INFO("switch cal state : %s", cal_states
[to_state
] );
996 /* Calendar: string calculations */
999 ALGORITHM (don't delete)
1000 (c)2009,2010 pier11 <pier11@operamail.com>
1002 SOD = Start of Work Day
1003 EOD = End of Work Day
1004 NSOD = Start of tomorrow's Work Day
1005 SW = Calendar status switch time
1007 if current_cal_state == Free
1008 until = min_t of SOD, EOD, NSOD, SW (min_t(x) = min(x-now) where x>now only)
1012 if (!until && (cal_period_end > now + 8H))
1013 until = cal_period_end
1016 return "Currently %", current_cal_state
1018 if (until - now > 8H)
1019 if (current_cal_state == Free && (work_hours && !in work_hours(now)))
1020 return "Outside of working hours for next 8 hours"
1022 return "%s for next 8 hours", current_cal_state
1024 if (current_cal_state == Free)
1025 if (work_hours && until !in work_hours(now))
1028 "%s", current_cal_state
1029 " until %.2d:%.2d", until
1031 "Currently %", current_cal_state
1032 if (work_hours && until !in work_hours(until))
1033 ". Outside of working hours at at %.2d:%.2d", until
1035 ". %s at %.2d:%.2d", to_state, until
1038 if (current_cal_state
< 1) { /* Free */
1039 until
= sipe_cal_get_until(now
, switch_time
, start
, end
, next_start
);
1041 until
= switch_time
;
1044 if (!IS(until
) && (cal_end
- now
> 8*60*60))
1048 return g_strdup_printf(_("Currently %s"), cal_states
[current_cal_state
]);
1051 if (until
- now
> 8*60*60) {
1052 /* Free & outside work hours */
1053 if (current_cal_state
< 1 && has_working_hours
&& !sipe_cal_is_in_work_hours(now
, start
, end
)) {
1054 return g_strdup(_("Outside of working hours for next 8 hours"));
1056 return g_strdup_printf(_("%s for next 8 hours"), cal_states
[current_cal_state
]);
1060 if (current_cal_state
< 1) { /* Free */
1062 struct tm
*until_tm
= localtime(&until
);
1064 if (has_working_hours
&& !sipe_cal_is_in_work_hours(now
, start
, end
)) {
1065 tmp
= _("Not working");
1067 tmp
= cal_states
[current_cal_state
];
1069 return g_strdup_printf(_("%s until %.2d:%.2d"), tmp
, until_tm
->tm_hour
, until_tm
->tm_min
);
1070 } else { /* Tentative or Busy or OOF */
1073 struct tm
*until_tm
= localtime(&until
);
1075 tmp
= g_strdup_printf(_("Currently %s"), cal_states
[current_cal_state
]);
1076 if (has_working_hours
&& !sipe_cal_is_in_work_hours(until
, start
, end
)) {
1077 res
= g_strdup_printf(_("%s. Outside of working hours at %.2d:%.2d"),
1078 tmp
, until_tm
->tm_hour
, until_tm
->tm_min
);
1082 res
= g_strdup_printf(_("%s. %s at %.2d:%.2d"), tmp
, cal_states
[to_state
], until_tm
->tm_hour
, until_tm
->tm_min
);
1087 /* End of - Calendar: string calculations */