sync with tizen_2.0
[platform/framework/native/appfw.git] / src / app / FAppAppResource.cpp
1 //
2 // Open Service Platform
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 /**
19  * @file        FAppAppResource.cpp
20  * @brief       This is the implementation for the AppResource class.
21  */
22
23 #include <unique_ptr.h>
24
25 #include <FBaseResult.h>
26 #include <FAppAppResource.h>
27
28 #include <FBaseSysLog.h>
29 #include "FApp_Aul.h"
30 #include <FSec_AccessController.h>
31 #include "FApp_AppResourceImpl.h"
32
33 using namespace Tizen::Base;
34 using namespace Tizen::Base::Collection;
35 using namespace Tizen::Graphics;
36 using namespace Tizen::Security;
37
38 namespace Tizen { namespace App
39 {
40
41 Tizen::Base::Collection::HashMap* AppResource::pContainer = null;
42
43 AppResource::AppResource(void)
44         : __pAppResourceImpl(null)
45 {
46 }
47
48
49 AppResource::~AppResource(void)
50 {
51         delete __pAppResourceImpl;
52 }
53
54
55 result
56 AppResource::Construct(void)
57 {
58         SysAssertf(__pAppResourceImpl == null,
59                         "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
60
61         __pAppResourceImpl = new (std::nothrow) _AppResourceImpl();
62         SysTryReturnResult(NID_APP, __pAppResourceImpl != null, E_OUT_OF_MEMORY, "Allocation failed.");
63
64         result r = __pAppResourceImpl->Construct();
65         SysTryReturn(NID_APP, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
66
67         return E_SUCCESS;
68
69 }
70
71
72 result
73 AppResource::GetString(const String& resourceId, String& loadedString) const
74 {
75         SysAssertf(__pAppResourceImpl != null, "Not yet constructed. Construct() should be called before use.");
76
77         return __pAppResourceImpl->GetString(resourceId, loadedString);
78 }
79
80
81 Bitmap*
82 AppResource::GetBitmapN(const String& imgFilePath, BitmapPixelFormat pixelFormat) const
83 {
84         SysAssertf(__pAppResourceImpl != null, "Not yet constructed. Construct() should be called before use.");
85
86         return __pAppResourceImpl->GetBitmapN(imgFilePath, pixelFormat);
87 }
88
89
90 Bitmap*
91 AppResource::GetBitmapN(const String& imgFilePath) const
92 {
93         SysAssertf(__pAppResourceImpl != null, "Not yet constructed. Construct() should be called before use.");
94
95         return __pAppResourceImpl->GetBitmapN(imgFilePath);
96 }
97
98
99 AppResource*
100 AppResource::GetInstance(void)
101 {
102         result r = E_SUCCESS;
103         static AppResource* pAppResource = null;
104
105         if (pAppResource == null)
106         {
107                 pAppResource = new (std::nothrow) AppResource();
108                 SysTryCatch(NID_APP, pAppResource != null, , r = E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] app resource allocation failed.");
109
110                 r = pAppResource->Construct();
111                 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Initialization of app resource failed.", GetErrorMessage(r));
112         }
113
114         SetLastResult(r);
115         return pAppResource;
116
117 CATCH:
118         delete pAppResource;
119         pAppResource = null;
120
121         SetLastResult(r);
122         return pAppResource;
123 }
124
125 AppResource*
126 AppResource::GetInstanceByAppId(const AppId& appId)
127 {
128         result r = E_SUCCESS;
129         
130         r = _AccessController::CheckUserPrivilege(_PRV_APPSETTING);
131         SysTryReturn(NID_APP, r == E_SUCCESS, null, E_PRIVILEGE_DENIED, "[E_PRIVILEGE_DENIED] The application does not have the privilege to call this method.");
132
133         SysTryReturn(NID_APP, _Aul::IsInstalled(appId) == true, null, E_APP_NOT_INSTALLED, "Application is not installed.", GetErrorMessage(E_APP_NOT_INSTALLED));
134
135         if (pContainer == null)
136         {
137                 std::unique_ptr< HashMap > pContainerMap(new (std::nothrow) HashMap());
138                 SysTryReturn(NID_APP, pContainerMap != null,
139                                 null, E_OUT_OF_MEMORY, "[%s] Creating the container is failed.", GetErrorMessage(E_OUT_OF_MEMORY));
140
141                 r = pContainerMap->Construct();
142                 SysTryReturn(NID_APP, !IsFailed(r), null, E_SYSTEM, "[E_SYSTEM] Creating the container is failed [%s].", GetErrorMessage(r));
143
144                 pContainer = pContainerMap.release();
145         }
146
147         String packageId;
148         r = appId.SubString(0, 10, packageId);
149         SysTryReturn(NID_APP, !IsFailed(r), null,
150                         E_APP_NOT_INSTALLED, "[%s] Failed to get package info", GetErrorMessage(E_APP_NOT_INSTALLED));
151
152         bool hasAppResource = false;
153         r = pContainer->ContainsKey(packageId, hasAppResource);
154         SysTryReturn(NID_APP, !IsFailed(r), null, r, "[%s] Checking to contain is failed.", GetErrorMessage(r));
155
156         if(hasAppResource == true)
157         {
158                 SetLastResult(r);
159                 return static_cast<AppResource*> (pContainer->GetValue(packageId));
160         }
161         else
162         {
163                 std::unique_ptr< String > pStr(new (std::nothrow) String(packageId));
164                 SysTryReturn(NID_APP, pStr != null, null,
165                                 E_OUT_OF_MEMORY, "[%s] Creating a key is failed.", GetErrorMessage(E_OUT_OF_MEMORY));
166
167                 AppResource* pAppResource = new (std::nothrow) AppResource;
168                 SysTryReturn(NID_APP, pAppResource != null, null,
169                                 E_OUT_OF_MEMORY, "[%s] Unable to allocate memory for AppResource", GetErrorMessage(E_OUT_OF_MEMORY));
170
171                 r = pAppResource->Construct(packageId);
172                 SysTryCatch(NID_APP, !IsFailed(r), ,
173                                 E_SYSTEM, "[E_SYSTEM] Adding an element to the container is failed [%s].", GetErrorMessage(r));
174
175                 r = pContainer->Add(*pStr, *pAppResource);
176                 SysTryCatch(NID_APP, !IsFailed(r), ,
177                                 E_SYSTEM, "[E_SYSTEM] Adding an element to the container is failed [%s].", GetErrorMessage(r));
178
179                 SetLastResult(r);
180                 pStr.release();
181                 return pAppResource;
182 CATCH:
183                 delete pAppResource;
184                 return null;
185         }
186 }
187
188 result
189 AppResource::ReleaseInstanceByAppId(const AppId& appId)
190 {
191         result r = E_SUCCESS;
192         AppResource* pAppResource = null;
193
194         r = _AccessController::CheckUserPrivilege(_PRV_APPSETTING);
195         SysTryReturn(NID_APP, r == E_SUCCESS, E_PRIVILEGE_DENIED, E_PRIVILEGE_DENIED, "[E_PRIVILEGE_DENIED] The application does not have the privilege to call this method.");
196
197         SysTryReturnResult(NID_APP, _Aul::IsInstalled(appId) == true, E_OBJ_NOT_FOUND, "Applicationis not installed.");
198
199         if (pContainer != null)
200         {
201                 String packageId;
202                 r = appId.SubString(0, 10, packageId);
203                 SysTryReturn(NID_APP, !IsFailed(r), E_OBJ_NOT_FOUND,
204                                 E_OBJ_NOT_FOUND, "[%s] Failed to get package info", GetErrorMessage(E_OBJ_NOT_FOUND));
205
206                 pAppResource = static_cast<AppResource*> (pContainer->GetValue(packageId));
207                 delete pAppResource;
208                 r = pContainer->Remove(packageId);
209         }
210
211         return r;
212 }
213
214 result
215 AppResource::Construct(const AppId& appId)
216 {
217         SysAssertf(__pAppResourceImpl == null,
218                         "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
219
220         __pAppResourceImpl = new (std::nothrow) _AppResourceImpl();
221         SysTryReturnResult(NID_APP, __pAppResourceImpl != null, E_OUT_OF_MEMORY, "Allocation failed.");
222
223         result r = __pAppResourceImpl->Construct(appId);
224         SysTryReturn(NID_APP, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
225
226         return E_SUCCESS;
227
228 }
229
230 void
231 AppResource::Reinitialize(void)
232 {
233         if (pContainer != null)
234         {
235                 IMapEnumerator* pMapEnum = pContainer->GetMapEnumeratorN();
236                 while(pMapEnum->MoveNext() == E_SUCCESS)
237                 {
238                         AppResource* pAppResource = static_cast<AppResource*> (pMapEnum->GetValue());
239                         String* pStr = static_cast<String*> (pMapEnum->GetKey());
240                         pAppResource->Reinitialize(*pStr);
241                 }
242         }
243 }
244
245 void
246 AppResource::Reinitialize(const AppId& appId)
247 {
248         __pAppResourceImpl->Reinitialize(appId);
249 }
250 } } // Tizen::App