Update codes for TIZEN 2.0
[apps/home/memo.git] / src / memo_ug.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 #include <Ecore_X.h>
19 #include <aul.h>
20 #include <appsvc.h>
21 #include "memo.h"
22 #include <supplement.h>
23 #include "memo_ug.h"
24 #include "memo_string.h"
25 #include <vconf.h>
26
27 static void _on_response_cb(void *data, Evas_Object *obj, void *event_info);
28
29 static GString *get_shared_text(Eina_List *list)
30 {
31     Eina_List *l = NULL;
32     struct memo_data *md = NULL;
33     GString *buf = g_string_new("");
34
35     EINA_LIST_FOREACH(list, l, md) {
36         if ((md != NULL) && !(md->has_doodle)) {
37             g_string_append_printf(buf, "%s", md->content);
38         }
39     }
40
41     return buf;
42 }
43
44 static GString *get_shared_attachment(Eina_List *list)
45 {
46     Eina_List *l = NULL;
47     struct memo_data *md = NULL;
48     GString *buf = g_string_new("");
49
50     EINA_LIST_FOREACH(list, l, md) {
51         if ((md != NULL) && (md->has_doodle)) {
52             g_string_append_printf(buf, DOODLEDIR "/%d.png\n", (int)md->id);
53         }
54     }
55
56     return buf;
57 }
58
59 static void _ug_layout_cb(ui_gadget_h ug, enum ug_mode mode, void *priv)
60 {
61     Evas_Object *base, *win;
62     base = (Evas_Object *)ug_get_layout(ug);
63     if (base == NULL) {
64         return;
65     }
66
67     win = ug_get_window();
68     switch (mode) {
69     case UG_MODE_FULLVIEW:
70         evas_object_size_hint_weight_set(base, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
71         elm_win_resize_object_add(win, base);
72         /* disable effect */
73         //ug_disable_effect(ug);
74         evas_object_show(base);
75         break;
76     default:
77         break;
78     }
79 }
80
81 static void _ug_result_cb(ui_gadget_h ug, service_h service, void *priv)
82 {
83     service_dump(service);
84 }
85
86 static void _ug_destroy_cb(ui_gadget_h ug, void *priv)
87 {
88     if (ug != NULL) {
89         ug_destroy(ug);
90     }
91 }
92
93 /**
94  * ug_launch_common
95  *
96  * @description
97  *  This is a basic function which designed to invoke any ui gadget.
98  *
99  * @param[in]   service         a service type which will send to ug directly
100  * @param[in]   ug_name    UG name marco defined in memo_ug.h
101  * @return      void
102  */
103 void ug_launch_common(service_h service, char *ug_name)
104 {
105         ug_cbs_t cbs={0, };
106
107         cbs.layout_cb = _ug_layout_cb;
108         cbs.destroy_cb = _ug_destroy_cb;
109         cbs.result_cb = _ug_result_cb;
110         cbs.priv = NULL;
111
112         service_dump(service);
113         ug_create(NULL, ug_name, UG_MODE_FULLVIEW, service, &cbs);
114         service_destroy(service);
115 }
116
117 /**
118  * ug_launch_common_var
119  *
120  * @description
121  *  This function supply a variadic version of ug_launch_common_var.
122  *  The first and only required variable is the ug_name.
123  *  Its type is string, but the caller should use the macro name which defined in memo_ug.h
124  *  The other variables are optional, it should appear for key-value pair
125  *  The end of optional variables must be NULL.
126  *
127  * @param[in]   ug_name    UG name marco defined in memo_ug.h
128  * @param[in]   ...        pair of key and value, end with NULL
129  * @return      void
130  */
131 void ug_launch_common_var(char *ug_name, ...)
132 {
133         char *key = NULL;
134         char *val = NULL;
135         service_h service = NULL;
136         service_create(&service);
137
138         va_list ap;
139         va_start(ap, ug_name);
140         while (1) {
141                 key = va_arg(ap, char *);
142                 val = va_arg(ap, char *);
143
144                 if (key == NULL || val == NULL) {
145                         break;
146                 }
147                 service_add_extra_data(service, key, val);
148         }
149         va_end(ap);
150
151         ug_launch_common(service, ug_name);
152 }
153
154 void ug_launch_message(Eina_List *list)
155 {
156         GString *text = get_shared_text(list);
157         GString *attachment = get_shared_attachment(list);
158         service_h service = NULL;
159         service_create(&service);
160         service_add_extra_data(service, "BODY", text->str);
161         service_add_extra_data(service, "ATTACHFILE", attachment->len == 0 ? NULL : attachment->str);
162
163         ug_launch_common(service, UG_NAME_MESSAGE);
164
165         g_string_free(text, TRUE);
166         g_string_free(attachment, TRUE);
167 }
168
169 void ug_launch_email(Eina_List *list)
170 {
171         GString *text = get_shared_text(list);
172         GString *attachment = get_shared_attachment(list);
173
174         service_h service = NULL;
175         service_create(&service);
176         service_add_extra_data(service, "RUN_TYPE", "5");
177         service_add_extra_data(service, "TO", "");
178         service_add_extra_data(service, "CC", "");
179         service_add_extra_data(service, "BCC", NULL);
180         service_add_extra_data(service, "SUBJECT", "");
181         service_add_extra_data(service, "BODY", text->str);
182         service_add_extra_data(service, "ATTACHMENT", attachment->len == 0 ? NULL : attachment->str);
183
184         ug_launch_common(service, UG_NAME_EMAIL);
185
186         g_string_free(text, TRUE);
187         g_string_free(attachment, TRUE);
188 }
189
190 void ug_launch_calender(Eina_List *list)
191 {
192         GString *text = get_shared_text(list);
193
194         service_h service = NULL;
195         service_create(&service);
196         service_add_extra_data(service, "index", "0");
197         service_add_extra_data(service, "note", text->str);
198
199         ug_launch_common(service, UG_NAME_CALENDAR);
200
201         g_string_free(text, TRUE);
202 }
203
204 void ug_launch_facebook(Eina_List *list)
205 {
206         GString *text = get_shared_text(list);
207         service_h service = NULL;
208         service_create(&service);
209         service_add_extra_data(service, "feature", "status_post");
210         service_add_extra_data(service, "text", text->str);
211         ug_launch_common(service, "facebook-efl");
212         g_string_free(text, TRUE);
213 }
214
215 void ug_launch_nfc(Eina_List *list)
216 {
217         GString *text = get_shared_text(list);
218         service_h service = NULL;
219         service_create(&service);
220         service_add_extra_data(service, "count", "1");  /* only one buffer suppored */
221         service_add_extra_data(service, "request_type", "data_buffer");
222         service_add_extra_data(service, "request_data", text->str);
223         ug_launch_common(service, UG_NAME_NFC);
224         g_string_free(text, TRUE);
225 }
226
227 void memo_ug_init(Evas_Object *win)
228 {
229     enum ug_option opt = UG_OPT_INDICATOR_ENABLE;
230     UG_INIT_EFL(win, opt);
231 }
232
233 /* BEGIN SHARE POPUP */
234
235 typedef struct _share_popup_data {
236     Evas_Object *popup;
237     Eina_List *list; /* records of memo */
238 }share_popup_data;
239
240 static void _share_messge_selected_cb(void *data, Evas_Object *obj, void *event_info)
241 {
242     share_popup_data *spd = (share_popup_data *)data;
243     ug_launch_message(spd->list);
244     _on_response_cb(spd, spd->popup, NULL);
245 }
246
247 static void _share_email_selected_cb(void *data, Evas_Object *obj, void *event_info)
248 {
249     share_popup_data *spd = (share_popup_data *)data;
250     ug_launch_email(spd->list);
251     _on_response_cb(spd, spd->popup, NULL);
252 }
253
254 static void _share_calender_selected_cb(void *data, Evas_Object *obj, void *event_info)
255 {
256     share_popup_data *spd = (share_popup_data *)data;
257     ug_launch_calender(spd->list);
258     _on_response_cb(spd, spd->popup, NULL);
259 }
260
261 static void _share_facebook_selected_cb(void *data, Evas_Object *obj, void *event_info)
262 {
263     share_popup_data *spd = (share_popup_data *)data;
264     ug_launch_facebook(spd->list);
265     _on_response_cb(spd, spd->popup, NULL);
266 }
267
268 static void _share_nfc_selected_cb(void *data, Evas_Object *obj, void *event_info)
269 {
270     share_popup_data *spd = (share_popup_data *)data;
271     ug_launch_nfc(spd->list);
272     _on_response_cb(spd, spd->popup, NULL);
273 }
274
275 static void _on_response_cb(void *data, Evas_Object *obj, void *event_info)
276 {
277     share_popup_data *spd = (share_popup_data *)data;
278     if (spd->popup != NULL) {
279         evas_object_del(spd->popup);
280         spd->popup = NULL;
281     }
282     if (spd->list != NULL) {
283         memo_data_t *md = NULL;
284         EINA_LIST_FREE(spd->list, md) {
285             memo_free_data(md);
286         }
287     }
288     SFREE(spd);
289 }
290
291 static char *_gl_label_get(void *data, Evas_Object *obj, const char *part)
292 {
293     char *label = (char *)data;
294     if (strcmp(part, "elm.text") == 0) {
295         return strdup(label);
296     }
297     return NULL;
298 }
299
300 void memo_share(Evas_Object *parent, int *indexes, int n)
301 {
302     share_popup_data *spd = SMALLOC(share_popup_data);
303     RETIF(spd == NULL);
304
305     int i = 0;
306     int c = 0;
307     int nfc_enable = -1;
308     Evas_Object *box = NULL;
309     Eina_Bool has_doodle = EINA_FALSE;
310     memo_data_t *md = NULL;
311     for (i=0; i<n ; i++) { /* retrieve records */
312         md = memo_get_data(indexes[i]);
313         if (md->has_doodle) {
314             has_doodle = EINA_TRUE;
315         }
316         spd->list = eina_list_append(spd->list, md);
317     }
318
319     static Elm_Genlist_Item_Class itc;
320     memset(&itc, 0, sizeof(Elm_Genlist_Item_Class));
321     itc.item_style = "1text";
322     itc.func.text_get = _gl_label_get;
323
324     Evas_Object *popup = elm_popup_add(parent);
325     spd->popup = popup;
326     elm_object_style_set(popup, "min_menustyle");
327     elm_object_part_text_set(popup, "title,text", MEMO_I18N_SHARE);
328     Evas_Object *btn1 = elm_button_add(popup);
329     elm_object_text_set(btn1, MEMO_I18N_CLOSE);
330     elm_object_part_content_set(popup, "button1", btn1);
331     evas_object_smart_callback_add(btn1, "clicked", _on_response_cb, spd);
332     evas_object_size_hint_weight_set(popup, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
333
334     Evas_Object *genlist = elm_genlist_add(popup);
335     evas_object_size_hint_weight_set(genlist, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
336     evas_object_size_hint_align_set(genlist, EVAS_HINT_FILL, EVAS_HINT_FILL);
337
338     /* message */
339     elm_genlist_item_append(genlist, &itc, MEMO_I18N_MESSAGE,
340             NULL, ELM_GENLIST_ITEM_NONE, _share_messge_selected_cb, spd);
341     c++;
342     /* email */
343     if (n == 1) {
344         elm_genlist_item_append(genlist, &itc, MEMO_I18N_EMAIL,
345                 NULL, ELM_GENLIST_ITEM_NONE, _share_email_selected_cb, spd);
346         c++;
347     }
348     if (!has_doodle) {
349         /* calendar */
350         elm_genlist_item_append(genlist, &itc, MEMO_I18N_CALENDAR,
351                 NULL, ELM_GENLIST_ITEM_NONE, _share_calender_selected_cb, spd);
352         c++;
353         /* facebook */
354         elm_genlist_item_append(genlist, &itc, MEMO_I18N_FACEBOOK,
355             NULL, ELM_GENLIST_ITEM_NONE, _share_facebook_selected_cb, spd);
356         c++;
357         /* NFC */
358         if (!vconf_get_bool(VCONFKEY_NFC_FEATURE, &nfc_enable)) {
359             if(nfc_enable == VCONFKEY_NFC_FEATURE_ON) {
360                 elm_genlist_item_append(genlist, &itc, MEMO_I18N_NFC,
361                 NULL, ELM_GENLIST_ITEM_NONE, _share_nfc_selected_cb, spd);
362                 c++;
363             }
364         }
365     }
366     evas_object_show(genlist);
367     box = elm_box_add(popup);
368     evas_object_size_hint_min_set(box, 0, (c>3 ? 3 : c)*113*elm_config_scale_get());
369     elm_box_pack_end(box, genlist);
370     evas_object_show(box);
371     elm_object_content_set(popup, box);
372     evas_object_show(popup);
373 }
374