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