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