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