Fixed prevent issues
[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                                         delete pContentIdList;
678                                 }
679                         }
680                         else if (*pDirPath == mediaPath)
681                         {
682                                 IList* pContentIdList = GetContentIdListInDirectoryN(*pDirPath, contentType);
683
684                                 if (pContentIdList != null)
685                                 {
686                                         pNoNameAlbumInfo->AppendDirectory(*pDirPath);
687                                         pNoNameAlbumInfo->AppendContentIdList(*pContentIdList);
688
689                                         pAllAlbumInfo->AppendDirectory(*pDirPath);
690                                         pAllAlbumInfo->AppendContentIdList(*pContentIdList);
691                                         delete pContentIdList;
692                                 }
693                         }
694                         else
695                         {
696                                 IList* pContentIdList = GetContentIdListInDirectoryN(*pDirPath, contentType);
697
698                                 if (pContentIdList != null)
699                                 {
700                                         if (pContentIdList->GetCount() > 0)
701                                         {
702                                                 pAlbumInfo = new (std::nothrow) AlbumInfo();
703                                                 IList* pSubDirList = new (std::nothrow) ArrayList(SingleObjectDeleter);
704
705                                                 pSubDirList->Add(new (std::nothrow) String(*pDirPath));
706
707                                                 pAlbumInfo->Construct(GetDirectoryNameFromFullPath(*pDirPath), contentType, *pSubDirList,
708                                                                 *pContentIdList);
709
710                                                 pAllAlbumInfo->AppendDirectory(*pDirPath);
711                                                 pAllAlbumInfo->AppendContentIdList(*pContentIdList);
712                                                 delete pSubDirList;
713                                         }
714                                         delete pContentIdList;
715                                 }
716                                 if (pAlbumInfo != null && pAlbumInfo->GetContentIdCount() > 0)
717                                 {
718                                         __pAlbumInfoList->Add(pAlbumInfo);
719                                 }
720                                 else
721                                 {
722                                         delete pAlbumInfo;
723                                 }
724                                 pAlbumInfo = null;
725                         }
726                 }
727
728                 int insertIndex = 0;
729                 if (pAllAlbumInfo->GetContentIdCount() > 0)
730                 {
731                         __pAlbumInfoList->InsertAt((Object&)*pAllAlbumInfo, insertIndex++);
732                 }
733                 else
734                 {
735                         delete pAllAlbumInfo;
736                 }
737
738                 if (pCameraAlbumInfo->GetContentIdCount() > 0)
739                 {
740                         __pAlbumInfoList->InsertAt((Object&)*pCameraAlbumInfo, insertIndex++);
741                 }
742                 else
743                 {
744                         delete pCameraAlbumInfo;
745                 }
746
747                 if (pDownloadAlbumInfo->GetContentIdCount() > 0)
748                 {
749                         __pAlbumInfoList->InsertAt((Object&)*pDownloadAlbumInfo, insertIndex++);
750                 }
751                 else
752                 {
753                         delete pDownloadAlbumInfo;
754                 }
755
756                 if (pNoNameAlbumInfo->GetContentIdCount() > 0)
757                 {
758                         __pAlbumInfoList->InsertAt((Object&)*pNoNameAlbumInfo, insertIndex++);
759                 }
760                 else
761                 {
762                         delete pNoNameAlbumInfo;
763                 }
764
765                 delete pEnum;
766         }
767         else
768         {
769                 delete pAllAlbumInfo;
770                 delete pCameraAlbumInfo;
771                 delete pDownloadAlbumInfo;
772                 delete pNoNameAlbumInfo;
773         }
774
775         if (pContentDirectoryPathList != null)
776         {
777                 delete pContentDirectoryPathList;
778         }
779         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
780         return;
781
782 CATCH:
783         delete pContentDirectoryPathList;
784         delete pAllAlbumInfo;
785         delete pCameraAlbumInfo;
786         delete pDownloadAlbumInfo;
787         delete pNoNameAlbumInfo;
788 }
789
790 String
791 AlbumListPresentationModel::GetDirectoryNameFromFullPath(const String& fullPath)const
792 {
793         AppLogDebug("ENTER");
794         if (fullPath == null)
795         {
796                 AppLogDebug("EXIT 1(%s)", GetErrorMessage(GetLastResult()));
797
798                 return null;
799         }
800         String delim(DIRECTORY_SEPARATOR);
801         StringTokenizer st(fullPath,delim);
802         String token;
803         while (st.HasMoreTokens())
804         {
805                 st.GetNextToken(token);
806         }
807         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
808
809         return token;
810 }
811
812 IList*
813 AlbumListPresentationModel::GetContentIdListInDirectoryN(const String& directoryPath, ContentType contentType)const
814 {
815         AppLogDebug("ENTER");
816
817         if (&directoryPath == null)
818         {
819                 AppLogDebug("EXIT 1(%s)", GetErrorMessage(GetLastResult()));
820                 return null;
821         }
822
823         ContentDirectory directory;
824         const String contentDirectoryPath = directoryPath;
825         if (contentType == CONTENT_TYPE_ALL)
826         {
827                 IListT<ContentType>* pListT = new (std::nothrow) ArrayListT<ContentType>();
828                 pListT->Add(CONTENT_TYPE_IMAGE);
829                 pListT->Add(CONTENT_TYPE_VIDEO);
830                 result r = directory.Construct(*pListT);
831                 delete pListT;
832                 if (r != E_SUCCESS)
833                 {
834                         AppLogDebug("EXIT 2(%s)", GetErrorMessage(GetLastResult()));
835                         return null;
836                 }
837         }
838         else
839         {
840                 result r = directory.Construct(contentType);
841                 if (r != E_SUCCESS)
842                 {
843                         AppLogDebug("EXIT 3(%s)", GetErrorMessage(GetLastResult()));
844                         return null;
845                 }
846         }
847
848         int pageNo = 1;
849         int countPerPage = directory.GetContentDirectoryItemCount(contentDirectoryPath);
850         IList* pContentInfoList = directory.GetContentDirectoryItemListN(contentDirectoryPath, pageNo, countPerPage,
851                         CONTENT_INFO_ORDER, SORT_ORDER_ASCENDING);
852         IList* pContentIdList = new (std::nothrow) ArrayList(SingleObjectDeleter);
853
854         if ((pContentInfoList != null) && (pContentInfoList->GetCount() > 0))
855         {
856                 IEnumerator* pEnum = pContentInfoList->GetEnumeratorN();
857                 ContentId* pItem;
858                 while (pEnum->MoveNext() == E_SUCCESS)
859                 {
860                         ContentInfo* pContentInfo = static_cast<ContentInfo*>(pEnum->GetCurrent());
861                         pItem = new (std::nothrow) ContentId(pContentInfo->GetContentId());
862                         pContentIdList->Add(pItem);
863                 }
864                 delete pEnum;
865         }
866         delete pContentInfoList;
867         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
868
869         return pContentIdList;
870 }
871
872 result
873 AlbumListPresentationModel::DeleteContentFile(const ContentId& contentId)
874 {
875         AppLogDebug("ENTER");
876         result r = E_SUCCESS;
877         ContentManager contentManager;
878         r = contentManager.Construct();
879         if (r == E_SUCCESS)
880         {
881                 r = contentManager.DeleteContent(contentId);
882         }
883         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
884
885         return r;
886 }
887
888 AppControlMode
889 AlbumListPresentationModel::GetAppControlMode(void) const
890 {
891         AppLogDebug("Enter");
892         AppLogDebug("Exit");
893         return __appControlMode;
894 }
895
896 AppControlMediaType
897 AlbumListPresentationModel::GetAppControlMediaType(void) const
898 {
899         AppLogDebug("Enter");
900         AppLogDebug("Exit");
901         return __appControlMediaType;
902 }
903
904 AppControlSelectionMode
905 AlbumListPresentationModel::GetAppControlSelectionMode(void) const
906 {
907         AppLogDebug("Enter");
908         AppLogDebug("Exit");
909         return __appControlSelectionMode;
910 }
911
912 IList*
913 AlbumListPresentationModel::GetAlbumInfoList(void) const
914 {
915         AppLogDebug("ENTER");
916         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
917
918         return __pAlbumInfoList;
919 }
920
921 IList* AlbumListPresentationModel::GetContentIdListAtIndexN(const IList& indexList) const
922 {
923         AppLogDebug("ENTER");
924         ContentDirectory directory;
925         IList* pContentIdList = new (std::nothrow) ArrayList(SingleObjectDeleter);
926
927         IListT<ContentType>* pListT = new (std::nothrow) ArrayListT<ContentType>();
928         TryReturn(pListT != null,null, "Unable to Create ArrayListT");
929         pListT->Add(CONTENT_TYPE_IMAGE);
930         pListT->Add(CONTENT_TYPE_VIDEO);
931
932         result r = directory.Construct(*pListT);
933         delete pListT;
934
935         if (r != E_SUCCESS)
936         {
937                 AppLogDebug("EXIT 3(%s)", GetErrorMessage(GetLastResult()));
938                 delete pContentIdList;
939                 return null;
940         }
941
942         if (static_cast<const Integer*>(indexList.GetAt(0))->ToInt() == 0) //if all-album is selected, get all files for delete
943         {
944                 IList* pContentDirectoryPathList = directory.GetContentDirectoryPathListN(SORT_ORDER_ASCENDING);
945                 String* pDirPath = null;
946                 if ((pContentDirectoryPathList != null) && (pContentDirectoryPathList->GetCount() > 0))
947                 {
948                         int loopCount = pContentDirectoryPathList->GetCount();
949                         for (int i = 0; i < loopCount; ++i)
950                         {
951                                 pDirPath = static_cast<String*>(pContentDirectoryPathList->GetAt(i));
952                                 pContentIdList->AddItems(*(GetContentIdListInDirectoryN(*pDirPath, CONTENT_TYPE_ALL)));
953                         }
954                 }
955         }
956         else
957         {
958                 if(__pAlbumInfoList != null)
959                 {
960                         int loopCount = indexList.GetCount();
961                         AlbumInfo* pInfo = null;
962                         for (int i = 0; i < loopCount; ++i)
963                         {
964                                 int index  = static_cast<const Integer*>(indexList.GetAt(i))->ToInt();
965                                 pInfo = static_cast<AlbumInfo*>(__pAlbumInfoList->GetAt(index));
966
967                                 if(pInfo != null)
968                                 {
969                                         IList* pList = pInfo->GetContentIdListN();
970                                         if(pList != null)
971                                         {
972                                                 pContentIdList->AddItems(*pList);
973                                         }
974                                 }
975                         }
976                 }
977         }
978         return pContentIdList;
979 }
980
981 void
982 AlbumListPresentationModel::OnThumbnailReceivedN(IEventArg& eventArg)
983 {
984         AppLogDebug("ENTER");
985         if (&eventArg != null)
986         {
987                 ThumbnailEventArg* pThumbnailEventArg = static_cast<ThumbnailEventArg*>(&eventArg);
988                 if (pThumbnailEventArg == null)
989                 {
990                         AppLogDebug("EXIT 1(%s)", GetErrorMessage(GetLastResult()));
991
992                         return;
993                 }               
994                 ThumbnailInfo* pThumbnailInfo = pThumbnailEventArg->GetThumbnailInfoN();
995                 if (pThumbnailInfo == null)
996                 {
997                         AppLogDebug("EXIT 2(%s)", GetErrorMessage(GetLastResult()));
998
999                         return;
1000                 }
1001
1002                 ContentId contentId = pThumbnailInfo->GetContentId();
1003                 ArrayList* pUpdateFolderIndex = new (std::nothrow) ArrayList(SingleObjectDeleter);
1004
1005                 if (__pAlbumInfoList != null)
1006                 {
1007                         int outterLoopCount = __pAlbumInfoList->GetCount();
1008                         for (int x = 0; x < outterLoopCount; ++x)
1009                         {
1010                                 AlbumInfo* pAlbumInfo = static_cast<AlbumInfo*>(__pAlbumInfoList->GetAt(x));
1011                                 int innerLoopCount = pAlbumInfo->GetContentIdCount();
1012                                 for (int i = 0; i < innerLoopCount; ++i)
1013                                 {
1014                                         if (i >= FOLDER_BITMAP_MAX)
1015                                         {
1016                                                 break;
1017                                         }
1018
1019                                         if (contentId == pAlbumInfo->GetContentId(i))
1020                                         {
1021                                                 if (pAlbumInfo->GetFolderThumnailBitmap() == null)
1022                                                 {
1023                                                         pAlbumInfo->SetFolderThumnailBitmap(pThumbnailInfo->GetBitmapN());
1024                                                         pUpdateFolderIndex->Add(new (std::nothrow) Integer(x));
1025                                                 }
1026                                         }
1027                                 }
1028                         }
1029                 }
1030
1031                 if (__pPresentationModelListener->GetCount() > 0)
1032                 {
1033                         IEnumerator* pEnum = __pPresentationModelListener->GetEnumeratorN();
1034                         while (pEnum->MoveNext() == E_SUCCESS)
1035                         {
1036                                 IAlbumEventListener* pListener = null;
1037                                 pListener = static_cast<IAlbumEventListener*>(pEnum->GetCurrent());
1038
1039                                 int loopCount = pUpdateFolderIndex->GetCount();
1040                                 for (int k = 0; k < loopCount; ++k)
1041                                 {
1042                                         Integer *pIndex = (Integer*)pUpdateFolderIndex->GetAt(k);
1043                                         int folderIndex = pIndex->ToInt();
1044                                         pListener->OnThumbnailDecoded(folderIndex);
1045                                 }
1046                         }
1047                         delete pEnum;
1048                 }
1049
1050                 delete pUpdateFolderIndex;
1051         }
1052         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
1053 }
1054
1055 void
1056 AlbumListPresentationModel::OnContentCreated(void)
1057 {
1058         AppLogDebug("ENTER");
1059         if (__updateProgressStatus == true)
1060         {
1061                 AppLogDebug("EXIT update in application");
1062                 return;
1063         }
1064         IFormContentUpdateEventListener* pInterface = null;
1065         IEnumerator* pEventEnum = __pContentEventListener->GetEnumeratorN();
1066         while (pEventEnum->MoveNext() == E_SUCCESS)
1067         {
1068                 pInterface = static_cast<IFormContentUpdateEventListener*>(pEventEnum->GetCurrent());
1069                 if (pInterface != null)
1070                 {
1071                         pInterface->OnContentUpdated();
1072                 }
1073         }
1074         delete pEventEnum;
1075         AppLogDebug("EXIT");
1076 }
1077
1078 void
1079 AlbumListPresentationModel::OnContentUpdated(void)
1080 {
1081         AppLogDebug("ENTER");
1082         if (__updateProgressStatus == true)
1083         {
1084                 AppLogDebug("EXIT update in application");
1085                 return;
1086         }
1087         IFormContentUpdateEventListener* pInterface = null;
1088         IEnumerator* pEventEnum = __pContentEventListener->GetEnumeratorN();
1089         while (pEventEnum->MoveNext() == E_SUCCESS)
1090         {
1091                 pInterface = static_cast<IFormContentUpdateEventListener*>(pEventEnum->GetCurrent());
1092                 pInterface->OnContentUpdated();
1093         }
1094         delete pEventEnum;
1095         AppLogDebug("EXIT");
1096 }
1097
1098 void
1099 AlbumListPresentationModel::OnContentDeleted(void)
1100 {
1101         AppLogDebug("ENTER");
1102         if (__updateProgressStatus == true)
1103         {
1104                 AppLogDebug("EXIT update in application");
1105                 return;
1106         }
1107         IFormContentUpdateEventListener* pInterface = null;
1108         IEnumerator* pEventEnum = __pContentEventListener->GetEnumeratorN();
1109         while (pEventEnum->MoveNext() == E_SUCCESS)
1110         {
1111                 pInterface = static_cast<IFormContentUpdateEventListener*>(pEventEnum->GetCurrent());
1112                 pInterface->OnContentUpdated();
1113         }
1114         delete pEventEnum;
1115         AppLogDebug("EXIT");
1116 }
1117
1118 void
1119 AlbumListPresentationModel::OnDeviceStateChanged(DeviceType deviceType, const Tizen::Base::String& state)
1120 {
1121         AppLogDebug("ENTER");
1122         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
1123 }
1124
1125 void
1126 AlbumListPresentationModel::SetUpdateProgressStatus(const bool status)
1127 {
1128         AppLogDebug("ENTER status = %d",status);
1129         __updateProgressStatus = status;
1130         AppLogDebug("EXIT");
1131 }
1132
1133 result
1134 AlbumListPresentationModel::RotateImage(int index, RotateMode rotateMode)
1135 {
1136         return E_SUCCESS;
1137 }
1138
1139 void
1140 AlbumListPresentationModel::RequestThumbnail(const int folderIndex, const int pathIndex)
1141 {
1142         AppLogDebug("ENTER : folderIndex(%d), pathIndex(%d)", folderIndex, pathIndex);
1143
1144         ContentId contentId;
1145         if (__pAlbumInfoList != null)
1146         {
1147                 AlbumInfo* pAlbumInfo = static_cast<AlbumInfo*>(__pAlbumInfoList->GetAt(folderIndex));
1148                 contentId = pAlbumInfo->GetContentId(pathIndex);
1149         }
1150
1151         __pThumbnailProvider->RequestThumbnail(contentId, __pThumbnailEvent);
1152         AppLogDebug("EXIT");
1153 }
1154
1155 void
1156 AlbumListPresentationModel::ClearThumbnailRequests(const bool appTerminating)
1157 {
1158         AppLogDebug("ENTER");
1159         if (__pThumbnailProvider != null)
1160         {
1161                 __pThumbnailProvider->ClearThumbnailRequests(appTerminating);
1162         }
1163         AppLogDebug("EXIT");
1164 }