Update change log and spec for wrt-plugins-tizen_0.4.9
[platform/framework/web/wrt-plugins-tizen.git] / src / Application / AppManagerWrapper.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an AS IS BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 #include "AppManagerWrapper.h"
19
20 #include <dpl/log/log.h>
21 #include <dpl/singleton_impl.h>
22 #include <Commons/Exception.h>
23 #include <Commons/Regex.h>
24
25 // To get package id from appId
26 #include <package_manager.h>
27
28 IMPLEMENT_SINGLETON(DeviceAPI::Application::AppManagerWrapper)
29
30 namespace DeviceAPI {
31 namespace Application {
32
33 using namespace std;
34 using namespace WrtDeviceApis::Commons;
35
36
37 AppManagerWrapper::AppManagerWrapper() :
38                 m_registeredCallback(false),
39                 m_watchIdAcc(0)
40 {
41 }
42
43 AppManagerWrapper::~AppManagerWrapper()
44 {
45         if(m_registeredCallback)
46         {
47                 unregisterAppListChangedCallbacks();
48         }
49 }
50
51 void AppManagerWrapper::registerAppListChangedCallbacks(IAppManagerAppListChangedCallbacks *callbacks)
52 {
53         if(callbacks == NULL)
54         {
55                 LogWarning("Parameter is NULL.");
56                 return;
57         }
58
59         if(!m_registeredCallback)
60         {
61                 registerAppListChangedCallbacks();
62         }
63
64         m_callbacks.insert(callbacks);
65 }
66
67 void AppManagerWrapper::unregisterAppListChangedCallbacks(IAppManagerAppListChangedCallbacks *callbacks)
68 {
69         if(callbacks == NULL)
70         {
71                 LogWarning("Parameter is NULL.");
72                 return;
73         }
74
75         if(m_callbacks.size() == 0)
76         {
77                 LogWarning("No callbacks are registered.");
78                 return;
79         }
80
81         AppListChangedCallbacksSet::iterator iter = m_callbacks.find(callbacks);
82         if(iter == m_callbacks.end())
83         {
84                 LogWarning("Callbacks " << callbacks << " are not registered.");
85                 return;
86         }
87
88         m_callbacks.erase(iter);
89
90         if(m_callbacks.size() == 0)
91         {
92                 unregisterAppListChangedCallbacks();
93         }
94 }
95
96
97 long AppManagerWrapper::getWatchIdAndInc()
98 {
99         return ++m_watchIdAcc;
100 }
101
102 void AppManagerWrapper::appListChangedCallback(app_manger_event_type_e event_type,
103                                                const char *appId,
104                                                void *user_data)
105 {
106         if(user_data == NULL)
107         {
108                 LogWarning("app_manager listener passed NULL user_data");
109                 return;
110         }
111
112         AppManagerWrapper *appManager = (AppManagerWrapper *)user_data;
113
114         appManager->appListChangedCallback(event_type, appId);
115 }
116
117 void AppManagerWrapper::appListChangedCallback(app_manger_event_type_e event_type,
118                                                const char *appId)
119 {
120         if(appId == NULL)
121         {
122                 LogWarning("app_manager listener gave a NULL package name.");
123                 return;
124         }
125
126         switch(event_type)
127         {
128         case APP_MANAGER_EVENT_INSTALLED:
129                 appListAppInstalled(appId);
130                 break;
131         case APP_MANAGER_EVENT_UNINSTALLED:
132                 appListAppUninstalled(appId);
133                 break;
134         case APP_MANAGER_EVENT_UPDATED:
135                 appListAppUpdated(appId);
136                 break;
137         default:
138                 LogWarning("app_manager listener gave wrong event_type : " << event_type);
139                 break;
140         }
141 }
142
143 void AppManagerWrapper::appListAppInstalled(const char *appId)
144 {
145         LogDebug("app installed : " << appId);
146
147         AppListChangedCallbacksSet::iterator iter = m_callbacks.begin();
148         for(; iter != m_callbacks.end(); iter++)
149         {
150                 (*iter)->onAppManagerEventInstalled(appId);
151         }
152 }
153
154 void AppManagerWrapper::appListAppUninstalled(const char *appId)
155 {
156         LogDebug("app uninstalled : " << appId);
157
158         AppListChangedCallbacksSet::iterator iter = m_callbacks.begin();
159         for(; iter != m_callbacks.end(); iter++)
160         {
161                 (*iter)->onAppManagerEventUninstalled(appId);
162         }
163 }
164
165 void AppManagerWrapper::appListAppUpdated(const char *appId)    
166 {
167         LogDebug("app updated : " << appId);
168
169         AppListChangedCallbacksSet::iterator iter = m_callbacks.begin();
170         for(; iter != m_callbacks.end(); iter++)
171         {
172                 (*iter)->onAppManagerEventUpdated(appId);
173         }
174 }
175
176 void AppManagerWrapper::registerAppListChangedCallbacks()
177 {
178         if(m_registeredCallback)
179         {
180                 LogWarning("Callback already registered.");
181                 return;
182         }
183
184         // AppManagerWrapper will be used as a singleton object,
185         // so this pointer of it can be passed as a parameter of a async function.
186         int result = app_manager_set_app_list_changed_cb(appListChangedCallback, this);
187         if (result != APP_MANAGER_ERROR_NONE)
188         {
189                 LogWarning("app_manager_set_app_list_changed_cb() returns APP_MANAGER_ERROR_INVALID_PARAMETER");
190
191                 ThrowMsg(InvalidArgumentException, "Error while registering listener to app_manager");
192         }
193
194         m_registeredCallback = true;
195 }
196
197 void AppManagerWrapper::unregisterAppListChangedCallbacks()
198 {
199         if(!m_registeredCallback)
200         {
201                 LogWarning("Callback already unregistered.");
202                 return;
203         }
204
205         app_manager_unset_app_list_changed_cb();
206
207         m_registeredCallback = false;
208 }
209
210 // @20130125-wscho: current pkgmanager has a problem while db operation.
211 // So, below function is temporal function. all attribute should be filled to appInfoObject
212 // when that is created.
213 // See ApplicationManager.cpp
214 bool AppManagerWrapper::initializeAppInfo(ApplicationInformationPtr &appInfo)
215 {
216         std::string appId = appInfo->getAppId();
217         char* packageId = NULL;
218         int ret = 0;
219
220         //LogDebug("InitializeAppInfo for " << appInfo->getAppId().c_str());
221
222         ret = package_manager_get_package_id_by_app_id(appId.c_str(), &packageId);
223         if (ret != PACKAGE_MANAGER_ERROR_NONE)
224         {
225                 LogDebug("Fail to get package id with appId : " << appId.c_str());
226         }
227
228 // Below code is already run on ApplicationManager.cpp
229 #if 0
230         char* name = NULL;
231         char* iconPath = NULL;  
232         bool noDisplay = false;
233         pkgmgrinfo_appinfo_h appinfo_h;
234
235         ret = pkgmgrinfo_appinfo_get_appinfo(appId.c_str(), &appinfo_h);
236         if (ret != PMINFO_R_OK) {
237                 LogDebug("Fail to get appinfo by " << appId.c_str());
238         }
239
240         ret = pkgmgrinfo_appinfo_get_label(appinfo_h, &name);
241         if (ret != PMINFO_R_OK) {
242                 LogDebug("Fail to get name by " << appId.c_str());
243         }
244         appInfo->setName(name);
245
246         ret = pkgmgrinfo_appinfo_get_icon(appinfo_h, &iconPath);
247         if (ret != PMINFO_R_OK) {
248                 LogDebug("Fail to get icon by " << appId.c_str());
249         }
250         appInfo->setIconPath(iconPath);
251
252         ret = pkgmgrinfo_appinfo_is_nodisplay(appinfo_h, &noDisplay);
253         if (ret != PMINFO_R_OK) {
254                 LogDebug("Fail to get nodisplay by " << appId.c_str());
255         }
256         appInfo->setShow(!noDisplay);
257
258         ret = pkgmgrinfo_appinfo_foreach_category(appinfo_h, category_cb, (void*)appInfo.Get());
259         if (ret < 0)
260         {
261                 LogDebug("Fail to get categories " << appId.c_str());
262         }
263         
264         ret = pkgmgrinfo_appinfo_destroy_appinfo(appinfo_h);
265         if (ret < 0)
266         {
267                 LogDebug("Fail to destroy " << appId.c_str());
268         }
269 #endif
270
271         char *version = NULL;
272         int total_size = 0;
273         int installed_time = 0;
274         pkgmgrinfo_pkginfo_h pkginfo_h;
275
276         ret = pkgmgrinfo_pkginfo_get_pkginfo(packageId, &pkginfo_h);
277         if (ret != PMINFO_R_OK) {
278                 LogDebug("Fail to get package info");
279         }
280
281         ret = pkgmgrinfo_pkginfo_get_version(pkginfo_h, &version);
282         if (ret != PMINFO_R_OK) {
283                 LogDebug("Fail to get version");
284         }
285         appInfo->setVersion(version);
286
287         ret = pkgmgrinfo_pkginfo_get_total_size(pkginfo_h, &total_size);
288         if (ret == PMINFO_R_OK) {
289                 appInfo->setInstallSize(total_size);
290         }
291
292         ret = pkgmgrinfo_pkginfo_get_installed_time(pkginfo_h, &installed_time);
293         if (ret == PMINFO_R_OK) {
294                 appInfo->setInstallDate(installed_time);
295         }
296
297         pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo_h);
298
299         appInfo->setInitialize();
300         return true;
301 }
302
303
304         
305 } // Application
306 } // DeviceAPI