Fixed prevent issues
[apps/osp/Gallery.git] / src / GlAlbumNameEditorForm.cpp
index d2c6e3e..4c76280 100644 (file)
@@ -1,7 +1,7 @@
 //
 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
 //
-// Licensed under the Flora License, Version 1.0 (the License);
+// Licensed under the Flora License, Version 1.1 (the License);
 // you may not use this file except in compliance with the License.
 // You may obtain a copy of the License at
 //
@@ -19,7 +19,9 @@
  * @brief              This is the implementation file for AlbumNameEditorForm class.
  */
 
-#include <FApp.h>
+#include <FMedia.h>
+#include <FText.h>
+
 #include "GlAlbumInfo.h"
 #include "GlAlbumListPresentationModel.h"
 #include "GlAlbumNameEditorForm.h"
@@ -39,35 +41,38 @@ using namespace Tizen::Graphics;
 using namespace Tizen::Io;
 using namespace Tizen::Media;
 using namespace Tizen::System;
+using namespace Tizen::Text;
 using namespace Tizen::Ui;
 using namespace Tizen::Ui::Controls;
 using namespace Tizen::Ui::Scenes;
 
-static const Rectangle RECT_NAME_EDIT_FIELD (0, 0, 720, 100);
 static const unsigned int COLOR_NAME_EDIT_FIELD = Color32<255, 255, 255>::Value;
-static const int ALBUM_MAX_LENGTH = 255;
 
-AlbumNameEditorForm::AlbumNameEditorForm()
+AlbumNameEditorForm::AlbumNameEditorForm(void)
        : __pNameEditField(null)
        , __folderIndex(0)
+       , __modalMsgBoxResult(0)
+       , __textLength(0)
+       , __mountState(false)
        , __albumNameEditorMode(ALBUM_NAME_EDITOR_MODE_RENAME)
        , __pMessageBox(null)
        , __pMoveIndexList(null)
-       , __fileActionMode(FILE_MOVE_ACTION)
+       , __fileActionMode(FILE_ACTION_MOVE)
        , __pFileMove(null)
        , __pProgressAnim(null)
        , __pPresentationModel(null)
        , __pFilePresentationModel(null)
 {
        AppLogDebug("ENTER");
-       __overlayMsg = false;
+       _overlayMsg = false;
        AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
 }
 
-AlbumNameEditorForm::~AlbumNameEditorForm()
+AlbumNameEditorForm::~AlbumNameEditorForm(void)
 {
-       delete __pFileMove;
        AppLogDebug("ENTER");
+       DeviceManager::RemoveDeviceEventListener(DEVICE_TYPE_STORAGE_CARD, *this);
+       delete __pFileMove;
        AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
 }
 
@@ -75,7 +80,7 @@ result
 AlbumNameEditorForm::Initialize(void)
 {
        AppLogDebug("ENTER");
-       result r = Construct(FORM_STYLE_HEADER | FORM_STYLE_FOOTER | FORM_STYLE_NORMAL | FORM_STYLE_INDICATOR);
+       result r = Construct(L"IDL_FORM_ALBUM_NAME_EDITOR");
        AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
 
        return r;
@@ -88,15 +93,13 @@ AlbumNameEditorForm::OnInitializing(void)
        AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
        __pFilePresentationModel = FileListPresentationModel::GetInstance();
 
+       DeviceManager::AddDeviceEventListener(DEVICE_TYPE_STORAGE_CARD, *this);
        Header* pHeader = GetHeader();
 
        pHeader->SetStyle(HEADER_STYLE_TITLE);
 
-       GetFooter()->SetBackButtonEnabled(true);
-       GetFooter()->SetBackButton();
-
        FooterItem itemSave;
-       itemSave.Construct(ACTION_ID_BUTTON_CREATE_NAME_SAVE);
+       itemSave.Construct(IDA_BUTTON_CREATE_NAME_SAVE);
        itemSave.SetText(ResourceManager::GetString(L"IDS_COM_OPT_SAVE"));
 
        GetFooter()->AddItem(itemSave);
@@ -106,14 +109,10 @@ AlbumNameEditorForm::OnInitializing(void)
        SetFormBackEventListener(this);
        SetActionBarsVisible(FORM_ACTION_BAR_FOOTER, true);
 
-       __pNameEditField = new (std::nothrow) EditField();
-       __pNameEditField->Construct(RECT_NAME_EDIT_FIELD, EDIT_FIELD_STYLE_NORMAL, INPUT_STYLE_OVERLAY,
-                       EDIT_FIELD_TITLE_STYLE_NONE, true, ALBUM_MAX_LENGTH);
+       __pNameEditField = static_cast<EditField*>(GetControl(L"IDC_EDITFIELD_NAME"));
        __pNameEditField->AddTextEventListener(*this);
        __pNameEditField->SetColor(EDIT_STATUS_NORMAL, COLOR_NAME_EDIT_FIELD);
        __pNameEditField->SetOverlayKeypadCommandButtonVisible(false);
-
-       AddControl(*__pNameEditField);
        __pNameEditField->AddActionEventListener(*this);
 
        return E_SUCCESS;
@@ -122,77 +121,132 @@ AlbumNameEditorForm::OnInitializing(void)
 void
 AlbumNameEditorForm::OnTextValueChanged(const Control& source)
 {
-       AppLogDebug("ENTER");
-       String inputString = __pNameEditField->GetText();
+       String currentInput;
+       String currentText;
+       String specialCharacters(L"/");
+       String byteCountForInput;
+       int byteCount = 0;
+       int inputLength = 0;
+       int tempLength = 255;
+       int textToCheck = 0;
+       MessageBox messageBox;
+       Utf8Encoding utf8;
+
+       FooterItemStatus currentStatus = FOOTER_ITEM_STATUS_NORMAL;
+
+       result r = E_SUCCESS;
+
+       if (__pNameEditField != null)
+       {
+               byteCountForInput = __pNameEditField->GetText();
+               r = utf8.GetByteCount(byteCountForInput, byteCount);
+               AppLogDebug("result is %s",GetErrorMessage(r));
+               AppLogDebug("Byte count is %d",byteCount);
+       }
 
-       if (inputString.Contains(DIRECTORY_SEPARATOR) == true)
+       if (__pNameEditField != null)
        {
-               if (__pMessageBox == null)
+               currentText = __pNameEditField->GetText();
+               textToCheck = currentText.GetLength() - __textLength;
+               if (currentText.IsEmpty())
                {
-                       __pMessageBox = new (std::nothrow) MessageBox();
-                       __pMessageBox->Construct(L"", ResourceManager::GetString(L"IDS_COM_POP_INVALID_CHARACTERS"),
-                                       MSGBOX_STYLE_NONE, 3000);
-                       int modalResult = 0;
-                       __overlayMsg = true;
-                       __pMessageBox->ShowAndWait(modalResult);
-                       __overlayMsg = false;
-                       delete __pMessageBox;
-                       __pMessageBox = null;
+                       if (GetFooter() != null)
+                       {
+                               GetFooter()->SetItemEnabled(0, false);
+                               GetFooter()->Invalidate(true);
+                       }
                }
+               else
+               {
+                       if (GetFooter() != null)
+                       {
+                               GetFooter()->GetItemStatus(0, currentStatus);
 
-               __pNameEditField->SetText(__nameEditFieldTempText);
-               __pNameEditField->SetFocus();
-
-               return;
-       }
-
-       ByteBuffer* buffer = StringUtil::StringToUtf8N(inputString);
-       int remaining = 0;
-       if (buffer != null)
-       {
-               remaining = buffer->GetRemaining();
-       }
+                               if (currentStatus == FOOTER_ITEM_STATUS_DISABLED)
+                               {
+                                       GetFooter()->SetItemEnabled(0,true);
+                                       GetFooter()->Invalidate(true);
+                               }
+                       }
+               }
 
-       if (remaining >= ALBUM_MAX_LENGTH)
-       {
-               if (__pMessageBox == null)
+               if (currentText.StartsWith(".", 0))
                {
-                       __pMessageBox = new (std::nothrow) MessageBox();
-                       __pMessageBox->Construct(L"",
-                                       ResourceManager::GetString(L"IDS_COM_POP_MAXIMUM_NUMBER_OF_CHARACTERS_REACHED"),
-                                       MSGBOX_STYLE_NONE, 3000);
-                       int modalResult = 0;
-                       __pMessageBox->ShowAndWait(modalResult);
-                       delete __pMessageBox;
-                       __pMessageBox = null;
-               }
+                       String invalidCharacterString;
+                       String validString;
+                       String msg = ResourceManager::GetString(L"IDS_COM_POP_INVALID_CHARACTERS");
+                       CreateMessage(msg);
+                       validString = __pNameEditField->GetText();
 
-               __pNameEditField->SetText(__nameEditFieldTempText);
-               __pNameEditField->SetFocus();
+                       if (validString.GetLength() > 1)
+                       {
+                               validString.Remove(0,1);
+                               __pNameEditField->SetText(validString);
+                       }
+                       else
+                       {
+                               __pNameEditField->SetText("");
 
-               return;
-       }
-       __nameEditFieldTempText = inputString;
+                               if (GetFooter() != null)
+                               {
+                                       GetFooter()->SetItemEnabled(0, false);
+                                       GetFooter()->Invalidate(true);
+                               }
+                       }
+                       __pNameEditField->ShowKeypad();
+               }
 
-       inputString.Trim();
+               currentInput = __pNameEditField->GetText();
 
-       FooterItemStatus itemStatus;
-       GetFooter()->GetItemStatus(0, itemStatus);
-       if (inputString.GetLength() == 0)
-       {
-               if (itemStatus != FOOTER_ITEM_STATUS_DISABLED)
+               if (currentInput.Contains(specialCharacters[0]))
                {
-                       GetFooter()->SetItemEnabled(0, false);
+                       String msg = ResourceManager::GetString(L"IDS_COM_POP_INVALID_CHARACTERS");
+                       CreateMessage(msg);
+                       currentInput.Replace("/", "");
+                       __pNameEditField->SetText(currentInput);
                }
-       }
-       else
-       {
-               if (itemStatus == FOOTER_ITEM_STATUS_DISABLED)
+
+               inputLength = byteCount;
+               if (inputLength  >= tempLength)
                {
-                       GetFooter()->SetItemEnabled(0, true);
+                       String maxCharacterString;
+                       String currentInputCharacter;
+                       int checkByteCount = 0;
+                       int lengthOfFinalString = 0;
+                       int currentCharacter = 0;
+
+                       String msg = ResourceManager::GetString(L"IDS_COM_POP_MAXIMUM_NUMBER_OF_CHARACTERS_REACHED");
+                       CreateMessage(msg);
+                       currentText = __pNameEditField->GetText();
+
+                       for (currentCharacter=0; checkByteCount < tempLength; currentCharacter++)
+                       {
+                               currentInputCharacter.Clear();
+                               currentInputCharacter = currentText[currentCharacter];
+                               r = utf8.GetByteCount(currentInputCharacter, byteCount);
+                               if (r != E_SUCCESS)
+                               {
+                                       break;
+                               }
+                               AppLogDebug("result of byte count is %s",GetErrorMessage(r));
+                               checkByteCount = checkByteCount + byteCount;
+
+                               if( checkByteCount < tempLength)
+                               {
+                                       lengthOfFinalString++;
+                               }
+                       }
+
+                       r = __pNameEditField->GetText().SubString(0, lengthOfFinalString, currentInput);
+                       AppLogDebug("current input is %S", currentInput.GetPointer());
+                       r = __pNameEditField->SetText(currentInput);
+                       __pNameEditField->ShowKeypad();
+                       return;
                }
+
+               r = utf8.GetByteCount(__pNameEditField->GetText(), byteCount);
+               __textLength = byteCount;
        }
-       GetFooter()->Invalidate(true);
 
        AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
 }
@@ -220,7 +274,7 @@ void AlbumNameEditorForm::OnFileOpInvalidate(enum FileActionMode actionId)
 
 void AlbumNameEditorForm::OnFileOpComplete(enum FileActionMode actionId, enum FileActionCompleteRes res)
 {
-       __overlayMsg = false;
+       _overlayMsg = false;
        if (res != COMPLETE_SUCCESS)
        {
                __pNameEditField->SetText(__nameEditFieldPreText);
@@ -232,43 +286,57 @@ void AlbumNameEditorForm::OnFileOpComplete(enum FileActionMode actionId, enum Fi
 }
 
 void
-AlbumNameEditorForm::OnRenameAlbum(const String& strOri)
+AlbumNameEditorForm::OnRenameAlbum(String& strOri)
 {
        AppLogDebug("Rename Album");
        String path;
-       path.Append(Tizen::System::Environment::GetMediaPath());
+       int index = 0;
+       strOri.Reverse();
+       strOri.IndexOf(DIRECTORY_SEPARATOR, 0, index);
+       strOri.Reverse();
+       strOri.SubString(0, (strOri.GetLength() - index), path);
        path.Append(__nameEditFieldText);
 
        //Rename
        result r = __pPresentationModel->RenameAlbum(strOri, path, this);
+
        if (IsFailed(r))
        {
                AppLogDebug("Unable to rename Album %s", GetErrorMessage(r));
                if (__pMessageBox == null)
                {
                        __pMessageBox = new (std::nothrow) MessageBox();
-                       __pMessageBox->Construct(L"", ResourceManager::GetString(L"IDS_MEDIABR_POP_UNABLE_TO_RENAME"),
-                                       MSGBOX_STYLE_NONE, 3000);
+                       if (r == E_FILE_ALREADY_EXIST)
+                       {
+                               __pMessageBox->Construct(L"", ResourceManager::GetString(L"IDS_MEDIABR_BODY_UNABLE_TO_RENAME_ALBUM_NAME_ALREADY_IN_USE_ABB"),
+                                                         MSGBOX_STYLE_NONE, 3000);
+                       }
+                       else
+                       {
+                               __pMessageBox->Construct(L"", ResourceManager::GetString(L"IDS_MEDIABR_POP_UNABLE_TO_RENAME"),
+                                                         MSGBOX_STYLE_NONE, 3000);
+                       }
                        int modalResult = 0;
-                       __overlayMsg = true;
+                       _overlayMsg = true;
                        __pMessageBox->ShowAndWait(modalResult);
-                       __overlayMsg = false;
+                       _overlayMsg = false;
                        delete __pMessageBox;
                        __pMessageBox = null;
                }
        }
        else
        {
-               //Add a popup here..
+               //Add a popup here
                Rectangle clientRect= GetClientAreaBounds();
-               Rectangle rect(0,0,clientRect.width,clientRect.height);
+               Rectangle rect(0, 0, clientRect.width, clientRect.height);
                __pProgressAnim = new (std::nothrow) ProgressAnimation;
                __pProgressAnim->Construct(rect);
-               AddControl(*__pProgressAnim);
+               AddControl(__pProgressAnim);
                __pProgressAnim->SetShowState(true);
+               GetFooter()->SetEnabled(false);
                __pProgressAnim->AnimationStart();
                __pProgressAnim->Draw();
-               __overlayMsg = true;
+               _overlayMsg = true;
        }
 }
 
@@ -306,8 +374,8 @@ AlbumNameEditorForm::OnCreateAlbum(void)
 
        delete __pFileMove;
        GetMoveFileIndexList(path, __pMoveIndexList, __pFilePresentationModel);
-       __pFileMove = new FileMoveTimer(path, __pMoveIndexList, __pFilePresentationModel, this);
-       if (__fileActionMode == FILE_COPY_ACTION)
+       __pFileMove = new (std::nothrow) FileMoveTimer(path, __pMoveIndexList, __pFilePresentationModel, this);
+       if (__fileActionMode == FILE_ACTION_COPY)
        {
                AppLogDebug("Rash: Setting to Copy in AlbumNameEditor");
                __pFileMove->SetCopy();
@@ -332,8 +400,8 @@ AlbumNameEditorForm::OnCreateAlbum(void)
        else
        {
                AppLogDebug("MoveToContentFileList content in progress");
-               __overlayMsg = true;
-               __fileActionMode = FILE_MOVE_ACTION;
+               _overlayMsg = true;
+               __fileActionMode = FILE_ACTION_MOVE;
        }
 }
 
@@ -342,10 +410,11 @@ void AlbumNameEditorForm::OnAlbumRenameComplete(void)
        //Hide popup here..
        __pProgressAnim->AnimationStop();
        __pProgressAnim->SetShowState(false);
-       RemoveControl(*__pProgressAnim);
-       __overlayMsg = false;
+       RemoveControl(__pProgressAnim);
+       _overlayMsg = false;
+       GetFooter()->SetEnabled(true);
        SceneManager* pSceneManager = SceneManager::GetInstance();
-       pSceneManager->GoForward(ForwardSceneTransition(IDSCN_ALBUM_LIST));
+       pSceneManager->GoBackward((BackwardSceneTransition(IDSCN_ALBUM_LIST)));
 }
 
 void
@@ -364,8 +433,10 @@ AlbumNameEditorForm::OnActionPerformed(const Control& source, int actionId)
 
        switch (actionId)
        {
-       case ACTION_ID_BUTTON_CREATE_NAME_SAVE:
+       case IDA_BUTTON_CREATE_NAME_SAVE:
        {
+               __pNameEditField->SetEnabled(false);
+
                if (__albumNameEditorMode == ALBUM_NAME_EDITOR_MODE_CREATE)
                {
                        OnCreateAlbum();
@@ -417,15 +488,18 @@ AlbumNameEditorForm::OnSceneActivatedN(const SceneId& previousSceneId,
        __pPresentationModel = AlbumListPresentationModel::GetInstance();
        __previousSceneId = previousSceneId;
 
+       __pNameEditField->SetEnabled(true);
+       __pPresentationModel->AddContentEventListener(this);
+
        if (pArgs != null)
        {
-               Integer* albumTypePtr = static_cast<Integer*>(pArgs->GetAt(ALBUM_FIRST_ELEMENT));
-               if (albumTypePtr->ToInt() == ALBUM_CREATE)
+               Integer* pAlbumTypePtr = static_cast<Integer*>(pArgs->GetAt(ALBUM_FIRST_ELEMENT));
+               if (pAlbumTypePtr->ToInt() == ALBUM_CREATE)
                {
                        AppLogDebug("ALBUM_NAME_EDITOR_MODE_CREATE");
                        __albumNameEditorMode = ALBUM_NAME_EDITOR_MODE_CREATE;
                }
-               else if (albumTypePtr->ToInt() == ALBUM_RENAME)
+               else if (pAlbumTypePtr->ToInt() == ALBUM_RENAME)
                {
                        AppLogDebug("ALBUM_NAME_EDITOR_MODE_RENAME");
                        __albumNameEditorMode = ALBUM_NAME_EDITOR_MODE_RENAME;
@@ -436,15 +510,15 @@ AlbumNameEditorForm::OnSceneActivatedN(const SceneId& previousSceneId,
        if (pArgs != null)
        {
                Integer* operationTypePtr = static_cast<Integer*>(pArgs->GetAt(ALBUM_FIRST_ELEMENT));
-               if (operationTypePtr->ToInt() == FILE_MOVE_ACTION)
+               if (operationTypePtr->ToInt() == FILE_ACTION_MOVE)
                {
                        AppLogDebug("Rash: FILE_MOVE_ACTION");
-                       __fileActionMode = FILE_MOVE_ACTION;
+                       __fileActionMode = FILE_ACTION_MOVE;
                }
-               else if (operationTypePtr->ToInt() == FILE_COPY_ACTION)
+               else if (operationTypePtr->ToInt() == FILE_ACTION_COPY)
                {
                        AppLogDebug("Rash: FILE_COPY_ACTION");
-                       __fileActionMode = FILE_COPY_ACTION;
+                       __fileActionMode = FILE_ACTION_COPY;
                }
                pArgs->RemoveAt(ALBUM_FIRST_ELEMENT, true);
        }
@@ -464,6 +538,7 @@ AlbumNameEditorForm::OnSceneActivatedN(const SceneId& previousSceneId,
                        }
                        delete pEnum;
                        delete pArgs;
+                       pArgs = NULL ;
                }
        }
 
@@ -494,6 +569,7 @@ AlbumNameEditorForm::OnSceneActivatedN(const SceneId& previousSceneId,
                __nameEditFieldPreText = EMPTY_SPACE;
                __pNameEditField->SetText(__nameEditFieldText);
                __nameEditFieldTempText = __nameEditFieldText;
+               __pNameEditField->ShowKeypad();
                __pNameEditField->SetFocus();
 
                GetHeader()->SetTitleText(ResourceManager::GetString(L"IDS_MEDIABR_OPT_CREATE_ALBUM"));
@@ -502,7 +578,8 @@ AlbumNameEditorForm::OnSceneActivatedN(const SceneId& previousSceneId,
        }
        else
        {
-
+               int byteCount = 0;
+               Utf8Encoding utf8;
                IList* pAlbumInfoList = __pPresentationModel->GetAlbumInfoList();
                AlbumInfo* pAlbumInfo = static_cast<AlbumInfo*>(pAlbumInfoList->GetAt(__folderIndex));
 
@@ -510,10 +587,16 @@ AlbumNameEditorForm::OnSceneActivatedN(const SceneId& previousSceneId,
                __nameEditFieldText = strName;
                __nameEditFieldPreText = strName;
                __pNameEditField->SetText(__nameEditFieldText);
+               __nameEditFieldTempText = __nameEditFieldText;
                GetHeader()->SetTitleText(ResourceManager::GetString(L"IDS_COM_BODY_RENAME"));
                __pNameEditField->SetOverlayKeypadCommandButtonVisible(false);
+               __pNameEditField->ShowKeypad();
                __pNameEditField->SetFocus();
                __originalText = __pNameEditField->GetText();
+               utf8.GetByteCount(__nameEditFieldText, byteCount);
+               __textLength = byteCount;
+               GetFooter()->SetItemEnabled(0, true);
+               GetFooter()->Invalidate(true);
        }
 
        if (pArgs != NULL)
@@ -529,5 +612,67 @@ AlbumNameEditorForm::OnSceneDeactivated(const SceneId& currentSceneId,
                const SceneId& nextSceneId)
 {
        AppLogDebug("ENTER");
+       __pPresentationModel->RemoveContentEventListener(*this);
        AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
 }
+
+void
+AlbumNameEditorForm::OnContentUpdated(void)
+{
+       AppLogDebug("ENTER");
+
+       if ( __mountState == false )
+       {
+               SceneManager* pSceneManager = SceneManager::GetInstance();
+               pSceneManager->GoForward(ForwardSceneTransition(IDSCN_ALBUM_LIST));
+               AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
+       }
+       else
+       {
+               __mountState = false;
+       }
+}
+
+void
+AlbumNameEditorForm::OnDeviceStateChanged(DeviceType deviceType, const Tizen::Base::String& state)
+{
+       AppLogDebug("ENTER");
+
+       if (deviceType == DEVICE_TYPE_STORAGE_CARD && state == DEVICE_STORAGE_CARD_UNMOUNTED)
+       {
+               if ( __pFileMove && __pFileMove->IsStarted() )
+               {
+                       __pFileMove->Cancel();
+                       delete __pFileMove;
+                       __pFileMove = null;
+               }
+               __mountState = false;
+               SceneManager* pSceneManager = SceneManager::GetInstance();
+               pSceneManager->GoForward(ForwardSceneTransition(IDSCN_ALBUM_LIST));
+               AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
+       }
+       else
+       {
+               __mountState = true;
+       }
+       AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
+}
+
+void
+AlbumNameEditorForm::CreateMessage(String& str)
+{
+       if (__pMessageBox == null)
+       {
+               __pMessageBox = new(std::nothrow) MessageBox;
+
+               if (__pMessageBox != null)
+               {
+                       __pMessageBox->Construct(L"", str, MSGBOX_STYLE_OK, 3000);
+                       __pMessageBox->ShowAndWait(__modalMsgBoxResult);
+
+                       delete __pMessageBox;
+                       __pMessageBox = null;
+                       __modalMsgBoxResult = 0;
+               }
+       }
+}