2 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
4 * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
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.
20 #include <service_app.h>
29 #include <notification.h>
30 #include <notification_internal.h>
32 #include <pkgmgr-info.h>
33 #include <package-manager.h>
34 #include <package-manager-types.h>
36 #include "installer-service.h"
38 #define KEY_PACKAGE_LABEL "__KEY_PACKAGE_LABEL__"
39 #define KEY_PACKAGE_PATH "__KEY_PACKAGE_PATH__"
40 #define KEY_PACKAGE_ICON_PATH "__KEY_PACKAGE_ICON_PATH__"
42 static const char DEFAULT_ICON[] = "/usr/apps/org.tizen.installer/shared/res/org.tizen.installer.png";
44 typedef struct _appdata
54 static char * __get_file_extension(const char *file_path)
56 return strrchr(file_path, '.') + 1;
59 static int __get_pkg_mainappid(const char *pkgid, char **mainappid)
63 pkgmgrinfo_pkginfo_h handle;
64 res = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &handle);
65 if (res != PMINFO_R_OK)
67 LOGE("%s pkgmgrinfo_pkginfo_get_pkginfo() failed.(%d)", __func__, res);
70 res = pkgmgrinfo_pkginfo_get_mainappid(handle, &appid);
71 if (res != PMINFO_R_OK)
73 LOGE("%s pkgmgrinfo_pkginfo_get_mainappid() failed.(%d)", __func__, res);
74 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
77 *mainappid = strdup(appid);
78 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
83 static int __get_pkg_icon(const char *pkgid, char **pkg_icon_path)
87 pkgmgrinfo_pkginfo_h handle;
88 res = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &handle);
89 if (res != PMINFO_R_OK)
91 LOGE("%s pkgmgrinfo_pkginfo_get_pkginfo() failed.(%d)", __func__, res);
94 res = pkgmgrinfo_pkginfo_get_icon(handle, &icon);
95 if (res != PMINFO_R_OK)
97 LOGE("%s pkgmgrinfo_pkginfo_get_icon() failed.(%d)", __func__, res);
98 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
104 LOGE("%s icon is NULL.(%d)", __func__, res);
105 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
109 *pkg_icon_path = strdup(icon);
110 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
115 int __notify_notification(const char *pkg_id, const char *pkg_label, const char *pkg_icon_path, bool result)
117 notification_h noti_h = NULL;
118 notification_error_e noti_err = NOTIFICATION_ERROR_NONE;
120 noti_h = notification_create(NOTIFICATION_TYPE_NOTI);
123 LOGE("%s notification_create() failed.", __func__);
129 noti_err = notification_set_text(noti_h, NOTIFICATION_TEXT_TYPE_TITLE, "Installation completed.", NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
133 noti_err = notification_set_text(noti_h, NOTIFICATION_TEXT_TYPE_TITLE, "Installation failed.", NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
136 if (noti_err != NOTIFICATION_ERROR_NONE)
138 LOGE("%s notification_set_text() failed.(%d)", __func__, noti_err);
139 notification_free(noti_h);
143 noti_err = notification_set_text(noti_h, NOTIFICATION_TEXT_TYPE_CONTENT, pkg_label, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
144 if (noti_err != NOTIFICATION_ERROR_NONE)
146 LOGE("%s notification_set_ ... failed.(%d)", __func__, noti_err);
147 notification_free(noti_h);
151 noti_err = notification_set_image(noti_h, NOTIFICATION_IMAGE_TYPE_ICON, pkg_icon_path);
152 if (noti_err != NOTIFICATION_ERROR_NONE)
154 LOGE("%s notification_set_image(icon) failed.(%d)", __func__, noti_err);
155 notification_free(noti_h);
159 noti_err = notification_set_image(noti_h, NOTIFICATION_IMAGE_TYPE_ICON_FOR_INDICATOR, pkg_icon_path);
160 if (noti_err != NOTIFICATION_ERROR_NONE)
162 LOGE("%s notification_set_image(indicator) failed.(%d)", __func__, noti_err);
163 notification_free(noti_h);
167 //TODO: Add sound state checking routine.
168 noti_err = notification_set_sound(noti_h, NOTIFICATION_SOUND_TYPE_DEFAULT, NULL);
169 if (noti_err != NOTIFICATION_ERROR_NONE)
171 LOGE("%s notification_set_sound() failed.(%d)", __func__, noti_err);
172 notification_free(noti_h);
176 //TODO: Add vibration state checking routine.
177 noti_err = notification_set_vibration(noti_h, NOTIFICATION_VIBRATION_TYPE_DEFAULT, NULL);
178 if (noti_err != NOTIFICATION_ERROR_NONE)
180 LOGE("%s notification_set_vibration() failed.(%d)", __func__, noti_err);
181 notification_free(noti_h);
186 char *mainappid = NULL;
188 res = __get_pkg_mainappid(pkg_id, &mainappid);
191 LOGE("%s __get_pkg_mainappid() failed.(%d)", __func__, res);
193 SECURE_LOGI("%s launch pkg_id: %s, mainappid: %s", __func__, pkg_id, mainappid);
199 LOGE("%s bundle_create() failed.", __func__);
200 notification_free(noti_h);
204 appsvc_set_operation(b, "http://tizen.org/appcontrol/operation/view");
205 appsvc_set_appid(b, mainappid);
206 noti_err = notification_set_execute_option(noti_h, NOTIFICATION_EXECUTE_TYPE_SINGLE_LAUNCH, NULL, NULL, b);
213 if (noti_err != NOTIFICATION_ERROR_NONE)
215 LOGE("%s notification_set_execute_option() failed.", __func__);
216 notification_free(noti_h);
222 noti_err = notification_insert(noti_h, &__priv_id);
223 if (noti_err != NOTIFICATION_ERROR_NONE)
225 LOGE("%s notification_insert() failed.(%d)", __func__, noti_err);
226 notification_free(noti_h);
232 noti_err = notification_update(noti_h);
233 if (noti_err != NOTIFICATION_ERROR_NONE)
235 LOGE("%s notification_update() failed.(%d)", __func__, noti_err);
236 notification_free(noti_h);
244 int _install_cb(int req_id, const char *pkg_type,
245 const char *pkgid, const char *key,
246 const char *val, const void *pmsg, void *data)
248 LOGD("%s pkgmgr_handler()", __func__);
250 appdata *ad = (appdata *)data;
253 if (strcmp(key, "start") == 0)
256 snprintf(str, 256, "installing %s.%s ..", pkgid, pkg_type);
257 notification_status_message_post(str);
259 else if (strcmp(key, "install_percent") == 0)
262 int percent = atoi(val);
263 notification_update_progress(NULL, __priv_id, (double)(percent/100));
265 else if (strcmp(key, "end") == 0)
267 if(strcasecmp(val,"fail") == 0){
270 SECURE_LOGD("pkg_type(%s), pkgid(%s), key(%s), val(%s), label(%s), icon_path(%s) result(%d)"
271 , pkg_type, pkgid, key, val, ad->pkg_label, ad->pkg_icon_path, result);
273 char *pkg_icon_path = NULL;
274 int res = __get_pkg_icon(pkgid, &pkg_icon_path);
277 pkg_icon_path = strdup(DEFAULT_ICON);
280 res = __notify_notification(pkgid, ad->pkg_label, pkg_icon_path, result);
283 LOGE("%s __notify_notification() failed.(%d)", __func__, res);
290 int request_id = pkgmgr_client_free(__pc);
293 LOGE("%s pkgmgr_client_free() failed.(%d)", __func__, request_id);
308 int __install_package(const appdata *ad)
310 if (ad->file_path == NULL)
312 LOGE("%s file_path is NULL", __func__);
315 if (ad->pkg_label == NULL)
317 LOGE("%s pkg_label is NULL", __func__);
320 LOGD("%s Install file_path[%s] pkg_label[%s] ", __func__, ad->file_path, ad->pkg_label);
323 char *extension = NULL;
325 __pc = pkgmgr_client_new(PC_REQUEST);
328 LOGE("%s pkgmgr_client_new(PC_REQUEST) failed", __func__);
331 extension = __get_file_extension(ad->file_path);
332 LOGD("%s Install extension = [%s]", __func__, extension);
334 request_id = pkgmgr_client_install(__pc, extension, NULL, ad->file_path, NULL,
335 PM_QUIET, _install_cb, (void *)ad);
338 LOGE("%s pkgmgr_client_isntall fail(%d)", __func__, request_id);
346 static int __parse_argv(int argc, char **argv, appdata *ad)
348 LOGD("%s", __func__);
349 static bundle *b = NULL;
355 b = bundle_import_from_argv(argc, argv);
358 LOGD("%s bundle for bundle_import_from_argv is NULL", __func__);
362 if (bundle_get_val(b, KEY_PACKAGE_PATH))
364 ad->file_path = (char *)bundle_get_val(b, KEY_PACKAGE_PATH);
366 if (bundle_get_val(b, KEY_PACKAGE_LABEL))
368 ad->pkg_label = (char *)bundle_get_val(b, KEY_PACKAGE_LABEL);
370 if (bundle_get_val(b, KEY_PACKAGE_ICON_PATH))
372 ad->pkg_icon_path = (char *)bundle_get_val(b, KEY_PACKAGE_ICON_PATH);
377 LOGE("%s errno: %d(%s)", __func__, errno, strerror(errno));
384 static bool __service_create(void* data)
389 static void __service_terminate(void* data)
391 LOGD("%s", __func__);
395 static void __service_control(app_control_h app_control, void* data)
397 appdata *ad = (appdata *)data;
400 res = __install_package(ad);
403 LOGE("%s __install_package() failed.(%d)", __func__, res);
410 int main(int argc, char* argv[])
412 LOGD("service_app_main() is called");
415 service_app_lifecycle_callback_s callback = {0,};
417 callback.create = __service_create;
418 callback.terminate = __service_terminate;
419 callback.app_control = __service_control;
421 if (__parse_argv(argc, argv, &ad))
423 LOGE("%s Failed to parse argv()", __func__);
427 return service_app_main(argc, argv, &callback, &ad);