Applied latest source code
[apps/native/preloaded/Installer.git] / InstallerService / src / InstallerService.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Flora License, Version 1.1 (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
7 //
8 //     http://floralicense.org/license/
9 //
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.
15 //
16
17 /**
18  * @file                InstallerService.cpp
19  * @brief               This file contains the declaration of InstallerService Class,
20  *                              which provides basic features necessary to define an ServiceApp.
21  */
22
23 #include <FApp.h>
24 #include <FGraphics.h>
25 #include <FLocales.h>
26 #include <FShell.h>
27 #include <FSystem.h>
28 #include <FUi.h>
29 #include "AppResourceId.h"
30 #include "InstallerService.h"
31
32 using namespace Tizen::App::Package;
33 using namespace Tizen::Base;
34 using namespace Tizen::Base::Collection;
35 using namespace Tizen::Graphics;
36 using namespace Tizen::Shell;
37 using namespace Tizen::System;
38 using namespace Tizen::Ui::Controls;
39
40 InstallerServiceApp::InstallerServiceApp(void)
41         : __pInfo(null)
42         , __pDegreeLabel(null)
43         , __pCountLabel(null)
44         , __pProgress(null)
45         , __pFrame(null)
46         , __filePathList(SingleObjectDeleter)
47         , __pDateTimeFormatter(null)
48         , __index(0)
49 {
50 }
51
52 InstallerServiceApp::~InstallerServiceApp(void)
53 {
54 }
55
56 Tizen::App::ServiceApp*
57 InstallerServiceApp::CreateInstance(void)
58 {
59         return new (std::nothrow) InstallerServiceApp();
60 }
61
62 bool
63 InstallerServiceApp::OnAppInitializing(Tizen::App::AppRegistry& appRegistry)
64 {
65         AppLogDebug("Enter");
66         Tizen::App::AppControlProviderManager::GetInstance()->SetAppControlProviderEventListener(this);
67
68         __filePathList.Construct();
69
70         __pFrame = new (std::nothrow) QuickPanelFrame();
71         __pFrame->Construct(IDL_QUICK_INSTALLATION);
72
73         return true;
74 }
75
76 bool
77 InstallerServiceApp::OnAppInitialized(void)
78 {
79         AppLogDebug("Enter");
80         TryReturn(__filePathList.GetCount() != 0, false, "The filePath is not exist.");
81
82         __pDateTimeFormatter = Tizen::Locales::DateTimeFormatter::CreateTimeFormatterN(Tizen::Locales::DATE_TIME_STYLE_NONE);
83         String pattern;
84         SettingInfo::GetValue(L"http://tizen.org/setting/locale.time.format", pattern);
85         if (pattern.Contains(L":ss"))
86         {
87                 pattern.Replace(L":ss", L"");
88         }
89         else if (pattern.Contains(L":SS"))
90         {
91                 pattern.Replace(L":SS", L"");
92         }
93         else if (pattern.Contains(L".ss"))
94         {
95                 pattern.Replace(L".ss", L"");
96         }
97         else if (pattern.Contains(L".SS"))
98         {
99                 pattern.Replace(L".SS", L"");
100         }
101         else if (pattern.Contains(L"-ss"))
102         {
103                 pattern.Replace(L"-ss", L"");
104         }
105         else if (pattern.Contains(L"-SS"))
106         {
107                 pattern.Replace(L"-SS", L"");
108         }
109         __pDateTimeFormatter->ApplyPattern(pattern);
110
111         __pCountLabel = static_cast<Label*>(__pFrame->GetControl(IDC_LABEL_COUNT));
112         __pDegreeLabel = static_cast<Label*>(__pFrame->GetControl(IDC_LABEL_DEGREE));
113         __pProgress = static_cast<Progress*>(__pFrame->GetControl(IDC_PROGRESS));
114
115         result r = Install(*static_cast<String*>(__filePathList.GetAt(__index)));
116         if (IsFailed(r))
117         {
118                 return false;
119         }
120
121         return true;
122 }
123
124 bool
125 InstallerServiceApp::OnAppTerminating(Tizen::App::AppRegistry& appRegistry, bool forcedTermination)
126 {
127         AppLogDebug("Enter");
128         delete __pFrame;
129         delete __pInfo;
130         delete __pDateTimeFormatter;
131         return true;
132 }
133
134 void
135 InstallerServiceApp::OnAppControlRequestReceived(RequestId reqId, const Tizen::Base::String& operationId,
136                 const Tizen::Base::String* pUriData, const Tizen::Base::String* pMimeType, const Tizen::Base::Collection::IMap* pExtraData)
137 {
138         AppLogDebug("Enter");
139         Tizen::App::AppId clientAppId = Tizen::App::AppControlProviderManager::GetInstance()->GetClientAppId(reqId);
140         TryReturnVoid(clientAppId == L"D7eOJquGtL.Installer", "[%ls] This application is not allowed.", clientAppId.GetPointer());
141         TryReturnVoid(pUriData && pUriData->StartsWith(L"file://", 0), "There isn't URI data.");
142
143         String filePath = *pUriData;
144         filePath.Replace(L"file://", L"");
145         __filePathList.Add(new (std::nothrow) String(filePath));
146
147         if (__pFrame->IsVisible())
148         {
149                 String count;
150                 count.Format(10, L"%d/%d", __index+1, __filePathList.GetCount());
151
152                 __pCountLabel->SetText(count);
153                 __pCountLabel->Invalidate(false);
154         }
155 }
156
157 void
158 InstallerServiceApp::OnPackageInstallationCompleted(const Tizen::App::PackageId& packageId,
159                 Tizen::App::Package::PackageInstallationResult installationResult)
160 {
161         AppLogDebug("Enter");
162         TryReturnVoid(packageId == __packageId, "PackageId: %ls", packageId.GetPointer());
163         PackageManager::GetInstance()->RemovePackageInstallationEventListener(*this);
164
165         Tizen::App::AppId mainAppId = __pInfo->GetMainAppId();
166         PackageAppInfo* pMainAppInfo = __pInfo->GetPackageAppInfoN(mainAppId);
167         String appName = __pInfo->GetDisplayName();
168
169         NotificationManager notificationManager;
170         notificationManager.Construct();
171
172         NotificationRequest request;
173         result r = request.SetIconFilePath(pMainAppInfo->GetAppMenuIconPath());
174         AppLogDebugIf(IsFailed(r), "[%s] Failed to set app icon.", GetErrorMessage(r));
175
176         if (!pMainAppInfo->IsMenuIconVisible())
177         {
178                 AppLogDebug("This app hasn't a menu icon.");
179                 mainAppId = L"";
180         }
181
182         delete pMainAppInfo;
183
184         String installTime;
185         DateTime curTime;
186         SystemTime::GetCurrentTime(TIME_MODE_WALL, curTime);
187         __pDateTimeFormatter->Format(curTime, installTime);
188         installTime.Insert(L"\n", 0);
189         installTime.Insert(appName, 0);
190
191         request.SetAlertText(installTime);
192
193         String titleText;
194
195         switch (installationResult)
196         {
197         case PACKAGE_INSTALLATION_RESULT_SUCCESS:
198                 AppLogDebug("PACKAGE_INSTALLATION_RESULT_SUCCESS");
199                 Tizen::App::AppResource::GetInstance()->GetString(L"IDS_APINST_POP_INSTALLED", titleText);
200                 break;
201         case PACKAGE_INSTALLATION_RESULT_STORAGE_FULL:
202                 AppLogDebug("PACKAGE_INSTALLATION_RESULT_STORAGE_FULL");
203                 Tizen::App::AppResource::GetInstance()->GetString(L"IDS_APINST_BODY_UNABLE_TO_INSTALL_NOT_ENOUGH_MEMORY_ABB", titleText);
204                 break;
205         case PACKAGE_INSTALLATION_RESULT_INVALID_PACKAGE:
206                 AppLogDebug("PACKAGE_INSTALLATION_RESULT_INVALID_PACKAGE");
207                 Tizen::App::AppResource::GetInstance()->GetString(L"IDS_APINST_POP_FAILED", titleText);
208                 break;
209         }
210
211         request.SetTitleText(titleText);
212
213         if (installationResult != PACKAGE_INSTALLATION_RESULT_SUCCESS)
214         {
215                 String* pFilePath = dynamic_cast<String*>(__filePathList.GetAt(__index));
216                 if (pFilePath)
217                 {
218                         request.SetAppMessage(*pFilePath);
219                 }
220                 mainAppId = L"D7eOJquGtL.Installer";
221         }
222
223         if (mainAppId.IsEmpty())
224         {
225                 r = notificationManager.Notify(request);
226                 AppLogExceptionIf(IsFailed(r), "[%s] Failed to notify message.", GetErrorMessage(r));
227         }
228         else
229         {
230                 r = notificationManager.NotifyByAppId(mainAppId, request);
231                 AppLogExceptionIf(IsFailed(r), "[%s] Failed to notify message.", GetErrorMessage(r));
232         }
233
234         String* pFilePath = null;
235         do
236         {
237                 pFilePath = dynamic_cast<String*>(__filePathList.GetAt(++__index));
238                 if (pFilePath)
239                 {
240                         r = Install(*pFilePath);
241                         if (r == E_SUCCESS)
242                         {
243                                 return;
244                         }
245                 }
246         } while (pFilePath);
247
248         __pFrame->SetShowState(false);
249         Terminate();
250 }
251
252 void
253 InstallerServiceApp::OnPackageInstallationInProgress(const Tizen::App::PackageId& packageId, int progress)
254 {
255         AppLogDebug("progress: %d", progress);
256         TryReturnVoid(packageId == __packageId, "PackageId: %ls", packageId.GetPointer());
257
258         if (__pProgress->GetValue() != progress)
259         {
260                 __pProgress->SetValue(progress);
261                 __pProgress->Invalidate(false);
262                 __pDegreeLabel->SetText(Integer::ToString(progress) + L"%");
263                 __pDegreeLabel->Invalidate(false);
264         }
265 }
266
267 void
268 InstallerServiceApp::OnPackageUninstallationCompleted(const Tizen::App::PackageId& packageId, bool uninstallationResult)
269 {
270         AppLogDebug("Enter");
271         TryReturnVoid(packageId == __packageId, "PackageId: %ls", packageId.GetPointer());
272 }
273
274 result
275 InstallerServiceApp::Install(const Tizen::Base::String& filePath)
276 {
277         AppLogDebug("Enter");
278         NotificationManager notificationManager;
279         String count;
280
281         delete __pInfo;
282         __pInfo = PackageManager::GetInstance()->GetPackageInfoFromFileN(filePath);
283         TryReturn(__pInfo, GetLastResult(), "[%s] Failed to get the packageInfo. - %ls", GetErrorMessage(GetLastResult()), filePath.GetPointer());
284         __packageId = __pInfo->GetId();
285
286         PackageAppInfo* pMainAppInfo = __pInfo->GetPackageAppInfoN(__pInfo->GetMainAppId());
287         Bitmap* pAppIcon = pMainAppInfo->GetAppMenuIconN();
288         delete pMainAppInfo;
289
290         String appName = __pInfo->GetDisplayName();
291
292         if (pAppIcon)
293         {
294                 Label* pIconLabel = static_cast<Label*>(__pFrame->GetControl(IDC_LABEL_ICON));
295                 pIconLabel->SetBackgroundBitmap(*pAppIcon);
296                 delete pAppIcon;
297         }
298
299         Label* pNameLabel = static_cast<Label*>(__pFrame->GetControl(IDC_LABEL_NAME));
300         pNameLabel->SetText(appName);
301
302         String notifyTextMsg;
303         Tizen::App::AppResource::GetInstance()->GetString(L"IDS_APINST_BODY_INSTALLING_PS_ING", notifyTextMsg);
304         notifyTextMsg.Format(notifyTextMsg.GetLength() + appName.GetLength(), notifyTextMsg.GetPointer(), appName.GetPointer());
305
306         PackageManager::GetInstance()->AddPackageInstallationEventListener(*this);
307         result r = PackageManager::GetInstance()->InstallPackage(__packageId, filePath, null);
308         TryCatch(r == E_SUCCESS, , "[%s] Failed to install package.", GetErrorMessage(r));
309
310         notificationManager.Construct();
311         notificationManager.NotifyTextMessage(notifyTextMsg);
312
313         count.Format(10, L"%d/%d", __index+1, __filePathList.GetCount());
314         __pCountLabel->SetText(count);
315         __pCountLabel->Invalidate(false);
316
317         __pProgress->SetValue(0);
318         __pDegreeLabel->SetText(L"0%");
319
320         __pFrame->Invalidate(true);
321         __pFrame->SetShowState(true);
322         __pFrame->Show();
323
324         return r;
325
326 CATCH:
327         delete __pInfo;
328         __pInfo = null;
329         return r;
330 }