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.
19 #include "alarm_fwk_util.h"
20 #include "alarm_fwk_vconf.h"
22 /**********************************************************************
23 ******************define, struct ,typedef, union, enum, global val *************************************
24 ***********************************************************************/
26 static alarm_pair_t g_repeat[DAY_NUM] = {
27 {ALARM_WDAY_SUNDAY, STRING_ALARM_IDS_COM_POP_SUN},
28 {ALARM_WDAY_MONDAY, STRING_ALARM_IDS_COM_POP_MON},
29 {ALARM_WDAY_TUESDAY, STRING_ALARM_IDS_COM_POP_TUE},
30 {ALARM_WDAY_WEDNESDAY, STRING_ALARM_IDS_COM_POP_WED},
31 {ALARM_WDAY_THURSDAY, STRING_ALARM_IDS_COM_POP_THU},
32 {ALARM_WDAY_FRIDAY, STRING_ALARM_IDS_COM_POP_FRI},
33 {ALARM_WDAY_SATURDAY, STRING_ALARM_IDS_COM_POP_SAT},
36 /**********************************************************************
37 ******************Global function ref*************************************
38 ***********************************************************************/
39 static int snooze_min_get()
41 int value = DEFAULT_ALARM_DB_SNOOZE_DURATION;
42 #ifdef ALARMAPP_FEATURE_SUPPORT_CSC
44 vconf_get_str(CSC_VCONF_KEY_ALARM_APP_DEFAULT_SNOOZE_MINUTES_STR);
46 CLK_ERR("string null");
49 if (IS_STR_EQUAL(string, STRING_ALARM_OFF)) {
51 } else if (IS_STR_EQUAL(string, STRING_ALARM_SNOOZE_TIME_1M)) {
53 } else if (IS_STR_EQUAL(string, STRING_ALARM_SNOOZE_TIME_3M)) {
55 } else if (IS_STR_EQUAL(string, STRING_ALARM_SNOOZE_TIME_5M)) {
57 } else if (IS_STR_EQUAL(string, STRING_ALARM_SNOOZE_TIME_10M)) {
68 * This function is used to set default alarm
69 * @param ad[in] pointer to struct appdata
73 void set_alarm_default(struct appdata *ad)
78 id = alarm_genlist_name_array_get_used_id();
81 memset(alarm, 0x0, sizeof(AData));
82 MAGIC_VALUE_SET(alarm->_magic);
84 alarm->enable = ALARM_DB_ENABLE_ON;
85 alarm->author = ALARM_DB_AUTHOR_ALARM;
87 snprintf(alarm->name, sizeof(alarm->name), "%s %d",
88 STRING_ALARM_IDS_COM_BODY_ALARM_S_, id);
90 time_t stime = time(NULL);
92 localtime_r(&stime, &tm);
96 clk_timezone_get(alarm->timezone, ALARM_BUF_LEN);
98 alarm->repeat_once = EINA_TRUE;
99 alarm->repeat_every = EINA_FALSE;
100 alarm->repeat_weekly = 0;
102 alarm->snooze_enable = EINA_TRUE;
103 alarm->snooze_min = snooze_min_get();
104 alarm->snooze_times = DEFAULT_ALARM_DB_SNOOZE_REPEAT;
106 #ifdef ALARMAPP_FEATURE_SUPPORT_CSC
108 vconf_get_str(CSC_VCONF_KEY_ALARM_APP_DEFAULT_ALARM_TONE_PATH_STR);
110 CLK_ERR("vconf_get str error");
111 snprintf(alarm->tone, sizeof(alarm->tone), "%s",
112 DEFAULT_ALARM_TONE_PATH);
114 snprintf(alarm->tone, sizeof(alarm->tone), "%s", pDefaultTone);
118 snprintf(alarm->tone, sizeof(alarm->tone), "%s",
119 DEFAULT_ALARM_TONE_PATH);
121 alarm->volume = DEFAULT_ALARM_DB_VOLUME;
122 alarm->auto_power_on = ALARM_DB_AUTOPOWER_OFF;
128 * This function is used to validate name
129 * @param ad[in] pointer to struct appdata
130 * @return when success, return SUCCESS or FALIED if error
133 int alm_name_validate(const char *buf)
135 retvm_if(IS_EQUAL(0, strlen(buf)), ALM_POPUP_ID_NAME_NULL, "name null");
139 //when time is past, and repeat type is "never",
140 //the alarm is invalid, which is executed never
141 //alarmdate must curtime.min>=(systime.min+1)
142 int alm_time_validate(AData * alarm_data)
144 retvm_if(!alarm_data, ALM_POPUP_ID_MAX, "alarm_data null");
145 retvm_if(!alarm_data->enable, SUCCESS,
146 "inable alarm, don't need to handle");
147 retvm_if(alarm_data->repeat_weekly, SUCCESS, "repeat alarm, valid");
148 retvm_if(alarm_data->stime <= alarm_data->etime,
149 ALM_POPUP_ID_TIME_INVALID, "invalid alarm");
155 * This function is used to show views
156 * @param data[in] pointer to struct appdata
157 * @param bVisible[in] Eina_Bool to controlbar visible(EINA_TREU) or invisible(EINA_FALSE)
158 * @return when success, return SUCCESS or FAILED if error
161 int alm_show_view(void *data, Eina_Bool bVisible)
163 struct appdata *ad = (struct appdata *)data;
165 EVAS_OBJECT_SHOWIF(ad->layout);
166 //set controbbar visible
167 if (ad->alarmapp_controlbar_cb) {
168 (ad->alarmapp_controlbar_cb) (bVisible);
174 #ifdef ALARMAPP_FEATURE_MORNINGCALL
177 * This function is used to check whether there is morning call or not in alarm db
178 * When enter the alarm, need to call the function
180 * @return when find, return EINA_TRUE, or return EINA_FALSE
183 Eina_Bool is_alarmapp_morningcall(void)
185 Eina_Bool ret = EINA_TRUE;
186 ADList *list = alarmdb_get_data_list_by_author(ALARM_DB_AUTHOR_ALARM);
188 CLK_GOTO_IF_RUN(IS_EQUAL(NULL, list), End, ret =
189 EINA_FALSE, "morning call cannot find");
191 for (; cur; cur = cur->next) {
192 CLK_GOTO_IF_RUN(IS_STR_EQUAL
193 ("IDS_ALM_BODY_MORNING_CALL", cur->ad.name),
195 EINA_TRUE, "morning call find!!, id=%d",
201 alarmdb_free_data_list(list);
209 * This function is used to create morning call
211 * @return when success, return SUCCESS, or return FAILED
214 int alarmapp_create_morningcall(void)
218 MAGIC_VALUE_SET(alarm._magic);
219 alarm.enable = ALARM_DB_ENABLE_OFF;
220 alarm.missed = ALARM_DB_RESERVED_ALARM;
221 alarm.author = ALARM_DB_AUTHOR_ALARM;
223 snprintf(alarm.name, sizeof(alarm.name), "%s",
224 STRING_ALARM_IDS_ALM_BODY_MORNING_CALL);
228 time_t ctime = time(NULL);
229 localtime_r(&ctime, &st);
231 alarm.etime = ctime; //creat alarm time
235 alarm.atime = alarm.stime = mktime(&st);
237 alarm.repeat_once = 0;
238 alarm.repeat_every = 1;
239 alarm.repeat_weekly = 0x3E;
241 alarm.snooze_enable = EINA_TRUE;
242 alarm.snooze_min = 5;
243 alarm.snooze_times = 3;
244 clk_timezone_get(alarm.timezone, ALARM_BUF_LEN);
246 #ifdef ALARMAPP_FEATURE_SUPPORT_CSC
248 vconf_get_str(CSC_VCONF_KEY_ALARM_APP_DEFAULT_ALARM_TONE_PATH_STR);
250 CLK_ERR("vconf_get str error");
251 snprintf(alarm.tone, sizeof(alarm.tone), "%s",
252 DEFAULT_ALARM_TONE_PATH);
254 snprintf(alarm.tone, sizeof(alarm.tone), "%s", pDefaultTone);
258 snprintf(alarm.tone, sizeof(alarm.tone), "%s", DEFAULT_ALARM_TONE_PATH);
260 alarm.volume = ALARM_DEF_VOLUME;
261 alarm.auto_power_on = ALARM_DB_AUTOPOWER_OFF;
263 int id = alarmdb_add_data(&alarm);
265 return id > -1 ? SUCCESS : FAILED;
272 * This function is used to get name from path
273 * @param path[in] string for path
274 * @param name[out] string for name array
275 * @param number[in] int for @name array number
276 * @return when success, return SUCCESS or FAILED if error
279 int alm_util_get_name_from_path(const char *path, char *name, int number)
281 retv_if(!path || !name, FAILED);
282 if (IS_STR_EQUAL(path, ALARM_SOUND_PATH "/" ALARM_DEF_TONE)) {
283 snprintf(name, sizeof(char) * number, "%s",
284 STRING_ALARM_IDS_ALM_BODY_SAMSUNG_TUNE_);
287 const char *file_name = ecore_file_file_get(path);
288 char *strip_name = ecore_file_strip_ext(file_name);
289 snprintf(name, sizeof(char) * number, "%s", strip_name);
296 * This function is used to get repeat string
297 * @param buf[out] string array
298 * @param num[in] int for @buf array number
299 * @param repeat_weekly[in] char for exchange
300 * @return when success, return SUCCESS or FAILED if error
303 int alm_util_get_str_repeat(alarm_db_repeat_t repeat_weekly,
304 char mark, char *buf, int num)
306 retvm_if(!buf || num <= 0, FAILED, "buf null");
308 if (IS_EQUAL(ALARM_DB_REPEAT_NEVER, repeat_weekly)) {
309 snprintf(buf, num * sizeof(char), "%s",
310 STRING_ALARM_IDS_ALM_BODY_ONCE_);
312 } else if (IS_EQUAL(ALARM_DB_REPEAT_WEEKDAYS, repeat_weekly)) {
313 snprintf(buf, num * sizeof(char), "%s",
314 STRING_ALARM_IDS_ALM_BODY_WEEKDAYS_);
316 } else if (IS_EQUAL(ALARM_DB_REPEAT_EVERYDAY, repeat_weekly)) {
317 snprintf(buf, num * sizeof(char), "%s",
318 STRING_ALARM_IDS_ALM_BODY_EVERYDAY_);
320 } else if (IS_EQUAL(ALARM_DB_REPEAT_WEEKENDS, repeat_weekly)) {
321 snprintf(buf, num * sizeof(char), "%s",
322 STRING_ALARM_IDS_ALM_BODY_WEEKENDS_);
325 char temp[32] = { 0 };
327 for (i = 0; i < DAY_NUM; i++) {
328 if (repeat_weekly & g_repeat[i].key) {
329 snprintf(temp, sizeof(temp), "%s%c ",
330 S_(g_repeat[i].str), mark);
331 strncat(buf, temp, strlen(temp));
334 int lengh = strlen(buf);
335 if (lengh > 2 && lengh < num) {
343 char *alarm_fwk_string_get(alarm_pair_t array[], int num, int key)
345 retv_if(!array || num <= 0, NULL);
347 for (index = 0; index != num; ++index) {
348 if (IS_EQUAL(array[index].key, key)) {
349 return array[index].str;
355 char *alarm_util_time_to_string(time_t stime, int *pTimeFormat)
357 static char string_time[BUF_SIZE_32] = { 0 };
358 char tmp[BUF_SIZE_32] = { 0 };
362 vconf_get_int(VCONFKEY_REGIONFORMAT_TIME1224, &timeformat)) {
363 timeformat = VCONFKEY_TIME_FORMAT_12;
367 gmtime_r(&stime, &tm);
368 Eina_Bool bAm = tm.tm_hour > 12 ? EINA_FALSE : EINA_TRUE;
370 switch (timeformat) {
371 case VCONFKEY_TIME_FORMAT_12:
372 strftime(tmp, sizeof(char) * BUF_SIZE_32, "%I:%M", &tm);
374 case VCONFKEY_TIME_FORMAT_24:
375 strftime(tmp, sizeof(char) * BUF_SIZE_32, "%H:%M", &tm);
380 if (IS_EQUAL(timeformat, VCONFKEY_TIME_FORMAT_12)) {
381 snprintf(string_time, sizeof(string_time), "%s %s", tmp,
382 bAm ? STRING_ALARM_IDS_COM_POP_AM_M_ABB_S_ :
383 STRING_ALARM_IDS_COM_POP_PM_S_);
385 snprintf(string_time, sizeof(string_time), "%s", tmp);
389 *pTimeFormat = timeformat;
394 int alarm_util_get_alert_time_offset(AData * alarm, alarm_date_t * result)
396 retv_if(!alarm || !result, FAILED);
399 time_t cur_time = time(NULL);
400 time_t alert_time = alarm->atime;
401 double diff_time = 0;
404 int cur_repeat_day = 1;
407 /**just need to get hour and minute and second*/
408 localtime_r(&cur_time, &cur_tm);
409 cur_time = 3600 * cur_tm.tm_hour + 60 * cur_tm.tm_min + cur_tm.tm_sec;
411 localtime_r(&alert_time, &alert_tm);
413 3600 * alert_tm.tm_hour + 60 * alert_tm.tm_min + alert_tm.tm_sec;
415 for (i = 0; i < cur_tm.tm_wday; ++i) {
419 /**if the alarm is not set repeat days*/
420 if (IS_EQUAL(ALARM_DB_REPEAT_NEVER, alarm->repeat_weekly)) {
421 diff_time = difftime(alert_time, cur_time);
424 /**if the alarm include repeat days*/
425 if (alarm->repeat_weekly & cur_repeat_day) {
426 diff_time = difftime(alert_time, cur_time);
427 if (diff_time >= 0) {
431 for (i = 1; i != 7; ++i) {
432 cur_repeat_day = (cur_repeat_day * 2) % max;
433 if (alarm->repeat_weekly & cur_repeat_day) {
437 alert_time += i * 24 * 3600;
438 diff_time = difftime(alert_time, cur_time);
440 temp = (int)diff_time;
441 result->day = temp / (24 * 3600);
442 int last = temp % (24 * 3600);
443 result->hour = last / 3600;
445 result->min = last / 60;
446 result->sec = last % 60;