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