Fixed prevent issue
[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                 const String mimeType = APPCONTROL_MIME_IMAGE_JPG;
347                 HashMap* pDataList = new (std::nothrow) HashMap(SingleObjectDeleter);
348                 pDataList->Construct();
349                 pDataList->Add(new (std::nothrow) String(APPCONTROL_KEY_CAMERA_ALLOW_SWITCH),
350                                 new (std::nothrow) String(APPCONTROL_DATA_TRUE));
351                 __pPresentationModel->StartAppControl(APPCONTROL_PROVIDER_ID_CAMERA,
352                                 APPCONTROL_OPERATION_ID_CREATE_CONTENT, null, &mimeType, pDataList, this);
353
354         }
355         break;
356
357         case IDA_FOOTER_EDIT:
358         {
359                 pSceneManager->GoForward(ForwardSceneTransition(IDSCN_ALBUM_LIST_EDITOR));
360         }
361         break;
362
363         default:
364                 break;
365         }
366         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
367 }
368
369 void
370 AlbumListForm::OnFormBackRequested(Form& source)
371 {
372         AppLogDebug("ENTER");
373         if (__pPresentationModel->GetAppControlMode() == APPCONTROL_MODE_PICK)
374         {
375                 GalleryApp* pApp = dynamic_cast<GalleryApp*>(UiApp::GetInstance());
376                 if (pApp != null && pApp->GetFrameActivated() == true)
377                 {
378                         pApp->SendAppControlResult(APP_CTRL_RESULT_CANCELED, null);
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 (__pAlbumViewIconList != null)
420         {
421                 __pAlbumViewIconList->UpdateList();
422         }
423
424         Invalidate(true);
425         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
426 }
427
428 void
429 AlbumListForm::OnSceneDeactivated(const SceneId& currentSceneId, const SceneId& nextSceneId)
430 {
431         AppLogDebug("ENTER");
432         __pPresentationModel->RemoveContentEventListener(*this);
433         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
434 }
435
436 void
437 AlbumListForm::OnContentUpdated(void)
438 {
439         AppLogDebug("ENTER");
440
441         if (__pOptionMenu != null && __pOptionMenu ->GetShowState() == true)
442         {
443                 __pOptionMenu->SetShowState(false);
444                 __pOptionMenu->Show();
445         }
446
447         if (__pPresentationModel->GetAppControlMode() == APPCONTROL_MODE_PICK)
448         {
449                 if (__pPresentationModel->GetAppControlMediaType() == APPCONTROL_MEDIA_TYPE_IMAGE)
450                 {
451                         __pPresentationModel->InitializeAlbumInfoList(CONTENT_TYPE_IMAGE);
452                 }
453                 else if (__pPresentationModel->GetAppControlMediaType() == APPCONTROL_MEDIA_TYPE_VIDEO)
454                 {
455                         __pPresentationModel->InitializeAlbumInfoList(CONTENT_TYPE_VIDEO);
456                 }
457                 else
458                 {
459                         __pPresentationModel->InitializeAlbumInfoList(CONTENT_TYPE_VIDEO);
460                 }
461         }
462         else
463         {
464                 __pPresentationModel->InitializeAlbumInfoList(CONTENT_TYPE_ALL);
465         }
466
467         __itemCount = __pPresentationModel->GetFolderCount();
468         __pAlbumViewIconList->UpdateList();
469
470         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
471 }
472
473 void
474 AlbumListForm::OnFileInfoChanged(const ContentType contentType)
475 {
476         AppLogDebug("ENTER");
477         Update();
478         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
479 }
480
481 void
482 AlbumListForm::OnThumbnailDecoded(const int index)
483 {
484         AppLogDebug("ENTER : index(%d)", index);
485         __pAlbumViewIconList->RefreshList(index, LIST_REFRESH_TYPE_ITEM_MODIFY);
486         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
487 }
488
489 void
490 AlbumListForm::OnAppControlCompleteResponseReceived(const AppId& appId, const Tizen::Base::String& operationId,
491                         AppCtrlResult appControlResult, const Tizen::Base::Collection::IMap* pExtraData)
492 {
493         AppLogDebug("OnAppControlCompleteResponseReceived : %d", appControlResult);
494
495         GalleryApp* pGalleryApp = static_cast<GalleryApp*>(GalleryApp::GetInstance());
496         TryReturnVoid(pGalleryApp != null, "pGalleryApp is null");
497         pGalleryApp->SetFrameEnabled(true);
498 }
499
500 result
501 AlbumListForm::Update(void)
502 {
503         AppLogDebug("ENTER");
504         result r = __pAlbumViewIconList->UpdateList();
505         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
506
507         return r;
508 }
509
510 void
511 AlbumListForm::OnFormMenuRequested (Tizen::Ui::Controls::Form &source)
512 {
513
514         if (__pOptionMenu == null || GetItemCount() == 0)
515         {
516                 return;
517         }
518
519         __pOptionMenu->SetShowState(true);
520         __pOptionMenu->Show();
521 }
522
523 void
524 AlbumListForm::OnOrientationChanged(const Tizen::Ui::Control &source, Tizen::Ui::OrientationStatus orientationStatus)
525 {
526         if (__pAlbumViewIconList != null)
527         {
528                 RemoveControl(__pAlbumViewIconList);
529         }
530
531         __pAlbumViewIconList = new IconListView();
532
533         if (orientationStatus == ORIENTATION_STATUS_PORTRAIT)
534         {
535         __pAlbumViewIconList->Construct(Rectangle(0, 0, GetClientAreaBounds().width, GetClientAreaBounds().height),
536                     Dimension(W_FOLDER_SIZE, H_FOLDER_SIZE), ICON_LIST_VIEW_STYLE_NORMAL, ICON_LIST_VIEW_SCROLL_DIRECTION_VERTICAL);
537         }
538         else if (orientationStatus == ORIENTATION_STATUS_LANDSCAPE || orientationStatus == ORIENTATION_STATUS_LANDSCAPE_REVERSE)
539         {
540                 int horizontalSpacing = 8;
541                 int verticalSpacing = 60;
542         int height = 424;
543         int width = 410;
544
545                 __pAlbumViewIconList->Construct(Rectangle(0, 0, GetClientAreaBounds().width, GetClientAreaBounds().height),
546                                     Dimension(width, height), ICON_LIST_VIEW_STYLE_NORMAL, ICON_LIST_VIEW_SCROLL_DIRECTION_VERTICAL);
547
548                 __pAlbumViewIconList->SetItemSpacing(horizontalSpacing, verticalSpacing);
549                 __pAlbumViewIconList->SetMargin(MARGIN_TYPE_TOP,60);
550         }
551
552         Bitmap* pBitmap = ResourceManager::GetBitmapN(IDB_LISTVIEW_EMPTY);
553
554         if (pBitmap != null)
555         {
556                 __pAlbumViewIconList->SetBitmapOfEmptyList(pBitmap);
557                 delete pBitmap;
558         }
559
560         __pAlbumViewIconList->SetTextOfEmptyList(ResourceManager::GetString(L"IDS_COM_BODY_NO_ITEMS"));
561
562         __pAlbumViewIconList->SetItemProvider(*this);
563         __pAlbumViewIconList->SetItemBorderStyle(ICON_LIST_VIEW_ITEM_BORDER_STYLE_NONE);
564         __pAlbumViewIconList->AddIconListViewItemEventListener(*this);
565
566         AddControl(__pAlbumViewIconList);
567
568 }