Merge "Enhance fault tolerance for the deleted DataControl instance (N_SE-41877)...
[platform/framework/native/appfw.git] / src / app / FApp_AppResourceImpl.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Apache License, Version 2.0 (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://www.apache.org/licenses/LICENSE-2.0
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        FApp_AppResourceImpl.cpp
19  * @brief       This is the implementation for the _AppResourceImpl class.
20  */
21
22 #include <unique_ptr.h>
23
24 #include <FBaseResult.h>
25 #include <FBaseSysLog.h>
26 #include <FBaseColHashMap.h>
27 #include <FBaseRt_LibraryImpl.h>
28
29 #include <FAppAppResource.h>
30 #include "FApp_AppManagerImpl.h"
31 #include "FApp_AppResourceString.h"
32 #include "FApp_IAppResourceBitmap.h"
33
34 #include "FApp_AppResourceImpl.h"
35
36
37 using namespace Tizen::Base;
38 using namespace Tizen::Base::Collection;
39 using namespace Tizen::Base::Runtime;
40 using namespace Tizen::Graphics;
41 using namespace Tizen::Io;
42
43
44 namespace Tizen { namespace App
45 {
46
47 #define BITMAP_PIXEL_FORMAT_INVALID (BITMAP_PIXEL_FORMAT_MIN)
48
49
50 static _IAppResourceBitmap*(* p_Create_IAppResourceBitmapInstanceN)(int type, const String& value) = null;
51
52 Tizen::Base::Collection::HashMap* _AppResourceImpl::__pContainer = null;
53
54 _AppResourceImpl::_AppResourceImpl(void)
55         : __p_AppResourceString(null)
56         , __p_IAppResourceBitmap(null)
57         , __type(APP_RESOURCE_DEFAULT)
58         , __value()
59 {
60 }
61
62 result
63 _AppResourceImpl::ReleaseInstance(const Tizen::Base::String& key)
64 {
65         if (__pContainer != null)
66         {
67                 AppResource* pAppResource = static_cast<AppResource*> (__pContainer->GetValue(key));
68                 delete pAppResource;
69
70                 return __pContainer->Remove(key);
71         }
72
73         return E_SUCCESS;
74 }
75
76 // Exception: E_OUT_OF_MEMORY, E_SYSTEM, E_INVALID_ARG, E_OBJ_NOT_FOUND, E_APP_NOT_INSTALLED [Get_AppResourceStringN], Get_IAppResourceBitmapN
77 AppResource*
78 _AppResourceImpl::GetInstanceN(AppResourceBy type, const Tizen::Base::String& value)
79 {
80         result r = E_SUCCESS;
81         if (__pContainer == null)
82         {
83                 std::unique_ptr< HashMap > pContainer(new (std::nothrow) HashMap());
84                 SysTryReturn(NID_APP, pContainer != null,
85                                 null, E_OUT_OF_MEMORY, "[%s] Creating the container is failed.", GetErrorMessage(E_OUT_OF_MEMORY));
86
87                 r = pContainer->Construct();
88                 SysTryReturn(NID_APP, !IsFailed(r), null, E_SYSTEM, "[E_SYSTEM] Creating the container is failed [%s].", GetErrorMessage(r));
89
90                 __pContainer = pContainer.release();
91         }
92
93         bool hasAppResource = false;
94         r = __pContainer->ContainsKey(value, hasAppResource);
95         SysTryReturn(NID_APP, !IsFailed(r), null, r, "[%s] Checking to contain is failed.", GetErrorMessage(r));
96
97         if (hasAppResource)
98         {
99                 return static_cast<AppResource*> (__pContainer->GetValue(value));
100         }
101         else
102         {
103                 std::unique_ptr< _AppResourceImpl > pAppResourceImpl(new (std::nothrow) _AppResourceImpl());
104                 SysTryReturn(NID_APP, pAppResourceImpl != null,
105                                 null, E_OUT_OF_MEMORY, "[%s] Unable to allocate memory for AppResourceImpl.", GetErrorMessage(E_OUT_OF_MEMORY));
106
107                 std::unique_ptr< _AppResourceString > pAppResourceString(_AppResourceString::Get_AppResourceStringN(type, value));
108                 r = GetLastResult();
109                 SysTryReturn(NID_APP, !IsFailed(r), null, r, "[%s] Unable to get _AppResourceString.", GetErrorMessage(r));
110
111                 std::unique_ptr< _IAppResourceBitmap > pAppResourceBitmap(Get_IAppResourceBitmapN(type, value));
112                 r = GetLastResult();
113                 SysTryReturn(NID_APP, !IsFailed(r), null, r, "[%s] Unable to get _IAppResourceBitmap.", GetErrorMessage(r));
114
115                 pAppResourceImpl->__p_AppResourceString = pAppResourceString.release();
116                 pAppResourceImpl->__p_IAppResourceBitmap = pAppResourceBitmap.release();
117                 pAppResourceImpl->__type = type;
118                 pAppResourceImpl->__value = value;
119
120                 std::unique_ptr< String > pStr(new (std::nothrow) String(value));
121                 SysTryReturn(NID_APP, pStr != null, null,
122                                 E_OUT_OF_MEMORY, "[%s] Creating a key is failed.", GetErrorMessage(E_OUT_OF_MEMORY));
123
124                 AppResource* pAppResource = new (std::nothrow) AppResource;
125                 SysTryReturn(NID_APP, pAppResource != null, null,
126                                 E_OUT_OF_MEMORY, "[%s] Unable to allocate memory for AppResource", GetErrorMessage(E_OUT_OF_MEMORY));
127
128                 pAppResource->__pAppResourceImpl = pAppResourceImpl.release();
129                 r = __pContainer->Add(*pStr, *pAppResource);
130                 if (IsFailed(r))
131                 {
132                         delete pAppResource;
133                         SysTryReturn(NID_APP, false, null,
134                                 E_SYSTEM, "[E_SYSTEM] Adding an element to the container is failed [%s].", GetErrorMessage(r));
135                 }
136
137                 pStr.release();
138                 return pAppResource;
139         }
140         return null;
141 }
142
143 _IAppResourceBitmap*
144 _AppResourceImpl::Get_IAppResourceBitmapN(AppResourceBy type, const Tizen::Base::String& value)
145 {
146         if (p_Create_IAppResourceBitmapInstanceN == null)
147         {
148                 _LibraryImpl& lib = _AppManagerImpl::GetInstance()->GetUiLibraryImpl();
149                 p_Create_IAppResourceBitmapInstanceN =
150                                 reinterpret_cast<_IAppResourceBitmap*(*)(int, const String&)>(lib.GetProcAddress(L"_Create_IAppResourceBitmapInstanceN"));
151                 SysTryReturn(NID_APP, p_Create_IAppResourceBitmapInstanceN != null,
152                                 null, E_SYSTEM, "[E_SYSTEM] Failed to get Bitmap resource [%s].", GetErrorMessage(E_SYSTEM));
153         }
154
155         std::unique_ptr< _IAppResourceBitmap > pAppResourceBitmap(p_Create_IAppResourceBitmapInstanceN(type, value));
156         SysTryReturn(NID_APP, pAppResourceBitmap != null, null,
157                         E_SYSTEM, "[%s] Unable to get IAppResourceBitmap.", GetErrorMessage(E_SYSTEM));
158
159         return pAppResourceBitmap.release();
160 }
161
162 _AppResourceImpl::~_AppResourceImpl(void)
163 {
164         delete __p_AppResourceString;
165         delete __p_IAppResourceBitmap;
166 }
167
168 result
169 _AppResourceImpl::GetString(const String& resourceId, String& loadedString)
170 {
171         if (__p_AppResourceString == null)
172         {
173                 __p_AppResourceString = _AppResourceString::Get_AppResourceStringN(__type, __value);
174                 SysTryReturnResult(NID_APP, __p_AppResourceString == null, E_FAILURE, "Failed to get String resource.");
175         }
176
177         return __p_AppResourceString->GetString(resourceId, loadedString);
178 }
179
180 Bitmap*
181 _AppResourceImpl::GetBitmapN(const String& imgFilePath, BitmapPixelFormat pixelFormat)
182 {
183         if (__p_IAppResourceBitmap == null)
184         {
185                 __p_IAppResourceBitmap = Get_IAppResourceBitmapN(__type, __value);
186                 SysTryReturn(NID_APP, __p_IAppResourceBitmap == null,
187                                 null, E_SYSTEM, "[%s] Failed to get Bitmap resource.", GetErrorMessage(E_SYSTEM));
188         }
189
190         return __p_IAppResourceBitmap->GetBitmapN(imgFilePath, pixelFormat);
191 }
192
193 Bitmap*
194 _AppResourceImpl::GetBitmapN(const String& imgFilePath)
195 {
196         return GetBitmapN(imgFilePath, BITMAP_PIXEL_FORMAT_INVALID);
197 }
198
199 _AppResourceImpl*
200 _AppResourceImpl::GetInstance(void)
201 {
202         AppResource* pAppResource = AppResource::GetInstance();
203         if (pAppResource)
204         {
205                 return pAppResource->__pAppResourceImpl;
206         }
207         return null;
208 }
209
210 void
211 _AppResourceImpl::Reinitialize(void)
212 {
213         _AppResourceImpl* pThis = _AppResourceImpl::GetInstance();
214         if (pThis == null)
215         {
216                 SysLog(NID_APP, "No _AppResourceImpl instance.");
217                 return;
218         }
219
220         if (pThis->__pContainer != null)
221         {
222                 std::unique_ptr< IMapEnumerator > pMapEnum(pThis->__pContainer->GetMapEnumeratorN());
223                 if (pMapEnum)
224                 {
225                         while(pMapEnum->MoveNext() == E_SUCCESS)
226                         {
227                                 AppResource* pAppResource = static_cast<AppResource*> (pMapEnum->GetValue());
228                                 if (pAppResource && pAppResource->__pAppResourceImpl)
229                                 {
230                                         _AppResourceImpl* pAppResourceImpl = pAppResource->__pAppResourceImpl;
231                                         delete pAppResourceImpl->__p_AppResourceString;
232                                         pAppResourceImpl->__p_AppResourceString =
233                                                         _AppResourceString::Get_AppResourceStringN(pAppResourceImpl->__type, pAppResourceImpl->__value);
234
235                                         // This function will be called on Language change event
236                                         // Need not to re-initialize Bitmap resource as language change does not affect bitmap resource path
237                                 }
238                         }
239                 }
240         }
241 }
242
243 Bitmap*
244 _AppResourceImpl::GetNonScalingBitmapN(const Tizen::Base::String& imagePath)
245 {
246         _IAppResourceBitmap* pAppResourceBitmap = Tizen::App::_AppResourceImpl::Get_IAppResourceBitmapN(APP_RESOURCE_BITMAP);
247
248         if (pAppResourceBitmap != null)
249         {
250                 return pAppResourceBitmap->GetBitmapN(imagePath, BITMAP_PIXEL_FORMAT_INVALID, false);
251         }
252         else
253         {
254                 SysLog(NID_APP, "Getting pAppResourceBitmap instance is failed");
255                 return null;
256         }
257 }
258
259 } } // Tizen::App