3 * Copyright 2012 Samsung Electronics Co., Ltd
5 * Licensed under the Flora License, Version 1.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.tizenopensource.org/license
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
26 #define LOGFILE "/tmp/calendar.log"
27 #define SIN_TBL_S (sizeof(SIN_TBL)/sizeof(SIN_TBL[0]) - 1)
28 #define WORLDCLOCK_DB "/opt/dbspace/.worldclock.db"
30 static time_t cal_max;
31 static time_t cal_min;
33 static const float const SIN_TBL[] = {
34 0.0000f, 0.0174f, 0.0349f, 0.0523f, 0.0698f,
35 0.0872f, 0.1045f, 0.1219f, 0.1392f, 0.1564f,
36 0.1736f, 0.1908f, 0.2079f, 0.2249f, 0.2419f,
37 0.2588f, 0.2756f, 0.2924f, 0.3090f, 0.3256f,
38 0.3420f, 0.3584f, 0.3746f, 0.3907f, 0.4067f,
39 0.4226f, 0.4384f, 0.4540f, 0.4695f, 0.4848f,
40 0.5000f, 0.5150f, 0.5299f, 0.5446f, 0.5592f,
41 0.5736f, 0.5878f, 0.6018f, 0.6157f, 0.6293f,
42 0.6528f, 0.6561f, 0.6691f, 0.6820f, 0.6947f,
43 0.7071f, 0.7193f, 0.7314f, 0.7431f, 0.7547f,
44 0.7660f, 0.7772f, 0.7880f, 0.7986f, 0.8090f,
45 0.8191f, 0.8290f, 0.8387f, 0.8480f, 0.8571f,
46 0.8660f, 0.8746f, 0.8829f, 0.8910f, 0.8988f,
47 0.9063f, 0.9135f, 0.9205f, 0.9272f, 0.9336f,
48 0.9397f, 0.9455f, 0.9511f, 0.9563f, 0.9613f,
49 0.9659f, 0.9703f, 0.9744f, 0.9781f, 0.9816f,
50 0.9848f, 0.9877f, 0.9903f, 0.9926f, 0.9945f,
51 0.9962f, 0.9976f, 0.9986f, 0.9994f, 0.9998f,
56 static int __cal_util_max_days[2][12] = {
57 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
58 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
60 int cal_util_get_max_days(int tm_year, int tm_mon)
72 tm_year = tm_year + 1900;
74 return __cal_util_max_days[(!(tm_year & 0x3) && (!(tm_year % 400)
75 || (tm_year % 100)))][tm_mon];
78 static int __cal_util_get_cal_min(void)
97 cal_min = mktime(&tm);
100 c_retv_if(!cal_min, -1);
105 static int __cal_util_get_cal_max(void)
114 localtime_r(&t, &tm);
116 // 2037-12-31 23:59:59
124 cal_max = mktime(&tm);
132 time_t cal_util_get_max_time(void)
134 return __cal_util_get_cal_max();
137 time_t cal_util_get_min_time(void)
139 return __cal_util_get_cal_min();
142 int cal_util_update_tm_year(struct tm *t, int delta)
149 tmp.tm_year += delta;
150 if (tmp.tm_mon == 1 && tmp.tm_mday == 29)
151 tmp.tm_mday = cal_util_get_max_days(tmp.tm_year, tmp.tm_mon);
157 if (r < __cal_util_get_cal_min() || r > __cal_util_get_cal_max())
164 int cal_util_update_tm_month(struct tm *t, int delta)
173 while (tmp.tm_mon < 0) {
178 while (tmp.tm_mon > 11) {
183 max = cal_util_get_max_days(tmp.tm_year, tmp.tm_mon);
184 if (max < tmp.tm_mday)
191 if (r < __cal_util_get_cal_min() || r > __cal_util_get_cal_max())
198 int cal_util_update_tm_day(struct tm *t, int delta)
204 tmp.tm_mday += delta;
210 if (r < __cal_util_get_cal_min() || r > __cal_util_get_cal_max())
217 int cal_util_update_tm_hour(struct tm* tm, int delta)
220 struct tm* returned_tm = NULL;
224 time = time + (delta * 60*60);
226 returned_tm = localtime_r(&time, &t);
229 ERR("localtime is failed.");
232 CAL_MEMCPY(tm, &t, struct tm);
237 int cal_util_get_day_time_t(struct tm *t, time_t *st, time_t *et)
239 c_retvm_if(!t, -1 , "t is null");
240 c_retvm_if(!st, -1 , "st is null");
241 c_retvm_if(!et, -1 , "et is null");
246 tm.tm_hour = tm.tm_min = tm.tm_sec = 0;
249 if (_t == (time_t) -1)
253 *et = _t + (24 * 60 * 60) - 1;
258 int cal_util_get_week_time_t(struct tm *t, time_t *st, time_t *et, int start)
260 c_retvm_if(!t, -1 , "t is null");
261 c_retvm_if(!st, -1 , "st is null");
262 c_retvm_if(!et, -1 , "et is null");
267 tm.tm_mday -= CAL_UTIL_GET_WDAY(tm.tm_wday - start);
269 tm.tm_hour = tm.tm_min = tm.tm_sec = 0;
272 if (_t == (time_t) -1)
276 *et = _t + (7 * 24 * 60 * 60) - 1;
281 int cal_util_get_month_time_t(struct tm *t, time_t *st, time_t *et)
283 c_retvm_if(!t, -1 , "t is null");
284 c_retvm_if(!st, -1 , "st is null");
285 c_retvm_if(!et, -1 , "et is null");
291 tm.tm_hour = tm.tm_min = tm.tm_sec = 0;
294 if (s == (time_t) -1)
299 if (e == (time_t) -1)
307 int cal_util_get_year_time_t(struct tm *t, time_t *st, time_t *et)
309 c_retvm_if(!t, -1 , "t is null");
310 c_retvm_if(!st, -1 , "st is null");
311 c_retvm_if(!et, -1 , "et is null");
318 tm.tm_hour = tm.tm_min = tm.tm_sec = 0;
321 if (s == (time_t) -1)
326 if (e == (time_t) -1)
335 float cal_util_nsin(float f)
337 if(f < 0.0 && f > 1.0)
340 return SIN_TBL[(int)(SIN_TBL_S * f)];
343 int cal_util_get_week_flag(char *week_s)
349 if (6 < CAL_STRLEN(week_s)) {
350 for (i = 0; i < 7; i++) {
351 if (week_s[i] == '1')
359 int cal_util_create_vcs_file_from_cs(cal_struct *cs, const char *filename)
361 c_retvm_if(!cs || !filename, CAL_ERR_FAIL, "input param is wrong");
366 GList *schedules = NULL;
368 schedules = g_list_append(schedules, cs);
369 c_retvm_if(!schedules, CAL_ERR_FAIL, "g_list_append return null");
371 r = calendar_svc_write_schedules(schedules, &stream);
373 ERR("Failed to write schedules(errno:%d)", r);
376 g_list_free(schedules);
379 if (NULL == stream) {
380 ERR("stream is NULL");
384 fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC, 0660);
386 ERR("Failed to open path(%s)\n", filename);
388 return CAL_ERR_IO_ERR;
391 r = write(fd, stream, strlen(stream));
396 ERR("Failed to write stream(errno:%d)\n", r);
397 return CAL_ERR_IO_ERR;
403 static void __cal_util_get_timezone_id(char **timezone_id)
405 c_ret_if(!timezone_id);
409 int ret = vconf_get_int(CAL_VCONFKEY_LOCK_TIMEZONE_ON_OFF, &value);
410 c_warn_if(ret, "vconf_get_int(CAL_VCONFKEY_LOCK_TIMEZONE_ON_OFF, &value) is failed");
415 text = vconf_get_str(CAL_VCONFKEY_LOCK_TIMEZONE_PATH);
416 c_retm_if(!text, "vconf_get_str(CAL_VCONFKEY_LOCK_TIMEZONE_PATH) is failed");
418 text = vconf_get_str(VCONFKEY_SETAPPL_TIMEZONE_ID);
419 c_retm_if(!text, "vconf_get_str(VCONFKEY_SETAPPL_TIMEZONE_ID) is failed");
424 c_warn_if(!CAL_STRLEN(text), "timezone_id is empty str");
427 void cal_util_get_timezone_id(int timezone_offset, char **timezone_id)
431 c_ret_if(!timezone_id);
433 sqlite3 *db_handler = NULL;
434 sqlite3_stmt *stmt = NULL;
435 char *error_message = NULL;
439 ret = sqlite3_open( WORLDCLOCK_DB, &db_handler);
440 c_retm_if(ret, "sqlite3_open() is failed(%d) : %s", ret, sqlite3_errmsg(db_handler));
444 int minutes_for_1_hour = 60;
445 int timezone_offset_hour = timezone_offset/minutes_for_1_hour;
446 int timezone_offset_minute = timezone_offset%minutes_for_1_hour;
448 if (0 <= timezone_offset_hour)
449 snprintf(offset_hour, 8, "+%d", timezone_offset_hour);
451 snprintf(offset_hour, 8, "%d", timezone_offset_hour);
454 if (timezone_offset_minute)
455 snprintf(offset, 16, "GMT%s:%d", offset_hour, timezone_offset_minute);
457 snprintf(offset, 16, "GMT%s", offset_hour);
460 snprintf(query, 256, "SELECT tz_path FROM city_table where timezone=\"%s\" limit 1", offset);
462 ret = sqlite3_prepare_v2(db_handler, query, strlen(query), &stmt, NULL);
463 c_retm_if(ret, "sqlite3_prepare_v2(%s) failed(%d) : %s.", query, ret, sqlite3_errmsg(db_handler));
465 ret = sqlite3_step(stmt);
466 c_retm_if(ret != SQLITE_ROW
468 && ret != SQLITE_DONE, "sqlite3_step is failed(%d) : %s", ret, sqlite3_errmsg(db_handler));
470 *timezone_id = CAL_STRDUP((char *)sqlite3_column_text(stmt, 0));
472 ret = sqlite3_finalize(stmt);
473 c_retm_if(ret, "sqlite3_finalize is failed(%d) : %s", ret, sqlite3_errmsg(db_handler));
475 sqlite3_free(error_message);
477 ret = sqlite3_close(db_handler);
478 c_retm_if(ret, "sqlite3_close is failed(%d) : %s", ret, sqlite3_errmsg(db_handler));
481 void cal_util_get_timezone(char **timezone_id, char **timezone_city, char **timezone_offset)
485 c_ret_if(!timezone_id);
486 c_ret_if(!timezone_city);
487 c_ret_if(!timezone_offset);
489 sqlite3 *db_handler = NULL;
490 sqlite3_stmt *stmt = NULL;
491 char *error_message = NULL;
495 ret = sqlite3_open( WORLDCLOCK_DB, &db_handler);
496 c_retm_if(ret, "sqlite3_open() is failed(%d) : %s", ret, sqlite3_errmsg(db_handler));
500 __cal_util_get_timezone_id(timezone_id);
503 snprintf(query, 256, "SELECT city, timezone FROM city_table where tz_path=\"%s\"", *timezone_id);
505 ret = sqlite3_prepare_v2(db_handler, query, strlen(query), &stmt, NULL);
506 c_retm_if(ret, "sqlite3_prepare_v2(%s) failed(%d) : %s.", query, ret, sqlite3_errmsg(db_handler));
508 ret = sqlite3_step(stmt);
509 c_retm_if(ret != SQLITE_ROW
511 && ret != SQLITE_DONE, "sqlite3_step is failed(%d) : %s", ret, sqlite3_errmsg(db_handler));
513 *timezone_city = CAL_STRDUP((char *)sqlite3_column_text(stmt, 0));
514 *timezone_offset = CAL_STRDUP((char *)sqlite3_column_text(stmt, 1));
516 ret = sqlite3_finalize(stmt);
517 c_retm_if(ret, "sqlite3_finalize is failed(%d) : %s", ret, sqlite3_errmsg(db_handler));
519 sqlite3_free(error_message);
521 ret = sqlite3_close(db_handler);
522 c_retm_if(ret, "sqlite3_close is failed(%d) : %s", ret, sqlite3_errmsg(db_handler));
525 char *cal_util_get_search_color_text( const char *search_str, const char *input_str)
527 c_retv_if(!CAL_STRLEN(search_str), NULL);
528 c_retv_if(!CAL_STRLEN(input_str), NULL);
530 char *color_str = NULL;
531 const char *color_start_str = "<color=#2A89C2FF>";
532 const char *color_end_str = "</color>";
533 char* sub_str = NULL;
535 int color_start_str_sz = CAL_STRLEN(color_start_str);
536 int color_end_str_sz = CAL_STRLEN(color_end_str);
537 int input_str_sz = CAL_STRLEN(input_str);
538 int search_str_sz = CAL_STRLEN(search_str);
540 sub_str= strcasestr(input_str, search_str);
541 int sub_str_sz = CAL_STRLEN(sub_str);
543 c_retv_if(!sub_str_sz, NULL);
545 color_str = calloc(1, color_start_str_sz+color_end_str_sz+input_str_sz+1);
546 c_retv_if(!color_str, NULL);
548 int head_str_sz = sub_str - input_str;
550 CAL_STRNCPY(color_str, input_str, head_str_sz);
551 CAL_STRNCPY(color_str + head_str_sz, color_start_str, color_start_str_sz);
552 CAL_STRNCPY(color_str + head_str_sz + color_start_str_sz, sub_str, search_str_sz);
553 CAL_STRNCPY(color_str + head_str_sz + color_start_str_sz + search_str_sz, color_end_str, color_end_str_sz);
554 CAL_STRNCPY(color_str + head_str_sz + color_start_str_sz + search_str_sz + color_end_str_sz, sub_str + search_str_sz, sub_str_sz - search_str_sz);