HW key changes
[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->AddItem(ResourceManager::GetString(L"IDS_COM_HEADER_EDIT"), IDA_FOOTER_EDIT);
222                             __pOptionMenu->SetShowState(false);
223                             __pOptionMenu->AddActionEventListener(*this);
224                             SetFormMenuEventListener(this);
225                 }
226
227                 SetFormBackEventListener(this);
228         }
229         pFooter->AddActionEventListener(*this);
230         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
231
232         return E_SUCCESS;
233 }
234
235 void
236 AlbumListForm::CreateIconListView(void)
237 {
238         AppLogDebug("ENTER");
239         Dimension itemSize(W_FOLDER_SIZE, H_FOLDER_SIZE);
240
241         __pAlbumViewIconList = static_cast<IconListView*>(GetControl(L"IDC_ICONLISTVIEW_ALBUM_LIST"));
242         AppAssert(__pAlbumViewIconList);
243
244         __pAlbumViewIconList->SetItemProvider(*this);
245         __pAlbumViewIconList->SetItemBorderStyle(ICON_LIST_VIEW_ITEM_BORDER_STYLE_NONE);
246         __pAlbumViewIconList->AddIconListViewItemEventListener(*this);
247
248         Bitmap* pBitmap = ResourceManager::GetBitmapN(IDB_LISTVIEW_EMPTY);
249         if (pBitmap != null)
250         {
251                 __pAlbumViewIconList->SetBitmapOfEmptyList(pBitmap);
252                 delete pBitmap;
253         }
254
255         __pAlbumViewIconList->SetShowState(true);
256         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
257 }
258
259 int
260 AlbumListForm::GetItemCount(void)
261 {
262         AppLogDebug("ENTER");
263         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
264
265         return  __itemCount;
266 }
267
268 IconListViewItem*
269 AlbumListForm::CreateItem(int index)
270 {
271         AppLogDebug("ENTER : index(%d)", index);
272
273         Bitmap* pBitmap = null;
274         IList* pAlbumInfoList = __pPresentationModel->GetAlbumInfoList();
275         if (pAlbumInfoList != null)
276         {
277                 AlbumInfo* pAlbumInfo = static_cast<AlbumInfo*>(pAlbumInfoList->GetAt(index));
278                 if (pAlbumInfo != null)
279                 {
280                         Bitmap* pAlbumBitmap = pAlbumInfo->GetFolderThumnailBitmap();
281
282                         if (pAlbumBitmap == null)
283                         {
284                                 __pPresentationModel->RequestThumbnail(index, 0);
285                         }
286
287                         pBitmap = __pPresentationModel->CreateMergeBitmapN(index);
288
289                         if (pBitmap != null)
290                         {
291                                 IconListViewItem* pIconListview = new (std::nothrow) IconListViewItem();
292                                 pIconListview->Construct(*pBitmap);
293                                 delete pBitmap;
294                                 AppLogDebug("EXIT 1(%s)", GetErrorMessage(GetLastResult()));
295
296                                 return pIconListview;
297                         }
298                 }
299         }
300         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
301
302         return null;
303 }
304
305 void
306 AlbumListForm::OnIconListViewItemStateChanged(IconListView& view, int index, IconListViewItemStatus status)
307 {
308         AppLogDebug("ENTER");
309         if (status == ICON_LIST_VIEW_ITEM_SELECTED)
310         {
311                 SceneManager* pSceneManager = SceneManager::GetInstance();
312
313                 IList* pAlbumInfoList = __pPresentationModel->GetAlbumInfoList();
314                 AlbumInfo* pAlbumInfo = static_cast<AlbumInfo*>(pAlbumInfoList->GetAt(index));
315                 AlbumInfo* pSendAlbumInfo = new (std::nothrow) AlbumInfo();
316                 pSendAlbumInfo->Construct(*pAlbumInfo);
317
318                 ArrayList* pArrayList = new (std::nothrow) ArrayList(SingleObjectDeleter);
319                 pArrayList->Construct();
320                 pArrayList->Add(pSendAlbumInfo);
321
322                 if (__pPresentationModel->GetAppControlMode() == APPCONTROL_MODE_PICK)
323                 {
324                         if (__pPresentationModel->GetAppControlMediaType() == APPCONTROL_MEDIA_TYPE_IMAGE)
325                         {
326                                 pSceneManager->GoForward(ForwardSceneTransition(IDSCN_IMAGE_LIST_EDITOR), pArrayList);
327                         }
328                         else if (__pPresentationModel->GetAppControlMediaType() == APPCONTROL_MEDIA_TYPE_VIDEO)
329                         {
330                                 pSceneManager->GoForward(ForwardSceneTransition(IDSCN_VIDEO_LIST_EDITOR), pArrayList);
331                         }
332                         else
333                         {
334                                 pSceneManager->GoForward(ForwardSceneTransition(IDSCN_IMAGE_LIST_EDITOR), pArrayList);
335                         }
336                 }
337                 else
338                 {
339                         pSceneManager->GoForward(ForwardSceneTransition(IDSCN_ALL_LIST), pArrayList);
340                 }
341         }
342         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
343 }
344
345 bool
346 AlbumListForm::DeleteItem(int index, IconListViewItem* pItem)
347 {
348         AppLogDebug("ENTER");
349         delete pItem;
350         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
351
352         return true;
353 }
354
355 result
356 AlbumListForm::OnTerminating(void)
357 {
358         AppLogDebug("ENTER");
359         __pPresentationModel->RemovePresentationModelListener(*this);
360         __pPresentationModel->RemoveContentEventListener(*this);
361         if (__pContextMenuMore != null)
362         {
363                 delete __pContextMenuMore;
364                 __pContextMenuMore = null;
365         }
366         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
367
368         return E_SUCCESS;
369 }
370
371 void
372 AlbumListForm::OnActionPerformed(const Control& source, int actionId)
373 {
374         AppLogDebug("ENTER");
375         SceneManager* pSceneManager = SceneManager::GetInstance();
376
377         switch (actionId)
378         {
379         case IDA_FOOTER_CAMERA:
380         {
381                 HashMap* pDataList = new (std::nothrow) HashMap(SingleObjectDeleter);
382                 pDataList->Construct();
383                 pDataList->Add(new (std::nothrow) String(APPCONTROL_KEY_CAMERA_ALLOW_SWITCH),
384                                 new (std::nothrow) String(APPCONTROL_DATA_TRUE));
385                 __pPresentationModel->StartAppControl(APPCONTROL_PROVIDER_ID_CAMERA,
386                                 APPCONTROL_OPERATION_ID_CREATE_CONTENT, null,
387                                 new (std::nothrow) String(APPCONTROL_MIME_IMAGE_JPG), pDataList, null);
388         }
389         break;
390
391         case IDA_FOOTER_EDIT:
392         {
393                 pSceneManager->GoForward(ForwardSceneTransition(IDSCN_ALBUM_LIST_EDITOR));
394         }
395         break;
396
397         case IDA_FOOTER_MORE:
398         {
399                 if (__pContextMenuMore->GetShowState() == false && __pPresentationModel->GetFolderCount() != 0)
400                 {
401                         Rectangle rect = source.GetBounds();
402                         __pContextMenuMore->SetAnchorPosition(Point(rect.width/10, rect.y));
403                         __pContextMenuMore->SetShowState(true);
404                         __pContextMenuMore->Show();
405                 }
406                 else
407                 {
408                         __pContextMenuMore->SetShowState(false);
409                 }
410         }
411         break;
412
413         default:
414                 break;
415         }
416         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
417 }
418
419 void
420 AlbumListForm::OnFormBackRequested(Form& source)
421 {
422         AppLogDebug("ENTER");
423         if (__pPresentationModel->GetAppControlMode() == APPCONTROL_MODE_PICK)
424         {
425                 AppLogDebug("APP_CONTROL_MODE_PICK");
426                 GalleryApp* pApp = dynamic_cast<GalleryApp*>(UiApp::GetInstance());
427                 if (pApp != null)
428                 {
429                         if (__pPresentationModel->GetAppControlMode() == APPCONTROL_MODE_PICK)
430                         {
431                                 pApp->SendAppControlResult(APP_CTRL_RESULT_CANCELED, null);
432                         }
433                         pApp->Terminate();
434                 }
435         }
436         else
437         {
438                 AppLogDebug("APP_CONTROL_MODE_MAIN");
439                 UiApp* pApp = UiApp::GetInstance();
440                 pApp->Terminate();
441         }
442         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
443 }
444
445 void
446 AlbumListForm::OnSceneActivatedN(const SceneId& previousSceneId, const SceneId& currentSceneId, IList* pArgs)
447 {
448         AppLogDebug("ENTER");
449         __pPresentationModel->AddContentEventListener(this);
450
451         if (__pPresentationModel->GetAppControlMode() == APPCONTROL_MODE_PICK)
452         {
453                 if (__pPresentationModel->GetAppControlMediaType() == APPCONTROL_MEDIA_TYPE_IMAGE)
454                 {
455                         __pPresentationModel->ClearThumbnailRequests(false);
456                         __pPresentationModel->InitializeAlbumInfoList(CONTENT_TYPE_IMAGE);
457                 }
458                 else if (__pPresentationModel->GetAppControlMediaType() == APPCONTROL_MEDIA_TYPE_VIDEO)
459                 {
460                         __pPresentationModel->InitializeAlbumInfoList(CONTENT_TYPE_VIDEO);
461                 }
462                 else
463                 {
464                         __pPresentationModel->InitializeAlbumInfoList(CONTENT_TYPE_VIDEO);
465                 }
466         }
467         else
468         {
469                 __pPresentationModel->InitializeAlbumInfoList(CONTENT_TYPE_ALL);
470         }
471         __itemCount = __pPresentationModel->GetFolderCount();
472
473         if ( __itemCount == 0 )
474         {
475                 GetFooter()->SetButtonEnabled(BUTTON_POSITION_LEFT, false);
476         }
477         else
478         {
479                 GetFooter()->SetButtonEnabled(BUTTON_POSITION_LEFT, true);
480         }
481
482         if (__pAlbumViewIconList != null)
483         {
484                 __pAlbumViewIconList->UpdateList();
485         }
486
487         Invalidate(true);
488         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
489 }
490
491 void
492 AlbumListForm::OnSceneDeactivated(const SceneId& currentSceneId, const SceneId& nextSceneId)
493 {
494         AppLogDebug("ENTER");
495         __pPresentationModel->RemoveContentEventListener(*this);
496         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
497 }
498
499 void
500 AlbumListForm::OnContentUpdated(void)
501 {
502         AppLogDebug("ENTER");
503
504         if(__pContextMenuMore != null && __pContextMenuMore ->GetShowState() == true)
505         {
506                 __pContextMenuMore->SetShowState(false);
507         }
508
509         if ( __pOptionMenu != null && __pOptionMenu ->GetShowState() == true)
510         {
511                 __pOptionMenu->SetShowState(false);
512                 __pOptionMenu->Show();
513         }
514
515         if (__pPresentationModel->GetAppControlMode() == APPCONTROL_MODE_PICK)
516         {
517                 if (__pPresentationModel->GetAppControlMediaType() == APPCONTROL_MEDIA_TYPE_IMAGE)
518                 {
519                         __pPresentationModel->ClearThumbnailRequests(false);
520                         __pPresentationModel->InitializeAlbumInfoList(CONTENT_TYPE_IMAGE);
521                 }
522                 else if (__pPresentationModel->GetAppControlMediaType() == APPCONTROL_MEDIA_TYPE_VIDEO)
523                 {
524                         __pPresentationModel->InitializeAlbumInfoList(CONTENT_TYPE_VIDEO);
525                 }
526                 else
527                 {
528                         __pPresentationModel->InitializeAlbumInfoList(CONTENT_TYPE_VIDEO);
529                 }
530         }
531         else
532         {
533                 __pPresentationModel->InitializeAlbumInfoList(CONTENT_TYPE_ALL);
534         }
535
536         __itemCount = __pPresentationModel->GetFolderCount();
537         __pAlbumViewIconList->UpdateList();
538
539         if (__itemCount == 0 )
540         {
541                 GetFooter()->SetButtonEnabled(BUTTON_POSITION_LEFT, false);
542         }
543         else
544         {
545                 GetFooter()->SetButtonEnabled(BUTTON_POSITION_LEFT, true);
546         }
547
548         GetFooter()->Invalidate(true);
549         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
550 }
551
552 void
553 AlbumListForm::OnFileInfoChanged(const ContentType contentType)
554 {
555         AppLogDebug("ENTER");
556         Update();
557         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
558 }
559
560 void
561 AlbumListForm::OnThumbnailDecoded(const int index)
562 {
563         AppLogDebug("ENTER : index(%d)", index);
564         __pAlbumViewIconList->RefreshList(index, LIST_REFRESH_TYPE_ITEM_MODIFY);
565         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
566 }
567
568 result
569 AlbumListForm::Update(void)
570 {
571         AppLogDebug("ENTER");
572         result r = __pAlbumViewIconList->UpdateList();
573         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
574
575         return r;
576 }
577
578 void
579 AlbumListForm::OnFormMenuRequested (Tizen::Ui::Controls::Form &source)
580 {
581
582         if ( __pOptionMenu == null )
583         {
584                 return ;
585         }
586
587         __pOptionMenu->SetShowState(true);
588         __pOptionMenu->Show();
589
590 }