[misc] Sync from master branch.
[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://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
19 #ifndef __CALENDAR_UTILS_H__
20 #define __CALENDAR_UTILS_H__
21
22 #include <assert.h>
23 #include <Elementary.h>
24 #include <glib.h>
25
26 #ifndef _
27 #  define _(str) gettext(str)
28 #endif
29
30 #ifndef N_
31 #  define gettext_noop(str) (str)
32 #  define N_(str) gettext_noop(str)
33 #endif
34
35 #ifndef S_
36 #  define S_(str) dgettext("sys_string", str)
37 #endif
38
39 #ifndef C_
40 #  define C_(str) dgettext(CALENDAR, str)
41 #endif
42
43 #ifndef U_
44 #  define U_(str) dgettext(UGNAME, str)
45 #endif
46
47 #ifndef EDJDIR
48 #  define EDJDIR "/usr/apps/"CALENDAR_PACKAGE"/res/edje"
49 #endif
50
51 #ifndef CALENDAR_VCONF_PATH
52 #define CALENDAR_VCONF_PATH "db/private/"CALENDAR_PACKAGE"/"
53 #endif
54
55 #ifndef CAL_VCONFKEY_SEND_EVENT_METHOD
56 #define CAL_VCONFKEY_SEND_EVENT_METHOD CALENDAR_VCONF_PATH"send"
57 #define CAL_VCONFKEY_LOCK_TIMEZONE_ON_OFF CALENDAR_VCONF_PATH"timezone_on_off"
58 #define CAL_VCONFKEY_LOCK_TIMEZONE_PATH CALENDAR_VCONF_PATH"timezone_path"
59 #define CAL_VCONFKEY_LOCK_TIMEZONE_CITY CALENDAR_VCONF_PATH"timezone_city"
60 #define CAL_VCONFKEY_LOCK_TIMEZONE_OFFSET CALENDAR_VCONF_PATH"timezone_offset"
61 #define CAL_VCONFKEY_FIRST_DAY_OF_WEEK CALENDAR_VCONF_PATH"first_day_of_week"
62 #define CAL_VCONFKEY_FOCUS_VIEW CALENDAR_VCONF_PATH"focusview"
63 #define CAL_VCONFKEY_COMPLETE_TODO_ON_OFF CALENDAR_VCONF_PATH"complete_todo_on_off"
64 #define CAL_VCONFKEY_WEEK_NMUBER_ON_OFF CALENDAR_VCONF_PATH"display_week_number"
65 #define CAL_VCONFKEY_NOTIFICATION_ID CALENDAR_VCONF_PATH"notification_id"
66 #define CAL_VCONFKEY_CREATE_EVENT_MODE CALENDAR_VCONF_PATH"create_event_mode"
67
68 #endif
69
70 #define EDJ_FILE EDJDIR "/" CALENDAR ".edj"
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 #define CAL_FREE(ptr) \
151         do { \
152                 free(ptr); \
153                 ptr = NULL; \
154         }while(0);
155
156
157 /*
158         Date & Time Format
159         http://userguide.icu-project.org/formatparse/datetime
160 */
161 #define CAL_UTIL_DATE_FORMAT_1 "EEMMMddyyyy" /* Thu Aug 23 2001 */
162 #define CAL_UTIL_DATE_FORMAT_2 "EE" /* Thu */
163 #define CAL_UTIL_DATE_FORMAT_3 "ddEE" /* 27 Thu */
164 #define CAL_UTIL_DATE_FORMAT_4 "MMMM" /* August */
165 #define CAL_UTIL_DATE_FORMAT_5 "yyyy" /* 2011 */
166 #define CAL_UTIL_DATE_FORMAT_6 "MMMMyyyy" /* August 2011 */
167 #define CAL_UTIL_DATE_FORMAT_7 "EEEEMMMdd" /* Thursday Aug 23 */
168 #define CAL_UTIL_DATE_FORMAT_8 "dMMyyyy" /* 23 08 2011 */
169 #define CAL_UTIL_DATE_FORMAT_9 "d" /* 27 */
170 #define CAL_UTIL_DATE_FORMAT_10 "MMMd" /* Aug 27 */
171 #define CAL_UTIL_DATE_FORMAT_11 "MMMMdyyyy" /* August 23 2011 */
172 #define CAL_UTIL_DATE_FORMAT_12 "MMMdyyyy" /* Aug 23 2011 */
173 #define CAL_UTIL_DATE_FORMAT_13 "EEMMMMddyyyy" /* Thu August 23 2001 */
174 #define CAL_UTIL_DATE_FORMAT_14 "EEEE" /* Thursday */
175 #define CAL_UTIL_DATE_FORMAT_15 "EEdMMMMyyyy" /* Thu August 23 2001 */
176 #define CAL_UTIL_DATE_FORMAT_16 "ww" /* week of year */
177 #define CAL_UTIL_DATE_FORMAT_17 "EEEEE" /* day of week "J" "T" "W" */
178 #define CAL_UTIL_DATE_FORMAT_18 "MMMyyyy" /* Aug 2011 */
179
180 #define CAL_UTIL_TIME_FORMAT_1 "hhmma" /* 07:58 AM */
181 #define CAL_UTIL_TIME_FORMAT_2 "hhmm" /* 07:58 */
182 #define CAL_UTIL_TIME_FORMAT_3 "hha" /* 07 AM */
183 #define CAL_UTIL_TIME_FORMAT_4 "a" /* AM */
184 #define CAL_UTIL_TIME_FORMAT_5 "hma" /* 7:58 AM */
185 #define CAL_UTIL_TIME_FORMAT_6 "HHmm" /* 19:58 */
186 #define CAL_UTIL_TIME_FORMAT_7 "HH" /* 19 */
187 #define CAL_UTIL_TIME_FORMAT_8 "Hm" /* 7:58 AM */
188
189 #define CAL_SECONDS_FOR_THIRTY_MINUTES (60 * 30)
190 #define CAL_HALF_HOURS_FOR_DAY (24 * 2)
191
192 enum cld_icon {
193         CI_EDIT = 0,
194         CI_NEW,
195         CI_DEL,
196         CI_PHOTO,
197         CI_MAX
198 };
199
200 static inline int CAL_UTIL_GET_WDAY(int w)
201 {
202         while (w > 6) w -= 7;
203         while (w < 0) w += 7;
204         return w;
205 }
206
207 int cal_util_get_max_days(int tm_year, int tm_mon);
208 time_t cal_util_get_max_time(void);
209 time_t cal_util_get_min_time(void);
210
211 time_t cal_util_update_tm_year(struct tm *t, int delta);
212 time_t cal_util_update_tm_month(struct tm *t, int delta);
213 time_t cal_util_update_tm_day(struct tm *t, int delta);
214 time_t cal_util_update_tm_hour(struct tm *tm, int delta);
215
216 void cal_util_convert_lli_to_tm(const char *timezone, long long int lli, struct tm *tm);
217 void cal_util_convert_tm_to_lli(const char *timezone, const struct tm *tm, long long int *lli);
218 void cal_util_convert_lli_to_time_t(const char *timezone, long long int lli, time_t *time);
219 void cal_util_convert_time_t_to_lli(const char *timezone, time_t time, long long int *lli);
220
221 int cal_util_get_day_time_t(struct tm *t, time_t *st, time_t *et);
222 int cal_util_get_week_time_t(struct tm *t, time_t *st, time_t *et, int start);
223 int cal_util_get_month_time_t(struct tm *t, time_t *st, time_t *et);
224 int cal_util_get_year_time_t(struct tm *t, time_t *st, time_t *et);
225
226 time_t cal_util_get_month_start_time(const struct tm* subject_date, struct tm* month_start_time);
227 time_t cal_util_get_month_end_time(const struct tm* subject_date, struct tm* month_end_time);
228 time_t cal_util_get_week_start_time(const struct tm* subject_date, struct tm* week_start_time,
229                                                                         int start_wday);
230 time_t cal_util_get_week_end_time(const struct tm* subject_date, struct tm* week_end_time,
231                                                                   int start_wday);
232
233 int cal_util_get_day_diff(const struct tm* date1, const struct tm* date2);
234 int cal_util_compare_day(const struct tm* date1, const struct tm* date2);
235
236 const char* cal_util_print_day(const struct tm* day);
237
238 float cal_util_nsin(float f);
239 int cal_util_get_week_flag(char *week_s);
240
241 int cal_util_get_time_text(char *buf, int buf_size, const char *date, const char *time, const struct tm *tm);
242 void cal_util_set_time_text_toupper(Evas_Object *obj, const char *part, const char *date, const char* time, const struct tm *tm);
243
244 int cal_util_connect_pattern_generator(void);
245 int cal_util_disconnect_pattern_generator(void);
246
247 Evas_Object* cal_util_add_bg(Evas_Object *obj, Eina_Bool is_window);
248 Evas_Object* cal_util_add_layout(Evas_Object *win, const char *group);
249 Evas_Object* cal_util_add_window(const char *name, Evas_Coord *w, Evas_Coord *h);
250 Evas_Object* cal_util_add_rectangle(Evas_Object *p);
251 Evas_Object* cal_util_add_separator(Evas_Object *box, const char *style);
252 Evas_Object* cal_util_add_scroller(Evas_Object *p);
253 Evas_Object* cal_util_add_box(Evas_Object *p);
254 Evas_Object * cal_util_add_edit_field(Evas_Object *parent, const char *title, const char *guide, Eina_Bool single_line, Eina_Bool is_editable, Eina_Bool is_no_bg);
255 Evas_Object * cal_util_add_nocontents(Evas_Object *parent, const char *label);
256 Evas_Object * cal_util_add_search_nocontents(Evas_Object *parent, const char *label);
257 Evas_Object * cal_util_add_popup(Evas_Object *parent, const char *style, const char *title, const char *desc,
258         void (*callback_func)(void *data, Evas_Object *obj, void *ei), void *data, ...);
259 Evas_Object * cal_util_add_datetime(Evas_Object *parent, const char *title, const struct tm *tm);
260
261 static inline void cal_util_get_geometry(Evas_Coord_Rectangle *r, const Evas_Object *obj)
262 {
263         evas_object_geometry_get(obj, &r->x, &r->y, &r->w, &r->h);
264 }
265
266 static inline void cal_util_delete_evas_object(Evas_Object **p)
267 {
268         if (p && *p) {
269                 evas_object_del(*p);
270                 *p = NULL;
271         }
272 }
273
274 char* cal_util_get_part_text(const char *fmt, int i);
275 void cal_util_set_text(Evas_Object *obj, const char *part, const char *fmt, ...);
276 void cal_util_set_time_text(Evas_Object *obj, const char *part, const char *date, const char* time, const struct tm *tm);
277 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);
278 void cal_util_emit_signal(Evas_Object *obj, const char *fmt, ...);
279 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);
280 void cal_util_get_timezone(char **timezone_id, char **timezone_city, char **timezone_offset);
281 void cal_util_get_week_number_text(const struct tm* tm, char *buffer, int buffer_size);
282 int cal_util_get_distance(Evas_Coord_Point *s, Evas_Coord_Point *e);
283 char *cal_util_get_search_color_text(const char *search_str, const char *input_str);
284 void cal_util_set_timezone(const char *timezone);
285 void cal_util_initialize_timezone();
286 void cal_util_get_timezone_id(int timezone_offset, char **timezone_id);
287 Evas_Object* cal_util_add_layout_noindicator(Evas_Object *win);
288 void cal_util_show_small_information(Evas_Object *layout, const char *message, double timeout);
289 void cal_util_hide_small_information(Evas_Object *layout);
290 Evas_Object * cal_util_add_toolbar_button(Evas_Object *naviframe, const char *part, const char *text, Evas_Smart_Cb func, void *data);
291 Evas_Object * cal_util_add_toolbar_button2(Evas_Object *naviframe, const char *part, const char *filename, Evas_Smart_Cb func, void *data);
292
293 void cal_util_update_media_db(const char *file_path);
294
295 void cal_util_set_item_time_text(Elm_Object_Item *item, const char *part, const char *date, const char* time, const struct tm *tm);
296 void cal_util_set_item_time_week_text(Elm_Object_Item *obj, const char *part, const char *date, const char* time, const struct tm *t, int start);
297
298 int cal_util_get_flick_distance_threshold();
299
300 int cal_util_get_default_first_day_of_week();
301
302 #endif /* __CALENDAR_UTILS_H__ */
303