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