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;
244 if( checkByteCount < tempLength)
246 lengthOfFinalString++;
250 r = __pNameEditField->GetText().SubString(0, lengthOfFinalString, currentInput);
251 AppLogDebug("current input is %S", currentInput.GetPointer());
252 r = __pNameEditField->SetText(currentInput);
253 __pNameEditField->ShowKeypad();
257 r = utf8.GetByteCount(__pNameEditField->GetText(), byteCount);
258 __textLength = byteCount;
261 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
265 AlbumNameEditorForm::OnTextValueChangeCanceled(const Control& source)
267 AppLogDebug("ENTER");
268 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
272 AlbumNameEditorForm::OnTerminating(void)
274 AppLogDebug("ENTER");
275 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
280 void AlbumNameEditorForm::OnFileOpInvalidate(enum FileActionMode actionId)
285 void AlbumNameEditorForm::OnFileOpComplete(enum FileActionMode actionId, enum FileActionCompleteRes res)
288 if (res != COMPLETE_SUCCESS)
290 __pNameEditField->SetText(__nameEditFieldPreText);
291 __pNameEditField->HideKeypad();
292 __pNameEditField->Draw();
293 SceneManager* pSceneManager = SceneManager::GetInstance();
294 pSceneManager->GoBackward(BackwardSceneTransition(__callerSceneId));
299 AlbumNameEditorForm::OnRenameAlbum(String& strOri)
301 AppLogDebug("Rename Album");
305 strOri.IndexOf(DIRECTORY_SEPARATOR, 0, index);
307 strOri.SubString(0, (strOri.GetLength() - index), path);
308 path.Append(__nameEditFieldText);
311 result r = __pPresentationModel->RenameAlbum(strOri, path, this);
315 AppLogDebug("Unable to rename Album %s", GetErrorMessage(r));
316 if (__pMessageBox == null)
318 __pMessageBox = new (std::nothrow) MessageBox();
319 if (r == E_FILE_ALREADY_EXIST)
321 __pMessageBox->Construct(L"", ResourceManager::GetString(L"IDS_MEDIABR_BODY_UNABLE_TO_RENAME_ALBUM_NAME_ALREADY_IN_USE_ABB"),
322 MSGBOX_STYLE_NONE, 3000);
326 __pMessageBox->Construct(L"", ResourceManager::GetString(L"IDS_MEDIABR_POP_UNABLE_TO_RENAME"),
327 MSGBOX_STYLE_NONE, 3000);
331 __pMessageBox->ShowAndWait(modalResult);
333 delete __pMessageBox;
334 __pMessageBox = null;
340 Rectangle clientRect= GetClientAreaBounds();
341 Rectangle rect(0, 0, clientRect.width, clientRect.height);
342 __pProgressAnim = new (std::nothrow) ProgressAnimation;
343 __pProgressAnim->Construct(rect);
344 AddControl(__pProgressAnim);
345 __pProgressAnim->SetShowState(true);
346 GetFooter()->SetEnabled(false);
347 __pProgressAnim->AnimationStart();
348 __pProgressAnim->Draw();
354 AlbumNameEditorForm::OnCreateAlbum(void)
357 __pNameEditField->HideKeypad();
358 __pNameEditField->RequestRedraw();
359 path.Append(Tizen::System::Environment::GetMediaPath());
360 path.Append(__nameEditFieldText);
362 if (__previousSceneId != IDSCN_ALL_LIST_EDITOR && __previousSceneId != IDSCN_ALL_LIST_SELECTION)
364 Directory::Create(path, true);
367 __nameEditFieldPreText = __nameEditFieldText;
371 AppLogDebug("EXIT 1(%s)", GetErrorMessage(GetLastResult()));
372 delete __pMoveIndexList;
373 __pMoveIndexList = null;
377 if (__pMoveIndexList == null || __pMoveIndexList->GetCount() <= 0)
379 AppLogDebug("EXIT 2(%s)", GetErrorMessage(GetLastResult()));
380 delete __pMoveIndexList;
381 __pMoveIndexList = null;
386 GetMoveFileIndexList(path, __pMoveIndexList, __pFilePresentationModel);
387 __pFileMove = new (std::nothrow) FileMoveTimer(path, __pMoveIndexList, __pFilePresentationModel, this);
388 if (__fileActionMode == FILE_ACTION_COPY)
390 AppLogDebug("Rash: Setting to Copy in AlbumNameEditor");
391 __pFileMove->SetCopy();
395 __pFileMove->ClearCopy();
397 result r = __pFileMove->StartTimer();
403 __pNameEditField->SetText(__nameEditFieldPreText);
404 __pNameEditField->HideKeypad();
405 __pNameEditField->Draw();
406 AppLogDebug("MoveToContentFileList content failed (%s)", GetErrorMessage(r));
407 SceneManager* pSceneManager = SceneManager::GetInstance();
408 pSceneManager->GoBackward(BackwardSceneTransition(__callerSceneId));
412 AppLogDebug("MoveToContentFileList content in progress");
414 __fileActionMode = FILE_ACTION_MOVE;
418 void AlbumNameEditorForm::OnAlbumRenameComplete(void)
421 __pProgressAnim->AnimationStop();
422 __pProgressAnim->SetShowState(false);
423 RemoveControl(__pProgressAnim);
425 GetFooter()->SetEnabled(true);
426 SceneManager* pSceneManager = SceneManager::GetInstance();
427 pSceneManager->GoBackward((BackwardSceneTransition(IDSCN_ALBUM_LIST)));
431 AlbumNameEditorForm::OnActionPerformed(const Control& source, int actionId)
433 AppLogDebug("ENTER");
435 __nameEditFieldText = __pNameEditField->GetText();
437 if (__albumNameEditorMode == ALBUM_NAME_EDITOR_MODE_RENAME)
439 IList* pAlbumInfoList = __pPresentationModel->GetAlbumInfoList();
440 AlbumInfo* pAlbumInfo = static_cast<AlbumInfo*>(pAlbumInfoList->GetAt(__folderIndex));
441 path = pAlbumInfo->GetDirectory(0);
446 case IDA_BUTTON_CREATE_NAME_SAVE:
448 __pNameEditField->SetEnabled(false);
450 if (__albumNameEditorMode == ALBUM_NAME_EDITOR_MODE_CREATE)
454 else if (__albumNameEditorMode == ALBUM_NAME_EDITOR_MODE_RENAME)
456 String albumName = __pNameEditField->GetText();
457 if (__originalText.Equals(albumName , true))
459 SceneManager* pSceneManager = SceneManager::GetInstance();
460 pSceneManager->GoBackward(BackwardSceneTransition());
473 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
477 AlbumNameEditorForm::OnFormBackRequested(Form& source)
479 AppLogDebug("ENTER");
480 if (__albumNameEditorMode == ALBUM_NAME_EDITOR_MODE_CREATE)
482 SceneManager* pSceneManager = SceneManager::GetInstance();
483 pSceneManager->GoForward(ForwardSceneTransition(__callerSceneId));
487 SceneManager* pSceneManager = SceneManager::GetInstance();
488 pSceneManager->GoForward(ForwardSceneTransition(IDSCN_ALBUM_LIST_EDITOR));
490 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
494 AlbumNameEditorForm::OnSceneActivatedN(const SceneId& previousSceneId,
495 const SceneId& currentSceneId, IList* pArgs)
497 AppLogDebug("ENTER");
498 __pPresentationModel = AlbumListPresentationModel::GetInstance();
499 __previousSceneId = previousSceneId;
501 __pNameEditField->SetEnabled(true);
502 __pPresentationModel->AddContentEventListener(this);
506 Integer* pAlbumTypePtr = static_cast<Integer*>(pArgs->GetAt(ALBUM_FIRST_ELEMENT));
507 if (pAlbumTypePtr->ToInt() == ALBUM_CREATE)
509 AppLogDebug("ALBUM_NAME_EDITOR_MODE_CREATE");
510 __albumNameEditorMode = ALBUM_NAME_EDITOR_MODE_CREATE;
512 else if (pAlbumTypePtr->ToInt() == ALBUM_RENAME)
514 AppLogDebug("ALBUM_NAME_EDITOR_MODE_RENAME");
515 __albumNameEditorMode = ALBUM_NAME_EDITOR_MODE_RENAME;
517 pArgs->RemoveAt(ALBUM_FIRST_ELEMENT, true);
522 Integer* operationTypePtr = static_cast<Integer*>(pArgs->GetAt(ALBUM_FIRST_ELEMENT));
523 if (operationTypePtr->ToInt() == FILE_ACTION_MOVE)
525 AppLogDebug("Rash: FILE_MOVE_ACTION");
526 __fileActionMode = FILE_ACTION_MOVE;
528 else if (operationTypePtr->ToInt() == FILE_ACTION_COPY)
530 AppLogDebug("Rash: FILE_COPY_ACTION");
531 __fileActionMode = FILE_ACTION_COPY;
533 pArgs->RemoveAt(ALBUM_FIRST_ELEMENT, true);
536 if (previousSceneId == IDSCN_ALBUM_LIST_EDITOR)
540 IEnumerator* pEnum = pArgs->GetEnumeratorN();
541 if (pEnum->MoveNext() == E_SUCCESS)
543 Integer *index = static_cast<Integer*>(pEnum->GetCurrent());
546 __folderIndex = index->ToInt();
554 if (previousSceneId == IDSCN_ALL_LIST_EDITOR
555 || previousSceneId == IDSCN_IMAGE_LIST_EDITOR
556 || previousSceneId == IDSCN_VIDEO_LIST_EDITOR
557 || previousSceneId == IDSCN_ALL_LIST_SELECTION)
559 if (previousSceneId == IDSCN_ALL_LIST_EDITOR)
561 AppLogDebug("[MOVE] previousSceneId = IDSCN_ALL_LIST_EDITOR");
563 else if (previousSceneId == IDSCN_ALL_LIST_SELECTION)
565 AppLogDebug("[MOVE] previousSceneId = IDSCN_ALL_LIST_SELECTION");
568 __callerSceneId = previousSceneId;
572 __callerSceneId = EMPTY_SPACE;
575 if (__albumNameEditorMode == ALBUM_NAME_EDITOR_MODE_CREATE)
577 __nameEditFieldText = EMPTY_SPACE;
578 __nameEditFieldPreText = EMPTY_SPACE;
579 __pNameEditField->SetText(__nameEditFieldText);
580 __nameEditFieldTempText = __nameEditFieldText;
581 __pNameEditField->ShowKeypad();
582 __pNameEditField->SetFocus();
584 GetHeader()->SetTitleText(ResourceManager::GetString(L"IDS_MEDIABR_OPT_CREATE_ALBUM"));
585 GetFooter()->SetItemEnabled(0, false);
586 GetFooter()->Invalidate(true);
592 IList* pAlbumInfoList = __pPresentationModel->GetAlbumInfoList();
593 AlbumInfo* pAlbumInfo = static_cast<AlbumInfo*>(pAlbumInfoList->GetAt(__folderIndex));
595 String strName = pAlbumInfo->GetAlbumName();
596 __nameEditFieldText = strName;
597 __nameEditFieldPreText = strName;
598 __pNameEditField->SetText(__nameEditFieldText);
599 __nameEditFieldTempText = __nameEditFieldText;
600 GetHeader()->SetTitleText(ResourceManager::GetString(L"IDS_COM_BODY_RENAME"));
601 __pNameEditField->SetOverlayKeypadCommandButtonVisible(false);
602 __pNameEditField->ShowKeypad();
603 __pNameEditField->SetFocus();
604 __originalText = __pNameEditField->GetText();
605 utf8.GetByteCount(__nameEditFieldText, byteCount);
606 __textLength = byteCount;
607 GetFooter()->SetItemEnabled(0, true);
608 GetFooter()->Invalidate(true);
613 __pMoveIndexList = pArgs;
616 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
620 AlbumNameEditorForm::OnSceneDeactivated(const SceneId& currentSceneId,
621 const SceneId& nextSceneId)
623 AppLogDebug("ENTER");
624 __pPresentationModel->RemoveContentEventListener(*this);
625 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
629 AlbumNameEditorForm::OnContentUpdated(void)
631 AppLogDebug("ENTER");
633 if ( __mountState == false )
635 SceneManager* pSceneManager = SceneManager::GetInstance();
636 pSceneManager->GoForward(ForwardSceneTransition(IDSCN_ALBUM_LIST));
637 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
641 __mountState = false;
646 AlbumNameEditorForm::OnDeviceStateChanged(DeviceType deviceType, const Tizen::Base::String& state)
648 AppLogDebug("ENTER");
650 if (deviceType == DEVICE_TYPE_STORAGE_CARD && state == DEVICE_STORAGE_CARD_UNMOUNTED)
652 if ( __pFileMove && __pFileMove->IsStarted() )
654 __pFileMove->Cancel();
658 __mountState = false;
659 SceneManager* pSceneManager = SceneManager::GetInstance();
660 pSceneManager->GoForward(ForwardSceneTransition(IDSCN_ALBUM_LIST));
661 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
667 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
671 AlbumNameEditorForm::CreateMessage(String& str)
673 if (__pMessageBox == null)
675 __pMessageBox = new(std::nothrow) MessageBox;
677 if (__pMessageBox != null)
679 __pMessageBox->Construct(L"", str, MSGBOX_STYLE_OK, 3000);
680 __pMessageBox->ShowAndWait(__modalMsgBoxResult);
682 delete __pMessageBox;
683 __pMessageBox = null;
684 __modalMsgBoxResult = 0;