Fixed Nabi Issues N_SE-54919,54952,55044
[apps/osp/Internet.git] / src / IntCommonLib.cpp
1 //
2
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Flora License, Version 1.1 (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
18 //!Internet
19 /*@file: IntCommonUtil.cpp
20  *@brief: To define the common method of Internet application
21  */
22
23 #include <FApp.h>
24 #include <FMedia.h>
25 #include <FSystem.h>
26 #include "IntCommonLib.h"
27
28 using namespace Tizen::App;
29 using namespace Tizen::Base;
30 using namespace Tizen::Graphics;
31 using namespace Tizen::Media;
32 using namespace Tizen::System;
33
34 const int FONT_MAIN_TEXT_SIZE_GIANT = 98;
35 const int FONT_MAIN_TEXT_SIZE_HUGE = 81;
36 const int FONT_MAIN_TEXT_SIZE_LARGE = 64;
37 const int FONT_MAIN_TEXT_SIZE_NORMAL = 44;
38 const int FONT_MAIN_TEXT_SIZE_SMALL = 36;
39 const wchar_t* SETTING_INFO_KEY_FONT_SIZE = L"http://tizen.org/setting/font.size";
40
41 CommonBitmapTable CommonUtil::__commonBitmapTable[] = {
42
43                 // -------- End of table -----------------------------------------------------
44                 {
45                                 NULL, 0, L"", -1, -1
46                 }                                   // DO NOT REPLACE THIS LINE
47 };
48
49 String
50 CommonUtil::GetString(const String& resourceId)
51 {
52
53         result r = E_SUCCESS;
54         String tmpString;
55         AppResource* pAppResource = Application::GetInstance()->GetAppResource();
56
57         if (!pAppResource)
58         {
59                 tmpString = "(Error)";
60                 return tmpString;
61         }
62
63         r = pAppResource->GetString(resourceId, tmpString);
64
65         TryCatch( !IsFailed(r),,"CommonUtil::GetString ,pAppResource->GetString Failed %s",GetErrorMessage(r));
66         return tmpString;
67
68         CATCH:
69         tmpString = "(Error)";
70         return tmpString;
71 }
72
73 Bitmap*
74 CommonUtil::GetBitmapN(const String& path, const int width, const int height)
75 {
76         Bitmap* pBitmap = null;
77         AppResource* pAppResource = Application::GetInstance()->GetAppResource();
78
79         if (pAppResource == null)
80         {
81                 return null;
82         }
83
84         if (path == null)
85         {
86                 return null;
87         }
88
89         if (width <= 0 || height <= 0)
90         {
91                 return null;
92         }
93
94         AppLogDebug("Calling Decode method for image %ls", path.GetPointer());
95         pBitmap = pAppResource->GetBitmapN(path, BITMAP_PIXEL_FORMAT_ARGB8888);
96         AppLogDebug("Image Decoded successfully");
97
98         if (pBitmap == null)
99         {
100                 return null;
101         }
102
103         if (width > 0 && height > 0)
104         {
105                 pBitmap->Scale(Dimension(width, height));
106         }
107
108         return pBitmap;
109 }
110
111
112 Bitmap*
113 CommonUtil::GetBitmap(uint id)
114 {
115         AppLogDebug("CommonUtil::GetBitmap entered");
116
117         Bitmap* pBitmap = null;
118
119         if (__commonBitmapTable[id].ptr)
120         {
121                 pBitmap = __commonBitmapTable[id].ptr;
122                 AppLogDebug("found already used bitmap pointer");
123         }
124         else
125         {
126                 AppLogDebug("Not found #%d bitmap data in cache. Start loading...\n", (int) id);
127                 pBitmap = GetBitmapN(__commonBitmapTable[id].filename,
128                                 __commonBitmapTable[id].width,
129                                 __commonBitmapTable[id].height);
130                 if (pBitmap == NULL)
131                 {
132                         AppLogDebug("Error: Failed to load #%d bitmap.\n", (int) id);
133                         return null;
134                 }
135
136                 __commonBitmapTable[id].ptr = pBitmap;
137         }
138
139         __commonBitmapTable[id].refcount++;
140         AppLogDebug("CommonUtil::GetBitmap exit");
141         return pBitmap;
142 }
143
144 Bitmap*
145 CommonUtil::GetNinepatchedBitmapN(const String& strPath, const int width, const int height)
146 {
147         Bitmap* pBmp = UiApp::GetInstance()->GetAppResource()->GetBitmapN(strPath);
148
149         if (pBmp == NULL)
150         {
151                 AppLogDebug("Couldn't create bitmap %ls error(%s)",strPath.GetPointer(),GetErrorMessage(GetLastResult()));
152                 return null;
153         }
154
155         Bitmap* pBitmap = null;
156         result r = E_SUCCESS;
157
158         Canvas* pCanvas = new(std::nothrow) Canvas();
159         TryCatch(pCanvas != null, , "Canvas new Fail.");
160         r = pCanvas->Construct(Rectangle(0,0,width, height));
161         TryCatch(r == E_SUCCESS, , "Canvas Construct Fail.");
162
163         r = pCanvas->DrawNinePatchedBitmap(Rectangle(0,0,width, height), *pBmp);
164
165         if (r != E_SUCCESS)
166         {
167                 AppLogDebug("DrawNinePatchedBitmap Error %ls", GetErrorMessage(r));
168                 pCanvas->DrawBitmap(Rectangle(0,0,width, height), *pBmp);
169         }
170
171         pBitmap = new(std::nothrow) Bitmap();
172         TryCatch(pBitmap != null, , "Bitmap new Fail.");
173
174         r = pBitmap->Construct(*pCanvas, Rectangle(0,0,width, height));
175         TryCatch(r == E_SUCCESS, , "Bitmap Construct Fail.");
176
177         if ( pBmp != NULL)
178         {
179                 delete pBmp;
180         }
181
182         if (pCanvas != NULL)
183         {
184                 delete pCanvas;
185         }
186
187         return pBitmap;
188
189         CATCH:
190         if (pBmp  != NULL)
191         {
192                 delete pBmp;
193         }
194
195         if (pCanvas  != NULL)
196         {
197                 delete pCanvas;
198         }
199
200         if (pBitmap != NULL)
201         {
202                 delete pBitmap;
203         }
204
205         return null;
206 }
207
208 int
209 CommonUtil::GetFontSize(void)
210 {
211         int fontSize = FONT_MAIN_TEXT_SIZE_NORMAL;
212         String fontSizeValue;
213
214         if (SettingInfo::GetValue(SETTING_INFO_KEY_FONT_SIZE, fontSizeValue) == E_SUCCESS)
215         {
216                 if (fontSizeValue.Equals(L"giant", false))
217                 {
218                         fontSize = FONT_MAIN_TEXT_SIZE_GIANT;
219                 }
220                 else if (fontSizeValue.Equals(L"huge", false))
221                 {
222                         fontSize = FONT_MAIN_TEXT_SIZE_HUGE;
223                 }
224                 else if (fontSizeValue.Equals(L"large", false))
225                 {
226                         fontSize = FONT_MAIN_TEXT_SIZE_LARGE;
227                 }
228                 else if (fontSizeValue.Equals(L"medium", false))
229                 {
230                         fontSize = FONT_MAIN_TEXT_SIZE_NORMAL;
231                 }
232                 else
233                 {
234                         fontSize = FONT_MAIN_TEXT_SIZE_SMALL;
235                 }
236         }
237         return fontSize;
238 }
239