2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 * @file FAppAppResource.cpp
20 * @brief This is the implementation for the AppResource class.
23 #include <unique_ptr.h>
25 #include <FBaseResult.h>
26 #include <FAppAppResource.h>
28 #include <FBaseSysLog.h>
30 #include <FSec_AccessController.h>
31 #include "FApp_AppResourceImpl.h"
33 using namespace Tizen::Base;
34 using namespace Tizen::Base::Collection;
35 using namespace Tizen::Graphics;
36 using namespace Tizen::Security;
38 namespace Tizen { namespace App
41 Tizen::Base::Collection::HashMap* AppResource::pContainer = null;
43 AppResource::AppResource(void)
44 : __pAppResourceImpl(null)
49 AppResource::~AppResource(void)
51 delete __pAppResourceImpl;
56 AppResource::Construct(void)
58 SysAssertf(__pAppResourceImpl == null,
59 "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
61 __pAppResourceImpl = new (std::nothrow) _AppResourceImpl();
62 SysTryReturnResult(NID_APP, __pAppResourceImpl != null, E_OUT_OF_MEMORY, "Allocation failed.");
64 result r = __pAppResourceImpl->Construct();
65 SysTryReturn(NID_APP, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
73 AppResource::GetString(const String& resourceId, String& loadedString) const
75 SysAssertf(__pAppResourceImpl != null, "Not yet constructed. Construct() should be called before use.");
77 return __pAppResourceImpl->GetString(resourceId, loadedString);
82 AppResource::GetBitmapN(const String& imgFilePath, BitmapPixelFormat pixelFormat) const
84 SysAssertf(__pAppResourceImpl != null, "Not yet constructed. Construct() should be called before use.");
86 return __pAppResourceImpl->GetBitmapN(imgFilePath, pixelFormat);
91 AppResource::GetBitmapN(const String& imgFilePath) const
93 SysAssertf(__pAppResourceImpl != null, "Not yet constructed. Construct() should be called before use.");
95 return __pAppResourceImpl->GetBitmapN(imgFilePath);
100 AppResource::GetInstance(void)
102 result r = E_SUCCESS;
103 static AppResource* pAppResource = null;
105 if (pAppResource == null)
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.");
110 r = pAppResource->Construct();
111 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Initialization of app resource failed.", GetErrorMessage(r));
126 AppResource::GetInstanceByAppId(const AppId& appId)
128 result r = E_SUCCESS;
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.");
133 SysTryReturn(NID_APP, _Aul::IsInstalled(appId) == true, null, E_APP_NOT_INSTALLED, "Application is not installed.", GetErrorMessage(E_APP_NOT_INSTALLED));
135 if (pContainer == null)
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));
141 r = pContainerMap->Construct();
142 SysTryReturn(NID_APP, !IsFailed(r), null, E_SYSTEM, "[E_SYSTEM] Creating the container is failed [%s].", GetErrorMessage(r));
144 pContainer = pContainerMap.release();
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));
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));
156 if(hasAppResource == true)
159 return static_cast<AppResource*> (pContainer->GetValue(packageId));
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));
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));
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));
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));
189 AppResource::ReleaseInstanceByAppId(const AppId& appId)
191 result r = E_SUCCESS;
192 AppResource* pAppResource = null;
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.");
197 SysTryReturnResult(NID_APP, _Aul::IsInstalled(appId) == true, E_OBJ_NOT_FOUND, "Applicationis not installed.");
199 if (pContainer != null)
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));
206 pAppResource = static_cast<AppResource*> (pContainer->GetValue(packageId));
208 r = pContainer->Remove(packageId);
215 AppResource::Construct(const AppId& appId)
217 SysAssertf(__pAppResourceImpl == null,
218 "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
220 __pAppResourceImpl = new (std::nothrow) _AppResourceImpl();
221 SysTryReturnResult(NID_APP, __pAppResourceImpl != null, E_OUT_OF_MEMORY, "Allocation failed.");
223 result r = __pAppResourceImpl->Construct(appId);
224 SysTryReturn(NID_APP, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
231 AppResource::Reinitialize(void)
233 if (pContainer != null)
235 IMapEnumerator* pMapEnum = pContainer->GetMapEnumeratorN();
236 while(pMapEnum->MoveNext() == E_SUCCESS)
238 AppResource* pAppResource = static_cast<AppResource*> (pMapEnum->GetValue());
239 String* pStr = static_cast<String*> (pMapEnum->GetKey());
240 pAppResource->Reinitialize(*pStr);
246 AppResource::Reinitialize(const AppId& appId)
248 __pAppResourceImpl->Reinitialize(appId);