Applied latest source code
[apps/native/preloaded/Settings.git] / src / StWallpaperAlbumListForm.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                StWallpaperAlbumListForm.cpp
19  * @brief               This is the implementation file for WallpaperAlbumListForm class.
20  */
21
22 #include "StSettingScenesList.h"
23 #include "StTypes.h"
24 #include "StWallpaperAlbumListForm.h"
25
26 using namespace Tizen::Base;
27 using namespace Tizen::Base::Collection;
28 using namespace Tizen::Graphics;
29 using namespace Tizen::System;
30 using namespace Tizen::Ui;
31 using namespace Tizen::Ui::Controls;
32 using namespace Tizen::Ui::Scenes;
33
34 static const int W_FOLDER_SIZE = 334;
35 static const int H_FOLDER_SIZE = 334;
36
37 static const int W_LARGE_SIZE_TEXT_GAP = 110;
38 static const int W_HUGE_SIZE_PORTRAIT_TEXT_GAP = 90;
39 static const int W_HUGE_SIZE_LANDSCAPE_TEXT_GAP = 220;
40 static const int LINE_COUNT_DEFAULT = 1;
41 static const int BASE_FORM_DEFAULT_MARGIN = 32;
42
43 WallpaperAlbumListForm::WallpaperAlbumListForm(void)
44         : __itemCount(0)
45         , __pPresentationModel(null)
46         , __pIconListView(null)
47 {
48 }
49
50 WallpaperAlbumListForm::~WallpaperAlbumListForm(void)
51 {
52 }
53
54 result
55 WallpaperAlbumListForm::Initialize(void)
56 {
57         AppLogDebug("ENTER");
58         Form::Construct(FORM_STYLE_NORMAL | FORM_STYLE_INDICATOR | FORM_STYLE_HEADER | FORM_STYLE_FOOTER);
59         SetOrientationAutoMode();
60         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
61         return GetLastResult();
62 }
63
64 void
65 WallpaperAlbumListForm::SetOrientationAutoMode(void)
66 {
67         this->SetOrientation(ORIENTATION_AUTOMATIC_FOUR_DIRECTION);
68         this->AddOrientationEventListener(*this);
69 }
70
71 void
72 WallpaperAlbumListForm::CreateHeader(const Tizen::Base::String& textTitle)
73 {
74         Header* pHeader = GetHeader();
75
76         pHeader->SetStyle(HEADER_STYLE_TITLE);
77         pHeader->SetTitleText(textTitle);
78 }
79
80 void
81 WallpaperAlbumListForm::CreateFooter(void)
82 {
83         Footer* pFooter = GetFooter();
84         AppAssert(pFooter);
85
86         pFooter->SetStyle(FOOTER_STYLE_BUTTON_TEXT);
87
88         SetFormBackEventListener(this);
89 }
90
91 result
92 WallpaperAlbumListForm::OnInitializing(void)
93 {
94         DeviceManager::AddDeviceEventListener(DEVICE_TYPE_STORAGE_CARD, *this);
95         return E_SUCCESS;
96 }
97
98 result
99 WallpaperAlbumListForm::OnTerminating(void)
100 {
101         AppLogDebug("ENTER");
102         if (__pPresentationModel != null)
103         {
104                 __pPresentationModel = null;
105         }
106
107         if (__pIconListView != null)
108         {
109                 __pIconListView = null;
110         }
111
112         DeviceManager::RemoveDeviceEventListener(DEVICE_TYPE_STORAGE_CARD, *this);
113         return E_SUCCESS;
114 }
115
116 int
117 WallpaperAlbumListForm::GetItemCount(void)
118 {
119         return __itemCount;
120 }
121
122 IconListViewItem*
123 WallpaperAlbumListForm::CreateItem(int index)
124 {
125         AppLogDebug("ENTER : index(%d)", index);
126
127         Bitmap* pBitmap = __pPresentationModel->CreateMergeBitmapN(index);
128
129         if (pBitmap != null)
130         {
131                 IconListViewItem* pIconListview = new (std::nothrow) IconListViewItem();
132                 pIconListview->Construct(*pBitmap);
133                 delete pBitmap;
134                 pBitmap = null;
135
136                 AppLogDebug("EXIT 1(%s)", GetErrorMessage(GetLastResult()));
137                 return pIconListview;
138         }
139
140         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
141         return null;
142 }
143
144 bool
145 WallpaperAlbumListForm::DeleteItem(int index, IconListViewItem* pItem)
146 {
147         delete pItem;
148         return true;
149 }
150
151 void
152 WallpaperAlbumListForm::OnIconListViewItemStateChanged(IconListView& view, int index, IconListViewItemStatus status)
153 {
154         if (status == ICON_LIST_VIEW_ITEM_SELECTED)
155         {
156                 SceneManager* pSceneManager = SceneManager::GetInstance();
157
158                 IList* pAlbumInfoList = __pPresentationModel->GetAlbumInfoList();
159                 AlbumInfo* pAlbumInfo = static_cast<AlbumInfo*>(pAlbumInfoList->GetAt(index));
160                 ArrayList* pAlbumPathList = new (std::nothrow) ArrayList(SingleObjectDeleter);
161                 pAlbumPathList->Construct();
162                 if (index == 0)
163                 {
164                         String allAlbumsString = ResourceManager::GetString(L"IDS_MEDIABR_BODY_ALL_ALBUMS");
165                         pAlbumPathList->Add(new (std::nothrow) String(allAlbumsString));
166                 }
167                 for (int i = 0; i < pAlbumInfo->GetDirectoryCount(); i++)
168                 {
169                         pAlbumPathList->Add(new (std::nothrow) String(pAlbumInfo->GetDirectory(i)));
170                 }
171
172                 pSceneManager->GoForward(ForwardSceneTransition(IDSCN_WALLPAPER_ALBUM_LIST_DETAIL, SCENE_TRANSITION_ANIMATION_TYPE_LEFT), pAlbumPathList);
173         }
174         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
175 }
176
177 void
178 WallpaperAlbumListForm::OnFormBackRequested(Tizen::Ui::Controls::Form& source)
179 {
180         SceneManager* pSceneManager = SceneManager::GetInstance();
181         AppAssert(pSceneManager);
182
183         pSceneManager->GoBackward(BackwardSceneTransition(SCENE_TRANSITION_ANIMATION_TYPE_RIGHT), null);
184 }
185
186 void
187 WallpaperAlbumListForm::OnSceneActivatedN(const Tizen::Ui::Scenes::SceneId& previousSceneId, const Tizen::Ui::Scenes::SceneId& currentSceneId, Tizen::Base::Collection::IList* pArgs)
188 {
189         if (__pPresentationModel == null)
190         {
191                 __pPresentationModel = WallpaperAlbumListPresentationModel::GetInstance();
192         }
193         if (pArgs != null)
194         {
195                 String* selectHomeLock = static_cast<String*>(pArgs->GetAt(0));
196                 __pPresentationModel->SetHomeLockArgument(selectHomeLock);
197         }
198
199         __pPresentationModel->InitializeAlbumInfoList();
200         __itemCount = __pPresentationModel->GetFolderCount();
201         UpdateIconListView();
202         CreateHeader(ResourceManager::GetString(L"IDS_MP_BODY_ADD_IMAGE"));
203 }
204
205 void
206 WallpaperAlbumListForm::OnSceneDeactivated(const Tizen::Ui::Scenes::SceneId& currentSceneId, const Tizen::Ui::Scenes::SceneId& nextSceneId)
207 {
208 }
209
210 void
211 WallpaperAlbumListForm::CreateIconListView(void)
212 {
213         if (__pIconListView != null)
214         {
215                 __pIconListView->UpdateList();
216                 return;
217         }
218         Dimension itemSize(W_FOLDER_SIZE, H_FOLDER_SIZE);
219
220         __pIconListView = new (std::nothrow) IconListView();
221         __pIconListView->Construct(Rectangle(0, 0, GetClientAreaBounds().width, GetClientAreaBounds().height),
222                 itemSize, ICON_LIST_VIEW_STYLE_NORMAL, ICON_LIST_VIEW_SCROLL_DIRECTION_VERTICAL);
223         __pIconListView->SetItemProvider(*this);
224         __pIconListView->SetItemBorderStyle(ICON_LIST_VIEW_ITEM_BORDER_STYLE_NONE);
225         __pIconListView->AddIconListViewItemEventListener(*this);
226
227         AddControl(__pIconListView);
228
229         Invalidate(true);
230 }
231
232 void
233 WallpaperAlbumListForm::OnOrientationChanged(const Tizen::Ui::Control& source, Tizen::Ui::OrientationStatus orientationStatus)
234 {
235         if (__pIconListView != null)
236         {
237                 __pIconListView->SetBounds(Rectangle(0, 0, GetClientAreaBounds().width, GetClientAreaBounds().height));
238         }
239         else
240         {
241                 Label* pLabel = static_cast<Label*>(this->GetControl(NO_CONTENTS, false));
242                 if (pLabel != null)
243                 {
244                         Rectangle clientRect = GetClientAreaBounds();
245                         Rectangle labelBound = pLabel->GetBounds();
246
247                         int xPos = (clientRect.width / LINE_COUNT_2) - (labelBound.width / LINE_COUNT_2);
248                         int yPos = (clientRect.height / LINE_COUNT_2) -(labelBound.height / LINE_COUNT_2);
249
250                         pLabel->SetBounds(Rectangle(xPos, yPos, labelBound.width, labelBound.height));
251                         yPos = yPos + pLabel->GetBounds().height;
252                         Label* pTextLabel = static_cast<Label*>(this->GetControl(NO_CONTENTS_TEXT, false));
253                         if (pTextLabel != null)
254                         {
255                                 pTextLabel->SetBounds(Rectangle(0, yPos, clientRect.width, pTextLabel->GetBounds().height));
256                         }
257                 }
258         }
259         Invalidate(true);
260 }
261
262 void
263 WallpaperAlbumListForm::OnUserEventReceivedN(RequestId requestId, Tizen::Base::Collection::IList* pArgs)
264 {
265         if (requestId == REFRESH_REQUEST_EVENT)
266         {
267                 __pPresentationModel->InitializeAlbumInfoList();
268                 __itemCount = __pPresentationModel->GetFolderCount();
269                 UpdateIconListView();
270                 if (pArgs)
271                 {
272                         pArgs->RemoveAll(true);
273                         delete pArgs;
274                 }
275         }
276 }
277
278 void
279 WallpaperAlbumListForm::UpdateIconListView(void)
280 {
281         bool createIconListView = true;
282         if (__itemCount == 0)
283         {
284                 createIconListView = false;
285         }
286         if (__pIconListView != null)
287         {
288                 if (createIconListView)
289                 {
290                         __pIconListView->UpdateList();
291                         return;
292                 }
293         }
294
295         int controlCount = GetControlCount();
296         for (int i = 0; i < controlCount; i++)
297         {
298                 RemoveControl(GetControl(0));
299         }
300 //      RemoveAllControls();
301
302         if (createIconListView)
303         {
304                 CreateIconListView();
305         }
306         else
307         {
308                 Label* pLabel = static_cast<Label*>(this->GetControl(NO_CONTENTS, false));
309                 if (pLabel == null)
310                 {
311                         Rectangle clientRect = GetClientAreaBounds();
312                         Bitmap* pBitmp = ResourceManager::GetBitmapN(IDB_NO_CONTENTS);
313
314                         int bitmapWidth = pBitmp->GetWidth();
315                         int bitmapHeight = pBitmp->GetHeight();
316                         String labelText = ResourceManager::GetString(L"IDS_ST_BODY_NO_CONTENT");
317                         Color textColor = COLOR_HELP_TEXT_TYPE_03_NORMAL;
318                         int noContentTextHeight = GetHeightForStringArea(labelText, bitmapWidth, 32);
319
320                         int xPos = (clientRect.width / LINE_COUNT_2) - (bitmapWidth / DIVIDE_BY_TWO);
321                         int yPos = (clientRect.height / LINE_COUNT_2) - ((bitmapHeight + noContentTextHeight) / DIVIDE_BY_TWO);
322
323                         Label* pLabel = new (std::nothrow) Label();
324                         pLabel->Construct(Rectangle(xPos, yPos, bitmapWidth, bitmapHeight), L"");
325                         pLabel->SetName(NO_CONTENTS);
326                         pLabel->SetBackgroundBitmap(*pBitmp);
327
328                         yPos = yPos + bitmapHeight;
329
330                         Label* pLabelNoContents = new (std::nothrow) Label();
331                         pLabelNoContents->Construct(Rectangle(0, yPos, clientRect.width, noContentTextHeight), L"");
332                         pLabelNoContents->SetName(NO_CONTENTS_TEXT);
333                         pLabelNoContents->SetTextColor(textColor);
334                         pLabelNoContents->SetText(labelText);
335
336                         AddControl(pLabel);
337                         AddControl(pLabelNoContents);
338                 }
339         }
340 }
341
342 int
343 WallpaperAlbumListForm::GetHeightForStringArea(const Tizen::Base::String source, int width, int fontSize) const
344 {
345         Font font;
346         Dimension dim;
347         String temp;
348
349         int lineCount = LINE_COUNT_DEFAULT;
350         int margin = BASE_FORM_DEFAULT_MARGIN + RELATIVE_LAYOUT_RIGHT_MARGIN;
351         if (fontSize > FONT_MAIN_TEXT_SIZE_LARGE)
352         {
353                 OrientationStatus orientationStatus = GetOrientationStatus();
354                 if (((orientationStatus == ORIENTATION_STATUS_PORTRAIT)
355                         || (orientationStatus == ORIENTATION_STATUS_PORTRAIT_REVERSE))
356                         && (fontSize == FONT_MAIN_TEXT_SIZE_HUGE))
357                 {
358                         width -= W_HUGE_SIZE_PORTRAIT_TEXT_GAP;
359                 }
360                 else if (((orientationStatus == ORIENTATION_STATUS_LANDSCAPE)
361                                 || (orientationStatus == ORIENTATION_STATUS_LANDSCAPE_REVERSE))
362                                 && (fontSize == FONT_MAIN_TEXT_SIZE_HUGE))
363                 {
364                         width -= W_HUGE_SIZE_LANDSCAPE_TEXT_GAP;
365                 }
366                 width -= W_LARGE_SIZE_TEXT_GAP;
367         }
368         int boudwidth = width - margin;
369         font.Construct(FONT_STYLE_PLAIN, fontSize);
370
371         for (int i = 0; i < source.GetLength(); i++)
372         {
373                 temp.Append(source[i]);
374                 font.GetTextExtent(temp, temp.GetLength(), dim);
375                 if (dim.width > boudwidth)
376                 {
377                         temp.Clear();
378                         temp.Append(source[i]);
379                         lineCount++;
380                 }
381         }
382
383         return dim.height * lineCount;
384 }
385
386 void
387 WallpaperAlbumListForm::OnDeviceStateChanged(DeviceType deviceType, const Tizen::Base::String& state)
388 {
389         AppLogDebug("ENTER");
390         if (deviceType == DEVICE_TYPE_STORAGE_CARD && state == DEVICE_STORAGE_CARD_UNMOUNTED)
391         {
392                 SceneManager::GetInstance()->GoBackward(BackwardSceneTransition(SCENE_TRANSITION_ANIMATION_TYPE_RIGHT), null);
393         }
394         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
395 }