d258949160fdc707566b2e6be69efeb6a79c3c95
[apps/core/preloaded/calendar.git] / common / reminder.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://floralicense.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 #include "cld.h"
19 #include "reminder.h"
20
21 static inline void __cal_reminder_get_week_str(int week, char *buf, int sz)
22 {
23         snprintf(buf, sz, "%d %s %s", week,C_("IDS_COM_POP_WEEK_LC"), C_("IDS_CLD_BODY_BEFORE_LC"));
24 }
25
26 static inline void __cal_reminder_get_day_str(int day, char *buf, int sz)
27 {
28         snprintf(buf, sz, "%d %s %s", day, C_("IDS_COM_POP_DAY_LC"), C_("IDS_CLD_BODY_BEFORE_LC"));
29 }
30
31 static inline void __cal_reminder_get_hour_str(int hour, char *buf, int sz)
32 {
33         snprintf(buf, sz, "%d %s %s", hour, S_("IDS_COM_POP_HOUR_LC"), C_("IDS_CLD_BODY_BEFORE_LC"));
34 }
35
36 static inline void __cal_reminder_get_min_str(int min, char *buf, int sz)
37 {
38         snprintf(buf, sz, "%d %s %s", min, S_("IDS_COM_BODY_MINUTE_LC"), C_("IDS_CLD_BODY_BEFORE_LC"));
39 }
40
41 static inline void __cal_reminder_get_str(const Cal_Reminder* reminder, char *buf, int sz)
42 {
43         int min = reminder->alarm_value;
44
45         if ((min % _WEEK) == 0) {
46                 __cal_reminder_get_week_str(min / _WEEK, buf, sz);
47                 return;
48         }
49
50         if ((min % _DAY) == 0) {
51                 __cal_reminder_get_day_str(min / _DAY, buf, sz);
52                 return;
53         }
54
55         if ((min % _HOUR) == 0) {
56                 __cal_reminder_get_hour_str(min / _HOUR, buf, sz);
57                 return;
58         }
59
60         __cal_reminder_get_min_str(min, buf, sz);
61 }
62
63 void cal_reminder_get_string(const Cal_Reminder* reminder, char *buf, int size)
64 {
65         if (!buf || size < 0)
66                 return;
67
68         switch (reminder->tick_unit) {
69         case CALENDAR_ALARM_TIME_UNIT_SPECIFIC:
70                 cal_util_get_time_text(buf, size, CAL_UTIL_DATE_FORMAT_1, CAL_UTIL_TIME_FORMAT_5, &reminder->datetime);
71                 break;
72         case CALENDAR_ALARM_NONE:
73                 g_snprintf(buf, size, "%s", S_("IDS_COM_BODY_OFF"));
74                 break;
75         case CALENDAR_ALARM_TIME_UNIT_MINUTE:
76                 if (reminder->alarm_value == 0)
77                         g_snprintf(buf, size, "%s", C_("IDS_CLD_BODY_ON_TIME"));
78                 else
79                         __cal_reminder_get_str(reminder, buf, size);
80                 break;
81         default:
82                 ERR("ERROR!! %d", reminder->tick_unit);
83                 break;
84         }
85 }
86
87 Cal_Reminder* cal_reminder_create(calendar_record_h calendar_alarm)
88 {
89         c_retv_if(!calendar_alarm, NULL);
90
91         calendar_error_e error = CALENDAR_ERROR_NONE;
92
93         int tick = 0;
94         error = calendar_record_get_int(calendar_alarm, _calendar_alarm.tick, &tick);
95         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_get_int() is failed(%x)", error);
96
97         calendar_alarm_time_unit_type_e tick_unit = CALENDAR_ALARM_NONE;
98
99         error = calendar_record_get_int(calendar_alarm, _calendar_alarm.tick_unit, &tick_unit);
100         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_get_int() is failed(%x)", error);
101
102         calendar_time_s alarm_time = {0};
103
104         error = calendar_record_get_lli(calendar_alarm, _calendar_alarm.time, &alarm_time.time.utime);
105         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_get_caltime() is failed(%x)", error);
106
107         _calendar_show_error(error);
108
109         Cal_Reminder* reminder = calloc(1, sizeof(Cal_Reminder));
110         c_retv_if(!reminder, NULL);
111
112         cal_util_convert_lli_to_tm(NULL, alarm_time.time.utime, &reminder->datetime);
113
114         if (tick_unit == CALENDAR_ALARM_NONE) {
115                 reminder->tick_unit = CALENDAR_ALARM_NONE;
116                 return reminder;
117         }
118
119         if (tick_unit == CALENDAR_ALARM_TIME_UNIT_SPECIFIC) {
120                 reminder->tick_unit = CALENDAR_ALARM_TIME_UNIT_SPECIFIC;
121                 return reminder;
122         }
123
124         switch (tick_unit) {
125         case CALENDAR_ALARM_TIME_UNIT_MONTH:
126                 reminder->alarm_value = tick * _DAY * 30; // TODO: BUG!! not used!!
127                 break;
128         case CALENDAR_ALARM_TIME_UNIT_WEEK:
129                 reminder->alarm_value = tick * _WEEK;
130                 break;
131         case CALENDAR_ALARM_TIME_UNIT_DAY:
132                 reminder->alarm_value = tick * _DAY;
133                 break;
134         case CALENDAR_ALARM_TIME_UNIT_HOUR:
135                 reminder->alarm_value = tick * _HOUR;
136                 break;
137         case CALENDAR_ALARM_TIME_UNIT_MINUTE:
138                 reminder->alarm_value = tick;
139                 break;
140         default:
141                 reminder->alarm_value = 0;
142                 break;
143         }
144
145         reminder->tick_unit = CALENDAR_ALARM_TIME_UNIT_MINUTE;
146
147         return reminder;
148 }
149
150 calendar_record_h cal_reminder_get_cal_val(const Cal_Reminder* reminder)
151 {
152         c_retv_if(!reminder, NULL);
153
154         calendar_record_h calendar_alarm = NULL;
155
156         calendar_error_e error = CALENDAR_ERROR_NONE;
157
158         error = calendar_record_create(_calendar_alarm._uri, &calendar_alarm);
159         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_create() is failed(%x)", error);
160
161         if (reminder->tick_unit == CALENDAR_ALARM_NONE) {
162
163                 error = calendar_record_set_int(calendar_alarm, _calendar_alarm.tick_unit, CALENDAR_ALARM_NONE);
164                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_set_int() is failed(%x)", error);
165
166                 return calendar_alarm;
167         }
168
169         long long int lli_time;
170         cal_util_convert_tm_to_lli(NULL, &reminder->datetime, &lli_time);
171
172         error = calendar_record_set_lli(calendar_alarm, _calendar_alarm.time, lli_time);
173         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_set_lli() is failed(%x)", error);
174
175         error = calendar_record_set_int(calendar_alarm, _calendar_alarm.tick_unit, CALENDAR_ALARM_TIME_UNIT_SPECIFIC);
176         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_set_int() is failed(%x)", error);
177
178         int min = reminder->alarm_value;
179         int tick;
180         calendar_alarm_time_unit_type_e tick_unit;
181
182         if (reminder->tick_unit == CALENDAR_ALARM_TIME_UNIT_SPECIFIC) {
183                 tick = 0;
184                 tick_unit = CALENDAR_ALARM_TIME_UNIT_SPECIFIC;
185         } else if (min == -1) {
186                 tick = -1;
187                 tick_unit = CALENDAR_ALARM_TIME_UNIT_MINUTE;
188         } else if ((min % _WEEK) == 0) {
189                 tick = min / _WEEK;
190                 tick_unit = CALENDAR_ALARM_TIME_UNIT_WEEK;
191         } else if ((min % _DAY) == 0) {
192                 tick = min / _DAY;
193                 tick_unit = CALENDAR_ALARM_TIME_UNIT_DAY;
194         } else if ((min % _HOUR) == 0) {
195                 tick = min / _HOUR;
196                 tick_unit = CALENDAR_ALARM_TIME_UNIT_HOUR;
197         } else {
198                 tick = min;
199                 tick_unit = CALENDAR_ALARM_TIME_UNIT_MINUTE;
200         }
201
202         error = calendar_record_set_int(calendar_alarm, _calendar_alarm.tick, tick);
203         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_set_int() is failed(%x)", error);
204
205         error = calendar_record_set_int(calendar_alarm, _calendar_alarm.tick_unit, tick_unit);
206         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_set_int() is failed(%x)", error);
207
208         return calendar_alarm;
209 }