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