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