26581455c1b3af33881a5d73998480a22de65996
[apps/osp/Gallery.git] / src / GlAlbumListForm.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Flora License, Version 1.0 (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                GlAlbumListForm.cpp
19  * @brief               This is the implementation file for AlbumListForm class.
20  */
21
22 #include <FApp.h>
23 #include "GlAlbumInfo.h"
24 #include "GlAlbumListForm.h"
25 #include "GlAlbumListPresentationModel.h"
26 #include "GlGalleryApp.h"
27 #include "GlResourceManager.h"
28
29 using namespace Tizen::App;
30 using namespace Tizen::Base;
31 using namespace Tizen::Base::Collection;
32 using namespace Tizen::Base::Runtime;
33 using namespace Tizen::Content;
34 using namespace Tizen::Graphics;
35 using namespace Tizen::Io;
36 using namespace Tizen::Media;
37 using namespace Tizen::System;
38 using namespace Tizen::Ui;
39 using namespace Tizen::Ui::Controls;
40 using namespace Tizen::Ui::Scenes;
41
42 static const int W_FOLDER_SIZE = 334;
43 static const int H_FOLDER_SIZE = 334;
44
45 AlbumListForm::AlbumListForm(void)
46         : __pAlbumViewIconList(null)
47         , __pContextMenuMore(null)
48         , __itemCount(0)
49         , __pPresentationModel(null)
50 {
51         AppLogDebug("ENTER");
52         __overlayMsg = false;
53         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
54 }
55
56 AlbumListForm::~AlbumListForm(void)
57 {
58         AppLogDebug("ENTER");
59         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
60 }
61
62 result
63 AlbumListForm::Initialize(void)
64 {
65         AppLogDebug("ENTER");
66         result r = Form::Construct(IDL_FORM_ALBUM_LIST);
67         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
68
69         return r;
70 }
71
72 result
73 AlbumListForm::OnInitializing(void)
74 {
75         AppLogDebug("ENTER");
76         result r = E_SUCCESS;
77
78         __pPresentationModel = AlbumListPresentationModel::GetInstance();
79         __pPresentationModel->ClearThumbnailRequests();
80         __pPresentationModel->AddPresentationModelListener(this);
81
82         if (__pPresentationModel->GetAppControlMode() == APP_CONTROL_MODE_PICK)
83         {
84                 if (__pPresentationModel->GetAppControlMediaType() == APPCONTROL_MEDIA_TYPE_IMAGE)
85                 {
86                         __pPresentationModel->InitializeAlbumInfoList(CONTENT_TYPE_IMAGE);
87                 }
88                 else if (__pPresentationModel->GetAppControlMediaType() == APPCONTROL_MEDIA_TYPE_VIDEO)
89                 {
90                         __pPresentationModel->InitializeAlbumInfoList(CONTENT_TYPE_VIDEO);
91                 }
92                 else
93                 {
94                         __pPresentationModel->InitializeAlbumInfoList(CONTENT_TYPE_VIDEO);
95                 }
96         }
97         else
98         {
99                 __pPresentationModel->InitializeAlbumInfoList(CONTENT_TYPE_ALL);
100         }
101
102         AppLogDebug("ENTER __pPresentationModel(%x)", __pPresentationModel);
103
104         r = InitializeHeader();
105         if (r != E_SUCCESS)
106         {
107                 AppLogDebug("[%s] Unable to set header.", GetErrorMessage(r));
108         }
109
110         r = InitializeFooter();
111         if (r != E_SUCCESS)
112         {
113                 AppLogDebug("[%s] Unable to set footer.", GetErrorMessage(r));
114         }
115
116         __itemCount = __pPresentationModel->GetFolderCount();
117         CreateIconListView();
118         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
119
120         return r;
121 }
122
123 result
124 AlbumListForm::InitializeHeader(void)
125 {
126         AppLogDebug("ENTER");
127         Header* pHeader = GetHeader();
128         if (pHeader == null)
129         {
130                 AppLogDebug("EXIT 1(%s)", GetErrorMessage(GetLastResult()));
131
132                 return E_SYSTEM;
133         }
134
135         result r = pHeader->SetStyle(HEADER_STYLE_TITLE);
136         if (r != E_SUCCESS)
137         {
138                 AppLogDebug("EXIT 2(%s)", GetErrorMessage(GetLastResult()));
139
140                 return r;
141         }
142
143         pHeader->SetTitleText(ResourceManager::GetString(L"IDS_COM_BODY_GALLERY"));
144         pHeader->AddActionEventListener(*this);
145
146         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
147         return E_SUCCESS;
148 }
149
150 result
151 AlbumListForm::InitializeFooter(void)
152 {
153         AppLogDebug("ENTER");
154         Footer* pFooter = GetFooter();
155         if (pFooter == null)
156         {
157                 AppLogDebug("EXIT 1");
158
159                 return E_SYSTEM;
160         }
161
162         if (__pPresentationModel->GetAppControlMode() == APP_CONTROL_MODE_PICK)
163         {
164                 pFooter->SetBackButton();
165                 SetFormBackEventListener(this);
166         }
167         else
168         {
169                 pFooter->SetStyle(FOOTER_STYLE_BUTTON_ICON);
170                 pFooter->SetBackButton();
171                 pFooter->RemoveAllItems();
172
173                 FooterItem itemCamera;
174                 itemCamera.Construct(ACTION_ID_FOOTER_CAMERA);
175                 itemCamera.SetText(ResourceManager::GetString(L"EMPTY_SPACE"));
176                 Bitmap* pBitmap1 = ResourceManager::GetBitmapN(IDB_CONTROLBAR_CAMERA);
177                 if (pBitmap1 != null)
178                 {
179                         itemCamera.SetIcon(FOOTER_ITEM_STATUS_NORMAL, pBitmap1);
180                 }
181                 pFooter->AddItem(itemCamera);
182                 delete pBitmap1;
183                 pBitmap1 = null;
184
185                 ButtonItem footerMore;
186                 footerMore.Construct(BUTTON_ITEM_STYLE_ICON, ACTION_ID_FOOTER_MORE);
187                 Bitmap* pBitmapMore = ResourceManager::GetBitmapN(IDB_CONTROLBAR_MORE);
188                 if (pBitmapMore != null)
189                 {
190                         footerMore.SetIcon(BUTTON_ITEM_STATUS_NORMAL, pBitmapMore);
191                 }
192                 pFooter->SetButton(BUTTON_POSITION_LEFT,footerMore);
193                 delete pBitmapMore;
194                 pBitmapMore = null;
195
196                 __pContextMenuMore = new (std::nothrow) ContextMenu();
197                 __pContextMenuMore->Construct(Point(0, 0), CONTEXT_MENU_STYLE_LIST);
198                 __pContextMenuMore->AddItem(ResourceManager::GetString(L"IDS_COM_HEADER_EDIT"), ACTION_ID_FOOTER_EDIT);
199                 __pContextMenuMore->SetShowState(false);
200                 __pContextMenuMore->AddActionEventListener(*this);
201
202                 SetFormBackEventListener(this);
203         }
204         pFooter->AddActionEventListener(*this);
205         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
206
207         return E_SUCCESS;
208 }
209
210 void
211 AlbumListForm::CreateIconListView(void)
212 {
213         AppLogDebug("ENTER");
214         Dimension itemSize(W_FOLDER_SIZE, H_FOLDER_SIZE);
215
216         __pAlbumViewIconList = static_cast< IconListView* >(GetControl(L"IDC_ICONLISTVIEW_ALBUM_LIST"));
217         AppAssert(__pAlbumViewIconList);
218
219         __pAlbumViewIconList->SetItemProvider(*this);
220         __pAlbumViewIconList->SetItemBorderStyle(ICON_LIST_VIEW_ITEM_BORDER_STYLE_NONE);
221         __pAlbumViewIconList->AddIconListViewItemEventListener(*this);
222
223         Bitmap* pBitmap = ResourceManager::GetBitmapN(IDB_LISTVIEW_EMPTY);
224         if (pBitmap != null)
225         {
226                 __pAlbumViewIconList->SetBitmapOfEmptyList(pBitmap);
227                 delete pBitmap;
228         }
229
230         __pAlbumViewIconList->SetShowState(true);
231         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
232 }
233
234 int
235 AlbumListForm::GetItemCount(void)
236 {
237         AppLogDebug("ENTER");
238         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
239
240         return  __itemCount;
241 }
242
243 IconListViewItem*
244 AlbumListForm::CreateItem(int index)
245 {
246         AppLogDebug("ENTER : index(%d)", index);
247
248         Bitmap* pBitmap = null;
249         IList* pAlbumInfoList = __pPresentationModel->GetAlbumInfoList();
250         if (pAlbumInfoList != null)
251         {
252                 AlbumInfo* pAlbumInfo = static_cast<AlbumInfo*>(pAlbumInfoList->GetAt(index));
253                 if (pAlbumInfo != null)
254                 {
255                         Bitmap* pAlbumBitmap = pAlbumInfo->GetFolderThumnailBitmap();
256
257                         if (pAlbumBitmap == null)
258                         {
259                                 __pPresentationModel->RequestThumbnail(index, 0);
260                         }
261
262                         pBitmap = __pPresentationModel->CreateMergeBitmapN(index);
263
264                         if (pBitmap != null)
265                         {
266                                 IconListViewItem* pIconListview = new (std::nothrow) IconListViewItem();
267                                 pIconListview->Construct(*pBitmap);
268                                 delete pBitmap;
269                                 AppLogDebug("EXIT 1(%s)", GetErrorMessage(GetLastResult()));
270
271                                 return pIconListview;
272                         }
273                 }
274         }
275         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
276
277         return null;
278 }
279
280 void
281 AlbumListForm::OnIconListViewItemStateChanged(IconListView& view, int index, IconListViewItemStatus status)
282 {
283         AppLogDebug("ENTER");
284         if (status == ICON_LIST_VIEW_ITEM_SELECTED)
285         {
286                 SceneManager* pSceneManager = SceneManager::GetInstance();
287
288                 IList* pAlbumInfoList = __pPresentationModel->GetAlbumInfoList();
289                 AlbumInfo* pAlbumInfo = static_cast<AlbumInfo*>(pAlbumInfoList->GetAt(index));
290                 AlbumInfo* pSendAlbumInfo = new (std::nothrow) AlbumInfo();
291                 pSendAlbumInfo->Construct(*pAlbumInfo);
292
293                 ArrayList* aList = new (std::nothrow) ArrayList(SingleObjectDeleter);
294                 aList->Construct();
295                 aList->Add(pSendAlbumInfo);
296
297                 if (__pPresentationModel->GetAppControlMode() == APP_CONTROL_MODE_PICK)
298                 {
299                         if (__pPresentationModel->GetAppControlMediaType() == APPCONTROL_MEDIA_TYPE_IMAGE)
300                         {
301                                 pSceneManager->GoForward(ForwardSceneTransition(IDSCN_IMAGE_LIST_EDITOR), aList);
302                         }
303                         else if  (__pPresentationModel->GetAppControlMediaType() == APPCONTROL_MEDIA_TYPE_VIDEO)
304                         {
305                                 pSceneManager->GoForward(ForwardSceneTransition(IDSCN_VIDEO_LIST_EDITOR), aList);
306                         }
307                         else
308                         {
309                                 pSceneManager->GoForward(ForwardSceneTransition(IDSCN_IMAGE_LIST_EDITOR), aList);
310                         }
311                 }
312                 else
313                 {
314                         pSceneManager->GoForward(ForwardSceneTransition(IDSCN_ALL_LIST), aList);
315                 }
316         }
317         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
318 }
319
320 bool
321 AlbumListForm::DeleteItem(int index, IconListViewItem* pItem)
322 {
323         AppLogDebug("ENTER");
324         delete pItem;
325         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
326
327         return true;
328 }
329
330 result
331 AlbumListForm::OnTerminating(void)
332 {
333         AppLogDebug("ENTER");
334         __pPresentationModel->RemovePresentationModelListener(*this);
335
336         if (__pContextMenuMore != null)
337         {
338                 delete __pContextMenuMore;
339                 __pContextMenuMore = null;
340         }
341         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
342
343         return E_SUCCESS;
344 }
345
346 void
347 AlbumListForm::OnActionPerformed(const Control& source, int actionId)
348 {
349         AppLogDebug("ENTER");
350         SceneManager* pSceneManager = SceneManager::GetInstance();
351
352         switch (actionId)
353         {
354         case ACTION_ID_FOOTER_CAMERA:
355                 {
356                         HashMap* pDataList = new (std::nothrow) HashMap(SingleObjectDeleter);
357                         pDataList->Construct();
358                         pDataList->Add(new (std::nothrow) String(APPCONTROL_KEY_TYPE),
359                                         new (std::nothrow) String(APPCONTROL_DATA_CAMERA));
360
361                         __pPresentationModel->StartAppControl(APPCONTROL_PROVIDER_ID_CAMERA,
362                                         APPCONTROL_OPERATION_ID_CAPTURE, pDataList, null);
363                 }
364                 break;
365         case ACTION_ID_FOOTER_EDIT:
366                 {
367                         pSceneManager->GoForward(ForwardSceneTransition(IDSCN_ALBUM_LIST_EDITOR));
368                 }
369                 break;
370         case ACTION_ID_FOOTER_MORE:
371                 {
372                         if (__pContextMenuMore->GetShowState() == false && __pPresentationModel->GetFolderCount() != 0)
373                         {
374                                 Rectangle rect = source.GetBounds();
375                                 __pContextMenuMore->SetAnchorPosition(Point(rect.width/8, rect.y));
376                                 __pContextMenuMore->SetShowState(true);
377                                 __pContextMenuMore->Show();
378                         }
379                         else
380                         {
381                                 __pContextMenuMore->SetShowState(false);
382                         }
383                 }
384                 break;
385         default:
386                 {
387                 }
388                 break;
389         }
390         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
391 }
392
393 void
394 AlbumListForm::OnFormBackRequested(Form& source)
395 {
396         AppLogDebug("ENTER");
397         if (__pPresentationModel->GetAppControlMode() == APP_CONTROL_MODE_PICK)
398         {
399                 AppLogDebug("APP_CONTROL_MODE_PICK");
400                 GalleryApp* pApp = dynamic_cast<GalleryApp*>(UiApp::GetInstance());
401                 if (pApp != null)
402                 {
403                         if (__pPresentationModel->GetAppControlMode() == APP_CONTROL_MODE_PICK)
404                         {
405                                 pApp->SendAppControlResult(APP_CTRL_RESULT_CANCELED, null);
406                         }
407                         pApp->Terminate();
408                 }
409         }
410         else
411         {
412                 AppLogDebug("APP_CONTROL_MODE_MAIN");
413                 UiApp* pApp = UiApp::GetInstance();
414                 pApp->Terminate();
415         }
416         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
417 }
418
419 void
420 AlbumListForm::OnSceneActivatedN(const SceneId& previousSceneId,
421                                                                                   const SceneId& currentSceneId, IList* pArgs)
422 {
423         AppLogDebug("ENTER");
424
425         if (__pPresentationModel->GetAppControlMode() == APP_CONTROL_MODE_PICK)
426         {
427                 if (__pPresentationModel->GetAppControlMediaType() == APPCONTROL_MEDIA_TYPE_IMAGE)
428                 {
429                         __pPresentationModel->InitializeAlbumInfoList(CONTENT_TYPE_IMAGE);
430                 }
431                 else if (__pPresentationModel->GetAppControlMediaType() == APPCONTROL_MEDIA_TYPE_VIDEO)
432                 {
433                         __pPresentationModel->InitializeAlbumInfoList(CONTENT_TYPE_VIDEO);
434                 }
435                 else
436                 {
437                         __pPresentationModel->InitializeAlbumInfoList(CONTENT_TYPE_VIDEO);
438                 }
439         }
440         else
441         {
442                 __pPresentationModel->InitializeAlbumInfoList(CONTENT_TYPE_ALL);
443         }
444         __itemCount = __pPresentationModel->GetFolderCount();
445
446         if (__pAlbumViewIconList != null)
447         {
448                 __pAlbumViewIconList->UpdateList();
449         }
450         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
451 }
452
453 void
454 AlbumListForm::OnSceneDeactivated(const SceneId& currentSceneId,
455                                                                                    const SceneId& nextSceneId)
456 {
457         AppLogDebug("ENTER");
458         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
459 }
460
461 void
462 AlbumListForm::OnUpdateContentList()
463 {
464         AppLogDebug("ENTER");
465
466         if (__pPresentationModel->GetAppControlMode() == APP_CONTROL_MODE_PICK)
467         {
468                 if (__pPresentationModel->GetAppControlMediaType() == APPCONTROL_MEDIA_TYPE_IMAGE)
469                 {
470                         __pPresentationModel->InitializeAlbumInfoList(CONTENT_TYPE_IMAGE);
471                 }
472                 else if (__pPresentationModel->GetAppControlMediaType() == APPCONTROL_MEDIA_TYPE_VIDEO)
473                 {
474                         __pPresentationModel->InitializeAlbumInfoList(CONTENT_TYPE_VIDEO);
475                 }
476                 else
477                 {
478                         __pPresentationModel->InitializeAlbumInfoList(CONTENT_TYPE_VIDEO);
479                 }
480         }
481         else
482         {
483                 __pPresentationModel->InitializeAlbumInfoList(CONTENT_TYPE_ALL);
484         }
485
486         __itemCount = __pPresentationModel->GetFolderCount();
487         __pAlbumViewIconList->UpdateList();
488         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
489 }
490
491 void
492 AlbumListForm::OnFileInfoChanged(const ContentType contentType)
493 {
494         AppLogDebug("ENTER");
495         Update();
496         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
497 }
498
499 void
500 AlbumListForm::OnThumbnailDecoded(const int index)
501 {
502         AppLogDebug("ENTER : index(%d)", index);
503         __pAlbumViewIconList->RefreshList(index, LIST_REFRESH_TYPE_ITEM_MODIFY);
504         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
505 }
506
507 result
508 AlbumListForm::Update(void)
509 {
510         AppLogDebug("ENTER");
511         result r = __pAlbumViewIconList->UpdateList();
512         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
513
514         return r;
515 }