Update codes for TIZEN 2.0
[apps/home/memo.git] / src / memo.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 <stdio.h>
20 #include <app.h>
21 #include <appcore-efl.h>
22 #include <ui-gadget.h>        /* ug_send_key_event */
23 #include <Ecore_X.h>
24 #include <heynoti.h>
25 #include <vconf.h>
26 #include <memo.h>
27 #include <memo_ug.h>
28 #include "memo_log.h"
29
30 static int heynoti_fd = -1;
31 Eina_Bool launch_by_new = EINA_FALSE; /*check wether clicked  new icon*/
32 Eina_Bool is_previous_edit_view = EINA_FALSE;/*check wether previous view is edit view*/
33 Eina_Bool normal_memo_launch = EINA_FALSE;
34
35
36 static void on_time_format_update(appdata *ad)
37 {
38     MEMO_FUN_BEG();
39     Elm_Object_Item *top_it = elm_naviframe_top_item_get(ad->navigator);
40     Evas_Object *eo = elm_object_item_content_get(top_it);
41     if(eo != NULL) {
42         Evas_Smart_Cb cb = evas_object_data_get(eo, "time_format_update_cb");
43         RETIF(cb == NULL);
44         void *cb_data = evas_object_data_get(eo, "time_format_update_data");
45         cb(cb_data, NULL, NULL);
46     }
47     MEMO_FUN_END();
48 }
49
50 void memo_timezone_changed_cb(void *data)
51 {
52     MEMO_FUN_BEG();
53     appdata *ad = data;
54     /* update ICU */
55     icu_init();
56     /* update top view */
57     on_time_format_update(ad);
58     MEMO_FUN_END();
59 }
60
61 int tzone_heynoti_init(void *data)
62 {
63     MEMO_FUN_BEG();
64     int fd = -1;
65     if((fd = heynoti_init()) < 0) {
66         return -1;
67     }
68     if(heynoti_subscribe(fd, "setting_time_changed", memo_timezone_changed_cb, data)) {
69         return -1;
70     }
71     if(heynoti_attach_handler(fd)) {
72         return -1;
73     }
74     LOGD("tzone heynoti register success!\n");
75     MEMO_FUN_BEG();
76     return fd;
77 }
78
79 void tzone_heynoti_fini()
80 {
81     MEMO_FUN_BEG();
82     int fd = heynoti_fd;
83     if (fd < 0) {
84         return;
85     }
86
87     heynoti_unsubscribe(fd, "setting_time_changed", memo_timezone_changed_cb);
88     heynoti_close(fd);
89     MEMO_FUN_END();
90 }
91
92 static void win_del(void *data, Evas_Object *obj, void *event)
93 {
94         MEMO_FUN_BEG();
95         if(normal_memo_launch)  {
96                 normal_memo_launch = EINA_FALSE;
97         }
98         elm_exit();
99         MEMO_FUN_END();
100 }
101
102 static void app_lang(void *data)
103 {
104     //Take necessary actions when system language setting changes
105 }
106
107 static void app_region(void *data)
108 {
109     MEMO_FUN_BEG();
110     //Take necessary actions when system region settings changes
111     appdata *ad = data;
112     /* time format 12/24 chanaged, update ICU */
113     icu_init();
114     /* update top view */
115     on_time_format_update(ad);
116     MEMO_FUN_END();
117 }
118
119 static Evas_Object *create_win(const char *name)
120 {
121     MEMO_FUN_BEG();
122     Evas_Object *eo;
123     int w, h;
124
125     eo = elm_win_add(NULL, name, ELM_WIN_BASIC);
126     if (eo) {
127         elm_win_title_set(eo, name);
128         elm_win_borderless_set(eo, EINA_TRUE);
129         evas_object_smart_callback_add(eo, "delete,request", win_del, NULL);
130         ecore_x_window_size_get(ecore_x_window_root_first_get(), &w, &h);
131         evas_object_resize(eo, w, h);
132     }
133
134     MEMO_FUN_BEG();
135     return eo;
136 }
137
138 static void _on_reload_cb(void *data, Evas_Object *obj, void *event_info)
139 {
140         MEMO_FUN_BEG();
141         evas_object_del((Evas_Object *)data);
142         if(normal_memo_launch)  {
143                 normal_memo_launch = EINA_FALSE;
144         }
145         elm_exit();
146         MEMO_FUN_END();
147 }
148
149 void on_change(keynode_t *node, void *user_data)
150 {
151     MEMO_FUN_BEG();
152     appdata *ad = (appdata *)user_data;
153     int sync = vconf_keynode_get_int(node);
154     if (sync == 1) {
155         if (ad->popup == NULL) {
156             Evas_Object *popup = elm_popup_add(ad->win_main);
157             evas_object_size_hint_weight_set(popup, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
158             elm_object_text_set(popup, "Memo data changed unexpectedly, close application and reload!");
159             Evas_Object *btn1 = elm_button_add(popup);
160             elm_object_text_set(btn1, MEMO_I18N_YES);
161             elm_object_part_content_set(popup, "button1", btn1);
162             evas_object_smart_callback_add(btn1, "clicked", _on_reload_cb, popup);
163             evas_object_show(popup);
164             ad->popup = popup;
165         }
166     }
167     MEMO_FUN_END();
168 }
169
170 static int app_create_delay(void *data)
171 {
172     MEMO_FUN_BEG();
173     appdata *ad = (appdata *)data;
174     Evas_Object *win;
175     int r;
176
177         /* create window */
178         win = create_win(PACKAGE);
179         if (win == NULL) {
180                 return -1;
181         }
182         normal_memo_launch  = EINA_TRUE;
183         ad->win_main = win;
184
185     /* monitor update of memo database from external application */
186     int sync = 0;
187     if (vconf_get_int("memory/mobex_engine/memo_sync_status", &sync) == 0) {
188         if (sync == 1) { /* KIES syncing */
189             Evas_Object *popup = elm_popup_add(ad->win_main);
190             evas_object_size_hint_weight_set(popup, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
191             elm_object_text_set(popup, "Memo data changed unexpectedly, close application and reload!");
192             Evas_Object *btn1 = elm_button_add(popup);
193             elm_object_text_set(btn1, MEMO_I18N_YES);
194             elm_object_part_content_set(popup, "button1", btn1);
195             evas_object_smart_callback_add(btn1, "clicked", _on_reload_cb, popup);
196             evas_object_show(popup);
197             ad->popup = popup;
198         }
199     }
200     vconf_notify_key_changed("memory/mobex_engine/memo_sync_status", on_change, ad);
201
202     /* theme extension */
203     elm_theme_extension_add(NULL, EDJ_FILE);
204
205     /* show indicator */
206     elm_win_indicator_mode_set(win, ELM_WIN_INDICATOR_SHOW);
207
208     /* init ui gadget */
209     memo_ug_init(ad->win_main);
210
211     /* bg */
212     Evas_Object *bg = elm_bg_add(win);
213     elm_bg_color_set(bg, 0, 0, 0); /* black */
214     evas_object_size_hint_weight_set(bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
215     elm_win_resize_object_add(win, bg);
216     evas_object_show(bg);
217     /* window */
218     ad->ly_main = elm_layout_add(win);
219     elm_layout_theme_set(ad->ly_main, "layout", "application", "default");
220     evas_object_size_hint_weight_set(ad->ly_main, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
221     elm_win_resize_object_add(ad->win_main, ad->ly_main);
222     evas_object_show(ad->ly_main);
223
224     /* navigationbar */
225     ad->navigator = elm_naviframe_add(ad->ly_main);
226     elm_object_part_content_set(ad->ly_main, "elm.swallow.content", ad->navigator);
227
228     if (ad->init_view == MEMO_LIST_VIEW) {
229         memo_load_list_view(ad);
230     } else if (ad->init_view == MEMO_DETAIL_VIEW) {
231         memo_load_detail_view(ad);
232     } else if (ad->init_view == MEMO_EDIT_VIEW) {
233         memo_load_edit_view(ad);
234     }
235
236     /* init internationalization */
237     r = appcore_set_i18n(PACKAGE, LOCALEDIR);
238     if (r) {
239         MEMO_FUN_END();
240         return -1;
241     }
242     app_lang(ad);
243
244     /* timezone inoti */
245     heynoti_fd = tzone_heynoti_init(ad);
246
247     MEMO_FUN_END();
248     return 0;
249 }
250
251 static bool app_create(void *data)
252 {
253     MEMO_FUN_BEG();
254     /* init memo database */
255     memo_init(NULL);
256     MEMO_FUN_END();
257     return true;
258 }
259
260 static void app_terminate(void *data)
261 {
262     MEMO_FUN_BEG();
263     appdata *ad = (appdata *)data;
264     /* fini memo database */
265     memo_fini();
266
267     tzone_heynoti_fini();
268     if (ad->win_main) {
269         evas_object_del(ad->win_main);
270     }
271     MEMO_FUN_END();
272 }
273
274 static void app_pause(void *data)
275 {
276     // Take necessary actions when application becomes invisible.
277 }
278
279 static void app_resume(void *data)
280 {
281     // Take necessary actions when application becomes visible.
282 }
283
284 static void app_service(service_h service, void *data)
285 {
286         MEMO_FUN_BEG();
287         appdata *ad = (appdata *)data;
288         char *value = NULL;
289         int lanuch_by_normal = EINA_FALSE;
290         /* launch memo */
291         ad->init_view = MEMO_LIST_VIEW;
292         ad->index = -1;
293
294         int r = service_get_operation(service, &value);
295         if (value) {
296                 if (!strcmp(value, SERVICE_OPERATION_DEFAULT)) {
297                         free(value);
298                         r= service_get_extra_data(service, "caller", &value);
299                         if(value && !strcmp(value,"New")){
300                                 ad->init_view = MEMO_EDIT_VIEW;
301                                 ad->index = -1;
302                                 free(value);
303                                 value = NULL;
304                                 launch_by_new = TRUE;
305                         } else if ((value && !strcmp(value,"Normal"))  || (value && !strcmp(value,"Search"))) {
306                                 ad->init_view = MEMO_LIST_VIEW;
307                                 ad->index = -1;
308                                 free(value);
309                                 value = NULL;
310                                 lanuch_by_normal = EINA_TRUE;
311                         }
312                 }
313         }
314         if (ad->win_main != NULL) { /* memo has already launced. */
315                 elm_win_activate(ad->win_main);
316                 if (is_previous_edit_view && !lanuch_by_normal && launch_by_new) { /* previous view is edit view  and click the new icon go the create view again*/
317                         return ;
318                 }
319                 if (is_previous_edit_view && lanuch_by_normal && launch_by_new) { /*first click new ,then back and click normal memo */
320                         is_previous_edit_view = EINA_FALSE;
321                         launch_by_new = EINA_FALSE;
322                         memo_load_list_view(ad);
323                         return ;
324
325                 }
326                 if (ad->init_view == MEMO_EDIT_VIEW) {
327                         memo_load_edit_view(ad);
328                         return ;
329                 }
330                 if( normal_memo_launch ) { /* normal click memo icon in livebox*/
331                         return ;
332                 }
333         }
334
335         app_create_delay(ad);
336         evas_object_show(ad->win_main);
337
338         MEMO_FUN_END();
339         return;
340 }
341
342 /**
343  * @describe
344  *  The entry of the program
345  *
346  * @param    argc
347  * @param    argv
348  * @param    int
349  * @exception
350  */
351 int main(int argc, char *argv[])
352 {
353     MEMO_FUN_BEG();
354     appdata ad;
355
356     app_event_callback_s event_callback;
357
358     event_callback.create = app_create;
359     event_callback.terminate = app_terminate;
360     event_callback.pause = app_pause;
361     event_callback.resume = app_resume;
362     event_callback.service = app_service;
363     event_callback.low_memory = NULL;
364     event_callback.low_battery = NULL;
365     event_callback.device_orientation = NULL;
366     event_callback.language_changed = app_lang;
367     event_callback.region_format_changed = app_region;
368
369     memset(&ad, 0x0, sizeof(appdata));
370
371     MEMO_FUN_END();
372     return app_efl_main(&argc, &argv, &event_callback, &ad);
373 }