dc4c9ecb13f9042fbaedc26a01bacb382370177a
[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         : __isHWBackButtonExist(false)
46         , __itemCount(0)
47         , __pAlbumViewIconList(null)
48         , __pContextMenuMore(null)
49         , __pOptionMenu(null)
50         , __pPresentationModel(null)
51
52 {
53         AppLogDebug("ENTER");
54         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
55 }
56
57 AlbumListForm::~AlbumListForm(void)
58 {
59         AppLogDebug("ENTER");
60         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
61 }
62
63 result
64 AlbumListForm::Initialize(void)
65 {
66         AppLogDebug("ENTER");
67         result r = Form::Construct(IDL_FORM_ALBUM_LIST);
68         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
69
70         return r;
71 }
72
73 result
74 AlbumListForm::OnInitializing(void)
75 {
76         AppLogDebug("ENTER");
77         result r = E_SUCCESS;
78         GalleryApp* pApp = static_cast<GalleryApp*>(GalleryApp::GetInstance());
79
80          __isHWBackButtonExist = false;
81          __isHWBackButtonExist = pApp->IsHwKeySupported();
82         AppLogDebug("EXIT(%s)", GetErrorMessage(r));
83
84
85         __pPresentationModel = AlbumListPresentationModel::GetInstance();
86         __pPresentationModel->ClearThumbnailRequests();
87         __pPresentationModel->AddPresentationModelListener(this);
88
89         if (__pPresentationModel->GetAppControlMode() == APPCONTROL_MODE_PICK)
90         {
91                 if (__pPresentationModel->GetAppControlMediaType() == APPCONTROL_MEDIA_TYPE_IMAGE)
92                 {
93                         __pPresentationModel->InitializeAlbumInfoList(CONTENT_TYPE_IMAGE);
94                 }
95                 else if (__pPresentationModel->GetAppControlMediaType() == APPCONTROL_MEDIA_TYPE_VIDEO)
96                 {
97                         __pPresentationModel->InitializeAlbumInfoList(CONTENT_TYPE_VIDEO);
98                 }
99                 else
100                 {
101                         __pPresentationModel->InitializeAlbumInfoList(CONTENT_TYPE_VIDEO);
102                 }
103         }
104         else
105         {
106                 __pPresentationModel->InitializeAlbumInfoList(CONTENT_TYPE_ALL);
107         }
108
109         AppLogDebug("ENTER __pPresentationModel(%x)", __pPresentationModel);
110
111         r = InitializeHeader();
112         if (r != E_SUCCESS)
113         {
114                 AppLogDebug("[%s] Unable to set header.", GetErrorMessage(r));
115         }
116
117         r = InitializeFooter();
118         if (r != E_SUCCESS)
119         {
120                 AppLogDebug("[%s] Unable to set footer.", GetErrorMessage(r));
121         }
122
123         __itemCount = __pPresentationModel->GetFolderCount();
124         CreateIconListView();
125         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
126
127         return r;
128 }
129
130 result
131 AlbumListForm::InitializeHeader(void)
132 {
133         AppLogDebug("ENTER");
134         Header* pHeader = GetHeader();
135         if (pHeader == null)
136         {
137                 AppLogDebug("EXIT 1(%s)", GetErrorMessage(GetLastResult()));
138                 return E_SYSTEM;
139         }
140
141         result r = pHeader->SetStyle(HEADER_STYLE_TITLE);
142         if (r != E_SUCCESS)
143         {
144                 AppLogDebug("EXIT 2(%s)", GetErrorMessage(GetLastResult()));
145                 return r;
146         }
147
148         pHeader->SetTitleText(ResourceManager::GetString(L"IDS_COM_BODY_GALLERY"));
149         pHeader->AddActionEventListener(*this);
150
151         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
152         return E_SUCCESS;
153 }
154
155 result
156 AlbumListForm::InitializeFooter(void)
157 {
158         AppLogDebug("ENTER");
159         Footer* pFooter = GetFooter();
160         if (pFooter == null)
161         {
162                 AppLogDebug("EXIT 1");
163                 return E_SYSTEM;
164         }
165
166         if (__pPresentationModel->GetAppControlMode() == APPCONTROL_MODE_PICK)
167         {
168                 pFooter->SetBackButton();
169                 SetFormBackEventListener(this);
170         }
171         else
172         {
173                 pFooter->SetStyle(FOOTER_STYLE_BUTTON_ICON);
174                 pFooter->SetBackButton();
175                 pFooter->RemoveAllItems();
176
177                 FooterItem itemCamera;
178                 itemCamera.Construct(IDA_FOOTER_CAMERA);
179                 itemCamera.SetText(ResourceManager::GetString(L"EMPTY_SPACE"));
180                 Bitmap* pBitmap1 = ResourceManager::GetBitmapN(IDB_CONTROLBAR_CAMERA);
181                 if (pBitmap1 != null)
182                 {
183                         itemCamera.SetIcon(FOOTER_ITEM_STATUS_NORMAL, pBitmap1);
184                 }
185                 pFooter->AddItem(itemCamera);
186                 delete pBitmap1;
187
188
189                 if( __isHWBackButtonExist == false )
190                 {
191                         ButtonItem footerMore;
192                         footerMore.Construct(BUTTON_ITEM_STYLE_ICON, IDA_FOOTER_MORE);
193
194                         Bitmap* pBitmapMore = ResourceManager::GetBitmapN(IDB_CONTROLBAR_MORE);
195                         Bitmap* pBitmapMoreDim = ResourceManager::GetBitmapN(IDB_CONTROLBAR_MORE_DISABLED);
196                         Bitmap* pBitmapMorePressed = ResourceManager::GetBitmapN(IDB_CONTROLBAR_MORE_PRESSED);
197
198                         if (pBitmapMore != null && pBitmapMoreDim != null && pBitmapMorePressed != null)
199                         {
200                                 footerMore.SetIcon(BUTTON_ITEM_STATUS_NORMAL, pBitmapMore);
201                                 footerMore.SetIcon(BUTTON_ITEM_STATUS_DISABLED, pBitmapMoreDim);
202                                 footerMore.SetIcon(BUTTON_ITEM_STATUS_PRESSED, pBitmapMorePressed);
203                         }
204
205                         pFooter->SetButton(BUTTON_POSITION_LEFT, footerMore);
206                         delete pBitmapMore;
207                         delete pBitmapMoreDim;
208                         delete pBitmapMorePressed;
209
210                         __pContextMenuMore = new (std::nothrow) ContextMenu();
211                         __pContextMenuMore->Construct(Point(0, 0), CONTEXT_MENU_STYLE_LIST);
212                         __pContextMenuMore->AddItem(ResourceManager::GetString(L"IDS_COM_HEADER_EDIT"), IDA_FOOTER_EDIT);
213                         __pContextMenuMore->SetShowState(false);
214                         __pContextMenuMore->AddActionEventListener(*this);
215                 }
216                 else
217                 {
218                             __pOptionMenu = new (std::nothrow) OptionMenu();
219                             __pOptionMenu->Construct();
220                             __pOptionMenu->AddItem(ResourceManager::GetString(L"IDS_COM_HEADER_EDIT"),IDA_FOOTER_EDIT);
221                             __pOptionMenu->SetShowState(false);
222                             __pOptionMenu->AddActionEventListener(*this);
223                             SetFormMenuEventListener(this);
224                 }
225
226                 SetFormBackEventListener(this);
227         }
228         pFooter->AddActionEventListener(*this);
229         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
230
231         return E_SUCCESS;
232 }
233
234 void
235 AlbumListForm::CreateIconListView(void)
236 {
237         AppLogDebug("ENTER");
238         Dimension itemSize(W_FOLDER_SIZE, H_FOLDER_SIZE);
239
240         __pAlbumViewIconList = static_cast<IconListView*>(GetControl(L"IDC_ICONLISTVIEW_ALBUM_LIST"));
241         AppAssert(__pAlbumViewIconList);
242
243         __pAlbumViewIconList->SetItemProvider(*this);
244         __pAlbumViewIconList->SetItemBorderStyle(ICON_LIST_VIEW_ITEM_BORDER_STYLE_NONE);
245         __pAlbumViewIconList->AddIconListViewItemEventListener(*this);
246
247         Bitmap* pBitmap = ResourceManager::GetBitmapN(IDB_LISTVIEW_EMPTY);
248         if (pBitmap != null)
249         {
250                 __pAlbumViewIconList->SetBitmapOfEmptyList(pBitmap);
251                 delete pBitmap;
252         }
253
254         __pAlbumViewIconList->SetShowState(true);
255         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
256 }
257
258 int
259 AlbumListForm::GetItemCount(void)
260 {
261         AppLogDebug("ENTER");
262         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
263
264         return  __itemCount;
265 }
266
267 IconListViewItem*
268 AlbumListForm::CreateItem(int index)
269 {
270         AppLogDebug("ENTER : index(%d)", index);
271
272         Bitmap* pBitmap = null;
273         IList* pAlbumInfoList = __pPresentationModel->GetAlbumInfoList();
274         if (pAlbumInfoList != null)
275         {
276                 AlbumInfo* pAlbumInfo = static_cast<AlbumInfo*>(pAlbumInfoList->GetAt(index));
277                 if (pAlbumInfo != null)
278                 {
279                         Bitmap* pAlbumBitmap = pAlbumInfo->GetFolderThumnailBitmap();
280
281                         if (pAlbumBitmap == null)
282                         {
283                                 __pPresentationModel->RequestThumbnail(index, 0);
284                         }
285
286                         pBitmap = __pPresentationModel->CreateMergeBitmapN(index);
287
288                         if (pBitmap != null)
289                         {
290                                 IconListViewItem* pIconListview = new (std::nothrow) IconListViewItem();
291                                 pIconListview->Construct(*pBitmap);
292                                 delete pBitmap;
293                                 AppLogDebug("EXIT 1(%s)", GetErrorMessage(GetLastResult()));
294
295                                 return pIconListview;
296                         }
297                 }
298         }
299         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
300
301         return null;
302 }
303
304 void
305 AlbumListForm::OnIconListViewItemStateChanged(IconListView& view, int index, IconListViewItemStatus status)
306 {
307         AppLogDebug("ENTER");
308         if (status == ICON_LIST_VIEW_ITEM_SELECTED)
309         {
310                 SceneManager* pSceneManager = SceneManager::GetInstance();
311
312                 IList* pAlbumInfoList = __pPresentationModel->GetAlbumInfoList();
313                 AlbumInfo* pAlbumInfo = static_cast<AlbumInfo*>(pAlbumInfoList->GetAt(index));
314                 AlbumInfo* pSendAlbumInfo = new (std::nothrow) AlbumInfo();
315                 pSendAlbumInfo->Construct(*pAlbumInfo);
316
317                 ArrayList* pArrayList = new (std::nothrow) ArrayList(SingleObjectDeleter);
318                 pArrayList->Construct();
319                 pArrayList->Add(pSendAlbumInfo);
320
321                 if (__pPresentationModel->GetAppControlMode() == APPCONTROL_MODE_PICK)
322                 {
323                         if (__pPresentationModel->GetAppControlMediaType() == APPCONTROL_MEDIA_TYPE_IMAGE)
324                         {
325                                 pSceneManager->GoForward(ForwardSceneTransition(IDSCN_IMAGE_LIST_EDITOR), pArrayList);
326                         }
327                         else if (__pPresentationModel->GetAppControlMediaType() == APPCONTROL_MEDIA_TYPE_VIDEO)
328                         {
329                                 pSceneManager->GoForward(ForwardSceneTransition(IDSCN_VIDEO_LIST_EDITOR), pArrayList);
330                         }
331                         else
332                         {
333                                 pSceneManager->GoForward(ForwardSceneTransition(IDSCN_IMAGE_LIST_EDITOR), pArrayList);
334                         }
335                 }
336                 else
337                 {
338                         pSceneManager->GoForward(ForwardSceneTransition(IDSCN_ALL_LIST), pArrayList);
339                 }
340         }
341         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
342 }
343
344 bool
345 AlbumListForm::DeleteItem(int index, IconListViewItem* pItem)
346 {
347         AppLogDebug("ENTER");
348         delete pItem;
349         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
350
351         return true;
352 }
353
354 result
355 AlbumListForm::OnTerminating(void)
356 {
357         AppLogDebug("ENTER");
358         __pPresentationModel->RemovePresentationModelListener(*this);
359         __pPresentationModel->RemoveContentEventListener(*this);
360         if (__pContextMenuMore != null)
361         {
362                 delete __pContextMenuMore;
363                 __pContextMenuMore = null;
364         }
365         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
366
367         return E_SUCCESS;
368 }
369
370 void
371 AlbumListForm::OnActionPerformed(const Control& source, int actionId)
372 {
373         AppLogDebug("ENTER");
374         SceneManager* pSceneManager = SceneManager::GetInstance();
375
376         switch (actionId)
377         {
378         case IDA_FOOTER_CAMERA:
379         {
380                 HashMap* pDataList = new (std::nothrow) HashMap(SingleObjectDeleter);
381                 pDataList->Construct();
382                 pDataList->Add(new (std::nothrow) String(APPCONTROL_KEY_CAMERA_ALLOW_SWITCH),
383                                 new (std::nothrow) String(APPCONTROL_DATA_TRUE));
384                 __pPresentationModel->StartAppControl(APPCONTROL_PROVIDER_ID_CAMERA,
385                                 APPCONTROL_OPERATION_ID_CREATE_CONTENT, null,
386                                 new (std::nothrow) String(APPCONTROL_MIME_IMAGE_JPG), pDataList, null);
387         }
388         break;
389
390         case IDA_FOOTER_EDIT:
391         {
392                 pSceneManager->GoForward(ForwardSceneTransition(IDSCN_ALBUM_LIST_EDITOR));
393         }
394         break;
395
396         case IDA_FOOTER_MORE:
397         {
398                 if (__pContextMenuMore->GetShowState() == false && __pPresentationModel->GetFolderCount() != 0)
399                 {
400                         Rectangle rect = source.GetBounds();
401                         __pContextMenuMore->SetAnchorPosition(Point(rect.width/10, rect.y));
402                         __pContextMenuMore->SetShowState(true);
403                         __pContextMenuMore->Show();
404                 }
405                 else
406                 {
407                         __pContextMenuMore->SetShowState(false);
408                 }
409         }
410         break;
411
412         default:
413                 break;
414         }
415         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
416 }
417
418 void
419 AlbumListForm::OnFormBackRequested(Form& source)
420 {
421         AppLogDebug("ENTER");
422         if (__pPresentationModel->GetAppControlMode() == APPCONTROL_MODE_PICK)
423         {
424                 AppLogDebug("APP_CONTROL_MODE_PICK");
425                 GalleryApp* pApp = dynamic_cast<GalleryApp*>(UiApp::GetInstance());
426                 if (pApp != null)
427                 {
428                         if (__pPresentationModel->GetAppControlMode() == APPCONTROL_MODE_PICK)
429                         {
430                                 pApp->SendAppControlResult(APP_CTRL_RESULT_CANCELED, null);
431                         }
432                         pApp->Terminate();
433                 }
434         }
435         else
436         {
437                 AppLogDebug("APP_CONTROL_MODE_MAIN");
438                 UiApp* pApp = UiApp::GetInstance();
439                 pApp->Terminate();
440         }
441         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
442 }
443
444 void
445 AlbumListForm::OnSceneActivatedN(const SceneId& previousSceneId, const SceneId& currentSceneId, IList* pArgs)
446 {
447         AppLogDebug("ENTER");
448         __pPresentationModel->AddContentEventListener(this);
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         __itemCount = __pPresentationModel->GetFolderCount();
471
472         if ( __itemCount == 0 )
473         {
474                 GetFooter()->SetButtonEnabled(BUTTON_POSITION_LEFT, false);
475         }
476         else
477         {
478                 GetFooter()->SetButtonEnabled(BUTTON_POSITION_LEFT, true);
479         }
480
481         if (__pAlbumViewIconList != null)
482         {
483                 __pAlbumViewIconList->UpdateList();
484         }
485
486         Invalidate(true);
487         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
488 }
489
490 void
491 AlbumListForm::OnSceneDeactivated(const SceneId& currentSceneId, const SceneId& nextSceneId)
492 {
493         AppLogDebug("ENTER");
494         __pPresentationModel->RemoveContentEventListener(*this);
495         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
496 }
497
498 void
499 AlbumListForm::OnContentUpdated(void)
500 {
501         AppLogDebug("ENTER");
502
503         if(__pContextMenuMore != null && __pContextMenuMore ->GetShowState() == true)
504         {
505                 __pContextMenuMore->SetShowState(false);
506         }
507
508         if ( __pOptionMenu != null && __pOptionMenu ->GetShowState() == true)
509         {
510                 __pOptionMenu->SetShowState(false);
511                 __pOptionMenu->Show();
512         }
513
514         if (__pPresentationModel->GetAppControlMode() == APPCONTROL_MODE_PICK)
515         {
516                 if (__pPresentationModel->GetAppControlMediaType() == APPCONTROL_MEDIA_TYPE_IMAGE)
517                 {
518                         __pPresentationModel->ClearThumbnailRequests(false);
519                         __pPresentationModel->InitializeAlbumInfoList(CONTENT_TYPE_IMAGE);
520                 }
521                 else if (__pPresentationModel->GetAppControlMediaType() == APPCONTROL_MEDIA_TYPE_VIDEO)
522                 {
523                         __pPresentationModel->InitializeAlbumInfoList(CONTENT_TYPE_VIDEO);
524                 }
525                 else
526                 {
527                         __pPresentationModel->InitializeAlbumInfoList(CONTENT_TYPE_VIDEO);
528                 }
529         }
530         else
531         {
532                 __pPresentationModel->InitializeAlbumInfoList(CONTENT_TYPE_ALL);
533         }
534
535         __itemCount = __pPresentationModel->GetFolderCount();
536         __pAlbumViewIconList->UpdateList();
537
538         if (__itemCount == 0 )
539         {
540                 GetFooter()->SetButtonEnabled(BUTTON_POSITION_LEFT, false);
541         }
542         else
543         {
544                 GetFooter()->SetButtonEnabled(BUTTON_POSITION_LEFT, true);
545         }
546
547         GetFooter()->Invalidate(true);
548         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
549 }
550
551 void
552 AlbumListForm::OnFileInfoChanged(const ContentType contentType)
553 {
554         AppLogDebug("ENTER");
555         Update();
556         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
557 }
558
559 void
560 AlbumListForm::OnThumbnailDecoded(const int index)
561 {
562         AppLogDebug("ENTER : index(%d)", index);
563         __pAlbumViewIconList->RefreshList(index, LIST_REFRESH_TYPE_ITEM_MODIFY);
564         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
565 }
566
567 result
568 AlbumListForm::Update(void)
569 {
570         AppLogDebug("ENTER");
571         result r = __pAlbumViewIconList->UpdateList();
572         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
573
574         return r;
575 }
576
577 void
578 AlbumListForm::OnFormMenuRequested (Tizen::Ui::Controls::Form &source)
579 {
580
581         if ( __pOptionMenu == null )
582         {
583                 return ;
584         }
585
586         __pOptionMenu->SetShowState(true);
587         __pOptionMenu->Show();
588
589 }