Upload Tizen2.0 source
[framework/appfw/aul-1.git] / src / service.c
1 /*
2  *  aul
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Jayoun Lee <airjany@samsung.com>, Sewook Park <sewook7.park@samsung.com>, Jaeho Lee <jaeho81.lee@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <stdlib.h>
26
27 #include <stdio.h>
28 #include <string.h>
29 #include <bundle.h>
30
31 #include "aul_service.h"
32 #include "aul.h"
33 #include "aul_api.h"
34 #include "mida.h"
35 #include "menu_db_util.h"
36 #include "simple_util.h"
37 #include "launch.h"
38
39 static int __get_defapp_from_desktop(const char *svcname, 
40                                         char *defapp, int len);
41
42
43
44 SLPAPI int aul_set_defapp_for_service(const char *svcname, const char *defapp)
45 {
46         if ((svcname == NULL) || (defapp == NULL))
47                 return AUL_R_EINVAL;
48
49         if (!is_supported_svc(svcname))
50                 return AUL_R_EINVAL;
51
52         if (svc_add_app(svcname, defapp) < 0) {
53                 _E("fail to add");
54                 return AUL_R_ERROR;
55         }
56
57         return AUL_R_OK;
58 }
59
60 static ail_cb_ret_e __defapp_with_service_func(
61                         const ail_appinfo_h appinfo, void *user_data)
62 {
63         char **package = (char **)user_data;
64         char *str;
65
66         ail_appinfo_get_str(appinfo, AIL_PROP_PACKAGE_STR, &str);
67
68         _D("defapp from desktop = %s", str);
69
70         *package = strdup(str);
71         
72         return AIL_CB_RET_CANCEL;       /*return AIL_CB_RET_CONTINUE;*/ 
73 }
74
75
76 static int __get_defapp_from_desktop(const char *svcname, char *defapp, int len)
77 {
78         char *pkgname = NULL;
79         int ret = -1;
80         ail_filter_h filter;
81         ail_error_e ail_ret;
82         int pkg_count = -1;
83
84         _D("The svcname is: %s", svcname);
85
86         ail_ret = ail_filter_new(&filter);
87         if (ail_ret != AIL_ERROR_OK) 
88                 return ret;
89         
90         ail_ret = ail_filter_add_str(filter, AIL_PROP_X_SLP_SERVICE_STR, svcname);
91         if (ail_ret != AIL_ERROR_OK) {
92                 _E("ail_filter_add_str failed");
93                 goto out;
94         }
95
96         ail_filter_count_appinfo(filter, &pkg_count);
97
98         
99         /* TODO: Prioritizing inhouse app depending on the UX policy */
100         if (pkg_count == 1) {
101                 ail_filter_list_appinfo_foreach(filter, 
102                         __defapp_with_service_func, (void *)&pkgname);
103
104                 if(pkgname) {
105                         strncpy(defapp,pkgname,len);
106                         _D("defapp from desktop = %s", defapp);
107                          aul_set_defapp_for_service(svcname, defapp);
108                         ret = 0;
109                         free(pkgname);
110                 }
111                 
112         } 
113
114  out:
115         if (ail_filter_destroy(filter) != AIL_ERROR_OK)
116                 _E("ail_filter_destroy failed");
117         return ret;
118 }
119
120 SLPAPI int aul_get_defapp_for_service(const char *svcname, char *defapp,
121                                       int len)
122 {
123         char *res = NULL;
124
125         if ((svcname == NULL) || (defapp == NULL) || len <= 0)
126                 return AUL_R_EINVAL;
127
128         if (!is_supported_svc(svcname))
129                 return AUL_R_EINVAL;
130
131         /* search mida db*/
132         if ((res = svc_get_app(svcname)) != NULL) {
133                 snprintf(defapp, len, "%s", res);
134                 free(res);
135                 _D("Found %s for %s from svc db", defapp, svcname);
136                 return AUL_R_OK;
137         }
138
139         if (__get_defapp_from_desktop(svcname, defapp, len) < 0)
140                 return AUL_R_ERROR;
141         else
142                 return AUL_R_OK;
143 }
144
145 SLPAPI int aul_open_service(const char *svcname, bundle *kb,
146                             aul_service_res_fn cbfunc, void *userdata)
147 {
148         char defapp[MAX_LOCAL_BUFSZ];
149         int must_free = 0;
150         int ret = AUL_R_ERROR;
151         ail_appinfo_h handle;
152         ail_error_e ail_ret;
153
154         if (svcname == NULL)
155                 return AUL_R_EINVAL;
156
157         if (!is_supported_svc(svcname))
158                 return AUL_R_EINVAL;
159
160         if (kb == NULL) {
161                 kb = bundle_create();
162                 must_free = 1;
163         }
164         bundle_add(kb, AUL_K_SERVICE_NAME, svcname);
165
166  retry:
167         if (aul_get_defapp_for_service(svcname, defapp, sizeof(defapp)) < 0) {
168                 _D("service : %s, no default app", svcname);
169                 if (must_free) {
170                         bundle_free(kb);
171                         kb = NULL;
172                 }
173                 return ret;
174         } else {
175                 ail_ret = ail_package_get_appinfo(defapp, &handle);
176
177                 if (ail_ret == AIL_ERROR_OK) {
178                         ail_destroy_appinfo(handle);
179                         _D("svcname: %s, defapp : %s", svcname, defapp);
180                         
181                         if (cbfunc) {
182                                 _D("svcname: %s, defapp : %s - with result",
183                                    svcname, defapp);
184                                 ret =
185                                     aul_launch_app_with_result(defapp, kb,
186                                                                cbfunc,
187                                                                userdata);
188                         } else {
189                                 _D("svcname: %s, defapp : %s - no result",
190                                    svcname, defapp);
191                                 ret = aul_launch_app(defapp, kb);
192                         }
193                 } else if (ail_ret == AIL_ERROR_NO_DATA) {
194                         _D("defapp %s for svcname: %s does NOT exist", defapp,
195                            svcname);
196                         svc_delete_with_pkgname(defapp);
197                         ail_destroy_appinfo(handle);
198                         goto retry;
199                 } else {
200                         _E("ail_get_appinfo with %s failed", defapp);
201                         if (must_free) {
202                                 bundle_free(kb);
203                                 kb = NULL;
204                         }
205                         return ret;
206                 }       
207         }
208         if (must_free)
209                 bundle_free(kb);
210
211         return ret;
212
213 }
214
215 SLPAPI int aul_send_service_result(bundle *b)
216 {
217         return aul_send_result(b, 0);
218 }
219