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