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