Fix memory leaks
[apps/osp/Gallery.git] / src / GlFileListPresentationModel.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                GlFileListPresentationModel.cpp
19  * @brief               This is the implementation file for FileListPresentationModel class.
20  */
21
22 #include <cstdlib>
23 #include <FMedia.h>
24 #include <FSystem.h>
25 #include "GlCommonUtil.h"
26 #include "GlFileListPresentationModel.h"
27 #include "GlContentUpdateEventListener.h"
28 #include "GlGalleryApp.h"
29 #include "GlThumbnailEvent.h"
30 #include "GlThumbnailEventArg.h"
31 #include "GlThumbnailProvider.h"
32 #include "GlTypes.h"
33
34 using namespace Tizen::App;
35 using namespace Tizen::Base;
36 using namespace Tizen::Base::Collection;
37 using namespace Tizen::Base::Runtime;
38 using namespace Tizen::Base::Utility;
39 using namespace Tizen::Content;
40 using namespace Tizen::Graphics;
41 using namespace Tizen::Io;
42 using namespace Tizen::Media;
43 using namespace Tizen::System;
44
45 static const String CONTENT_INFO_ORDER = L"ContentFileName";
46
47 static const int SIZE_PAGE_NO = 1;
48 static const int SIZE_CONUNT_PER_PAGE = 3000;
49 static const int SIZE_ICON_LIST_CACHE = 50;
50 static const int W_DURATION_TEXT = 44;
51 static const int H_DURATION_TEXT = 4;
52 static const int W_DURATION = 171;
53 static const int H_DURATION = 32;
54 static const int W_DEFAULT_THUMBNAIL = 171;
55 static const int H_DEFAULT_THUMBNAIL = 127;
56 static const int W_WIDE_THUMBNAIL = 171;
57 static const int H_WIDE_THUMBNAIL = 127;
58 static const int W_PLAY_FG = 64;
59 static const int H_PLAY_FG = 64;
60 static const int GAP_W_PLAY_FG = (W_DEFAULT_THUMBNAIL -W_PLAY_FG)/2 ;
61 static const int GAP_H_PLAY_FG = (H_DEFAULT_THUMBNAIL - H_PLAY_FG)/2;
62 static const int GAP_H_DURATION = H_DEFAULT_THUMBNAIL - H_DURATION;
63 static const int ALPHA_DURATION = 70;
64 static const int FONT_SIZE_DURATION = 24;
65 static const Dimension DIMENSION_REAL_ICON (171,127);
66 static const Rectangle RECTANGLE_THUMBNAIL_OFFSET_POSION (1, 1, 171, 127);
67 static const Rectangle RECTANGLE_VIDEO_THUMBNAIL_OFFSET_POSION (1, 1, 171, 127);
68 static const Color COLOR_DURATION_BG (Color::GetColor(COLOR_ID_BLACK));
69 static const Color COLOR_TEXT_OUTLINE (Color::GetColor(COLOR_ID_WHITE));
70
71 static const String RESERVED_CAMERA_PATH = Environment::GetMediaPath() + L"Camera";
72 static const String RESERVED_CAMERA_PATH_EXT = Environment::GetExternalStoragePath() + L"Camera";
73 static const String RESERVED_DOWNLOAD_PATH = Environment::GetMediaPath() + L"Downloads";
74
75 FileListPresentationModel* FileListPresentationModel::__pPresentationModelInstance = null;
76 ThumbnailProvider* FileListPresentationModel::__pThumbnailProvider = null;
77 ThumbnailEvent* FileListPresentationModel::__pThumbnailEvent = null;
78 ArrayList* FileListPresentationModel::__pPresentationModelListener = null;
79 ArrayList* FileListPresentationModel::__pContentEventListener = null;
80 ArrayList* FileListPresentationModel::__pIconListViewCache = null;
81 IList* FileListPresentationModel::__pDirectoryList = null;
82 IList* FileListPresentationModel::__pContentInfoList = null;
83
84 FileListPresentationModel::FileListPresentationModel(void)
85         : __pContentManager(null)
86         , __albumContentType(CONTENT_TYPE_ALL)
87         , __appControlMode(APPCONTROL_MODE_MAIN)
88         , __appControlMediaType(APPCONTROL_MEDIA_TYPE_IMAGE)
89         , __appControlSelectionMode(APPCONTROL_SELECTION_MODE_SINGLE)
90         , __pContentIdList(null)
91         , __updateProgressStatus(false)
92 {
93         AppLogDebug("ENTER");
94         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
95 }
96
97 FileListPresentationModel::~FileListPresentationModel(void)
98 {
99         AppLogDebug("ENTER");
100         DeviceManager::RemoveDeviceEventListener(DEVICE_TYPE_STORAGE_CARD, *this);
101         if (__pContentIdList)
102         {
103                 __pContentIdList->RemoveAll(true);
104                 delete __pContentIdList;
105                 __pContentIdList = null;
106         }
107         delete __pContentManager;
108         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
109 }
110
111 FileListPresentationModel*
112 FileListPresentationModel::GetInstance(void)
113 {
114         AppLogDebug("ENTER");
115         if (__pPresentationModelInstance == null)
116         {
117                 CreateInstance();
118         }
119         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
120
121         return __pPresentationModelInstance;
122 }
123
124 result
125 FileListPresentationModel::Construct(void)
126 {
127         AppLogDebug("ENTER");
128
129         IThumbnailEventListener* pThumbnailEventListener = static_cast<IThumbnailEventListener*>(this);
130         __pThumbnailEvent = new (std::nothrow) ThumbnailEvent();
131         __pThumbnailEvent->AddListener(*pThumbnailEventListener, true);
132
133         DeviceManager::AddDeviceEventListener(DEVICE_TYPE_STORAGE_CARD, *this);
134
135         ContentUpdateEventListener::GetInstance();
136         __pContentManager = new (std::nothrow) ContentManager();
137         __pContentManager->Construct();
138
139         if (__pIconListViewCache != null)
140         {
141                 __pIconListViewCache->RemoveAll(true);
142                 delete __pIconListViewCache;
143                 __pIconListViewCache = null;
144         }
145         __pIconListViewCache = new (std::nothrow) ArrayList(SingleObjectDeleter);
146         __pIconListViewCache->Construct(SIZE_ICON_LIST_CACHE);
147
148         if (__pDirectoryList != null)
149         {
150                 delete __pDirectoryList;
151         }
152         __pDirectoryList = new (std::nothrow) ArrayList(SingleObjectDeleter);
153
154         if (__pContentInfoList != null)
155         {
156                 delete __pContentInfoList;
157         }
158         __pContentInfoList = new (std::nothrow) ArrayList(SingleObjectDeleter);
159
160         __pPresentationModelListener = new (std::nothrow) ArrayList();
161         __pPresentationModelListener->Construct();
162
163         __pContentEventListener = new (std::nothrow) ArrayList();
164         __pContentEventListener->Construct();
165
166         __pThumbnailProvider = ThumbnailProvider::GetInstance();
167
168         GalleryApp* pApp = static_cast<GalleryApp*>(GalleryApp::GetInstance());
169         const IMap* pArguments = pApp->GetAppControlArguments();
170         String operationId = pApp->GetAppControlOperationId();
171
172         if (operationId.CompareTo(APPCONTROL_OPERATION_ID_MAIN) == 0)
173         {
174                 __appControlMode = APPCONTROL_MODE_MAIN;
175         }
176         else if (operationId.CompareTo(APPCONTROL_OPERATION_ID_PICK) == 0)
177         {
178                 __appControlMode = APPCONTROL_MODE_PICK;
179
180                 String mimeType = pApp->GetMimeType();
181                 if (mimeType.IsEmpty() != true)
182                 {
183                         if (mimeType.StartsWith(APPCONTROL_DATA_VIDEO, 0) == true)
184                         {
185                                 __appControlMediaType = APPCONTROL_MEDIA_TYPE_VIDEO;
186                         }
187                 }
188                 if (pArguments != null)
189                 {
190                         const String* selectionMode = static_cast<const String*>(pArguments->GetValue(String(APPCONTROL_KEY_SELECTION_MODE)));
191
192                         if (pApp->GetMimeType().StartsWith(APPCONTROL_DATA_VIDEO, 0) == true)
193                         {
194                                 __appControlMediaType = APPCONTROL_MEDIA_TYPE_VIDEO;
195                         }
196
197                         if (selectionMode != null && selectionMode->CompareTo(APPCONTROL_DATA_MULTIPLE) == 0)
198                         {
199                                 __appControlSelectionMode = APPCONTROL_SELECTION_MODE_MULTIPLE;
200                         }
201                 }
202         }
203         else if (operationId.CompareTo(APPCONTROL_OPERATION_ID_CONFIGURE) == 0)
204         {
205                 __appControlMode = APPCONTROL_MODE_SETTING;
206         }
207
208         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
209
210         return E_SUCCESS;
211 }
212
213 void
214 FileListPresentationModel::CreateInstance(void)
215 {
216         AppLogDebug("ENTER");
217         __pPresentationModelInstance = new (std::nothrow) FileListPresentationModel();
218         result r = __pPresentationModelInstance->Construct();
219         if (IsFailed(r) == true)
220         {
221                 delete __pPresentationModelInstance;
222                 __pPresentationModelInstance = null;
223                 AppLogDebug("EXIT 1(%s)", GetErrorMessage(GetLastResult()));
224
225                 return;
226         }
227
228         std::atexit(DestroyInstance);
229         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
230 }
231
232 void
233 FileListPresentationModel::DestroyInstance(void)
234 {
235         AppLogDebug("ENTER");
236
237         delete __pThumbnailEvent;
238         __pIconListViewCache = null;
239
240         if (__pIconListViewCache != null)
241         {
242                 __pIconListViewCache->RemoveAll(true);
243                 delete __pIconListViewCache;
244                 __pIconListViewCache = null;
245         }
246
247         if (__pDirectoryList != null)
248         {
249                 delete __pDirectoryList;
250                 __pDirectoryList = null;
251         }
252
253         if (__pContentInfoList != null)
254         {
255                 delete __pContentInfoList;
256                 __pContentInfoList = null;
257         }
258
259         if (__pPresentationModelListener != null)
260         {
261                 delete __pPresentationModelListener;
262                 __pPresentationModelListener = null;
263         }
264
265         if (__pContentEventListener != null)
266         {
267                 delete __pContentEventListener;
268                 __pContentEventListener = null;
269         }
270
271         delete __pPresentationModelInstance;
272         __pPresentationModelInstance = null;
273
274         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
275 }
276
277 String
278 FileListPresentationModel::GetFileNameFromFullPath(const String& fullPath, const bool withExt) const
279 {
280         AppLogDebug("ENTER");
281         if (fullPath.CompareTo(EMPTY_SPACE) == 0)
282         {
283                 AppLogDebug("EXIT 1(%s)", GetErrorMessage(GetLastResult()));
284
285                 return EMPTY_SPACE;
286         }
287
288         String delim(DIRECTORY_SEPARATOR);
289         StringTokenizer st(fullPath,delim);
290         String token;
291         while (st.HasMoreTokens())
292         {
293                 st.GetNextToken(token);
294         }
295
296         if (withExt == true)
297         {
298                 AppLogDebug("EXIT 2(%s)", GetErrorMessage(GetLastResult()));
299
300                 return token;
301         }
302         else
303         {
304                 String subDelim(FILE_EXT_SEPARATOR);
305                 StringTokenizer subSt(token, subDelim);
306                 String subToken;
307                 subSt.GetNextToken(subToken);
308                 AppLogDebug("EXIT 3(%s)", GetErrorMessage(GetLastResult()));
309
310                 return subToken;
311         }
312 }
313
314 String
315 FileListPresentationModel::GetDirecotyNameFromFullPath(const String& fullPath)const
316 {
317         AppLogDebug("ENTER");
318         if (fullPath == null)
319         {
320                 AppLogDebug("EXIT 1(%s)", GetErrorMessage(GetLastResult()));
321
322                 return null;
323         }
324         String delim(DIRECTORY_SEPARATOR);
325         StringTokenizer st(fullPath,delim);
326         String token;
327         while (st.HasMoreTokens())
328         {
329                 st.GetNextToken(token);
330         }
331         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
332
333         return token;
334 }
335
336 bool
337 FileListPresentationModel::IsContainContentInDirectory(const String& directoryPath, ContentType contentType) const
338 {
339         AppLogDebug("ENTER");
340         IListT<ContentType>* pContentTypeList = new (std::nothrow) ArrayListT<ContentType>();
341         if (&directoryPath == null || pContentTypeList == null)
342         {
343                 AppLogDebug("EXIT 1(%s)", GetErrorMessage(GetLastResult()));
344
345                 return false;
346         }
347
348         if (contentType == CONTENT_TYPE_IMAGE || contentType == CONTENT_TYPE_ALL)
349         {
350                 pContentTypeList->Add(CONTENT_TYPE_IMAGE);
351         }
352         if (contentType == CONTENT_TYPE_VIDEO || contentType == CONTENT_TYPE_ALL)
353         {
354                 pContentTypeList->Add(CONTENT_TYPE_VIDEO);
355         }
356
357         ContentDirectory contentDirectory;
358         result r = contentDirectory.Construct(*pContentTypeList);
359         if (pContentTypeList != null)
360         {
361                 delete pContentTypeList;
362         }
363         TryReturn(r == E_SUCCESS, false, "[%s] Unable to construct ContentDirectory", GetErrorMessage(r));
364
365         bool returnValue = false;
366         int pageNo = SIZE_PAGE_NO;
367         int countPerPage = SIZE_CONUNT_PER_PAGE;
368
369         IList* pContentList = contentDirectory.GetContentDirectoryItemListN(directoryPath, pageNo, countPerPage,
370                         CONTENT_INFO_ORDER, SORT_ORDER_ASCENDING);
371
372         if (pContentList == null || pContentList->GetCount() <= 0)
373         {
374                 returnValue = false;
375         }
376         else
377         {
378                 returnValue = true;
379         }
380
381         if (pContentList != null)
382         {
383                 delete pContentList;
384         }
385         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
386
387         return returnValue;
388 }
389
390 result
391 FileListPresentationModel::GetThumbnailInSyncCacheN(const int index, String*& pName, Bitmap*& pThumbnail)
392 {
393         AppLogDebug("ENTER : index(%d)", index);
394         Bitmap* pOriginalBitmap = null;
395         Bitmap* pDestinationBitmap = null;
396         ContentInfo* pContentInfo = null;
397
398         if (__pContentInfoList != null && index < __pContentInfoList->GetCount())
399         {
400                 pContentInfo = static_cast<ContentInfo*>(__pContentInfoList->GetAt(index));
401                 if (pContentInfo == null)
402                 {
403                         AppLogDebug("EXIT 1(%s)", GetErrorMessage(GetLastResult()));
404
405                         return E_FAILURE;
406                 }
407         }
408         else
409         {
410                 AppLogDebug("EXIT 2 __pContentInfoList(%x) index(%d)", __pContentInfoList, index);
411                 return E_FAILURE;
412         }
413
414         ThumbnailInfo* pThumbnailInfo = GetThumbnailInfoFromInternalBufferN(pContentInfo->GetContentId());
415         if (pThumbnailInfo != null)
416         {
417                 pName = new (std::nothrow) String(pThumbnailInfo->GetFilePath());
418                 pOriginalBitmap = pThumbnailInfo->GetBitmapN();
419         }
420
421         if ((pThumbnailInfo != null)&&(pThumbnailInfo->GetContentType() == CONTENT_TYPE_VIDEO))
422         {
423                 if (pOriginalBitmap != null)
424                 {
425                         pDestinationBitmap = CoverVideoOverlayedImageOnThumbnailN(*pOriginalBitmap, *pThumbnailInfo);
426                         delete pOriginalBitmap;
427                 }
428         }
429         else
430         {
431                 pDestinationBitmap = pOriginalBitmap;
432         }
433
434         if (pDestinationBitmap != null)
435         {
436                 Bitmap* pThumbnailBgBitmap = ResourceManager::GetBitmapN(IDB_VIDEOTHUMBNAIL_BG);
437                 Rectangle thumbnailRect(RECTANGLE_THUMBNAIL_OFFSET_POSION);
438                 pDestinationBitmap->Scale(Dimension(RECTANGLE_THUMBNAIL_OFFSET_POSION.width - RECTANGLE_THUMBNAIL_OFFSET_POSION.x
439                                 , RECTANGLE_THUMBNAIL_OFFSET_POSION.height - RECTANGLE_THUMBNAIL_OFFSET_POSION.y));
440                 if (pThumbnailBgBitmap != null)
441                 {
442                         pThumbnailBgBitmap->Scale(DIMENSION_REAL_ICON);
443                         pThumbnail = GetShadedBackgroundBitmapN(*pThumbnailBgBitmap, *pDestinationBitmap, thumbnailRect);
444                         delete pThumbnailBgBitmap;
445                         delete pDestinationBitmap;
446                 }
447                 else
448                 {
449                         pThumbnail = pDestinationBitmap;
450                 }
451         }
452         else
453         {
454                 pThumbnail = null;
455         }
456
457         if (pThumbnailInfo != null)
458         {
459                 delete pThumbnailInfo;
460         }
461
462         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
463
464         return E_SUCCESS;
465 }
466
467 result
468 FileListPresentationModel::GetThumbnailVideoInSyncCacheN(const int index,
469                 String*& pName, Bitmap*& pThumbnail, String*& pDuration) const
470 {
471         AppLogDebug("ENTER : index(%d)", index);
472         ContentInfo* pContentInfo = null;
473         Bitmap* pOriginalBitmap = null;
474
475         if (__pContentInfoList != null && index < __pContentInfoList->GetCount())
476         {
477                 pContentInfo = static_cast<ContentInfo*>(__pContentInfoList->GetAt(index));
478                 if (pContentInfo == null)
479                 {
480                         AppLogDebug("EXIT 1(%s)", GetErrorMessage(GetLastResult()));
481                         return E_FAILURE;
482                 }
483         }
484         else
485         {
486                 AppLogDebug("EXIT 2 __pContentInfoList(%x) index(%d)", __pContentInfoList, index);
487                 return E_FAILURE;
488         }
489
490         ThumbnailInfo* pThumbnailInfo = GetThumbnailInfoFromInternalBufferN(pContentInfo->GetContentId());
491         if (pThumbnailInfo != null)
492         {
493                 pName = new (std::nothrow) String(pThumbnailInfo->GetFilePath());
494                 pOriginalBitmap = pThumbnailInfo->GetBitmapN();
495                 pDuration = new (std::nothrow) String(CommonUtil::DurationToTimeString(pThumbnailInfo->GetDuration()));
496         }
497         else
498         {
499                 pName = new (std::nothrow) String(EMPTY_SPACE);
500                 pDuration = new (std::nothrow) String(CommonUtil::DurationToTimeString(0));
501         }
502
503         if (pOriginalBitmap != null)
504         {
505                 Bitmap* pThumbnailBgBitmap = ResourceManager::GetBitmapN(IDB_VIDEOTHUMBNAIL_BG);
506                 Rectangle thumbnailRect(RECTANGLE_VIDEO_THUMBNAIL_OFFSET_POSION);
507                 pOriginalBitmap->Scale(Dimension(RECTANGLE_VIDEO_THUMBNAIL_OFFSET_POSION.width - RECTANGLE_VIDEO_THUMBNAIL_OFFSET_POSION.x
508                                 , RECTANGLE_VIDEO_THUMBNAIL_OFFSET_POSION.height - RECTANGLE_VIDEO_THUMBNAIL_OFFSET_POSION.y));
509                 if (pThumbnailBgBitmap != null)
510                 {
511                         pThumbnailBgBitmap->Scale(Dimension(W_WIDE_THUMBNAIL, H_WIDE_THUMBNAIL));
512                         pThumbnail = GetShadedBackgroundBitmapN(*pThumbnailBgBitmap, *pOriginalBitmap, thumbnailRect);
513                         delete pThumbnailBgBitmap;
514                 }
515                 delete pOriginalBitmap;
516         }
517         else
518         {
519                 pThumbnail = null;
520         }
521
522         if (pThumbnail != null)
523         {
524                 pThumbnail->Scale(Dimension(W_WIDE_THUMBNAIL, H_WIDE_THUMBNAIL));
525         }
526
527         if (pThumbnailInfo != null)
528         {
529                 delete pThumbnailInfo;
530         }
531         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
532
533         return E_SUCCESS;
534 }
535
536 void
537 FileListPresentationModel::RequestThumbnail(const int index)
538 {
539         AppLogDebug("ENTER : index(%d)", index);
540         ContentInfo* pContentInfo = null;
541
542         if (__pContentInfoList != null && index < __pContentInfoList->GetCount())
543         {
544                 pContentInfo = static_cast<ContentInfo*>(__pContentInfoList->GetAt(index));
545                 if (pContentInfo == null)
546                 {
547                         AppLogDebug("EXIT 1(%s)", GetErrorMessage(GetLastResult()));
548                         return;
549                 }
550         }
551         else
552         {
553                 AppLogDebug("EXIT 2 __pContentInfoList(%x) index(%d)", __pContentInfoList, index);
554
555                 return;
556         }
557
558         __pThumbnailProvider->RequestThumbnail(pContentInfo->GetContentId(), __pThumbnailEvent);
559         AppLogDebug("EXIT");
560 }
561
562 void
563 FileListPresentationModel::ClearThumbnailRequests(const bool appTerminating)
564 {
565         AppLogDebug("ENTER");
566
567         if (__pThumbnailProvider != null)
568         {
569                 __pThumbnailProvider->ClearThumbnailRequests(appTerminating);
570         }
571
572         AppLogDebug("EXIT");
573 }
574
575 void
576 FileListPresentationModel::OnThumbnailReceivedN(IEventArg& eventArg)
577 {
578         AppLogDebug("ENTER");
579
580         if (&eventArg != null)
581         {
582                 ThumbnailEventArg* pThumbnailEventArg = static_cast<ThumbnailEventArg*>(&eventArg);
583                 if (pThumbnailEventArg == null)
584                 {
585                         AppLogDebug("EXIT 1(%s)", GetErrorMessage(GetLastResult()));
586
587                         return;
588                 }               
589                 ThumbnailInfo* pThumbnailInfo = pThumbnailEventArg->GetThumbnailInfoN();
590                 if (pThumbnailInfo == null)
591                 {
592                         AppLogDebug("EXIT 2(%s)", GetErrorMessage(GetLastResult()));
593
594                         return;
595                 }
596
597                 bool isMatch = false;
598                 ContentId contentId = pThumbnailInfo->GetContentId();
599
600                 if (__pIconListViewCache != null)
601                 {
602                         IEnumerator* pEnum = __pIconListViewCache->GetEnumeratorN();
603                         Bitmap* pBitmap = null;
604                         while (pEnum->MoveNext() == E_SUCCESS)
605                         {
606                                 ThumbnailInfo* pTempThumbnailInfo = static_cast<ThumbnailInfo*>(pEnum->GetCurrent());
607
608                                 if (contentId == pTempThumbnailInfo->GetContentId())
609                                 {
610                                         AppLogDebug("[CHASEFIRE] PM receive (%ls)", pThumbnailInfo->GetFilePath().GetPointer());
611                                         pBitmap = pThumbnailInfo->GetBitmapN();
612                                         pTempThumbnailInfo->SetBitmap(*pBitmap);
613                                         delete pBitmap;
614
615                                         pTempThumbnailInfo->SetContentType(pThumbnailInfo->GetContentType());
616                                         pTempThumbnailInfo->SetDuration(pThumbnailInfo->GetDuration());
617                                         delete pThumbnailInfo;
618                                         isMatch = true;
619                                         break;
620                                 }
621                         }
622
623                         delete pEnum;
624
625                         if (isMatch == false)
626                         {
627                                 if (SIZE_ICON_LIST_CACHE <= __pIconListViewCache->GetCount())
628                                 {
629                                         __pIconListViewCache->RemoveAt(0, true);
630                                 }
631                                 AppLogDebug("[CHASEFIRE] (all:%d) Add Thumbail(%ls)", __pIconListViewCache->GetCount(),
632                                                 pThumbnailInfo->GetFilePath().GetPointer());
633                                 __pIconListViewCache->Add(pThumbnailInfo);
634                         }
635                 }
636
637                 int index = -1;
638                 int loopCount = __pContentInfoList->GetCount();
639                 for (int i = 0; i < loopCount; ++i)
640                 {
641                         if (contentId == (static_cast<ContentInfo*>(__pContentInfoList->GetAt(i)))->GetContentId())
642                         {
643                                 index = i;
644                                 break;
645                         }
646                 }
647
648                 IAlbumEventListener* pInterface = null;
649                 IEnumerator* pEventEnum = __pPresentationModelListener->GetEnumeratorN();
650                 while (pEventEnum->MoveNext() == E_SUCCESS)
651                 {
652                         pInterface = static_cast<IAlbumEventListener*>(pEventEnum->GetCurrent());
653                         pInterface->OnThumbnailDecoded(index);
654                 }
655                 delete pEventEnum;
656         }
657         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
658 }
659
660 void
661 FileListPresentationModel::OnContentCreated(void)
662 {
663         AppLogDebug("ENTER");
664         if (__updateProgressStatus == true)
665         {
666                 AppLogDebug("EXIT update in application");
667                 return;
668         }
669         IFormContentUpdateEventListener* pInterface = null;
670         if (__pContentEventListener == null)
671         {
672                 AppLogDebug("EXIT");
673                 return;
674         }
675         IEnumerator* pEventEnum = __pContentEventListener->GetEnumeratorN();
676         while (pEventEnum->MoveNext() == E_SUCCESS)
677         {
678                 pInterface = static_cast<IFormContentUpdateEventListener*>(pEventEnum->GetCurrent());
679                 if (pInterface != null)
680                 {
681                         pInterface->OnContentUpdated();
682                 }
683         }
684         delete pEventEnum;
685         AppLogDebug("EXIT");
686 }
687
688 void
689 FileListPresentationModel::OnContentUpdated(void)
690 {
691         AppLogDebug("ENTER");
692         if (__updateProgressStatus == true)
693         {
694                 AppLogDebug("EXIT update in application");
695                 return;
696         }
697         //Clear the ImageCaches when content is updated like rotate.
698         if ( __pContentEventListener->GetCount() )
699         {
700                 ClearImageCache();
701         }
702
703         IFormContentUpdateEventListener* pInterface = null;
704         if (__pContentEventListener == null)
705         {
706                 AppLogDebug("EXIT");
707                 return;
708         }
709         IEnumerator* pEventEnum = __pContentEventListener->GetEnumeratorN();
710         while (pEventEnum->MoveNext() == E_SUCCESS)
711         {
712                 pInterface = static_cast<IFormContentUpdateEventListener*>(pEventEnum->GetCurrent());
713                 pInterface->OnContentUpdated();
714         }
715         delete pEventEnum;
716         AppLogDebug("EXIT");
717 }
718
719 void
720 FileListPresentationModel::OnContentDeleted(void)
721 {
722         AppLogDebug("ENTER");
723         if (__updateProgressStatus == true)
724         {
725                 AppLogDebug("EXIT update in application");
726                 return;
727         }
728         IFormContentUpdateEventListener* pInterface = null;
729         if (__pContentEventListener == null)
730         {
731                 AppLogDebug("EXIT");
732                 return;
733         }
734         IEnumerator* pEventEnum = __pContentEventListener->GetEnumeratorN();
735         while (pEventEnum->MoveNext() == E_SUCCESS)
736         {
737                 pInterface = static_cast<IFormContentUpdateEventListener*>(pEventEnum->GetCurrent());
738                 pInterface->OnContentUpdated();
739         }
740         delete pEventEnum;
741         AppLogDebug("EXIT");
742 }
743
744 void
745 FileListPresentationModel::AddPresentationModelListener(const IAlbumEventListener* listener)
746 {
747         AppLogDebug("ENTER");
748         __pPresentationModelListener->Add((Object*)listener);
749         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
750 }
751
752 void
753 FileListPresentationModel::RemovePresentationModelListener(const IAlbumEventListener& listener)
754 {
755         AppLogDebug("ENTER");
756         __pPresentationModelListener->Remove(listener);
757         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
758 }
759
760 void
761 FileListPresentationModel::AddContentEventListener(const IFormContentUpdateEventListener* listener)
762 {
763         AppLogDebug("ENTER");
764         if (__pContentEventListener != null)
765         {
766                 __pContentEventListener->RemoveAll();
767                 __pContentEventListener->Add((Object*)listener);
768                 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
769         }
770 }
771
772 void
773 FileListPresentationModel::RemoveContentEventListener(const IFormContentUpdateEventListener* listener)
774 {
775         AppLogDebug("ENTER");
776         __pContentEventListener->Remove(*listener);
777         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
778 }
779
780 ThumbnailInfo*
781 FileListPresentationModel::GetThumbnailInfoFromInternalBufferN(const ContentId& contentId) const
782 {
783         AppLogDebug("ENTER");
784         ThumbnailInfo* pThumbnailInfo = null;
785         ThumbnailInfo* pTempThumbnailInfo = null;
786         if (__pIconListViewCache != null)
787         {
788                 int loopCount = __pIconListViewCache->GetCount();
789                 for (int i = 0; i < loopCount; ++i)
790                 {
791                         pTempThumbnailInfo = static_cast<ThumbnailInfo*>(__pIconListViewCache->GetAt(i));
792                         if (contentId == pTempThumbnailInfo->GetContentId())
793                         {
794
795                                 pThumbnailInfo = new (std::nothrow) ThumbnailInfo();
796                                 Bitmap* pBitmap = pTempThumbnailInfo->GetBitmapN();
797                                 pThumbnailInfo->Construct(pTempThumbnailInfo->GetContentId(), pTempThumbnailInfo->GetFilePath(),
798                                                 *pBitmap, pTempThumbnailInfo->GetContentType(), pTempThumbnailInfo->GetDuration());
799                                 delete pBitmap;
800                                 break;
801                         }
802                 }
803         }
804         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
805
806         return pThumbnailInfo;
807 }
808
809 result
810 FileListPresentationModel::DeleteContentFileList(const IList& contentIndexList)
811 {
812         AppLogDebug("ENTER");
813         if (&contentIndexList == null || contentIndexList.GetCount() <= 0)
814         {
815                 AppLogDebug("EXIT 1(%s)", GetErrorMessage(GetLastResult()));
816
817                 return E_SUCCESS;
818         }
819
820         IList * pContentIdList = new (std::nothrow) ArrayList(SingleObjectDeleter);
821         int loopCount = contentIndexList.GetCount();
822         for (int i = 0; i < loopCount; ++i)
823         {
824                 const Integer* pIndex = static_cast<const Integer*>(contentIndexList.GetAt(i));
825
826                 if ((pIndex != null) && (pIndex->ToInt())>=0 && __pContentInfoList->GetCount() > pIndex->ToInt())
827                 {
828                         pContentIdList->Add(new (std::nothrow) ContentId((static_cast<ContentInfo*>
829                                         (__pContentInfoList->GetAt(pIndex->ToInt())))->GetContentId()));
830                 }
831         }
832
833         loopCount = pContentIdList->GetCount();
834         for (int i = 0; i < loopCount; ++i)
835         {
836                 ContentId* pContentId = static_cast<ContentId*>(pContentIdList->GetAt(i));
837                 if (pContentId != null)
838                 {
839                         DeleteContentFile(*pContentId);
840                 }
841         }
842         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
843
844         return E_SUCCESS;
845 }
846
847 IList*
848 FileListPresentationModel::GetCollisionIndexListN(const IList& contentIndexList, const String& destDirectory)
849 {
850         AppLogDebug("ENTER");
851         if (&contentIndexList == null || contentIndexList.GetCount() <= 0 || &destDirectory == null
852                         || destDirectory.IsEmpty() == true)
853         {
854                 AppLogDebug("EXIT 1(%s)", GetErrorMessage(GetLastResult()));
855
856                 return null;
857         }
858
859         if (File::IsFileExist(destDirectory) == false)
860         {
861                 AppLogDebug("EXIT 2(%s)", GetErrorMessage(GetLastResult()));
862
863                 return null;
864         }
865
866         IList * pCollisionInfoList = new (std::nothrow) ArrayList(SingleObjectDeleter);
867         ContentInfo* pContentInfo = null;
868         int loopCount = contentIndexList.GetCount();
869         for (int i = 0; i < loopCount; ++i)
870         {
871                 const Integer* pIndex = static_cast<const Integer*>(contentIndexList.GetAt(i));
872
873                 if ((pIndex != null) && (pIndex->ToInt()) >= 0 && __pContentInfoList->GetCount() > pIndex->ToInt())
874                 {
875                         pContentInfo = const_cast<ContentInfo*>(static_cast<const ContentInfo*>(__pContentInfoList->GetAt(pIndex->ToInt())));
876                         String filePath = pContentInfo->GetContentPath();
877                         String fileName = GetFileNameFromFullPath(filePath, true);
878
879                         String destPath;
880                         if (destDirectory.EndsWith(DIRECTORY_SEPARATOR) == true)
881                         {
882                                 destPath.Append(destDirectory);
883                                 destPath.Append(fileName);
884                         }
885                         else
886                         {
887                                 destPath.Append(destDirectory);
888                                 destPath.Append(DIRECTORY_SEPARATOR);
889                                 destPath.Append(fileName);
890                         }
891
892                         if (File::IsFileExist(destPath) == true)
893                         {
894                                 pCollisionInfoList->Add(*(new (std::nothrow) String(pIndex->ToString() + MULTI_ITEM_SEPARATOR + fileName)));
895                         }
896                 }
897         }
898         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
899
900         return pCollisionInfoList;
901 }
902
903 result
904 FileListPresentationModel::MoveToContentFileList(const IList& contentIndexList, const String& destDirectory)
905 {
906         AppLogDebug("ENTER");
907         result r = E_SUCCESS;
908         if (&contentIndexList == null || contentIndexList.GetCount() <= 0 || &destDirectory == null || destDirectory.IsEmpty() == true)
909         {
910                 AppLogDebug("EXIT 1(%s)", GetErrorMessage(GetLastResult()));
911
912                 return r;
913         }
914
915         if (File::IsFileExist(destDirectory) == false)
916         {
917                 Directory::Create(destDirectory, true);
918         }
919
920         delete __pContentIdList;
921         __pContentIdList = new (std::nothrow) ArrayList(SingleObjectDeleter);
922         int loopCount = contentIndexList.GetCount();
923         for (int i = 0; i < loopCount; ++i)
924         {
925                 const Integer* pIndex = static_cast<const Integer*>(contentIndexList.GetAt(i));
926
927                 if ((pIndex != null) && (pIndex->ToInt())>=0 && __pContentInfoList->GetCount() > pIndex->ToInt())
928                 {
929                         __pContentIdList->Add(new (std::nothrow) ContentId((static_cast<ContentInfo*>(__pContentInfoList->GetAt(pIndex->ToInt())))->GetContentId()));
930                 }
931         }
932
933
934         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
935
936         return E_SUCCESS;
937 }
938
939 result
940 FileListPresentationModel::DeleteContentFile(const ContentId& contentId)
941 {
942         AppLogDebug("ENTER");
943         if (&contentId == null)
944         {
945                 AppLogDebug("EXIT 1(%s)", GetErrorMessage(GetLastResult()));
946
947                 return E_SUCCESS;
948         }
949         result r = __pContentManager->DeleteContent(contentId);
950
951         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
952
953         return r;
954 }
955
956 result
957 FileListPresentationModel::MoveToContentFile(const ContentId& contentId, const String& destDirectory, const bool isCopyOperation)
958 {
959         AppLogDebug("ENTER");
960         if (contentId.ToString().IsEmpty() == true || destDirectory.IsEmpty() == true)
961         {
962                 AppLogDebug("EXIT 1(%s)", GetErrorMessage(GetLastResult()));
963                 return E_FAILURE;
964         }
965         result r = E_SUCCESS;
966         ContentInfo* pContentInfo = __pContentManager->GetContentInfoN(contentId);
967         if (pContentInfo == null)
968         {
969                 AppLogDebug("Santhosh %s", destDirectory.GetPointer());
970                 AppLogDebug("EXIT 2(%s)", GetErrorMessage(GetLastResult()));
971                 return E_FAILURE;
972         }
973
974         String filePath = pContentInfo->GetContentPath();
975         if (filePath.GetLength() < 4)
976         {
977                 AppLogDebug("EXIT 3(%s)", GetErrorMessage(GetLastResult()));
978                 return E_SUCCESS;
979         }
980
981         String destPath;
982         if (destDirectory.EndsWith(DIRECTORY_SEPARATOR) == true)
983         {
984                 destPath.Append(destDirectory);
985                 destPath.Append(GetFileNameFromFullPath(filePath, true));
986         }
987         else
988         {
989                 destPath.Append(destDirectory);
990                 destPath.Append(DIRECTORY_SEPARATOR);
991                 destPath.Append(GetFileNameFromFullPath(filePath, true));
992         }
993
994         if (destPath.CompareTo(filePath) == 0)
995         {
996                 AppLogDebug("EXIT 4(%s)", GetErrorMessage(GetLastResult()));
997                 return E_SUCCESS;
998         }
999
1000         if (File::IsFileExist(destPath) == true)
1001         {
1002                 File::Remove(destPath);
1003         }
1004
1005         r = File::Copy(filePath, destPath, false);
1006
1007         if ( r == E_STORAGE_FULL)
1008         {
1009                 File::Remove(destPath);
1010         }
1011         ContentManager::ScanFile(destPath);
1012
1013         if ( r == E_SUCCESS )
1014         {
1015                 if ( isCopyOperation == false)
1016                 {
1017                         File::Remove(filePath);               // if Move Op remove original file
1018                         ContentManager::ScanFile(filePath);
1019                 }
1020         }
1021
1022         delete pContentInfo;
1023
1024         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
1025
1026         return r;
1027 }
1028
1029 void
1030 FileListPresentationModel::ClearProviderId(void)
1031 {
1032         __providerId.Clear();
1033 }
1034
1035 result
1036 FileListPresentationModel::StartAppControl(const String& providerId, const String& operationId,
1037                 const String* pUriData, const Tizen::Base::String* pMimeType, const HashMap* pDataList,
1038                 IAppControlResponseListener* pListener)
1039 {
1040         AppLogDebug("ENTER");
1041         AppControl* pAc = AppManager::FindAppControlN(providerId, operationId);
1042
1043         ClearProviderId();
1044         __providerId.Append(providerId);
1045
1046         if (pAc == null)
1047         {
1048                 AppLogDebug("EXIT 1(%s)", GetErrorMessage(GetLastResult()));
1049                 return E_FAILURE;
1050         }
1051         result r = pAc->Start(pUriData, pMimeType, pDataList, pListener);
1052
1053         if (r == E_SUCCESS)
1054         {
1055                 GalleryApp* pGalleryApp = static_cast<GalleryApp*>(GalleryApp::GetInstance());
1056                 pGalleryApp->SetFrameEnabled(false);
1057         }
1058
1059         delete pAc;
1060
1061         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
1062
1063         return r;
1064 }
1065
1066 result
1067 FileListPresentationModel::SetCurrentAlbumInfo(const String& albumName, const IList& directoryList)
1068 {
1069         AppLogDebug("ENTER");
1070
1071         if (&directoryList != null)
1072         {
1073                 if (__pDirectoryList != null && __pDirectoryList->GetCount() > 0)
1074                 {
1075                         bool isMatched = false;
1076                         int outterLoopCount = __pDirectoryList->GetCount();
1077                         int innerLoopCount = 0;
1078                         for (int i = 0; i < outterLoopCount ; ++i)
1079                         {
1080                                 isMatched = false;
1081                                 innerLoopCount = directoryList.GetCount();
1082                                 for (int j = 0; j < innerLoopCount; ++j)
1083                                 {
1084                                         if (static_cast<String*>(__pDirectoryList->GetAt(i))->CompareTo(
1085                                                         *(const_cast<String*>(static_cast<const String*>(directoryList.GetAt(j))))) == 0)
1086                                         {
1087                                                 isMatched = true;
1088                                                 break;
1089                                         }
1090                                 }
1091                         }
1092
1093                         outterLoopCount = directoryList.GetCount();
1094                         for (int i = 0; i < outterLoopCount; ++i)
1095                         {
1096                                 isMatched = false;
1097                                 innerLoopCount = __pDirectoryList->GetCount();
1098                                 for (int j = 0; j < innerLoopCount; ++j)
1099                                 {
1100                                         if (const_cast<String*>(static_cast<const String*>(directoryList.GetAt(i)))->CompareTo(
1101                                                         *(static_cast<String*>(__pDirectoryList->GetAt(j)))) == 0)
1102                                         {
1103                                                 isMatched = true;
1104                                                 break;
1105                                         }
1106                                 }
1107                         }
1108                 }
1109         }
1110
1111         __albumName = albumName;
1112         __providerId.Clear();
1113
1114         if (__pDirectoryList != null)
1115         {
1116                 delete __pDirectoryList;
1117         }
1118         __pDirectoryList = new (std::nothrow) ArrayList(SingleObjectDeleter);
1119
1120         int loopCount = directoryList.GetCount();
1121         for (int i = 0; i < loopCount; ++i)
1122         {
1123                 __pDirectoryList->Add(new (std::nothrow) String(*(const_cast<String*>
1124                         (static_cast<const String*>(directoryList.GetAt(i))))));
1125         }
1126
1127         if (__pContentInfoList != null)
1128         {
1129                 delete __pContentInfoList;
1130                 __pContentInfoList = null;
1131         }
1132
1133         if (GetAppControlMode() == APPCONTROL_MODE_PICK)
1134         {
1135                 AppControlMediaType appControlMediaType = GetAppControlMediaType();
1136                 if (appControlMediaType == APPCONTROL_MEDIA_TYPE_IMAGE)
1137                 {
1138                         __albumContentType = CONTENT_TYPE_IMAGE;
1139                 }
1140                 else if (appControlMediaType == APPCONTROL_MEDIA_TYPE_VIDEO)
1141                 {
1142                         __albumContentType = CONTENT_TYPE_VIDEO;
1143                 }
1144         }
1145         __pContentInfoList = GetContentInfoListInDirectoryListN(*__pDirectoryList, __albumContentType);
1146         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
1147
1148         return E_SUCCESS;
1149 }
1150
1151 result
1152 FileListPresentationModel::RefreshCurrentAlbumContentInfoList(const ContentType contentType)
1153 {
1154         AppLogDebug("ENTER");
1155         result r = RefreshContentInfoList(contentType);
1156         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
1157
1158         return r;
1159 }
1160
1161 void
1162 FileListPresentationModel::AddDirectoryIfNew(const String& dirName)
1163 {
1164         bool isMatched = false;
1165         int dirCount = __pDirectoryList->GetCount();
1166         if (__albumName != ResourceManager::GetString(L"IDS_MEDIABR_BODY_ALL_ALBUMS"))//we are not in All albums
1167         {
1168                 return;
1169         }
1170         for (int i = 0; i < dirCount ; ++i)
1171         {
1172                 if (static_cast<String*>(__pDirectoryList->GetAt(i))->CompareTo(dirName) == 0)
1173                 {
1174                         isMatched = true;
1175                         break;
1176                 }
1177         }
1178         if (!isMatched)
1179         {
1180                 String fullPath = Environment::GetMediaPath();
1181                 fullPath.Append(dirName);
1182                 __pDirectoryList->Add(new (std::nothrow) String(fullPath));
1183
1184         }
1185 }
1186
1187
1188
1189 result
1190 FileListPresentationModel::RefreshContentInfoList(const ContentType contentType)
1191 {
1192         AppLogDebug("ENTER");
1193         if ((contentType != CONTENT_TYPE_ALL) && (contentType != CONTENT_TYPE_IMAGE) && (contentType != CONTENT_TYPE_VIDEO))
1194         {
1195                 AppLogDebug("EXIT 1(%s)", GetErrorMessage(GetLastResult()));
1196
1197                 return E_FAILURE;
1198         }
1199
1200         if ((__pDirectoryList == null) || (__pDirectoryList->GetCount() <= 0))
1201         {
1202                 AppLogDebug("EXIT 2(%s)", GetErrorMessage(GetLastResult()));
1203
1204                 return E_SUCCESS;
1205         }
1206
1207         __albumContentType = contentType;
1208
1209         if (__pContentInfoList != null)
1210         {
1211                 delete __pContentInfoList;
1212                 __pContentInfoList = null;
1213         }
1214
1215         __pContentInfoList = GetContentInfoListInDirectoryListN(*__pDirectoryList, __albumContentType);
1216         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
1217
1218         return E_SUCCESS;
1219 }
1220
1221 String
1222 FileListPresentationModel::GetCurrentAlbumName(void) const
1223 {
1224         AppLogDebug("ENTER");
1225         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
1226
1227         return __albumName;
1228 }
1229
1230 void
1231 FileListPresentationModel::SetCurrentAlbumName(const String& albumName)
1232 {
1233         AppLogDebug("ENTER");
1234         if (&albumName == null)
1235         {
1236                 __albumName = EMPTY_SPACE;
1237         }
1238         else
1239         {
1240                 __albumName = albumName;
1241         }
1242         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
1243 }
1244
1245 ContentType
1246 FileListPresentationModel::GetCurrentAlbumContentType(void) const
1247 {
1248         AppLogDebug("ENTER");
1249         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
1250
1251         return __albumContentType;
1252 }
1253
1254 void
1255 FileListPresentationModel::SetCurrentAlbumContentType(const ContentType contentType)
1256 {
1257         AppLogDebug("ENTER");
1258         __albumContentType = contentType;
1259         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
1260 }
1261
1262 IList*
1263 FileListPresentationModel::GetAlbumDirectoryListN(void) const
1264 {
1265         AppLogDebug("ENTER");
1266         IList* pAlbumDirectoryList = new (std::nothrow) ArrayList(SingleObjectDeleter);
1267         if (__pDirectoryList == null)
1268         {
1269                 AppLogDebug("EXIT 1(%s)", GetErrorMessage(GetLastResult()));
1270
1271                 return pAlbumDirectoryList;
1272         }
1273
1274         String* pDirectory = null;
1275         int loopCount = __pDirectoryList->GetCount();
1276         for (int i = 0; i < loopCount; ++i)
1277         {
1278                 pDirectory = static_cast<String*>(__pDirectoryList->GetAt(i));
1279                 pAlbumDirectoryList->Add(new (std::nothrow) String(*pDirectory));
1280         }
1281         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
1282
1283         return pAlbumDirectoryList;
1284 }
1285
1286 IList*
1287 FileListPresentationModel::GetAlbumContentInfoList(void) const
1288 {
1289         AppLogDebug("ENTER");
1290         IList* pContentList = new (std::nothrow) ArrayList(SingleObjectDeleter);
1291         if (__pContentInfoList == null)
1292         {
1293                 AppLogDebug("EXIT 1(%s)", GetErrorMessage(GetLastResult()));
1294
1295                 return pContentList;
1296         }
1297
1298         ContentInfo* pContentInfo = null;
1299         int loopCount = __pContentInfoList->GetCount();
1300         for (int i = 0; i < loopCount; ++i)
1301         {
1302                 pContentInfo = static_cast<ContentInfo*>(__pContentInfoList->GetAt(i));
1303                 pContentList->Add(pContentInfo);
1304         }
1305         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
1306
1307         return pContentList;
1308 }
1309
1310 ContentInfo*
1311 FileListPresentationModel::GetContentInfo(const int index) const
1312 {
1313         AppLogDebug("ENTER");
1314         if (__pContentInfoList == null || __pContentInfoList->GetCount() == 0)
1315         {
1316                 AppLogDebug("EXIT 1(%s)", GetErrorMessage(GetLastResult()));
1317
1318                 return null;
1319         }
1320
1321         if (index < 0 || index >= __pContentInfoList->GetCount())
1322         {
1323                 AppLogDebug("EXIT 2(%s)", GetErrorMessage(GetLastResult()));
1324
1325                 return null;
1326         }
1327         ContentInfo* pContentInfo = static_cast<ContentInfo*>(__pContentInfoList->GetAt(index));
1328         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
1329
1330         return pContentInfo;
1331 }
1332
1333 IList* FileListPresentationModel::GetContentIdListAtIndexN(const IList& indexList) const
1334 {
1335         IList* pContentIdList = new (std::nothrow) ArrayList(SingleObjectDeleter);
1336         int loopCount = indexList.GetCount();
1337         for (int i = 0; i < loopCount; ++i)
1338         {
1339                 const Integer* pIndex = static_cast<const Integer*>(indexList.GetAt(i));
1340
1341                 if ((pIndex != null) && (pIndex->ToInt())>=0 && __pContentInfoList->GetCount() > pIndex->ToInt())
1342                 {
1343                         pContentIdList->Add(new (std::nothrow) ContentId((static_cast<ContentInfo*>(__pContentInfoList->GetAt(pIndex->ToInt())))->GetContentId()));
1344                 }
1345         }
1346
1347         return pContentIdList;
1348 }
1349
1350 int
1351 FileListPresentationModel::GetCurrentAlbumContentInfoCount(void) const
1352 {
1353         AppLogDebug("ENTER");
1354         if (__pContentInfoList == null)
1355         {
1356                 AppLogDebug("EXIT 1(%s)", GetErrorMessage(GetLastResult()));
1357
1358                 return 0;
1359         }
1360         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
1361
1362         return __pContentInfoList->GetCount();
1363 }
1364
1365 ContentId
1366 FileListPresentationModel::GetContentInfoIndex(const int index) const
1367 {
1368         AppLogDebug("ENTER");
1369         if (__pContentInfoList == null || __pContentInfoList->GetCount() == 0)
1370         {
1371                 AppLogDebug("EXIT 1(%s)", GetErrorMessage(GetLastResult()));
1372
1373                 return 0;
1374         }
1375
1376         if (index < 0 || index >= __pContentInfoList->GetCount())
1377         {
1378                 AppLogDebug("EXIT 2(%s)", GetErrorMessage(GetLastResult()));
1379
1380                 return 0;
1381         }
1382         ContentInfo* pContentInfo = static_cast<ContentInfo*>(__pContentInfoList->GetAt(index));
1383         ContentId contentId = pContentInfo->GetContentId();
1384         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
1385
1386         return contentId;
1387 }
1388
1389 String
1390 FileListPresentationModel::GetContentFilePath(const int index) const
1391 {
1392         AppLogDebug("ENTER index(%d)", index);
1393         if (__pContentInfoList == null || __pContentInfoList->GetCount() == 0)
1394         {
1395                 AppLogDebug("EXIT 1(%s)", GetErrorMessage(GetLastResult()));
1396
1397                 return EMPTY_SPACE;
1398         }
1399
1400         if (index < 0 || index >= __pContentInfoList->GetCount())
1401         {
1402                 AppLogDebug("EXIT 2(%s)", GetErrorMessage(GetLastResult()));
1403
1404                 return EMPTY_SPACE;
1405         }
1406
1407         ContentInfo* pContentInfo = static_cast<ContentInfo*>(__pContentInfoList->GetAt(index));
1408         String contentPath = pContentInfo->GetContentPath();
1409         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
1410
1411         return contentPath;
1412 }
1413
1414 Bitmap*
1415 FileListPresentationModel::CoverVideoOverlayedImageOnThumbnailN(Bitmap& thumbnail,
1416                 const ThumbnailInfo& thumbmailInfo)
1417 {
1418         AppLogDebug("ENTER");
1419         Bitmap* pOverlayedImage = null;
1420
1421         if (&thumbmailInfo != null)
1422         {
1423                 long duration = thumbmailInfo.GetDuration();
1424
1425                 Canvas mainCanvas;
1426                 if (&thumbnail != null)
1427                 {
1428                         BufferInfo bufferInfo;
1429                         thumbnail.Lock(bufferInfo, INFINITE);
1430                         thumbnail.Unlock();
1431                         mainCanvas.Construct(bufferInfo);
1432                 }
1433                 else
1434                 {
1435                         Rectangle mainRect(0, 0, W_DEFAULT_THUMBNAIL, H_DEFAULT_THUMBNAIL);
1436                         mainCanvas.Construct(mainRect);
1437                         mainCanvas.FillRectangle(Color::GetColor(COLOR_ID_BLACK), mainRect);
1438                 }
1439
1440                 Rectangle playRect(GAP_W_PLAY_FG, GAP_H_PLAY_FG, W_PLAY_FG, H_PLAY_FG);
1441                 Bitmap* playBitmap = ResourceManager::GetBitmapN(IDB_VIDEOTHUMBNAIL_PLAY);
1442                 if (playBitmap != null)
1443                 {
1444                         mainCanvas.DrawBitmap(playRect, *playBitmap);
1445                         delete playBitmap;
1446                 }
1447
1448                 Canvas durCanvas;
1449                 Rectangle durSize(0, 0, W_DURATION, H_DURATION);
1450                 Rectangle durRect(0, GAP_H_DURATION, W_DURATION, H_DURATION);
1451                 Color durColor(COLOR_DURATION_BG);
1452                 durColor.SetAlpha(ALPHA_DURATION);
1453                 durCanvas.Construct(durSize);
1454                 durCanvas.FillRectangle(durColor, durSize);
1455                 Font durFont;
1456                 durFont.Construct(FONT_STYLE_PLAIN, FONT_SIZE_DURATION);
1457                 durCanvas.SetFont(durFont);
1458                 durCanvas.DrawText(Point(W_DURATION_TEXT, H_DURATION_TEXT),
1459                                 CommonUtil::DurationToTimeString(duration), COLOR_TEXT_OUTLINE);
1460                 Bitmap durBitmap;
1461                 durBitmap.Construct(durCanvas, durCanvas.GetBounds());
1462                 mainCanvas.DrawBitmap(durRect, durBitmap);
1463
1464                 pOverlayedImage = new (std::nothrow) Bitmap();
1465                 pOverlayedImage->Construct(mainCanvas, mainCanvas.GetBounds());
1466         }
1467         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
1468
1469         return pOverlayedImage;
1470 }
1471
1472 Bitmap*
1473 FileListPresentationModel::GetShadedBackgroundBitmapN(Bitmap& bgBitmap,
1474                 const Bitmap& originalBitmap, const Rectangle& originalBitmapPosition) const
1475 {
1476         AppLogDebug("ENTER");
1477         if (&originalBitmap == null || &originalBitmap == null)
1478         {
1479                 AppLogDebug("EXIT 1(%s)", GetErrorMessage(GetLastResult()));
1480
1481                 return null;
1482         }
1483
1484         BufferInfo bufferInfo;
1485         bgBitmap.Lock(bufferInfo, INFINITE);
1486         bgBitmap.Unlock();
1487         Canvas mainCanvas;
1488         mainCanvas.Construct(bufferInfo);
1489         mainCanvas.Clear();
1490         mainCanvas.DrawBitmap(originalBitmapPosition, originalBitmap);
1491
1492         Bitmap* pMainImage = new (std::nothrow) Bitmap();
1493         pMainImage->Construct(mainCanvas,mainCanvas.GetBounds());
1494         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
1495
1496         return pMainImage;
1497 }
1498
1499 IList*
1500 FileListPresentationModel::GetContentInfoListInDirectoryListN(const IList& contentDirectoryList,
1501                 ContentType contentType)const
1502 {
1503         AppLogDebug("ENTER");
1504         IList* pContentList = new (std::nothrow) ArrayList(SingleObjectDeleter);
1505
1506         if ((&contentDirectoryList == null) || (contentDirectoryList.GetCount() == 0)
1507                         || ((contentType != CONTENT_TYPE_IMAGE) && (contentType != CONTENT_TYPE_VIDEO) && (contentType != CONTENT_TYPE_ALL)))
1508         {
1509                 AppLogDebug("EXIT 1(%s)", GetErrorMessage(GetLastResult()));
1510                 return pContentList;
1511         }
1512
1513         ContentDirectory contentDirectory;
1514         IListT<ContentType>* pContentTypeList = new (std::nothrow) ArrayListT<ContentType>();
1515         if (contentType == CONTENT_TYPE_IMAGE || contentType == CONTENT_TYPE_ALL)
1516         {
1517                 pContentTypeList->Add(CONTENT_TYPE_IMAGE);
1518         }
1519         if (contentType == CONTENT_TYPE_VIDEO || contentType == CONTENT_TYPE_ALL)
1520         {
1521                 pContentTypeList->Add(CONTENT_TYPE_VIDEO);
1522         }
1523         result r = contentDirectory.Construct(*pContentTypeList);
1524         delete pContentTypeList;
1525
1526         if (r == E_SUCCESS)
1527         {
1528                 int pageNo = SIZE_PAGE_NO;
1529                 int countPerPage = SIZE_CONUNT_PER_PAGE;
1530                 String* pContentDirectory = null;
1531
1532                 int loopCount = contentDirectoryList.GetCount();
1533                 for (int i = 0; i < loopCount; ++i)
1534                 {
1535                         IList* pIList = null;
1536                         pContentDirectory = const_cast<String*>(static_cast<const String*>(contentDirectoryList.GetAt(i)));
1537                         if (pContentDirectory != null)
1538                         {
1539                                 pIList = contentDirectory.GetContentDirectoryItemListN(*pContentDirectory, pageNo, countPerPage,
1540                                                 CONTENT_INFO_ORDER, SORT_ORDER_NONE);
1541
1542                                 if (pIList != null)
1543                                 {
1544                                         pContentList->AddItems(*pIList);
1545                                 }
1546                         }
1547                 }
1548         }
1549         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
1550
1551         return pContentList;
1552 }
1553
1554 String
1555 FileListPresentationModel::ConvertToAlbumName(const String& targetPath)const
1556 {
1557         AppLogDebug("ENTER");
1558         String albumName;
1559         if (&targetPath == null || targetPath.GetLength() == 0)
1560         {
1561                 return albumName;
1562         }
1563
1564         if (targetPath == RESERVED_CAMERA_PATH || targetPath == RESERVED_CAMERA_PATH_EXT)
1565         {
1566                 albumName = ResourceManager::GetString(L"IDS_MEDIABR_BODY_CAMERA_ROLL_ABB");
1567         }
1568         else if (targetPath == RESERVED_DOWNLOAD_PATH)
1569         {
1570                 albumName = ResourceManager::GetString(L"IDS_COM_BODY_DOWNLOADS");
1571         }
1572         else
1573         {
1574                 String directoryName = GetDirecotyNameFromFullPath(targetPath);
1575                 albumName = directoryName;
1576         }
1577
1578         return albumName;
1579 }
1580
1581 AppControlMode
1582 FileListPresentationModel::GetAppControlMode(void) const
1583 {
1584         AppLogDebug("Enter");
1585         AppLogDebug("Exit");
1586
1587         return __appControlMode;
1588 }
1589
1590 AppControlMediaType
1591 FileListPresentationModel::GetAppControlMediaType(void) const
1592 {
1593         AppLogDebug("Enter");
1594         AppLogDebug("Exit");
1595
1596         return __appControlMediaType;
1597 }
1598
1599 AppControlSelectionMode
1600 FileListPresentationModel::GetAppControlSelectionMode(void) const
1601 {
1602         AppLogDebug("Enter");
1603         AppLogDebug("Exit");
1604
1605         return __appControlSelectionMode;
1606 }
1607
1608 void
1609 FileListPresentationModel::SetUpdateProgressStatus(const bool status)
1610 {
1611         AppLogDebug("ENTER status = %d",status);
1612         __updateProgressStatus = status;
1613         AppLogDebug("EXIT");
1614 }
1615
1616 result
1617 FileListPresentationModel::RotateImage(int index, RotateMode rotateMode)
1618 {
1619         result r = E_SUCCESS;
1620         ImageBuffer rotateBuffer;
1621         ImageBuffer* pRotatedBuffer = null;
1622         ImageFormat imageFormat;
1623         Image img;
1624         String filePath = GetContentFilePath(index);
1625         ContentType contentType = ContentManagerUtil::CheckContentType(filePath);
1626         if (contentType == CONTENT_TYPE_VIDEO)
1627         {
1628                 return r;
1629         }
1630
1631         r = img.Construct();
1632
1633         if (r == E_SUCCESS)
1634         {
1635                 imageFormat = img.GetImageFormat(filePath);
1636
1637                 r = rotateBuffer.Construct(filePath);
1638                 if (r == E_SUCCESS)
1639                 {
1640                         if (rotateMode == ROTATE_MODE_RIGHT)
1641                         {
1642                                 pRotatedBuffer = rotateBuffer.RotateN(IMAGE_ROTATION_90);
1643                         }
1644                         else
1645                         {
1646                                 pRotatedBuffer = rotateBuffer.RotateN(IMAGE_ROTATION_270);
1647                         }
1648                 }
1649
1650                 if (pRotatedBuffer != null)
1651                 {
1652                         r = pRotatedBuffer->EncodeToFile(filePath, imageFormat, true, 100);
1653                         delete pRotatedBuffer;
1654                         ContentManager::ScanFile(filePath);
1655                 }
1656         }
1657
1658         return r;
1659 }
1660
1661 void
1662 FileListPresentationModel::ClearImageCache(void)
1663 {
1664         AppLogDebug("ENTER");
1665
1666         if(__pIconListViewCache != null)
1667         {
1668                 bool clearThumbnailReq = true;
1669                 while(__pIconListViewCache->GetCount() != 0)
1670                 {
1671                         //This is to Clear the Thumbnail Reuqests only Once
1672                         if(clearThumbnailReq == true)
1673                         {
1674                                 ClearThumbnailRequests();
1675                                 clearThumbnailReq = false;
1676                         }
1677                         __pIconListViewCache->RemoveAt(0, true);
1678                 }
1679         }
1680         AppLogDebug("EXIT");
1681 }
1682
1683 void
1684 FileListPresentationModel::OnDeviceStateChanged(DeviceType deviceType, const Tizen::Base::String& state)
1685 {
1686         AppLogDebug("ENTER");
1687         if (deviceType == DEVICE_TYPE_STORAGE_CARD && state == DEVICE_STORAGE_CARD_UNMOUNTED)
1688         {
1689                 IFormContentUpdateEventListener* pInterface = null;
1690                 IEnumerator* pEventEnum = __pContentEventListener->GetEnumeratorN();
1691                 while (pEventEnum->MoveNext() == E_SUCCESS)
1692                 {
1693                         pInterface = static_cast<IFormContentUpdateEventListener*>(pEventEnum->GetCurrent());
1694                         pInterface->OnContentUpdated();
1695                 }
1696                 delete pEventEnum;
1697         }
1698         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
1699 }