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