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