Fixed issue 54120
[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 && pApp->GetFrameActivated() == true)
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->InitializeAlbumInfoList(CONTENT_TYPE_IMAGE);
455                 }
456                 else if (__pPresentationModel->GetAppControlMediaType() == APPCONTROL_MEDIA_TYPE_VIDEO)
457                 {
458                         __pPresentationModel->InitializeAlbumInfoList(CONTENT_TYPE_VIDEO);
459                 }
460                 else
461                 {
462                         __pPresentationModel->InitializeAlbumInfoList(CONTENT_TYPE_VIDEO);
463                 }
464         }
465         else
466         {
467                 __pPresentationModel->InitializeAlbumInfoList(CONTENT_TYPE_ALL);
468         }
469
470         __itemCount = __pPresentationModel->GetFolderCount();
471         __pAlbumViewIconList->UpdateList();
472
473         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
474 }
475
476 void
477 AlbumListForm::OnFileInfoChanged(const ContentType contentType)
478 {
479         AppLogDebug("ENTER");
480         Update();
481         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
482 }
483
484 void
485 AlbumListForm::OnThumbnailDecoded(const int index)
486 {
487         AppLogDebug("ENTER : index(%d)", index);
488         __pAlbumViewIconList->RefreshList(index, LIST_REFRESH_TYPE_ITEM_MODIFY);
489         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
490 }
491
492 void
493 AlbumListForm::OnAppControlCompleteResponseReceived(const AppId& appId, const Tizen::Base::String& operationId,
494                         AppCtrlResult appControlResult, const Tizen::Base::Collection::IMap* pExtraData)
495 {
496         AppLogDebug("OnAppControlCompleteResponseReceived : %d", appControlResult);
497
498         GalleryApp* pGalleryApp = static_cast<GalleryApp*>(GalleryApp::GetInstance());
499         TryReturnVoid(pGalleryApp != null, "pGalleryApp is null");
500         pGalleryApp->SetFrameEnabled(true);
501 }
502
503 result
504 AlbumListForm::Update(void)
505 {
506         AppLogDebug("ENTER");
507         result r = __pAlbumViewIconList->UpdateList();
508         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
509
510         return r;
511 }
512
513 void
514 AlbumListForm::OnFormMenuRequested (Tizen::Ui::Controls::Form &source)
515 {
516
517         if (__pOptionMenu == null || GetItemCount() == 0)
518         {
519                 return;
520         }
521
522         __pOptionMenu->SetShowState(true);
523         __pOptionMenu->Show();
524 }
525
526 void
527 AlbumListForm::OnOrientationChanged(const Tizen::Ui::Control &source, Tizen::Ui::OrientationStatus orientationStatus)
528 {
529         if (__pAlbumViewIconList != null)
530         {
531                 RemoveControl(__pAlbumViewIconList);
532         }
533
534         __pAlbumViewIconList = new IconListView();
535
536         if (orientationStatus == ORIENTATION_STATUS_PORTRAIT)
537         {
538         __pAlbumViewIconList->Construct(Rectangle(0, 0, GetClientAreaBounds().width, GetClientAreaBounds().height),
539                     Dimension(W_FOLDER_SIZE, H_FOLDER_SIZE), ICON_LIST_VIEW_STYLE_NORMAL, ICON_LIST_VIEW_SCROLL_DIRECTION_VERTICAL);
540         }
541         else if (orientationStatus == ORIENTATION_STATUS_LANDSCAPE || orientationStatus == ORIENTATION_STATUS_LANDSCAPE_REVERSE)
542         {
543                 int horizontalSpacing = 8;
544                 int verticalSpacing = 60;
545         int height = 424;
546         int width = 410;
547
548                 __pAlbumViewIconList->Construct(Rectangle(0, 0, GetClientAreaBounds().width, GetClientAreaBounds().height),
549                                     Dimension(width, height), ICON_LIST_VIEW_STYLE_NORMAL, ICON_LIST_VIEW_SCROLL_DIRECTION_VERTICAL);
550
551                 __pAlbumViewIconList->SetItemSpacing(horizontalSpacing, verticalSpacing);
552                 __pAlbumViewIconList->SetMargin(MARGIN_TYPE_TOP,60);
553         }
554
555         Bitmap* pBitmap = ResourceManager::GetBitmapN(IDB_LISTVIEW_EMPTY);
556
557         if (pBitmap != null)
558         {
559                 __pAlbumViewIconList->SetBitmapOfEmptyList(pBitmap);
560                 delete pBitmap;
561         }
562
563         __pAlbumViewIconList->SetTextOfEmptyList(ResourceManager::GetString(L"IDS_COM_BODY_NO_ITEMS"));
564
565         __pAlbumViewIconList->SetItemProvider(*this);
566         __pAlbumViewIconList->SetItemBorderStyle(ICON_LIST_VIEW_ITEM_BORDER_STYLE_NONE);
567         __pAlbumViewIconList->AddIconListViewItemEventListener(*this);
568
569         AddControl(__pAlbumViewIconList);
570
571 }