License Agreement changes
[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                 Bitmap* pBitmapMore = ResourceManager::GetBitmapN(IDB_CONTROLBAR_MORE);
182                 if (pBitmapMore != null)
183                 {
184                         footerMore.SetIcon(BUTTON_ITEM_STATUS_NORMAL, pBitmapMore);
185                 }
186                 pFooter->SetButton(BUTTON_POSITION_LEFT,footerMore);
187                 delete pBitmapMore;
188
189                 __pContextMenuMore = new (std::nothrow) ContextMenu();
190                 __pContextMenuMore->Construct(Point(0, 0), CONTEXT_MENU_STYLE_LIST);
191                 __pContextMenuMore->AddItem(ResourceManager::GetString(L"IDS_COM_HEADER_EDIT"), IDA_FOOTER_EDIT);
192                 __pContextMenuMore->SetShowState(false);
193                 __pContextMenuMore->AddActionEventListener(*this);
194
195                 SetFormBackEventListener(this);
196         }
197         pFooter->AddActionEventListener(*this);
198         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
199
200         return E_SUCCESS;
201 }
202
203 void
204 AlbumListForm::CreateIconListView(void)
205 {
206         AppLogDebug("ENTER");
207         Dimension itemSize(W_FOLDER_SIZE, H_FOLDER_SIZE);
208
209         __pAlbumViewIconList = static_cast<IconListView*>(GetControl(L"IDC_ICONLISTVIEW_ALBUM_LIST"));
210         AppAssert(__pAlbumViewIconList);
211
212         __pAlbumViewIconList->SetItemProvider(*this);
213         __pAlbumViewIconList->SetItemBorderStyle(ICON_LIST_VIEW_ITEM_BORDER_STYLE_NONE);
214         __pAlbumViewIconList->AddIconListViewItemEventListener(*this);
215
216         Bitmap* pBitmap = ResourceManager::GetBitmapN(IDB_LISTVIEW_EMPTY);
217         if (pBitmap != null)
218         {
219                 __pAlbumViewIconList->SetBitmapOfEmptyList(pBitmap);
220                 delete pBitmap;
221         }
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         case IDA_FOOTER_MORE:
366         {
367                 if (__pContextMenuMore->GetShowState() == false && __pPresentationModel->GetFolderCount() != 0)
368                 {
369                         Rectangle rect = source.GetBounds();
370                         __pContextMenuMore->SetAnchorPosition(Point(rect.width/10, rect.y));
371                         __pContextMenuMore->SetShowState(true);
372                         __pContextMenuMore->Show();
373                 }
374                 else
375                 {
376                         __pContextMenuMore->SetShowState(false);
377                 }
378         }
379         break;
380
381         default:
382                 break;
383         }
384         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
385 }
386
387 void
388 AlbumListForm::OnFormBackRequested(Form& source)
389 {
390         AppLogDebug("ENTER");
391         if (__pPresentationModel->GetAppControlMode() == APPCONTROL_MODE_PICK)
392         {
393                 AppLogDebug("APP_CONTROL_MODE_PICK");
394                 GalleryApp* pApp = dynamic_cast<GalleryApp*>(UiApp::GetInstance());
395                 if (pApp != null)
396                 {
397                         if (__pPresentationModel->GetAppControlMode() == APPCONTROL_MODE_PICK)
398                         {
399                                 pApp->SendAppControlResult(APP_CTRL_RESULT_CANCELED, null);
400                         }
401                         pApp->Terminate();
402                 }
403         }
404         else
405         {
406                 AppLogDebug("APP_CONTROL_MODE_MAIN");
407                 UiApp* pApp = UiApp::GetInstance();
408                 pApp->Terminate();
409         }
410         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
411 }
412
413 void
414 AlbumListForm::OnSceneActivatedN(const SceneId& previousSceneId, const SceneId& currentSceneId, IList* pArgs)
415 {
416         AppLogDebug("ENTER");
417         __pPresentationModel->AddContentEventListener(this);
418
419         if (__pPresentationModel->GetAppControlMode() == APPCONTROL_MODE_PICK)
420         {
421                 if (__pPresentationModel->GetAppControlMediaType() == APPCONTROL_MEDIA_TYPE_IMAGE)
422                 {
423                         __pPresentationModel->InitializeAlbumInfoList(CONTENT_TYPE_IMAGE);
424                 }
425                 else if (__pPresentationModel->GetAppControlMediaType() == APPCONTROL_MEDIA_TYPE_VIDEO)
426                 {
427                         __pPresentationModel->InitializeAlbumInfoList(CONTENT_TYPE_VIDEO);
428                 }
429                 else
430                 {
431                         __pPresentationModel->InitializeAlbumInfoList(CONTENT_TYPE_VIDEO);
432                 }
433         }
434         else
435         {
436                 __pPresentationModel->InitializeAlbumInfoList(CONTENT_TYPE_ALL);
437         }
438         __itemCount = __pPresentationModel->GetFolderCount();
439
440         if (__pAlbumViewIconList != null)
441         {
442                 __pAlbumViewIconList->UpdateList();
443         }
444
445         Invalidate(true);
446         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
447 }
448
449 void
450 AlbumListForm::OnSceneDeactivated(const SceneId& currentSceneId, const SceneId& nextSceneId)
451 {
452         AppLogDebug("ENTER");
453         __pPresentationModel->RemoveContentEventListener(*this);
454         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
455 }
456
457 void
458 AlbumListForm::OnContentUpdated(void)
459 {
460         AppLogDebug("ENTER");
461
462         if (__pPresentationModel->GetAppControlMode() == APPCONTROL_MODE_PICK)
463         {
464                 if (__pPresentationModel->GetAppControlMediaType() == APPCONTROL_MEDIA_TYPE_IMAGE)
465                 {
466                         __pPresentationModel->InitializeAlbumInfoList(CONTENT_TYPE_IMAGE);
467                 }
468                 else if (__pPresentationModel->GetAppControlMediaType() == APPCONTROL_MEDIA_TYPE_VIDEO)
469                 {
470                         __pPresentationModel->InitializeAlbumInfoList(CONTENT_TYPE_VIDEO);
471                 }
472                 else
473                 {
474                         __pPresentationModel->InitializeAlbumInfoList(CONTENT_TYPE_VIDEO);
475                 }
476         }
477         else
478         {
479                 __pPresentationModel->InitializeAlbumInfoList(CONTENT_TYPE_ALL);
480         }
481
482         __itemCount = __pPresentationModel->GetFolderCount();
483         __pAlbumViewIconList->UpdateList();
484         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
485 }
486
487 void
488 AlbumListForm::OnFileInfoChanged(const ContentType contentType)
489 {
490         AppLogDebug("ENTER");
491         Update();
492         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
493 }
494
495 void
496 AlbumListForm::OnThumbnailDecoded(const int index)
497 {
498         AppLogDebug("ENTER : index(%d)", index);
499         __pAlbumViewIconList->RefreshList(index, LIST_REFRESH_TYPE_ITEM_MODIFY);
500         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
501 }
502
503 result
504 AlbumListForm::Update(void)
505 {
506         AppLogDebug("ENTER");
507         result r = __pAlbumViewIconList->UpdateList();
508         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
509
510         return r;
511 }