magic number cleanup
[framework/uifw/cbhm.git] / src / clipdrawer.c
1 #include "common.h"
2 #include "cbhm_main.h"
3 #include "storage.h"
4 #include "xcnphandler.h"
5 #include "clipdrawer.h"
6
7 #define DELETE_ICON_PATH "/usr/share/icon/cbhm/05_delete.png"
8 #define IM      "/opt/media/Images and videos/My photo clips/"
9 static const char *g_images_path[] = {
10         IM"1_photo.jpg",
11 /*
12         IM"2_photo.jpg",
13         IM"3_photo.jpg",
14         IM"4_photo.jpg",
15         IM"5_photo.jpg",
16         IM"6_photo.jpg",
17 */
18 };
19 #define N_IMAGES (1)
20
21 #define GRID_ITEM_SIZE 100
22
23 static int g_clipdrawer_mode = 0;
24 // gic should live at gengrid callback functions
25 Elm_Gengrid_Item_Class gic;
26
27 typedef struct tag_gridimgitem
28 {
29         Elm_Gengrid_Item *item;
30         const char *path;
31         Evas_Object *delbtn;
32 } gridimgitem_t;
33
34 static int toggle_clipdrawer_mode()
35 {
36         g_clipdrawer_mode = !g_clipdrawer_mode;
37         return g_clipdrawer_mode;
38 }
39
40 static int get_clipdrawer_mode()
41 {
42         return g_clipdrawer_mode;
43 }
44
45 static void _list_longpress(void *data, Evas_Object *obj, void *event_info)
46 {
47         struct appdata *ad = data;
48
49         clipdrawer_change_mode(ad);
50 }
51
52 static void _list_click_paste(void *data, Evas_Object *obj, void *event_info)
53 {
54         struct appdata *ad = data;
55     Elm_List_Item *it = (Elm_List_Item *) elm_list_selected_item_get(obj);
56         if (it == NULL)
57                 return;
58
59     elm_list_item_selected_set(it, 0);
60
61         char *p = NULL;
62         int cplen;
63
64         char *cpdata = NULL;
65         cpdata = elm_list_item_label_get(it);
66         if (cpdata == NULL)
67                 return;
68         cplen = strlen(cpdata);
69         p = malloc(cplen + 1);
70         snprintf(p, cplen+1, "%s", cpdata);
71         elm_selection_set(1, obj, /*ELM_SEL_FORMAT_TEXT*/1, p);
72 //      elm_selection_set(1, obj, /*ELM_SEL_FORMAT_MARKUP*/2, p);
73
74 //      clipdrawer_lower_view(ad);
75 }
76
77 static void _list_del_response_cb(void *data, Evas_Object *obj, void *event_info)
78 {
79         Elm_List_Item *it = (Elm_List_Item *)data;
80         evas_object_del(obj);
81
82         if((int)event_info == ELM_POPUP_RESPONSE_OK)
83                 elm_list_item_del(it);
84 }
85
86 static void _list_click_delete(void *data, Evas_Object *obj, void *event_info)
87 {
88         struct appdata *ad = data;
89     Elm_List_Item *it = (Elm_List_Item *) elm_list_selected_item_get(obj);
90         if (it == NULL)
91                 return;
92
93     elm_list_item_selected_set(it, 0);
94
95         Evas_Object *popup = elm_popup_add(ad->win_main);
96         elm_popup_timeout_set(popup, 3);
97         evas_object_size_hint_weight_set(popup, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
98         elm_popup_desc_set(popup, "Are you sure delete this?");
99         elm_popup_buttons_add(popup, 2,
100                                                   "Yes", ELM_POPUP_RESPONSE_OK,
101                                                   "No", ELM_POPUP_RESPONSE_CANCEL,
102                                                   NULL);
103         evas_object_smart_callback_add(popup, "response", _list_del_response_cb, it);
104         evas_object_show(popup);
105
106 //      char *slabel = NULL;
107 //      slabel = elm_list_item_label_get(it);
108 }
109
110 int clipdrawer_update_contents(void *data)
111 {
112         struct appdata *ad = data;
113         int i, pos;
114
115         // if delete mode, then back to normal mode
116         if (get_clipdrawer_mode())
117                 clipdrawer_change_mode(ad);
118
119         elm_list_clear(ad->txtlist);
120         for (i = 0; i < HISTORY_QUEUE_MAX_TXT_ITEMS; i++)
121         {
122                 pos = get_current_history_position()+i;
123                 if (pos > HISTORY_QUEUE_MAX_TXT_ITEMS-1)
124                         pos = pos-HISTORY_QUEUE_MAX_TXT_ITEMS;
125                 if (get_item_contents_by_pos(pos) != NULL && strlen(get_item_contents_by_pos(pos)) > 0)
126                 {
127                         elm_list_item_append(ad->txtlist, get_item_contents_by_pos(pos), NULL, NULL, NULL, ad);
128                 }
129         }
130         elm_list_go(ad->txtlist);
131
132         /* FIXME : sometimes when list update, screen isn't updated */
133
134         return 0;
135 }
136
137 const char* clipdrawer_get_plain_string_from_escaped(char *escstr)
138 {
139         /* TODO : is it should be here? besides, remove dependency for entry */
140         /* NOTE : return string should be freed */
141         return elm_entry_markup_to_utf8(escstr);
142 }
143
144 Evas_Object* _grid_icon_get(const void *data, Evas_Object *obj, const char *part)
145 {
146         int delete_mode = get_clipdrawer_mode();
147         gridimgitem_t *ti = (gridimgitem_t *)data;
148         if (!strcmp(part, "elm.swallow.icon"))
149         {
150                 Evas_Object *icon = elm_icon_add(obj);
151                 elm_icon_file_set(icon, ti->path, NULL);
152                 evas_object_size_hint_aspect_set(icon, EVAS_ASPECT_CONTROL_VERTICAL, 1, 1);
153                 evas_object_show(icon);
154                 return icon;
155         }
156         else if (!strcmp(part, "elm.swallow.end") && delete_mode)
157         {
158                 ti->delbtn = elm_check_add(obj);
159                 elm_object_style_set(ti->delbtn, "extended/imagegrid");
160                 elm_check_state_set(ti->delbtn, 1);
161                 evas_object_show(ti->delbtn);
162                 return ti->delbtn;
163         }
164            
165         return NULL;
166 }
167
168 static void _grid_longpress(void *data, Evas_Object *obj, void *event_info)
169 {
170         struct appdata *ad = data;
171         clipdrawer_change_mode(ad);
172 }
173
174
175 static void _grid_click_paste(void *data, Evas_Object *obj, void *event_info)
176 {
177         struct appdata *ad = data;
178         char *file, *p;
179         int len;
180         Elm_Gengrid_Item *sgobj = NULL;
181         sgobj = elm_gengrid_selected_item_get(ad->imggrid);
182         gridimgitem_t *ti = NULL;
183         ti = elm_gengrid_item_data_get(sgobj);
184
185         if (!sgobj || !ti)
186         {
187                 DTRACE("ERR: cbhm can't get the selected image\n");
188                 return;
189         }
190         len = strlen(ti->path);
191         p = malloc(len + 10);
192         snprintf(p,len+10, "file:///%s", ti->path);
193
194         elm_selection_set(/*secondary*/1,obj,/*ELM_SEL_FORMAT_IMAGE*/4,p);
195
196 //      clipdrawer_lower_view(ad);
197
198         elm_gengrid_item_selected_set(sgobj, EINA_FALSE);
199 }
200
201 static void _grid_del_response_cb(void *data, Evas_Object *obj, void *event_info)
202 {
203         Elm_Gengrid_Item *it = (Elm_Gengrid_Item *)data;
204         evas_object_del(obj);
205
206         if((int)event_info == ELM_POPUP_RESPONSE_OK)
207                 elm_gengrid_item_del(it);
208 }
209
210 static void _grid_click_delete(void *data, Evas_Object *obj, void *event_info)
211 {
212         struct appdata *ad = data;
213         Elm_Gengrid_Item *sgobj = NULL;
214         sgobj = elm_gengrid_selected_item_get(ad->imggrid);
215         gridimgitem_t *ti = NULL;
216         ti = elm_gengrid_item_data_get(sgobj);
217
218         if (!sgobj || !ti)
219         {
220                 DTRACE("ERR: cbhm can't get the clicked del image\n");
221                 return;
222         }
223
224         Evas_Object *popup = elm_popup_add(ad->win_main);
225         elm_popup_timeout_set(popup, 3);
226         evas_object_size_hint_weight_set(popup, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
227         elm_popup_desc_set(popup, "Are you sure delete this?");
228         elm_popup_buttons_add(popup, 2,
229                                                   "Yes", ELM_POPUP_RESPONSE_OK,
230                                                   "No", ELM_POPUP_RESPONSE_CANCEL,
231                                                   NULL);
232         evas_object_smart_callback_add(popup, "response", _grid_del_response_cb, sgobj);
233         evas_object_show(popup);
234 }
235
236 void _grid_del(const void *data, Evas_Object *obj)
237 {
238         gridimgitem_t *ti = (gridimgitem_t *)data;
239         eina_stringshare_del(ti->path);
240         free(ti);
241 }
242
243 int clipdrawer_refresh_imghistory_item(void *data, int delete_mode)
244 {
245         struct appdata *ad = data;
246         Eina_List *oldlist = NULL;
247         const Eina_List *l;
248         Elm_Gengrid_Item *lgrid;
249         gridimgitem_t *lgitem;
250         Evas_Object *ngg;
251         Evas_Object *oldgg;
252         
253         oldlist = elm_gengrid_items_get(ad->imggrid);
254         elm_layout_content_unset(ad->ly_main, "imagehistory/list");
255         ngg = elm_gengrid_add(ad->win_main);
256         elm_layout_content_set(ad->ly_main, "imagehistory/list", ngg);
257         oldgg = ad->imggrid;
258         ad->imggrid = ngg;
259         elm_gengrid_item_size_set(ad->imggrid, GRID_ITEM_SIZE, GRID_ITEM_SIZE+3);
260         elm_gengrid_align_set(ad->imggrid, 0.0, 0.0);
261         elm_gengrid_horizontal_set(ad->imggrid, EINA_TRUE);
262         elm_gengrid_bounce_set(ad->imggrid, EINA_TRUE, EINA_FALSE);
263         elm_gengrid_multi_select_set(ad->imggrid, EINA_FALSE);
264         if (delete_mode)
265                 evas_object_smart_callback_add(ad->imggrid, "selected", _grid_click_delete, ad);
266         else
267                 evas_object_smart_callback_add(ad->imggrid, "selected", _grid_click_paste, ad);
268         evas_object_smart_callback_add(ad->imggrid, "longpressed", _grid_longpress, ad);
269         evas_object_size_hint_weight_set(ad->imggrid, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
270
271         gic.item_style = "default_grid";
272         gic.func.label_get = NULL;
273         gic.func.icon_get = _grid_icon_get;
274         gic.func.state_get = NULL;
275         gic.func.del = _grid_del;
276
277         EINA_LIST_REVERSE_FOREACH(oldlist, l, lgrid)
278         {
279                 lgitem = elm_gengrid_item_data_get(lgrid);
280                 clipdrawer_add_image_item(lgitem->path);
281         }
282
283         evas_object_show (ad->imggrid);
284
285         elm_gengrid_clear(oldgg);
286         evas_object_hide(oldgg);
287         evas_object_del(oldgg);
288
289         return 0;
290 }
291
292 int clipdrawer_refresh_txthistory_item(void *data, int delete_mode)
293 {
294         struct appdata *ad = data;
295         Eina_List *oldlist = NULL;
296         const Eina_List *l;
297         Elm_List_Item *litem;
298         const char *ltext;
299         Evas_Object *ntl = NULL;
300 //      Evas_Object *ci = NULL;
301
302         oldlist = elm_list_items_get(ad->txtlist);
303         elm_layout_content_unset(ad->ly_main, "texthistory/list");
304         ntl = elm_list_add(ad->win_main);
305         elm_layout_content_set(ad->ly_main, "texthistory/list", ntl);
306         elm_object_style_set(ntl, "extended/historylist");
307
308         EINA_LIST_FOREACH(oldlist, l, litem)
309         {
310                 ltext = elm_list_item_label_get(litem);
311                 if (delete_mode)
312                 {
313                         Evas_Object *ci = elm_icon_add(ad->win_main);
314                         elm_icon_file_set(ci, DELETE_ICON_PATH, NULL);
315                         evas_object_size_hint_min_set(ci, 33 * elm_scale_get(), 33 * elm_scale_get());
316 //                      evas_object_size_hint_aspect_set(ci, EVAS_ASPECT_CONTROL_VERTICAL, 1, 1);
317                         evas_object_show(ci);
318                         elm_list_item_append(ntl, ltext, NULL, ci, NULL, ad);
319                 }
320                 else
321                 {
322                         elm_list_item_append(ntl, ltext, NULL, NULL, NULL, ad);
323                 }
324         }
325
326         elm_list_clear(ad->txtlist);
327         evas_object_hide(ad->txtlist);
328         evas_object_del(ad->txtlist);
329
330         ad->txtlist = ntl;
331         evas_object_size_hint_weight_set(ad->txtlist, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
332         if (delete_mode)
333                 evas_object_smart_callback_add(ad->txtlist, "selected", _list_click_delete, ad);
334         else
335                 evas_object_smart_callback_add(ad->txtlist, "selected", _list_click_paste, ad);
336         evas_object_smart_callback_add(ad->txtlist, "longpressed", _list_longpress, ad);
337
338         elm_list_go(ad->txtlist);
339
340         return 0;
341 }
342
343 int clipdrawer_change_mode(void *data)
344 {
345         struct appdata *ad = data;
346
347         toggle_clipdrawer_mode();
348
349         DTRACE("clipdrawer delete mode = %d\n", get_clipdrawer_mode());
350
351         clipdrawer_refresh_imghistory_item(ad, get_clipdrawer_mode());
352         clipdrawer_refresh_txthistory_item(ad, get_clipdrawer_mode());
353
354         return 0;
355 }
356
357 // FIXME: how to remove calling g_get_main_appdata()? 
358 //        it's mainly used at 'clipdrawer_add_image_item'
359 int clipdrawer_add_image_item(char *imagepath)
360 {
361         struct appdata *ad = g_get_main_appdata();
362         gridimgitem_t *newgenimg = NULL;
363         char* filepath = NULL;
364         Eina_List *igl = NULL;
365         unsigned int igl_counter = 0;
366
367         // if delete mode, then back to normal mode
368         if (get_clipdrawer_mode())
369                 clipdrawer_change_mode(ad);
370
371         if (!check_regular_file(imagepath))
372         {
373                 DTRACE("Error : it isn't normal file = %s\n", imagepath);
374                 return -1;
375         }
376
377         igl = elm_gengrid_items_get(ad->imggrid);
378         igl_counter = eina_list_count(igl);
379         if (igl_counter >= HISTORY_QUEUE_MAX_IMG_ITEMS)
380         {
381                 elm_gengrid_item_del(eina_list_data_get(eina_list_last(igl)));
382         }
383
384         newgenimg = malloc(sizeof(gridimgitem_t));
385         newgenimg->path = eina_stringshare_add(imagepath);
386         newgenimg->item = elm_gengrid_item_prepend(ad->imggrid, &gic, newgenimg, NULL, NULL);
387
388         return TRUE;
389 }
390
391 static void
392 clipdrawer_ly_clicked(void *data, Evas_Object *obj, const char *emission, const char *source)
393 {
394         struct appdata *ad = data;
395
396         #define EDJE_CLOSE_PART_PREFIX "closebutton/"
397         if (!strncmp(source, EDJE_CLOSE_PART_PREFIX, strlen(EDJE_CLOSE_PART_PREFIX)))
398         {
399                 clipdrawer_lower_view(ad);
400         }
401 }
402
403 int clipdrawer_init(void *data)
404 {
405         struct appdata *ad = data;
406         double cdy, cdw;
407
408         // for elm_check, elm_list
409         elm_theme_extension_add(NULL, APP_EDJ_FILE);
410
411         cdy = (1.0*CLIPDRAWER_HEIGHT/SCREEN_HEIGHT)*ad->root_h;
412         cdw = (1.0*CLIPDRAWER_POS_Y/SCREEN_HEIGHT)*ad->root_h;
413
414         evas_object_resize(ad->win_main, ad->root_w, (int)cdy);
415         evas_object_move(ad->win_main, CLIPDRAWER_POS_X, (int)cdw);
416         evas_object_resize(ad->ly_main, ad->root_w, (int)cdy);
417         evas_object_move(ad->ly_main, CLIPDRAWER_POS_X, (int)cdw);
418
419         edje_object_signal_callback_add(elm_layout_edje_get(ad->ly_main), "mouse,up,1", "*", clipdrawer_ly_clicked, ad);
420
421         ad->imggrid = NULL;
422         ad->imggrid = elm_gengrid_add(ad->win_main);
423         elm_layout_content_set(ad->ly_main, "imagehistory/list", ad->imggrid);
424         elm_gengrid_item_size_set(ad->imggrid, GRID_ITEM_SIZE, GRID_ITEM_SIZE+3);
425         elm_gengrid_align_set(ad->imggrid, 0.0, 0.0);
426         elm_gengrid_horizontal_set(ad->imggrid, EINA_TRUE);
427         elm_gengrid_bounce_set(ad->imggrid, EINA_TRUE, EINA_FALSE);
428         elm_gengrid_multi_select_set(ad->imggrid, EINA_FALSE);
429         evas_object_smart_callback_add(ad->imggrid, "selected", _grid_click_paste, ad);
430         evas_object_smart_callback_add(ad->imggrid, "longpressed", _grid_longpress, ad);
431         evas_object_size_hint_weight_set(ad->imggrid, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
432
433         elm_gengrid_clear(ad->imggrid);
434
435         gic.item_style = "default_grid";
436         gic.func.label_get = NULL;
437         gic.func.icon_get = _grid_icon_get;
438         gic.func.state_get = NULL;
439         gic.func.del = _grid_del;
440
441         int i;
442         gridimgitem_t *newgenimg;
443
444         for (i = 0; i < N_IMAGES; i++)
445         {
446                 clipdrawer_add_image_item(g_images_path[i]);
447         }
448
449         evas_object_show (ad->imggrid);
450
451 // for debugging, calc image history pos
452 /*
453    Evas_Coord x, y, w, h;
454    Evas_Coord vx, vy, vw, vh;
455
456    edje_object_part_geometry_get(elm_layout_edje_get(ad->ly_main),"imagehistory/list",&x,&y,&w,&h);
457    evas_object_geometry_get (ad->imggrid, &vx,&vy,&vw,&vh);
458    fprintf(stderr, "## x = %d, y = %d, w = %d, h = %d\n", x, y, w, h);
459    fprintf(stderr, "## vx = %d, vy = %d, vw = %d, vh = %d\n", vx, vy, vw, vh);
460 */
461
462         ad->txtlist = elm_list_add(ad->win_main);
463         elm_layout_content_set(ad->ly_main, "texthistory/list", ad->txtlist);
464         elm_object_style_set(ad->txtlist, "extended/historylist");
465         evas_object_size_hint_weight_set(ad->txtlist, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
466         evas_object_smart_callback_add(ad->txtlist, "selected", _list_click_paste, ad);
467         evas_object_smart_callback_add(ad->txtlist, "longpressed", _list_longpress, ad);
468         elm_list_item_append(ad->txtlist, "default", NULL, NULL, NULL, ad);
469
470         elm_list_go(ad->txtlist);
471
472         if (get_item_counts() != 0)
473                 clipdrawer_update_contents(ad);
474
475         return 0;
476 }
477
478 int clipdrawer_create_view(void *data)
479 {
480         struct appdata *ad = data;
481
482         clipdrawer_init(ad);
483
484         // for debug
485         // at starting, showing app view
486         //clipdrawer_activate_view(ad);
487
488         return 0;
489 }
490
491 void clipdrawer_activate_view(void *data)
492 {
493         struct appdata *ad = data;
494         
495         if (ad->win_main)
496         {
497                 evas_object_show(ad->win_main);
498                 elm_win_activate(ad->win_main);
499         }
500 }
501
502 void clipdrawer_lower_view(void *data)
503 {
504         struct appdata *ad = data;
505         
506         if (ad->win_main)
507         {
508                 evas_object_hide(ad->win_main);
509                 elm_win_lower(ad->win_main);
510
511                 // if delete mode, then back to normal mode
512                 if (get_clipdrawer_mode())
513                         clipdrawer_change_mode(ad);
514         }
515 }