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