apply FSL(Flora Software License)
[apps/home/memo.git] / src / memo.c
1 /*
2   * Copyright 2012  Samsung Electronics Co., Ltd
3   * 
4   * Licensed under the Flora License, Version 1.0 (the "License");
5   * you may not use this file except in compliance with the License.
6   * You may obtain a copy of the License at
7   * 
8   *     http://www.tizenopensource.org/license
9   * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16
17 #include <stdio.h>
18 #include <appcore-efl.h>
19 #include <ui-gadget.h>        /* ug_send_key_event */
20 #include <Ecore_X.h>
21 #include <heynoti.h>
22 #include <vconf.h>
23 #include <memo.h>
24 #include <memo_ug.h>
25
26 static int heynoti_fd = -1;
27
28 static void on_time_format_update(appdata *ad)
29 {
30     Elm_Object_Item *top_it = elm_naviframe_top_item_get(ad->navigator);
31     Evas_Object *eo = elm_object_item_content_get(top_it);
32     if(eo != NULL) {
33         Evas_Smart_Cb cb = evas_object_data_get(eo, "time_format_update_cb");
34         RETIF(cb == NULL);
35         void *cb_data = evas_object_data_get(eo, "time_format_update_data");
36         cb(cb_data, NULL, NULL);
37     }
38 }
39
40 void memo_timezone_changed_cb(void *data)
41 {
42     appdata *ad = data;
43     /* update ICU */
44     icu_init();
45     /* update top view */
46     on_time_format_update(ad);
47 }
48
49 int tzone_heynoti_init(void *data)
50 {
51     int fd = -1;
52     if((fd = heynoti_init()) < 0) {
53         return -1;
54     }
55     if(heynoti_subscribe(fd, "setting_time_changed", memo_timezone_changed_cb, data)) {
56         return -1;
57     }
58     if(heynoti_attach_handler(fd)) {
59         return -1;
60     }
61     LOGD("tzone heynoti register success!\n");
62     return fd;
63 }
64
65 void tzone_heynoti_fini()
66 {
67     int fd = heynoti_fd;
68     if (fd < 0) {
69         return;
70     }
71
72     heynoti_unsubscribe(fd, "setting_time_changed", memo_timezone_changed_cb);
73     heynoti_close(fd);
74 }
75
76 static void win_del(void *data, Evas_Object *obj, void *event)
77 {
78     elm_exit();
79 }
80
81 static int lang_changed(void *data)
82 {
83     return 0;
84 }
85
86 static int region_changed(void *data)
87 {
88     appdata *ad = data;
89     /* time format 12/24 chanaged, update ICU */
90     icu_init();
91     /* update top view */
92     on_time_format_update(ad);
93     return 0;
94 }
95
96 static Evas_Object *create_win(const char *name)
97 {
98     Evas_Object *eo;
99     int w, h;
100
101     eo = elm_win_add(NULL, name, ELM_WIN_BASIC);
102     elm_win_conformant_set(eo, EINA_TRUE);
103     if (eo) {
104         elm_win_title_set(eo, name);
105         elm_win_borderless_set(eo, EINA_TRUE);
106         evas_object_smart_callback_add(eo, "delete,request", win_del, NULL);
107         ecore_x_window_size_get(ecore_x_window_root_first_get(), &w, &h);
108         evas_object_resize(eo, w, h);
109     }
110
111     return eo;
112 }
113
114 static void _on_reload_cb(void *data, Evas_Object *obj, void *event_info)
115 {
116     evas_object_del(obj);
117     elm_exit();
118 }
119
120 void on_change(keynode_t *node, void *user_data)
121 {
122     appdata *ad = (appdata *)user_data;
123     int sync = vconf_keynode_get_int(node);
124     if (sync == 1) {
125         if (ad->popup == NULL) {
126             Evas_Object *popup = elm_popup_add(ad->win_main);
127             evas_object_size_hint_weight_set(popup, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
128             elm_object_text_set(popup, "Memo data changed unexpectedly, close application and reload!");
129             Evas_Object *btn1 = elm_button_add(popup);
130             elm_object_text_set(btn1, MEMO_I18N_YES);
131             elm_object_part_content_set(popup, "button1", btn1);
132             evas_object_smart_callback_add(btn1, "clicked", _on_reload_cb, popup);
133             evas_object_show(popup);
134             ad->popup = popup;
135         }
136     }
137 }
138
139 static int app_create_delay(void *data)
140 {
141     appdata *ad = (appdata *)data;
142     Evas_Object *win;
143     int r;
144
145     /* create window */
146     win = create_win(PACKAGE);
147     if (win == NULL) {
148         return -1;
149     }
150     ad->win_main = win;
151
152     /* monitor update of memo database from external application */
153     int sync = 0;
154     if (vconf_get_int("memory/mobex_engine/memo_sync_status", &sync) == 0) {
155         if (sync == 1) { /* KIES syncing */
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     vconf_notify_key_changed("memory/mobex_engine/memo_sync_status", on_change, ad);
168
169     /* theme extension */
170     elm_theme_extension_add(NULL, EDJ_FILE);
171
172     /* show indicator */
173     elm_win_indicator_mode_set(win, ELM_WIN_INDICATOR_SHOW);
174
175     /* init ui gadget */
176     memo_ug_init(ad->win_main);
177
178     /* bg */
179     Evas_Object *bg = elm_bg_add(win);
180     elm_bg_color_set(bg, 0, 0, 0); /* black */
181     evas_object_size_hint_weight_set(bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
182     elm_win_resize_object_add(win, bg);
183     evas_object_show(bg);
184     /* window */
185     ad->ly_main = elm_layout_add(win);
186     elm_layout_theme_set(ad->ly_main, "layout", "application", "default");
187     evas_object_size_hint_weight_set(ad->ly_main, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
188     elm_win_resize_object_add(ad->win_main, ad->ly_main);
189     evas_object_show(ad->ly_main);
190
191     /* navigationbar */
192     ad->navigator = elm_naviframe_add(ad->ly_main);
193     elm_object_part_content_set(ad->ly_main, "elm.swallow.content", ad->navigator);
194
195     if (ad->init_view == MEMO_LIST_VIEW) {
196         memo_load_list_view(ad);
197     } else if (ad->init_view == MEMO_DETAIL_VIEW) {
198         memo_load_detail_view(ad);
199     } else if (ad->init_view == MEMO_EDIT_VIEW) {
200         memo_load_edit_view(ad);
201     }
202
203     /* init internationalization */
204     r = appcore_set_i18n(PACKAGE, LOCALEDIR);
205     if (r) {
206         return -1;
207     }
208     lang_changed(ad);
209
210     appcore_set_event_callback(APPCORE_EVENT_REGION_CHANGE, region_changed, ad);
211
212     /* timezone inoti */
213     heynoti_fd = tzone_heynoti_init(ad);
214
215     /* add system event callback */
216     appcore_set_event_callback(APPCORE_EVENT_LANG_CHANGE, lang_changed, ad);
217
218     return 0;
219 }
220
221 static int app_create(void *data)
222 {
223     /* init memo database */
224     memo_init(NULL);
225     return 0;
226 }
227
228 static int app_terminate(void *data)
229 {
230     appdata *ad = data;
231     /* fini memo database */
232     memo_fini();
233
234     tzone_heynoti_fini();
235     if (ad->win_main) {
236         evas_object_del(ad->win_main);
237     }
238
239     return 0;
240 }
241
242 static int app_pause(void *data)
243 {
244     return 0;
245 }
246
247 static int app_resume(void *data)
248 {
249     return 0;
250 }
251
252 static int app_reset(bundle *bd, void *data)
253 {
254     appdata *ad = (appdata *)data;
255
256     if (ad->win_main != NULL) { /* memo has already launced. */
257         elm_win_activate(ad->win_main);
258         return 0;
259     }
260
261     /* launch memo */
262     ad->init_view = MEMO_LIST_VIEW;
263     ad->index = -1;
264     if (bd != NULL) { /* launch type check */
265         /* detail view */
266         const char *s = bundle_get_val(bd, AUL_PARAM_ID);
267         if (s != NULL) {
268             int id = atoi(s);
269             memo_data_t *md = memo_get_data(id);
270             if (md != NULL) { /* launch detail view */
271                 ad->init_view = MEMO_DETAIL_VIEW;
272                 ad->index = id;
273                 memo_free_data(md);
274             }
275         }
276         /* voice memo, launch edit view with specified text */
277         s = bundle_get_val(bd, "memo");
278         if (s != NULL) {    /* launch edit text view */
279             ad->init_view = MEMO_EDIT_VIEW;
280             ad->index = -1;
281             ad->init_str = strdup(s);    /* ugh, edit text view will take over init_str, no need to free explicitly */
282         }
283     }
284     app_create_delay(ad);
285     evas_object_show(ad->win_main);
286
287     return 0;
288 }
289
290 int main(int argc, char *argv[])
291 {
292     appdata ad;
293     struct appcore_ops ops = {
294         .create = app_create,
295         .terminate = app_terminate,
296         .pause = app_pause,
297         .resume = app_resume,
298         .reset = app_reset,
299     };
300
301     memset(&ad, 0x0, sizeof(appdata));
302     ops.data = &ad;
303
304     return appcore_efl_main(PACKAGE, &argc, &argv, &ops);
305 }