b01221528a0c0eb622f618082a2e020102301471
[platform/core/convergence/service-adaptor.git] / server / sal_observer.c
1 /*
2  * Service Adaptor
3  *
4  * Copyright (c) 2014 - 2015 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 #include <stdio.h>
21 #include <glib.h>
22
23 #include <tizen.h>
24 #include <app_info.h>
25 #include <package_manager.h>
26
27 #include "sal.h"
28 #include "sal_observer.h"
29
30 //******************************************************************************
31 //* Global variables and defines
32 //******************************************************************************
33 #define SAL_PLUGIN_METADATA_KEY_ADAPTOR "service-adaptor"
34
35 //******************************************************************************
36 //* Private interface
37 //******************************************************************************
38
39 //******************************************************************************
40 //* Private interface definition
41 //******************************************************************************
42
43 /**
44  * @brief callback of app info
45  *
46  * @return      void.
47  */
48 static bool _sal_app_meta_cb(const char *key, const char *value, void *user_data)
49 {
50         SAL_FN_CALL;
51
52         char **app_meta = (char **) user_data;
53
54         if ((NULL != key) && (NULL != value))
55         {
56                 if (0 == strncmp(SAL_PLUGIN_METADATA_KEY_ADAPTOR, key, strlen(SAL_PLUGIN_METADATA_KEY_ADAPTOR)))
57                 {
58                         app_meta[0] = strdup(value);
59                         SAL_INFO("SAL_PLUGIN_METADATA_KEY_ADAPTOR: %s", value);
60                 }
61         }
62
63         return true;
64 }
65
66 /**
67  * @brief callback of app info
68  *
69  * @return      void.
70  */
71 static bool _sal_app_info_cb(package_info_app_component_type_e comp_type, const char *app_id, void *user_data)
72 {
73         SAL_FN_CALL;
74
75         app_info_h app_info = NULL;
76         int ret = PACKAGE_MANAGER_ERROR_NONE;
77         char *pkg_path = (char *) user_data;
78         char **app_meta = (char **) g_malloc0(sizeof(char *) * 1);
79
80         (void) pkg_path;
81
82         app_info_create(app_id, &app_info);
83         ret = app_info_foreach_metadata(app_info, _sal_app_meta_cb, (void *) app_meta);
84         RETVM_IF(PACKAGE_MANAGER_ERROR_NONE != ret, false, "app_info_foreach_metadata() Fail(%d)", ret);
85
86         app_info_destroy(app_info);
87
88         // TODO: using app_meta
89
90         SAL_FREE(app_meta);
91
92         return true;
93 }
94
95 /**
96  * @brief callback of package manager
97  *
98  * @return      void.
99  */
100 static void _sal_package_event_cb(const char *type,
101                 const char *package,
102                 package_manager_event_type_e event_type,
103                 package_manager_event_state_e event_state,
104                 int progress,
105                 package_manager_error_e error,
106                 void *user_data)
107 {
108         SAL_FN_CALL;
109
110         if ((PACKAGE_MANAGER_EVENT_TYPE_INSTALL == event_type)
111                         && (PACKAGE_MANAGER_EVENT_STATE_COMPLETED == event_state))
112         {
113                 int ret = PACKAGE_MANAGER_ERROR_NONE;
114                 package_info_h p_info = NULL;
115
116                 ret = package_manager_get_package_info(package, &p_info);
117                 RETM_IF(PACKAGE_MANAGER_ERROR_NONE != ret, "package_manager_get_package_info() Fail(%d)", ret);
118
119                 char *path = NULL;
120                 ret = package_info_get_root_path(p_info, &path);
121                 RETM_IF(PACKAGE_MANAGER_ERROR_NONE != ret, "package_info_get_root_path() Fail(%d)", ret);
122
123                 ret = package_info_foreach_app_from_package(p_info, PACKAGE_INFO_ALLAPP, _sal_app_info_cb, path);
124                 RETM_IF(PACKAGE_MANAGER_ERROR_NONE != ret, "package_info_foreach_app_from_package() Fail(%d)", ret);
125
126                 ret = package_info_destroy(p_info);
127                 RETM_IF(PACKAGE_MANAGER_ERROR_NONE != ret, "package_info_destroy() Fail(%d)", ret);
128
129                 SAL_FREE(path);
130         }
131
132         SAL_FN_END;
133 }
134
135 //******************************************************************************
136 //* Public interface definition
137 //******************************************************************************
138
139 /**
140  * @brief start observer using package manager
141  *
142  * @return      void.
143  */
144 service_adaptor_error_e sal_observer_start()
145 {
146         int ret = 0;
147         package_manager_h package = NULL;
148
149         package_manager_create(&package);
150
151         ret = package_manager_set_event_cb(package, _sal_package_event_cb, NULL);
152         RETVM_IF(PACKAGE_MANAGER_ERROR_NONE != ret, SERVICE_ADAPTOR_ERROR_SYSTEM, "package_manager_set_event_cb() Fail(%d)", ret);
153
154         return SERVICE_ADAPTOR_ERROR_NONE;
155 }