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;
50 static const Rectangle RECT_NAME_EDIT_FIELD (0, 0, 720, 100);
51 static const unsigned int COLOR_NAME_EDIT_FIELD = Color32<255, 255, 255>::Value;
52 static const int ALBUM_MAX_LENGTH = 255;
54 AlbumNameEditorForm::AlbumNameEditorForm(void)
55 : __pNameEditField(null)
57 , __modalMsgBoxResult(0)
60 , __albumNameEditorMode(ALBUM_NAME_EDITOR_MODE_RENAME)
62 , __pMoveIndexList(null)
63 , __fileActionMode(FILE_ACTION_MOVE)
65 , __pProgressAnim(null)
66 , __pPresentationModel(null)
67 , __pFilePresentationModel(null)
71 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
74 AlbumNameEditorForm::~AlbumNameEditorForm(void)
77 DeviceManager::RemoveDeviceEventListener(DEVICE_TYPE_STORAGE_CARD, *this);
79 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
83 AlbumNameEditorForm::Initialize(void)
86 result r = Construct(FORM_STYLE_HEADER | FORM_STYLE_FOOTER | FORM_STYLE_NORMAL | FORM_STYLE_INDICATOR);
87 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
93 AlbumNameEditorForm::OnInitializing(void)
96 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
97 __pFilePresentationModel = FileListPresentationModel::GetInstance();
99 DeviceManager::AddDeviceEventListener(DEVICE_TYPE_STORAGE_CARD, *this);
100 Header* pHeader = GetHeader();
102 pHeader->SetStyle(HEADER_STYLE_TITLE);
104 GetFooter()->SetBackButtonEnabled(true);
105 GetFooter()->SetBackButton();
108 itemSave.Construct(IDA_BUTTON_CREATE_NAME_SAVE);
109 itemSave.SetText(ResourceManager::GetString(L"IDS_COM_OPT_SAVE"));
111 GetFooter()->AddItem(itemSave);
112 GetFooter()->AddActionEventListener(*this);
113 GetFooter()->Invalidate(true);
115 SetFormBackEventListener(this);
116 SetActionBarsVisible(FORM_ACTION_BAR_FOOTER, true);
118 __pNameEditField = new (std::nothrow) EditField();
119 __pNameEditField->Construct(RECT_NAME_EDIT_FIELD, EDIT_FIELD_STYLE_NORMAL, INPUT_STYLE_OVERLAY,
120 EDIT_FIELD_TITLE_STYLE_NONE, true, ALBUM_MAX_LENGTH);
121 __pNameEditField->AddTextEventListener(*this);
122 __pNameEditField->SetColor(EDIT_STATUS_NORMAL, COLOR_NAME_EDIT_FIELD);
123 __pNameEditField->SetOverlayKeypadCommandButtonVisible(false);
125 AddControl(__pNameEditField);
126 __pNameEditField->AddActionEventListener(*this);
132 AlbumNameEditorForm::OnTextValueChanged(const Control& source)
136 String specialCharacters(L"/");
137 String byteCountForInput;
140 int tempLength = 255;
142 MessageBox messageBox;
145 FooterItemStatus currentStatus = FOOTER_ITEM_STATUS_NORMAL;
147 result r = E_SUCCESS;
149 if (__pNameEditField != null)
151 byteCountForInput = __pNameEditField->GetText();
152 r = utf8.GetByteCount(byteCountForInput, byteCount);
153 AppLogDebug("result is %s",GetErrorMessage(r));
154 AppLogDebug("Byte count is %d",byteCount);
157 if (__pNameEditField != null)
159 currentText = __pNameEditField->GetText();
160 textToCheck = currentText.GetLength() - __textLength;
161 if (currentText.IsEmpty())
163 if (GetFooter() != null)
165 GetFooter()->SetItemEnabled(0, false);
166 GetFooter()->Invalidate(true);
171 if (GetFooter() != null)
173 GetFooter()->GetItemStatus(0, currentStatus);
175 if (currentStatus == FOOTER_ITEM_STATUS_DISABLED)
177 GetFooter()->SetItemEnabled(0,true);
178 GetFooter()->Invalidate(true);
183 if (currentText.StartsWith(".", 0))
185 String invalidCharacterString;
187 String msg = ResourceManager::GetString(L"IDS_COM_POP_INVALID_CHARACTERS");
189 validString = __pNameEditField->GetText();
191 if (validString.GetLength() > 1)
193 validString.Remove(0,1);
194 __pNameEditField->SetText(validString);
198 __pNameEditField->SetText("");
200 if (GetFooter() != null)
202 GetFooter()->SetItemEnabled(0, false);
203 GetFooter()->Invalidate(true);
206 __pNameEditField->ShowKeypad();
209 currentInput = __pNameEditField->GetText();
211 if (currentInput.Contains(specialCharacters[0]))
213 String msg = ResourceManager::GetString(L"IDS_COM_POP_INVALID_CHARACTERS");
215 currentInput.Replace("/", "");
216 __pNameEditField->SetText(currentInput);
219 inputLength = byteCount;
220 if (inputLength >= tempLength)
222 String maxCharacterString;
223 String currentInputCharacter;
224 int checkByteCount = 0;
225 int lengthOfFinalString = 0;
226 int currentCharacter = 0;
228 String msg = ResourceManager::GetString(L"IDS_COM_POP_MAXIMUM_NUMBER_OF_CHARACTERS_REACHED");
230 currentText = __pNameEditField->GetText();
232 for (currentCharacter=0; checkByteCount < tempLength; currentCharacter++)
234 currentInputCharacter.Clear();
235 currentInputCharacter = currentText[currentCharacter];
236 r = utf8.GetByteCount(currentInputCharacter, byteCount);
241 AppLogDebug("result of byte count is %s",GetErrorMessage(r));
242 checkByteCount = checkByteCount + byteCount;
243 lengthOfFinalString++;
246 r = __pNameEditField->GetText().SubString(0, lengthOfFinalString, currentInput);
247 AppLogDebug("current input is %S", currentInput.GetPointer());
248 r = __pNameEditField->SetText(currentInput);
249 __pNameEditField->ShowKeypad();
253 r = utf8.GetByteCount(__pNameEditField->GetText(), byteCount);
254 __textLength = byteCount;
257 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
261 AlbumNameEditorForm::OnTextValueChangeCanceled(const Control& source)
263 AppLogDebug("ENTER");
264 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
268 AlbumNameEditorForm::OnTerminating(void)
270 AppLogDebug("ENTER");
271 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
276 void AlbumNameEditorForm::OnFileOpInvalidate(enum FileActionMode actionId)
281 void AlbumNameEditorForm::OnFileOpComplete(enum FileActionMode actionId, enum FileActionCompleteRes res)
284 if (res != COMPLETE_SUCCESS)
286 __pNameEditField->SetText(__nameEditFieldPreText);
287 __pNameEditField->HideKeypad();
288 __pNameEditField->Draw();
289 SceneManager* pSceneManager = SceneManager::GetInstance();
290 pSceneManager->GoBackward(BackwardSceneTransition(__callerSceneId));
295 AlbumNameEditorForm::OnRenameAlbum(String& strOri)
297 AppLogDebug("Rename Album");
301 strOri.IndexOf(DIRECTORY_SEPARATOR, 0, index);
303 strOri.SubString(0, (strOri.GetLength() - index), path);
304 path.Append(__nameEditFieldText);
307 result r = __pPresentationModel->RenameAlbum(strOri, path, this);
311 AppLogDebug("Unable to rename Album %s", GetErrorMessage(r));
312 if (__pMessageBox == null)
314 __pMessageBox = new (std::nothrow) MessageBox();
315 if (r == E_FILE_ALREADY_EXIST)
317 __pMessageBox->Construct(L"", ResourceManager::GetString(L"IDS_MEDIABR_BODY_UNABLE_TO_RENAME_ALBUM_NAME_ALREADY_IN_USE_ABB"),
318 MSGBOX_STYLE_NONE, 3000);
322 __pMessageBox->Construct(L"", ResourceManager::GetString(L"IDS_MEDIABR_POP_UNABLE_TO_RENAME"),
323 MSGBOX_STYLE_NONE, 3000);
327 __pMessageBox->ShowAndWait(modalResult);
329 delete __pMessageBox;
330 __pMessageBox = null;
336 Rectangle clientRect= GetClientAreaBounds();
337 Rectangle rect(0, 0, clientRect.width, clientRect.height);
338 __pProgressAnim = new (std::nothrow) ProgressAnimation;
339 __pProgressAnim->Construct(rect);
340 AddControl(__pProgressAnim);
341 __pProgressAnim->SetShowState(true);
342 GetFooter()->SetEnabled(false);
343 __pProgressAnim->AnimationStart();
344 __pProgressAnim->Draw();
350 AlbumNameEditorForm::OnCreateAlbum(void)
353 __pNameEditField->HideKeypad();
354 __pNameEditField->RequestRedraw();
355 path.Append(Tizen::System::Environment::GetMediaPath());
356 path.Append(__nameEditFieldText);
358 if (__previousSceneId != IDSCN_ALL_LIST_EDITOR && __previousSceneId != IDSCN_ALL_LIST_SELECTION)
360 Directory::Create(path, true);
363 __nameEditFieldPreText = __nameEditFieldText;
367 AppLogDebug("EXIT 1(%s)", GetErrorMessage(GetLastResult()));
368 delete __pMoveIndexList;
369 __pMoveIndexList = null;
373 if (__pMoveIndexList == null || __pMoveIndexList->GetCount() <= 0)
375 AppLogDebug("EXIT 2(%s)", GetErrorMessage(GetLastResult()));
376 delete __pMoveIndexList;
377 __pMoveIndexList = null;
382 GetMoveFileIndexList(path, __pMoveIndexList, __pFilePresentationModel);
383 __pFileMove = new (std::nothrow) FileMoveTimer(path, __pMoveIndexList, __pFilePresentationModel, this);
384 if (__fileActionMode == FILE_ACTION_COPY)
386 AppLogDebug("Rash: Setting to Copy in AlbumNameEditor");
387 __pFileMove->SetCopy();
391 __pFileMove->ClearCopy();
393 result r = __pFileMove->StartTimer();
399 __pNameEditField->SetText(__nameEditFieldPreText);
400 __pNameEditField->HideKeypad();
401 __pNameEditField->Draw();
402 AppLogDebug("MoveToContentFileList content failed (%s)", GetErrorMessage(r));
403 SceneManager* pSceneManager = SceneManager::GetInstance();
404 pSceneManager->GoBackward(BackwardSceneTransition(__callerSceneId));
408 AppLogDebug("MoveToContentFileList content in progress");
410 __fileActionMode = FILE_ACTION_MOVE;
414 void AlbumNameEditorForm::OnAlbumRenameComplete(void)
417 __pProgressAnim->AnimationStop();
418 __pProgressAnim->SetShowState(false);
419 RemoveControl(__pProgressAnim);
421 GetFooter()->SetEnabled(true);
422 SceneManager* pSceneManager = SceneManager::GetInstance();
423 pSceneManager->GoBackward((BackwardSceneTransition(IDSCN_ALBUM_LIST)));
427 AlbumNameEditorForm::OnActionPerformed(const Control& source, int actionId)
429 AppLogDebug("ENTER");
431 __nameEditFieldText = __pNameEditField->GetText();
433 if (__albumNameEditorMode == ALBUM_NAME_EDITOR_MODE_RENAME)
435 IList* pAlbumInfoList = __pPresentationModel->GetAlbumInfoList();
436 AlbumInfo* pAlbumInfo = static_cast<AlbumInfo*>(pAlbumInfoList->GetAt(__folderIndex));
437 path = pAlbumInfo->GetDirectory(0);
442 case IDA_BUTTON_CREATE_NAME_SAVE:
444 __pNameEditField->SetEnabled(false);
446 if (__albumNameEditorMode == ALBUM_NAME_EDITOR_MODE_CREATE)
450 else if (__albumNameEditorMode == ALBUM_NAME_EDITOR_MODE_RENAME)
452 String albumName = __pNameEditField->GetText();
453 if (__originalText.Equals(albumName , true))
455 SceneManager* pSceneManager = SceneManager::GetInstance();
456 pSceneManager->GoBackward(BackwardSceneTransition());
469 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
473 AlbumNameEditorForm::OnFormBackRequested(Form& source)
475 AppLogDebug("ENTER");
476 if (__albumNameEditorMode == ALBUM_NAME_EDITOR_MODE_CREATE)
478 SceneManager* pSceneManager = SceneManager::GetInstance();
479 pSceneManager->GoForward(ForwardSceneTransition(__callerSceneId));
483 SceneManager* pSceneManager = SceneManager::GetInstance();
484 pSceneManager->GoForward(ForwardSceneTransition(IDSCN_ALBUM_LIST_EDITOR));
486 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
490 AlbumNameEditorForm::OnSceneActivatedN(const SceneId& previousSceneId,
491 const SceneId& currentSceneId, IList* pArgs)
493 AppLogDebug("ENTER");
494 __pPresentationModel = AlbumListPresentationModel::GetInstance();
495 __previousSceneId = previousSceneId;
497 __pNameEditField->SetEnabled(true);
498 __pPresentationModel->AddContentEventListener(this);
502 Integer* pAlbumTypePtr = static_cast<Integer*>(pArgs->GetAt(ALBUM_FIRST_ELEMENT));
503 if (pAlbumTypePtr->ToInt() == ALBUM_CREATE)
505 AppLogDebug("ALBUM_NAME_EDITOR_MODE_CREATE");
506 __albumNameEditorMode = ALBUM_NAME_EDITOR_MODE_CREATE;
508 else if (pAlbumTypePtr->ToInt() == ALBUM_RENAME)
510 AppLogDebug("ALBUM_NAME_EDITOR_MODE_RENAME");
511 __albumNameEditorMode = ALBUM_NAME_EDITOR_MODE_RENAME;
513 pArgs->RemoveAt(ALBUM_FIRST_ELEMENT, true);
518 Integer* operationTypePtr = static_cast<Integer*>(pArgs->GetAt(ALBUM_FIRST_ELEMENT));
519 if (operationTypePtr->ToInt() == FILE_ACTION_MOVE)
521 AppLogDebug("Rash: FILE_MOVE_ACTION");
522 __fileActionMode = FILE_ACTION_MOVE;
524 else if (operationTypePtr->ToInt() == FILE_ACTION_COPY)
526 AppLogDebug("Rash: FILE_COPY_ACTION");
527 __fileActionMode = FILE_ACTION_COPY;
529 pArgs->RemoveAt(ALBUM_FIRST_ELEMENT, true);
532 if (previousSceneId == IDSCN_ALBUM_LIST_EDITOR)
536 IEnumerator* pEnum = pArgs->GetEnumeratorN();
537 if (pEnum->MoveNext() == E_SUCCESS)
539 Integer *index = static_cast<Integer*>(pEnum->GetCurrent());
542 __folderIndex = index->ToInt();
550 if (previousSceneId == IDSCN_ALL_LIST_EDITOR
551 || previousSceneId == IDSCN_IMAGE_LIST_EDITOR
552 || previousSceneId == IDSCN_VIDEO_LIST_EDITOR
553 || previousSceneId == IDSCN_ALL_LIST_SELECTION)
555 if (previousSceneId == IDSCN_ALL_LIST_EDITOR)
557 AppLogDebug("[MOVE] previousSceneId = IDSCN_ALL_LIST_EDITOR");
559 else if (previousSceneId == IDSCN_ALL_LIST_SELECTION)
561 AppLogDebug("[MOVE] previousSceneId = IDSCN_ALL_LIST_SELECTION");
564 __callerSceneId = previousSceneId;
568 __callerSceneId = EMPTY_SPACE;
571 if (__albumNameEditorMode == ALBUM_NAME_EDITOR_MODE_CREATE)
573 __nameEditFieldText = EMPTY_SPACE;
574 __nameEditFieldPreText = EMPTY_SPACE;
575 __pNameEditField->SetText(__nameEditFieldText);
576 __nameEditFieldTempText = __nameEditFieldText;
577 __pNameEditField->ShowKeypad();
578 __pNameEditField->SetFocus();
580 GetHeader()->SetTitleText(ResourceManager::GetString(L"IDS_MEDIABR_OPT_CREATE_ALBUM"));
581 GetFooter()->SetItemEnabled(0, false);
582 GetFooter()->Invalidate(true);
588 IList* pAlbumInfoList = __pPresentationModel->GetAlbumInfoList();
589 AlbumInfo* pAlbumInfo = static_cast<AlbumInfo*>(pAlbumInfoList->GetAt(__folderIndex));
591 String strName = pAlbumInfo->GetAlbumName();
592 __nameEditFieldText = strName;
593 __nameEditFieldPreText = strName;
594 __pNameEditField->SetText(__nameEditFieldText);
595 __nameEditFieldTempText = __nameEditFieldText;
596 GetHeader()->SetTitleText(ResourceManager::GetString(L"IDS_COM_BODY_RENAME"));
597 __pNameEditField->SetOverlayKeypadCommandButtonVisible(false);
598 __pNameEditField->ShowKeypad();
599 __pNameEditField->SetFocus();
600 __originalText = __pNameEditField->GetText();
601 utf8.GetByteCount(__nameEditFieldText, byteCount);
602 __textLength = byteCount;
603 GetFooter()->SetItemEnabled(0, true);
604 GetFooter()->Invalidate(true);
609 __pMoveIndexList = pArgs;
612 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
616 AlbumNameEditorForm::OnSceneDeactivated(const SceneId& currentSceneId,
617 const SceneId& nextSceneId)
619 AppLogDebug("ENTER");
620 __pPresentationModel->RemoveContentEventListener(*this);
621 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
625 AlbumNameEditorForm::OnContentUpdated(void)
627 AppLogDebug("ENTER");
629 if ( __mountState == false )
631 SceneManager* pSceneManager = SceneManager::GetInstance();
632 pSceneManager->GoForward(ForwardSceneTransition(IDSCN_ALBUM_LIST));
633 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
637 __mountState = false;
642 AlbumNameEditorForm::OnDeviceStateChanged(DeviceType deviceType, const Tizen::Base::String& state)
644 AppLogDebug("ENTER");
646 if (deviceType == DEVICE_TYPE_STORAGE_CARD && state == DEVICE_STORAGE_CARD_UNMOUNTED)
648 if ( __pFileMove && __pFileMove->IsStarted() )
650 __pFileMove->Cancel();
654 __mountState = false;
655 SceneManager* pSceneManager = SceneManager::GetInstance();
656 pSceneManager->GoForward(ForwardSceneTransition(IDSCN_ALBUM_LIST));
657 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
663 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
667 AlbumNameEditorForm::CreateMessage(String& str)
669 if (__pMessageBox == null)
671 __pMessageBox = new(std::nothrow) MessageBox;
673 if (__pMessageBox != null)
675 __pMessageBox->Construct(L"", str, MSGBOX_STYLE_OK, 3000);
676 __pMessageBox->ShowAndWait(__modalMsgBoxResult);
678 delete __pMessageBox;
679 __pMessageBox = null;
680 __modalMsgBoxResult = 0;