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