NABI_Se issues resolved
[apps/osp/MyFiles.git] / src / MfUtility.cpp
1 //\r
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.\r
3 //\r
4 // Licensed under the Flora License, Version 1.1 (the License);\r
5 // you may not use this file except in compliance with the License.\r
6 // You may obtain a copy of the License at\r
7 //\r
8 //     http://floralicense.org/license/\r
9 //\r
10 // Unless required by applicable law or agreed to in writing, software\r
11 // distributed under the License is distributed on an AS IS BASIS,\r
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13 // See the License for the specific language governing permissions and\r
14 // limitations under the License.\r
15 //\r
16 \r
17 /**\r
18  * @file: MfUtility.cpp\r
19  * @brief: This file contains the implementation of MfUtility class, which is a common utility function class and\r
20  * contains helper functions which are used throughout the Application.\r
21  */\r
22 \r
23 #include "MfUtility.h"\r
24 \r
25 using namespace Tizen::App;\r
26 using namespace Tizen::Base;\r
27 using namespace Tizen::Base::Utility;\r
28 using namespace Tizen::Graphics;\r
29 using namespace Tizen::Locales;\r
30 \r
31 MfUtility::MfUtility(void)\r
32 {\r
33         // Empty Implementation\r
34 \r
35 }\r
36 \r
37 MfUtility::~MfUtility(void)\r
38 {\r
39         // Empty Implementation\r
40 }\r
41 \r
42 MfUtility&\r
43 MfUtility::operator =(const MfUtility& mfUtility)\r
44 {\r
45         return *this;\r
46 }\r
47 \r
48 MfUtility::MfUtility(const MfUtility& mfUtility)\r
49 {\r
50         //Do Nothing\r
51 }\r
52 \r
53 \r
54 Tizen::Base::String\r
55 MfUtility::ConvertFileSizeToString(long long int fileSize)\r
56 {\r
57         AppLogDebug("Entry");\r
58         AppResource* pAppResource = null;\r
59         String size;\r
60         pAppResource = Application::GetInstance()->GetAppResource();\r
61 \r
62         float preciseFileSize = (float)fileSize / (float)MB_DIVISOR;\r
63 \r
64         if ((int)preciseFileSize == 0.0)\r
65         {\r
66                 preciseFileSize = (float)fileSize / (float)KB_DIVISOR;\r
67                 if ((int)preciseFileSize == 0)\r
68                 {\r
69                         String bytes;\r
70                         pAppResource->GetString(L"IDS_BYTES", bytes);\r
71                         size.Append(fileSize);\r
72                         size.Append(L" ");\r
73                         size.Append(bytes);\r
74                 }\r
75                 else\r
76                 {\r
77                         String kb;\r
78                         pAppResource->GetString(L"IDS_COM_BODY_KB", kb);\r
79                         size.Append(preciseFileSize);\r
80                         size.Append(L" ");\r
81                         size.Append(kb);\r
82                 }\r
83         }\r
84         else\r
85         {\r
86                 String mb;\r
87                 pAppResource->GetString(L"IDS_COM_BODY_MB", mb);\r
88                 size.Append(preciseFileSize);\r
89                 size.Append(L" ");\r
90                 size.Append(mb);\r
91         }\r
92         AppLogDebug("Exit");\r
93         return size;\r
94 }\r
95 \r
96 Tizen::Base::String\r
97 MfUtility::ConvertDateTimeToString(Tizen::Base::DateTime fileCreationTime)\r
98 {\r
99         AppLogDebug("Entry");\r
100         String dateAndTime;\r
101         String cutomizedPattern;\r
102         DateTimeFormatter* pDateFormatter = null;\r
103         //DateTimeFormatter* pTimeFormatter = null;\r
104         DateTime dirCreationTime;\r
105         Locale locale(LANGUAGE_ENG, COUNTRY_US);\r
106 \r
107         dirCreationTime = fileCreationTime;\r
108 \r
109         pDateFormatter = DateTimeFormatter::CreateDateFormatterN(locale, DATE_TIME_STYLE_DEFAULT);\r
110         TryCatch(pDateFormatter != null, dateAndTime.Append("Not Found"), "Failed to get date time formatter");\r
111 \r
112         // Customized pattern\r
113         cutomizedPattern.Append(L"d MMM, yyyy HH:mm");\r
114         pDateFormatter->ApplyPattern(cutomizedPattern);\r
115 \r
116         // Format creation time with date formatter\r
117         pDateFormatter->Format(dirCreationTime, dateAndTime);\r
118 \r
119         //if (pDateFormatter != null)\r
120         //{\r
121         delete pDateFormatter;\r
122         pDateFormatter = null;\r
123         //}\r
124 \r
125         AppLogDebug("Exit");\r
126         return dateAndTime;\r
127 \r
128 CATCH:\r
129 \r
130         AppLogDebug("Exit");\r
131         return dateAndTime;\r
132 }\r
133 \r
134 Tizen::Graphics::Bitmap*\r
135 MfUtility::GetNinepatchedBitmapN(const String& strPath, const int width, const int height)\r
136 {\r
137         Bitmap* bmp = null;\r
138         AppResource* pAppResource = null;\r
139 \r
140         pAppResource = Application::GetInstance()->GetAppResource();\r
141         bmp = pAppResource->GetBitmapN(strPath, BITMAP_PIXEL_FORMAT_ARGB8888);\r
142 \r
143         if (!bmp)\r
144         {\r
145                 return null;\r
146         }\r
147 \r
148         Bitmap* ret = null;\r
149         result r = E_SUCCESS;\r
150 \r
151         Canvas* pCanvas = new (std::nothrow) Canvas();\r
152         TryCatch(pCanvas != null, , "Canvas new Fail.");\r
153         r = pCanvas->Construct(Rectangle(0, 0, width, height));\r
154         TryCatch(r == E_SUCCESS, , "Canvas Construct Fail.");\r
155 \r
156         r = pCanvas->DrawNinePatchedBitmap(Rectangle(0, 0, width, height), *bmp);\r
157         if (r != E_SUCCESS)\r
158         {\r
159                 AppLogDebug("DrawNinePatchedBitmap Error %S", GetErrorMessage(r));\r
160                 pCanvas->DrawBitmap(Rectangle(0, 0, width, height), *bmp);\r
161         }\r
162         ret = new (std::nothrow) Bitmap();\r
163         TryCatch(ret != null, , "Bitmap new Fail.");\r
164         r = ret->Construct(*pCanvas, Rectangle(0, 0, width, height));\r
165         TryCatch(r == E_SUCCESS, , "Bitmap Construct Fail.");\r
166 \r
167         if (bmp != null)\r
168         {\r
169                 delete bmp;\r
170         }\r
171         if (pCanvas != null)\r
172         {\r
173                 delete pCanvas;\r
174         }\r
175 \r
176         return ret;\r
177 \r
178 CATCH:\r
179         if (bmp != null)\r
180         {\r
181                 delete bmp;\r
182         }\r
183 \r
184         if (pCanvas != null)\r
185         {\r
186                 delete pCanvas;\r
187         }\r
188         if (ret != null)\r
189         {\r
190                 delete ret;\r
191         }\r
192         return null;\r
193 \r
194 }\r
195 \r
196 \r
197 Tizen::Graphics::Bitmap*\r
198 MfUtility::MergeBitmapN(const Tizen::Base::String& firstBitmapPath, const Tizen::Base::String& secondBitmapPath, const int width, const int height)\r
199 {\r
200         Bitmap* pFirstBitmap = null;\r
201         Bitmap* pSecondBitmap = null;\r
202 \r
203         Bitmap* pMergedBitmap = null;\r
204 \r
205         AppResource* pAppResource = null;\r
206 \r
207         pAppResource = Application::GetInstance()->GetAppResource();\r
208         pFirstBitmap = pAppResource->GetBitmapN(firstBitmapPath, BITMAP_PIXEL_FORMAT_ARGB8888);\r
209         pSecondBitmap = pAppResource->GetBitmapN(secondBitmapPath, BITMAP_PIXEL_FORMAT_ARGB8888);\r
210 \r
211         if (!pFirstBitmap || !pSecondBitmap)\r
212         {\r
213                 return null;\r
214         }\r
215 \r
216         result r = E_SUCCESS;\r
217 \r
218         Canvas* pCanvas = new (std::nothrow) Canvas();\r
219         TryCatch(pCanvas != null, , "Canvas new Fail.");\r
220 \r
221         r = pCanvas->Construct(Rectangle(0, 0, width, height));\r
222         TryCatch(r == E_SUCCESS, , "Canvas Construct Fail.");\r
223 \r
224         pCanvas->DrawBitmap(Rectangle(0, 0, width, height), *pFirstBitmap);\r
225         pCanvas->DrawBitmap(Rectangle(0, 0, width, height), *pSecondBitmap);\r
226 \r
227         pMergedBitmap = new (std::nothrow) Bitmap();\r
228         pMergedBitmap->Construct(*pCanvas, Rectangle(0, 0, width, height));\r
229 \r
230         if (pFirstBitmap != null)\r
231         {\r
232                 delete pFirstBitmap;\r
233                 pFirstBitmap = null;\r
234         }\r
235 \r
236         if (pSecondBitmap != null)\r
237         {\r
238                 delete pSecondBitmap;\r
239                 pSecondBitmap = null;\r
240         }\r
241 \r
242         if (pCanvas != null)\r
243         {\r
244                 delete pCanvas;\r
245                 pCanvas = null;\r
246         }\r
247 \r
248         return pMergedBitmap;\r
249 \r
250 CATCH:\r
251         if (pCanvas != null)\r
252         {\r
253                 delete pCanvas;\r
254                 pCanvas = null;\r
255         }\r
256 \r
257         if (pFirstBitmap != null)\r
258         {\r
259                 delete pFirstBitmap;\r
260                 pFirstBitmap = null;\r
261         }\r
262 \r
263         if (pSecondBitmap != null)\r
264         {\r
265                 delete pSecondBitmap;\r
266                 pSecondBitmap = null;\r
267         }\r
268         return null;\r
269 }\r
270 \r
271 Tizen::Graphics::Bitmap*\r
272 MfUtility::MergeBitmapN(Tizen::Graphics::Bitmap* pFirstBitmap, const Tizen::Graphics::Bitmap* pSecondBitmap, const int width, const int height)\r
273 {\r
274         result r = E_SUCCESS;\r
275         Bitmap* pMergedBitmap = null;\r
276         Canvas* pCanvas = null;\r
277 \r
278         TryCatch(pFirstBitmap != null, , "Failed to fetch the background bitmap.");\r
279         TryCatch(pSecondBitmap != null, , "Failed to fetch the foreground bitmap.");\r
280 \r
281         pCanvas = new (std::nothrow) Canvas();\r
282         TryCatch(pCanvas != null, , "Canvas new Fail.");\r
283 \r
284         r = pCanvas->Construct(Rectangle(0, 0, width, height));\r
285         TryCatch(r == E_SUCCESS, , "Canvas Construct Fail.");\r
286 \r
287         pCanvas->DrawBitmap(Rectangle(0, 0, width, height), *pFirstBitmap);\r
288         pCanvas->DrawBitmap(Point(0, 0), *pSecondBitmap);\r
289 \r
290         pMergedBitmap = new (std::nothrow) Bitmap();\r
291         pMergedBitmap->Construct(*pCanvas, Rectangle(0, 0, width, height));\r
292 \r
293         delete pFirstBitmap;\r
294         delete pSecondBitmap;\r
295         delete pCanvas;\r
296 \r
297         return pMergedBitmap;\r
298 CATCH:\r
299         if (pCanvas != null)\r
300         {\r
301                 delete pCanvas;\r
302         }\r
303 \r
304         if (pFirstBitmap != null)\r
305         {\r
306                 delete pFirstBitmap;\r
307         }\r
308 \r
309         if (pSecondBitmap != null)\r
310         {\r
311                 delete pSecondBitmap;\r
312         }\r
313         return null;\r
314 }\r
315 \r
316 \r
317 Tizen::Graphics::Bitmap*\r
318 MfUtility::MergeNinePatchedBitmapN(const Tizen::Base::String& firstBitmapPath, const Tizen::Base::String& secondBitmapPath, const int width, const int height, Tizen::Graphics::Point* pPoint)\r
319 {\r
320         AppResource* pAppResource = null;\r
321         result r = E_SUCCESS;\r
322 \r
323         Bitmap* pFirstBitmap = null;\r
324         Bitmap* pSecondBitmap = null;\r
325 \r
326         Bitmap* pMergedBitmap = null;\r
327 \r
328         Point startPoint(0,0);\r
329         Canvas* pCanvas = null;\r
330 \r
331         if (pPoint == null)\r
332         {\r
333                 startPoint.x = 0;\r
334                 startPoint.y = 0;\r
335         }\r
336         else\r
337         {\r
338                 startPoint.x = pPoint->x;\r
339                 startPoint.y = pPoint->y;\r
340         }\r
341 \r
342         pAppResource = Application::GetInstance()->GetAppResource();\r
343         pFirstBitmap = GetNinepatchedBitmapN(firstBitmapPath, width, height);\r
344         pSecondBitmap = pAppResource->GetBitmapN(secondBitmapPath, BITMAP_PIXEL_FORMAT_ARGB8888);\r
345 \r
346         TryCatch(pFirstBitmap != null, , "Failed to fetch the background bitmap.");\r
347         TryCatch(pSecondBitmap != null, , "Failed to fetch the foreground bitmap.");\r
348 \r
349         pCanvas = new (std::nothrow) Canvas();\r
350         TryCatch(pCanvas != null, , "Canvas new Fail.");\r
351 \r
352         r = pCanvas->Construct(Rectangle(0, 0, width, height));\r
353         TryCatch(r == E_SUCCESS, , "Canvas Construct Fail.");\r
354 \r
355         pCanvas->DrawBitmap(Rectangle(0, 0, width, height), *pFirstBitmap);\r
356         pCanvas->DrawBitmap(startPoint, *pSecondBitmap);\r
357 \r
358         pMergedBitmap = new (std::nothrow) Bitmap();\r
359         pMergedBitmap->Construct(*pCanvas, Rectangle(0, 0, width, height));\r
360 \r
361         if (pFirstBitmap != null)\r
362         {\r
363                 delete pFirstBitmap;\r
364                 pFirstBitmap = null;\r
365         }\r
366 \r
367         if (pSecondBitmap != null)\r
368         {\r
369                 delete pSecondBitmap;\r
370                 pSecondBitmap = null;\r
371         }\r
372 \r
373         if (pCanvas != null)\r
374         {\r
375                 delete pCanvas;\r
376                 pCanvas = null;\r
377         }\r
378 \r
379         return pMergedBitmap;\r
380 \r
381 CATCH:\r
382         if (pCanvas != null)\r
383         {\r
384                 delete pCanvas;\r
385                 pCanvas = null;\r
386         }\r
387 \r
388         if (pFirstBitmap != null)\r
389         {\r
390                 delete pFirstBitmap;\r
391                 pFirstBitmap = null;\r
392         }\r
393 \r
394         if (pSecondBitmap != null)\r
395         {\r
396                 delete pSecondBitmap;\r
397                 pSecondBitmap = null;\r
398         }\r
399         return null;\r
400 }\r