Tizen 2.1 base
[framework/osp/uifw.git] / src / ui / FUi_ResourceManager.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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 #include <dlfcn.h>
18 #include <pthread.h>
19 #include <FBaseInteger.h>
20 #include <FBaseByteBuffer.h>
21 #include <FBaseColIMap.h>
22 #include <FBaseColArrayListT.h>
23 #include <FBaseSysLog.h>
24 #include <FIoDirectory.h>
25 #include <FIoFile.h>
26 #include <FGrpBitmap.h>
27 #include <FGrpDimension.h>
28 #include <FGrpColor.h>
29 #include <FMediaImageTypes.h>
30 #include <FApp_AppInfo.h>
31 #include <FAppPkg_PackageManagerImpl.h>
32 #include <FIo_DirectoryImpl.h>
33 #include <FGrp_BitmapImpl.h>
34 #include <FGrp_CanvasImpl.h>
35 #include <FGrp_CoordinateSystem.h>
36 #include <FMedia_ImageDecoder.h>
37 #include <FSys_SystemInfoImpl.h>
38 #include "FUi_ControlManager.h"
39 #include "FUi_ResourceManager.h"
40 #include "FUi_ResourceStringLoader.h"
41 #include "FUi_ResourceConfigLoader.h"
42 #include "FUi_ResourceConfigParser.h"
43 #include "FUi_ResourceMapContainer.h"
44
45 using namespace Tizen::App;
46 using namespace Tizen::App::Package;
47 using namespace Tizen::Base;
48 using namespace Tizen::Base::Collection;
49 using namespace Tizen::Io;
50 using namespace Tizen::Graphics;
51 using namespace Tizen::Media;
52 using namespace Tizen::Ui::_Resource;
53
54 namespace
55 {
56 const int DEFAULT_SCREEN_WIDTH = 720;
57 const int DEFAULT_SCREEN_HEIGHT = 1280;
58
59 const int DPI_FOR_XHIGH = 290;
60 const int DPI_FOR_HIGH = 200;
61 const int DPI_FOR_MIDDLE = 150;
62 const int DPI_FOR_LOW = 0;
63
64 template<typename T> class ResourceFallbackItemComparer
65         : public IComparerT <T>
66 {
67 public:
68         ResourceFallbackItemComparer(void) {}
69         virtual ~ResourceFallbackItemComparer(void) {}
70         virtual result Compare(const T& obj1, const T& obj2, int& cmp) const
71         {
72                 if (obj1->scaleFactor > obj2->scaleFactor)
73                 {
74                         cmp = 1;
75                         return E_SUCCESS;
76                 }
77                 else if (obj1->scaleFactor < obj2->scaleFactor)
78                 {
79                         cmp = -1;
80                         return E_SUCCESS;
81                 }
82                 else
83                 {
84                         cmp = 0;
85                         return E_SUCCESS;
86                 }
87         }
88 };
89
90 MediaPixelFormat ConvertBitmapPixelFormatToMediaPixelFormat(BitmapPixelFormat format)
91 {
92         MediaPixelFormat out = MEDIA_PIXEL_FORMAT_NONE;
93         switch(format)
94         {
95                 case BITMAP_PIXEL_FORMAT_RGB565:
96                         out = MEDIA_PIXEL_FORMAT_RGB565LE;
97                         break;
98                 case BITMAP_PIXEL_FORMAT_ARGB8888:
99                         out = MEDIA_PIXEL_FORMAT_BGRA8888;
100                         break;
101                 case BITMAP_PIXEL_FORMAT_R8G8B8A8:
102                         out = MEDIA_PIXEL_FORMAT_RGBA8888;
103                         break;
104                 default:
105                         break;
106         }
107         return out;
108 }
109 }
110
111 namespace Tizen { namespace Graphics
112 {
113 result
114 _CreateCoordinateTransformer(_ICoordinateSystemTransformer*& pTransformer, int srcResolution, _BaseScreenSize srcBaseScreenSize, Dimension destResolution, _BaseScreenSize destBaseScreenSize);
115 }}
116
117 namespace Tizen { namespace Ui
118 {
119
120 Color _GetDefaultBackgroundColor(void)
121 {
122         Color color;
123         _ResourceManager::GetInstance()->GetColor(L"DEFAULTCOLORTABLE::background", color);
124         return color;
125 }
126 Color _GetDefaultForegroundColor(void)
127 {
128         Color color;
129         _ResourceManager::GetInstance()->GetColor(L"DEFAULTCOLORTABLE::foreground", color);
130         return color;
131 }
132
133 _ResourceManager* pRsrcMgr = null;
134
135 _ResourceManager*
136 _ResourceManager::GetInstance(void)
137 {
138         static pthread_once_t onceBlock = PTHREAD_ONCE_INIT;
139
140         if (pRsrcMgr == null)
141         {
142                 pthread_once(&onceBlock, CreateInstance);
143         }
144         return pRsrcMgr;
145 }
146 void
147 _ResourceManager::CreateInstance(void)
148 {
149         static _ResourceManager pManager;
150         pRsrcMgr = &pManager;
151         _CanvasImpl::SetThemeInfoCallback(_GetDefaultForegroundColor, _GetDefaultBackgroundColor);
152 }
153
154 _ResourceManager::_ResourceManager(void)
155         : __pStringLoader(null)
156         , __pTransformer(null)
157         , __appBasePortraitMode("")
158         , __appBaseLandscapeMode("")
159         , __targetPortraitMode("")
160         , __targetLandscapeMode("")
161         , __defaultPortraitMode(L"720x1280")
162         , __defaultLandscapeMode(L"1280x720")
163         , __systemTheme(L"")
164         , __userTheme(L"")
165         , __appliedUserTheme(false)
166         , __appBaseWidth(-1)
167         , __appBaseHeight(-1)
168         , __targetWidth(0)
169         , __targetHeight(0)
170         , __deviceDPI(0)
171         , __pFallbackList(null)
172 {
173         __pMapContainer[MAP_CONTAINER_TYPE_APP_BASE_PORTRAIT] = null;
174         __pMapContainer[MAP_CONTAINER_TYPE_APP_BASE_LANDSCAPE] = null;
175         __pMapContainer[MAP_CONTAINER_TYPE_DEFAULT_PORTRAIT] = null;
176         __pMapContainer[MAP_CONTAINER_TYPE_DEFAULT_LANDSCAPE] = null;
177
178         int appBaseWidth = DEFAULT_SCREEN_WIDTH;
179         int appBaseHeight = DEFAULT_SCREEN_HEIGHT;
180
181         result r = E_SUCCESS;
182
183         int appLogicalResolution = _CoordinateSystem::GetInstance()->GetLogicalResolutionInt();
184
185         if (_CreateCoordinateTransformer(__pTransformer,DEFAULT_SCREEN_WIDTH, BASE_SCREEN_SIZE_NORMAL, Dimension(appLogicalResolution, appLogicalResolution), BASE_SCREEN_SIZE_NORMAL) != E_SUCCESS)
186         {
187
188                 SysAssert(0);
189         }
190
191         r = Tizen::System::_SystemInfoImpl::GetSysInfo(L"http://tizen.org/feature/screen.dpi", __deviceDPI);
192         SysTryReturn(NID_UI, r == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] system error occurred");
193
194         switch(appLogicalResolution)
195         {
196                 case 240:
197                         appBaseWidth = 240;
198                         appBaseHeight = 400;
199                         break;
200                 case 320:
201                         appBaseWidth = 320;
202                         appBaseHeight = 480;
203                         break;
204                 case 480:
205                         appBaseWidth = 480;
206                         appBaseHeight = 800;
207                         break;
208                 case 720:
209                         appBaseWidth = 720;
210                         appBaseHeight = 1280;
211                         break;
212                 default:
213                         r = Tizen::System::_SystemInfoImpl::GetSysInfo("http://tizen.org/feature/screen.width", appBaseWidth);
214                         SysTryReturn(NID_UI, r == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] system error occurred");
215                         r = Tizen::System::_SystemInfoImpl::GetSysInfo("http://tizen.org/feature/screen.height", appBaseHeight);
216                         SysTryReturn(NID_UI, r == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] system error occurred");
217                         break;
218         }
219         __appBaseWidth = appBaseWidth;
220         __appBaseHeight = appBaseHeight;
221
222         __appBasePortraitMode.Append(appBaseWidth);
223         __appBasePortraitMode.Append(L"x");
224         __appBasePortraitMode.Append(appBaseHeight);
225
226         __appBaseLandscapeMode.Append(appBaseHeight);
227         __appBaseLandscapeMode.Append(L"x");
228         __appBaseLandscapeMode.Append(appBaseWidth);
229
230         int _width = 0;
231         int _height = 0;
232         r = Tizen::System::_SystemInfoImpl::GetSysInfo("http://tizen.org/feature/screen.width", _width);
233         SysTryReturn(NID_UI, r == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] system error occurred");
234         r = Tizen::System::_SystemInfoImpl::GetSysInfo("http://tizen.org/feature/screen.height", _height);
235         SysTryReturn(NID_UI, r == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] system error occurred");
236
237         __targetWidth = _width;
238         __targetHeight = _height;
239         __targetPortraitMode.Append(_width);
240         __targetPortraitMode.Append(L"x");
241         __targetPortraitMode.Append(_height);
242
243         __targetLandscapeMode.Append(_height);
244         __targetLandscapeMode.Append(L"x");
245         __targetLandscapeMode.Append(_width);
246
247         LoadThemeInformation(__systemTheme, __userTheme);
248
249         // theme 2.0
250         __pMapContainer[MAP_CONTAINER_TYPE_APP_BASE_PORTRAIT] = new (std::nothrow) MapContainer();
251         SysTryReturnVoidResult(NID_UI, __pMapContainer[MAP_CONTAINER_TYPE_APP_BASE_PORTRAIT], E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation is failed.");
252         __pMapContainer[MAP_CONTAINER_TYPE_APP_BASE_PORTRAIT]->SetResolution(__appBasePortraitMode);
253         if (!(InitializeTheme(*__pMapContainer[MAP_CONTAINER_TYPE_APP_BASE_PORTRAIT])))
254         {
255                 SysAssert(0);
256         }
257
258         __pStringLoader = new (std::nothrow) StringLoader();
259         SysTryReturnVoidResult(NID_UI, __pStringLoader, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation is failed.");
260         SetSystemColor();
261 }
262
263 bool
264 _ResourceManager::InitializeTheme(MapContainer& mapContainer)
265 {
266         bool r = false;
267         String themeFile(L"");
268
269         mapContainer.CreateMap(RESOURCE_TYPE_SHAPE);
270         mapContainer.CreateMap(RESOURCE_TYPE_FIXED_VALUE);
271         mapContainer.CreateMap(RESOURCE_TYPE_DIMENSION);
272         mapContainer.CreateMap(RESOURCE_TYPE_IMAGE);
273         mapContainer.CreateMap(RESOURCE_TYPE_COLOR);
274         mapContainer.SetResolution(__appBasePortraitMode);
275         themeFile = String(L"/usr/share/osp/themes/"+__systemTheme + ".xml");
276         ConfigParser* pParser = new (std::nothrow) ConfigParser();
277         SysTryReturn(NID_UI, pParser, false, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation is failed.");
278         r = pParser->Parse(themeFile, mapContainer);
279         LoadConfig(__appBasePortraitMode, mapContainer);
280
281         if (__userTheme.GetLength() > 0)
282         {
283                 themeFile = String(_AppInfo::GetAppRootPath()+L"res/themes/" +__userTheme + L"/" +__userTheme + ".xml");
284                 r = pParser->Parse(themeFile, mapContainer);
285         }
286
287         delete pParser;
288         return r;
289 }
290
291 void
292 _ResourceManager::LoadConfig(const String& resolution, MapContainer& mapContainer)
293 {
294         int count = ConfigLoader::GetInstance()->GetInitFuncCount();
295         for (int i = 0; i < count; i++)
296         {
297                 _Init_Func func = null;
298                 ConfigLoader::GetInstance()->GetInitFunc(i, func);
299                 if (func)
300                 {
301                         func(mapContainer, resolution);
302                 }
303         }
304 }
305 MapContainer*
306 _ResourceManager::GetMapContainer(MapContainerType type)
307 {
308         MapContainer* pContainer = null;
309         switch (type)
310         {
311                 case MAP_CONTAINER_TYPE_APP_BASE_PORTRAIT:
312                         pContainer = __pMapContainer[MAP_CONTAINER_TYPE_APP_BASE_PORTRAIT];
313                         break;
314                 case MAP_CONTAINER_TYPE_APP_BASE_LANDSCAPE:
315                         if (__pMapContainer[MAP_CONTAINER_TYPE_APP_BASE_LANDSCAPE])
316                         {
317                                 pContainer = __pMapContainer[MAP_CONTAINER_TYPE_APP_BASE_LANDSCAPE];
318                         }
319                         else
320                         {
321                                 pContainer = new (std::nothrow) MapContainer();
322                                 SysTryReturn(NID_UI, pContainer, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation is failed.");
323                                 pContainer->SetResolution(__appBaseLandscapeMode);
324                                 pContainer->CreateMap(RESOURCE_TYPE_SHAPE);
325                                 pContainer->CreateMap(RESOURCE_TYPE_DIMENSION);
326                                 pContainer->CreateMap(RESOURCE_TYPE_FIXED_VALUE);
327                                 LoadConfig(__appBaseLandscapeMode, *pContainer);
328                                 __pMapContainer[MAP_CONTAINER_TYPE_APP_BASE_LANDSCAPE] = pContainer;
329                         }
330                         break;
331                 case MAP_CONTAINER_TYPE_DEFAULT_PORTRAIT:
332                         if (__pMapContainer[MAP_CONTAINER_TYPE_DEFAULT_PORTRAIT])
333                         {
334                                 pContainer = __pMapContainer[MAP_CONTAINER_TYPE_DEFAULT_PORTRAIT];
335                         }
336                         else
337                         {
338                                 pContainer = new (std::nothrow) MapContainer();
339                                 SysTryReturn(NID_UI, pContainer, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation is failed.");
340                                 pContainer->SetResolution(__defaultPortraitMode);
341                                 pContainer->CreateMap(RESOURCE_TYPE_SHAPE);
342                                 pContainer->CreateMap(RESOURCE_TYPE_DIMENSION);
343                                 pContainer->CreateMap(RESOURCE_TYPE_FIXED_VALUE);
344                                 LoadConfig(__defaultPortraitMode, *pContainer);
345                                 __pMapContainer[MAP_CONTAINER_TYPE_DEFAULT_PORTRAIT] = pContainer;
346                         }
347                         break;
348                 case MAP_CONTAINER_TYPE_DEFAULT_LANDSCAPE:
349                         if (__pMapContainer[MAP_CONTAINER_TYPE_DEFAULT_LANDSCAPE])
350                         {
351                                 pContainer = __pMapContainer[MAP_CONTAINER_TYPE_DEFAULT_LANDSCAPE];
352                         }
353                         else
354                         {
355                                 pContainer = new (std::nothrow) MapContainer();
356                                 SysTryReturn(NID_UI, pContainer, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation is failed.");
357                                 pContainer->SetResolution(__defaultLandscapeMode);
358                                 pContainer->CreateMap(RESOURCE_TYPE_SHAPE);
359                                 pContainer->CreateMap(RESOURCE_TYPE_DIMENSION);
360                                 pContainer->CreateMap(RESOURCE_TYPE_FIXED_VALUE);
361                                 LoadConfig(__defaultLandscapeMode, *pContainer);
362                                 __pMapContainer[MAP_CONTAINER_TYPE_DEFAULT_LANDSCAPE] = pContainer;
363                         }
364                         break;
365                 default:
366                         break;
367         }
368         return pContainer;
369 }
370
371 _ResourceManager::~_ResourceManager(void)
372 {
373         delete __pStringLoader;
374         __pStringLoader = null;
375         delete __pTransformer;
376         __pTransformer = null;
377         delete __pFallbackList;
378         __pFallbackList = null;
379         delete __pMapContainer[MAP_CONTAINER_TYPE_APP_BASE_PORTRAIT];
380         __pMapContainer[MAP_CONTAINER_TYPE_APP_BASE_PORTRAIT] = null;
381         delete __pMapContainer[MAP_CONTAINER_TYPE_APP_BASE_LANDSCAPE];
382         __pMapContainer[MAP_CONTAINER_TYPE_APP_BASE_LANDSCAPE] = null;
383         delete __pMapContainer[MAP_CONTAINER_TYPE_DEFAULT_PORTRAIT];
384         __pMapContainer[MAP_CONTAINER_TYPE_DEFAULT_PORTRAIT] = null;
385         delete __pMapContainer[MAP_CONTAINER_TYPE_DEFAULT_LANDSCAPE];
386         __pMapContainer[MAP_CONTAINER_TYPE_DEFAULT_LANDSCAPE] = null;
387 }
388 void
389 _ResourceManager::ReloadTheme(Tizen::Base::String& themeName, bool userdefine)
390 {
391         if(userdefine)
392         {
393                 __userTheme = themeName;
394         }
395         else
396         {
397                 __systemTheme = themeName;
398         }
399 }
400
401 result
402 _ResourceManager::GetBitmapInternalN(const String& fileName, BitmapPixelFormat pixelFormat, _ControlOrientation orientation, Bitmap*& pBitmap)
403 {
404         result r = E_SYSTEM;
405         Bitmap* pTempBitmap = null;
406         pTempBitmap = new (std::nothrow) Bitmap;
407         SysTryReturn(NID_UI, pTempBitmap, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Propagating.", GetErrorMessage(r));
408
409         r = _BitmapImpl::GetInstance(*pTempBitmap)->Construct(fileName, pixelFormat);
410         SysTryCatch(NID_UI, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
411         pBitmap = pTempBitmap;
412         return r;
413
414 CATCH:
415         delete pTempBitmap;
416         return r;
417 }
418
419 result
420 _ResourceManager::GetBitmapN(const String& bitmapId, BitmapPixelFormat pixelFormat, _ControlOrientation orientation, Bitmap*& pBitmap)
421 {
422         result r = E_SYSTEM;
423         String* pFileName = null;
424         r = __pMapContainer[MAP_CONTAINER_TYPE_APP_BASE_PORTRAIT]->GetImageMap()->GetValue(bitmapId, pFileName);
425         if (r == E_SUCCESS)
426         {
427                 int foundFolderWidth = 0;
428                 String fullName = FindImagePath(*pFileName, foundFolderWidth);
429                 if(foundFolderWidth == __targetWidth)
430                 {
431                         r = GetBitmapInternalN(fullName, pixelFormat, orientation, pBitmap);
432                 }
433                 else
434                 {
435                         ByteBuffer* pImageBuffer = null;
436                         Bitmap* pTempBitmap = null;
437                         int imageWidth = 0;
438                         int imageHeight = 0;
439                         MediaPixelFormat format = ConvertBitmapPixelFormatToMediaPixelFormat(pixelFormat);
440                         pImageBuffer = _ImageDecoder::DecodeToBufferN(fullName, format, imageWidth, imageHeight);
441                         if (pImageBuffer)
442                         {
443                                 pTempBitmap = _BitmapImpl::GetNonScaledBitmapN(*pImageBuffer, Dimension(imageWidth, imageHeight), pixelFormat);
444                                 delete pImageBuffer;
445                         }
446                         if(pTempBitmap != null)
447                         {
448                                 if(pTempBitmap->IsNinePatchedBitmap())
449                                 {
450                                         pBitmap = pTempBitmap;
451                                         r = E_SUCCESS;
452                                 }
453                                 else
454                                 {
455                                         float scaleFactor = (float)((float)__targetWidth/(float)foundFolderWidth);
456
457                                         r = pTempBitmap->Scale(Dimension(imageWidth*scaleFactor, imageHeight*scaleFactor));
458                                         if (r == E_SUCCESS)
459                                         {
460                                                 pBitmap = pTempBitmap;
461                                         }
462                                         else
463                                         {
464                                                 delete pTempBitmap;
465                                         }
466                                 }
467                         }
468                 }
469         }
470         return r;
471 }
472
473 result
474 _ResourceManager::GetString(const String& stringId, String& string)
475 {
476         return __pStringLoader->GetString(stringId, string);
477 }
478
479 result
480 _ResourceManager::GetColor(const String& colorId, Color& color)
481 {
482         result r = E_SYSTEM;
483         Color* pColor = null;
484         r = __pMapContainer[MAP_CONTAINER_TYPE_APP_BASE_PORTRAIT]->GetColorMap()->GetValue(colorId, pColor);
485         if(r == E_SUCCESS)
486         {
487                 color = *pColor;
488         }
489         return r;
490 }
491
492 result
493 _ResourceManager::GetDimension(const String& dimensionId, _ControlOrientation orientation, Dimension& dimension)
494 {
495         result r = E_SUCCESS;
496         Dimension dim(0, 0);
497
498         Dimension* pDimension = null;
499         if (orientation == _CONTROL_ORIENTATION_PORTRAIT)
500         {
501                 r = GetMapContainer(MAP_CONTAINER_TYPE_APP_BASE_PORTRAIT)->GetDimensionMap()->GetValue(dimensionId, pDimension);
502                 if (r != E_SUCCESS)
503                 {
504                         r = GetMapContainer(MAP_CONTAINER_TYPE_APP_BASE_LANDSCAPE)->GetDimensionMap()->GetValue(dimensionId, pDimension);
505                 }
506         }
507         else
508         {
509                 r = GetMapContainer(MAP_CONTAINER_TYPE_APP_BASE_LANDSCAPE)->GetDimensionMap()->GetValue(dimensionId, pDimension);
510                 if (r != E_SUCCESS)
511                 {
512                         r = GetMapContainer(MAP_CONTAINER_TYPE_APP_BASE_PORTRAIT)->GetDimensionMap()->GetValue(dimensionId, pDimension);
513                 }
514         }
515         if (r == E_SUCCESS)
516         {
517                 dim = *pDimension;
518         }
519
520         if (r != E_SUCCESS)
521         {
522                 r = GetDefaultShapeWithScaling(dimensionId, orientation, dim);
523         }
524
525         if (r == E_SUCCESS)
526         {
527                 dimension = dim;
528         }
529         else
530         {
531                 return E_SYSTEM;
532         }
533
534         return r;
535 }
536
537 result
538 _ResourceManager::GetShape(const String& shapeId, _ControlOrientation orientation, int& value)
539 {
540         result r = E_SUCCESS;
541         Integer integer(-1);
542
543         Integer* pInteger = null;
544         if (orientation == _CONTROL_ORIENTATION_PORTRAIT)
545         {
546                 r = __pMapContainer[MAP_CONTAINER_TYPE_APP_BASE_PORTRAIT]->GetShapeMap()->GetValue(shapeId, pInteger);
547                 if (r != E_SUCCESS)
548                 {
549                         r = GetMapContainer(MAP_CONTAINER_TYPE_APP_BASE_LANDSCAPE)->GetShapeMap()->GetValue(shapeId, pInteger);
550                 }
551         }
552         else
553         {
554                 r = GetMapContainer(MAP_CONTAINER_TYPE_APP_BASE_LANDSCAPE)->GetShapeMap()->GetValue(shapeId, pInteger);
555                 if (r != E_SUCCESS)
556                 {
557                         r = __pMapContainer[MAP_CONTAINER_TYPE_APP_BASE_PORTRAIT]->GetShapeMap()->GetValue(shapeId, pInteger);
558                 }
559         }
560
561         if (r == E_SUCCESS)
562         {
563                 integer = *pInteger;
564         }
565
566         if (r != E_SUCCESS)
567         {
568                 r = GetDefaultShapeWithScaling(shapeId, orientation, integer);
569         }
570
571         if (r == E_SUCCESS)
572         {
573                 value = integer.ToInt();
574         }
575         else
576         {
577                 return E_SYSTEM;
578         }
579
580         return r;
581 }
582
583 result
584 _ResourceManager::GetFixedValue(const String& fixedValueId, _ControlOrientation orientation, int& value)
585 {
586         result r = E_SUCCESS;
587         Integer integer(-1);
588         Integer* pInteger = null;
589         if (orientation == _CONTROL_ORIENTATION_PORTRAIT)
590         {
591                 r = GetMapContainer(MAP_CONTAINER_TYPE_DEFAULT_PORTRAIT)->GetFixedValueMap()->GetValue(fixedValueId, pInteger);
592                 if (r != E_SUCCESS)
593                 {
594
595                         r = GetMapContainer(MAP_CONTAINER_TYPE_DEFAULT_LANDSCAPE)->GetFixedValueMap()->GetValue(fixedValueId, pInteger);
596                 }
597         }
598         else
599         {
600                 r = GetMapContainer(MAP_CONTAINER_TYPE_DEFAULT_LANDSCAPE)->GetFixedValueMap()->GetValue(fixedValueId, pInteger);
601                 if (r != E_SUCCESS)
602                 {
603                         r = GetMapContainer(MAP_CONTAINER_TYPE_DEFAULT_PORTRAIT)->GetFixedValueMap()->GetValue(fixedValueId, pInteger);
604                 }
605         }
606
607         if (r != E_SUCCESS)
608         {
609                 if (orientation == _CONTROL_ORIENTATION_PORTRAIT)
610                 {
611                         r = GetMapContainer(MAP_CONTAINER_TYPE_DEFAULT_PORTRAIT)->GetShapeMap()->GetValue(fixedValueId, pInteger);
612                         if (r != E_SUCCESS)
613                         {
614                                 r = GetMapContainer(MAP_CONTAINER_TYPE_DEFAULT_LANDSCAPE)->GetShapeMap()->GetValue(fixedValueId, pInteger);
615                         }
616                 }
617                 else
618                 {
619                         r = GetMapContainer(MAP_CONTAINER_TYPE_DEFAULT_LANDSCAPE)->GetShapeMap()->GetValue(fixedValueId, pInteger);
620                         if (r != E_SUCCESS)
621                         {
622                                 r = GetMapContainer(MAP_CONTAINER_TYPE_DEFAULT_PORTRAIT)->GetShapeMap()->GetValue(fixedValueId, pInteger);
623                         }
624                 }
625         }
626
627         if (r == E_SUCCESS)
628         {
629                 integer = *pInteger;
630         }
631
632         if (r == E_SUCCESS)
633         {
634                 value = integer.ToInt();
635         }
636         else
637         {
638                 return E_SYSTEM;
639         }
640
641         return r;
642 }
643
644 bool
645 _ResourceManager::IsCustomColor(const Tizen::Base::String& colorId)
646 {
647         return __pMapContainer[MAP_CONTAINER_TYPE_APP_BASE_PORTRAIT]->GetColorMap()->IsUserThemeItem(colorId);
648 }
649 bool
650 _ResourceManager::IsCustomBitmap(const Tizen::Base::String& bitmapId)
651 {
652         return __pMapContainer[MAP_CONTAINER_TYPE_APP_BASE_PORTRAIT]->GetImageMap()->IsUserThemeItem(bitmapId);
653 }
654
655 result
656 _ResourceManager::GetDefaultShapeWithScaling(const String& shapeId, _ControlOrientation orientation, Integer& integer)
657 {
658         result r = E_SUCCESS;
659         Integer* pInteger = null;
660         if (orientation == _CONTROL_ORIENTATION_PORTRAIT)
661         {
662                 r = GetMapContainer(MAP_CONTAINER_TYPE_DEFAULT_PORTRAIT)->GetShapeMap()->GetValue(shapeId, pInteger);
663                 if (r != E_SUCCESS)
664                 {
665                         r = GetMapContainer(MAP_CONTAINER_TYPE_DEFAULT_LANDSCAPE)->GetShapeMap()->GetValue(shapeId, pInteger);
666                 }
667         }
668         else
669         {
670                 r = GetMapContainer(MAP_CONTAINER_TYPE_DEFAULT_LANDSCAPE)->GetShapeMap()->GetValue(shapeId, pInteger);
671                 if (r != E_SUCCESS)
672                 {
673                         r = GetMapContainer(MAP_CONTAINER_TYPE_DEFAULT_PORTRAIT)->GetShapeMap()->GetValue(shapeId, pInteger);
674                 }
675         }
676         if (r == E_SUCCESS)
677         {
678                 integer = *pInteger;
679         }
680
681         if (r == E_SUCCESS)
682         {
683                 int temp = __pTransformer->Transform(integer.ToInt());
684                 if (temp == 0 && integer.ToInt() > 0)
685                 {
686                         integer = 1;
687                 }
688                 else
689                 {
690                         integer = temp;
691                 }
692         }
693         return r;
694 }
695
696 result
697 _ResourceManager::GetDefaultShapeWithScaling(const String& shapeId, _ControlOrientation orientation, Dimension& dimension)
698 {
699         result r = E_SUCCESS;
700         Dimension dim(0,0);
701         Dimension* pDimension = null;
702         if (orientation == _CONTROL_ORIENTATION_PORTRAIT)
703         {
704                 r = GetMapContainer(MAP_CONTAINER_TYPE_DEFAULT_PORTRAIT)->GetDimensionMap()->GetValue(shapeId, pDimension);
705                 if (r != E_SUCCESS)
706                 {
707                         r = GetMapContainer(MAP_CONTAINER_TYPE_DEFAULT_LANDSCAPE)->GetDimensionMap()->GetValue(shapeId, pDimension);
708                 }
709         }
710         else
711         {
712                 r = GetMapContainer(MAP_CONTAINER_TYPE_DEFAULT_LANDSCAPE)->GetDimensionMap()->GetValue(shapeId, pDimension);
713                 if (r != E_SUCCESS)
714                 {
715                         r = GetMapContainer(MAP_CONTAINER_TYPE_DEFAULT_PORTRAIT)->GetDimensionMap()->GetValue(shapeId, pDimension);
716                 }
717         }
718         if (r == E_SUCCESS)
719         {
720                 dim = *pDimension;
721         }
722
723         if (r == E_SUCCESS)
724         {
725                 Dimension temp = __pTransformer->Transform(dim);
726                 if (temp.width == 0 && dim.width > 0)
727                 {
728                         temp.width = 1;
729                 }
730                 if (temp.height == 0 && dim.height > 0)
731                 {
732                         temp.height = 1;
733                 }
734                 dimension = temp;
735         }
736         return r;
737 }
738
739 result
740 _ResourceManager::LoadThemeInformation(String& systemTheme, String& userTheme)
741 {
742         AppId appId = _AppInfo::GetAppId();
743         IMap* pList = null;
744         pList = _PackageManagerImpl::GetInstance()->GetUiThemeListN(appId);
745
746         if (pList)
747         {
748                 IMapEnumerator* pMapEnum = pList->GetMapEnumeratorN();
749                 while (pMapEnum->MoveNext() == E_SUCCESS)
750                 {
751                         String* pKey = static_cast<String*> (pMapEnum->GetKey());
752                         String* pValue = static_cast<String*> (pMapEnum->GetValue());
753                         String key(*pKey);
754                         key.ToLowerCase();
755                         if (key == L"systemtheme")
756                         {
757                                 systemTheme = *pValue;
758                                 systemTheme.ToLowerCase();
759                         }
760                         if (key == L"userdefinedtheme")
761                         {
762                                 userTheme = *pValue;
763                         }
764                 }
765                 delete pMapEnum;
766
767                 pList->RemoveAll(true);
768                 delete pList;
769         }
770         if (systemTheme.GetLength() < 1)
771         {
772                 systemTheme = L"black";
773         }
774         return E_SUCCESS;
775 }
776
777 void
778 _ResourceManager::GetThemeName(String& systemTheme, String& userTheme) const
779 {
780         systemTheme = __systemTheme;
781         userTheme = __userTheme;
782 }
783
784 void
785 _ResourceManager::SetSystemColor(void)
786 {
787         void* handle =null;
788         Color* pColor = null;
789         Color* (*GetSystemColor)(const String&) = null;
790
791         handle = dlopen ("libosp-compat.so", RTLD_LAZY);
792         if (!handle)
793         {
794                 return;
795         }
796         GetSystemColor = reinterpret_cast<Color*(*)(const String&)>(dlsym(handle, "_GetSystemColor"));
797         if (dlerror() != NULL)
798         {
799                 goto CATCH;
800         }
801
802         pColor = GetSystemColor(L"TITLE_TEXT");
803         if (pColor != null)
804         {
805                 GetColor("HEADER::TITLE_TEXT_NORMAL", *pColor);
806         }
807
808         pColor = GetSystemColor(L"BODY_TEXT");
809         if (pColor != null)
810         {
811                 GetColor("BUTTON::TEXT_NORMAL", *pColor);
812         }
813
814         pColor = GetSystemColor(L"FOREGROUND");
815         if (pColor != null)
816         {
817                 GetColor("foreground", *pColor);
818         }
819
820         pColor = GetSystemColor(L"BACKGROUND");
821         if (pColor != null)
822         {
823                 GetColor("background", *pColor);
824         }
825
826         pColor = GetSystemColor(L"LIST_BACKGROUND");
827         if (pColor != null)
828         {
829                 GetColor("FORM::BG_NORMAL", *pColor);
830         }
831
832         pColor = GetSystemColor(L"FORM_BACKGROUND");
833         if (pColor != null)
834         {
835                 GetColor("FORM::BG_NORMAL", *pColor);
836         }
837
838         pColor = GetSystemColor(L"FORM_GROUP_BACKGROUND");
839         if (pColor != null)
840         {
841                 GetColor("DEFAULTCOLORTABLE::form_group_list_bg", *pColor);
842         }
843
844         pColor = GetSystemColor(L"POPUP_BACKGROUND");
845         if (pColor != null)
846         {
847                 GetColor("POPUP::BG_NORMAL", *pColor);
848         }
849
850         pColor = GetSystemColor(L"GROUP_ITEM_TEXT");
851         if (pColor != null)
852         {
853                 GetColor("CHECKBUTTON::TEXT_NORMAL", *pColor);
854         }
855
856         pColor = GetSystemColor(L"LIST_ITEM_TEXT");
857         if (pColor != null)
858         {
859                 GetColor("LISTVIEW::ITEM_TEXT_NORMAL", *pColor);
860         }
861
862         pColor = GetSystemColor(L"LIST_ITEM_PRESSED_TEXT");
863         if (pColor != null)
864         {
865                 GetColor("LISTVIEW::ITEM_TEXT_PRESSED", *pColor);
866         }
867
868         pColor = GetSystemColor(L"LIST_ITEM_HIGHLIGHTED_TEXT");
869         if (pColor != null)
870         {
871                 GetColor("LISTVIEW::ITEM_TEXT_HIGHLIGHTED", *pColor);
872         }
873
874         //fall through
875 CATCH:
876         dlclose(handle);
877         return;
878 }
879 bool
880 _ResourceManager::GetDensityDirectory(String directoryName, float scaleFactor)
881 {
882         int denominator = 0;
883         int numerator = 0;
884         if(!(directoryName.StartsWith(L"screen-density",0)))
885         {
886                 return false;
887         }
888
889         if (__deviceDPI >= DPI_FOR_XHIGH)
890         {
891                 numerator = 9;
892         }
893         else if (__deviceDPI >= DPI_FOR_HIGH)
894         {
895                 numerator = 6;
896         }
897         else if (__deviceDPI >= DPI_FOR_MIDDLE)
898         {
899                 numerator = 4;
900         }
901         else if (__deviceDPI < DPI_FOR_MIDDLE && __deviceDPI > DPI_FOR_LOW)
902         {
903                 numerator = 3;
904         }
905         else
906         {
907                 return false;
908         }
909
910         if(directoryName.Contains(L"xhigh"))
911         {
912                 denominator = 9;
913         }
914         else if(directoryName.Contains(L"high"))
915         {
916                 denominator = 6;
917         }
918         else if(directoryName.Contains(L"middle"))
919         {
920                 denominator = 4;
921         }
922         else if(directoryName.Contains(L"low"))
923         {
924                 denominator = 3;
925         }
926         else
927         {
928                 return false;
929         }
930
931         scaleFactor = (float)((float)numerator / (float)denominator);
932         return true;
933 }
934
935 bool
936 _ResourceManager::GetResolutionDirectory(String directoryName, float scaleFactor)
937 {
938         if(!(directoryName.Contains(L"0")))
939         {
940                 return false;
941         }
942
943         int index = 0;
944         if(directoryName.IndexOf(L"x", 0, index) != E_SUCCESS)
945         {
946                 return false;
947         }
948         String width = 0;
949
950         if(directoryName.SubString(0, index - 1, width))
951         {
952                 int denominator = 0;
953                 int numerator = 0;
954                 if(Integer::Parse(width, denominator) == E_SUCCESS)
955                 {
956                         numerator = __targetWidth;
957                         scaleFactor = (float)((float)numerator / (float)denominator);
958                         return true;
959                 }
960         }
961         return false;
962 }
963
964 String
965 _ResourceManager::FindImagePath(const String& fileName, int& foundFolderWidth)
966 {
967         bool find = false;
968         String fullName(L"");
969         ResourceFallbackItem* pItem = null;
970
971         if(__userTheme.GetLength() > 0)
972         {
973                 String resDirectory = _AppInfo::GetAppRootPath() + L"res/theme/res";
974                 if(__pFallbackList == null)
975                 {
976                         __pFallbackList = new (std::nothrow) ArrayListT<ResourceFallbackItem*>;
977                         SysTryReturn(NID_UI, __pFallbackList, L"", E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation is failed.");
978                         Directory dir;
979                         result r = dir.Construct(resDirectory);
980                         if (r == E_SUCCESS)
981                         {
982                                 DirEnumerator* pDirEnum = dir.ReadN();
983                                 if (pDirEnum != null)
984                                 {
985                                         while (pDirEnum->MoveNext() == E_SUCCESS)
986                                         {
987                                                 DirEntry entry = pDirEnum->GetCurrentDirEntry();
988                                                 if(entry.IsDirectory())
989                                                 {
990                                                         float scaleFactor = 0;
991                                                         String directoryName = entry.GetName();
992                                                         if(GetDensityDirectory(directoryName, scaleFactor))
993                                                         {
994                                                                 pItem = new (std::nothrow) ResourceFallbackItem;
995                                                                 SysTryCatch(NID_UI, pItem, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation is failed.");
996                                                                 pItem->scaleFactor = scaleFactor;
997                                                                 pItem->directoryName = directoryName;
998                                                                 __pFallbackList->Add(pItem);
999                                                         }
1000                                                         else if(GetResolutionDirectory(directoryName, scaleFactor))
1001                                                         {
1002                                                                 pItem = new (std::nothrow) ResourceFallbackItem;
1003                                                                 SysTryCatch(NID_UI, pItem, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation is failed.");
1004                                                                 pItem->scaleFactor = scaleFactor;
1005                                                                 pItem->directoryName = directoryName;
1006                                                                 __pFallbackList->Add(pItem);
1007                                                         }
1008                                                 }
1009                                         }
1010                                         ResourceFallbackItemComparer<ResourceFallbackItem*> comparer;
1011                                         __pFallbackList->Sort(comparer);
1012                                         delete pDirEnum;
1013                                 }
1014                                 for(int i = 0 ; i < __pFallbackList->GetCount() ; i++)
1015                                 {
1016                                         ResourceFallbackItem* pItem;
1017                                         if(__pFallbackList->GetAt(i,pItem) == E_SUCCESS)
1018                                         {
1019                                                 if(pItem->scaleFactor < 1.001 && pItem->scaleFactor > 0.999)
1020                                                 {
1021                                                         __pFallbackList->RemoveAt(i);
1022                                                         __pFallbackList->InsertAt(pItem,0);
1023                                                         if(pItem->directoryName.Contains(L"0"))
1024                                                         {
1025                                                                 break;
1026                                                         }
1027                                                 }
1028                                         }
1029                                 }
1030                         }
1031                 }
1032                 for(int i = 0 ; i <__pFallbackList->GetCount() ; i++)
1033                 {
1034                         ResourceFallbackItem* pItem;
1035                         __pFallbackList->GetAt(i,pItem);
1036                         fullName = resDirectory + L"/" + pItem->directoryName + L"/" + fileName;
1037                         if (File::IsFileExist(fullName))
1038                         {
1039                                 find = true;
1040                                 foundFolderWidth = __targetWidth / pItem->scaleFactor;
1041                                 if(foundFolderWidth > __targetWidth)
1042                                 {
1043                                         if( foundFolderWidth < __targetWidth + 3)
1044                                         {
1045                                                 foundFolderWidth = __targetWidth;
1046                                         }
1047                                 }
1048                                 else
1049                                 {
1050                                         if( foundFolderWidth >__targetWidth - 3)
1051                                         {
1052                                                 foundFolderWidth = __targetWidth;
1053                                         }
1054                                 }
1055                                 break;
1056                         }
1057                 }
1058         }
1059         if(!find)
1060         {
1061                 fullName = String(L"/usr/share/osp/bitmaps/" + __targetPortraitMode+ "/" + __systemTheme + L"/" + fileName);
1062                 if (File::IsFileExist(fullName))
1063                 {
1064                         find = true;
1065                         foundFolderWidth = __targetWidth;
1066                 }
1067                 else
1068                 {
1069                         fullName = String(L"/usr/share/osp/bitmaps/" + __defaultPortraitMode+ "/" + __systemTheme + L"/" + fileName);
1070                         if (File::IsFileExist(fullName))
1071                         {
1072                                 find = true;
1073                                 foundFolderWidth = DEFAULT_SCREEN_WIDTH;
1074                         }
1075                 }
1076         }
1077
1078         if(!find)
1079         {
1080                 fullName = String(L"/usr/share/osp/bitmaps/" + __targetPortraitMode + "/" + fileName);
1081                 if (File::IsFileExist(fullName))
1082                 {
1083                         find = true;
1084                         foundFolderWidth = __targetWidth;
1085                 }
1086                 else
1087                 {
1088                         fullName = String(L"/usr/share/osp/bitmaps/" + __defaultPortraitMode+ "/" + fileName);
1089                         if (File::IsFileExist(fullName))
1090                         {
1091                                 find = true;
1092                                 foundFolderWidth = DEFAULT_SCREEN_WIDTH;
1093                         }
1094                 }
1095         }
1096
1097         if(!find)
1098         {
1099                 return L"";
1100         }
1101         else
1102         {
1103                 return fullName;
1104         }
1105 CATCH:
1106         delete __pFallbackList;
1107         __pFallbackList = null;
1108         return L"";
1109 }
1110
1111 }}//Tizen::Ui