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);
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 = new (std::nothrow) EditField();
116 __pNameEditField->Construct(RECT_NAME_EDIT_FIELD, EDIT_FIELD_STYLE_NORMAL, INPUT_STYLE_OVERLAY,
117 EDIT_FIELD_TITLE_STYLE_NONE, true, ALBUM_MAX_LENGTH);
118 __pNameEditField->AddTextEventListener(*this);
119 __pNameEditField->SetColor(EDIT_STATUS_NORMAL, COLOR_NAME_EDIT_FIELD);
120 __pNameEditField->SetOverlayKeypadCommandButtonVisible(false);
122 AddControl(__pNameEditField);
123 __pNameEditField->AddActionEventListener(*this);
129 AlbumNameEditorForm::OnTextValueChanged(const Control& source)
133 String specialCharacters(L"/");
134 String byteCountForInput;
137 int tempLength = 255;
139 MessageBox messageBox;
142 FooterItemStatus currentStatus = FOOTER_ITEM_STATUS_NORMAL;
144 result r = E_SUCCESS;
146 if (__pNameEditField != null)
148 byteCountForInput = __pNameEditField->GetText();
149 r = utf8.GetByteCount(byteCountForInput, byteCount);
150 AppLogDebug("result is %s",GetErrorMessage(r));
151 AppLogDebug("Byte count is %d",byteCount);
154 if (__pNameEditField != null)
156 currentText = __pNameEditField->GetText();
157 textToCheck = currentText.GetLength() - __textLength;
158 if (currentText.IsEmpty())
160 if (GetFooter() != null)
162 GetFooter()->SetItemEnabled(0, false);
163 GetFooter()->Invalidate(true);
168 if (GetFooter() != null)
170 GetFooter()->GetItemStatus(0, currentStatus);
172 if (currentStatus == FOOTER_ITEM_STATUS_DISABLED)
174 GetFooter()->SetItemEnabled(0,true);
175 GetFooter()->Invalidate(true);
180 if (currentText.StartsWith(".", 0))
182 String invalidCharacterString;
184 String msg = ResourceManager::GetString(L"IDS_COM_POP_INVALID_CHARACTERS");
186 validString = __pNameEditField->GetText();
188 if (validString.GetLength() > 1)
190 validString.Remove(0,1);
191 __pNameEditField->SetText(validString);
195 __pNameEditField->SetText("");
197 if (GetFooter() != null)
199 GetFooter()->SetItemEnabled(0, false);
200 GetFooter()->Invalidate(true);
203 __pNameEditField->ShowKeypad();
206 currentInput = __pNameEditField->GetText();
208 if (currentInput.Contains(specialCharacters[0]))
210 String msg = ResourceManager::GetString(L"IDS_COM_POP_INVALID_CHARACTERS");
212 currentInput.Replace("/", "");
213 __pNameEditField->SetText(currentInput);
216 inputLength = byteCount;
217 if (inputLength >= tempLength)
219 String maxCharacterString;
220 String currentInputCharacter;
221 int checkByteCount = 0;
222 int lengthOfFinalString = 0;
223 int currentCharacter = 0;
225 String msg = ResourceManager::GetString(L"IDS_COM_POP_MAXIMUM_NUMBER_OF_CHARACTERS_REACHED");
227 currentText = __pNameEditField->GetText();
229 for (currentCharacter=0; checkByteCount < tempLength; currentCharacter++)
231 currentInputCharacter.Clear();
232 currentInputCharacter = currentText[currentCharacter];
233 r = utf8.GetByteCount(currentInputCharacter, byteCount);
238 AppLogDebug("result of byte count is %s",GetErrorMessage(r));
239 checkByteCount = checkByteCount + byteCount;
241 if( checkByteCount < tempLength)
243 lengthOfFinalString++;
247 r = __pNameEditField->GetText().SubString(0, lengthOfFinalString, currentInput);
248 AppLogDebug("current input is %S", currentInput.GetPointer());
249 r = __pNameEditField->SetText(currentInput);
250 __pNameEditField->ShowKeypad();
254 r = utf8.GetByteCount(__pNameEditField->GetText(), byteCount);
255 __textLength = byteCount;
258 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
262 AlbumNameEditorForm::OnTextValueChangeCanceled(const Control& source)
264 AppLogDebug("ENTER");
265 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
269 AlbumNameEditorForm::OnTerminating(void)
271 AppLogDebug("ENTER");
272 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
277 void AlbumNameEditorForm::OnFileOpInvalidate(enum FileActionMode actionId)
282 void AlbumNameEditorForm::OnFileOpComplete(enum FileActionMode actionId, enum FileActionCompleteRes res)
285 if (res != COMPLETE_SUCCESS)
287 __pNameEditField->SetText(__nameEditFieldPreText);
288 __pNameEditField->HideKeypad();
289 __pNameEditField->Draw();
290 SceneManager* pSceneManager = SceneManager::GetInstance();
291 pSceneManager->GoBackward(BackwardSceneTransition(__callerSceneId));
296 AlbumNameEditorForm::OnRenameAlbum(String& strOri)
298 AppLogDebug("Rename Album");
302 strOri.IndexOf(DIRECTORY_SEPARATOR, 0, index);
304 strOri.SubString(0, (strOri.GetLength() - index), path);
305 path.Append(__nameEditFieldText);
308 result r = __pPresentationModel->RenameAlbum(strOri, path, this);
312 AppLogDebug("Unable to rename Album %s", GetErrorMessage(r));
313 if (__pMessageBox == null)
315 __pMessageBox = new (std::nothrow) MessageBox();
316 if (r == E_FILE_ALREADY_EXIST)
318 __pMessageBox->Construct(L"", ResourceManager::GetString(L"IDS_MEDIABR_BODY_UNABLE_TO_RENAME_ALBUM_NAME_ALREADY_IN_USE_ABB"),
319 MSGBOX_STYLE_NONE, 3000);
323 __pMessageBox->Construct(L"", ResourceManager::GetString(L"IDS_MEDIABR_POP_UNABLE_TO_RENAME"),
324 MSGBOX_STYLE_NONE, 3000);
328 __pMessageBox->ShowAndWait(modalResult);
330 delete __pMessageBox;
331 __pMessageBox = null;
337 Rectangle clientRect= GetClientAreaBounds();
338 Rectangle rect(0, 0, clientRect.width, clientRect.height);
339 __pProgressAnim = new (std::nothrow) ProgressAnimation;
340 __pProgressAnim->Construct(rect);
341 AddControl(__pProgressAnim);
342 __pProgressAnim->SetShowState(true);
343 GetFooter()->SetEnabled(false);
344 __pProgressAnim->AnimationStart();
345 __pProgressAnim->Draw();
351 AlbumNameEditorForm::OnCreateAlbum(void)
354 __pNameEditField->HideKeypad();
355 __pNameEditField->RequestRedraw();
356 path.Append(Tizen::System::Environment::GetMediaPath());
357 path.Append(__nameEditFieldText);
359 if (__previousSceneId != IDSCN_ALL_LIST_EDITOR && __previousSceneId != IDSCN_ALL_LIST_SELECTION)
361 Directory::Create(path, true);
364 __nameEditFieldPreText = __nameEditFieldText;
368 AppLogDebug("EXIT 1(%s)", GetErrorMessage(GetLastResult()));
369 delete __pMoveIndexList;
370 __pMoveIndexList = null;
374 if (__pMoveIndexList == null || __pMoveIndexList->GetCount() <= 0)
376 AppLogDebug("EXIT 2(%s)", GetErrorMessage(GetLastResult()));
377 delete __pMoveIndexList;
378 __pMoveIndexList = null;
383 GetMoveFileIndexList(path, __pMoveIndexList, __pFilePresentationModel);
384 __pFileMove = new (std::nothrow) FileMoveTimer(path, __pMoveIndexList, __pFilePresentationModel, this);
385 if (__fileActionMode == FILE_ACTION_COPY)
387 AppLogDebug("Rash: Setting to Copy in AlbumNameEditor");
388 __pFileMove->SetCopy();
392 __pFileMove->ClearCopy();
394 result r = __pFileMove->StartTimer();
400 __pNameEditField->SetText(__nameEditFieldPreText);
401 __pNameEditField->HideKeypad();
402 __pNameEditField->Draw();
403 AppLogDebug("MoveToContentFileList content failed (%s)", GetErrorMessage(r));
404 SceneManager* pSceneManager = SceneManager::GetInstance();
405 pSceneManager->GoBackward(BackwardSceneTransition(__callerSceneId));
409 AppLogDebug("MoveToContentFileList content in progress");
411 __fileActionMode = FILE_ACTION_MOVE;
415 void AlbumNameEditorForm::OnAlbumRenameComplete(void)
418 __pProgressAnim->AnimationStop();
419 __pProgressAnim->SetShowState(false);
420 RemoveControl(__pProgressAnim);
422 GetFooter()->SetEnabled(true);
423 SceneManager* pSceneManager = SceneManager::GetInstance();
424 pSceneManager->GoBackward((BackwardSceneTransition(IDSCN_ALBUM_LIST)));
428 AlbumNameEditorForm::OnActionPerformed(const Control& source, int actionId)
430 AppLogDebug("ENTER");
432 __nameEditFieldText = __pNameEditField->GetText();
434 if (__albumNameEditorMode == ALBUM_NAME_EDITOR_MODE_RENAME)
436 IList* pAlbumInfoList = __pPresentationModel->GetAlbumInfoList();
437 AlbumInfo* pAlbumInfo = static_cast<AlbumInfo*>(pAlbumInfoList->GetAt(__folderIndex));
438 path = pAlbumInfo->GetDirectory(0);
443 case IDA_BUTTON_CREATE_NAME_SAVE:
445 __pNameEditField->SetEnabled(false);
447 if (__albumNameEditorMode == ALBUM_NAME_EDITOR_MODE_CREATE)
451 else if (__albumNameEditorMode == ALBUM_NAME_EDITOR_MODE_RENAME)
453 String albumName = __pNameEditField->GetText();
454 if (__originalText.Equals(albumName , true))
456 SceneManager* pSceneManager = SceneManager::GetInstance();
457 pSceneManager->GoBackward(BackwardSceneTransition());
470 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
474 AlbumNameEditorForm::OnFormBackRequested(Form& source)
476 AppLogDebug("ENTER");
477 if (__albumNameEditorMode == ALBUM_NAME_EDITOR_MODE_CREATE)
479 SceneManager* pSceneManager = SceneManager::GetInstance();
480 pSceneManager->GoForward(ForwardSceneTransition(__callerSceneId));
484 SceneManager* pSceneManager = SceneManager::GetInstance();
485 pSceneManager->GoForward(ForwardSceneTransition(IDSCN_ALBUM_LIST_EDITOR));
487 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
491 AlbumNameEditorForm::OnSceneActivatedN(const SceneId& previousSceneId,
492 const SceneId& currentSceneId, IList* pArgs)
494 AppLogDebug("ENTER");
495 __pPresentationModel = AlbumListPresentationModel::GetInstance();
496 __previousSceneId = previousSceneId;
498 __pNameEditField->SetEnabled(true);
499 __pPresentationModel->AddContentEventListener(this);
503 Integer* pAlbumTypePtr = static_cast<Integer*>(pArgs->GetAt(ALBUM_FIRST_ELEMENT));
504 if (pAlbumTypePtr->ToInt() == ALBUM_CREATE)
506 AppLogDebug("ALBUM_NAME_EDITOR_MODE_CREATE");
507 __albumNameEditorMode = ALBUM_NAME_EDITOR_MODE_CREATE;
509 else if (pAlbumTypePtr->ToInt() == ALBUM_RENAME)
511 AppLogDebug("ALBUM_NAME_EDITOR_MODE_RENAME");
512 __albumNameEditorMode = ALBUM_NAME_EDITOR_MODE_RENAME;
514 pArgs->RemoveAt(ALBUM_FIRST_ELEMENT, true);
519 Integer* operationTypePtr = static_cast<Integer*>(pArgs->GetAt(ALBUM_FIRST_ELEMENT));
520 if (operationTypePtr->ToInt() == FILE_ACTION_MOVE)
522 AppLogDebug("Rash: FILE_MOVE_ACTION");
523 __fileActionMode = FILE_ACTION_MOVE;
525 else if (operationTypePtr->ToInt() == FILE_ACTION_COPY)
527 AppLogDebug("Rash: FILE_COPY_ACTION");
528 __fileActionMode = FILE_ACTION_COPY;
530 pArgs->RemoveAt(ALBUM_FIRST_ELEMENT, true);
533 if (previousSceneId == IDSCN_ALBUM_LIST_EDITOR)
537 IEnumerator* pEnum = pArgs->GetEnumeratorN();
538 if (pEnum->MoveNext() == E_SUCCESS)
540 Integer *index = static_cast<Integer*>(pEnum->GetCurrent());
543 __folderIndex = index->ToInt();
551 if (previousSceneId == IDSCN_ALL_LIST_EDITOR
552 || previousSceneId == IDSCN_IMAGE_LIST_EDITOR
553 || previousSceneId == IDSCN_VIDEO_LIST_EDITOR
554 || previousSceneId == IDSCN_ALL_LIST_SELECTION)
556 if (previousSceneId == IDSCN_ALL_LIST_EDITOR)
558 AppLogDebug("[MOVE] previousSceneId = IDSCN_ALL_LIST_EDITOR");
560 else if (previousSceneId == IDSCN_ALL_LIST_SELECTION)
562 AppLogDebug("[MOVE] previousSceneId = IDSCN_ALL_LIST_SELECTION");
565 __callerSceneId = previousSceneId;
569 __callerSceneId = EMPTY_SPACE;
572 if (__albumNameEditorMode == ALBUM_NAME_EDITOR_MODE_CREATE)
574 __nameEditFieldText = EMPTY_SPACE;
575 __nameEditFieldPreText = EMPTY_SPACE;
576 __pNameEditField->SetText(__nameEditFieldText);
577 __nameEditFieldTempText = __nameEditFieldText;
578 __pNameEditField->ShowKeypad();
579 __pNameEditField->SetFocus();
581 GetHeader()->SetTitleText(ResourceManager::GetString(L"IDS_MEDIABR_OPT_CREATE_ALBUM"));
582 GetFooter()->SetItemEnabled(0, false);
583 GetFooter()->Invalidate(true);
589 IList* pAlbumInfoList = __pPresentationModel->GetAlbumInfoList();
590 AlbumInfo* pAlbumInfo = static_cast<AlbumInfo*>(pAlbumInfoList->GetAt(__folderIndex));
592 String strName = pAlbumInfo->GetAlbumName();
593 __nameEditFieldText = strName;
594 __nameEditFieldPreText = strName;
595 __pNameEditField->SetText(__nameEditFieldText);
596 __nameEditFieldTempText = __nameEditFieldText;
597 GetHeader()->SetTitleText(ResourceManager::GetString(L"IDS_COM_BODY_RENAME"));
598 __pNameEditField->SetOverlayKeypadCommandButtonVisible(false);
599 __pNameEditField->ShowKeypad();
600 __pNameEditField->SetFocus();
601 __originalText = __pNameEditField->GetText();
602 utf8.GetByteCount(__nameEditFieldText, byteCount);
603 __textLength = byteCount;
604 GetFooter()->SetItemEnabled(0, true);
605 GetFooter()->Invalidate(true);
610 __pMoveIndexList = pArgs;
613 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
617 AlbumNameEditorForm::OnSceneDeactivated(const SceneId& currentSceneId,
618 const SceneId& nextSceneId)
620 AppLogDebug("ENTER");
621 __pPresentationModel->RemoveContentEventListener(*this);
622 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
626 AlbumNameEditorForm::OnContentUpdated(void)
628 AppLogDebug("ENTER");
630 if ( __mountState == false )
632 SceneManager* pSceneManager = SceneManager::GetInstance();
633 pSceneManager->GoForward(ForwardSceneTransition(IDSCN_ALBUM_LIST));
634 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
638 __mountState = false;
643 AlbumNameEditorForm::OnDeviceStateChanged(DeviceType deviceType, const Tizen::Base::String& state)
645 AppLogDebug("ENTER");
647 if (deviceType == DEVICE_TYPE_STORAGE_CARD && state == DEVICE_STORAGE_CARD_UNMOUNTED)
649 if ( __pFileMove && __pFileMove->IsStarted() )
651 __pFileMove->Cancel();
655 __mountState = false;
656 SceneManager* pSceneManager = SceneManager::GetInstance();
657 pSceneManager->GoForward(ForwardSceneTransition(IDSCN_ALBUM_LIST));
658 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
664 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
668 AlbumNameEditorForm::CreateMessage(String& str)
670 if (__pMessageBox == null)
672 __pMessageBox = new(std::nothrow) MessageBox;
674 if (__pMessageBox != null)
676 __pMessageBox->Construct(L"", str, MSGBOX_STYLE_OK, 3000);
677 __pMessageBox->ShowAndWait(__modalMsgBoxResult);
679 delete __pMessageBox;
680 __pMessageBox = null;
681 __modalMsgBoxResult = 0;