Fixed keypad issue
[apps/osp/Gallery.git] / src / GlAlbumNameEditorForm.cpp
index 29e5d43..1b59781 100644 (file)
@@ -20,6 +20,8 @@
  */
 
 #include <FMedia.h>
+#include <FText.h>
+
 #include "GlAlbumInfo.h"
 #include "GlAlbumListPresentationModel.h"
 #include "GlAlbumNameEditorForm.h"
@@ -39,10 +41,12 @@ 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;
@@ -50,6 +54,9 @@ static const int ALBUM_MAX_LENGTH = 255;
 AlbumNameEditorForm::AlbumNameEditorForm(void)
        : __pNameEditField(null)
        , __folderIndex(0)
+       , __modalMsgBoxResult(0)
+       , __textLength(0)
+       , __mountState(false)
        , __albumNameEditorMode(ALBUM_NAME_EDITOR_MODE_RENAME)
        , __pMessageBox(null)
        , __pMoveIndexList(null)
@@ -66,8 +73,9 @@ AlbumNameEditorForm::AlbumNameEditorForm(void)
 
 AlbumNameEditorForm::~AlbumNameEditorForm(void)
 {
-       delete __pFileMove;
        AppLogDebug("ENTER");
+       DeviceManager::RemoveDeviceEventListener(DEVICE_TYPE_STORAGE_CARD, *this);
+       delete __pFileMove;
        AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
 }
 
@@ -88,6 +96,7 @@ 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);
@@ -113,7 +122,7 @@ AlbumNameEditorForm::OnInitializing(void)
        __pNameEditField->SetColor(EDIT_STATUS_NORMAL, COLOR_NAME_EDIT_FIELD);
        __pNameEditField->SetOverlayKeypadCommandButtonVisible(false);
 
-       AddControl(*__pNameEditField);
+       AddControl(__pNameEditField);
        __pNameEditField->AddActionEventListener(*this);
 
        return E_SUCCESS;
@@ -122,77 +131,128 @@ 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* pBuffer = StringUtil::StringToUtf8N(inputString);
-       int remaining = 0;
-       if (pBuffer != null)
-       {
-               remaining = pBuffer->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;
+                               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()));
 }
@@ -277,7 +337,7 @@ AlbumNameEditorForm::OnRenameAlbum(String& strOri)
                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();
@@ -356,11 +416,11 @@ void AlbumNameEditorForm::OnAlbumRenameComplete(void)
        //Hide popup here..
        __pProgressAnim->AnimationStop();
        __pProgressAnim->SetShowState(false);
-       RemoveControl(*__pProgressAnim);
+       RemoveControl(__pProgressAnim);
        _overlayMsg = false;
        GetFooter()->SetEnabled(true);
        SceneManager* pSceneManager = SceneManager::GetInstance();
-       pSceneManager->GoForward(ForwardSceneTransition(IDSCN_ALBUM_LIST));
+       pSceneManager->GoBackward((BackwardSceneTransition(IDSCN_ALBUM_LIST)));
 }
 
 void
@@ -381,6 +441,8 @@ AlbumNameEditorForm::OnActionPerformed(const Control& source, int actionId)
        {
        case IDA_BUTTON_CREATE_NAME_SAVE:
        {
+               __pNameEditField->SetEnabled(false);
+
                if (__albumNameEditorMode == ALBUM_NAME_EDITOR_MODE_CREATE)
                {
                        OnCreateAlbum();
@@ -432,6 +494,7 @@ AlbumNameEditorForm::OnSceneActivatedN(const SceneId& previousSceneId,
        __pPresentationModel = AlbumListPresentationModel::GetInstance();
        __previousSceneId = previousSceneId;
 
+       __pNameEditField->SetEnabled(true);
        __pPresentationModel->AddContentEventListener(this);
 
        if (pArgs != null)
@@ -511,6 +574,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"));
@@ -519,7 +583,8 @@ AlbumNameEditorForm::OnSceneActivatedN(const SceneId& previousSceneId,
        }
        else
        {
-
+               int byteCount = 0;
+               Utf8Encoding utf8;
                IList* pAlbumInfoList = __pPresentationModel->GetAlbumInfoList();
                AlbumInfo* pAlbumInfo = static_cast<AlbumInfo*>(pAlbumInfoList->GetAt(__folderIndex));
 
@@ -527,10 +592,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)
@@ -555,7 +626,58 @@ AlbumNameEditorForm::OnContentUpdated(void)
 {
        AppLogDebug("ENTER");
 
-       SceneManager* pSceneManager = SceneManager::GetInstance();
-       pSceneManager->GoForward(ForwardSceneTransition(IDSCN_ALBUM_LIST));
+       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;
+               }
+       }
+}