fixed the prevent issue CID:48477
[platform/framework/native/appfw.git] / src / app / FApp_AppResourceImpl.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        FApp_AppResourceImpl.cpp
20  * @brief       This is the implementation for the _AppResourceImpl class.
21  */
22
23 #include <unique_ptr.h>
24
25 #include <FBaseResult.h>
26 #include <FBaseSysLog.h>
27 #include <FBaseColHashMap.h>
28 #include <FBaseRt_LibraryImpl.h>
29
30 #include <FAppAppResource.h>
31 #include "FApp_AppManagerImpl.h"
32 #include "FApp_AppResourceString.h"
33 #include "FApp_IAppResourceBitmap.h"
34
35 #include "FApp_AppResourceImpl.h"
36
37
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;
43
44
45 namespace Tizen { namespace App
46 {
47
48 #define BITMAP_PIXEL_FORMAT_INVALID (BITMAP_PIXEL_FORMAT_MIN)
49
50
51 static _IAppResourceBitmap*(* p_Create_IAppResourceBitmapInstanceN)(int type, const String& value) = null;
52
53 Tizen::Base::Collection::HashMap* _AppResourceImpl::__pContainer = null;
54
55 _AppResourceImpl::_AppResourceImpl(void)
56         : __p_AppResourceString(null)
57         , __p_IAppResourceBitmap(null)
58         , __type(APP_RESOURCE_DEFAULT)
59         , __value()
60 {
61 }
62
63 result
64 _AppResourceImpl::ReleaseInstance(const Tizen::Base::String& key)
65 {
66         if (__pContainer != null)
67         {
68                 AppResource* pAppResource = static_cast<AppResource*> (__pContainer->GetValue(key));
69                 delete pAppResource;
70
71                 return __pContainer->Remove(key);
72         }
73
74         return E_SUCCESS;
75 }
76
77 // Exception: E_OUT_OF_MEMORY, E_SYSTEM, E_INVALID_ARG, E_OBJ_NOT_FOUND, E_APP_NOT_INSTALLED [Get_AppResourceStringN], Get_IAppResourceBitmapN
78 AppResource*
79 _AppResourceImpl::GetInstanceN(AppResourceBy type, const Tizen::Base::String& value)
80 {
81         result r = E_SUCCESS;
82         if (__pContainer == null)
83         {
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));
87
88                 r = pContainer->Construct();
89                 SysTryReturn(NID_APP, !IsFailed(r), null, E_SYSTEM, "[E_SYSTEM] Creating the container is failed [%s].", GetErrorMessage(r));
90
91                 __pContainer = pContainer.release();
92         }
93
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));
97
98         if(hasAppResource == true)
99         {
100                 return static_cast<AppResource*> (__pContainer->GetValue(value));
101         }
102         else
103         {
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));
107
108                 std::unique_ptr< _AppResourceString > pAppResourceString(_AppResourceString::Get_AppResourceStringN(type, value));
109                 r = GetLastResult();
110                 SysTryReturn(NID_APP, !IsFailed(r), null, r, "[%s] Unable to get _AppResourceString.", GetErrorMessage(r));
111
112                 std::unique_ptr< _IAppResourceBitmap > pAppResourceBitmap(Get_IAppResourceBitmapN(type, value));
113                 r = GetLastResult();
114                 SysTryReturn(NID_APP, !IsFailed(r), null, r, "[%s] Unable to get _IAppResourceBitmap.", GetErrorMessage(r));
115
116                 pAppResourceImpl->__p_AppResourceString = pAppResourceString.release();
117                 pAppResourceImpl->__p_IAppResourceBitmap = pAppResourceBitmap.release();
118                 pAppResourceImpl->__type = type;
119                 pAppResourceImpl->__value = value;
120
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));
124
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));
128
129                 pAppResource->__pAppResourceImpl = pAppResourceImpl.release();
130                 r = __pContainer->Add(*pStr, *pAppResource);
131                 if (IsFailed(r))
132                 {
133                         delete pAppResource;
134                         SysTryReturn(NID_APP, false, null,
135                                 E_SYSTEM, "[E_SYSTEM] Adding an element to the container is failed [%s].", GetErrorMessage(r));
136                 }
137
138                 pStr.release();
139                 return pAppResource;
140         }
141         return null;
142 }
143
144 _IAppResourceBitmap*
145 _AppResourceImpl::Get_IAppResourceBitmapN(AppResourceBy type, const Tizen::Base::String& value)
146 {
147         if (p_Create_IAppResourceBitmapInstanceN == null)
148         {
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));
154         }
155
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));
159
160         return pAppResourceBitmap.release();
161 }
162
163 _AppResourceImpl::~_AppResourceImpl(void)
164 {
165         delete __p_AppResourceString;
166         delete __p_IAppResourceBitmap;
167 }
168
169 result
170 _AppResourceImpl::GetString(const String& resourceId, String& loadedString)
171 {
172         if (__p_AppResourceString == null)
173         {
174                 __p_AppResourceString = _AppResourceString::Get_AppResourceStringN(__type, __value);
175                 SysTryReturnResult(NID_APP, __p_AppResourceString == null, E_FAILURE, "Failed to get String resource.");
176         }
177
178         return __p_AppResourceString->GetString(resourceId, loadedString);
179 }
180
181 Bitmap*
182 _AppResourceImpl::GetBitmapN(const String& imgFilePath, BitmapPixelFormat pixelFormat)
183 {
184         if (__p_IAppResourceBitmap == null)
185         {
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));
189         }
190
191         return __p_IAppResourceBitmap->GetBitmapN(imgFilePath, pixelFormat);
192 }
193
194 Bitmap*
195 _AppResourceImpl::GetBitmapN(const String& imgFilePath)
196 {
197         return GetBitmapN(imgFilePath, BITMAP_PIXEL_FORMAT_INVALID);
198 }
199
200 _AppResourceImpl*
201 _AppResourceImpl::GetInstance(void)
202 {
203         AppResource* pAppResource = AppResource::GetInstance();
204         if (pAppResource)
205         {
206                 return pAppResource->__pAppResourceImpl;
207         }
208         return null;
209 }
210
211 void
212 _AppResourceImpl::Reinitialize(void)
213 {
214         _AppResourceImpl* pThis = _AppResourceImpl::GetInstance();
215         if (pThis == null)
216         {
217                 SysLog(NID_APP, "No _AppResourceImpl instance.");
218                 return;
219         }
220
221         if (pThis->__pContainer != null)
222         {
223                 std::unique_ptr< IMapEnumerator > pMapEnum(pThis->__pContainer->GetMapEnumeratorN());
224                 if (pMapEnum)
225                 {
226                         while(pMapEnum->MoveNext() == E_SUCCESS)
227                         {
228                                 AppResource* pAppResource = static_cast<AppResource*> (pMapEnum->GetValue());
229                                 if (pAppResource && pAppResource->__pAppResourceImpl)
230                                 {
231                                         _AppResourceImpl* pAppResourceImpl = pAppResource->__pAppResourceImpl;
232                                         delete pAppResourceImpl->__p_AppResourceString;
233                                         pAppResourceImpl->__p_AppResourceString =
234                                                         _AppResourceString::Get_AppResourceStringN(pAppResourceImpl->__type, pAppResourceImpl->__value);
235
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
238                                 }
239                         }
240                 }
241         }
242 }
243
244 Bitmap*
245 _AppResourceImpl::GetNonScalingBitmapN(const Tizen::Base::String& imagePath)
246 {
247         _IAppResourceBitmap* pAppResourceBitmap = Tizen::App::_AppResourceImpl::Get_IAppResourceBitmapN(APP_RESOURCE_BITMAP);
248
249         if (pAppResourceBitmap != null)
250         {
251                 return pAppResourceBitmap->GetBitmapN(imagePath, BITMAP_PIXEL_FORMAT_INVALID);
252         }
253         else
254         {
255                 SysLog(NID_APP, "Getting pAppResourceBitmap instance is failed");
256                 return null;
257         }
258 }
259
260 } } // Tizen::App