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