test
[apps/core/preloaded/calendar.git] / include / cld-utils.h
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 #ifndef __CALENDAR_UTILS_H__
20 #define __CALENDAR_UTILS_H__
21
22 #include <assert.h>
23 #include <Elementary.h>
24
25 #ifndef _
26 #  define _(str) gettext(str)
27 #endif
28
29 #ifndef N_
30 #  define gettext_noop(str) (str)
31 #  define N_(str) gettext_noop(str)
32 #endif
33
34 #ifndef S_
35 #  define S_(str) dgettext("sys_string", str)
36 #endif
37
38 #ifndef C_
39 #  define C_(str) dgettext(CALENDAR_PACKAGE, str)
40 #endif
41
42 #ifndef U_
43 #  define U_(str) dgettext(UGNAME, str)
44 #endif
45
46 #ifndef EDJDIR
47 #  define EDJDIR "/opt/apps/org.tizen.calendar/res/edje"
48 #endif
49
50 #ifndef CALENDAR_VCONF_PATH
51 #define CALENDAR_VCONF_PATH "db/private/org.tizen.calendar/"
52 #endif
53
54 #ifndef CAL_VCONFKEY_SEND_EVENT_METHOD
55 #define CAL_VCONFKEY_SEND_EVENT_METHOD CALENDAR_VCONF_PATH"send"
56 #define CAL_VCONFKEY_LOCK_TIMEZONE_ON_OFF CALENDAR_VCONF_PATH"timezone_on_off"
57 #define CAL_VCONFKEY_LOCK_TIMEZONE_PATH CALENDAR_VCONF_PATH"timezone_path"
58 #define CAL_VCONFKEY_LOCK_TIMEZONE_CITY CALENDAR_VCONF_PATH"timezone_city"
59 #define CAL_VCONFKEY_LOCK_TIMEZONE_OFFSET CALENDAR_VCONF_PATH"timezone_offset"
60 #define CAL_VCONFKEY_FIRST_DAY_OF_WEEK CALENDAR_VCONF_PATH"first_day_of_week"
61 #define CAL_VCONFKEY_FOCUS_VIEW CALENDAR_VCONF_PATH"focusview"
62 #define CAL_VCONFKEY_COMPLETE_TODO_ON_OFF CALENDAR_VCONF_PATH"complete_todo_on_off"
63 #define CAL_VCONFKEY_WEEK_NMUBER_ON_OFF CALENDAR_VCONF_PATH"display_week_number"
64 #define CAL_VCONFKEY_LAST_LONGITUDE CALENDAR_VCONF_PATH"longitude"
65 #define CAL_VCONFKEY_LAST_LATITUDE CALENDAR_VCONF_PATH"latitude"
66 #endif
67
68 #define EDJ_FILE EDJDIR "/" CALENDAR_PACKAGE "-black-theme.edj"
69 #define EDJ_FILE_WHITE EDJDIR "/" CALENDAR_PACKAGE "-white-theme.edj"
70
71
72 #define CAL_UTIL_GET_PRIV_DATA(obj) evas_object_data_get(obj, "priv")
73 #define CAL_UTIL_GET_EDJ_DATA(layout) elm_layout_edje_get(layout)
74
75 #define CAL_MAX_HEAP_SIZE       5*1024*1024  /*Malloc more than this will not be allowd*/
76
77
78 /*      void *memcpy(size_t nelements, size_t bfbes);
79  *      which will allocate a memory and copy the data to the allocated memory
80  *
81  */
82 #define CAL_MEMCPY(dest,src,type) \
83         do{ \
84                 if(src != NULL) \
85                 { \
86                         assert(dest); \
87                         memcpy(dest,src,sizeof(type)); \
88                 } \
89         } while(0);
90
91 /*      void *calloc(size_t nelements, size_t bfbes);           */
92 /*      which allocates a region of memory large enough to hold nelements of size bfbes each.
93         The allocated region is initialized to zero.    */
94 #define CAL_CALLOC(ptr, no_elements, type)       \
95         do { \
96                 if ((int)(no_elements) <= 0) \
97                 { \
98                         ptr = NULL; \
99                 } \
100                 else if(CAL_MAX_HEAP_SIZE < no_elements* sizeof (type)) \
101                 { \
102                         assert(0); \
103                 } \
104                 else \
105                 { \
106                         ptr = (type *) calloc (no_elements , sizeof (type));    \
107                         assert(ptr);             \
108                 }\
109         }while(0);
110
111 /*
112  * Returns string length of src and 0 if it is NULL
113  */
114 #define CAL_STRLEN(src)  ((src != NULL)? strlen(src): 0)
115
116 /*
117  * Gives comparison result of two strings and returns -1 if any of two is NULL
118  */
119 #define CAL_STRCMP(str1, str2)  g_strcmp0(str1, str2)
120
121 /*      This assert will log an err on stderr and abort,  if assertion failed,  */
122 /*      Use this only if the now service needs to be exited, if asertion fails          */
123
124 #define CAL_ASSERT(scalar_exp)  \
125         do { \
126                 if (!scalar_exp)        \
127                 {       \
128                         ERR("CAL_ASSERT()");\
129                         assert(scalar_exp);     \
130                 }       \
131         }while(0);
132
133 #define CAL_STRDUP(str) (str && *str && strlen(str))?strdup(str):NULL
134
135 #define CAL_STRNCPY(dest,src,size)      \
136         do { \
137                 if(src != NULL && dest != NULL && size > 0) \
138                 {       \
139                         strncpy(dest,src,size); \
140                 }       \
141         }while(0);
142
143 #define CAL_STRNCAT(dest,src,size)      \
144         do { \
145                 if(src != NULL && dest != NULL && size > 0) \
146                 {       \
147                         strncat(dest,src,size); \
148                 }       \
149         }while(0);
150
151
152 /*
153         Date & Time Format
154         http://userguide.icu-project.org/formatparse/datetime
155 */
156 #define CAL_UTIL_DATE_FORMAT_1 "EEMMMddyyyy" /* Thu Aug 23 2001 */
157 #define CAL_UTIL_DATE_FORMAT_2 "EE" /* Thu */
158 #define CAL_UTIL_DATE_FORMAT_3 "ddEE" /* 27 Thu */
159 #define CAL_UTIL_DATE_FORMAT_4 "MMMM" /* August */
160 #define CAL_UTIL_DATE_FORMAT_5 "yyyy" /* 2011 */
161 #define CAL_UTIL_DATE_FORMAT_6 "MMMMyyyy" /* August 2011 */
162 #define CAL_UTIL_DATE_FORMAT_7 "EEEEMMMdd" /* Thursday Aug 23 */
163 #define CAL_UTIL_DATE_FORMAT_8 "dMMyyyy" /* 23 08 2011 */
164 #define CAL_UTIL_DATE_FORMAT_9 "d" /* 27 */
165 #define CAL_UTIL_DATE_FORMAT_10 "MMMd" /* Aug 27 */
166 #define CAL_UTIL_DATE_FORMAT_11 "MMMMdyyyy" /* August 23 2011 */
167 #define CAL_UTIL_DATE_FORMAT_12 "MMMdyyyy" /* Aug 23 2011 */
168 #define CAL_UTIL_DATE_FORMAT_13 "EEMMMMddyyyy" /* Thu August 23 2001 */
169 #define CAL_UTIL_DATE_FORMAT_14 "EEEE" /* Thursday */
170 #define CAL_UTIL_DATE_FORMAT_15 "EEdMMMMyyyy" /* Thu August 23 2001 */
171 #define CAL_UTIL_DATE_FORMAT_16 "ww" /* week of year */
172 #define CAL_UTIL_DATE_FORMAT_17 "EEEEE" /* day of week "J" "T" "W" */
173
174 #define CAL_UTIL_TIME_FORMAT_1 "hhmma" /* 07:58 AM */
175 #define CAL_UTIL_TIME_FORMAT_2 "hhmm" /* 07:58 */
176 #define CAL_UTIL_TIME_FORMAT_3 "hha" /* 07 AM */
177 #define CAL_UTIL_TIME_FORMAT_4 "a" /* AM */
178 #define CAL_UTIL_TIME_FORMAT_5 "hma" /* 7:58 AM */
179 #define CAL_UTIL_TIME_FORMAT_6 "HHmm" /* 19:58 */
180 #define CAL_UTIL_TIME_FORMAT_7 "HH" /* 19 */
181 #define CAL_UTIL_TIME_FORMAT_8 "Hm" /* 7:58 AM */
182
183 #define CAL_SECONDS_FOR_THIRTY_MINUTES (60 * 30)
184 #define CAL_HALF_HOURS_FOR_DAY (24 * 2)
185
186 enum cld_icon {
187         CI_EDIT = 0,
188         CI_NEW,
189         CI_DEL,
190         CI_PHOTO,
191         CI_MAX
192 };
193
194 static inline int CAL_UTIL_GET_WDAY(int w)
195 {
196         while (w > 6) w -= 7;
197         while (w < 0) w += 7;
198         return w;
199 }
200
201 int cal_util_get_max_days(int tm_year, int tm_mon);
202 time_t cal_util_get_max_time(void);
203 time_t cal_util_get_min_time(void);
204
205 int cal_util_update_tm_year(struct tm *t, int delta);
206 int cal_util_update_tm_month(struct tm *t, int delta);
207 int cal_util_update_tm_day(struct tm *t, int delta);
208 int cal_util_update_tm_hour(struct tm *tm, int delta);
209
210 void cal_util_convert_lli_to_tm(const char *timezone, long long int lli, struct tm *tm);
211 void cal_util_convert_tm_to_lli(const char *timezone, struct tm *tm, long long int *lli);
212 void cal_util_convert_lli_to_time_t(const char *timezone, long long int lli, time_t *time);
213 void cal_util_convert_time_t_to_lli(const char *timezone, time_t time, long long int *lli);
214
215 int cal_util_get_day_time_t(struct tm *t, time_t *st, time_t *et);
216 int cal_util_get_week_time_t(struct tm *t, time_t *st, time_t *et, int start);
217 int cal_util_get_month_time_t(struct tm *t, time_t *st, time_t *et);
218 int cal_util_get_year_time_t(struct tm *t, time_t *st, time_t *et);
219
220 float cal_util_nsin(float f);
221 int cal_util_get_week_flag(char *week_s);
222
223 int cal_util_get_time_text(char *buf, int buf_size, const char *date, const char *time, const struct tm *tm);
224
225 int cal_util_connect_pattern_generator(void);
226 int cal_util_disconnect_pattern_generator(void);
227
228 Evas_Object* cal_util_add_bg(Evas_Object *obj, Eina_Bool is_window);
229 Evas_Object* cal_util_add_layout(Evas_Object *win, const char *group);
230 Evas_Object* cal_util_add_window(const char *name, Evas_Coord *w, Evas_Coord *h);
231 Evas_Object* cal_util_add_icon_button(Evas_Object *p, enum cld_icon i);
232 Evas_Object* cal_util_add_icon(Evas_Object *p, enum cld_icon i);
233 Evas_Object* cal_util_add_rectangle(Evas_Object *p);
234 Evas_Object* cal_util_add_separator(Evas_Object *box, const char *style);
235 Evas_Object* cal_util_add_scroller(Evas_Object *p);
236 Evas_Object* cal_util_add_box(Evas_Object *p);
237 Evas_Object* cal_util_add_edit_field(Evas_Object *parent, const char *title, const char *guide, Eina_Bool single_line, Eina_Bool is_editable);
238 Evas_Object * cal_util_add_searchbar(Evas_Object *parent, const char *text,  const char *guide, Eina_Bool cancel_button);
239 Evas_Object * cal_util_add_nocontents(Evas_Object *parent, const char *label);
240 Evas_Object * cal_util_add_controlbar(Evas_Object * parent);
241 Evas_Object * cal_util_add_popup(Evas_Object *parent, const char *style, const char *title, const char *desc,
242         void (*callback_func)(void *data, Evas_Object *obj, void *ei), void *data, ...);
243 Evas_Object * cal_util_add_datetime(Evas_Object *parent, const char *title, const struct tm *tm);
244
245 static inline void cal_util_get_geometry(Evas_Coord_Rectangle *r, const Evas_Object *obj)
246 {
247         evas_object_geometry_get(obj, &r->x, &r->y, &r->w, &r->h);
248 }
249
250 static inline void cal_util_delete_evas_object(Evas_Object **p)
251 {
252         if (p && *p) {
253                 evas_object_del(*p);
254                 *p = NULL;
255         }
256 }
257
258 char* cal_util_get_part_text(const char *fmt, int i);
259 void cal_util_set_text(Evas_Object *obj, const char *part, const char *fmt, ...);
260 void cal_util_set_time_text(Evas_Object *obj, const char *part, const char *date, const char* time, const struct tm *tm);
261 void cal_util_set_time_week_text(Evas_Object *obj, const char *part, const char *date, const char* time, const struct tm *t, int start);
262 void cal_util_emit_signal(Evas_Object *obj, const char *fmt, ...);
263 void cal_util_set_controlbar_button(Evas_Object *btn, char *label, char *style, void (*func) (void *data, Evas_Object *obj, void *event_info), void* data);
264 int cal_util_create_vcs_file_from_cs(cal_struct *cs, const char *filename);
265 void cal_util_get_timezone(char **timezone_id, char **timezone_city, char **timezone_offset);
266 void cal_util_get_week_number_text(const struct tm* tm, char *buffer, int buffer_size);
267 int cal_util_get_distance(Evas_Coord_Point *s, Evas_Coord_Point *e);
268 char *cal_util_get_search_color_text(const char *search_str, const char *input_str);
269 void cal_util_set_timezone(const char *timezone);
270 void cal_util_initialize_timezone();
271 void cal_util_get_timezone_id(int timezone_offset, char **timezone_id);
272
273 #endif /* __CALENDAR_UTILS_H__ */
274