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