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