Flora license update
[apps/osp/Home.git] / src / HmApplicationUtils.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Flora License, Version 1.1 (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://floralicense.org/license/
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 exprs or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 /**
18  * @file        HmApplicationUtils.cpp
19  * @brief       Keeps the implementations for Utility functions,
20  * Implementations of the various Utility function which may be used throughout the application
21  */
22
23 #include <FApp.h>
24 #include <FBase.h>
25 #include <FGraphics.h>
26 #include <FMedia.h>
27 #include <FUi.h>
28 #include "HmApplicationUtils.h"
29 #include "HmTypes.h"
30
31 using namespace Tizen::App;
32 using namespace Tizen::Base;
33 using namespace Tizen::Base::Collection;
34 using namespace Tizen::Graphics;
35 using namespace Tizen::Media;
36 using namespace Tizen::Ui;
37 using namespace Tizen::Ui::Controls;
38
39 Bitmap*
40 ApplicationUtils::GetApplicationIconBitmapN(const String& imagePath, const String& appCaption, bool isResourceBitmap)
41 {
42         result r = E_SUCCESS;
43         Canvas* pCanvas = null;
44         Bitmap* pBitmap = null;
45         Bitmap* pAppIconBitmap = null;
46         EnrichedText appEnrichedCaption;
47         EnrichedText appEnrichedShadowCaption;
48         TryReturn(!imagePath.IsEmpty(), null, "Invalid Parameter");
49
50         if (isResourceBitmap)
51         {
52                 pBitmap = GetResourceBitmapN(imagePath);
53         }
54
55         if (pBitmap == null)
56         {
57                 pBitmap = GetBitmapN(imagePath);
58                 // creates bitmap for the icon if is default icon path
59                 if (pBitmap == null)
60                 {
61                         pBitmap = GetResourceBitmapN(IDB_DEFAULT_APP_ICON);
62                 }
63         }
64
65         TryCatch(pBitmap != null, , "Failed to create bitmap");
66
67         if (pBitmap->GetHeight() != APP_ICON_IMAGE_SIZE || pBitmap->GetWidth() != APP_ICON_IMAGE_SIZE)
68         {
69                 pBitmap->Scale(Dimension(APP_ICON_IMAGE_SIZE, APP_ICON_IMAGE_SIZE));
70         }
71         // get a Canvas instance
72         pCanvas = new (std::nothrow) Canvas();
73         TryCatch(pCanvas != null, , "Failed to allocate for Canvas");
74
75         r = pCanvas->Construct(Rectangle(0, 0, W_APPLICATION_ICON, H_APPLICATION_ICON));
76         TryCatch(r == E_SUCCESS, , "pCanvas->Construct failed with error %s", GetErrorMessage(r));
77         pCanvas->SetBackgroundColor(Color(0, 0, 0, 0));
78         pCanvas->Clear();
79
80         r = pCanvas->DrawBitmap(Point((W_APPLICATION_ICON - pBitmap->GetWidth()) / 2, Y_APP_ICON_IMAGE_START) /*, APP_ICON_IMAGE_SIZE, APP_ICON_IMAGE_SIZE)*/, *pBitmap);
81         TryCatch(r == E_SUCCESS, , "pCanvas->DrawBitmap failed with error %s", GetErrorMessage(r));
82
83         if (!appCaption.IsEmpty())
84         {
85                 Font font;
86                 String fontToUse;
87                 TextElement captionElement;
88
89                 IList* pFontsList = Font::GetSystemFontListN();
90
91                 if (pFontsList != null)
92                 {
93                         IEnumerator* pEnum = pFontsList->GetEnumeratorN();
94
95                         if (pEnum != null)
96                         {
97                                 while (pEnum->MoveNext() == E_SUCCESS)
98                                 {
99                                         String* pFontName = static_cast<String*>(pEnum->GetCurrent());
100
101                                         if (pFontName && pFontName->Contains(L"HelveticaNeue"))
102                                         {
103                                                 fontToUse = *pFontName;
104                                                 break;
105                                         }
106                                 }
107
108                                 delete pEnum;
109                                 pEnum = null;
110                         }
111
112                         pFontsList->RemoveAll(true);
113                         delete pFontsList;
114                         pFontsList = null;
115                 }
116
117                 if (fontToUse.IsEmpty())
118                 {
119                         r = font.Construct(FONT_STYLE_PLAIN, FONT_SIZE_APP_CAPTION);
120                         TryCatch(r == E_SUCCESS, , "font.Construct failed with error = %s", GetErrorMessage(r));
121                 }
122                 else
123                 {
124                         AppLogDebug("FontTest :: Calling with font.Construct(name) ");
125                         r = font.Construct(fontToUse, FONT_STYLE_PLAIN, FONT_SIZE_APP_CAPTION);
126                         TryCatch(r == E_SUCCESS, , "font.Construct failed with error = %s", GetErrorMessage(r));
127                 }
128
129                 // app name text
130                 r = captionElement.Construct(appCaption);
131                 TryCatch(r == E_SUCCESS, , "captionElement.Construct failed with error = %s", GetErrorMessage(r));
132                 captionElement.SetTextColor(COLOR_APP_ICON_CAPTION);
133                 captionElement.SetFont(font);
134                 captionElement.SetOutlineColor(COLOR_APP_CAPTION_SHADOW1);
135                 appEnrichedCaption.Construct(Dimension(W_APPLICATION_ICON, H_APP_CAPTION_TEXT));
136                 appEnrichedCaption.SetHorizontalAlignment(TEXT_ALIGNMENT_CENTER);
137                 appEnrichedCaption.SetVerticalAlignment(TEXT_ALIGNMENT_MIDDLE);
138                 appEnrichedCaption.SetTextWrapStyle(TEXT_WRAP_NONE);
139                 appEnrichedCaption.SetTextAbbreviationEnabled(true);
140                 appEnrichedCaption.Add(captionElement);
141
142                 // app name shadow text
143                 TextElement shadowCaptionElement;
144                 r = shadowCaptionElement.Construct(appCaption);
145                 TryCatch(r == E_SUCCESS, , "shadowCaptionElement.Construct failed with error = %s", GetErrorMessage(r));
146                 shadowCaptionElement.SetTextColor(COLOR_APP_CAPTION_SHADOW2);
147                 shadowCaptionElement.SetFont(font);
148                 shadowCaptionElement.SetOutlineColor(COLOR_APP_CAPTION_SHADOW2);
149                 appEnrichedShadowCaption.Construct(Dimension(W_APPLICATION_ICON, H_APP_CAPTION_TEXT));
150                 appEnrichedShadowCaption.SetHorizontalAlignment(TEXT_ALIGNMENT_CENTER);
151                 appEnrichedShadowCaption.SetVerticalAlignment(TEXT_ALIGNMENT_MIDDLE);
152                 appEnrichedShadowCaption.SetTextWrapStyle(TEXT_WRAP_NONE);
153                 appEnrichedShadowCaption.SetTextAbbreviationEnabled(true);
154                 appEnrichedShadowCaption.Add(shadowCaptionElement);
155
156                 pCanvas->DrawText(Point(0, Y_APP_ICON_IMAGE_START + APP_ICON_IMAGE_SIZE + 2), appEnrichedShadowCaption);
157                 pCanvas->DrawText(Point(0, Y_APP_ICON_IMAGE_START + APP_ICON_IMAGE_SIZE), appEnrichedCaption);
158         }
159
160         pAppIconBitmap = new (std::nothrow) Bitmap();
161         TryCatch(pAppIconBitmap != null, , "Failed to allocate for Bitmap");
162         r = pAppIconBitmap->Construct(*pCanvas, Rectangle(0, 0, W_APPLICATION_ICON, H_APPLICATION_ICON));
163         TryCatch(r == E_SUCCESS, , "pCanvas->Construct failed with error %s", GetErrorMessage(r));
164
165         if (pBitmap != null)
166         {
167                 delete pBitmap;
168                 pBitmap = null;
169         }
170
171         if (pCanvas != null)
172         {
173                 delete pCanvas;
174                 pCanvas = null;
175         }
176
177         return pAppIconBitmap;
178
179 CATCH:
180
181         if (pBitmap != null)
182         {
183                 delete pBitmap;
184                 pBitmap = null;
185         }
186
187         if (pCanvas != null)
188         {
189                 delete pCanvas;
190                 pCanvas = null;
191         }
192
193         if (pAppIconBitmap != null)
194         {
195                 delete pAppIconBitmap;
196                 pAppIconBitmap = null;
197         }
198
199         return null;
200 }
201
202 Bitmap*
203 ApplicationUtils::GetBitmapN(const String& imageAbsolutePath)
204 {
205         result r = E_SUCCESS;
206         Bitmap* pBitmap = null;
207         Image* pImage = new (std::nothrow) Image();
208
209         String fullname;
210         fullname.Append(imageAbsolutePath);
211
212         if (pImage != null)
213         {
214                 r = pImage->Construct();
215                 // goes to CATCH to release memory and return null if failed to construct Image
216                 TryCatch(r == E_SUCCESS, , "Image Construct Failed = (%s)\n", GetErrorMessage(r));
217                 // creates bitmap for the specified path of the image which is of jpeg format.
218                 if (fullname.EndsWith(L"jpg") || fullname.EndsWith(L"JPG") || fullname.EndsWith(L"jpeg") || fullname.EndsWith(L"JPEG"))
219                 {
220                         pBitmap = pImage->DecodeN(fullname, BITMAP_PIXEL_FORMAT_RGB565);
221                 }
222                 // creates bitmap for the specified path of the image which is of bmp format.
223                 else if (fullname.EndsWith(L"bmp") || fullname.EndsWith(L"BMP"))
224                 {
225                         pBitmap = pImage->DecodeN(fullname, BITMAP_PIXEL_FORMAT_RGB565);
226                 }
227                 // creates bitmap for the specified path of the image which is of png and wbmp formats.
228                 else if (fullname.EndsWith(L"png") || fullname.EndsWith(L"PNG") || fullname.EndsWith(L"wbmp") || fullname.EndsWith(L"WBMP"))
229                 {
230                         pBitmap = pImage->DecodeN(fullname, BITMAP_PIXEL_FORMAT_ARGB8888);
231                 }
232                 // creates bitmap for the specified path of the image which is of gif format.
233                 else if (fullname.EndsWith(L"gif") || fullname.EndsWith(L"GIF"))
234                 {
235                         pBitmap = pImage->DecodeN(fullname, BITMAP_PIXEL_FORMAT_RGB565);
236                 }
237
238                 // goes to CATCH to release memory and return null if failed to construct bitmap
239                 TryCatch(pBitmap != null, , "not found image path or not enough memory %ls error = %s", fullname.GetPointer()
240                                 , GetErrorMessage(GetLastResult()));
241
242                 delete pImage;
243                 return pBitmap;
244         }
245
246 CATCH:
247
248         if (pImage != null)
249         {
250                 delete pImage;
251         }
252
253         return null;
254 }
255
256 Bitmap*
257 ApplicationUtils::GetResourceBitmapN(const String& imageName)
258 {
259         //sanity test
260         TryReturn(!imageName.IsEmpty(), null, "Invalid parameter");
261
262         AppResource* pAppResource = UiApp::GetInstance()->GetAppResource();
263         Bitmap* pBitmap = null;
264
265         // creates bitmap for the specified image name from the application resource.
266         pBitmap = pAppResource->GetBitmapN(imageName, BITMAP_PIXEL_FORMAT_ARGB8888);
267
268         TryReturn(pBitmap != null, pBitmap, "failed to create bitmap error = %s", GetErrorMessage(GetLastResult()));
269
270         return pBitmap;
271 }
272
273 result
274 ApplicationUtils::GetStringResource(const String& stringId, String& retString)
275 {
276         result r = E_INVALID_ARG;
277
278         if (!stringId.IsEmpty())
279         {
280                 AppResource* pAppResource = UiApp::GetInstance()->GetAppResource();
281                 r = pAppResource->GetString(stringId, retString);
282         }
283
284         return r;
285 }
286
287 //Shows delete confirmation message box
288 int
289 ApplicationUtils::ShowDeletePopup(String& applicationName)
290 {
291         MessageBox messsageBox;
292         String message;
293         String messageTitle;
294         String displayMessage;
295         int modalResult = 0;
296         //get the resource string
297         GetStringResource(L"IDS_IDLE_POP_UNINSTALL_PS_Q", message);
298         //format the message by including the application name
299         displayMessage.Format(256, message.GetPointer(), applicationName.GetPointer());
300         messsageBox.Construct(messageTitle, displayMessage, MSGBOX_STYLE_YESNO);
301         //Show message box and wait for user response
302         messsageBox.ShowAndWait(modalResult);
303
304         return modalResult;
305 }
306
307 ApplicationUtils::ApplicationUtils(void)
308 {
309         //  No implementation required
310 }
311
312 ApplicationUtils::~ApplicationUtils(void)
313 {
314         //  No implementation required
315 }