add to resume event
[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 "IntCommonLib.h"
26
27 using namespace Tizen::App;
28 using namespace Tizen::Base;
29 using namespace Tizen::Graphics;
30 using namespace Tizen::Media;
31
32 CommonBitmapTable CommonUtil::__commonBitmapTable[] = {
33
34                 // -------- End of table -----------------------------------------------------
35                 {
36                                 NULL, 0, L"", -1, -1
37                 }                                   // DO NOT REPLACE THIS LINE
38 };
39
40 String
41 CommonUtil::GetString(const String& resourceId)
42 {
43
44         result r = E_SUCCESS;
45         String tmpString;
46         AppResource* pAppResource = Application::GetInstance()->GetAppResource();
47
48         if (!pAppResource)
49         {
50                 tmpString = "(Error)";
51                 return tmpString;
52         }
53
54         r = pAppResource->GetString(resourceId, tmpString);
55
56         TryCatch( !IsFailed(r),,"CommonUtil::GetString ,pAppResource->GetString Failed %s",GetErrorMessage(r));
57         return tmpString;
58
59         CATCH:
60         tmpString = "(Error)";
61         return tmpString;
62 }
63
64 Bitmap*
65 CommonUtil::GetBitmapN(const String& path, const int width, const int height)
66 {
67         Bitmap* pBitmap = null;
68         AppResource* pAppResource = Application::GetInstance()->GetAppResource();
69
70         if (pAppResource == null)
71         {
72                 return null;
73         }
74
75         if (path == null)
76         {
77                 return null;
78         }
79
80         if (width <= 0 || height <= 0)
81         {
82                 return null;
83         }
84
85         AppLogDebug("Calling Decode method for image %ls", path.GetPointer());
86         pBitmap = pAppResource->GetBitmapN(path, BITMAP_PIXEL_FORMAT_ARGB8888);
87         AppLogDebug("Image Decoded successfully");
88
89         if (pBitmap == null)
90         {
91                 return null;
92         }
93
94         if (width > 0 && height > 0)
95         {
96                 pBitmap->Scale(Dimension(width, height));
97         }
98
99         return pBitmap;
100 }
101
102
103 Bitmap*
104 CommonUtil::GetBitmap(uint id)
105 {
106         AppLogDebug("CommonUtil::GetBitmap entered");
107
108         Bitmap* pBitmap = null;
109
110         if (__commonBitmapTable[id].ptr)
111         {
112                 pBitmap = __commonBitmapTable[id].ptr;
113                 AppLogDebug("found already used bitmap pointer");
114         }
115         else
116         {
117                 AppLogDebug("Not found #%d bitmap data in cache. Start loading...\n", (int) id);
118                 pBitmap = GetBitmapN(__commonBitmapTable[id].filename,
119                                 __commonBitmapTable[id].width,
120                                 __commonBitmapTable[id].height);
121                 if (pBitmap == NULL)
122                 {
123                         AppLogDebug("Error: Failed to load #%d bitmap.\n", (int) id);
124                         return null;
125                 }
126
127                 __commonBitmapTable[id].ptr = pBitmap;
128         }
129
130         __commonBitmapTable[id].refcount++;
131         AppLogDebug("CommonUtil::GetBitmap exit");
132         return pBitmap;
133 }
134
135 Bitmap*
136 CommonUtil::GetNinepatchedBitmapN(const String& strPath, const int width, const int height)
137 {
138         Bitmap* pBmp = UiApp::GetInstance()->GetAppResource()->GetBitmapN(strPath);
139
140         if (pBmp == NULL)
141         {
142                 AppLogDebug("Couldn't create bitmap %ls error(%s)",strPath.GetPointer(),GetErrorMessage(GetLastResult()));
143                 return null;
144         }
145
146         Bitmap* pBitmap = null;
147         result r = E_SUCCESS;
148
149         Canvas* pCanvas = new(std::nothrow) Canvas();
150         TryCatch(pCanvas != null, , "Canvas new Fail.");
151         r = pCanvas->Construct(Rectangle(0,0,width, height));
152         TryCatch(r == E_SUCCESS, , "Canvas Construct Fail.");
153
154         r = pCanvas->DrawNinePatchedBitmap(Rectangle(0,0,width, height), *pBmp);
155
156         if (r != E_SUCCESS)
157         {
158                 AppLogDebug("DrawNinePatchedBitmap Error %ls", GetErrorMessage(r));
159                 pCanvas->DrawBitmap(Rectangle(0,0,width, height), *pBmp);
160         }
161
162         pBitmap = new(std::nothrow) Bitmap();
163         TryCatch(pBitmap != null, , "Bitmap new Fail.");
164
165         r = pBitmap->Construct(*pCanvas, Rectangle(0,0,width, height));
166         TryCatch(r == E_SUCCESS, , "Bitmap Construct Fail.");
167
168         if ( pBmp != NULL)
169         {
170                 delete pBmp;
171         }
172
173         if (pCanvas != NULL)
174         {
175                 delete pCanvas;
176         }
177
178         return pBitmap;
179
180         CATCH:
181         if (pBmp  != NULL)
182         {
183                 delete pBmp;
184         }
185
186         if (pCanvas  != NULL)
187         {
188                 delete pCanvas;
189         }
190
191         if (pBitmap != NULL)
192         {
193                 delete pBitmap;
194         }
195
196         return null;
197 }