tizen 2.4 release
[apps/home/attach-panel.git] / sample / sample.c
1 /*
2  * Samsung API
3  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
4  *
5  * Licensed under the Apache License, Version 2.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.apache.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 <app.h>
19 #include <app_control.h>
20 #include <Elementary.h>
21 #include <efl_extension.h>
22
23 #include "attach_panel.h"
24 #include "log.h"
25
26
27
28 const char *const KEY_BACK = "XF86Back";
29 const char *const LAYOUT = "/usr/share/attach-panel/sample/sample.edj";
30
31
32
33 static struct {
34         Evas_Object *win;
35         Evas_Object *layout;
36         Evas_Object *bg;
37         Evas_Object *conformant;
38         attach_panel_h attach_panel;
39
40         int root_w;
41         int root_h;
42 } sample_info = {
43         .win = NULL,
44         .layout = NULL,
45         .bg = NULL,
46         .conformant = NULL,
47         .attach_panel = NULL,
48
49         .root_w = 0,
50         .root_h = 0,
51 };
52
53
54
55 static void _rotate_cb(void *data, Evas_Object *obj, void *event)
56 {
57         ret_if(!obj);
58
59         int angle = 0;
60
61         angle = elm_win_rotation_get(obj);
62
63         _D("Angle is %d degree", angle);
64
65         switch (angle) {
66         case 0:
67         case 180:
68                 evas_object_size_hint_min_set(obj, sample_info.root_w, sample_info.root_h);
69                 evas_object_resize(obj, sample_info.root_w, sample_info.root_h);
70                 evas_object_move(obj, 0, 0);
71                 break;
72         case 90:
73         case 270:
74                 evas_object_size_hint_min_set(obj, sample_info.root_h, sample_info.root_w);
75                 evas_object_resize(obj, sample_info.root_h, sample_info.root_w);
76                 evas_object_move(obj, 0, 0);
77                 break;
78         default:
79                 _E("cannot reach here");
80         }
81 }
82
83
84
85 static void _result_cb(attach_panel_h attach_panel, attach_panel_content_category_e content_category, app_control_h result, app_control_result_e result_code, void *user_data)
86 {
87         char **select = NULL;
88         int i = 0;
89         int length = 0;
90         int ret = APP_CONTROL_ERROR_NONE;
91
92         ret_if(!result);
93
94         ret = app_control_get_extra_data_array(result, "http://tizen.org/appcontrol/data/selected", &select, &length);
95         if (APP_CONTROL_ERROR_NONE == ret) {
96                 for (i = 0; i < length; i++) {
97                         _D("file_path is %s[%d]", select[i], i);
98                         elm_object_part_text_set(sample_info.layout, "result", select[i]);
99                         free(select[i]);
100                 }
101         }
102
103         free(select);
104 }
105
106
107
108 static void _event_cb(attach_panel_h attach_panel, attach_panel_event_e event, void *event_info, void *data)
109 {
110         ret_if(!attach_panel);
111
112         switch (event) {
113         case ATTACH_PANEL_EVENT_SHOW_START:
114                 _D("attach panel : show start");
115                 break;
116         case ATTACH_PANEL_EVENT_SHOW_FINISH:
117                 _D("attach panel : show finish");
118                 break;
119         case ATTACH_PANEL_EVENT_HIDE_START:
120                 _D("attach panel : hide start");
121                 break;
122         case ATTACH_PANEL_EVENT_HIDE_FINISH:
123                 _D("attach panel : hide finish");
124                 break;
125         }
126 }
127
128
129
130 static void _win_back_key_cb(void *data, Evas_Object *obj, void *event_info)
131 {
132         bool visible = false;
133
134         if (sample_info.attach_panel) {
135                 if (attach_panel_get_visibility(sample_info.attach_panel, &visible) != ATTACH_PANEL_ERROR_NONE) {
136                         _E("Fail to get visible of attach panel");
137                         return;
138                 }
139                 if (visible) {
140                         _D("attach panel state : show->hide");
141                         attach_panel_hide(sample_info.attach_panel);
142                 } else {
143                         attach_panel_remove_content_category(sample_info.attach_panel, ATTACH_PANEL_CONTENT_CATEGORY_IMAGE);
144                         attach_panel_remove_content_category(sample_info.attach_panel, ATTACH_PANEL_CONTENT_CATEGORY_CAMERA);
145                         attach_panel_remove_content_category(sample_info.attach_panel, ATTACH_PANEL_CONTENT_CATEGORY_VOICE);
146                         attach_panel_remove_content_category(sample_info.attach_panel, ATTACH_PANEL_CONTENT_CATEGORY_VIDEO);
147                         attach_panel_remove_content_category(sample_info.attach_panel, ATTACH_PANEL_CONTENT_CATEGORY_AUDIO);
148                         attach_panel_remove_content_category(sample_info.attach_panel, ATTACH_PANEL_CONTENT_CATEGORY_CALENDAR);
149                         attach_panel_remove_content_category(sample_info.attach_panel, ATTACH_PANEL_CONTENT_CATEGORY_CONTACT);
150                         attach_panel_remove_content_category(sample_info.attach_panel, ATTACH_PANEL_CONTENT_CATEGORY_MYFILES);
151                         attach_panel_remove_content_category(sample_info.attach_panel, ATTACH_PANEL_CONTENT_CATEGORY_VIDEO_RECORDER);
152
153                         attach_panel_unset_result_cb(sample_info.attach_panel);
154                         attach_panel_unset_event_cb(sample_info.attach_panel);
155
156                         attach_panel_destroy(sample_info.attach_panel);
157                         sample_info.attach_panel = NULL;
158                 }
159         } else {
160                 ui_app_exit();
161         }
162 }
163
164
165
166 static void _attach_panel_cancel_cb(void *data, Evas_Object *obj, const char *emission, const char *source)
167 {
168         _D("");
169
170         if (sample_info.attach_panel) {
171                 attach_panel_remove_content_category(sample_info.attach_panel, ATTACH_PANEL_CONTENT_CATEGORY_IMAGE);
172                 attach_panel_remove_content_category(sample_info.attach_panel, ATTACH_PANEL_CONTENT_CATEGORY_CAMERA);
173                 attach_panel_remove_content_category(sample_info.attach_panel, ATTACH_PANEL_CONTENT_CATEGORY_VOICE);
174                 attach_panel_remove_content_category(sample_info.attach_panel, ATTACH_PANEL_CONTENT_CATEGORY_VIDEO);
175                 attach_panel_remove_content_category(sample_info.attach_panel, ATTACH_PANEL_CONTENT_CATEGORY_AUDIO);
176                 attach_panel_remove_content_category(sample_info.attach_panel, ATTACH_PANEL_CONTENT_CATEGORY_CALENDAR);
177                 attach_panel_remove_content_category(sample_info.attach_panel, ATTACH_PANEL_CONTENT_CATEGORY_CONTACT);
178                 attach_panel_remove_content_category(sample_info.attach_panel, ATTACH_PANEL_CONTENT_CATEGORY_MYFILES);
179                 attach_panel_remove_content_category(sample_info.attach_panel, ATTACH_PANEL_CONTENT_CATEGORY_VIDEO_RECORDER);
180
181                 attach_panel_hide(sample_info.attach_panel);
182                 attach_panel_unset_result_cb(sample_info.attach_panel);
183                 attach_panel_unset_event_cb(sample_info.attach_panel);
184
185                 attach_panel_destroy(sample_info.attach_panel);
186                 sample_info.attach_panel = NULL;
187         }
188 }
189
190
191
192 static void _attach_panel_create_cb(void *data, Evas_Object *obj, void *event_info)
193 {
194         attach_panel_h attach_panel = NULL;
195         int ret = ATTACH_PANEL_ERROR_NONE;
196         bool visible = false;
197
198         if (sample_info.attach_panel) {
199                 _D("attach panel already existed");
200                 if (attach_panel_get_visibility(sample_info.attach_panel, &visible) != ATTACH_PANEL_ERROR_NONE) {
201                         _E("Fail to get visible of attach panel");
202                         return;
203                 }
204                 if (!visible) {
205                         _D("attach panel state : hide->show");
206                         attach_panel_show(sample_info.attach_panel);
207                 }
208                 return;
209         }
210
211         ret = attach_panel_create(sample_info.conformant, &attach_panel);
212         ret_if(ATTACH_PANEL_ERROR_NONE != ret);
213
214         attach_panel_add_content_category(attach_panel, ATTACH_PANEL_CONTENT_CATEGORY_IMAGE, NULL);
215         attach_panel_add_content_category(attach_panel, ATTACH_PANEL_CONTENT_CATEGORY_CAMERA, NULL);
216         attach_panel_add_content_category(attach_panel, ATTACH_PANEL_CONTENT_CATEGORY_VOICE, NULL);
217         attach_panel_add_content_category(attach_panel, ATTACH_PANEL_CONTENT_CATEGORY_VIDEO, NULL);
218         attach_panel_add_content_category(attach_panel, ATTACH_PANEL_CONTENT_CATEGORY_AUDIO, NULL);
219         attach_panel_add_content_category(attach_panel, ATTACH_PANEL_CONTENT_CATEGORY_CALENDAR, NULL);
220         attach_panel_add_content_category(attach_panel, ATTACH_PANEL_CONTENT_CATEGORY_CONTACT, NULL);
221         attach_panel_add_content_category(attach_panel, ATTACH_PANEL_CONTENT_CATEGORY_MYFILES, NULL);
222         attach_panel_add_content_category(attach_panel, ATTACH_PANEL_CONTENT_CATEGORY_VIDEO_RECORDER, NULL);
223         attach_panel_set_result_cb(attach_panel, _result_cb, NULL);
224         attach_panel_set_event_cb(attach_panel, _event_cb, NULL);
225
226         attach_panel_show(attach_panel);
227
228         sample_info.attach_panel = attach_panel;
229 }
230
231
232
233 static Evas_Object *_create_entry(Evas_Object *layout)
234 {
235         Evas_Object *entry = NULL;
236
237         retv_if(!layout, NULL);
238
239         entry = elm_entry_add(layout);
240         retv_if(!entry, NULL);
241
242         elm_entry_scrollable_set(entry, EINA_TRUE);
243         elm_object_part_text_set(entry, "elm.guide", "search");
244
245         elm_object_part_content_set(layout, "entry", entry);
246
247         elm_entry_cnp_mode_set(entry, ELM_CNP_MODE_PLAINTEXT);
248         elm_entry_input_panel_layout_set(entry, ELM_INPUT_PANEL_LAYOUT_NORMAL);
249         elm_entry_prediction_allow_set(entry, EINA_FALSE);
250         elm_entry_autocapital_type_set(entry, ELM_AUTOCAPITAL_TYPE_NONE);
251         elm_entry_input_panel_return_key_type_set(entry, ELM_INPUT_PANEL_RETURN_KEY_TYPE_DONE);
252         elm_entry_input_panel_return_key_disabled_set(entry, EINA_FALSE);
253
254         evas_object_show(entry);
255
256         return entry;
257 }
258
259
260
261 static void _destroy_entry(Evas_Object *layout)
262 {
263         Evas_Object *entry = NULL;
264         _D("");
265
266         entry = elm_object_part_content_unset(layout, "entry");
267         if (!entry) {
268                 return;
269         }
270         evas_object_del(entry);
271 }
272
273
274
275 static Evas_Object *_create_button(Evas_Object *layout)
276 {
277         Evas_Object *button = NULL;
278
279         retv_if(!layout, NULL);
280
281         button = elm_button_add(layout);
282         retv_if(!button, NULL);
283
284         elm_object_text_set(button, "attach");
285         elm_object_part_content_set(layout, "button", button);
286         evas_object_size_hint_weight_set(button, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
287         evas_object_show(button);
288
289         evas_object_smart_callback_add(button, "clicked", _attach_panel_create_cb, NULL);
290
291         return button;
292 }
293
294
295
296 static void _destroy_button(Evas_Object *layout)
297 {
298         Evas_Object *button = NULL;
299
300         button = elm_object_part_content_unset(layout, "button");
301         if (!button) {
302                 return;
303         }
304
305         evas_object_smart_callback_del(button, "clicked", _attach_panel_create_cb);
306         evas_object_del(button);
307 }
308
309
310
311 static void _destroy_layout(void)
312 {
313         ret_if(!sample_info.layout);
314
315         _destroy_button(sample_info.layout);
316         _destroy_entry(sample_info.layout);
317
318         elm_object_signal_callback_del(sample_info.layout, "bg,up", "bg", _attach_panel_cancel_cb);
319
320         if (sample_info.conformant) {
321                 elm_object_content_unset(sample_info.conformant);
322         }
323         evas_object_del(sample_info.layout);
324         sample_info.layout = NULL;
325 }
326
327
328
329 static Evas_Object *_create_layout(Evas_Object *parent)
330 {
331         Evas_Object *layout = NULL;
332         Eina_Bool ret = EINA_FALSE;
333
334         retv_if(!parent, NULL);
335
336         layout = elm_layout_add(parent);
337         goto_if(!layout, ERROR);
338
339         ret = elm_layout_file_set(layout, LAYOUT, "layout");
340         goto_if(EINA_FALSE == ret, ERROR);
341
342         evas_object_size_hint_weight_set(layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
343         evas_object_size_hint_align_set(layout, EVAS_HINT_FILL, EVAS_HINT_FILL);
344         evas_object_show(layout);
345         elm_object_signal_callback_add(layout, "bg,up", "bg", _attach_panel_cancel_cb, NULL);
346
347         goto_if(!_create_entry(layout), ERROR);
348         goto_if(!_create_button(layout), ERROR);
349
350         elm_object_content_set(parent, layout);
351
352         return layout;
353
354
355 ERROR:
356         _destroy_layout();
357         return NULL;
358 }
359
360
361
362 static Evas_Object *_create_bg(Evas_Object *parent)
363 {
364         Evas_Object *bg = NULL;
365         retv_if(!parent, NULL);
366
367         bg = elm_bg_add(parent);
368         retv_if(!bg, NULL);
369         evas_object_size_hint_weight_set(bg,  EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
370         elm_win_resize_object_add(parent, bg);
371
372         evas_object_show(bg);
373
374         return bg;
375 }
376
377
378
379 static void _destroy_bg(void)
380 {
381         ret_if(!sample_info.bg);
382         evas_object_del(sample_info.bg);
383         sample_info.bg = NULL;
384 }
385
386
387
388 static void _destroy_conformant(void)
389 {
390         ret_if(!sample_info.conformant);
391         evas_object_del(sample_info.conformant);
392         sample_info.conformant = NULL;
393 }
394
395
396
397 static Evas_Object *_create_conformant(Evas_Object *parent)
398 {
399         Evas_Object *conformant = NULL;
400         retv_if(!parent, NULL);
401
402         conformant = elm_conformant_add(parent);
403         retv_if(!conformant, NULL);
404
405         elm_win_indicator_mode_set(parent, ELM_WIN_INDICATOR_SHOW);
406         elm_win_indicator_opacity_set(parent, ELM_WIN_INDICATOR_OPAQUE);
407         evas_object_size_hint_weight_set(conformant,  EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
408         elm_win_resize_object_add(parent, conformant);
409         elm_win_conformant_set(parent, EINA_TRUE);
410
411         evas_object_show(conformant);
412
413         return conformant;
414 }
415
416
417
418 static void _create_cb(void)
419 {
420         Evas_Object *layout = NULL;
421         Evas_Object *conformant = NULL;
422         Evas_Object *bg = NULL;
423
424         sample_info.win = elm_win_add(NULL, "Attach Panel Sample", ELM_WIN_BASIC);
425         ret_if(!sample_info.win);
426
427         elm_app_base_scale_set(1.7);
428
429         elm_win_title_set(sample_info.win, "Attach Panel");
430         elm_win_alpha_set(sample_info.win, EINA_FALSE);
431         elm_win_borderless_set(sample_info.win, EINA_TRUE);
432         elm_win_autodel_set(sample_info.win, EINA_TRUE);
433         elm_win_raise(sample_info.win);
434
435         evas_object_show(sample_info.win);
436
437         elm_win_screen_size_get(sample_info.win, NULL, NULL, &sample_info.root_w, &sample_info.root_h);
438         _D("screen size is (%d, %d)", sample_info.root_w, sample_info.root_h);
439
440
441         if (elm_win_wm_rotation_supported_get(sample_info.win)) {
442                 int rots[4] = { 0, 90, 180, 270 };
443                 elm_win_wm_rotation_available_rotations_set(sample_info.win, rots, 4);
444         }
445
446         eext_object_event_callback_add(sample_info.win, EEXT_CALLBACK_BACK, _win_back_key_cb, NULL);
447         evas_object_smart_callback_add(sample_info.win, "wm,rotation,changed", _rotate_cb, NULL);
448
449         bg = _create_bg(sample_info.win);
450         goto_if(!bg, ERROR);
451
452         conformant = _create_conformant(sample_info.win);
453         goto_if(!conformant, ERROR);
454
455         layout = _create_layout(conformant);
456         goto_if(!layout, ERROR);
457
458         sample_info.layout = layout;
459         sample_info.conformant = conformant;
460         sample_info.bg = bg;
461
462         return;
463
464 ERROR:
465         _D("there is some error");
466         if (conformant) {
467                 _destroy_conformant();
468         }
469         if (bg) {
470                 _destroy_bg();
471         }
472
473         if (sample_info.win) {
474                 evas_object_del(sample_info.win);
475                 eext_object_event_callback_del(sample_info.win, EEXT_CALLBACK_BACK, _win_back_key_cb);
476                 sample_info.win = NULL;
477         }
478 }
479
480
481
482 static void _terminate_cb(void)
483 {
484         _D("");
485
486         _destroy_layout();
487         _destroy_conformant();
488         _destroy_bg();
489
490         if (sample_info.win) {
491                 eext_object_event_callback_del(sample_info.win, EEXT_CALLBACK_BACK, _win_back_key_cb);
492                 evas_object_del(sample_info.win);
493                 sample_info.win = NULL;
494         }
495 }
496
497
498
499 int main(int argc, char **argv)
500 {
501         elm_init(argc, argv);
502         _create_cb();
503         elm_run();
504         _terminate_cb();
505         elm_shutdown();
506
507         return 0;
508 }