Fix for NSE_38362
[apps/osp/Gallery.git] / src / GlAlbumNameEditorForm.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                GlAlbumNameEditorForm.cpp
19  * @brief               This is the implementation file for AlbumNameEditorForm class.
20  */
21
22 #include <FMedia.h>
23 #include "GlAlbumInfo.h"
24 #include "GlAlbumListPresentationModel.h"
25 #include "GlAlbumNameEditorForm.h"
26 #include "GlFileListPresentationModel.h"
27 #include "GlFileMoveTimer.h"
28 #include "GlProgressAnimation.h"
29 #include "GlResourceManager.h"
30 #include "GlTypes.h"
31
32 using namespace Tizen::App;
33 using namespace Tizen::Base;
34 using namespace Tizen::Base::Collection;
35 using namespace Tizen::Base::Runtime;
36 using namespace Tizen::Base::Utility;
37 using namespace Tizen::Content;
38 using namespace Tizen::Graphics;
39 using namespace Tizen::Io;
40 using namespace Tizen::Media;
41 using namespace Tizen::System;
42 using namespace Tizen::Ui;
43 using namespace Tizen::Ui::Controls;
44 using namespace Tizen::Ui::Scenes;
45
46 static const Rectangle RECT_NAME_EDIT_FIELD (0, 0, 720, 100);
47 static const unsigned int COLOR_NAME_EDIT_FIELD = Color32<255, 255, 255>::Value;
48 static const int ALBUM_MAX_LENGTH = 255;
49
50 AlbumNameEditorForm::AlbumNameEditorForm(void)
51         : __pNameEditField(null)
52         , __folderIndex(0)
53         , __mountState(false)
54         , __albumNameEditorMode(ALBUM_NAME_EDITOR_MODE_RENAME)
55         , __pMessageBox(null)
56         , __pMoveIndexList(null)
57         , __fileActionMode(FILE_ACTION_MOVE)
58         , __pFileMove(null)
59         , __pProgressAnim(null)
60         , __pPresentationModel(null)
61         , __pFilePresentationModel(null)
62 {
63         AppLogDebug("ENTER");
64         _overlayMsg = false;
65         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
66 }
67
68 AlbumNameEditorForm::~AlbumNameEditorForm(void)
69 {
70         AppLogDebug("ENTER");
71         DeviceManager::RemoveDeviceEventListener(DEVICE_TYPE_STORAGE_CARD, *this);
72         delete __pFileMove;
73         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
74 }
75
76 result
77 AlbumNameEditorForm::Initialize(void)
78 {
79         AppLogDebug("ENTER");
80         result r = Construct(FORM_STYLE_HEADER | FORM_STYLE_FOOTER | FORM_STYLE_NORMAL | FORM_STYLE_INDICATOR);
81         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
82
83         return r;
84 }
85
86 result
87 AlbumNameEditorForm::OnInitializing(void)
88 {
89         AppLogDebug("ENTER");
90         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
91         __pFilePresentationModel = FileListPresentationModel::GetInstance();
92
93         DeviceManager::AddDeviceEventListener(DEVICE_TYPE_STORAGE_CARD, *this);
94         Header* pHeader = GetHeader();
95
96         pHeader->SetStyle(HEADER_STYLE_TITLE);
97
98         GetFooter()->SetBackButtonEnabled(true);
99         GetFooter()->SetBackButton();
100
101         FooterItem itemSave;
102         itemSave.Construct(IDA_BUTTON_CREATE_NAME_SAVE);
103         itemSave.SetText(ResourceManager::GetString(L"IDS_COM_OPT_SAVE"));
104
105         GetFooter()->AddItem(itemSave);
106         GetFooter()->AddActionEventListener(*this);
107         GetFooter()->Invalidate(true);
108
109         SetFormBackEventListener(this);
110         SetActionBarsVisible(FORM_ACTION_BAR_FOOTER, true);
111
112         __pNameEditField = new (std::nothrow) EditField();
113         __pNameEditField->Construct(RECT_NAME_EDIT_FIELD, EDIT_FIELD_STYLE_NORMAL, INPUT_STYLE_OVERLAY,
114                         EDIT_FIELD_TITLE_STYLE_NONE, true, ALBUM_MAX_LENGTH);
115         __pNameEditField->AddTextEventListener(*this);
116         __pNameEditField->SetColor(EDIT_STATUS_NORMAL, COLOR_NAME_EDIT_FIELD);
117         __pNameEditField->SetOverlayKeypadCommandButtonVisible(false);
118
119         AddControl(__pNameEditField);
120         __pNameEditField->AddActionEventListener(*this);
121
122         return E_SUCCESS;
123 }
124
125 void
126 AlbumNameEditorForm::OnTextValueChanged(const Control& source)
127 {
128         AppLogDebug("ENTER");
129         String inputString = __pNameEditField->GetText();
130
131         if (inputString.Contains(DIRECTORY_SEPARATOR) == true)
132         {
133                 if (__pMessageBox == null)
134                 {
135                         __pMessageBox = new (std::nothrow) MessageBox();
136                         __pMessageBox->Construct(L"", ResourceManager::GetString(L"IDS_COM_POP_INVALID_CHARACTERS"),
137                                         MSGBOX_STYLE_NONE, 3000);
138                         int modalResult = 0;
139                         _overlayMsg = true;
140                         __pMessageBox->ShowAndWait(modalResult);
141                         _overlayMsg = false;
142                         delete __pMessageBox;
143                         __pMessageBox = null;
144                 }
145
146                 __pNameEditField->SetText(__nameEditFieldTempText);
147                 __pNameEditField->SetFocus();
148
149                 return;
150         }
151
152         ByteBuffer* pBuffer = StringUtil::StringToUtf8N(inputString);
153         int remaining = 0;
154         if (pBuffer != null)
155         {
156                 remaining = pBuffer->GetRemaining();
157         }
158
159         if (remaining >= ALBUM_MAX_LENGTH)
160         {
161                 if (__pMessageBox == null)
162                 {
163                         __pMessageBox = new (std::nothrow) MessageBox();
164                         __pMessageBox->Construct(L"",
165                                         ResourceManager::GetString(L"IDS_COM_POP_MAXIMUM_NUMBER_OF_CHARACTERS_REACHED"),
166                                         MSGBOX_STYLE_NONE, 3000);
167                         int modalResult = 0;
168                         __pMessageBox->ShowAndWait(modalResult);
169                         delete __pMessageBox;
170                         __pMessageBox = null;
171                 }
172
173                 __pNameEditField->SetText(__nameEditFieldTempText);
174                 __pNameEditField->SetFocus();
175
176                 return;
177         }
178         __nameEditFieldTempText = inputString;
179
180         inputString.Trim();
181
182         FooterItemStatus itemStatus;
183         GetFooter()->GetItemStatus(0, itemStatus);
184         if (inputString.GetLength() == 0)
185         {
186                 if (itemStatus != FOOTER_ITEM_STATUS_DISABLED)
187                 {
188                         GetFooter()->SetItemEnabled(0, false);
189                 }
190         }
191         else
192         {
193                 if (itemStatus == FOOTER_ITEM_STATUS_DISABLED)
194                 {
195                         GetFooter()->SetItemEnabled(0, true);
196                 }
197         }
198         GetFooter()->Invalidate(true);
199
200         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
201 }
202
203 void
204 AlbumNameEditorForm::OnTextValueChangeCanceled(const Control& source)
205 {
206         AppLogDebug("ENTER");
207         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
208 }
209
210 result
211 AlbumNameEditorForm::OnTerminating(void)
212 {
213         AppLogDebug("ENTER");
214         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
215
216         return E_SUCCESS;
217 }
218
219 void AlbumNameEditorForm::OnFileOpInvalidate(enum FileActionMode actionId)
220 {
221         Invalidate(true);
222 }
223
224 void AlbumNameEditorForm::OnFileOpComplete(enum FileActionMode actionId, enum FileActionCompleteRes res)
225 {
226         _overlayMsg = false;
227         if (res != COMPLETE_SUCCESS)
228         {
229                 __pNameEditField->SetText(__nameEditFieldPreText);
230                 __pNameEditField->HideKeypad();
231                 __pNameEditField->Draw();
232                 SceneManager* pSceneManager = SceneManager::GetInstance();
233                 pSceneManager->GoBackward(BackwardSceneTransition(__callerSceneId));
234         }
235 }
236
237 void
238 AlbumNameEditorForm::OnRenameAlbum(String& strOri)
239 {
240         AppLogDebug("Rename Album");
241         String path;
242         int index = 0;
243         strOri.Reverse();
244         strOri.IndexOf(DIRECTORY_SEPARATOR, 0, index);
245         strOri.Reverse();
246         strOri.SubString(0, (strOri.GetLength() - index), path);
247         path.Append(__nameEditFieldText);
248
249         //Rename
250         result r = __pPresentationModel->RenameAlbum(strOri, path, this);
251
252         if (IsFailed(r))
253         {
254                 AppLogDebug("Unable to rename Album %s", GetErrorMessage(r));
255                 if (__pMessageBox == null)
256                 {
257                         __pMessageBox = new (std::nothrow) MessageBox();
258                         if (r == E_FILE_ALREADY_EXIST)
259                         {
260                                 __pMessageBox->Construct(L"", ResourceManager::GetString(L"IDS_MEDIABR_BODY_UNABLE_TO_RENAME_ALBUM_NAME_ALREADY_IN_USE_ABB"),
261                                                           MSGBOX_STYLE_NONE, 3000);
262                         }
263                         else
264                         {
265                                 __pMessageBox->Construct(L"", ResourceManager::GetString(L"IDS_MEDIABR_POP_UNABLE_TO_RENAME"),
266                                                           MSGBOX_STYLE_NONE, 3000);
267                         }
268                         int modalResult = 0;
269                         _overlayMsg = true;
270                         __pMessageBox->ShowAndWait(modalResult);
271                         _overlayMsg = false;
272                         delete __pMessageBox;
273                         __pMessageBox = null;
274                 }
275         }
276         else
277         {
278                 //Add a popup here
279                 Rectangle clientRect= GetClientAreaBounds();
280                 Rectangle rect(0, 0, clientRect.width, clientRect.height);
281                 __pProgressAnim = new (std::nothrow) ProgressAnimation;
282                 __pProgressAnim->Construct(rect);
283                 AddControl(__pProgressAnim);
284                 __pProgressAnim->SetShowState(true);
285                 GetFooter()->SetEnabled(false);
286                 __pProgressAnim->AnimationStart();
287                 __pProgressAnim->Draw();
288                 _overlayMsg = true;
289         }
290 }
291
292 void
293 AlbumNameEditorForm::OnCreateAlbum(void)
294 {
295         String path;
296         __pNameEditField->HideKeypad();
297         __pNameEditField->RequestRedraw();
298         path.Append(Tizen::System::Environment::GetMediaPath());
299         path.Append(__nameEditFieldText);
300
301         if (__previousSceneId != IDSCN_ALL_LIST_EDITOR && __previousSceneId != IDSCN_ALL_LIST_SELECTION)
302         {
303                 Directory::Create(path, true);
304         }
305
306         __nameEditFieldPreText = __nameEditFieldText;
307
308         if (path.IsEmpty())
309         {
310                 AppLogDebug("EXIT 1(%s)", GetErrorMessage(GetLastResult()));
311                 delete __pMoveIndexList;
312                 __pMoveIndexList = null;
313                 return ;
314         }
315
316         if (__pMoveIndexList == null || __pMoveIndexList->GetCount() <= 0)
317         {
318                 AppLogDebug("EXIT 2(%s)", GetErrorMessage(GetLastResult()));
319                 delete __pMoveIndexList;
320                 __pMoveIndexList = null;
321                 return ;
322         }
323
324         delete __pFileMove;
325         GetMoveFileIndexList(path, __pMoveIndexList, __pFilePresentationModel);
326         __pFileMove = new (std::nothrow) FileMoveTimer(path, __pMoveIndexList, __pFilePresentationModel, this);
327         if (__fileActionMode == FILE_ACTION_COPY)
328         {
329                 AppLogDebug("Rash: Setting to Copy in AlbumNameEditor");
330                 __pFileMove->SetCopy();
331         }
332         else
333         {
334                 __pFileMove->ClearCopy();
335         }
336         result r = __pFileMove->StartTimer();
337
338         if (IsFailed(r))
339         {
340                 delete __pFileMove;
341                 __pFileMove = null;
342                 __pNameEditField->SetText(__nameEditFieldPreText);
343                 __pNameEditField->HideKeypad();
344                 __pNameEditField->Draw();
345                 AppLogDebug("MoveToContentFileList content failed (%s)", GetErrorMessage(r));
346                 SceneManager* pSceneManager = SceneManager::GetInstance();
347                 pSceneManager->GoBackward(BackwardSceneTransition(__callerSceneId));
348         }
349         else
350         {
351                 AppLogDebug("MoveToContentFileList content in progress");
352                 _overlayMsg = true;
353                 __fileActionMode = FILE_ACTION_MOVE;
354         }
355 }
356
357 void AlbumNameEditorForm::OnAlbumRenameComplete(void)
358 {
359         //Hide popup here..
360         __pProgressAnim->AnimationStop();
361         __pProgressAnim->SetShowState(false);
362         RemoveControl(__pProgressAnim);
363         _overlayMsg = false;
364         GetFooter()->SetEnabled(true);
365         SceneManager* pSceneManager = SceneManager::GetInstance();
366         pSceneManager->GoForward(ForwardSceneTransition(IDSCN_ALBUM_LIST));
367 }
368
369 void
370 AlbumNameEditorForm::OnActionPerformed(const Control& source, int actionId)
371 {
372         AppLogDebug("ENTER");
373         String path;
374         __nameEditFieldText = __pNameEditField->GetText();
375
376         if (__albumNameEditorMode == ALBUM_NAME_EDITOR_MODE_RENAME)
377         {
378                 IList* pAlbumInfoList = __pPresentationModel->GetAlbumInfoList();
379                 AlbumInfo* pAlbumInfo = static_cast<AlbumInfo*>(pAlbumInfoList->GetAt(__folderIndex));
380                 path = pAlbumInfo->GetDirectory(0);
381         }
382
383         switch (actionId)
384         {
385         case IDA_BUTTON_CREATE_NAME_SAVE:
386         {
387                 if (__albumNameEditorMode == ALBUM_NAME_EDITOR_MODE_CREATE)
388                 {
389                         OnCreateAlbum();
390                 }
391                 else if (__albumNameEditorMode == ALBUM_NAME_EDITOR_MODE_RENAME)
392                 {
393                         String albumName = __pNameEditField->GetText();
394                         if (__originalText.Equals(albumName , true))
395                         {
396                                 SceneManager* pSceneManager = SceneManager::GetInstance();
397                                 pSceneManager->GoBackward(BackwardSceneTransition());
398                         }
399                         else
400                         {
401                                 OnRenameAlbum(path);
402                         }
403                 }
404                 break;
405         }
406
407         default:
408                 break;
409         }
410         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
411 }
412
413 void
414 AlbumNameEditorForm::OnFormBackRequested(Form& source)
415 {
416         AppLogDebug("ENTER");
417         if (__albumNameEditorMode == ALBUM_NAME_EDITOR_MODE_CREATE)
418         {
419                 SceneManager* pSceneManager = SceneManager::GetInstance();
420                 pSceneManager->GoForward(ForwardSceneTransition(__callerSceneId));
421         }
422         else
423         {
424                 SceneManager* pSceneManager = SceneManager::GetInstance();
425                 pSceneManager->GoForward(ForwardSceneTransition(IDSCN_ALBUM_LIST_EDITOR));
426         }
427         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
428 }
429
430 void
431 AlbumNameEditorForm::OnSceneActivatedN(const SceneId& previousSceneId,
432                 const SceneId& currentSceneId, IList* pArgs)
433 {
434         AppLogDebug("ENTER");
435         __pPresentationModel = AlbumListPresentationModel::GetInstance();
436         __previousSceneId = previousSceneId;
437
438         __pPresentationModel->AddContentEventListener(this);
439
440         if (pArgs != null)
441         {
442                 Integer* pAlbumTypePtr = static_cast<Integer*>(pArgs->GetAt(ALBUM_FIRST_ELEMENT));
443                 if (pAlbumTypePtr->ToInt() == ALBUM_CREATE)
444                 {
445                         AppLogDebug("ALBUM_NAME_EDITOR_MODE_CREATE");
446                         __albumNameEditorMode = ALBUM_NAME_EDITOR_MODE_CREATE;
447                 }
448                 else if (pAlbumTypePtr->ToInt() == ALBUM_RENAME)
449                 {
450                         AppLogDebug("ALBUM_NAME_EDITOR_MODE_RENAME");
451                         __albumNameEditorMode = ALBUM_NAME_EDITOR_MODE_RENAME;
452                 }
453                 pArgs->RemoveAt(ALBUM_FIRST_ELEMENT, true);
454         }
455
456         if (pArgs != null)
457         {
458                 Integer* operationTypePtr = static_cast<Integer*>(pArgs->GetAt(ALBUM_FIRST_ELEMENT));
459                 if (operationTypePtr->ToInt() == FILE_ACTION_MOVE)
460                 {
461                         AppLogDebug("Rash: FILE_MOVE_ACTION");
462                         __fileActionMode = FILE_ACTION_MOVE;
463                 }
464                 else if (operationTypePtr->ToInt() == FILE_ACTION_COPY)
465                 {
466                         AppLogDebug("Rash: FILE_COPY_ACTION");
467                         __fileActionMode = FILE_ACTION_COPY;
468                 }
469                 pArgs->RemoveAt(ALBUM_FIRST_ELEMENT, true);
470         }
471
472         if (previousSceneId == IDSCN_ALBUM_LIST_EDITOR)
473         {
474                 if (pArgs != null)
475                 {
476                         IEnumerator* pEnum = pArgs->GetEnumeratorN();
477                         if (pEnum->MoveNext() == E_SUCCESS)
478                         {
479                                 Integer *index = static_cast<Integer*>(pEnum->GetCurrent());
480                                 if (index != null)
481                                 {
482                                         __folderIndex = index->ToInt();
483                                 }
484                         }
485                         delete pEnum;
486                         delete pArgs;
487                 }
488         }
489
490         if (previousSceneId == IDSCN_ALL_LIST_EDITOR
491                         || previousSceneId == IDSCN_IMAGE_LIST_EDITOR
492                         || previousSceneId == IDSCN_VIDEO_LIST_EDITOR
493                         || previousSceneId == IDSCN_ALL_LIST_SELECTION)
494         {
495                 if (previousSceneId == IDSCN_ALL_LIST_EDITOR)
496                 {
497                         AppLogDebug("[MOVE] previousSceneId = IDSCN_ALL_LIST_EDITOR");
498                 }
499                 else if (previousSceneId == IDSCN_ALL_LIST_SELECTION)
500                 {
501                         AppLogDebug("[MOVE] previousSceneId = IDSCN_ALL_LIST_SELECTION");
502                 }
503
504                 __callerSceneId = previousSceneId;
505         }
506         else
507         {
508                 __callerSceneId = EMPTY_SPACE;
509         }
510
511         if (__albumNameEditorMode == ALBUM_NAME_EDITOR_MODE_CREATE)
512         {
513                 __nameEditFieldText = EMPTY_SPACE;
514                 __nameEditFieldPreText = EMPTY_SPACE;
515                 __pNameEditField->SetText(__nameEditFieldText);
516                 __nameEditFieldTempText = __nameEditFieldText;
517                 __pNameEditField->SetFocus();
518
519                 GetHeader()->SetTitleText(ResourceManager::GetString(L"IDS_MEDIABR_OPT_CREATE_ALBUM"));
520                 GetFooter()->SetItemEnabled(0, false);
521                 GetFooter()->Invalidate(true);
522         }
523         else
524         {
525
526                 IList* pAlbumInfoList = __pPresentationModel->GetAlbumInfoList();
527                 AlbumInfo* pAlbumInfo = static_cast<AlbumInfo*>(pAlbumInfoList->GetAt(__folderIndex));
528
529                 String strName = pAlbumInfo->GetAlbumName();
530                 __nameEditFieldText = strName;
531                 __nameEditFieldPreText = strName;
532                 __pNameEditField->SetText(__nameEditFieldText);
533                 GetHeader()->SetTitleText(ResourceManager::GetString(L"IDS_COM_BODY_RENAME"));
534                 __pNameEditField->SetOverlayKeypadCommandButtonVisible(false);
535                 __pNameEditField->ShowKeypad();
536                 __pNameEditField->SetFocus();
537                 __originalText = __pNameEditField->GetText();
538         }
539
540         if (pArgs != NULL)
541         {
542                 __pMoveIndexList = pArgs;
543         }
544
545         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
546 }
547
548 void
549 AlbumNameEditorForm::OnSceneDeactivated(const SceneId& currentSceneId,
550                 const SceneId& nextSceneId)
551 {
552         AppLogDebug("ENTER");
553         __pPresentationModel->RemoveContentEventListener(*this);
554         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
555 }
556
557 void
558 AlbumNameEditorForm::OnContentUpdated(void)
559 {
560         AppLogDebug("ENTER");
561
562         if ( __mountState == false )
563         {
564                 SceneManager* pSceneManager = SceneManager::GetInstance();
565                 pSceneManager->GoForward(ForwardSceneTransition(IDSCN_ALBUM_LIST));
566                 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
567         }
568         else
569         {
570                 __mountState = false;
571         }
572 }
573
574 void
575 AlbumNameEditorForm::OnDeviceStateChanged(DeviceType deviceType, const Tizen::Base::String& state)
576 {
577         AppLogDebug("ENTER");
578
579         if (deviceType == DEVICE_TYPE_STORAGE_CARD && state == DEVICE_STORAGE_CARD_UNMOUNTED)
580         {
581                 __mountState = false;
582                 SceneManager* pSceneManager = SceneManager::GetInstance();
583                 pSceneManager->GoForward(ForwardSceneTransition(IDSCN_ALBUM_LIST));
584                 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
585         }
586         else
587         {
588                 __mountState = true;
589         }
590         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
591 }