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 , __albumNameEditorMode(ALBUM_NAME_EDITOR_MODE_RENAME)
59 , __pMoveIndexList(null)
60 , __fileActionMode(FILE_ACTION_MOVE)
62 , __pProgressAnim(null)
63 , __pPresentationModel(null)
64 , __pFilePresentationModel(null)
68 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
71 AlbumNameEditorForm::~AlbumNameEditorForm(void)
74 DeviceManager::RemoveDeviceEventListener(DEVICE_TYPE_STORAGE_CARD, *this);
76 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
80 AlbumNameEditorForm::Initialize(void)
83 result r = Construct(L"IDL_FORM_ALBUM_NAME_EDITOR");
84 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
90 AlbumNameEditorForm::OnInitializing(void)
93 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
94 __pFilePresentationModel = FileListPresentationModel::GetInstance();
96 SetOrientation(ORIENTATION_AUTOMATIC);
97 AddOrientationEventListener(*this);
99 DeviceManager::AddDeviceEventListener(DEVICE_TYPE_STORAGE_CARD, *this);
100 Header* pHeader = GetHeader();
102 pHeader->SetStyle(HEADER_STYLE_TITLE);
105 itemSave.Construct(IDA_BUTTON_CREATE_NAME_SAVE);
106 itemSave.SetText(ResourceManager::GetString(L"IDS_COM_OPT_SAVE"));
108 GetFooter()->AddItem(itemSave);
109 GetFooter()->AddActionEventListener(*this);
110 GetFooter()->Invalidate(true);
112 SetFormBackEventListener(this);
113 SetActionBarsVisible(FORM_ACTION_BAR_FOOTER, true);
115 __pNameEditField = static_cast<EditField*>(GetControl(L"IDC_EDITFIELD_NAME"));
116 __pNameEditField->AddTextEventListener(*this);
117 __pNameEditField->SetColor(EDIT_STATUS_NORMAL, COLOR_NAME_EDIT_FIELD);
118 __pNameEditField->SetOverlayKeypadCommandButtonVisible(false);
119 __pNameEditField->AddActionEventListener(*this);
125 AlbumNameEditorForm::OnTextValueChanged(const Control& source)
129 String specialCharacters(L"/");
130 String byteCountForInput;
133 int tempLength = 255;
135 MessageBox messageBox;
138 FooterItemStatus currentStatus = FOOTER_ITEM_STATUS_NORMAL;
140 result r = E_SUCCESS;
142 if (__pNameEditField != null)
144 byteCountForInput = __pNameEditField->GetText();
145 r = utf8.GetByteCount(byteCountForInput, byteCount);
146 AppLogDebug("result is %s",GetErrorMessage(r));
147 AppLogDebug("Byte count is %d",byteCount);
150 if (__pNameEditField != null)
152 currentText = __pNameEditField->GetText();
153 textToCheck = currentText.GetLength() - __textLength;
154 if (currentText.IsEmpty())
156 if (GetFooter() != null)
158 GetFooter()->SetItemEnabled(0, false);
159 GetFooter()->Invalidate(true);
164 if (GetFooter() != null)
166 GetFooter()->GetItemStatus(0, currentStatus);
168 if (currentStatus == FOOTER_ITEM_STATUS_DISABLED)
170 GetFooter()->SetItemEnabled(0,true);
171 GetFooter()->Invalidate(true);
176 if (currentText.StartsWith(".", 0))
178 String invalidCharacterString;
180 String msg = ResourceManager::GetString(L"IDS_COM_POP_INVALID_CHARACTERS");
182 validString = __pNameEditField->GetText();
184 if (validString.GetLength() > 1)
186 validString.Remove(0,1);
187 __pNameEditField->SetText(validString);
191 __pNameEditField->SetText("");
193 if (GetFooter() != null)
195 GetFooter()->SetItemEnabled(0, false);
196 GetFooter()->Invalidate(true);
199 __pNameEditField->ShowKeypad();
202 currentInput = __pNameEditField->GetText();
204 if (currentInput.Contains(specialCharacters[0]))
206 String msg = ResourceManager::GetString(L"IDS_COM_POP_INVALID_CHARACTERS");
208 currentInput.Replace("/", "");
209 __pNameEditField->SetText(currentInput);
212 inputLength = byteCount;
213 if (inputLength >= tempLength)
215 String maxCharacterString;
216 String currentInputCharacter;
217 int checkByteCount = 0;
218 int lengthOfFinalString = 0;
219 int currentCharacter = 0;
221 String msg = ResourceManager::GetString(L"IDS_COM_POP_MAXIMUM_NUMBER_OF_CHARACTERS_REACHED");
223 currentText = __pNameEditField->GetText();
225 for (currentCharacter=0; checkByteCount < tempLength; ++currentCharacter)
227 currentInputCharacter.Clear();
228 currentInputCharacter = currentText[currentCharacter];
229 r = utf8.GetByteCount(currentInputCharacter, byteCount);
234 AppLogDebug("result of byte count is %s",GetErrorMessage(r));
235 checkByteCount = checkByteCount + byteCount;
237 if (checkByteCount < tempLength)
239 ++lengthOfFinalString;
243 r = __pNameEditField->GetText().SubString(0, lengthOfFinalString, currentInput);
244 AppLogDebug("current input is %S", currentInput.GetPointer());
245 r = __pNameEditField->SetText(currentInput);
246 __pNameEditField->ShowKeypad();
250 r = utf8.GetByteCount(__pNameEditField->GetText(), byteCount);
251 __textLength = byteCount;
254 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
258 AlbumNameEditorForm::OnTextValueChangeCanceled(const Control& source)
260 AppLogDebug("ENTER");
261 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
265 AlbumNameEditorForm::OnTerminating(void)
267 AppLogDebug("ENTER");
268 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
273 void AlbumNameEditorForm::OnFileOpInvalidate(enum FileActionMode actionId)
278 void AlbumNameEditorForm::OnFileOpComplete(enum FileActionMode actionId, enum FileActionCompleteRes res)
281 if (res != COMPLETE_SUCCESS)
283 __pNameEditField->SetText(__nameEditFieldPreText);
284 __pNameEditField->HideKeypad();
285 __pNameEditField->Draw();
286 SceneManager* pSceneManager = SceneManager::GetInstance();
287 pSceneManager->GoBackward(BackwardSceneTransition(__callerSceneId));
292 AlbumNameEditorForm::OnRenameAlbum(String& strOri)
294 AppLogDebug("Rename Album");
298 strOri.IndexOf(DIRECTORY_SEPARATOR, 0, index);
300 strOri.SubString(0, (strOri.GetLength() - index), path);
301 path.Append(__nameEditFieldText);
304 result r = __pPresentationModel->RenameAlbum(strOri, path, this);
308 AppLogDebug("Unable to rename Album %s", GetErrorMessage(r));
309 if (__pMessageBox == null)
311 __pMessageBox = new (std::nothrow) MessageBox();
312 if (r == E_FILE_ALREADY_EXIST)
314 __pMessageBox->Construct(L"", ResourceManager::GetString(L"IDS_MEDIABR_BODY_UNABLE_TO_RENAME_ALBUM_NAME_ALREADY_IN_USE_ABB"),
315 MSGBOX_STYLE_NONE, 3000);
320 __pMessageBox->Construct(L"", ResourceManager::GetString(L"IDS_MEDIABR_POP_UNABLE_TO_RENAME"),
321 MSGBOX_STYLE_NONE, 3000);
325 __pMessageBox->ShowAndWait(modalResult);
327 delete __pMessageBox;
328 __pMessageBox = null;
329 __pNameEditField->SetEnabled(true);
330 __pNameEditField->ShowKeypad();
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 (__pProgressAnim != null)
478 if (__pProgressAnim->GetShowState())
484 if (__albumNameEditorMode == ALBUM_NAME_EDITOR_MODE_CREATE)
486 SceneManager* pSceneManager = SceneManager::GetInstance();
487 pSceneManager->GoForward(ForwardSceneTransition(__callerSceneId));
491 SceneManager* pSceneManager = SceneManager::GetInstance();
492 pSceneManager->GoForward(ForwardSceneTransition(IDSCN_ALBUM_LIST_EDITOR));
494 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
498 AlbumNameEditorForm::OnSceneActivatedN(const SceneId& previousSceneId,
499 const SceneId& currentSceneId, IList* pArgs)
501 AppLogDebug("ENTER");
502 __pPresentationModel = AlbumListPresentationModel::GetInstance();
503 __previousSceneId = previousSceneId;
505 __pNameEditField->SetEnabled(true);
506 __pPresentationModel->AddContentEventListener(this);
510 Integer* pAlbumTypePtr = static_cast<Integer*>(pArgs->GetAt(ALBUM_FIRST_ELEMENT));
511 if (pAlbumTypePtr->ToInt() == ALBUM_CREATE)
513 AppLogDebug("ALBUM_NAME_EDITOR_MODE_CREATE");
514 __albumNameEditorMode = ALBUM_NAME_EDITOR_MODE_CREATE;
516 else if (pAlbumTypePtr->ToInt() == ALBUM_RENAME)
518 AppLogDebug("ALBUM_NAME_EDITOR_MODE_RENAME");
519 __albumNameEditorMode = ALBUM_NAME_EDITOR_MODE_RENAME;
521 pArgs->RemoveAt(ALBUM_FIRST_ELEMENT, true);
526 Integer* operationTypePtr = static_cast<Integer*>(pArgs->GetAt(ALBUM_FIRST_ELEMENT));
527 if (operationTypePtr->ToInt() == FILE_ACTION_MOVE)
529 AppLogDebug("Rash: FILE_MOVE_ACTION");
530 __fileActionMode = FILE_ACTION_MOVE;
532 else if (operationTypePtr->ToInt() == FILE_ACTION_COPY)
534 AppLogDebug("Rash: FILE_COPY_ACTION");
535 __fileActionMode = FILE_ACTION_COPY;
537 pArgs->RemoveAt(ALBUM_FIRST_ELEMENT, true);
540 if (previousSceneId == IDSCN_ALBUM_LIST_EDITOR)
544 IEnumerator* pEnum = pArgs->GetEnumeratorN();
545 if (pEnum->MoveNext() == E_SUCCESS)
547 Integer *index = static_cast<Integer*>(pEnum->GetCurrent());
550 __folderIndex = index->ToInt();
559 if (previousSceneId == IDSCN_ALL_LIST_EDITOR
560 || previousSceneId == IDSCN_IMAGE_LIST_EDITOR
561 || previousSceneId == IDSCN_VIDEO_LIST_EDITOR
562 || previousSceneId == IDSCN_ALL_LIST_SELECTION)
564 if (previousSceneId == IDSCN_ALL_LIST_EDITOR)
566 AppLogDebug("[MOVE] previousSceneId = IDSCN_ALL_LIST_EDITOR");
568 else if (previousSceneId == IDSCN_ALL_LIST_SELECTION)
570 AppLogDebug("[MOVE] previousSceneId = IDSCN_ALL_LIST_SELECTION");
573 __callerSceneId = previousSceneId;
577 __callerSceneId = EMPTY_SPACE;
580 if (__albumNameEditorMode == ALBUM_NAME_EDITOR_MODE_CREATE)
582 __nameEditFieldText = EMPTY_SPACE;
583 __nameEditFieldPreText = EMPTY_SPACE;
584 __pNameEditField->SetText(__nameEditFieldText);
585 __nameEditFieldTempText = __nameEditFieldText;
586 __pNameEditField->ShowKeypad();
587 __pNameEditField->SetFocus();
589 GetHeader()->SetTitleText(ResourceManager::GetString(L"IDS_MEDIABR_OPT_CREATE_ALBUM"));
590 GetFooter()->SetItemEnabled(0, false);
591 GetFooter()->Invalidate(true);
597 IList* pAlbumInfoList = __pPresentationModel->GetAlbumInfoList();
598 AlbumInfo* pAlbumInfo = static_cast<AlbumInfo*>(pAlbumInfoList->GetAt(__folderIndex));
600 String strName = pAlbumInfo->GetAlbumName();
601 __nameEditFieldText = strName;
602 __nameEditFieldPreText = strName;
603 __pNameEditField->SetText(__nameEditFieldText);
604 __nameEditFieldTempText = __nameEditFieldText;
605 GetHeader()->SetTitleText(ResourceManager::GetString(L"IDS_COM_BODY_RENAME"));
606 __pNameEditField->SetOverlayKeypadCommandButtonVisible(false);
607 __pNameEditField->ShowKeypad();
608 __pNameEditField->SetFocus();
609 __originalText = __pNameEditField->GetText();
610 utf8.GetByteCount(__nameEditFieldText, byteCount);
611 __textLength = byteCount;
612 GetFooter()->SetItemEnabled(0, true);
613 GetFooter()->Invalidate(true);
618 __pMoveIndexList = pArgs;
621 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
625 AlbumNameEditorForm::OnSceneDeactivated(const SceneId& currentSceneId,
626 const SceneId& nextSceneId)
628 AppLogDebug("ENTER");
629 __pPresentationModel->RemoveContentEventListener(*this);
630 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
634 AlbumNameEditorForm::OnContentUpdated(void)
636 AppLogDebug("ENTER");
638 if (__mountState == false)
640 SceneManager* pSceneManager = SceneManager::GetInstance();
641 pSceneManager->GoForward(ForwardSceneTransition(IDSCN_ALBUM_LIST));
642 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
646 __mountState = false;
651 AlbumNameEditorForm::OnDeviceStateChanged(DeviceType deviceType, const Tizen::Base::String& state)
653 AppLogDebug("ENTER");
655 if (deviceType == DEVICE_TYPE_STORAGE_CARD && state == DEVICE_STORAGE_CARD_UNMOUNTED)
657 if (__pFileMove && __pFileMove->IsStarted())
659 __pFileMove->Cancel();
663 __mountState = false;
664 SceneManager* pSceneManager = SceneManager::GetInstance();
665 pSceneManager->GoForward(ForwardSceneTransition(IDSCN_ALBUM_LIST));
666 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
672 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
676 AlbumNameEditorForm::CreateMessage(String& str)
678 if (__pMessageBox == null)
680 __pMessageBox = new(std::nothrow) MessageBox;
682 if (__pMessageBox != null)
684 __pMessageBox->Construct(L"", str, MSGBOX_STYLE_OK, 3000);
685 __pMessageBox->ShowAndWait(__modalMsgBoxResult);
687 delete __pMessageBox;
688 __pMessageBox = null;
689 __modalMsgBoxResult = 0;
695 AlbumNameEditorForm::OnOrientationChanged(const Tizen::Ui::Control &source, Tizen::Ui::OrientationStatus orientationStatus)
697 if (__pProgressAnim != NULL)
699 __pProgressAnim->ChangeOrientation(GetClientAreaBounds());
700 __pProgressAnim->Invalidate(true);