Applied latest source code
[apps/native/preloaded/Settings.git] / src / StWallpaperImageSelectForm.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 express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 /**
18  * @file                StWallpaperImageSelectForm.cpp
19  * @brief               This is the implementation file for WallpaperImageSelectForm class.
20  */
21
22 #include <FContent.h>
23 #include <FIo.h>
24 #include "StSettingScenesList.h"
25 #include "StWallpaperAlbumListPresentationModel.h"
26 #include "StWallpaperImageSelectForm.h"
27
28 using namespace Tizen::App;
29 using namespace Tizen::Base;
30 using namespace Tizen::Base::Collection;
31 using namespace Tizen::Content;
32 using namespace Tizen::Graphics;
33 using namespace Tizen::Io;
34 using namespace Tizen::System;
35 using namespace Tizen::Ui;
36 using namespace Tizen::Ui::Controls;
37 using namespace Tizen::Ui::Scenes;
38
39 static const int IDA_POPUP_ACTION_ID_BASE = 200;
40 static const int IDA_FOOTER_SAVE = IDA_POPUP_ACTION_ID_BASE + 1;
41
42 WallpaperImageSelectForm::WallpaperImageSelectForm(void)
43         : __pContentId(null)
44         , __argIndex(0)
45 {
46 }
47
48 WallpaperImageSelectForm::~WallpaperImageSelectForm(void)
49 {
50 }
51
52 result
53 WallpaperImageSelectForm::Initialize(void)
54 {
55         AppLogDebug("ENTER");
56         Form::Construct(FORM_STYLE_NORMAL | FORM_STYLE_INDICATOR | FORM_STYLE_FOOTER);
57         SetOrientationAutoMode();
58         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
59         return GetLastResult();
60 }
61
62 void
63 WallpaperImageSelectForm::SetOrientationAutoMode(void)
64 {
65         this->SetOrientation(ORIENTATION_AUTOMATIC_FOUR_DIRECTION);
66         this->AddOrientationEventListener(*this);
67 }
68
69 void
70 WallpaperImageSelectForm::CreateFooter(void)
71 {
72         Footer* pFooter = GetFooter();
73         AppAssert(pFooter);
74
75         pFooter->SetStyle(FOOTER_STYLE_BUTTON_TEXT);
76
77         FooterItem footerSave;
78         footerSave.Construct(IDA_FOOTER_SAVE);
79         footerSave.SetText(ResourceManager::GetString(L"IDS_ST_BODY_SAVE"));
80
81         pFooter->AddItem(footerSave);
82         pFooter->AddActionEventListener(*this);
83         pFooter->SetItemEnabled(0, true);
84
85         SetFormBackEventListener(this);
86 }
87
88 result
89 WallpaperImageSelectForm::OnInitializing(void)
90 {
91         SetFormStyle(FORM_STYLE_NORMAL | FORM_STYLE_INDICATOR | FORM_STYLE_HEADER | FORM_STYLE_FOOTER | FORM_STYLE_LANDSCAPE_INDICATOR_AUTO_HIDE);
92         CreateFooter();
93         SetOrientationAutoMode();
94         return E_SUCCESS;
95 }
96
97 result
98 WallpaperImageSelectForm::OnTerminating(void)
99 {
100         AppLogDebug("ENTER");
101         return E_SUCCESS;
102 }
103
104 void
105 WallpaperImageSelectForm::OnFormBackRequested(Tizen::Ui::Controls::Form& source)
106 {
107         SceneManager* pSceneManager = SceneManager::GetInstance();
108         AppAssert(pSceneManager);
109
110         pSceneManager->GoBackward(BackwardSceneTransition(SCENE_TRANSITION_ANIMATION_TYPE_RIGHT), null);
111 }
112
113 void
114 WallpaperImageSelectForm::OnActionPerformed(const Tizen::Ui::Control& source, int actionId)
115 {
116         switch (actionId)
117         {
118         case IDA_FOOTER_SAVE:
119                 {
120                         ContentManager pContentManager;
121                         pContentManager.Construct();
122                         ImageContentInfo* pImageContentInfo = static_cast<ImageContentInfo*>(pContentManager.GetContentInfoN(*__pContentId));
123
124                         if (pImageContentInfo == null)
125                         {
126                                 MessageBox messageBox;
127                                 int result = E_SUCCESS;
128
129                                 messageBox.Construct(ResourceManager::GetString(L"IDS_EMAIL_POP_ALERT"), ResourceManager::GetString(L"IDS_COM_POP_FAILED"), MSGBOX_STYLE_NONE, MESSAGEBOX_DISPLAY_TIME_2_SEC);
130                                 messageBox.ShowAndWait(result);
131
132                                 SceneManager* pSceneManager = SceneManager::GetInstance();
133                                 AppAssert(pSceneManager);
134
135                                 pSceneManager->GoBackward(BackwardSceneTransition(SCENE_TRANSITION_ANIMATION_TYPE_RIGHT), null);
136                                 return;
137                         }
138
139                         String dstPath = Application::GetInstance()->GetAppSharedPath() + PATH_DATA_DIRECTORY;
140                         String srcPath = pImageContentInfo->GetContentPath();
141                         String srcFileExtention = File::GetFileExtension(srcPath);
142
143                         ArrayList* aList = new (std::nothrow) ArrayList(SingleObjectDeleter);
144                         aList->Construct();
145                         aList->Add(new (std::nothrow) String(Integer::ToString(__argIndex)));
146
147                         switch (__argIndex)
148                         {
149                         case 0:
150                                 {
151                                         dstPath.Append(L"home.");
152                                         dstPath.Append(srcFileExtention);
153                                         File::Copy(srcPath, dstPath, false);
154
155                                         if (SettingInfo::SetValue(RUNTIME_INFO_KEY_WALLPAPER_SCREEN, dstPath) != E_SUCCESS)
156                                         {
157                                                 AppLogDebug("Wallpapaer Screen SetValue is Fail [%s]", GetErrorMessage(GetLastResult()));
158                                         }
159                                 }
160                                 break;
161
162                         case 1:
163                                 {
164                                         dstPath.Append(L"lock.");
165                                         dstPath.Append(srcFileExtention);
166                                         File::Copy(srcPath, dstPath, false);
167
168                                         if (SettingInfo::SetValue(RUNTIME_INFO_KEY_WALLPAPER_SCREEN_LOCK, dstPath) != E_SUCCESS)
169                                         {
170                                                 AppLogDebug("Wallpapaer Lock Screen SetValue is Fail [%s]", GetErrorMessage(GetLastResult()));
171                                         }
172                                 }
173                                 break;
174
175                         case 2:
176                                 {
177                                         dstPath.Append(L"homelock.");
178                                         dstPath.Append(srcFileExtention);
179                                         File::Copy(srcPath, dstPath, false);
180
181                                         if (SettingInfo::SetValue(RUNTIME_INFO_KEY_WALLPAPER_SCREEN, dstPath) != E_SUCCESS)
182                                         {
183                                                 AppLogDebug("Wallpapaer Screen SetValue is Fail [%s]", GetErrorMessage(GetLastResult()));
184                                                 return;
185                                         }
186                                         if (SettingInfo::SetValue(RUNTIME_INFO_KEY_WALLPAPER_SCREEN_LOCK, dstPath) != E_SUCCESS)
187                                         {
188                                                 AppLogDebug("Wallpapaer Lock Screen SetValue is Fail [%s]", GetErrorMessage(GetLastResult()));
189                                                 return;
190                                         }
191                                 }
192                                 break;
193
194                         default:
195                                 break;
196                         }
197                         SceneManager* pSceneManager = SceneManager::GetInstance();
198                         AppAssert(pSceneManager);
199                         IListT<SceneId>* sceneList = pSceneManager->GetSceneHistoryN();
200                         pSceneManager->ClearSceneHistory();
201                         int sceneIndex = 0;
202                         while (sceneIndex < sceneList->GetCount())
203                         {
204                                 String* pSceneId = new (std::nothrow) String();
205                                 sceneList->GetAt(sceneIndex++, *pSceneId);
206                                 if ((pSceneId->Equals(IDSCN_WALLPAPER_ALBUM_LIST, true) == true)
207                                         || (pSceneId->Equals(IDSCN_WALLPAPER_ALBUM_LIST_DETAIL, true) == true))
208                                 {
209                                         if (pSceneManager->IsSceneAlive(*pSceneId) == true)
210                                         {
211                                                 pSceneManager->DestroyScene(*pSceneId);
212                                         }
213                                         delete pSceneId;
214                                         continue;
215                                 }
216
217                                 pSceneManager->AddToSceneHistory(*pSceneId);
218                                 delete pSceneId;
219                         }
220                         pSceneManager->GoBackward(BackwardSceneTransition(SCENE_TRANSITION_ANIMATION_TYPE_RIGHT), aList);
221                 }
222                 break;
223
224         default:
225                 {
226                         AppLogDebug("no search Tab Selected %d", actionId);
227                 }
228                 break;
229         }
230 }
231
232 void
233 WallpaperImageSelectForm::OnSceneActivatedN(const Tizen::Ui::Scenes::SceneId& previousSceneId, const Tizen::Ui::Scenes::SceneId& currentSceneId, Tizen::Base::Collection::IList* pArgs)
234 {
235         if (pArgs != null)
236         {
237                 String* pStr = WallpaperAlbumListPresentationModel::GetInstance()->GetHomeLockArgument();
238                 if (pStr->Equals(L"0", true))
239                 {
240                         __argIndex = 0;
241                 }
242                 else if (pStr->Equals(L"1", true))
243                 {
244                         __argIndex = 1;
245                 }
246                 else if (pStr->Equals(L"2", true))
247                 {
248                         __argIndex = 2;
249                 }
250                 __pContentId = static_cast<ContentId*>(pArgs->GetAt(0));
251         }
252         if (__pContentId != null)
253         {
254                 CreateImage();
255         }
256 }
257
258 void
259 WallpaperImageSelectForm::OnSceneDeactivated(const Tizen::Ui::Scenes::SceneId& currentSceneId, const Tizen::Ui::Scenes::SceneId& nextSceneId)
260 {
261 }
262
263 void
264 WallpaperImageSelectForm::CreateImage(void)
265 {
266         ContentManager pContentManager;
267         pContentManager.Construct();
268         ImageContentInfo* pImageContentInfo = static_cast<ImageContentInfo*>(pContentManager.GetContentInfoN(*__pContentId));
269
270         if (pImageContentInfo == null)
271         {
272                 AppLogDebug("pImageContentInfo is null");
273                 return;
274         }
275
276         Rectangle rect = GetClientAreaBounds();
277         Bitmap* pBitmap = ResourceManager::GetApplicationBitmapN(pImageContentInfo->GetContentPath());
278         Rectangle clientRect = GetClientAreaBounds();
279
280         float bitmapWidthRate = rect.width / (float)pBitmap->GetWidth();
281         float bitmapHeightRate = rect.height / (float)pBitmap->GetHeight();
282
283         int bitmapWidth = 0;
284         int bitmapHeight = 0;
285
286         if (bitmapWidthRate > bitmapHeightRate)
287         {
288                 bitmapHeight = pBitmap->GetHeight() * bitmapHeightRate;
289                 bitmapWidth = pBitmap->GetWidth() * bitmapHeightRate;
290         }
291         else
292         {
293                 bitmapHeight = pBitmap->GetHeight() * bitmapWidthRate;
294                 bitmapWidth = pBitmap->GetWidth() * bitmapWidthRate;
295         }
296         int xPos = (clientRect.width / LINE_COUNT_2) - (bitmapWidth / DIVIDE_BY_TWO);
297         int yPos = (clientRect.height / LINE_COUNT_2) - (bitmapHeight / DIVIDE_BY_TWO);
298
299         Label* pLabel = new (std::nothrow) Label();
300         pLabel->Construct(Rectangle(xPos, yPos, bitmapWidth, bitmapHeight), L"");
301         pLabel->SetName(NO_CONTENTS);
302         pLabel->SetBackgroundBitmap(*pBitmap);
303
304         AddControl(pLabel);
305 }
306
307 void
308 WallpaperImageSelectForm::OnOrientationChanged(const Tizen::Ui::Control& source, Tizen::Ui::OrientationStatus orientationStatus)
309 {
310         Label* pLabel = static_cast<Label*>(GetControl(NO_CONTENTS, true));
311         Rectangle rect = GetClientAreaBounds();
312         ContentManager pContentManager;
313         pContentManager.Construct();
314         ImageContentInfo* pImageContentInfo = static_cast<ImageContentInfo*>(pContentManager.GetContentInfoN(*__pContentId));
315         Bitmap* pBitmap = ResourceManager::GetApplicationBitmapN(pImageContentInfo->GetContentPath());
316         Rectangle clientRect = GetClientAreaBounds();
317
318         float bitmapWidthRate = rect.width / (float)pBitmap->GetWidth();
319         float bitmapHeightRate = rect.height / (float)pBitmap->GetHeight();
320
321         int bitmapWidth = 0;
322         int bitmapHeight = 0;
323
324         if (bitmapWidthRate > bitmapHeightRate)
325         {
326                 bitmapHeight = pBitmap->GetHeight() * bitmapHeightRate;
327                 bitmapWidth = pBitmap->GetWidth() * bitmapHeightRate;
328         }
329         else
330         {
331                 bitmapHeight = pBitmap->GetHeight() * bitmapWidthRate;
332                 bitmapWidth = pBitmap->GetWidth() * bitmapWidthRate;
333         }
334         int xPos = (clientRect.width / LINE_COUNT_2) - (bitmapWidth / DIVIDE_BY_TWO);
335         int yPos = (clientRect.height / LINE_COUNT_2) - (bitmapHeight / DIVIDE_BY_TWO);
336         pLabel->SetBounds(Rectangle(xPos, yPos, bitmapWidth, bitmapHeight));
337         Invalidate(true);
338 }