2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
8 // http://floralicense.org/license/
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.
18 * @file GlAlbumNameEditorForm.cpp
19 * @brief This is the implementation file for AlbumNameEditorForm class.
25 #include "GlAlbumInfo.h"
26 #include "GlAlbumListPresentationModel.h"
27 #include "GlAlbumNameEditorForm.h"
28 #include "GlFileListPresentationModel.h"
29 #include "GlFileMoveTimer.h"
30 #include "GlProgressAnimation.h"
31 #include "GlResourceManager.h"
34 using namespace Tizen::App;
35 using namespace Tizen::Base;
36 using namespace Tizen::Base::Collection;
37 using namespace Tizen::Base::Runtime;
38 using namespace Tizen::Base::Utility;
39 using namespace Tizen::Content;
40 using namespace Tizen::Graphics;
41 using namespace Tizen::Io;
42 using namespace Tizen::Media;
43 using namespace Tizen::System;
44 using namespace Tizen::Text;
45 using namespace Tizen::Ui;
46 using namespace Tizen::Ui::Controls;
47 using namespace Tizen::Ui::Scenes;
49 static const unsigned int COLOR_NAME_EDIT_FIELD = Color32<255, 255, 255>::Value;
51 AlbumNameEditorForm::AlbumNameEditorForm(void)
52 : __pNameEditField(null)
54 , __modalMsgBoxResult(0)
57 , __isCreateAlbum(false)
58 , __isKeyPadOpen(false)
59 , __albumNameEditorMode(ALBUM_NAME_EDITOR_MODE_RENAME)
61 , __pMoveIndexList(null)
62 , __fileActionMode(FILE_ACTION_MOVE)
64 , __pPresentationModel(null)
65 , __pFilePresentationModel(null)
69 _pProgressAnim = null;
70 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
73 AlbumNameEditorForm::~AlbumNameEditorForm(void)
76 DeviceManager::RemoveDeviceEventListener(DEVICE_TYPE_STORAGE_CARD, *this);
78 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
82 AlbumNameEditorForm::Initialize(void)
85 result r = Construct(L"IDL_FORM_ALBUM_NAME_EDITOR");
86 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
92 AlbumNameEditorForm::OnInitializing(void)
95 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
96 __pFilePresentationModel = FileListPresentationModel::GetInstance();
98 SetOrientation(ORIENTATION_AUTOMATIC);
99 AddOrientationEventListener(*this);
101 DeviceManager::AddDeviceEventListener(DEVICE_TYPE_STORAGE_CARD, *this);
102 Header* pHeader = GetHeader();
104 pHeader->SetStyle(HEADER_STYLE_TITLE);
107 itemSave.Construct(IDA_BUTTON_CREATE_NAME_SAVE);
108 itemSave.SetText(ResourceManager::GetString(L"IDS_COM_OPT_SAVE"));
110 GetFooter()->AddItem(itemSave);
111 GetFooter()->AddActionEventListener(*this);
112 GetFooter()->Invalidate(true);
114 SetFormBackEventListener(this);
115 SetActionBarsVisible(FORM_ACTION_BAR_FOOTER, true);
117 __pNameEditField = static_cast<EditField*>(GetControl(L"IDC_EDITFIELD_NAME"));
118 __pNameEditField->AddTextEventListener(*this);
119 __pNameEditField->SetColor(EDIT_STATUS_NORMAL, COLOR_NAME_EDIT_FIELD);
120 __pNameEditField->SetOverlayKeypadCommandButtonVisible(false);
121 __pNameEditField->AddKeypadEventListener(*this);
122 __pNameEditField->AddActionEventListener(*this);
128 AlbumNameEditorForm::OnTextValueChanged(const Control& source)
132 String specialCharacters(L"/");
133 String byteCountForInput;
136 int tempLength = 255;
138 MessageBox messageBox;
141 FooterItemStatus currentStatus = FOOTER_ITEM_STATUS_NORMAL;
143 result r = E_SUCCESS;
145 if (__pNameEditField != null)
147 byteCountForInput = __pNameEditField->GetText();
148 r = utf8.GetByteCount(byteCountForInput, byteCount);
149 AppLogDebug("result is %s",GetErrorMessage(r));
150 AppLogDebug("Byte count is %d",byteCount);
153 if (__pNameEditField != null)
155 currentText = __pNameEditField->GetText();
156 textToCheck = currentText.GetLength() - __textLength;
157 if (currentText.IsEmpty())
159 if (GetFooter() != null)
161 GetFooter()->SetItemEnabled(0, false);
162 GetFooter()->Invalidate(true);
167 if (GetFooter() != null)
169 GetFooter()->GetItemStatus(0, currentStatus);
171 if (currentStatus == FOOTER_ITEM_STATUS_DISABLED)
173 GetFooter()->SetItemEnabled(0,true);
174 GetFooter()->Invalidate(true);
179 if (currentText.StartsWith(".", 0))
181 String invalidCharacterString;
183 String msg = ResourceManager::GetString(L"IDS_COM_POP_INVALID_CHARACTERS");
185 validString = __pNameEditField->GetText();
187 if (validString.GetLength() > 1)
189 validString.Remove(0,1);
190 __pNameEditField->SetText(validString);
194 __pNameEditField->SetText("");
196 if (GetFooter() != null)
198 GetFooter()->SetItemEnabled(0, false);
199 GetFooter()->Invalidate(true);
202 __pNameEditField->ShowKeypad();
205 currentInput = __pNameEditField->GetText();
207 if (currentInput.Contains(specialCharacters[0]))
209 String msg = ResourceManager::GetString(L"IDS_COM_POP_INVALID_CHARACTERS");
211 currentInput.Replace("/", "");
212 __pNameEditField->SetText(currentInput);
214 if (currentInput == L"")
216 if (GetFooter() != null)
218 GetFooter()->SetItemEnabled(0, false);
219 GetFooter()->Invalidate(true);
224 byteCountForInput = __pNameEditField->GetText();
225 r = utf8.GetByteCount(byteCountForInput, inputLength);
227 if ( inputLength == tempLength )
229 String msg = ResourceManager::GetString(L"IDS_COM_POP_MAXIMUM_NUMBER_OF_CHARACTERS_REACHED");
231 __pNameEditField->ShowKeypad();
234 else if (inputLength > tempLength)
236 String maxCharacterString;
237 String currentInputCharacter;
238 int checkByteCount = 0;
239 int lengthOfFinalString = 0;
240 int currentCharacter = 0;
242 String msg = ResourceManager::GetString(L"IDS_COM_POP_MAXIMUM_NUMBER_OF_CHARACTERS_REACHED");
244 currentText = __pNameEditField->GetText();
246 for (currentCharacter=0; checkByteCount < tempLength; ++currentCharacter)
248 currentInputCharacter.Clear();
249 currentInputCharacter = currentText[currentCharacter];
250 r = utf8.GetByteCount(currentInputCharacter, byteCount);
255 AppLogDebug("result of byte count is %s",GetErrorMessage(r));
256 checkByteCount = checkByteCount + byteCount;
258 if (checkByteCount < tempLength)
260 ++lengthOfFinalString;
264 r = __pNameEditField->GetText().SubString(0, lengthOfFinalString, currentInput);
265 AppLogDebug("current input is %S", currentInput.GetPointer());
266 r = __pNameEditField->SetText(currentInput);
267 __pNameEditField->ShowKeypad();
271 r = utf8.GetByteCount(__pNameEditField->GetText(), byteCount);
272 __textLength = byteCount;
275 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
279 AlbumNameEditorForm::OnTextValueChangeCanceled(const Control& source)
281 AppLogDebug("ENTER");
282 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
286 AlbumNameEditorForm::OnTerminating(void)
288 AppLogDebug("ENTER");
289 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
294 void AlbumNameEditorForm::OnFileOpInvalidate(enum FileActionMode actionId)
299 void AlbumNameEditorForm::OnFileOpComplete(enum FileActionMode actionId, enum FileActionCompleteRes res)
302 if (res != COMPLETE_SUCCESS)
304 __pNameEditField->SetText(__nameEditFieldPreText);
305 __pNameEditField->HideKeypad();
306 __pNameEditField->Draw();
307 SceneManager* pSceneManager = SceneManager::GetInstance();
308 pSceneManager->GoBackward(BackwardSceneTransition(__callerSceneId));
313 AlbumNameEditorForm::OnRenameAlbum(String& strOri)
315 AppLogDebug("Rename Album");
319 strOri.IndexOf(DIRECTORY_SEPARATOR, 0, index);
321 strOri.SubString(0, (strOri.GetLength() - index), path);
322 path.Append(__nameEditFieldText);
325 result r = __pPresentationModel->RenameAlbum(strOri, path, this);
329 AppLogDebug("Unable to rename Album %s", GetErrorMessage(r));
330 if (__pMessageBox == null)
332 __pMessageBox = new (std::nothrow) MessageBox();
333 if (r == E_FILE_ALREADY_EXIST)
335 __pMessageBox->Construct(L"", ResourceManager::GetString(L"IDS_MEDIABR_BODY_UNABLE_TO_RENAME_ALBUM_NAME_ALREADY_IN_USE_ABB"),
336 MSGBOX_STYLE_NONE, 3000);
341 __pMessageBox->Construct(L"", ResourceManager::GetString(L"IDS_MEDIABR_POP_UNABLE_TO_RENAME"),
342 MSGBOX_STYLE_NONE, 3000);
346 __pMessageBox->ShowAndWait(modalResult);
348 delete __pMessageBox;
349 __pMessageBox = null;
350 __pNameEditField->SetEnabled(true);
351 __pNameEditField->ShowKeypad();
357 Rectangle clientRect = Form::GetBounds();
358 Rectangle rect(0, 0, clientRect.width, clientRect.height);
359 _pProgressAnim = new (std::nothrow) ProgressAnimation;
360 _pProgressAnim->Construct(rect);
361 AddControl(_pProgressAnim);
362 _pProgressAnim->SetShowState(true);
363 GetFooter()->SetEnabled(false);
364 _pProgressAnim->AnimationStart();
365 _pProgressAnim->Draw();
371 AlbumNameEditorForm::ShowAnimation()
373 Rectangle clientRect= GetClientAreaBounds();
374 Rectangle rect(0, 0, clientRect.width, clientRect.height);
375 _pProgressAnim = new (std::nothrow) ProgressAnimation;
376 _pProgressAnim->Construct(rect);
377 AddControl(_pProgressAnim);
378 _pProgressAnim->SetShowState(true);
379 GetFooter()->SetEnabled(false);
380 _pProgressAnim->AnimationStart();
381 _pProgressAnim->Draw();
386 AlbumNameEditorForm::StopAnimation(void)
390 _pProgressAnim->AnimationStop();
391 _pProgressAnim->SetShowState(false);
392 RemoveControl(_pProgressAnim);
393 GetFooter()->SetEnabled(true);
394 _pProgressAnim = null;
400 AlbumNameEditorForm::OnCreateAlbum(void)
403 path.Append(Tizen::System::Environment::GetMediaPath());
404 path.Append(__nameEditFieldText);
406 if (__previousSceneId != IDSCN_ALL_LIST_EDITOR && __previousSceneId != IDSCN_ALL_LIST_SELECTION)
408 Directory::Create(path, true);
411 __nameEditFieldPreText = __nameEditFieldText;
415 AppLogDebug("EXIT 1(%s)", GetErrorMessage(GetLastResult()));
416 delete __pMoveIndexList;
417 __pMoveIndexList = null;
421 if (__pMoveIndexList == null || __pMoveIndexList->GetCount() <= 0)
423 AppLogDebug("EXIT 2(%s)", GetErrorMessage(GetLastResult()));
424 delete __pMoveIndexList;
425 __pMoveIndexList = null;
430 __pFileMove = new (std::nothrow) FileMoveTimer(path, __pMoveIndexList, __pFilePresentationModel, this);
431 if (__fileActionMode == FILE_ACTION_COPY)
433 AppLogDebug("Rash: Setting to Copy in AlbumNameEditor");
434 __pFileMove->SetCopy();
438 __pFileMove->ClearCopy();
440 result r = __pFileMove->StartTimer();
446 __pNameEditField->SetText(__nameEditFieldPreText);
447 __pNameEditField->HideKeypad();
448 __pNameEditField->Draw();
450 String msg = ResourceManager::GetString(L"IDS_COM_BODY_OPERATION_FAILED");
452 AppLogDebug("MoveToContentFileList content failed (%s)", GetErrorMessage(r));
453 SceneManager* pSceneManager = SceneManager::GetInstance();
454 pSceneManager->GoBackward(BackwardSceneTransition(__callerSceneId));
458 AppLogDebug("MoveToContentFileList content in progress");
460 __fileActionMode = FILE_ACTION_MOVE;
465 AlbumNameEditorForm::OnAlbumRenameComplete(void)
468 _pProgressAnim->AnimationStop();
469 _pProgressAnim->SetShowState(false);
470 RemoveControl(_pProgressAnim);
472 GetFooter()->SetEnabled(true);
473 SceneManager* pSceneManager = SceneManager::GetInstance();
474 pSceneManager->GoBackward((BackwardSceneTransition(IDSCN_ALBUM_LIST)));
478 AlbumNameEditorForm::OnActionPerformed(const Control& source, int actionId)
480 AppLogDebug("ENTER");
482 __nameEditFieldText = __pNameEditField->GetText();
484 if (__albumNameEditorMode == ALBUM_NAME_EDITOR_MODE_RENAME)
486 IList* pAlbumInfoList = __pPresentationModel->GetAlbumInfoList();
487 AlbumInfo* pAlbumInfo = static_cast<AlbumInfo*>(pAlbumInfoList->GetAt(__folderIndex));
488 path = pAlbumInfo->GetDirectory(0);
493 case IDA_BUTTON_CREATE_NAME_SAVE:
495 __pNameEditField->SetEnabled(false);
496 __isCreateAlbum = false;
497 GetFooter()->SetItemEnabled(0, false);
499 if (__albumNameEditorMode == ALBUM_NAME_EDITOR_MODE_CREATE)
501 if (__isKeyPadOpen == true )
503 __pNameEditField->HideKeypad();
504 __isCreateAlbum = true;
512 else if (__albumNameEditorMode == ALBUM_NAME_EDITOR_MODE_RENAME)
514 String albumName = __pNameEditField->GetText();
515 if (__originalText.Equals(albumName, true))
517 SceneManager* pSceneManager = SceneManager::GetInstance();
518 pSceneManager->GoBackward(BackwardSceneTransition());
531 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
535 AlbumNameEditorForm::OnFormBackRequested(Form& source)
537 AppLogDebug("ENTER");
538 if (_pProgressAnim != null)
540 if (_pProgressAnim->GetShowState())
546 if (__albumNameEditorMode == ALBUM_NAME_EDITOR_MODE_CREATE)
548 SceneManager* pSceneManager = SceneManager::GetInstance();
549 pSceneManager->GoForward(ForwardSceneTransition(__callerSceneId));
553 SceneManager* pSceneManager = SceneManager::GetInstance();
554 pSceneManager->GoForward(ForwardSceneTransition(IDSCN_ALBUM_LIST_EDITOR));
556 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
560 AlbumNameEditorForm::OnSceneActivatedN(const SceneId& previousSceneId,
561 const SceneId& currentSceneId, IList* pArgs)
563 AppLogDebug("ENTER");
564 __pPresentationModel = AlbumListPresentationModel::GetInstance();
565 __previousSceneId = previousSceneId;
568 __pPresentationModel->AddContentEventListener(this);
572 Integer* pAlbumTypePtr = static_cast<Integer*>(pArgs->GetAt(ALBUM_FIRST_ELEMENT));
573 if (pAlbumTypePtr->ToInt() == ALBUM_CREATE)
575 AppLogDebug("ALBUM_NAME_EDITOR_MODE_CREATE");
576 __albumNameEditorMode = ALBUM_NAME_EDITOR_MODE_CREATE;
578 else if (pAlbumTypePtr->ToInt() == ALBUM_RENAME)
580 AppLogDebug("ALBUM_NAME_EDITOR_MODE_RENAME");
581 __albumNameEditorMode = ALBUM_NAME_EDITOR_MODE_RENAME;
583 pArgs->RemoveAt(ALBUM_FIRST_ELEMENT, true);
588 Integer* operationTypePtr = static_cast<Integer*>(pArgs->GetAt(ALBUM_FIRST_ELEMENT));
589 if (operationTypePtr->ToInt() == FILE_ACTION_MOVE)
591 AppLogDebug("Rash: FILE_MOVE_ACTION");
592 __fileActionMode = FILE_ACTION_MOVE;
594 else if (operationTypePtr->ToInt() == FILE_ACTION_COPY)
596 AppLogDebug("Rash: FILE_COPY_ACTION");
597 __fileActionMode = FILE_ACTION_COPY;
599 pArgs->RemoveAt(ALBUM_FIRST_ELEMENT, true);
602 if (previousSceneId == IDSCN_ALBUM_LIST_EDITOR)
606 IEnumerator* pEnum = pArgs->GetEnumeratorN();
607 if (pEnum->MoveNext() == E_SUCCESS)
609 Integer *index = static_cast<Integer*>(pEnum->GetCurrent());
612 __folderIndex = index->ToInt();
621 if (previousSceneId == IDSCN_ALL_LIST_EDITOR
622 || previousSceneId == IDSCN_IMAGE_LIST_EDITOR
623 || previousSceneId == IDSCN_VIDEO_LIST_EDITOR
624 || previousSceneId == IDSCN_ALL_LIST_SELECTION)
626 if (previousSceneId == IDSCN_ALL_LIST_EDITOR)
628 AppLogDebug("[MOVE] previousSceneId = IDSCN_ALL_LIST_EDITOR");
630 else if (previousSceneId == IDSCN_ALL_LIST_SELECTION)
632 AppLogDebug("[MOVE] previousSceneId = IDSCN_ALL_LIST_SELECTION");
635 __callerSceneId = previousSceneId;
639 __callerSceneId = EMPTY_SPACE;
642 if (__albumNameEditorMode == ALBUM_NAME_EDITOR_MODE_CREATE)
644 __nameEditFieldText = EMPTY_SPACE;
645 __nameEditFieldPreText = EMPTY_SPACE;
646 __pNameEditField->SetText(__nameEditFieldText);
647 __nameEditFieldTempText = __nameEditFieldText;
648 __pNameEditField->ShowKeypad();
649 __pNameEditField->SetFocus();
651 GetHeader()->SetTitleText(ResourceManager::GetString(L"IDS_MEDIABR_OPT_CREATE_ALBUM"));
652 GetFooter()->SetItemEnabled(0, false);
653 GetFooter()->Invalidate(true);
659 IList* pAlbumInfoList = __pPresentationModel->GetAlbumInfoList();
660 AlbumInfo* pAlbumInfo = static_cast<AlbumInfo*>(pAlbumInfoList->GetAt(__folderIndex));
662 String strName = pAlbumInfo->GetAlbumName();
663 __nameEditFieldText = strName;
664 __nameEditFieldPreText = strName;
665 __pNameEditField->SetText(__nameEditFieldText);
666 __nameEditFieldTempText = __nameEditFieldText;
667 GetHeader()->SetTitleText(ResourceManager::GetString(L"IDS_COM_BODY_RENAME"));
668 __pNameEditField->SetOverlayKeypadCommandButtonVisible(false);
669 __pNameEditField->ShowKeypad();
670 __pNameEditField->SetFocus();
671 __originalText = __pNameEditField->GetText();
672 utf8.GetByteCount(__nameEditFieldText, byteCount);
673 __textLength = byteCount;
674 GetFooter()->SetItemEnabled(0, true);
675 GetFooter()->Invalidate(true);
680 __pMoveIndexList = pArgs;
683 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
687 AlbumNameEditorForm::OnSceneDeactivated(const SceneId& currentSceneId,
688 const SceneId& nextSceneId)
690 AppLogDebug("ENTER");
692 if(__pNameEditField->IsEnabled() == false)
694 __pNameEditField->SetEnabled(true);
697 __isKeyPadOpen = false;
699 __pPresentationModel->RemoveContentEventListener(*this);
700 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
704 AlbumNameEditorForm::OnContentUpdated(void)
706 AppLogDebug("ENTER");
708 if (__mountState == false)
710 SceneManager* pSceneManager = SceneManager::GetInstance();
711 pSceneManager->GoForward(ForwardSceneTransition(IDSCN_ALBUM_LIST));
712 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
716 __mountState = false;
721 AlbumNameEditorForm::OnDeviceStateChanged(DeviceType deviceType, const Tizen::Base::String& state)
723 AppLogDebug("ENTER");
725 if (deviceType == DEVICE_TYPE_STORAGE_CARD && state == DEVICE_STORAGE_CARD_UNMOUNTED)
727 if (__pFileMove && __pFileMove->IsStarted())
729 __pFileMove->Cancel();
733 __mountState = false;
734 SceneManager* pSceneManager = SceneManager::GetInstance();
735 pSceneManager->GoForward(ForwardSceneTransition(IDSCN_ALBUM_LIST));
736 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
742 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
746 AlbumNameEditorForm::CreateMessage(String& str)
748 if (__pMessageBox == null)
750 __pMessageBox = new(std::nothrow) MessageBox;
752 if (__pMessageBox != null)
754 __pMessageBox->Construct(L"", str, MSGBOX_STYLE_OK, 3000);
755 __pMessageBox->ShowAndWait(__modalMsgBoxResult);
757 delete __pMessageBox;
758 __pMessageBox = null;
759 __modalMsgBoxResult = 0;
765 AlbumNameEditorForm::OnOrientationChanged(const Tizen::Ui::Control &source, Tizen::Ui::OrientationStatus orientationStatus)
767 if (_pProgressAnim != null)
769 _pProgressAnim->ChangeOrientation(Form::GetBounds());
770 _pProgressAnim->Invalidate(true);
775 AlbumNameEditorForm::OnKeypadClosed(Tizen::Ui::Control& source)
777 AppLogDebug("ENTER");
779 if ( __isCreateAlbum == true)
782 __isCreateAlbum = false;
784 __isKeyPadOpen = false;
785 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
789 AlbumNameEditorForm::OnKeypadOpened(Tizen::Ui::Control& source)
791 AppLogDebug("ENTER");
792 __isKeyPadOpen = true;
793 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));