f3306a1c042875ad85b78bfea80ccbab05790d48
[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         String strDirectory = pAlbumInfo->GetDirectory(0);
392         AppLog("The directory path is  %ls" , strDirectory.GetPointer());
393
394         Rectangle rectCanvas(ICON_CAP + FOLDER_CAP, Y_POS_CANVAS, CANVAS_WIDTH, CANVAS_HEIGHT);
395         Color bgColor(COLOR_CANVAS_BG);
396         bgColor.SetAlpha(ALPHA_BG);
397         canvas.FillRectangle(bgColor, rectCanvas);
398
399
400         if (strDirectory == RESERVED_DOWNLOAD_PATH)
401         {
402                 pBitmap = ResourceManager::GetBitmapN(IDB_ALBUM_ICON_DOWNLOAD);
403         }
404         else if (strDirectory == RESERVED_CAMERA_PATH ||  strDirectory == RESERVED_CAMERA_PATH_EXT)
405         {
406                 pBitmap = ResourceManager::GetBitmapN(IDB_ALBUM_ICON_CAMERA);
407         }
408         else
409         {
410                 pBitmap = ResourceManager::GetBitmapN(IDB_ALBUM_ICON_FOLDER);
411         }
412
413         if (pBitmap != null)
414         {
415                 canvas.DrawBitmap(Rectangle(ICON_CAP + FOLDER_CAP, rectCanvas.y + HEIGHT_OFFSET, CANVAS_IMAGE_FOLDER_WIDTH,
416                                         CANVAS_IMAGE_FOLDER_HEIGHT), *pBitmap);
417                 delete pBitmap;
418         }
419
420         ftFolderName.Construct(FONT_STYLE_PLAIN, FONT_SIZE_FOLDER_NAME);
421         ftDate.Construct(FONT_STYLE_PLAIN, FONT_SIZE_FOLDER_DATE);
422         ftCnt.Construct(FONT_STYLE_PLAIN, FONT_SIZE_FOLDER_COUNT);
423         ftCntSmall.Construct(FONT_STYLE_PLAIN, FONT_SIZE_FOLDER_COUNT_SMALL);
424
425         enText.Construct(Dimension(FOLDER_NAME_WIDTH, FOLDER_NAME_HEIGHT));
426         enText.SetHorizontalAlignment(TEXT_ALIGNMENT_LEFT);
427
428         textElement.Construct(strFolderName);
429         textElement.SetFont(ftFolderName);
430         textElement.SetTextColor(Color::GetColor(COLOR_ID_WHITE));
431         enText.SetTextAbbreviationEnabled(true);
432         enText.Add(textElement);
433
434         enTextCount.Construct(Dimension(IMAGE_COUNT_WIDTH, IMAGE_COUNT_HEIGHT));
435         enTextCount.SetHorizontalAlignment(TEXT_ALIGNMENT_RIGHT);
436
437         textElementCount.Construct(strCnt);
438
439         if (strCnt.GetLength() > 3)
440         {
441                 textElementCount.SetFont(ftCntSmall);
442         }
443         else
444         {
445                 textElementCount.SetFont(ftCnt);
446         }
447         textElementCount.SetTextColor(Color::GetColor(COLOR_ID_WHITE));
448         enTextCount.SetTextAbbreviationEnabled(true);
449         enTextCount.Add(textElementCount);
450
451         pointText = Point(CANVAS_MARGIN_LEFT + CANVAS_IMAGE_FOLDER_WIDTH + GAP + FOLDER_NAME_WIDTH,
452                         rectCanvas.y + HEIGHT_OFFSET);
453         canvas.SetFont(ftCnt);
454         canvas.SetForegroundColor(Color::GetColor(COLOR_ID_WHITE));
455         canvas.DrawText(pointText, enTextCount);
456
457         pointText = Point(CANVAS_MARGIN_LEFT + CANVAS_IMAGE_FOLDER_WIDTH + GAP,
458                         rectCanvas.y + GAP + HEIGHT_OFFSET);
459         canvas.SetForegroundColor(Color::GetColor(COLOR_ID_WHITE));
460         canvas.DrawText(pointText, enText);
461
462         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
463 }
464
465 Bitmap*
466 AlbumListPresentationModel::CreateMergeBitmapN(const int index)
467 {
468         AppLogDebug("ENTER index(%d)", index);
469
470         if (__pAlbumInfoList == null)
471         {
472                 AppLogDebug("EXIT 1(%s)", GetErrorMessage(GetLastResult()));
473
474                 return null;
475         }
476
477         AlbumInfo* pAlbumInfo = static_cast<AlbumInfo*>(__pAlbumInfoList->GetAt(index));
478
479         Bitmap* pBitmapNew = new (std::nothrow) Bitmap();
480         BufferInfo bufferinfo;
481         Canvas canvas;
482         Rectangle rectNew(0, 0, FOLDER_SIZE_WIDTH, FOLDER_SIZE_HEIGHT);
483         Dimension size(FOLDER_SIZE_WIDTH, FOLDER_SIZE_HEIGHT);
484
485         result r = pBitmapNew->Construct(size, BITMAP_PIXEL_FORMAT_ARGB8888);
486         if (r != E_SUCCESS)
487         {
488                 delete pBitmapNew;
489                 AppLogDebug("EXIT 3(%s)", GetErrorMessage(GetLastResult()));
490
491                 return null;
492         }
493
494         pBitmapNew->Lock(bufferinfo);
495         r = canvas.Construct(bufferinfo);
496         if (r != E_SUCCESS)
497         {
498                 delete pBitmapNew;
499                 AppLogDebug("EXIT 4(%s)", GetErrorMessage(GetLastResult()));
500
501                 return null;
502         }
503
504         DrawPartialImage(canvas, pAlbumInfo, rectNew);
505         DrawFontImage(canvas, pAlbumInfo, index);
506
507         pBitmapNew->Unlock();
508         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
509
510         return pBitmapNew;
511 }
512
513 result
514 AlbumListPresentationModel::StartAppControl(const String& providerId, const String& operationId,
515                 const String* pUriData, const String* pMimeType, const HashMap* pDataList,
516                 IAppControlResponseListener* pListener)
517 {
518         AppLogDebug("ENTER");
519         FileListPresentationModel::GetInstance()->ClearProviderId();
520         AppControl* pAc = AppManager::FindAppControlN(providerId, operationId);
521         if (pAc == null)
522         {
523                 AppLogDebug("EXIT 1(%s)", GetErrorMessage(GetLastResult()));
524
525                 return E_FAILURE;
526         }
527         result r = pAc->Start(pUriData, pMimeType, pDataList, pListener);
528
529         if (r == E_SUCCESS)
530         {
531                 GalleryApp* pGalleryApp = static_cast<GalleryApp*>(GalleryApp::GetInstance());
532                 pGalleryApp->SetFrameEnabled(false);
533         }
534
535         delete pAc;
536
537         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
538
539         return r;
540 }
541
542 String
543 AlbumListPresentationModel::GetDirectoryName(String& directoryPath) const
544 {
545         AppLogDebug("ENTER");
546         String directoryName;
547         int lastIndex = -1;
548
549         directoryPath.LastIndexOf(DIRECTORY_SEPARATOR, directoryPath.GetLength() - 1, lastIndex);
550         directoryPath.SubString(lastIndex + 1, directoryName);
551         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
552
553         return directoryName;
554 }
555
556 int
557 AlbumListPresentationModel::GetFolderCount(void) const
558 {
559         AppLogDebug("ENTER");
560         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
561
562         return __pAlbumInfoList->GetCount();
563 }
564
565 void
566 AlbumListPresentationModel::InitializeAlbumInfoList(const ContentType& contentType)
567 {
568         AppLogDebug("ENTER");
569
570         if (__pFolderBackgroundBitmap == null)
571         {
572                 __pFolderBackgroundBitmap = ResourceManager::GetBitmapN(IDB_MAIN_FOLDER_BACKGROUND);
573         }
574
575         if (__pAlbumInfoList != null)
576         {
577                 AlbumInfo* pInfo = null;
578                 for (int i = 0; i < __pAlbumInfoList->GetCount(); ++i)
579                 {
580                         pInfo = static_cast<AlbumInfo*>(__pAlbumInfoList->GetAt(i));
581                         if (pInfo->GetAlbumName().IsEmpty() == false)
582                         {
583                                 AppLog("Print AlbumName(%d)(%ls)", i, pInfo->GetAlbumName().GetPointer());
584                         }
585                 }
586                 delete __pAlbumInfoList;
587         }
588         __pAlbumInfoList = new (std::nothrow) ArrayList(SingleObjectDeleter);
589
590         ContentDirectory directory;
591
592         if (contentType == CONTENT_TYPE_ALL)
593         {
594                 IListT<ContentType>* pListT = new (std::nothrow) ArrayListT<ContentType>();
595                 TryReturnVoid(pListT != null, "Unable to Create ArrayListT");
596                 pListT->Add(CONTENT_TYPE_IMAGE);
597                 pListT->Add(CONTENT_TYPE_VIDEO);
598
599                 result r = directory.Construct(*pListT);
600                 delete pListT;
601
602                 if (r != E_SUCCESS)
603                 {
604                         delete __pAlbumInfoList;
605                         __pAlbumInfoList = null;
606                         AppLogDebug("EXIT 1(%s)", GetErrorMessage(r));
607                         return;
608                 }
609         }
610         else
611         {
612                 result r = directory.Construct(contentType);
613                 if (r != E_SUCCESS)
614                 {
615                         delete __pAlbumInfoList;
616                         __pAlbumInfoList = null;
617                         AppLogDebug("EXIT 2(%s)", GetErrorMessage(r));
618                         return;
619                 }
620         }
621
622         IList* pContentDirectoryPathList = directory.GetContentDirectoryPathListN(SORT_ORDER_ASCENDING);
623         String* pDirPath = null;
624         AlbumInfo* pAlbumInfo = null;
625
626         AlbumInfo* pAllAlbumInfo = new (std::nothrow) AlbumInfo();
627         AlbumInfo* pCameraAlbumInfo = new (std::nothrow) AlbumInfo();
628         AlbumInfo* pDownloadAlbumInfo = new (std::nothrow) AlbumInfo();
629         AlbumInfo* pNoNameAlbumInfo = new (std::nothrow) AlbumInfo();
630
631         result r = pAllAlbumInfo->Construct();
632         TryCatch(IsFailed(r) == E_SUCCESS,,"new pNoNameAlbumInfo is %s",GetErrorMessage(r));
633         pAllAlbumInfo->SetAlbumName(ResourceManager::GetString(L"IDS_MEDIABR_BODY_ALL_ALBUMS"));
634
635         r = pCameraAlbumInfo->Construct();
636         TryCatch(IsFailed(r) == E_SUCCESS,,"new pNoNameAlbumInfo is %s",GetErrorMessage(r));
637         pCameraAlbumInfo->SetAlbumName(ResourceManager::GetString(L"IDS_MEDIABR_BODY_CAMERA_ROLL_ABB"));
638
639         r = pDownloadAlbumInfo->Construct();
640         TryCatch(IsFailed(r) == E_SUCCESS,,"new pNoNameAlbumInfo is %s",GetErrorMessage(r));
641         pDownloadAlbumInfo->SetAlbumName(ResourceManager::GetString(L"IDS_COM_BODY_DOWNLOADS"));
642
643         r = pNoNameAlbumInfo->Construct();
644         TryCatch(IsFailed(r) == E_SUCCESS,,"new pNoNameAlbumInfo is %s",GetErrorMessage(r));
645         pNoNameAlbumInfo->SetAlbumName(ResourceManager::GetString(L"IDS_MEDIABR_POP_NO_NAME"));
646
647         if ((pContentDirectoryPathList != null) && (pContentDirectoryPathList->GetCount() > 0))
648         {
649                 IEnumerator* pEnum = pContentDirectoryPathList->GetEnumeratorN();
650                 while (pEnum->MoveNext() == E_SUCCESS)
651                 {
652                         pDirPath = static_cast<String*>(pEnum->GetCurrent());
653                         String mediaPath = RESERVED_MEDIA_PATH;
654                         int length = mediaPath.GetLength();
655                         mediaPath.Remove(length-1,1);
656
657                         if (pDirPath == null)
658                         {
659                                 continue;
660                         }
661
662                         if ((*pDirPath == RESERVED_CAMERA_PATH) ||(*pDirPath == RESERVED_CAMERA_PATH_EXT))
663                         {
664                                 IList* pContentIdList = GetContentIdListInDirectoryN(*pDirPath, contentType);
665
666                                 if (pContentIdList != null)
667                                 {
668                                         pCameraAlbumInfo->AppendDirectory(*pDirPath);
669                                         pCameraAlbumInfo->AppendContentIdList(*pContentIdList);
670
671                                         pAllAlbumInfo->AppendDirectory(*pDirPath);
672                                         pAllAlbumInfo->AppendContentIdList(*pContentIdList);
673                                         delete pContentIdList;
674                                 }
675                         }
676                         else if (*pDirPath == RESERVED_DOWNLOAD_PATH)
677                         {
678                                 IList* pContentIdList = GetContentIdListInDirectoryN(*pDirPath, contentType);
679
680                                 if (pContentIdList != null)
681                                 {
682                                         pDownloadAlbumInfo->AppendDirectory(*pDirPath);
683                                         pDownloadAlbumInfo->AppendContentIdList(*pContentIdList);
684
685                                         pAllAlbumInfo->AppendDirectory(*pDirPath);
686                                         pAllAlbumInfo->AppendContentIdList(*pContentIdList);
687
688                                         pContentIdList->RemoveAll(true);
689                                         delete pContentIdList;
690                                 }
691                         }
692                         else if (*pDirPath == mediaPath)
693                         {
694                                 IList* pContentIdList = GetContentIdListInDirectoryN(*pDirPath, contentType);
695
696                                 if (pContentIdList != null)
697                                 {
698                                         pNoNameAlbumInfo->AppendDirectory(*pDirPath);
699                                         pNoNameAlbumInfo->AppendContentIdList(*pContentIdList);
700
701                                         pAllAlbumInfo->AppendDirectory(*pDirPath);
702                                         pAllAlbumInfo->AppendContentIdList(*pContentIdList);
703
704                                         pContentIdList->RemoveAll(true);
705                                         delete pContentIdList;
706                                 }
707                         }
708                         else
709                         {
710                                 IList* pContentIdList = GetContentIdListInDirectoryN(*pDirPath, contentType);
711
712                                 if (pContentIdList != null)
713                                 {
714                                         if (pContentIdList->GetCount() > 0)
715                                         {
716                                                 pAlbumInfo = new (std::nothrow) AlbumInfo();
717                                                 IList* pSubDirList = new (std::nothrow) ArrayList(SingleObjectDeleter);
718
719                                                 pSubDirList->Add(new (std::nothrow) String(*pDirPath));
720
721                                                 pAlbumInfo->Construct(GetDirectoryNameFromFullPath(*pDirPath), contentType, *pSubDirList,
722                                                                 *pContentIdList);
723
724                                                 pAllAlbumInfo->AppendDirectory(*pDirPath);
725                                                 pAllAlbumInfo->AppendContentIdList(*pContentIdList);
726
727                                                 pSubDirList->RemoveAll(true);
728                                                 delete pSubDirList;
729                                         }
730
731                                         if (pContentIdList != NULL)
732                                         {
733                                                 pContentIdList->RemoveAll(true);
734                                                 delete pContentIdList;
735                                         }
736                                 }
737                                 if (pAlbumInfo != null && pAlbumInfo->GetContentIdCount() > 0)
738                                 {
739                                         __pAlbumInfoList->Add(pAlbumInfo);
740                                         AppLog("Adding album");
741                                 }
742                                 else
743                                 {
744                                         delete pAlbumInfo;
745                                 }
746                                 pAlbumInfo = null;
747                         }
748                 }
749
750                 int insertIndex = 0;
751                 if (pAllAlbumInfo->GetContentIdCount() > 0)
752                 {
753                         __pAlbumInfoList->InsertAt((Object&)*pAllAlbumInfo, insertIndex++);
754                 }
755                 else
756                 {
757                         delete pAllAlbumInfo;
758                 }
759
760                 if (pCameraAlbumInfo->GetContentIdCount() > 0)
761                 {
762                         __pAlbumInfoList->InsertAt((Object&)*pCameraAlbumInfo, insertIndex++);
763                 }
764                 else
765                 {
766                         delete pCameraAlbumInfo;
767                 }
768
769                 if (pDownloadAlbumInfo->GetContentIdCount() > 0)
770                 {
771                         __pAlbumInfoList->InsertAt((Object&)*pDownloadAlbumInfo, insertIndex++);
772                 }
773                 else
774                 {
775                         delete pDownloadAlbumInfo;
776                 }
777
778                 if (pNoNameAlbumInfo->GetContentIdCount() > 0)
779                 {
780                         __pAlbumInfoList->InsertAt((Object&)*pNoNameAlbumInfo, insertIndex++);
781                 }
782                 else
783                 {
784                         delete pNoNameAlbumInfo;
785                 }
786
787                 delete pEnum;
788         }
789         else
790         {
791                 delete pAllAlbumInfo;
792                 delete pCameraAlbumInfo;
793                 delete pDownloadAlbumInfo;
794                 delete pNoNameAlbumInfo;
795         }
796
797         if (pContentDirectoryPathList != null)
798         {
799                 pContentDirectoryPathList->RemoveAll(true);
800                 delete pContentDirectoryPathList;
801         }
802         ClearThumbnailRequests(false);
803         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
804         return;
805
806 CATCH:
807 if (pContentDirectoryPathList != null)
808 {
809         pContentDirectoryPathList->RemoveAll(true);
810         delete pContentDirectoryPathList;
811 }
812         delete pAllAlbumInfo;
813         delete pCameraAlbumInfo;
814         delete pDownloadAlbumInfo;
815         delete pNoNameAlbumInfo;
816 }
817
818 String
819 AlbumListPresentationModel::GetDirectoryNameFromFullPath(const String& fullPath)const
820 {
821         AppLogDebug("ENTER");
822         if (fullPath == null)
823         {
824                 AppLogDebug("EXIT 1(%s)", GetErrorMessage(GetLastResult()));
825
826                 return null;
827         }
828         String delim(DIRECTORY_SEPARATOR);
829         StringTokenizer st(fullPath,delim);
830         String token;
831         while (st.HasMoreTokens())
832         {
833                 st.GetNextToken(token);
834         }
835         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
836
837         return token;
838 }
839
840 IList*
841 AlbumListPresentationModel::GetContentIdListInDirectoryN(const String& directoryPath, ContentType contentType)const
842 {
843         AppLogDebug("ENTER");
844
845         if (&directoryPath == null)
846         {
847                 AppLogDebug("EXIT 1(%s)", GetErrorMessage(GetLastResult()));
848                 return null;
849         }
850
851         ContentDirectory directory;
852         const String contentDirectoryPath = directoryPath;
853         if (contentType == CONTENT_TYPE_ALL)
854         {
855                 IListT<ContentType>* pListT = new (std::nothrow) ArrayListT<ContentType>();
856                 pListT->Add(CONTENT_TYPE_IMAGE);
857                 pListT->Add(CONTENT_TYPE_VIDEO);
858                 result r = directory.Construct(*pListT);
859                 delete pListT;
860                 if (r != E_SUCCESS)
861                 {
862                         AppLogDebug("EXIT 2(%s)", GetErrorMessage(GetLastResult()));
863                         return null;
864                 }
865         }
866         else
867         {
868                 result r = directory.Construct(contentType);
869                 if (r != E_SUCCESS)
870                 {
871                         AppLogDebug("EXIT 3(%s)", GetErrorMessage(GetLastResult()));
872                         return null;
873                 }
874         }
875
876         int pageNo = 1;
877         int countPerPage = directory.GetContentDirectoryItemCount(contentDirectoryPath);
878         IList* pContentInfoList = directory.GetContentDirectoryItemListN(contentDirectoryPath, pageNo, countPerPage,
879                         CONTENT_INFO_ORDER, SORT_ORDER_ASCENDING);
880         IList* pContentIdList = new (std::nothrow) ArrayList(SingleObjectDeleter);
881
882         if ((pContentInfoList != null) && (pContentInfoList->GetCount() > 0))
883         {
884                 IEnumerator* pEnum = pContentInfoList->GetEnumeratorN();
885                 ContentId* pItem;
886                 while (pEnum->MoveNext() == E_SUCCESS)
887                 {
888                         ContentInfo* pContentInfo = static_cast<ContentInfo*>(pEnum->GetCurrent());
889                         pItem = new (std::nothrow) ContentId(pContentInfo->GetContentId());
890                         pContentIdList->Add(pItem);
891                 }
892                 delete pEnum;
893         }
894
895         if (pContentInfoList != NULL)
896         {
897                 pContentInfoList->RemoveAll(true);
898                 delete pContentInfoList;
899         }
900
901         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
902
903         return pContentIdList;
904 }
905
906 result
907 AlbumListPresentationModel::DeleteContentFile(const ContentId& contentId)
908 {
909         AppLogDebug("ENTER");
910         result r = E_SUCCESS;
911         ContentManager contentManager;
912         r = contentManager.Construct();
913         if (r == E_SUCCESS)
914         {
915                 r = contentManager.DeleteContent(contentId);
916         }
917         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
918
919         return r;
920 }
921
922 AppControlMode
923 AlbumListPresentationModel::GetAppControlMode(void) const
924 {
925         AppLogDebug("Enter");
926         AppLogDebug("Exit");
927         return __appControlMode;
928 }
929
930 AppControlMediaType
931 AlbumListPresentationModel::GetAppControlMediaType(void) const
932 {
933         AppLogDebug("Enter");
934         AppLogDebug("Exit");
935         return __appControlMediaType;
936 }
937
938 AppControlSelectionMode
939 AlbumListPresentationModel::GetAppControlSelectionMode(void) const
940 {
941         AppLogDebug("Enter");
942         AppLogDebug("Exit");
943         return __appControlSelectionMode;
944 }
945
946 IList*
947 AlbumListPresentationModel::GetAlbumInfoList(void) const
948 {
949         AppLogDebug("ENTER");
950         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
951
952         return __pAlbumInfoList;
953 }
954
955 IList* AlbumListPresentationModel::GetContentIdListAtIndexN(const IList& indexList) const
956 {
957         AppLogDebug("ENTER");
958         ContentDirectory directory;
959         IList* pContentIdList = new (std::nothrow) ArrayList(SingleObjectDeleter);
960
961         IListT<ContentType>* pListT = new (std::nothrow) ArrayListT<ContentType>();
962         TryReturn(pListT != null,null, "Unable to Create ArrayListT");
963         pListT->Add(CONTENT_TYPE_IMAGE);
964         pListT->Add(CONTENT_TYPE_VIDEO);
965
966         result r = directory.Construct(*pListT);
967         delete pListT;
968
969         if (r != E_SUCCESS)
970         {
971                 AppLogDebug("EXIT 3(%s)", GetErrorMessage(GetLastResult()));
972                 delete pContentIdList;
973                 return null;
974         }
975
976         if (static_cast<const Integer*>(indexList.GetAt(0))->ToInt() == 0) //if all-album is selected, get all files for delete
977         {
978                 IList* pContentDirectoryPathList = directory.GetContentDirectoryPathListN(SORT_ORDER_ASCENDING);
979                 String* pDirPath = null;
980                 if ((pContentDirectoryPathList != null) && (pContentDirectoryPathList->GetCount() > 0))
981                 {
982                         int loopCount = pContentDirectoryPathList->GetCount();
983                         for (int i = 0; i < loopCount; ++i)
984                         {
985                                 pDirPath = static_cast<String*>(pContentDirectoryPathList->GetAt(i));
986                                 pContentIdList->AddItems(*(GetContentIdListInDirectoryN(*pDirPath, CONTENT_TYPE_ALL)));
987                         }
988                 }
989         }
990         else
991         {
992                 if(__pAlbumInfoList != null)
993                 {
994                         int loopCount = indexList.GetCount();
995                         AlbumInfo* pInfo = null;
996                         for (int i = 0; i < loopCount; ++i)
997                         {
998                                 int index  = static_cast<const Integer*>(indexList.GetAt(i))->ToInt();
999                                 pInfo = static_cast<AlbumInfo*>(__pAlbumInfoList->GetAt(index));
1000
1001                                 if(pInfo != null)
1002                                 {
1003                                         IList* pList = pInfo->GetContentIdListN();
1004                                         if(pList != null)
1005                                         {
1006                                                 pContentIdList->AddItems(*pList);
1007                                         }
1008                                 }
1009                         }
1010                 }
1011         }
1012         return pContentIdList;
1013 }
1014
1015 void
1016 AlbumListPresentationModel::OnThumbnailReceivedN(IEventArg& eventArg)
1017 {
1018         AppLogDebug("ENTER");
1019         if (&eventArg != null)
1020         {
1021                 ThumbnailEventArg* pThumbnailEventArg = static_cast<ThumbnailEventArg*>(&eventArg);
1022                 if (pThumbnailEventArg == null)
1023                 {
1024                         AppLogDebug("EXIT 1(%s)", GetErrorMessage(GetLastResult()));
1025
1026                         return;
1027                 }               
1028                 ThumbnailInfo* pThumbnailInfo = pThumbnailEventArg->GetThumbnailInfoN();
1029                 if (pThumbnailInfo == null)
1030                 {
1031                         AppLogDebug("EXIT 2(%s)", GetErrorMessage(GetLastResult()));
1032
1033                         return;
1034                 }
1035
1036                 ContentId contentId = pThumbnailInfo->GetContentId();
1037                 ArrayList* pUpdateFolderIndex = new (std::nothrow) ArrayList(SingleObjectDeleter);
1038
1039                 if (__pAlbumInfoList != null)
1040                 {
1041                         int outterLoopCount = __pAlbumInfoList->GetCount();
1042                         for (int x = 0; x < outterLoopCount; ++x)
1043                         {
1044                                 AlbumInfo* pAlbumInfo = static_cast<AlbumInfo*>(__pAlbumInfoList->GetAt(x));
1045                                 int innerLoopCount = pAlbumInfo->GetContentIdCount();
1046                                 for (int i = 0; i < innerLoopCount; ++i)
1047                                 {
1048                                         if (i >= FOLDER_BITMAP_MAX)
1049                                         {
1050                                                 break;
1051                                         }
1052
1053                                         if (contentId == pAlbumInfo->GetContentId(i))
1054                                         {
1055                                                 if (pAlbumInfo->GetFolderThumnailBitmap() == null)
1056                                                 {
1057                                                         pAlbumInfo->SetFolderThumnailBitmap(pThumbnailInfo->GetBitmapN());
1058                                                         pUpdateFolderIndex->Add(new (std::nothrow) Integer(x));
1059                                                 }
1060                                         }
1061                                 }
1062                         }
1063                 }
1064
1065                 delete pThumbnailInfo;
1066
1067                 if (__pPresentationModelListener->GetCount() > 0)
1068                 {
1069                         IEnumerator* pEnum = __pPresentationModelListener->GetEnumeratorN();
1070                         while (pEnum->MoveNext() == E_SUCCESS)
1071                         {
1072                                 IAlbumEventListener* pListener = null;
1073                                 pListener = static_cast<IAlbumEventListener*>(pEnum->GetCurrent());
1074
1075                                 int loopCount = pUpdateFolderIndex->GetCount();
1076                                 for (int k = 0; k < loopCount; ++k)
1077                                 {
1078                                         Integer *pIndex = (Integer*)pUpdateFolderIndex->GetAt(k);
1079                                         int folderIndex = pIndex->ToInt();
1080                                         pListener->OnThumbnailDecoded(folderIndex);
1081                                 }
1082                         }
1083                         delete pEnum;
1084                 }
1085
1086                 delete pUpdateFolderIndex;
1087         }
1088         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
1089 }
1090
1091 void
1092 AlbumListPresentationModel::OnContentCreated(void)
1093 {
1094         AppLogDebug("ENTER");
1095         if (__updateProgressStatus == true)
1096         {
1097                 AppLogDebug("EXIT update in application");
1098                 return;
1099         }
1100         IFormContentUpdateEventListener* pInterface = null;
1101         IEnumerator* pEventEnum = __pContentEventListener->GetEnumeratorN();
1102         while (pEventEnum->MoveNext() == E_SUCCESS)
1103         {
1104                 pInterface = static_cast<IFormContentUpdateEventListener*>(pEventEnum->GetCurrent());
1105                 if (pInterface != null)
1106                 {
1107                         pInterface->OnContentUpdated();
1108                 }
1109         }
1110         delete pEventEnum;
1111         AppLogDebug("EXIT");
1112 }
1113
1114 void
1115 AlbumListPresentationModel::OnContentUpdated(void)
1116 {
1117         AppLogDebug("ENTER");
1118         if (__updateProgressStatus == true)
1119         {
1120                 AppLogDebug("EXIT update in application");
1121                 return;
1122         }
1123         IFormContentUpdateEventListener* pInterface = null;
1124         IEnumerator* pEventEnum = __pContentEventListener->GetEnumeratorN();
1125         while (pEventEnum->MoveNext() == E_SUCCESS)
1126         {
1127                 pInterface = static_cast<IFormContentUpdateEventListener*>(pEventEnum->GetCurrent());
1128                 pInterface->OnContentUpdated();
1129         }
1130         delete pEventEnum;
1131         AppLogDebug("EXIT");
1132 }
1133
1134 void
1135 AlbumListPresentationModel::OnContentDeleted(void)
1136 {
1137         AppLogDebug("ENTER");
1138         if (__updateProgressStatus == true)
1139         {
1140                 AppLogDebug("EXIT update in application");
1141                 return;
1142         }
1143         IFormContentUpdateEventListener* pInterface = null;
1144         IEnumerator* pEventEnum = __pContentEventListener->GetEnumeratorN();
1145         while (pEventEnum->MoveNext() == E_SUCCESS)
1146         {
1147                 pInterface = static_cast<IFormContentUpdateEventListener*>(pEventEnum->GetCurrent());
1148                 pInterface->OnContentUpdated();
1149         }
1150         delete pEventEnum;
1151         AppLogDebug("EXIT");
1152 }
1153
1154 void
1155 AlbumListPresentationModel::OnDeviceStateChanged(DeviceType deviceType, const Tizen::Base::String& state)
1156 {
1157         AppLogDebug("ENTER");
1158         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
1159 }
1160
1161 void
1162 AlbumListPresentationModel::SetUpdateProgressStatus(const bool status)
1163 {
1164         AppLogDebug("ENTER status = %d",status);
1165         __updateProgressStatus = status;
1166         AppLogDebug("EXIT");
1167 }
1168
1169 result
1170 AlbumListPresentationModel::RotateImage(int index, RotateMode rotateMode)
1171 {
1172         return E_SUCCESS;
1173 }
1174
1175 void
1176 AlbumListPresentationModel::RequestThumbnail(const int folderIndex, const int pathIndex)
1177 {
1178         AppLogDebug("ENTER : folderIndex(%d), pathIndex(%d)", folderIndex, pathIndex);
1179
1180         ContentId contentId;
1181         if (__pAlbumInfoList != null)
1182         {
1183                 AlbumInfo* pAlbumInfo = static_cast<AlbumInfo*>(__pAlbumInfoList->GetAt(folderIndex));
1184                 contentId = pAlbumInfo->GetContentId(pathIndex);
1185         }
1186
1187         __pThumbnailProvider->RequestThumbnail(contentId, __pThumbnailEvent);
1188         AppLogDebug("EXIT");
1189 }
1190
1191 void
1192 AlbumListPresentationModel::ClearThumbnailRequests(const bool appTerminating)
1193 {
1194         AppLogDebug("ENTER");
1195         if (__pThumbnailProvider != null)
1196         {
1197                 __pThumbnailProvider->ClearThumbnailRequests(appTerminating);
1198         }
1199         AppLogDebug("EXIT");
1200 }