2 * Copyright 2012 Samsung Electronics Co., Ltd
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
8 * http://www.tizenopensource.org/license
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.
22 #include <libsoup/soup.h>
24 #include "app-selector.h"
25 #include "app-selector-view.h"
27 #define TBL_NAME "menu"
28 #define MIME_FIELD_NAME "mimetype"
31 #define MAX_MIME_STR_SIZE 256
32 #define MAX_SCHEME_STR_SIZE 256
33 #define MAX_HOST_STR_SIZE 256
34 #define EDJ_NAME "/usr/share/edje/app-selector/app-selector.edj"
36 extern int aul_forward_app(const char* pkgname, bundle *kb);
38 static Eina_Bool __unload_info_popup(void *data);
39 static void __default_app_set_cb(void *data, Evas_Object * obj,
41 static void __launch_set_default_app_popup(struct _select_app_info *info);
42 static void __response_cb(void *data, Evas_Object * obj, void *event_info);
43 static void __clear_app_list(void *data);
44 static int __app_list_get_with_mimetype(void *data);
45 static Eina_Bool __gl_state_get(const void *data, Evas_Object * obj,
47 static void __gl_del(const void *data, Evas_Object * obj);
48 static void __gl_sel(void *data, Evas_Object * obj, void *event_info);
49 static Evas_Object *__gl_content_get(const void *data, Evas_Object * obj,
51 static char *__gl_text_get(const void *data, Evas_Object * obj,
53 static void __app_genlist_item_append(struct appdata *ad);
54 static Evas_Object *__app_genlist_add(struct appdata *ad);
56 static Eina_Bool __unload_info_popup(void *data)
58 /* Evas_Object *eo = (Evas_Object *) data;
65 return ECORE_CALLBACK_CANCEL;
68 void load_info_popup(struct appdata *ad, char *str)
70 Evas_Object *eo = NULL;
71 eo = elm_popup_add(ad->win);
72 evas_object_size_hint_weight_set(eo, EVAS_HINT_EXPAND,
74 elm_object_text_set(eo, str);
76 ecore_timer_add(2.0, __unload_info_popup, eo);
80 static void __default_app_set_ok_cb(void *data, Evas_Object * obj,
85 char *uri_r_info = NULL;
94 struct _select_app_info *info = (struct _select_app_info *)data;
97 //printf("MIME :: %s, PKGNAME : %s\n", info->ad->mime_type, info->pkg_name);
99 if(info->ad->mime_type){
100 if (aul_set_defapp_with_mime(info->ad->mime_type, info->pkg_name) == AUL_R_OK){
101 //printf("[Select Application] Insert Complete!!! %d\n", __LINE__);
104 //printf("[Select Application] Insert Error!!! %d\n", __LINE__);
106 } else if(info->ad->control_op) {
108 if(info->ad->control_uri) {
109 if(strncmp(info->ad->control_uri,"/",1) == 0){
110 if(!info->ad->control_mime) {
111 info->ad->control_mime = malloc(MAX_MIME_STR_SIZE);
112 aul_get_mime_from_file(info->ad->control_uri, info->ad->control_mime, MAX_MIME_STR_SIZE);
114 info->ad->control_uri = NULL;
115 } else if(strncmp(info->ad->control_uri,"file:/",6)==0){
116 if(!info->ad->control_mime) {
117 info->ad->control_mime = malloc(MAX_MIME_STR_SIZE);
118 aul_get_mime_from_file(&info->ad->control_uri[5], info->ad->control_mime, MAX_MIME_STR_SIZE);
120 info->ad->control_uri = NULL;
121 } else if(strncmp(info->ad->control_uri,"file:///",8) == 0){
122 if(!info->ad->control_mime) {
123 info->ad->control_mime = malloc(MAX_MIME_STR_SIZE);
124 aul_get_mime_from_file(&info->ad->control_uri[7], info->ad->control_mime, MAX_MIME_STR_SIZE);
126 info->ad->control_uri = NULL;
130 s_uri = soup_uri_new(info->ad->control_uri);
132 scheme = malloc(MAX_SCHEME_STR_SIZE);
133 strncpy(scheme, s_uri->scheme, MAX_SCHEME_STR_SIZE-1);
136 host = malloc(MAX_HOST_STR_SIZE);
137 strncpy(host, s_uri->host, MAX_HOST_STR_SIZE-1);
140 uri_r_info = malloc(MAX_SCHEME_STR_SIZE+MAX_HOST_STR_SIZE+2);
141 snprintf(uri_r_info, MAX_SCHEME_STR_SIZE+MAX_HOST_STR_SIZE+1,
142 "%s://%s", scheme, host);
144 soup_uri_free(s_uri);
146 val = bundle_get_val(info->ad->kb, APP_SVC_K_URI_R_INFO);
149 appsvc_set_defapp(info->ad->control_op,info->ad->control_mime,scheme,info->pkg_name);
151 if(strncmp(val, APP_SVC_V_SCHEME_AND_HOST, strlen(APP_SVC_V_SCHEME_AND_HOST)) == 0) {
152 appsvc_set_defapp(info->ad->control_op,info->ad->control_mime,uri_r_info,info->pkg_name);
154 appsvc_set_defapp(info->ad->control_op,info->ad->control_mime,scheme,info->pkg_name);
156 bundle_del(info->ad->kb, APP_SVC_K_URI_R_INFO);
160 ret = aul_forward_app(info->pkg_name, info->ad->kb);
161 _D("ok : aul_forward_app() pkg_name:%s ret:%d", info->pkg_name, ret);
166 static void __default_app_set_calcel_cb(void *data, Evas_Object * obj,
174 struct _select_app_info *info = (struct _select_app_info *)data;
176 ret = aul_forward_app(info->pkg_name, info->ad->kb);
177 _D("cancel : aul_forward_app() pkg_name:%s ret:%d", info->pkg_name, ret);
183 static void __launch_set_default_app_popup(struct _select_app_info *info)
185 Evas_Object *popup = NULL;
186 Evas_Object *btn1 = NULL;
187 Evas_Object *btn2 = NULL;
192 struct appdata *ad = info->ad;
194 popup = elm_popup_add(ad->win);
195 evas_object_size_hint_weight_set(popup, EVAS_HINT_EXPAND,
197 elm_object_text_set(popup, _("Set this application as default?"));
198 elm_object_part_text_set(popup, "title,text", _("Default app setting"));
200 btn1 = elm_button_add(popup);
201 elm_object_text_set(btn1, dgettext("sys_string", "IDS_COM_SK_OK"));
202 elm_object_part_content_set(popup, "button1", btn1);
203 evas_object_smart_callback_add(btn1, "clicked", __default_app_set_ok_cb, info);
204 btn2 = elm_button_add(popup);
205 elm_object_text_set(btn2, _("Cancel"));
206 elm_object_part_content_set(popup, "button2", btn2);
207 evas_object_smart_callback_add(btn2, "clicked", __default_app_set_calcel_cb, info);
209 ad->def_set_popup = popup;
211 evas_object_show(popup);
215 static void __response_cb(void *data, Evas_Object * obj, void *event_info)
220 static void __clear_app_list(void *data)
222 struct appdata *ad = data;
224 struct _select_app_info *info;
227 EINA_LIST_FOREACH(ad->app_list, l, info) {
229 free(info->pkg_name);
230 free(info->app_name);
231 free(info->app_path);
232 free(info->app_icon_path);
238 eina_list_free(ad->app_list);
242 if (ad->app_genlist) {
243 elm_genlist_clear(ad->app_genlist);
247 static ail_cb_ret_e __iter_func(
248 const ail_appinfo_h appinfo, void *user_data)
250 struct appdata *ad = (struct appdata *)user_data;
251 struct _select_app_info *info;
254 info = calloc(1, sizeof(struct _select_app_info));
256 ail_appinfo_get_str(appinfo, AIL_PROP_PACKAGE_STR, &str);
257 info->pkg_name = strdup(str);
259 ail_appinfo_get_str(appinfo, AIL_PROP_NAME_STR, &str);
260 info->app_name = strdup(str);
262 ail_appinfo_get_str(appinfo, AIL_PROP_EXEC_STR, &str);
263 info->app_path = strdup(str);
265 ail_appinfo_get_str(appinfo, AIL_PROP_ICON_STR, &str);
266 info->app_icon_path = strdup(str);
268 printf("PKGNAME : %s, ICONPATH : %s\n", info->pkg_name,
269 info->app_icon_path);
273 ad->app_list = eina_list_append(ad->app_list, info);
275 return AIL_CB_RET_CONTINUE;
279 static int __app_list_get_with_mimetype(void *data)
282 struct appdata *ad = data;
287 ret = ail_filter_new(&filter);
288 if (ret != AIL_ERROR_OK)
291 ret = ail_filter_add_str(filter, AIL_PROP_MIMETYPE_STR, ad->mime_type);
292 if (ret != AIL_ERROR_OK) {
293 ail_filter_destroy(filter);
297 ail_filter_list_appinfo_foreach(filter, __iter_func, (void *)ad);
299 if (eina_list_count(ad->app_list) < 1) {
300 load_info_popup(ad, dgettext("sys_string", "IDS_COM_BODY_NO_APPLICATIONS_CAN_PERFORM_THIS_ACTION"));
301 ail_filter_destroy(filter);
305 ail_filter_destroy(filter);
310 static int __iterate(const char* pkg_name, void *data)
312 struct appdata *ad = data;
313 struct _select_app_info *info;
314 ail_appinfo_h handle;
318 ret = ail_get_appinfo(pkg_name, &handle);
319 if (ret != AIL_ERROR_OK) {
323 info = calloc(1, sizeof(struct _select_app_info));
325 ail_appinfo_get_str(handle, AIL_PROP_PACKAGE_STR, &str);
326 info->pkg_name = strdup(str);
328 ail_appinfo_get_str(handle, AIL_PROP_NAME_STR, &str);
329 info->app_name = strdup(str);
331 ail_appinfo_get_str(handle, AIL_PROP_EXEC_STR, &str);
332 info->app_path = strdup(str);
334 ail_appinfo_get_str(handle, AIL_PROP_ICON_STR, &str);
335 info->app_icon_path = strdup(str);
337 printf("PKGNAME : %s, ICONPATH : %s\n", info->pkg_name,
338 info->app_icon_path);
342 ad->app_list = eina_list_append(ad->app_list, info);
344 ret = ail_package_destroy_appinfo(handle);
345 if (ret != AIL_ERROR_OK) {
353 static int __app_list_get_with_control(void *data)
355 struct appdata *ad = data;
356 struct _select_app_info *info;
361 char op_mime[1024] = {0,};
362 char op_scheme[1024] = {0,};
364 kb = bundle_create();
367 _E("bundle creation fail\n");
372 appsvc_set_operation(kb, ad->control_op);
376 appsvc_set_uri(kb, ad->control_uri);
379 if(ad->control_mime){
380 appsvc_set_mime(kb, ad->control_mime);
383 if( appsvc_get_list(kb, __iterate, ad) != 0 ){
384 load_info_popup(ad, _("Cannot find associated application"));
394 static Eina_Bool __gl_state_get(const void *data, Evas_Object * obj,
400 static void __gl_del(const void *data, Evas_Object * obj)
405 static void __gl_sel(void *data, Evas_Object * obj, void *event_info)
407 Elm_Object_Item *item = (Elm_Object_Item *) event_info;
417 struct _select_app_info *info =
418 (struct _select_app_info *)elm_object_item_data_get(item);
420 __launch_set_default_app_popup(info);
425 Evas_Object *_add_layout(Evas_Object *parent, const char *file,
428 Evas_Object *eo = NULL;
431 eo = elm_layout_add(parent);
433 printf("[Error] Cannot add layout\n");
437 r = elm_layout_file_set(eo, file, group);
439 printf("[Error] Cannot set file layout\n");
444 evas_object_size_hint_weight_set(eo, EVAS_HINT_EXPAND,
450 static Evas_Object *__gl_content_get(const void *data, Evas_Object * obj,
453 struct _select_app_info *info = (struct _select_app_info *)data;
454 char buf[PATH_LEN] = { 0, };
457 Evas_Object *rt, *icon_ly = NULL;
458 double scale = elm_config_scale_get();
460 if (!strcmp(part, "elm.icon")) {
461 snprintf(buf, (size_t) PATH_LEN, "%s", info->app_icon_path);
462 if (!ecore_file_exists(buf))
463 snprintf((char *)buf, PATH_LEN,
464 (const char *)ICON_PATH
465 "/icon_app-selector.png");
468 if (!strncmp(&buf[strlen(buf) - 3], "edj", 3)) {
469 icon_ly = _add_layout(obj, buf, "icon");
470 icon = _add_layout(obj, EDJ_NAME, "icon");
471 rt = evas_object_rectangle_add(evas_object_evas_get(obj));
473 evas_object_color_set(rt, 0, 0, 0, 0);
474 evas_object_size_hint_min_set(rt, (int)(50.0 * scale),
475 (int)(50.0 * scale));
476 evas_object_size_hint_max_set(rt, (int)(50.0 * scale),
477 (int)(50.0 * scale));
478 elm_object_part_content_set(icon, "icon_ly", rt);
480 elm_object_part_content_set(icon, "icon", icon_ly);
483 icon = elm_icon_add(obj);
484 elm_icon_file_set(icon, buf, NULL);
485 evas_object_size_hint_aspect_set(icon,
486 EVAS_ASPECT_CONTROL_VERTICAL,
495 static char *__gl_text_get(const void *data, Evas_Object * obj,
498 struct _select_app_info *info = (struct _select_app_info *)data;
499 char buf[NAME_LEN] = { 0, };
501 if (!strcmp(part, "elm.text")) {
502 snprintf(buf, NAME_LEN, "%s", info->app_name);
508 static void __app_genlist_item_append(struct appdata *ad)
511 struct _select_app_info *info;
512 Elm_Genlist_Item_Class *itc;
517 itc = elm_genlist_item_class_new();
519 itc->item_style = "1text.1icon.2";
520 itc->func.text_get = __gl_text_get;
521 itc->func.content_get = __gl_content_get;
522 itc->func.state_get = __gl_state_get;
523 itc->func.del = __gl_del;
525 EINA_LIST_FOREACH(ad->app_list, l, info) {
528 elm_genlist_item_append(ad->app_genlist, itc, (void *)info,
529 NULL, ELM_GENLIST_ITEM_NONE,
534 elm_genlist_item_class_free(itc);
537 static Evas_Object *__app_genlist_add(struct appdata *ad)
539 Evas_Object *genlist;
546 genlist = elm_genlist_add(ad->popup);
548 evas_object_size_hint_weight_set(genlist, EVAS_HINT_EXPAND,
550 evas_object_size_hint_align_set(genlist, EVAS_HINT_FILL,
556 void load_app_list(struct appdata *ad)
561 popup = elm_popup_add(ad->win);
562 elm_object_style_set(popup, "menustyle");
565 evas_object_size_hint_weight_set(popup, EVAS_HINT_EXPAND,
567 elm_object_part_text_set(popup, "title,text", _("Select application"));
569 btn1 = elm_button_add(popup);
570 elm_object_text_set(btn1, _("Cancel"));
571 elm_object_part_content_set(popup, "button1", btn1);
573 ad->app_genlist = __app_genlist_add(ad);
574 if (!ad->app_genlist)
577 __app_genlist_item_append(ad);
579 elm_object_content_set(popup, ad->app_genlist);
580 evas_object_smart_callback_add(btn1, "clicked", __response_cb, NULL);
582 evas_object_show(popup);
585 void update_app_list(struct appdata *ad)
587 __clear_app_list(ad);
588 if (__app_list_get_with_mimetype(ad) == -1)
591 __app_genlist_item_append(ad);
594 void load_app_select_popup(struct appdata *ad)
599 ret = __app_list_get_with_mimetype(ad);
600 else if(ad->control_op)
601 ret = __app_list_get_with_control(ad);
605 printf("fail fail fail\n");