Git init
[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
38 static int __get_defapp_from_desktop(const char *svcname, 
39                                         char *defapp, int len);
40
41
42
43 SLPAPI int aul_set_defapp_for_service(const char *svcname, const char *defapp)
44 {
45         if ((svcname == NULL) || (defapp == NULL))
46                 return AUL_R_EINVAL;
47
48         if (!is_supported_svc(svcname))
49                 return AUL_R_EINVAL;
50
51         if (svc_add_app(svcname, defapp) < 0) {
52                 _E("fail to add");
53                 return AUL_R_ERROR;
54         }
55
56         return AUL_R_OK;
57 }
58
59 static ail_cb_ret_e __defapp_with_service_func(
60                         const ail_appinfo_h appinfo, void *user_data)
61 {
62         char **package = (char **)user_data;
63         char *str;
64
65         ail_appinfo_get_str(appinfo, AIL_PROP_PACKAGE_STR, &str);
66
67         _D("defapp from desktop = %s", str);
68
69         *package = strdup(str);
70         
71         return AIL_CB_RET_CANCEL;       /*return AIL_CB_RET_CONTINUE;*/ 
72 }
73
74
75 static int __get_defapp_from_desktop(const char *svcname, char *defapp, int len)
76 {
77         char *pkgname = NULL;
78         int ret = -1;
79         ail_filter_h filter;
80         ail_error_e ail_ret;
81         int pkg_count = -1;
82
83         _D("The svcname is: %s", svcname);
84
85         ail_ret = ail_filter_new(&filter);
86         if (ail_ret != AIL_ERROR_OK) 
87                 return ret;
88         
89         ail_ret = ail_filter_add_str(filter, AIL_PROP_X_SLP_SERVICE_STR, svcname);
90         if (ail_ret != AIL_ERROR_OK) {
91                 _E("ail_filter_add_str failed");
92                 goto out;
93         }
94
95         ail_filter_count_appinfo(filter, &pkg_count);
96
97         
98         /* TODO: Prioritizing inhouse app depending on the UX policy */
99         if (pkg_count == 1) {
100                 ail_filter_list_appinfo_foreach(filter, 
101                         __defapp_with_service_func, (void *)&pkgname);
102
103                 if(pkgname) {
104                         strncpy(defapp,pkgname,len);
105                         _D("defapp from desktop = %s", defapp);
106                          aul_set_defapp_for_service(svcname, defapp);
107                         ret = 0;
108                         free(pkgname);
109                 }
110                 
111         } 
112
113  out:
114         if (ail_filter_destroy(filter) != AIL_ERROR_OK)
115                 _E("ail_filter_destroy failed");
116         return ret;
117 }
118
119 SLPAPI int aul_get_defapp_for_service(const char *svcname, char *defapp,
120                                       int len)
121 {
122         char *res = NULL;
123
124         if ((svcname == NULL) || (defapp == NULL) || len <= 0)
125                 return AUL_R_EINVAL;
126
127         if (!is_supported_svc(svcname))
128                 return AUL_R_EINVAL;
129
130         /* search mida db*/
131         if ((res = svc_get_app(svcname)) != NULL) {
132                 snprintf(defapp, len, "%s", res);
133                 free(res);
134                 _D("Found %s for %s from svc db", defapp, svcname);
135                 return AUL_R_OK;
136         }
137
138         if (__get_defapp_from_desktop(svcname, defapp, len) < 0)
139                 return AUL_R_ERROR;
140         else
141                 return AUL_R_OK;
142 }
143
144 SLPAPI int aul_open_service(const char *svcname, bundle *kb,
145                             aul_service_res_fn cbfunc, void *userdata)
146 {
147         char defapp[MAX_LOCAL_BUFSZ];
148         int must_free = 0;
149         int ret = AUL_R_ERROR;
150         ail_appinfo_h handle;
151         ail_error_e ail_ret;
152
153         if (svcname == NULL)
154                 return AUL_R_EINVAL;
155
156         if (!is_supported_svc(svcname))
157                 return AUL_R_EINVAL;
158
159         if (kb == NULL) {
160                 kb = bundle_create();
161                 must_free = 1;
162         }
163         bundle_add(kb, AUL_K_SERVICE_NAME, svcname);
164
165  retry:
166         if (aul_get_defapp_for_service(svcname, defapp, sizeof(defapp)) < 0) {
167                 _D("service : %s, no default app", svcname);
168                 if (must_free) {
169                         bundle_free(kb);
170                         kb = NULL;
171                 }
172                 return ret;
173         } else {
174                 ail_ret = ail_package_get_appinfo(defapp, &handle);
175
176                 if (ail_ret == AIL_ERROR_OK) {
177                         ail_package_destroy_appinfo(handle);
178                         _D("svcname: %s, defapp : %s", svcname, defapp);
179                         
180                         if (cbfunc) {
181                                 _D("svcname: %s, defapp : %s - with result",
182                                    svcname, defapp);
183                                 ret =
184                                     aul_launch_app_with_result(defapp, kb,
185                                                                cbfunc,
186                                                                userdata);
187                         } else {
188                                 _D("svcname: %s, defapp : %s - no result",
189                                    svcname, defapp);
190                                 ret = aul_launch_app(defapp, kb);
191                         }
192                 } else if (ail_ret == AIL_ERROR_NO_DATA) {
193                         _D("defapp %s for svcname: %s does NOT exist", defapp,
194                            svcname);
195                         svc_delete_with_pkgname(defapp);
196                         ail_package_destroy_appinfo(handle);
197                         goto retry;
198                 } else {
199                         _E("ail_package_get_appinfo with %s failed", defapp);
200                         if (must_free) {
201                                 bundle_free(kb);
202                                 kb = NULL;
203                         }
204                         return ret;
205                 }       
206         }
207         if (must_free)
208                 bundle_free(kb);
209
210         return ret;
211
212 }
213
214 SLPAPI int aul_send_service_result(bundle *b)
215 {
216         return aul_send_result(b, 0);
217 }
218