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