upload codes for TIZEN 2.0
[apps/home/clock.git] / alarm / src / alarm_fwk_util.c
1 /*
2 *
3 * Copyright 2012  Samsung Electronics Co., Ltd
4 *
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
8 *
9 *    http://www.tizenopensource.org/license
10 *
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.
16 *
17 */
18
19 #include "alarm_fwk_util.h"
20 #include "alarm_fwk_vconf.h"
21
22 /**********************************************************************
23 ******************define, struct ,typedef, union, enum, global val *************************************
24 ***********************************************************************/
25
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},
34 };
35
36 /**********************************************************************
37 ******************Global function ref*************************************
38 ***********************************************************************/
39 static int snooze_min_get()
40 {
41         int value = DEFAULT_ALARM_DB_SNOOZE_DURATION;
42 #ifdef ALARMAPP_FEATURE_SUPPORT_CSC
43         char *string =
44             vconf_get_str(CSC_VCONF_KEY_ALARM_APP_DEFAULT_SNOOZE_MINUTES_STR);
45         if (!string) {
46                 CLK_ERR("string null");
47                 goto End;
48         }
49         if (IS_STR_EQUAL(string, STRING_ALARM_OFF)) {
50                 value = 0;
51         } else if (IS_STR_EQUAL(string, STRING_ALARM_SNOOZE_TIME_1M)) {
52                 value = 1;
53         } else if (IS_STR_EQUAL(string, STRING_ALARM_SNOOZE_TIME_3M)) {
54                 value = 3;
55         } else if (IS_STR_EQUAL(string, STRING_ALARM_SNOOZE_TIME_5M)) {
56                 value = 5;
57         } else if (IS_STR_EQUAL(string, STRING_ALARM_SNOOZE_TIME_10M)) {
58                 value = 10;
59         }
60         free(string);
61  End:
62 #endif
63         return value;
64 }
65
66 /**
67 * send
68 * This function is  used to set default alarm
69 * @param           ad[in]           pointer to struct  appdata
70 * @return          void
71 * @exception
72 */
73 void set_alarm_default(struct appdata *ad)
74 {
75         AData *alarm = NULL;
76         int id = 0;
77         CLK_FUN_BEG();
78         id = alarm_genlist_name_array_get_used_id();
79
80         alarm = ad->alarm;
81         memset(alarm, 0x0, sizeof(AData));
82         MAGIC_VALUE_SET(alarm->_magic);
83
84         alarm->enable = ALARM_DB_ENABLE_ON;
85         alarm->author = ALARM_DB_AUTHOR_ALARM;
86
87         snprintf(alarm->name, sizeof(alarm->name), "%s %d",
88                  STRING_ALARM_IDS_COM_BODY_ALARM_S_, id);
89
90         time_t stime = time(NULL);
91         struct tm tm;
92         localtime_r(&stime, &tm);
93         alarm->etime = stime;
94         alarm->stime = stime;
95         alarm->atime = stime;
96         clk_timezone_get(alarm->timezone, ALARM_BUF_LEN);
97
98         alarm->repeat_once = EINA_TRUE;
99         alarm->repeat_every = EINA_FALSE;
100         alarm->repeat_weekly = 0;
101
102         alarm->snooze_enable = EINA_TRUE;
103         alarm->snooze_min = snooze_min_get();
104         alarm->snooze_times = DEFAULT_ALARM_DB_SNOOZE_REPEAT;
105
106 #ifdef ALARMAPP_FEATURE_SUPPORT_CSC
107         char *pDefaultTone =
108             vconf_get_str(CSC_VCONF_KEY_ALARM_APP_DEFAULT_ALARM_TONE_PATH_STR);
109         if (!pDefaultTone) {
110                 CLK_ERR("vconf_get str error");
111                 snprintf(alarm->tone, sizeof(alarm->tone), "%s",
112                          DEFAULT_ALARM_TONE_PATH);
113         } else {
114                 snprintf(alarm->tone, sizeof(alarm->tone), "%s", pDefaultTone);
115                 free(pDefaultTone);
116         }
117 #else
118         snprintf(alarm->tone, sizeof(alarm->tone), "%s",
119                  DEFAULT_ALARM_TONE_PATH);
120 #endif
121         alarm->volume = DEFAULT_ALARM_DB_VOLUME;
122         alarm->auto_power_on = ALARM_DB_AUTOPOWER_OFF;
123         CLK_FUN_END();
124 }
125
126 /**
127 * send
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
131 * @exception
132 */
133 int alm_name_validate(const char *buf)
134 {
135         retvm_if(IS_EQUAL(0, strlen(buf)), ALM_POPUP_ID_NAME_NULL, "name null");
136         return SUCCESS;
137 }
138
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)
143 {
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");
150         return SUCCESS;
151 }
152
153 /**
154 * send
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
159 * @exception
160 */
161 int alm_show_view(void *data, Eina_Bool bVisible)
162 {
163         struct appdata *ad = (struct appdata *)data;
164         CLK_FUN_BEG();
165         EVAS_OBJECT_SHOWIF(ad->layout);
166         //set controbbar visible
167         if (ad->alarmapp_controlbar_cb) {
168                 (ad->alarmapp_controlbar_cb) (bVisible);
169         }
170         CLK_FUN_END();
171         return 0;
172 }
173
174 #ifdef ALARMAPP_FEATURE_MORNINGCALL
175 /**
176 * send
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
179 * @param           void
180 * @return          when find, return EINA_TRUE, or return EINA_FALSE
181 * @exception
182 */
183 Eina_Bool is_alarmapp_morningcall(void)
184 {
185         Eina_Bool ret = EINA_TRUE;
186         ADList *list = alarmdb_get_data_list_by_author(ALARM_DB_AUTHOR_ALARM);
187         CLK_FUN_BEG();
188         CLK_GOTO_IF_RUN(IS_EQUAL(NULL, list), End, ret =
189                         EINA_FALSE, "morning call cannot find");
190         ADList *cur = list;
191         for (; cur; cur = cur->next) {
192                 CLK_GOTO_IF_RUN(IS_STR_EQUAL
193                                 ("IDS_ALM_BODY_MORNING_CALL", cur->ad.name),
194                                 End, ret =
195                                 EINA_TRUE, "morning call find!!, id=%d",
196                                 (int)cur->ad.id);
197         }
198         ret = EINA_FALSE;
199  End:
200         if (list) {
201                 alarmdb_free_data_list(list);
202         }
203         CLK_FUN_END();
204         return ret;
205 }
206
207 /**
208 * send
209 * This function is  used to create morning call
210 * @param           void
211 * @return          when success, return SUCCESS, or return FAILED
212 * @exception
213 */
214 int alarmapp_create_morningcall(void)
215 {
216         AData alarm = { 0 };
217         CLK_FUN_BEG();
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;
222
223         snprintf(alarm.name, sizeof(alarm.name), "%s",
224                  STRING_ALARM_IDS_ALM_BODY_MORNING_CALL);
225         //set time
226         struct tm st;
227
228         time_t ctime = time(NULL);
229         localtime_r(&ctime, &st);
230
231         alarm.etime = ctime;    //creat alarm time
232         st.tm_hour = 6;
233         st.tm_min = 0;
234         st.tm_sec = 0;
235         alarm.atime = alarm.stime = mktime(&st);
236
237         alarm.repeat_once = 0;
238         alarm.repeat_every = 1;
239         alarm.repeat_weekly = 0x3E;
240
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);
245
246 #ifdef ALARMAPP_FEATURE_SUPPORT_CSC
247         char *pDefaultTone =
248             vconf_get_str(CSC_VCONF_KEY_ALARM_APP_DEFAULT_ALARM_TONE_PATH_STR);
249         if (!pDefaultTone) {
250                 CLK_ERR("vconf_get str error");
251                 snprintf(alarm.tone, sizeof(alarm.tone), "%s",
252                          DEFAULT_ALARM_TONE_PATH);
253         } else {
254                 snprintf(alarm.tone, sizeof(alarm.tone), "%s", pDefaultTone);
255                 free(pDefaultTone);
256         }
257 #else
258         snprintf(alarm.tone, sizeof(alarm.tone), "%s", DEFAULT_ALARM_TONE_PATH);
259 #endif
260         alarm.volume = ALARM_DEF_VOLUME;
261         alarm.auto_power_on = ALARM_DB_AUTOPOWER_OFF;
262
263         int id = alarmdb_add_data(&alarm);
264         CLK_FUN_END();
265         return id > -1 ? SUCCESS : FAILED;
266
267 }
268 #endif
269
270 /**
271 * send
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
277 * @exception
278 */
279 int alm_util_get_name_from_path(const char *path, char *name, int number)
280 {
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_);
285                 return SUCCESS;
286         }
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);
290         FREEIF(strip_name);
291         return SUCCESS;
292 }
293
294 /**
295 * send
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
301 * @exception
302 */
303 int alm_util_get_str_repeat(alarm_db_repeat_t repeat_weekly,
304                             char mark, char *buf, int num)
305 {
306         retvm_if(!buf || num <= 0, FAILED, "buf null");
307
308         if (IS_EQUAL(ALARM_DB_REPEAT_NEVER, repeat_weekly)) {
309                 snprintf(buf, num * sizeof(char), "%s",
310                          STRING_ALARM_IDS_ALM_BODY_ONCE_);
311                 goto End;
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_);
315                 goto End;
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_);
319                 goto End;
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_);
323                 goto End;
324         }
325         char temp[32] = { 0 };
326         int i = 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));
332                 }
333         }
334         int lengh = strlen(buf);
335         if (lengh > 2 && lengh < num) {
336                 buf[lengh - 2] = 0;
337         }
338  End:
339         return SUCCESS;
340 }
341
342 //
343 char *alarm_fwk_string_get(alarm_pair_t array[], int num, int key)
344 {
345         retv_if(!array || num <= 0, NULL);
346         int index;
347         for (index = 0; index != num; ++index) {
348                 if (IS_EQUAL(array[index].key, key)) {
349                         return array[index].str;
350                 }
351         }
352         return NULL;
353 }
354
355 char *alarm_util_time_to_string(time_t stime, int *pTimeFormat)
356 {
357         static char string_time[BUF_SIZE_32] = { 0 };
358         char tmp[BUF_SIZE_32] = { 0 };
359         //get time format
360         int timeformat;
361         if (SUCCESS !=
362             vconf_get_int(VCONFKEY_REGIONFORMAT_TIME1224, &timeformat)) {
363                 timeformat = VCONFKEY_TIME_FORMAT_12;
364         }
365         //get am/pm
366         struct tm tm;
367         gmtime_r(&stime, &tm);
368         Eina_Bool bAm = tm.tm_hour > 12 ? EINA_FALSE : EINA_TRUE;
369         //string
370         switch (timeformat) {
371         case VCONFKEY_TIME_FORMAT_12:
372                 strftime(tmp, sizeof(char) * BUF_SIZE_32, "%I:%M", &tm);
373                 break;
374         case VCONFKEY_TIME_FORMAT_24:
375                 strftime(tmp, sizeof(char) * BUF_SIZE_32, "%H:%M", &tm);
376                 break;
377         default:
378                 break;
379         }
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_);
384         } else {
385                 snprintf(string_time, sizeof(string_time), "%s", tmp);
386         }
387         //save timeformat
388         if (pTimeFormat) {
389                 *pTimeFormat = timeformat;
390         }
391         return string_time;
392 }
393
394 int alarm_util_get_alert_time_offset(AData * alarm, alarm_date_t * result)
395 {
396         retv_if(!alarm || !result, FAILED);
397         struct tm cur_tm;
398         struct tm alert_tm;
399         time_t cur_time = time(NULL);
400         time_t alert_time = alarm->atime;
401         double diff_time = 0;
402         int max = 128;
403         int i;
404         int cur_repeat_day = 1;
405         int temp;
406
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;
410
411         localtime_r(&alert_time, &alert_tm);
412         alert_time =
413             3600 * alert_tm.tm_hour + 60 * alert_tm.tm_min + alert_tm.tm_sec;
414
415         for (i = 0; i < cur_tm.tm_wday; ++i) {
416                 cur_repeat_day *= 2;
417         }
418
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);
422                 goto End;
423         }
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) {
428                         goto End;
429                 }
430         }
431         for (i = 1; i != 7; ++i) {
432                 cur_repeat_day = (cur_repeat_day * 2) % max;
433                 if (alarm->repeat_weekly & cur_repeat_day) {
434                         break;
435                 }
436         }
437         alert_time += i * 24 * 3600;
438         diff_time = difftime(alert_time, cur_time);
439  End:
440         temp = (int)diff_time;
441         result->day = temp / (24 * 3600);
442         int last = temp % (24 * 3600);
443         result->hour = last / 3600;
444         last = last % 3600;
445         result->min = last / 60;
446         result->sec = last % 60;
447
448         return SUCCESS;
449 }