190954bfc96be10970cd56f09897b9c3f0c4de4b
[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.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                GlAlbumListForm.cpp
19  * @brief               This is the implementation file for AlbumListForm class.
20  */
21
22 #include "GlAlbumInfo.h"
23 #include "GlAlbumListForm.h"
24 #include "GlAlbumListPresentationModel.h"
25 #include "GlGalleryApp.h"
26 #include "GlResourceManager.h"
27
28 using namespace Tizen::App;
29 using namespace Tizen::Base;
30 using namespace Tizen::Base::Collection;
31 using namespace Tizen::Base::Runtime;
32 using namespace Tizen::Content;
33 using namespace Tizen::Graphics;
34 using namespace Tizen::Io;
35 using namespace Tizen::Media;
36 using namespace Tizen::System;
37 using namespace Tizen::Ui;
38 using namespace Tizen::Ui::Controls;
39 using namespace Tizen::Ui::Scenes;
40
41 static const int W_FOLDER_SIZE = 348;
42 static const int H_FOLDER_SIZE = 348;
43
44 AlbumListForm::AlbumListForm(void)
45         : __itemCount(0)
46         , __pAlbumViewIconList(null)
47         , __pOptionMenu(null)
48         , __pPresentationModel(null)
49
50 {
51         AppLogDebug("ENTER");
52         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
53 }
54
55 AlbumListForm::~AlbumListForm(void)
56 {
57         AppLogDebug("ENTER");
58         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
59 }
60
61 result
62 AlbumListForm::Initialize(void)
63 {
64         AppLogDebug("ENTER");
65         result r = Form::Construct(IDL_FORM_ALBUM_LIST);
66         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
67
68         return r;
69 }
70
71 result
72 AlbumListForm::OnInitializing(void)
73 {
74         AppLogDebug("ENTER");
75         result r = E_SUCCESS;
76
77         SetOrientation(ORIENTATION_AUTOMATIC);
78         AddOrientationEventListener(*this);
79
80         __pPresentationModel = AlbumListPresentationModel::GetInstance();
81         __pPresentationModel->ClearThumbnailRequests();
82         __pPresentationModel->AddPresentationModelListener(this);
83
84         if (__pPresentationModel->GetAppControlMode() == APPCONTROL_MODE_PICK)
85         {
86                 if (__pPresentationModel->GetAppControlMediaType() == APPCONTROL_MEDIA_TYPE_IMAGE)
87                 {
88                         __pPresentationModel->InitializeAlbumInfoList(CONTENT_TYPE_IMAGE);
89                 }
90                 else if (__pPresentationModel->GetAppControlMediaType() == APPCONTROL_MEDIA_TYPE_VIDEO)
91                 {
92                         __pPresentationModel->InitializeAlbumInfoList(CONTENT_TYPE_VIDEO);
93                 }
94                 else
95                 {
96                         __pPresentationModel->InitializeAlbumInfoList(CONTENT_TYPE_VIDEO);
97                 }
98         }
99         else
100         {
101                 __pPresentationModel->InitializeAlbumInfoList(CONTENT_TYPE_ALL);
102         }
103
104         AppLogDebug("ENTER __pPresentationModel(%x)", __pPresentationModel);
105
106         r = InitializeHeader();
107         if (r != E_SUCCESS)
108         {
109                 AppLogDebug("[%s] Unable to set header.", GetErrorMessage(r));
110         }
111
112         r = InitializeFooter();
113         if (r != E_SUCCESS)
114         {
115                 AppLogDebug("[%s] Unable to set footer.", GetErrorMessage(r));
116         }
117
118         __itemCount = __pPresentationModel->GetFolderCount();
119         CreateIconListView();
120         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
121
122         return r;
123 }
124
125 result
126 AlbumListForm::InitializeHeader(void)
127 {
128         AppLogDebug("ENTER");
129         Header* pHeader = GetHeader();
130         if (pHeader == null)
131         {
132                 AppLogDebug("EXIT 1(%s)", GetErrorMessage(GetLastResult()));
133                 return E_SYSTEM;
134         }
135
136         result r = pHeader->SetStyle(HEADER_STYLE_TITLE);
137         if (r != E_SUCCESS)
138         {
139                 AppLogDebug("EXIT 2(%s)", GetErrorMessage(GetLastResult()));
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                 return E_SYSTEM;
159         }
160
161         if (__pPresentationModel->GetAppControlMode() == APPCONTROL_MODE_PICK)
162         {
163                 SetFormBackEventListener(this);
164                 pFooter->SetShowState(false);
165         }
166         else
167         {
168                 pFooter->SetShowState(true);
169                 pFooter->SetStyle(FOOTER_STYLE_BUTTON_ICON);
170                 pFooter->RemoveAllItems();
171
172                 FooterItem itemCamera;
173                 itemCamera.Construct(IDA_FOOTER_CAMERA);
174                 itemCamera.SetText(ResourceManager::GetString(L"EMPTY_SPACE"));
175                 Bitmap* pBitmap1 = ResourceManager::GetBitmapN(IDB_CONTROLBAR_CAMERA);
176                 if (pBitmap1 != null)
177                 {
178                         itemCamera.SetIcon(FOOTER_ITEM_STATUS_NORMAL, pBitmap1);
179                 }
180                 pFooter->AddItem(itemCamera);
181                 delete pBitmap1;
182
183                 __pOptionMenu = new (std::nothrow) OptionMenu();
184                 __pOptionMenu->Construct();
185                 __pOptionMenu->AddItem(ResourceManager::GetString(L"IDS_COM_HEADER_EDIT"),IDA_FOOTER_EDIT);
186                 __pOptionMenu->AddItem(ResourceManager::GetString(L"IDS_COM_HEADER_EDIT"), IDA_FOOTER_EDIT);
187                 __pOptionMenu->SetShowState(false);
188                 __pOptionMenu->AddActionEventListener(*this);
189                 SetFormMenuEventListener(this);
190                 SetFormBackEventListener(this);
191         }
192
193         pFooter->AddActionEventListener(*this);
194         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
195
196         return E_SUCCESS;
197 }
198
199 void
200 AlbumListForm::CreateIconListView(void)
201 {
202         AppLogDebug("ENTER");
203         Dimension itemSize(W_FOLDER_SIZE, H_FOLDER_SIZE);
204
205         __pAlbumViewIconList = new IconListView();
206         __pAlbumViewIconList->Construct(Rectangle(0, 0, GetClientAreaBounds().width, GetClientAreaBounds().height),
207                     Dimension(W_FOLDER_SIZE, H_FOLDER_SIZE), ICON_LIST_VIEW_STYLE_NORMAL, ICON_LIST_VIEW_SCROLL_DIRECTION_VERTICAL);
208         __pAlbumViewIconList->SetItemProvider(*this);
209         __pAlbumViewIconList->SetItemBorderStyle(ICON_LIST_VIEW_ITEM_BORDER_STYLE_NONE);
210         __pAlbumViewIconList->AddIconListViewItemEventListener(*this);
211
212
213
214         Bitmap* pBitmap = ResourceManager::GetBitmapN(IDB_LISTVIEW_EMPTY);
215
216         if (pBitmap != null)
217         {
218                 __pAlbumViewIconList->SetBitmapOfEmptyList(pBitmap);
219                 delete pBitmap;
220         }
221         __pAlbumViewIconList->SetTextOfEmptyList(ResourceManager::GetString(L"IDS_COM_BODY_NO_ITEMS"));
222
223         AddControl(*__pAlbumViewIconList);
224
225         __pAlbumViewIconList->SetShowState(true);
226         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
227 }
228
229 int
230 AlbumListForm::GetItemCount(void)
231 {
232         AppLogDebug("ENTER");
233         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
234
235         return  __itemCount;
236 }
237
238 IconListViewItem*
239 AlbumListForm::CreateItem(int index)
240 {
241         AppLogDebug("ENTER : index(%d)", index);
242
243         Bitmap* pBitmap = null;
244         IList* pAlbumInfoList = __pPresentationModel->GetAlbumInfoList();
245         if (pAlbumInfoList != null)
246         {
247                 AlbumInfo* pAlbumInfo = static_cast<AlbumInfo*>(pAlbumInfoList->GetAt(index));
248                 if (pAlbumInfo != null)
249                 {
250                         Bitmap* pAlbumBitmap = pAlbumInfo->GetFolderThumnailBitmap();
251
252                         if (pAlbumBitmap == null)
253                         {
254                                 __pPresentationModel->RequestThumbnail(index, 0);
255                         }
256
257                         pBitmap = __pPresentationModel->CreateMergeBitmapN(index);
258
259                         if (pBitmap != null)
260                         {
261                                 IconListViewItem* pIconListview = new (std::nothrow) IconListViewItem();
262                                 pIconListview->Construct(*pBitmap);
263                                 delete pBitmap;
264                                 AppLogDebug("EXIT 1(%s)", GetErrorMessage(GetLastResult()));
265
266                                 return pIconListview;
267                         }
268                 }
269         }
270         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
271
272         return null;
273 }
274
275 void
276 AlbumListForm::OnIconListViewItemStateChanged(IconListView& view, int index, IconListViewItemStatus status)
277 {
278         AppLogDebug("ENTER");
279         if (status == ICON_LIST_VIEW_ITEM_SELECTED)
280         {
281                 SceneManager* pSceneManager = SceneManager::GetInstance();
282
283                 IList* pAlbumInfoList = __pPresentationModel->GetAlbumInfoList();
284                 AlbumInfo* pAlbumInfo = static_cast<AlbumInfo*>(pAlbumInfoList->GetAt(index));
285                 AlbumInfo* pSendAlbumInfo = new (std::nothrow) AlbumInfo();
286                 pSendAlbumInfo->Construct(*pAlbumInfo);
287
288                 ArrayList* pArrayList = new (std::nothrow) ArrayList(SingleObjectDeleter);
289                 pArrayList->Construct();
290                 pArrayList->Add(pSendAlbumInfo);
291
292                 if (__pPresentationModel->GetAppControlMode() == APPCONTROL_MODE_PICK)
293                 {
294                         if (__pPresentationModel->GetAppControlMediaType() == APPCONTROL_MEDIA_TYPE_IMAGE)
295                         {
296                                 pSceneManager->GoForward(ForwardSceneTransition(IDSCN_IMAGE_LIST_EDITOR), pArrayList);
297                         }
298                         else if (__pPresentationModel->GetAppControlMediaType() == APPCONTROL_MEDIA_TYPE_VIDEO)
299                         {
300                                 pSceneManager->GoForward(ForwardSceneTransition(IDSCN_VIDEO_LIST_EDITOR), pArrayList);
301                         }
302                         else
303                         {
304                                 pSceneManager->GoForward(ForwardSceneTransition(IDSCN_IMAGE_LIST_EDITOR), pArrayList);
305                         }
306                 }
307                 else
308                 {
309                         pSceneManager->GoForward(ForwardSceneTransition(IDSCN_ALL_LIST), pArrayList);
310                 }
311         }
312         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
313 }
314
315 bool
316 AlbumListForm::DeleteItem(int index, IconListViewItem* pItem)
317 {
318         AppLogDebug("ENTER");
319         delete pItem;
320         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
321
322         return true;
323 }
324
325 result
326 AlbumListForm::OnTerminating(void)
327 {
328         AppLogDebug("ENTER");
329         __pPresentationModel->RemovePresentationModelListener(*this);
330         __pPresentationModel->RemoveContentEventListener(*this);
331         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
332
333         return E_SUCCESS;
334 }
335
336 void
337 AlbumListForm::OnActionPerformed(const Control& source, int actionId)
338 {
339         AppLogDebug("ENTER");
340         SceneManager* pSceneManager = SceneManager::GetInstance();
341
342         switch (actionId)
343         {
344         case IDA_FOOTER_CAMERA:
345         {
346                 HashMap* pDataList = new (std::nothrow) HashMap(SingleObjectDeleter);
347                 pDataList->Construct();
348                 pDataList->Add(new (std::nothrow) String(APPCONTROL_KEY_CAMERA_ALLOW_SWITCH),
349                                 new (std::nothrow) String(APPCONTROL_DATA_TRUE));
350                 __pPresentationModel->StartAppControl(APPCONTROL_PROVIDER_ID_CAMERA,
351                                 APPCONTROL_OPERATION_ID_CREATE_CONTENT, null,
352                                 new (std::nothrow) String(APPCONTROL_MIME_IMAGE_JPG), pDataList, this);
353         }
354         break;
355
356         case IDA_FOOTER_EDIT:
357         {
358                 pSceneManager->GoForward(ForwardSceneTransition(IDSCN_ALBUM_LIST_EDITOR));
359         }
360         break;
361
362         default:
363                 break;
364         }
365         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
366 }
367
368 void
369 AlbumListForm::OnFormBackRequested(Form& source)
370 {
371         AppLogDebug("ENTER");
372         if (__pPresentationModel->GetAppControlMode() == APPCONTROL_MODE_PICK)
373         {
374                 AppLogDebug("APP_CONTROL_MODE_PICK");
375                 GalleryApp* pApp = dynamic_cast<GalleryApp*>(UiApp::GetInstance());
376                 if (pApp != null)
377                 {
378                         if (__pPresentationModel->GetAppControlMode() == APPCONTROL_MODE_PICK)
379                         {
380                                 pApp->SendAppControlResult(APP_CTRL_RESULT_CANCELED, null);
381                         }
382                         pApp->Terminate();
383                 }
384         }
385         else
386         {
387                 AppLogDebug("APP_CONTROL_MODE_MAIN");
388                 UiApp* pApp = UiApp::GetInstance();
389                 pApp->Terminate();
390         }
391         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
392 }
393
394 void
395 AlbumListForm::OnSceneActivatedN(const SceneId& previousSceneId, const SceneId& currentSceneId, IList* pArgs)
396 {
397         AppLogDebug("ENTER");
398         __pPresentationModel->AddContentEventListener(this);
399
400         if (__pPresentationModel->GetAppControlMode() == APPCONTROL_MODE_PICK)
401         {
402                 if (__pPresentationModel->GetAppControlMediaType() == APPCONTROL_MEDIA_TYPE_IMAGE)
403                 {
404                         __pPresentationModel->ClearThumbnailRequests(false);
405                         __pPresentationModel->InitializeAlbumInfoList(CONTENT_TYPE_IMAGE);
406                 }
407                 else if (__pPresentationModel->GetAppControlMediaType() == APPCONTROL_MEDIA_TYPE_VIDEO)
408                 {
409                         __pPresentationModel->InitializeAlbumInfoList(CONTENT_TYPE_VIDEO);
410                 }
411                 else
412                 {
413                         __pPresentationModel->InitializeAlbumInfoList(CONTENT_TYPE_VIDEO);
414                 }
415         }
416         else
417         {
418                 __pPresentationModel->InitializeAlbumInfoList(CONTENT_TYPE_ALL);
419         }
420         __itemCount = __pPresentationModel->GetFolderCount();
421
422         if (__pAlbumViewIconList != null)
423         {
424                 __pAlbumViewIconList->UpdateList();
425         }
426
427         Invalidate(true);
428         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
429 }
430
431 void
432 AlbumListForm::OnSceneDeactivated(const SceneId& currentSceneId, const SceneId& nextSceneId)
433 {
434         AppLogDebug("ENTER");
435         __pPresentationModel->RemoveContentEventListener(*this);
436         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
437 }
438
439 void
440 AlbumListForm::OnContentUpdated(void)
441 {
442         AppLogDebug("ENTER");
443
444         if ( __pOptionMenu != null && __pOptionMenu ->GetShowState() == true)
445         {
446                 __pOptionMenu->SetShowState(false);
447                 __pOptionMenu->Show();
448         }
449
450         if (__pPresentationModel->GetAppControlMode() == APPCONTROL_MODE_PICK)
451         {
452                 if (__pPresentationModel->GetAppControlMediaType() == APPCONTROL_MEDIA_TYPE_IMAGE)
453                 {
454                         __pPresentationModel->ClearThumbnailRequests(false);
455                         __pPresentationModel->InitializeAlbumInfoList(CONTENT_TYPE_IMAGE);
456                 }
457                 else if (__pPresentationModel->GetAppControlMediaType() == APPCONTROL_MEDIA_TYPE_VIDEO)
458                 {
459                         __pPresentationModel->InitializeAlbumInfoList(CONTENT_TYPE_VIDEO);
460                 }
461                 else
462                 {
463                         __pPresentationModel->InitializeAlbumInfoList(CONTENT_TYPE_VIDEO);
464                 }
465         }
466         else
467         {
468                 __pPresentationModel->InitializeAlbumInfoList(CONTENT_TYPE_ALL);
469         }
470
471         __itemCount = __pPresentationModel->GetFolderCount();
472         __pAlbumViewIconList->UpdateList();
473
474         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
475 }
476
477 void
478 AlbumListForm::OnFileInfoChanged(const ContentType contentType)
479 {
480         AppLogDebug("ENTER");
481         Update();
482         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
483 }
484
485 void
486 AlbumListForm::OnThumbnailDecoded(const int index)
487 {
488         AppLogDebug("ENTER : index(%d)", index);
489         __pAlbumViewIconList->RefreshList(index, LIST_REFRESH_TYPE_ITEM_MODIFY);
490         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
491 }
492
493 void
494 AlbumListForm::OnAppControlCompleteResponseReceived(const AppId& appId, const Tizen::Base::String& operationId,
495                         AppCtrlResult appControlResult, const Tizen::Base::Collection::IMap* pExtraData)
496 {
497         AppLogDebug("OnAppControlCompleteResponseReceived : %d", appControlResult);
498
499         GalleryApp* pGalleryApp = static_cast<GalleryApp*>(GalleryApp::GetInstance());
500         TryReturnVoid(pGalleryApp != null, "pGalleryApp is null");
501         pGalleryApp->SetFrameEnabled(true);
502 }
503
504 result
505 AlbumListForm::Update(void)
506 {
507         AppLogDebug("ENTER");
508         result r = __pAlbumViewIconList->UpdateList();
509         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
510
511         return r;
512 }
513
514 void
515 AlbumListForm::OnFormMenuRequested (Tizen::Ui::Controls::Form &source)
516 {
517
518         if ( __pOptionMenu == null || GetItemCount() == 0)
519         {
520                 return ;
521         }
522
523         __pOptionMenu->SetShowState(true);
524         __pOptionMenu->Show();
525
526 }
527
528 void
529 AlbumListForm::OnOrientationChanged(const Tizen::Ui::Control &source, Tizen::Ui::OrientationStatus orientationStatus)
530 {
531
532         if( __pAlbumViewIconList != null )
533         {
534                 RemoveControl(__pAlbumViewIconList);
535         }
536
537         __pAlbumViewIconList = new IconListView();
538
539         if( orientationStatus == ORIENTATION_STATUS_PORTRAIT )
540         {
541         __pAlbumViewIconList->Construct(Rectangle(0, 0, GetClientAreaBounds().width, GetClientAreaBounds().height),
542                     Dimension(W_FOLDER_SIZE, H_FOLDER_SIZE), ICON_LIST_VIEW_STYLE_NORMAL, ICON_LIST_VIEW_SCROLL_DIRECTION_VERTICAL);
543         }
544         else if (orientationStatus == ORIENTATION_STATUS_LANDSCAPE || orientationStatus == ORIENTATION_STATUS_LANDSCAPE_REVERSE)
545         {
546                 int horizontalSpacing = 8;
547                 int verticalSpacing = 60;
548         int height = 424;
549         int width = 410;
550
551                 __pAlbumViewIconList->Construct(Rectangle(0, 0, GetClientAreaBounds().width, GetClientAreaBounds().height),
552                                     Dimension(width, height), ICON_LIST_VIEW_STYLE_NORMAL, ICON_LIST_VIEW_SCROLL_DIRECTION_VERTICAL);
553
554                 __pAlbumViewIconList->SetItemSpacing(horizontalSpacing, verticalSpacing);
555                 __pAlbumViewIconList->SetMargin(MARGIN_TYPE_TOP,60);
556         }
557
558         Bitmap* pBitmap = ResourceManager::GetBitmapN(IDB_LISTVIEW_EMPTY);
559
560         if (pBitmap != null)
561         {
562                 __pAlbumViewIconList->SetBitmapOfEmptyList(pBitmap);
563                 delete pBitmap;
564         }
565
566         __pAlbumViewIconList->SetTextOfEmptyList(ResourceManager::GetString(L"IDS_COM_BODY_NO_ITEMS"));
567
568         __pAlbumViewIconList->SetItemProvider(*this);
569         __pAlbumViewIconList->SetItemBorderStyle(ICON_LIST_VIEW_ITEM_BORDER_STYLE_NONE);
570         __pAlbumViewIconList->AddIconListViewItemEventListener(*this);
571
572         AddControl(*__pAlbumViewIconList);
573
574 }