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