45979
[apps/osp/Gallery.git] / src / GlAlbumListPresentationModel.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                GlAlbumListPresentationModel.cpp
19  * @brief               This is the implementation file for AlbumListPresentationModel class.
20  */
21
22 #include <cstdlib>
23 #include <FMedia.h>
24 #include <FSystem.h>
25
26 #include "GlAlbumInfo.h"
27 #include "GlAlbumListPresentationModel.h"
28 #include "GlAlbumRenameEventListener.h"
29 #include "GlContentUpdateEventListener.h"
30 #include "GlFileListPresentationModel.h"
31 #include "GlGalleryApp.h"
32 #include "GlThumbnailEvent.h"
33 #include "GlThumbnailEventArg.h"
34 #include "GlThumbnailProvider.h"
35
36 using namespace Tizen::App;
37 using namespace Tizen::Base;
38 using namespace Tizen::Base::Collection;
39 using namespace Tizen::Base::Runtime;
40 using namespace Tizen::Base::Utility;
41 using namespace Tizen::Content;
42 using namespace Tizen::Graphics;
43 using namespace Tizen::Io;
44 using namespace Tizen::Media;
45 using namespace Tizen::System;
46 using namespace Tizen::Ui::Controls;
47
48 static const int ICON_CAP = 3;
49 static const int FOLDER_CAP = 8;
50 static const int ICON_SIZE = 107;
51 static const int FOLDER_SIZE_WIDTH = 348;
52 static const int FOLDER_SIZE_HEIGHT = 348;
53
54 static const int CANVAS_HEIGHT = 71;
55 static const int CANVAS_WIDTH = 326;
56
57 static const int FONT_SIZE_FOLDER_NAME = 32;
58 static const int FONT_SIZE_FOLDER_DATE = 25;
59 static const int FONT_SIZE_FOLDER_COUNT = 45;
60 static const int FONT_SIZE_FOLDER_COUNT_SMALL = 35;
61
62 static const int CANVAS_IMAGE_FOLDER_HEIGHT = 48;
63 static const int CANVAS_IMAGE_FOLDER_WIDTH = 48;
64 static const int IMAGE_COUNT_WIDTH = 100;
65 static const int IMAGE_COUNT_HEIGHT = 60;
66 static const int CANVAS_MARGIN_LEFT = 10;
67 static const int GAP = 10;
68 static const int HEIGHT_OFFSET = 5;
69 static const int Y_POS_CANVAS = 260;
70 static const int FOLDER_NAME_WIDTH = 165;
71 static const int FOLDER_NAME_HEIGHT = 38;
72
73
74 static const int ALPHA_BG = 110;
75
76 static const Color COLOR_CANVAS_BG (Color::GetColor(COLOR_ID_BLACK));
77
78 static const String RESERVED_MEDIA_PATH = Environment::GetMediaPath();
79 static const String RESERVED_CAMERA_PATH = Environment::GetMediaPath() + L"Camera";
80 static const String RESERVED_CAMERA_PATH_EXT = Environment::GetExternalStoragePath() + L"Camera";
81 static const String RESERVED_DOWNLOAD_PATH = Environment::GetMediaPath() + L"Downloads";
82 static const String CONTENT_INFO_ORDER = L"ContentFileName";
83
84 static const int FORMAT_BUFFER_SIZE = 256;
85
86 static const int DOUBLE_NUMBER = 2;
87 static const int TRIPLE_NUMBER = 3;
88
89 AlbumListPresentationModel* AlbumListPresentationModel::__pPresentationModelInstance = null;
90 ArrayList* AlbumListPresentationModel::__pPresentationModelListener = null;
91 ArrayList* AlbumListPresentationModel::__pContentEventListener = null;
92 ThumbnailProvider* AlbumListPresentationModel::__pThumbnailProvider = null;
93 ThumbnailEvent* AlbumListPresentationModel::__pThumbnailEvent = null;
94 IList* AlbumListPresentationModel::__pAlbumInfoList = null;
95
96 AlbumListPresentationModel::AlbumListPresentationModel(void)
97         : __pFolderBackgroundBitmap(null)
98         , __pContentManager(null)
99         , __pAlbumListener(null)
100         , __appControlMode(APPCONTROL_MODE_MAIN)
101         , __appControlMediaType(APPCONTROL_MEDIA_TYPE_IMAGE)
102         , __appControlSelectionMode(APPCONTROL_SELECTION_MODE_SINGLE)
103         , __updateProgressStatus(false)
104 {
105         AppLogDebug("ENTER");
106         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
107 }
108
109 AlbumListPresentationModel::~AlbumListPresentationModel(void)
110 {
111         AppLogDebug("ENTER");
112         DeviceManager::RemoveDeviceEventListener(DEVICE_TYPE_STORAGE_CARD, *this);
113         delete __pFolderBackgroundBitmap;
114         delete __pContentManager;
115         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
116 }
117
118 AlbumListPresentationModel*
119 AlbumListPresentationModel::GetInstance(void)
120 {
121         AppLogDebug("ENTER");
122         if (__pPresentationModelInstance == null)
123         {
124                 CreateInstance();
125         }
126         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
127
128         return __pPresentationModelInstance;
129 }
130
131 result
132 AlbumListPresentationModel::Construct(void)
133 {
134         AppLogDebug("ENTER");
135         __pPresentationModelListener = new (std::nothrow) ArrayList();
136         __pPresentationModelListener->Construct();
137
138         ContentUpdateEventListener::GetInstance();
139         __pContentEventListener = new (std::nothrow) ArrayList();
140         __pContentEventListener->Construct();
141
142         IThumbnailEventListener* pThumbnailEventListener = static_cast<IThumbnailEventListener*>(this);
143         __pThumbnailEvent = new (std::nothrow) ThumbnailEvent();
144         __pThumbnailEvent->AddListener(*pThumbnailEventListener, true);
145
146         __pContentManager = new (std::nothrow) ContentManager();
147         __pContentManager->Construct();
148
149         DeviceManager::AddDeviceEventListener(DEVICE_TYPE_STORAGE_CARD, *this);
150
151         __pThumbnailProvider = ThumbnailProvider::GetInstance();
152         AppLogDebug("ENTER1");
153         GalleryApp* pApp = static_cast<GalleryApp*>(GalleryApp::GetInstance());
154         const IMap* pArguments = pApp->GetAppControlArguments();
155         String operationId = pApp->GetAppControlOperationId();
156
157         if (operationId.CompareTo(APPCONTROL_OPERATION_ID_MAIN) == 0)
158         {
159                 __appControlMode = APPCONTROL_MODE_MAIN;
160         }
161         else if (operationId.CompareTo(APPCONTROL_OPERATION_ID_PICK) == 0)
162         {
163                 __appControlMode = APPCONTROL_MODE_PICK;
164
165                 String mimeType = pApp->GetMimeType();
166                 if (mimeType.IsEmpty() != true)
167                 {
168                         if (mimeType.StartsWith(APPCONTROL_DATA_VIDEO, 0) == true)
169                         {
170                                 __appControlMediaType = APPCONTROL_MEDIA_TYPE_VIDEO;
171                         }
172                 }
173                 if (pArguments != null)
174                 {
175                         const String* selectionMode =
176                                         static_cast<const String*>(pArguments->GetValue(String(APPCONTROL_KEY_SELECTION_MODE)));
177                         if (selectionMode != null && selectionMode->CompareTo(APPCONTROL_DATA_MULTIPLE) == 0)
178                         {
179                                 __appControlSelectionMode = APPCONTROL_SELECTION_MODE_MULTIPLE;
180                         }
181                 }
182         }
183         else if (operationId.CompareTo(APPCONTROL_OPERATION_ID_CONFIGURE) == 0)
184         {
185                 __appControlMode = APPCONTROL_MODE_SETTING;
186         }
187
188         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
189
190         return E_SUCCESS;
191 }
192
193 void
194 AlbumListPresentationModel::CreateInstance(void)
195 {
196         AppLogDebug("ENTER");
197         __pPresentationModelInstance = new (std::nothrow) AlbumListPresentationModel;
198         result r = __pPresentationModelInstance->Construct();
199
200         if (IsFailed(r) == true)
201         {
202                 delete __pPresentationModelInstance;
203                 __pPresentationModelInstance = null;
204                 AppLogDebug("EXIT false");
205                 return;
206         }
207         std::atexit(DestroyInstance);
208         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
209 }
210
211 void
212 AlbumListPresentationModel::DestroyInstance(void)
213 {
214         AppLogDebug("ENTER");
215
216         delete __pThumbnailEvent;
217         __pThumbnailEvent = null;
218
219         if (__pAlbumInfoList != null)
220         {
221                 delete __pAlbumInfoList;
222                 __pAlbumInfoList = null;
223         }
224
225         if (__pPresentationModelListener != null)
226         {
227                 delete __pPresentationModelListener;
228                 __pPresentationModelListener = null;
229         }
230
231         if (__pContentEventListener != null)
232         {
233                 delete __pContentEventListener;
234                 __pContentEventListener = null;
235         }
236
237         delete __pPresentationModelInstance;
238         __pPresentationModelInstance = null;
239
240         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
241 }
242
243 result
244 AlbumListPresentationModel::AddPresentationModelListener(IAlbumEventListener* listener)
245 {
246         AppLogDebug("ENTER");
247         result r = __pPresentationModelListener->Add(listener);
248         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
249
250         return r;
251 }
252
253 result
254 AlbumListPresentationModel::RemovePresentationModelListener(IAlbumEventListener& listner)
255 {
256         AppLogDebug("ENTER");
257         result r = __pPresentationModelListener->Remove(listner);
258         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
259
260         return r;
261 }
262
263 void
264 AlbumListPresentationModel::AddContentEventListener(const IFormContentUpdateEventListener* listener)
265 {
266         AppLogDebug("ENTER");
267         if (__pContentEventListener != null)
268         {
269                 __pContentEventListener->RemoveAll();
270                 __pContentEventListener->Add((Object*)listener);
271                 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
272         }
273 }
274
275 void
276 AlbumListPresentationModel::RemoveContentEventListener(const IFormContentUpdateEventListener& listener)
277 {
278         AppLogDebug("ENTER");
279         __pContentEventListener->Remove(listener);
280         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
281 }
282
283 void
284 AlbumListPresentationModel::CallAlbumChangedEvent(void)
285 {
286         AppLogDebug("ENTER");
287         if (__pPresentationModelListener->GetCount() > 0)
288         {
289                 IEnumerator* pEnum = __pPresentationModelListener->GetEnumeratorN();
290
291                 while (pEnum->MoveNext() == E_SUCCESS)
292                 {
293                         IAlbumEventListener* pListener = null;
294                         pListener = static_cast<IAlbumEventListener*>(pEnum->GetCurrent());
295                         pListener->OnFileInfoChanged(CONTENT_TYPE_ALL);
296                 }
297
298                 delete pEnum;
299         }
300         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
301 }
302
303 result
304 AlbumListPresentationModel::RenameAlbum(const String& orgPath, const String& newPath,
305                 IAlbumRenameEventListener* pAlbumListener)
306 {
307         AppLogDebug("ENTER");
308         result r = Directory::Rename(orgPath, newPath);
309         if (IsFailed(r))
310         {
311                 AppLogDebug("Album Rename failed(%s)", GetErrorMessage(r));
312         }
313         else
314         {
315                 __pAlbumListener = pAlbumListener;
316                 __renameNewPath = newPath;\r
317                 r = ContentManager::ScanDirectory(newPath, true, NULL, __reqId);\r
318                 AppLogDebug("Content scan failed (%s)", GetErrorMessage(r));
319                 r = ContentManager::ScanDirectory(orgPath, true, NULL, __reqId);\r
320                 AppLogDebug("Content scan failed (%s)", GetErrorMessage(r));
321         }
322         AppLogDebug("EXIT");
323         return r;
324 }
325
326 bool AlbumListPresentationModel::OnContentScanCompleted (const Tizen::Base::String &scanPath)\r
327 {
328         AppLogDebug("ENTER");\r
329         if (__pAlbumListener && (scanPath == __renameNewPath))\r
330         {
331                 InitializeAlbumInfoList(CONTENT_TYPE_ALL);
332                 __pAlbumListener->OnAlbumRenameComplete();
333                 __renameNewPath = L"";
334                 AppLogDebug("EXIT");
335                 return true;
336         }
337         AppLogDebug("EXIT");
338         return false;
339 }
340
341 void
342 AlbumListPresentationModel::DrawPartialImage(Canvas& canvas, AlbumInfo* pAlbumInfo, const Rectangle& rectNew)
343 {
344         AppLogDebug("ENTER");
345         if (&canvas == null || __pFolderBackgroundBitmap == null || pAlbumInfo == null)
346         {
347                 AppLogDebug("EXIT 1(%s)", GetErrorMessage(GetLastResult()));
348                 return;
349         }
350
351         canvas.DrawBitmap(rectNew, *__pFolderBackgroundBitmap);
352
353         Bitmap* pBitmap = pAlbumInfo->GetFolderThumnailBitmap();
354
355         if (pBitmap != null)
356         {
357                 Rectangle rectFirst(ICON_CAP+FOLDER_CAP, ICON_CAP,
358                                 ICON_CAP * DOUBLE_NUMBER + ICON_SIZE * TRIPLE_NUMBER,
359                                 ICON_CAP * DOUBLE_NUMBER + ICON_SIZE * TRIPLE_NUMBER);
360                 canvas.DrawBitmap(rectFirst, *pBitmap);
361         }
362
363         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
364 }
365
366 void
367 AlbumListPresentationModel::DrawFontImage(Canvas& canvas, AlbumInfo* pAlbumInfo, int index)
368 {
369         AppLogDebug("ENTER");
370
371         if (&canvas == null)
372         {
373                 AppLogDebug("EXIT 1(%s)", GetErrorMessage(GetLastResult()));
374
375                 return;
376         }
377
378         Point pointText;
379         String strCnt;
380         Bitmap* pBitmap = null;
381         Font ftFolderName, ftDate, ftCnt,ftCntSmall;
382         EnrichedText enText;
383         EnrichedText enTextCount;
384         TextElement textElement;
385         TextElement textElementCount;
386
387         int allContentCount = pAlbumInfo->GetContentIdCount();
388         strCnt.Format(FORMAT_BUFFER_SIZE, L"%d", allContentCount);
389
390         String strFolderName = pAlbumInfo->GetAlbumName();
391
392         Rectangle rectCanvas(ICON_CAP + FOLDER_CAP, Y_POS_CANVAS, CANVAS_WIDTH, CANVAS_HEIGHT);
393         Color bgColor(COLOR_CANVAS_BG);
394         bgColor.SetAlpha(ALPHA_BG);
395         canvas.FillRectangle(bgColor, rectCanvas);
396
397         if (strFolderName == ResourceManager::GetString(L"IDS_COM_BODY_DOWNLOADS"))
398         {
399                 pBitmap = ResourceManager::GetBitmapN(IDB_ALBUM_ICON_DOWNLOAD);
400         }
401         else if (strFolderName == ResourceManager::GetString(L"IDS_MEDIABR_BODY_CAMERA_ROLL_ABB"))
402         {
403                 pBitmap = ResourceManager::GetBitmapN(IDB_ALBUM_ICON_CAMERA);
404         }
405         else
406         {
407                 pBitmap = ResourceManager::GetBitmapN(IDB_ALBUM_ICON_FOLDER);
408         }
409
410         if (pBitmap != null)
411         {
412                 canvas.DrawBitmap(Rectangle(ICON_CAP + FOLDER_CAP, rectCanvas.y + HEIGHT_OFFSET, CANVAS_IMAGE_FOLDER_WIDTH,
413                                         CANVAS_IMAGE_FOLDER_HEIGHT), *pBitmap);
414                 delete pBitmap;
415         }
416
417         ftFolderName.Construct(FONT_STYLE_PLAIN, FONT_SIZE_FOLDER_NAME);
418         ftDate.Construct(FONT_STYLE_PLAIN, FONT_SIZE_FOLDER_DATE);
419         ftCnt.Construct(FONT_STYLE_PLAIN, FONT_SIZE_FOLDER_COUNT);
420         ftCntSmall.Construct(FONT_STYLE_PLAIN, FONT_SIZE_FOLDER_COUNT_SMALL);
421
422         enText.Construct(Dimension(FOLDER_NAME_WIDTH, FOLDER_NAME_HEIGHT));
423         enText.SetHorizontalAlignment(TEXT_ALIGNMENT_LEFT);
424
425         textElement.Construct(strFolderName);
426         textElement.SetFont(ftFolderName);
427         textElement.SetTextColor(Color::GetColor(COLOR_ID_WHITE));
428         enText.SetTextAbbreviationEnabled(true);
429         enText.Add(textElement);
430
431         enTextCount.Construct(Dimension(IMAGE_COUNT_WIDTH, IMAGE_COUNT_HEIGHT));
432         enTextCount.SetHorizontalAlignment(TEXT_ALIGNMENT_RIGHT);
433
434         textElementCount.Construct(strCnt);
435
436         if (strCnt.GetLength() > 3)
437         {
438                 textElementCount.SetFont(ftCntSmall);
439         }
440         else
441         {
442                 textElementCount.SetFont(ftCnt);
443         }
444         textElementCount.SetTextColor(Color::GetColor(COLOR_ID_WHITE));
445         enTextCount.SetTextAbbreviationEnabled(true);
446         enTextCount.Add(textElementCount);
447
448         pointText = Point(CANVAS_MARGIN_LEFT + CANVAS_IMAGE_FOLDER_WIDTH + GAP + FOLDER_NAME_WIDTH,
449                         rectCanvas.y + HEIGHT_OFFSET);
450         canvas.SetFont(ftCnt);
451         canvas.SetForegroundColor(Color::GetColor(COLOR_ID_WHITE));
452         canvas.DrawText(pointText, enTextCount);
453
454         pointText = Point(CANVAS_MARGIN_LEFT + CANVAS_IMAGE_FOLDER_WIDTH + GAP,
455                         rectCanvas.y + GAP + HEIGHT_OFFSET);
456         canvas.SetForegroundColor(Color::GetColor(COLOR_ID_WHITE));
457         canvas.DrawText(pointText, enText);
458
459         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
460 }
461
462 Bitmap*
463 AlbumListPresentationModel::CreateMergeBitmapN(const int index)
464 {
465         AppLogDebug("ENTER index(%d)", index);
466
467         if (__pAlbumInfoList == null)
468         {
469                 AppLogDebug("EXIT 1(%s)", GetErrorMessage(GetLastResult()));
470
471                 return null;
472         }
473
474         AlbumInfo* pAlbumInfo = static_cast<AlbumInfo*>(__pAlbumInfoList->GetAt(index));
475
476         Bitmap* pBitmapNew = new (std::nothrow) Bitmap();
477         BufferInfo bufferinfo;
478         Canvas canvas;
479         Rectangle rectNew(0, 0, FOLDER_SIZE_WIDTH, FOLDER_SIZE_HEIGHT);
480         Dimension size(FOLDER_SIZE_WIDTH, FOLDER_SIZE_HEIGHT);
481
482         result r = pBitmapNew->Construct(size, BITMAP_PIXEL_FORMAT_ARGB8888);
483         if (r != E_SUCCESS)
484         {
485                 delete pBitmapNew;
486                 AppLogDebug("EXIT 3(%s)", GetErrorMessage(GetLastResult()));
487
488                 return null;
489         }
490
491         pBitmapNew->Lock(bufferinfo);
492         r = canvas.Construct(bufferinfo);
493         if (r != E_SUCCESS)
494         {
495                 delete pBitmapNew;
496                 AppLogDebug("EXIT 4(%s)", GetErrorMessage(GetLastResult()));
497
498                 return null;
499         }
500
501         DrawPartialImage(canvas, pAlbumInfo, rectNew);
502         DrawFontImage(canvas, pAlbumInfo, index);
503
504         pBitmapNew->Unlock();
505         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
506
507         return pBitmapNew;
508 }
509
510 result
511 AlbumListPresentationModel::StartAppControl(const String& providerId, const String& operationId,
512                 const String* pUriData, const String* pMimeType, const HashMap* pDataList,
513                 IAppControlResponseListener* pListener)
514 {
515         AppLogDebug("ENTER");
516         FileListPresentationModel::GetInstance()->ClearProviderId();
517         AppControl* pAc = AppManager::FindAppControlN(providerId, operationId);
518         if (pAc == null)
519         {
520                 AppLogDebug("EXIT 1(%s)", GetErrorMessage(GetLastResult()));
521
522                 return E_FAILURE;
523         }
524         result r = pAc->Start(pUriData, pMimeType, pDataList, pListener);
525
526         if (r == E_SUCCESS)
527         {
528                 GalleryApp* pGalleryApp = static_cast<GalleryApp*>(GalleryApp::GetInstance());
529                 pGalleryApp->SetFrameEnabled(false);
530         }
531
532         delete pAc;
533
534         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
535
536         return r;
537 }
538
539 String
540 AlbumListPresentationModel::GetDirectoryName(String& directoryPath) const
541 {
542         AppLogDebug("ENTER");
543         String directoryName;
544         int lastIndex = -1;
545
546         directoryPath.LastIndexOf(DIRECTORY_SEPARATOR, directoryPath.GetLength() - 1, lastIndex);
547         directoryPath.SubString(lastIndex + 1, directoryName);
548         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
549
550         return directoryName;
551 }
552
553 int
554 AlbumListPresentationModel::GetFolderCount(void) const
555 {
556         AppLogDebug("ENTER");
557         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
558
559         return __pAlbumInfoList->GetCount();
560 }
561
562 void
563 AlbumListPresentationModel::InitializeAlbumInfoList(const ContentType& contentType)
564 {
565         AppLogDebug("ENTER");
566
567         if (__pFolderBackgroundBitmap == null)
568         {
569                 __pFolderBackgroundBitmap = ResourceManager::GetBitmapN(IDB_MAIN_FOLDER_BACKGROUND);
570         }
571
572         if (__pAlbumInfoList != null)
573         {
574                 AlbumInfo* pInfo = null;
575                 for (int i = 0; i < __pAlbumInfoList->GetCount(); ++i)
576                 {
577                         pInfo = static_cast<AlbumInfo*>(__pAlbumInfoList->GetAt(i));
578                         if (pInfo->GetAlbumName().IsEmpty() == false)
579                         {
580                                 AppLog("Print AlbumName(%d)(%ls)", i, pInfo->GetAlbumName().GetPointer());
581                         }
582                 }
583                 delete __pAlbumInfoList;
584         }
585         __pAlbumInfoList = new (std::nothrow) ArrayList(SingleObjectDeleter);
586
587         ContentDirectory directory;
588
589         if (contentType == CONTENT_TYPE_ALL)
590         {
591                 IListT<ContentType>* pListT = new (std::nothrow) ArrayListT<ContentType>();
592                 TryReturnVoid(pListT != null, "Unable to Create ArrayListT");
593                 pListT->Add(CONTENT_TYPE_IMAGE);
594                 pListT->Add(CONTENT_TYPE_VIDEO);
595
596                 result r = directory.Construct(*pListT);
597                 delete pListT;
598
599                 if (r != E_SUCCESS)
600                 {
601                         delete __pAlbumInfoList;
602                         __pAlbumInfoList = null;
603                         AppLogDebug("EXIT 1(%s)", GetErrorMessage(r));
604                         return;
605                 }
606         }
607         else
608         {
609                 result r = directory.Construct(contentType);
610                 if (r != E_SUCCESS)
611                 {
612                         delete __pAlbumInfoList;
613                         __pAlbumInfoList = null;
614                         AppLogDebug("EXIT 2(%s)", GetErrorMessage(r));
615                         return;
616                 }
617         }
618
619         IList* pContentDirectoryPathList = directory.GetContentDirectoryPathListN(SORT_ORDER_ASCENDING);
620         String* pDirPath = null;
621         AlbumInfo* pAlbumInfo = null;
622
623         AlbumInfo* pAllAlbumInfo = new (std::nothrow) AlbumInfo();
624         AlbumInfo* pCameraAlbumInfo = new (std::nothrow) AlbumInfo();
625         AlbumInfo* pDownloadAlbumInfo = new (std::nothrow) AlbumInfo();
626         AlbumInfo* pNoNameAlbumInfo = new (std::nothrow) AlbumInfo();
627
628         result r = pAllAlbumInfo->Construct();
629         TryCatch(IsFailed(r) == E_SUCCESS,,"new pNoNameAlbumInfo is %s",GetErrorMessage(r));
630         pAllAlbumInfo->SetAlbumName(ResourceManager::GetString(L"IDS_MEDIABR_BODY_ALL_ALBUMS"));
631
632         r = pCameraAlbumInfo->Construct();
633         TryCatch(IsFailed(r) == E_SUCCESS,,"new pNoNameAlbumInfo is %s",GetErrorMessage(r));
634         pCameraAlbumInfo->SetAlbumName(ResourceManager::GetString(L"IDS_MEDIABR_BODY_CAMERA_ROLL_ABB"));
635
636         r = pDownloadAlbumInfo->Construct();
637         TryCatch(IsFailed(r) == E_SUCCESS,,"new pNoNameAlbumInfo is %s",GetErrorMessage(r));
638         pDownloadAlbumInfo->SetAlbumName(ResourceManager::GetString(L"IDS_COM_BODY_DOWNLOADS"));
639
640         r = pNoNameAlbumInfo->Construct();
641         TryCatch(IsFailed(r) == E_SUCCESS,,"new pNoNameAlbumInfo is %s",GetErrorMessage(r));
642         pNoNameAlbumInfo->SetAlbumName(ResourceManager::GetString(L"IDS_MEDIABR_POP_NO_NAME"));
643
644         if ((pContentDirectoryPathList != null) && (pContentDirectoryPathList->GetCount() > 0))
645         {
646                 IEnumerator* pEnum = pContentDirectoryPathList->GetEnumeratorN();
647                 while (pEnum->MoveNext() == E_SUCCESS)
648                 {
649                         pDirPath = static_cast<String*>(pEnum->GetCurrent());
650                         String mediaPath = RESERVED_MEDIA_PATH;
651                         int length = mediaPath.GetLength();
652                         mediaPath.Remove(length-1,1);
653
654                         if (pDirPath == null)
655                         {
656                                 continue;
657                         }
658
659                         if ((*pDirPath == RESERVED_CAMERA_PATH) ||(*pDirPath == RESERVED_CAMERA_PATH_EXT))
660                         {
661                                 IList* pContentIdList = GetContentIdListInDirectoryN(*pDirPath, contentType);
662
663                                 if (pContentIdList != null)
664                                 {
665                                         pCameraAlbumInfo->AppendDirectory(*pDirPath);
666                                         pCameraAlbumInfo->AppendContentIdList(*pContentIdList);
667
668                                         pAllAlbumInfo->AppendDirectory(*pDirPath);
669                                         pAllAlbumInfo->AppendContentIdList(*pContentIdList);
670                                         delete pContentIdList;
671                                 }
672                         }
673                         else if (*pDirPath == RESERVED_DOWNLOAD_PATH)
674                         {
675                                 IList* pContentIdList = GetContentIdListInDirectoryN(*pDirPath, contentType);
676
677                                 if (pContentIdList != null)
678                                 {
679                                         pDownloadAlbumInfo->AppendDirectory(*pDirPath);
680                                         pDownloadAlbumInfo->AppendContentIdList(*pContentIdList);
681
682                                         pAllAlbumInfo->AppendDirectory(*pDirPath);
683                                         pAllAlbumInfo->AppendContentIdList(*pContentIdList);
684
685                                         pContentIdList->RemoveAll(true);
686                                         delete pContentIdList;
687                                 }
688                         }
689                         else if (*pDirPath == mediaPath)
690                         {
691                                 IList* pContentIdList = GetContentIdListInDirectoryN(*pDirPath, contentType);
692
693                                 if (pContentIdList != null)
694                                 {
695                                         pNoNameAlbumInfo->AppendDirectory(*pDirPath);
696                                         pNoNameAlbumInfo->AppendContentIdList(*pContentIdList);
697
698                                         pAllAlbumInfo->AppendDirectory(*pDirPath);
699                                         pAllAlbumInfo->AppendContentIdList(*pContentIdList);
700
701                                         pContentIdList->RemoveAll(true);
702                                         delete pContentIdList;
703                                 }
704                         }
705                         else
706                         {
707                                 IList* pContentIdList = GetContentIdListInDirectoryN(*pDirPath, contentType);
708
709                                 if (pContentIdList != null)
710                                 {
711                                         if (pContentIdList->GetCount() > 0)
712                                         {
713                                                 pAlbumInfo = new (std::nothrow) AlbumInfo();
714                                                 IList* pSubDirList = new (std::nothrow) ArrayList(SingleObjectDeleter);
715
716                                                 pSubDirList->Add(new (std::nothrow) String(*pDirPath));
717
718                                                 pAlbumInfo->Construct(GetDirectoryNameFromFullPath(*pDirPath), contentType, *pSubDirList,
719                                                                 *pContentIdList);
720
721                                                 pAllAlbumInfo->AppendDirectory(*pDirPath);
722                                                 pAllAlbumInfo->AppendContentIdList(*pContentIdList);
723
724                                                 pSubDirList->RemoveAll(true);
725                                                 delete pSubDirList;
726                                         }
727
728                                         if (pContentIdList != NULL)
729                                         {
730                                                 pContentIdList->RemoveAll(true);
731                                                 delete pContentIdList;
732                                         }
733                                 }
734                                 if (pAlbumInfo != null && pAlbumInfo->GetContentIdCount() > 0)
735                                 {
736                                         __pAlbumInfoList->Add(pAlbumInfo);
737                                         AppLog("Adding album");
738                                 }
739                                 else
740                                 {
741                                         delete pAlbumInfo;
742                                 }
743                                 pAlbumInfo = null;
744                         }
745                 }
746
747                 int insertIndex = 0;
748                 if (pAllAlbumInfo->GetContentIdCount() > 0)
749                 {
750                         __pAlbumInfoList->InsertAt((Object&)*pAllAlbumInfo, insertIndex++);
751                 }
752                 else
753                 {
754                         delete pAllAlbumInfo;
755                 }
756
757                 if (pCameraAlbumInfo->GetContentIdCount() > 0)
758                 {
759                         __pAlbumInfoList->InsertAt((Object&)*pCameraAlbumInfo, insertIndex++);
760                 }
761                 else
762                 {
763                         delete pCameraAlbumInfo;
764                 }
765
766                 if (pDownloadAlbumInfo->GetContentIdCount() > 0)
767                 {
768                         __pAlbumInfoList->InsertAt((Object&)*pDownloadAlbumInfo, insertIndex++);
769                 }
770                 else
771                 {
772                         delete pDownloadAlbumInfo;
773                 }
774
775                 if (pNoNameAlbumInfo->GetContentIdCount() > 0)
776                 {
777                         __pAlbumInfoList->InsertAt((Object&)*pNoNameAlbumInfo, insertIndex++);
778                 }
779                 else
780                 {
781                         delete pNoNameAlbumInfo;
782                 }
783
784                 delete pEnum;
785         }
786         else
787         {
788                 delete pAllAlbumInfo;
789                 delete pCameraAlbumInfo;
790                 delete pDownloadAlbumInfo;
791                 delete pNoNameAlbumInfo;
792         }
793
794         if (pContentDirectoryPathList != null)
795         {
796                 pContentDirectoryPathList->RemoveAll(true);
797                 delete pContentDirectoryPathList;
798         }
799         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
800         return;
801
802 CATCH:
803 if (pContentDirectoryPathList != null)
804 {
805         pContentDirectoryPathList->RemoveAll(true);
806         delete pContentDirectoryPathList;
807 }
808         delete pAllAlbumInfo;
809         delete pCameraAlbumInfo;
810         delete pDownloadAlbumInfo;
811         delete pNoNameAlbumInfo;
812 }
813
814 String
815 AlbumListPresentationModel::GetDirectoryNameFromFullPath(const String& fullPath)const
816 {
817         AppLogDebug("ENTER");
818         if (fullPath == null)
819         {
820                 AppLogDebug("EXIT 1(%s)", GetErrorMessage(GetLastResult()));
821
822                 return null;
823         }
824         String delim(DIRECTORY_SEPARATOR);
825         StringTokenizer st(fullPath,delim);
826         String token;
827         while (st.HasMoreTokens())
828         {
829                 st.GetNextToken(token);
830         }
831         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
832
833         return token;
834 }
835
836 IList*
837 AlbumListPresentationModel::GetContentIdListInDirectoryN(const String& directoryPath, ContentType contentType)const
838 {
839         AppLogDebug("ENTER");
840
841         if (&directoryPath == null)
842         {
843                 AppLogDebug("EXIT 1(%s)", GetErrorMessage(GetLastResult()));
844                 return null;
845         }
846
847         ContentDirectory directory;
848         const String contentDirectoryPath = directoryPath;
849         if (contentType == CONTENT_TYPE_ALL)
850         {
851                 IListT<ContentType>* pListT = new (std::nothrow) ArrayListT<ContentType>();
852                 pListT->Add(CONTENT_TYPE_IMAGE);
853                 pListT->Add(CONTENT_TYPE_VIDEO);
854                 result r = directory.Construct(*pListT);
855                 delete pListT;
856                 if (r != E_SUCCESS)
857                 {
858                         AppLogDebug("EXIT 2(%s)", GetErrorMessage(GetLastResult()));
859                         return null;
860                 }
861         }
862         else
863         {
864                 result r = directory.Construct(contentType);
865                 if (r != E_SUCCESS)
866                 {
867                         AppLogDebug("EXIT 3(%s)", GetErrorMessage(GetLastResult()));
868                         return null;
869                 }
870         }
871
872         int pageNo = 1;
873         int countPerPage = directory.GetContentDirectoryItemCount(contentDirectoryPath);
874         IList* pContentInfoList = directory.GetContentDirectoryItemListN(contentDirectoryPath, pageNo, countPerPage,
875                         CONTENT_INFO_ORDER, SORT_ORDER_ASCENDING);
876         IList* pContentIdList = new (std::nothrow) ArrayList(SingleObjectDeleter);
877
878         if ((pContentInfoList != null) && (pContentInfoList->GetCount() > 0))
879         {
880                 IEnumerator* pEnum = pContentInfoList->GetEnumeratorN();
881                 ContentId* pItem;
882                 while (pEnum->MoveNext() == E_SUCCESS)
883                 {
884                         ContentInfo* pContentInfo = static_cast<ContentInfo*>(pEnum->GetCurrent());
885                         pItem = new (std::nothrow) ContentId(pContentInfo->GetContentId());
886                         pContentIdList->Add(pItem);
887                 }
888                 delete pEnum;
889         }
890
891         if (pContentInfoList != NULL)
892         {
893                 pContentInfoList->RemoveAll(true);
894                 delete pContentInfoList;
895         }
896
897         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
898
899         return pContentIdList;
900 }
901
902 result
903 AlbumListPresentationModel::DeleteContentFile(const ContentId& contentId)
904 {
905         AppLogDebug("ENTER");
906         result r = E_SUCCESS;
907         ContentManager contentManager;
908         r = contentManager.Construct();
909         if (r == E_SUCCESS)
910         {
911                 r = contentManager.DeleteContent(contentId);
912         }
913         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
914
915         return r;
916 }
917
918 AppControlMode
919 AlbumListPresentationModel::GetAppControlMode(void) const
920 {
921         AppLogDebug("Enter");
922         AppLogDebug("Exit");
923         return __appControlMode;
924 }
925
926 AppControlMediaType
927 AlbumListPresentationModel::GetAppControlMediaType(void) const
928 {
929         AppLogDebug("Enter");
930         AppLogDebug("Exit");
931         return __appControlMediaType;
932 }
933
934 AppControlSelectionMode
935 AlbumListPresentationModel::GetAppControlSelectionMode(void) const
936 {
937         AppLogDebug("Enter");
938         AppLogDebug("Exit");
939         return __appControlSelectionMode;
940 }
941
942 IList*
943 AlbumListPresentationModel::GetAlbumInfoList(void) const
944 {
945         AppLogDebug("ENTER");
946         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
947
948         return __pAlbumInfoList;
949 }
950
951 IList* AlbumListPresentationModel::GetContentIdListAtIndexN(const IList& indexList) const
952 {
953         AppLogDebug("ENTER");
954         ContentDirectory directory;
955         IList* pContentIdList = new (std::nothrow) ArrayList(SingleObjectDeleter);
956
957         IListT<ContentType>* pListT = new (std::nothrow) ArrayListT<ContentType>();
958         TryReturn(pListT != null,null, "Unable to Create ArrayListT");
959         pListT->Add(CONTENT_TYPE_IMAGE);
960         pListT->Add(CONTENT_TYPE_VIDEO);
961
962         result r = directory.Construct(*pListT);
963         delete pListT;
964
965         if (r != E_SUCCESS)
966         {
967                 AppLogDebug("EXIT 3(%s)", GetErrorMessage(GetLastResult()));
968                 delete pContentIdList;
969                 return null;
970         }
971
972         if (static_cast<const Integer*>(indexList.GetAt(0))->ToInt() == 0) //if all-album is selected, get all files for delete
973         {
974                 IList* pContentDirectoryPathList = directory.GetContentDirectoryPathListN(SORT_ORDER_ASCENDING);
975                 String* pDirPath = null;
976                 if ((pContentDirectoryPathList != null) && (pContentDirectoryPathList->GetCount() > 0))
977                 {
978                         int loopCount = pContentDirectoryPathList->GetCount();
979                         for (int i = 0; i < loopCount; ++i)
980                         {
981                                 pDirPath = static_cast<String*>(pContentDirectoryPathList->GetAt(i));
982                                 pContentIdList->AddItems(*(GetContentIdListInDirectoryN(*pDirPath, CONTENT_TYPE_ALL)));
983                         }
984                 }
985         }
986         else
987         {
988                 if(__pAlbumInfoList != null)
989                 {
990                         int loopCount = indexList.GetCount();
991                         AlbumInfo* pInfo = null;
992                         for (int i = 0; i < loopCount; ++i)
993                         {
994                                 int index  = static_cast<const Integer*>(indexList.GetAt(i))->ToInt();
995                                 pInfo = static_cast<AlbumInfo*>(__pAlbumInfoList->GetAt(index));
996
997                                 if(pInfo != null)
998                                 {
999                                         IList* pList = pInfo->GetContentIdListN();
1000                                         if(pList != null)
1001                                         {
1002                                                 pContentIdList->AddItems(*pList);
1003                                         }
1004                                 }
1005                         }
1006                 }
1007         }
1008         return pContentIdList;
1009 }
1010
1011 void
1012 AlbumListPresentationModel::OnThumbnailReceivedN(IEventArg& eventArg)
1013 {
1014         AppLogDebug("ENTER");
1015         if (&eventArg != null)
1016         {
1017                 ThumbnailEventArg* pThumbnailEventArg = static_cast<ThumbnailEventArg*>(&eventArg);
1018                 if (pThumbnailEventArg == null)
1019                 {
1020                         AppLogDebug("EXIT 1(%s)", GetErrorMessage(GetLastResult()));
1021
1022                         return;
1023                 }               
1024                 ThumbnailInfo* pThumbnailInfo = pThumbnailEventArg->GetThumbnailInfoN();
1025                 if (pThumbnailInfo == null)
1026                 {
1027                         AppLogDebug("EXIT 2(%s)", GetErrorMessage(GetLastResult()));
1028
1029                         return;
1030                 }
1031
1032                 ContentId contentId = pThumbnailInfo->GetContentId();
1033                 ArrayList* pUpdateFolderIndex = new (std::nothrow) ArrayList(SingleObjectDeleter);
1034
1035                 if (__pAlbumInfoList != null)
1036                 {
1037                         int outterLoopCount = __pAlbumInfoList->GetCount();
1038                         for (int x = 0; x < outterLoopCount; ++x)
1039                         {
1040                                 AlbumInfo* pAlbumInfo = static_cast<AlbumInfo*>(__pAlbumInfoList->GetAt(x));
1041                                 int innerLoopCount = pAlbumInfo->GetContentIdCount();
1042                                 for (int i = 0; i < innerLoopCount; ++i)
1043                                 {
1044                                         if (i >= FOLDER_BITMAP_MAX)
1045                                         {
1046                                                 break;
1047                                         }
1048
1049                                         if (contentId == pAlbumInfo->GetContentId(i))
1050                                         {
1051                                                 if (pAlbumInfo->GetFolderThumnailBitmap() == null)
1052                                                 {
1053                                                         pAlbumInfo->SetFolderThumnailBitmap(pThumbnailInfo->GetBitmapN());
1054                                                         pUpdateFolderIndex->Add(new (std::nothrow) Integer(x));
1055                                                 }
1056                                         }
1057                                 }
1058                         }
1059                 }
1060
1061                 if (__pPresentationModelListener->GetCount() > 0)
1062                 {
1063                         IEnumerator* pEnum = __pPresentationModelListener->GetEnumeratorN();
1064                         while (pEnum->MoveNext() == E_SUCCESS)
1065                         {
1066                                 IAlbumEventListener* pListener = null;
1067                                 pListener = static_cast<IAlbumEventListener*>(pEnum->GetCurrent());
1068
1069                                 int loopCount = pUpdateFolderIndex->GetCount();
1070                                 for (int k = 0; k < loopCount; ++k)
1071                                 {
1072                                         Integer *pIndex = (Integer*)pUpdateFolderIndex->GetAt(k);
1073                                         int folderIndex = pIndex->ToInt();
1074                                         pListener->OnThumbnailDecoded(folderIndex);
1075                                 }
1076                         }
1077                         delete pEnum;
1078                 }
1079
1080                 delete pUpdateFolderIndex;
1081         }
1082         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
1083 }
1084
1085 void
1086 AlbumListPresentationModel::OnContentCreated(void)
1087 {
1088         AppLogDebug("ENTER");
1089         if (__updateProgressStatus == true)
1090         {
1091                 AppLogDebug("EXIT update in application");
1092                 return;
1093         }
1094         IFormContentUpdateEventListener* pInterface = null;
1095         IEnumerator* pEventEnum = __pContentEventListener->GetEnumeratorN();
1096         while (pEventEnum->MoveNext() == E_SUCCESS)
1097         {
1098                 pInterface = static_cast<IFormContentUpdateEventListener*>(pEventEnum->GetCurrent());
1099                 if (pInterface != null)
1100                 {
1101                         pInterface->OnContentUpdated();
1102                 }
1103         }
1104         delete pEventEnum;
1105         AppLogDebug("EXIT");
1106 }
1107
1108 void
1109 AlbumListPresentationModel::OnContentUpdated(void)
1110 {
1111         AppLogDebug("ENTER");
1112         if (__updateProgressStatus == true)
1113         {
1114                 AppLogDebug("EXIT update in application");
1115                 return;
1116         }
1117         IFormContentUpdateEventListener* pInterface = null;
1118         IEnumerator* pEventEnum = __pContentEventListener->GetEnumeratorN();
1119         while (pEventEnum->MoveNext() == E_SUCCESS)
1120         {
1121                 pInterface = static_cast<IFormContentUpdateEventListener*>(pEventEnum->GetCurrent());
1122                 pInterface->OnContentUpdated();
1123         }
1124         delete pEventEnum;
1125         AppLogDebug("EXIT");
1126 }
1127
1128 void
1129 AlbumListPresentationModel::OnContentDeleted(void)
1130 {
1131         AppLogDebug("ENTER");
1132         if (__updateProgressStatus == true)
1133         {
1134                 AppLogDebug("EXIT update in application");
1135                 return;
1136         }
1137         IFormContentUpdateEventListener* pInterface = null;
1138         IEnumerator* pEventEnum = __pContentEventListener->GetEnumeratorN();
1139         while (pEventEnum->MoveNext() == E_SUCCESS)
1140         {
1141                 pInterface = static_cast<IFormContentUpdateEventListener*>(pEventEnum->GetCurrent());
1142                 pInterface->OnContentUpdated();
1143         }
1144         delete pEventEnum;
1145         AppLogDebug("EXIT");
1146 }
1147
1148 void
1149 AlbumListPresentationModel::OnDeviceStateChanged(DeviceType deviceType, const Tizen::Base::String& state)
1150 {
1151         AppLogDebug("ENTER");
1152         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
1153 }
1154
1155 void
1156 AlbumListPresentationModel::SetUpdateProgressStatus(const bool status)
1157 {
1158         AppLogDebug("ENTER status = %d",status);
1159         __updateProgressStatus = status;
1160         AppLogDebug("EXIT");
1161 }
1162
1163 result
1164 AlbumListPresentationModel::RotateImage(int index, RotateMode rotateMode)
1165 {
1166         return E_SUCCESS;
1167 }
1168
1169 void
1170 AlbumListPresentationModel::RequestThumbnail(const int folderIndex, const int pathIndex)
1171 {
1172         AppLogDebug("ENTER : folderIndex(%d), pathIndex(%d)", folderIndex, pathIndex);
1173
1174         ContentId contentId;
1175         if (__pAlbumInfoList != null)
1176         {
1177                 AlbumInfo* pAlbumInfo = static_cast<AlbumInfo*>(__pAlbumInfoList->GetAt(folderIndex));
1178                 contentId = pAlbumInfo->GetContentId(pathIndex);
1179         }
1180
1181         __pThumbnailProvider->RequestThumbnail(contentId, __pThumbnailEvent);
1182         AppLogDebug("EXIT");
1183 }
1184
1185 void
1186 AlbumListPresentationModel::ClearThumbnailRequests(const bool appTerminating)
1187 {
1188         AppLogDebug("ENTER");
1189         if (__pThumbnailProvider != null)
1190         {
1191                 __pThumbnailProvider->ClearThumbnailRequests(appTerminating);
1192         }
1193         AppLogDebug("EXIT");
1194 }