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