Tizen 2.1 base
[apps/core/preloaded/calendar.git] / ug / settings / ug-settings.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
19 #include <ui-gadget-module.h>
20 #include <libintl.h>
21 #include <vconf.h>
22
23 #include "cld.h"
24 #include "ug-settings.h"
25 #include "ug-settings-view.h"
26
27 static void __cal_setting_ug_back_button_callback(void *data, Evas_Object *obj, void *event_info)
28 {
29         struct ug_data *ugd = data;
30         evas_object_hide(ugd->bg);
31         ug_destroy_me(ugd->ug);
32 }
33
34 static void __cal_setting_ug_initialize(struct ug_data *ugd)
35 {
36         calendar_error_e error = CALENDAR_ERROR_NONE;
37
38         error = calendar_connect();
39         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_connect() is failed(%x)", error);
40
41         _calendar_init_hash();
42
43         char *text = vconf_get_str(CAL_VCONFKEY_LOCK_TIMEZONE_CITY);
44         c_warn_if(!text, "vconf_get_str(CAL_VCONFKEY_LOCK_TIMEZONE_CITY) is failed");
45
46         if (text) {
47                 ugd->city = strdup(text);
48                 free(text);
49         }
50         else {
51                 ERR("tz_city is null");
52                 ugd->city = strdup("IDS_WCL_BODY_CITYNAME_SEOUL");
53                 c_warn_if(!ugd->city, "ugd->city is null");
54         }
55
56         text = vconf_get_str(CAL_VCONFKEY_LOCK_TIMEZONE_OFFSET);
57         c_warn_if(!text, "vconf_get_str(CAL_VCONFKEY_LOCK_TIMEZONE_OFFSET) is failed");
58
59         if (text) {
60                 ugd->time_zone = g_strdup_printf("GMT%s",text);
61                 free(text);
62         }
63         else {
64                 ERR("tz_offset is null");
65                 ugd->time_zone = strdup("GMT+9");
66                 c_warn_if(!ugd->time_zone, "ugd->city is null");
67         }
68
69         int ret = 0;
70
71         ret = vconf_get_int(CAL_VCONFKEY_LOCK_TIMEZONE_ON_OFF, &ugd->is_on);
72         c_warn_if(ret, "vconf_get_int(CAL_VCONFKEY_LOCK_TIMEZONE_ON_OFF, &ugd->is_on) is failed");
73
74         ret = vconf_get_int(CAL_VCONFKEY_WEEK_NMUBER_ON_OFF, &ugd->is_display_weeknumber);
75         c_warn_if(ret, "vconf_get_int(CAL_VCONFKEY_WEEK_NMUBER_ON_OFF, &ugd->is_display_weeknumber) is failed");
76
77         ret = vconf_get_int(CAL_VCONFKEY_COMPLETE_TODO_ON_OFF, &ugd->is_complete_todo);
78         c_warn_if(ret, "vconf_get_int(CAL_VCONFKEY_COMPLETE_TODO_ON_OFF, &ugd->is_complete_todo) is failed");
79
80         if (0 != vconf_get_int(CAL_VCONFKEY_FIRST_DAY_OF_WEEK,&ugd->first_day_of_week)) {
81                 ret = vconf_set_int(CAL_VCONFKEY_FIRST_DAY_OF_WEEK, FIRST_DAY_MONDAY);
82                 c_warn_if(ret != 0,"vconf_set_int(CAL_VCONFKEY_FIRST_DAY_OF_WEEK, 1) is failed(%d)", ret);
83         }
84
85 }
86
87 static Evas_Object *__cal_setting_ug_create_layout(Evas_Object *parent, struct ug_data *ugd)
88 {
89         Evas_Object *bt_back = NULL;
90         Elm_Object_Item* navi_item = NULL;
91
92         ugd->base = cal_util_add_layout(parent, NULL);
93         CAL_ASSERT(ugd->base);
94
95         ugd->bg = cal_util_add_bg(ugd->base, EINA_FALSE);
96         CAL_ASSERT(ugd->bg);
97
98         ugd->naviframe = elm_naviframe_add(ugd->base);
99         CAL_ASSERT(ugd->naviframe);
100
101         ugd->layout = cal_setting_ug_create_view(ugd->naviframe, ugd);
102         CAL_ASSERT(ugd->layout);
103
104         navi_item = elm_naviframe_item_push(ugd->naviframe, S_("IDS_COM_BODY_CALENDAR"), NULL, NULL, ugd->layout, NULL);
105
106         bt_back = elm_object_item_part_content_get(navi_item, "prev_btn");
107         if (!bt_back)
108         {
109                 bt_back = elm_button_add(ugd->naviframe);
110                 elm_object_item_part_content_set(navi_item, "prev_btn", bt_back);
111         }
112         elm_object_style_set(bt_back, "naviframe/back_btn/default");
113         evas_object_smart_callback_add(bt_back, "clicked", __cal_setting_ug_back_button_callback, ugd);
114
115         elm_object_part_content_set(ugd->base, "elm.swallow.content", ugd->naviframe);
116         elm_object_part_content_set(ugd->base, "elm.swallow.bg", ugd->bg);
117
118         return ugd->base;
119 }
120
121 static void *__cal_setting_ug_create_callback(ui_gadget_h ug, enum ug_mode mode, service_h data, void *priv)
122 {
123         Evas_Object *parent;
124         struct ug_data *ugd;
125         Evas_Object *base;
126
127         if (!ug || !priv)
128                 return NULL;
129
130         cal_util_connect_pattern_generator();
131
132         ugd = priv;
133         ugd->ug = ug;
134         __cal_setting_ug_initialize(ugd);
135         if (mode != UG_MODE_FULLVIEW)
136                 return NULL;
137         parent = ug_get_parent_layout(ug);
138         if (!parent)
139                 return NULL;
140
141         base = __cal_setting_ug_create_layout(parent, ugd);
142         if (!base) {
143                 calendar_error_e error = CALENDAR_ERROR_NONE;
144
145                 error = calendar_disconnect();
146                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_disconnect() is failed(%x)", error);
147                 return NULL;
148         }
149         return base;
150 }
151
152 static void __cal_setting_ug_destroy_callback(ui_gadget_h ug, service_h data, void *priv)
153 {
154         struct ug_data *ugd;
155
156         if (!ug || !priv)
157                 return;
158
159         ugd = priv;
160
161         calendar_error_e error = CALENDAR_ERROR_NONE;
162
163         error = calendar_disconnect();
164         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_disconnect() is failed(%x)", error);
165
166         cal_util_delete_evas_object(&ugd->base);
167
168         if (ugd->idler) {
169                 ecore_idler_del(ugd->idler);
170                 ugd->idler = NULL;
171         }
172
173         _calendar_free_record_list(&ugd->my_phone_calendar_list);
174
175         _calendar_free_record_list(&ugd->checked_calendar_list);
176 }
177
178 static void __cal_setting_ug_key_callback(ui_gadget_h ug, enum ug_key_event evt,
179                 service_h data, void *priv)
180 {
181         if (!ug)
182                 return;
183
184         DBG("UG: key event %d", evt);
185
186         switch (evt) {
187         case UG_KEY_EVENT_END:
188                 ug_destroy_me(ug);
189                 break;
190         default:
191                 break;
192         }
193 }
194
195 static int * __cal_setting_get_arrary_from_list(Eina_List *list)
196 {
197         c_retv_if(!list, NULL);
198
199         int *array = NULL;
200
201         array = calloc(eina_list_count(list), sizeof(int));
202
203         Eina_List *cursor = NULL;
204
205         int index = 0;
206
207         void *data = 0;
208
209         EINA_LIST_FOREACH(list, cursor, data) {
210                 *(array + index++) = (int)data;
211         }
212
213         return array;
214 }
215
216 API int setting_plugin_reset(service_h data, void *priv)
217 {
218         CAL_FN_START;
219
220         int ret;
221
222         calendar_error_e error = CALENDAR_ERROR_NONE;
223
224         error = calendar_connect();
225         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_connect() is failed(%x)", error);
226
227         _calendar_init_hash();
228
229         ret = vconf_set_int(CAL_VCONFKEY_FIRST_DAY_OF_WEEK, FIRST_DAY_MONDAY);
230         c_warn_if(ret != 0, "vconf_set_int(CAL_VCONFKEY_FIRST_DAY_OF_WEEK, 1) is failed(%d)", ret);
231
232         ret = vconf_set_int(CAL_VCONFKEY_FOCUS_VIEW, 0);
233         c_warn_if(ret != 0, "vconf_set_int(CAL_VCONFKEY_FOCUS_VIEW, 0) is failed(%d)", ret);
234
235         ret = vconf_set_int(CAL_VCONFKEY_LOCK_TIMEZONE_ON_OFF, 0);
236         c_warn_if(ret != 0, "vconf_set_int(CAL_VCONFKEY_LOCK_TIMEZONE_ON_OFF, 0) is failed(%d)", ret);
237
238         ret = vconf_set_int(CAL_VCONFKEY_WEEK_NMUBER_ON_OFF, 0);
239         c_warn_if(ret != 0, "vconf_set_int(CAL_VCONFKEY_WEEK_NMUBER_ON_OFF, 0) is failed(%d)", ret);
240
241         ret = vconf_set_int(CAL_VCONFKEY_COMPLETE_TODO_ON_OFF, 1);
242         c_warn_if(ret != 0, "vconf_set_int(CAL_VCONFKEY_COMPLETE_TODO_ON_OFF, 1) is failed(%d)", ret);
243
244         ret = vconf_set_str(CAL_VCONFKEY_LOCK_TIMEZONE_PATH, "Asia/Seoul");
245         c_warn_if(ret != 0, "vconf_set_int(CAL_VCONFKEY_LOCK_TIMEZONE_PATH, 'Seoul') is failed(%d)", ret);
246
247         ret = vconf_set_str(CAL_VCONFKEY_LOCK_TIMEZONE_CITY, "IDS_WCL_BODY_CITYNAME_SEOUL");
248         c_warn_if(ret != 0, "vconf_set_str(CAL_VCONFKEY_LOCK_TIMEZONE_CITY, tzpath) is failed");
249
250         ret = vconf_set_str(CAL_VCONFKEY_LOCK_TIMEZONE_OFFSET, "+9");
251         c_warn_if(ret != 0, "vconf_set_str(CAL_VCONFKEY_LOCK_TIMEZONE_OFFSET, tzpath) is failed");
252
253         calendar_list_h list = NULL;
254
255         error = calendar_db_get_all_records(_calendar_book._uri, 0, 0, &list);
256         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_db_get_all_records() is failed(%x)", error);
257
258         int count = 0;
259
260         error = calendar_list_get_count(list, &count);
261         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_list_get_count() is failed(%x)", error);
262
263         int i = 0;
264
265         calendar_record_h record = NULL;
266
267         int index = 0;
268
269         Eina_List *index_list = NULL;
270
271         for (i = 0; i < count; i++) {
272
273                 error = calendar_list_get_current_record_p(list, &record);
274                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_list_get_current_record_p() is failed(%x)", error);
275
276                 error = calendar_record_get_int(record, _calendar_book.id, &index);
277                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_get_int() is failed(%x)", error);
278
279                 if (index != DEFAULT_EVENT_CALENDAR_BOOK_ID && index != DEFAULT_TODO_CALENDAR_BOOK_ID)
280                         index_list = eina_list_append(index_list, (void *)index);
281         }
282
283         error = calendar_list_destroy(list, true);
284         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_list_destroy() is failed(%x)", error);
285
286         int *index_array = __cal_setting_get_arrary_from_list(index_list);
287
288         error = calendar_db_delete_records(_calendar_book._uri, index_array, eina_list_count(index_list));
289         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_db_delete_records() is failed(%x)", error);
290
291         CAL_FREE(index_array);
292
293         eina_list_free(index_list);
294
295         error = calendar_db_get_all_records(_calendar_event._uri, 0, 0, &list);
296         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_db_get_all_records() is failed(%x)", error);
297
298         count = 0;
299
300         error = calendar_list_get_count(list, &count);
301         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_list_get_count() is failed(%x)", error);
302
303         i = 0;
304
305         record = NULL;
306
307         index = 0;
308
309         index_list = NULL;
310
311         for (i = 0; i < count; i++) {
312
313                 error = calendar_list_get_current_record_p(list, &record);
314                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_list_get_current_record_p() is failed(%x)", error);
315
316                 error = calendar_record_get_int(record, _calendar_event.id, &index);
317                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_get_int() is failed(%x)", error);
318
319                 index_list = eina_list_append(index_list, (void *)index);
320         }
321
322         error = calendar_list_destroy(list, true);
323         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_list_destroy() is failed(%x)", error);
324
325         index_array = __cal_setting_get_arrary_from_list(index_list);
326
327         error = calendar_db_delete_records(_calendar_event._uri, index_array, eina_list_count(index_list));
328         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_db_delete_records() is failed(%x)", error);
329
330         CAL_FREE(index_array);
331
332         eina_list_free(index_list);
333
334         error = calendar_db_get_all_records(_calendar_todo._uri, 0, 0, &list);
335         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_db_get_all_records() is failed(%x)", error);
336
337         count = 0;
338
339         error = calendar_list_get_count(list, &count);
340         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_list_get_count() is failed(%x)", error);
341
342         i = 0;
343
344         record = NULL;
345
346         index = 0;
347
348         index_list = NULL;
349
350         for (i = 0; i < count; i++) {
351
352                 error = calendar_list_get_current_record_p(list, &record);
353                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_list_get_current_record_p() is failed(%x)", error);
354
355                 error = calendar_record_get_int(record, _calendar_todo.id, &index);
356                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_get_int() is failed(%x)", error);
357
358                 index_list = eina_list_append(index_list, (void *)index);
359         }
360
361         error = calendar_list_destroy(list, true);
362         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_list_destroy() is failed(%x)", error);
363
364         index_array = __cal_setting_get_arrary_from_list(index_list);
365
366         error = calendar_db_delete_records(_calendar_todo._uri, index_array, eina_list_count(index_list));
367         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_db_delete_records() is failed(%x)", error);
368
369         CAL_FREE(index_array);
370
371         eina_list_free(index_list);
372
373         error = calendar_disconnect();
374         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_disconnect() is failed(%x)", error);
375
376         return 0;
377 }
378
379 API int UG_MODULE_INIT(struct ug_module_ops *ops)
380 {
381         struct ug_data *ugd;
382
383         if (!ops)
384                 return -1;
385
386         CAL_CALLOC(ugd, 1, struct ug_data);
387
388         ops->create = __cal_setting_ug_create_callback;
389         ops->destroy = __cal_setting_ug_destroy_callback;
390         ops->key_event = __cal_setting_ug_key_callback;
391         ops->priv = ugd;
392         ops->opt = UG_OPT_INDICATOR_ENABLE;
393
394         const char *path = bindtextdomain(CALENDAR, LOCALEDIR);
395         c_warn_if(!path, "bindtextdomain(%s, %s) is failed.", CALENDAR, LOCALEDIR);
396
397         return 0;
398 }
399
400 API void UG_MODULE_EXIT(struct ug_module_ops *ops)
401 {
402         struct ug_data *ugd;
403
404         cal_util_disconnect_pattern_generator();
405
406         if (!ops)
407                 return;
408
409         ugd = ops->priv;
410         free(ugd);
411 }