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