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