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 FApp_AppResourceImpl.cpp
20 * @brief This is the implementation for the _AppResourceImpl class.
23 #include <unique_ptr.h>
25 #include <FBaseResult.h>
26 #include <FBaseSysLog.h>
27 #include <FBaseColHashMap.h>
28 #include <FBaseRt_LibraryImpl.h>
30 #include <FAppAppResource.h>
31 #include "FApp_AppManagerImpl.h"
32 #include "FApp_AppResourceString.h"
33 #include "FApp_IAppResourceBitmap.h"
35 #include "FApp_AppResourceImpl.h"
38 using namespace Tizen::Base;
39 using namespace Tizen::Base::Collection;
40 using namespace Tizen::Base::Runtime;
41 using namespace Tizen::Graphics;
42 using namespace Tizen::Io;
45 namespace Tizen { namespace App
48 #define BITMAP_PIXEL_FORMAT_INVALID (BITMAP_PIXEL_FORMAT_MIN)
51 static _IAppResourceBitmap*(* p_Create_IAppResourceBitmapInstanceN)(int type, const String& value) = null;
53 Tizen::Base::Collection::HashMap* _AppResourceImpl::__pContainer = null;
55 _AppResourceImpl::_AppResourceImpl(void)
56 : __p_AppResourceString(null)
57 , __p_IAppResourceBitmap(null)
58 , __type(APP_RESOURCE_DEFAULT)
64 _AppResourceImpl::ReleaseInstance(const Tizen::Base::String& key)
66 if (__pContainer != null)
68 AppResource* pAppResource = static_cast<AppResource*> (__pContainer->GetValue(key));
71 return __pContainer->Remove(key);
77 // Exception: E_OUT_OF_MEMORY, E_SYSTEM, E_INVALID_ARG, E_OBJ_NOT_FOUND, E_APP_NOT_INSTALLED [Get_AppResourceStringN], Get_IAppResourceBitmapN
79 _AppResourceImpl::GetInstanceN(AppResourceBy type, const Tizen::Base::String& value)
82 if (__pContainer == null)
84 std::unique_ptr< HashMap > pContainer(new (std::nothrow) HashMap());
85 SysTryReturn(NID_APP, pContainer != null,
86 null, E_OUT_OF_MEMORY, "[%s] Creating the container is failed.", GetErrorMessage(E_OUT_OF_MEMORY));
88 r = pContainer->Construct();
89 SysTryReturn(NID_APP, !IsFailed(r), null, E_SYSTEM, "[E_SYSTEM] Creating the container is failed [%s].", GetErrorMessage(r));
91 __pContainer = pContainer.release();
94 bool hasAppResource = false;
95 r = __pContainer->ContainsKey(value, hasAppResource);
96 SysTryReturn(NID_APP, !IsFailed(r), null, r, "[%s] Checking to contain is failed.", GetErrorMessage(r));
98 if(hasAppResource == true)
100 return static_cast<AppResource*> (__pContainer->GetValue(value));
104 std::unique_ptr< _AppResourceImpl > pAppResourceImpl(new (std::nothrow) _AppResourceImpl());
105 SysTryReturn(NID_APP, pAppResourceImpl != null,
106 null, E_OUT_OF_MEMORY, "[%s] Unable to allocate memory for AppResourceImpl.", GetErrorMessage(E_OUT_OF_MEMORY));
108 std::unique_ptr< _AppResourceString > pAppResourceString(_AppResourceString::Get_AppResourceStringN(type, value));
110 SysTryReturn(NID_APP, !IsFailed(r), null, r, "[%s] Unable to get _AppResourceString.", GetErrorMessage(r));
112 std::unique_ptr< _IAppResourceBitmap > pAppResourceBitmap(Get_IAppResourceBitmapN(type, value));
114 SysTryReturn(NID_APP, !IsFailed(r), null, r, "[%s] Unable to get _IAppResourceBitmap.", GetErrorMessage(r));
116 pAppResourceImpl->__p_AppResourceString = pAppResourceString.release();
117 pAppResourceImpl->__p_IAppResourceBitmap = pAppResourceBitmap.release();
118 pAppResourceImpl->__type = type;
119 pAppResourceImpl->__value = value;
121 std::unique_ptr< String > pStr(new (std::nothrow) String(value));
122 SysTryReturn(NID_APP, pStr != null, null,
123 E_OUT_OF_MEMORY, "[%s] Creating a key is failed.", GetErrorMessage(E_OUT_OF_MEMORY));
125 AppResource* pAppResource = new (std::nothrow) AppResource;
126 SysTryReturn(NID_APP, pAppResource != null, null,
127 E_OUT_OF_MEMORY, "[%s] Unable to allocate memory for AppResource", GetErrorMessage(E_OUT_OF_MEMORY));
129 pAppResource->__pAppResourceImpl = pAppResourceImpl.release();
130 r = __pContainer->Add(*pStr, *pAppResource);
134 SysTryReturn(NID_APP, false, null,
135 E_SYSTEM, "[E_SYSTEM] Adding an element to the container is failed [%s].", GetErrorMessage(r));
145 _AppResourceImpl::Get_IAppResourceBitmapN(AppResourceBy type, const Tizen::Base::String& value)
147 if (p_Create_IAppResourceBitmapInstanceN == null)
149 _LibraryImpl& lib = _AppManagerImpl::GetInstance()->GetUiLibraryImpl();
150 p_Create_IAppResourceBitmapInstanceN =
151 reinterpret_cast<_IAppResourceBitmap*(*)(int, const String&)>(lib.GetProcAddress(L"_Create_IAppResourceBitmapInstanceN"));
152 SysTryReturn(NID_APP, p_Create_IAppResourceBitmapInstanceN != null,
153 null, E_SYSTEM, "[E_SYSTEM] Failed to get Bitmap resource [%s].", GetErrorMessage(E_SYSTEM));
156 std::unique_ptr< _IAppResourceBitmap > pAppResourceBitmap(p_Create_IAppResourceBitmapInstanceN(type, value));
157 SysTryReturn(NID_APP, pAppResourceBitmap != null, null,
158 E_SYSTEM, "[%s] Unable to get IAppResourceBitmap.", GetErrorMessage(E_SYSTEM));
160 return pAppResourceBitmap.release();
163 _AppResourceImpl::~_AppResourceImpl(void)
165 delete __p_AppResourceString;
166 delete __p_IAppResourceBitmap;
170 _AppResourceImpl::GetString(const String& resourceId, String& loadedString)
172 if (__p_AppResourceString == null)
174 __p_AppResourceString = _AppResourceString::Get_AppResourceStringN(__type, __value);
175 SysTryReturnResult(NID_APP, __p_AppResourceString == null, E_FAILURE, "Failed to get String resource.");
178 return __p_AppResourceString->GetString(resourceId, loadedString);
182 _AppResourceImpl::GetBitmapN(const String& imgFilePath, BitmapPixelFormat pixelFormat)
184 if (__p_IAppResourceBitmap == null)
186 __p_IAppResourceBitmap = Get_IAppResourceBitmapN(__type, __value);
187 SysTryReturn(NID_APP, __p_IAppResourceBitmap == null,
188 null, E_SYSTEM, "[%s] Failed to get Bitmap resource.", GetErrorMessage(E_SYSTEM));
191 return __p_IAppResourceBitmap->GetBitmapN(imgFilePath, pixelFormat);
195 _AppResourceImpl::GetBitmapN(const String& imgFilePath)
197 return GetBitmapN(imgFilePath, BITMAP_PIXEL_FORMAT_INVALID);
201 _AppResourceImpl::GetInstance(void)
203 AppResource* pAppResource = AppResource::GetInstance();
206 return pAppResource->__pAppResourceImpl;
212 _AppResourceImpl::Reinitialize(void)
214 _AppResourceImpl* pThis = _AppResourceImpl::GetInstance();
217 SysLog(NID_APP, "No _AppResourceImpl instance.");
221 if (pThis->__pContainer != null)
223 std::unique_ptr< IMapEnumerator > pMapEnum(pThis->__pContainer->GetMapEnumeratorN());
226 while(pMapEnum->MoveNext() == E_SUCCESS)
228 AppResource* pAppResource = static_cast<AppResource*> (pMapEnum->GetValue());
229 if (pAppResource && pAppResource->__pAppResourceImpl)
231 _AppResourceImpl* pAppResourceImpl = pAppResource->__pAppResourceImpl;
232 delete pAppResourceImpl->__p_AppResourceString;
233 pAppResourceImpl->__p_AppResourceString =
234 _AppResourceString::Get_AppResourceStringN(pAppResourceImpl->__type, pAppResourceImpl->__value);
236 // This function will be called on Language change event
237 // Need not to re-initialize Bitmap resource as language change does not affect bitmap resource path