Fixed issues 39013, 39036
[apps/osp/Gallery.git] / src / GlAlbumNameEditorForm.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
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
7 //
8 //     http://floralicense.org/license/
9 //
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.
15 //
16
17 /**
18  * @file                GlAlbumNameEditorForm.cpp
19  * @brief               This is the implementation file for AlbumNameEditorForm class.
20  */
21
22 #include <FMedia.h>
23 #include "GlAlbumInfo.h"
24 #include "GlAlbumListPresentationModel.h"
25 #include "GlAlbumNameEditorForm.h"
26 #include "GlFileListPresentationModel.h"
27 #include "GlFileMoveTimer.h"
28 #include "GlProgressAnimation.h"
29 #include "GlResourceManager.h"
30 #include "GlTypes.h"
31
32 using namespace Tizen::App;
33 using namespace Tizen::Base;
34 using namespace Tizen::Base::Collection;
35 using namespace Tizen::Base::Runtime;
36 using namespace Tizen::Base::Utility;
37 using namespace Tizen::Content;
38 using namespace Tizen::Graphics;
39 using namespace Tizen::Io;
40 using namespace Tizen::Media;
41 using namespace Tizen::System;
42 using namespace Tizen::Ui;
43 using namespace Tizen::Ui::Controls;
44 using namespace Tizen::Ui::Scenes;
45
46 static const Rectangle RECT_NAME_EDIT_FIELD (0, 0, 720, 100);
47 static const unsigned int COLOR_NAME_EDIT_FIELD = Color32<255, 255, 255>::Value;
48 static const int ALBUM_MAX_LENGTH = 255;
49
50 AlbumNameEditorForm::AlbumNameEditorForm(void)
51         : __pNameEditField(null)
52         , __folderIndex(0)
53         , __mountState(false)
54         , __albumNameEditorMode(ALBUM_NAME_EDITOR_MODE_RENAME)
55         , __pMessageBox(null)
56         , __pMoveIndexList(null)
57         , __fileActionMode(FILE_ACTION_MOVE)
58         , __pFileMove(null)
59         , __pProgressAnim(null)
60         , __pPresentationModel(null)
61         , __pFilePresentationModel(null)
62 {
63         AppLogDebug("ENTER");
64         _overlayMsg = false;
65         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
66 }
67
68 AlbumNameEditorForm::~AlbumNameEditorForm(void)
69 {
70         AppLogDebug("ENTER");
71         DeviceManager::RemoveDeviceEventListener(DEVICE_TYPE_STORAGE_CARD, *this);
72         delete __pFileMove;
73         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
74 }
75
76 result
77 AlbumNameEditorForm::Initialize(void)
78 {
79         AppLogDebug("ENTER");
80         result r = Construct(FORM_STYLE_HEADER | FORM_STYLE_FOOTER | FORM_STYLE_NORMAL | FORM_STYLE_INDICATOR);
81         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
82
83         return r;
84 }
85
86 result
87 AlbumNameEditorForm::OnInitializing(void)
88 {
89         AppLogDebug("ENTER");
90         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
91         __pFilePresentationModel = FileListPresentationModel::GetInstance();
92
93         DeviceManager::AddDeviceEventListener(DEVICE_TYPE_STORAGE_CARD, *this);
94         Header* pHeader = GetHeader();
95
96         pHeader->SetStyle(HEADER_STYLE_TITLE);
97
98         GetFooter()->SetBackButtonEnabled(true);
99         GetFooter()->SetBackButton();
100
101         FooterItem itemSave;
102         itemSave.Construct(IDA_BUTTON_CREATE_NAME_SAVE);
103         itemSave.SetText(ResourceManager::GetString(L"IDS_COM_OPT_SAVE"));
104
105         GetFooter()->AddItem(itemSave);
106         GetFooter()->AddActionEventListener(*this);
107         GetFooter()->Invalidate(true);
108
109         SetFormBackEventListener(this);
110         SetActionBarsVisible(FORM_ACTION_BAR_FOOTER, true);
111
112         __pNameEditField = new (std::nothrow) EditField();
113         __pNameEditField->Construct(RECT_NAME_EDIT_FIELD, EDIT_FIELD_STYLE_NORMAL, INPUT_STYLE_OVERLAY,
114                         EDIT_FIELD_TITLE_STYLE_NONE, true, ALBUM_MAX_LENGTH);
115         __pNameEditField->AddTextEventListener(*this);
116         __pNameEditField->SetColor(EDIT_STATUS_NORMAL, COLOR_NAME_EDIT_FIELD);
117         __pNameEditField->SetOverlayKeypadCommandButtonVisible(false);
118
119         AddControl(__pNameEditField);
120         __pNameEditField->AddActionEventListener(*this);
121
122         return E_SUCCESS;
123 }
124
125 void
126 AlbumNameEditorForm::OnTextValueChanged(const Control& source)
127 {
128         AppLogDebug("ENTER");
129         String inputString = __pNameEditField->GetText();
130
131         if (inputString.Contains(DIRECTORY_SEPARATOR) == true)
132         {
133                 if (__pMessageBox == null)
134                 {
135                         __pMessageBox = new (std::nothrow) MessageBox();
136                         __pMessageBox->Construct(L"", ResourceManager::GetString(L"IDS_COM_POP_INVALID_CHARACTERS"),
137                                         MSGBOX_STYLE_NONE, 3000);
138                         int modalResult = 0;
139                         _overlayMsg = true;
140                         __pMessageBox->ShowAndWait(modalResult);
141                         _overlayMsg = false;
142                         delete __pMessageBox;
143                         __pMessageBox = null;
144                 }
145
146                 __pNameEditField->SetText(__nameEditFieldTempText);
147                 __pNameEditField->SetFocus();
148
149                 return;
150         }
151
152         ByteBuffer* pBuffer = StringUtil::StringToUtf8N(inputString);
153         int remaining = 0;
154         if (pBuffer != null)
155         {
156                 remaining = pBuffer->GetRemaining();
157         }
158
159         if (remaining >= ALBUM_MAX_LENGTH)
160         {
161                 if (__pMessageBox == null)
162                 {
163                         __pMessageBox = new (std::nothrow) MessageBox();
164                         __pMessageBox->Construct(L"",
165                                         ResourceManager::GetString(L"IDS_COM_POP_MAXIMUM_NUMBER_OF_CHARACTERS_REACHED"),
166                                         MSGBOX_STYLE_NONE, 3000);
167                         int modalResult = 0;
168                         __pMessageBox->ShowAndWait(modalResult);
169                         delete __pMessageBox;
170                         __pMessageBox = null;
171                 }
172
173                 __pNameEditField->SetText(__nameEditFieldTempText);
174                 __pNameEditField->SetFocus();
175
176                 return;
177         }
178         __nameEditFieldTempText = inputString;
179
180         FooterItemStatus itemStatus;
181         GetFooter()->GetItemStatus(0, itemStatus);
182         if (inputString.GetLength() == 0)
183         {
184                 if (itemStatus != FOOTER_ITEM_STATUS_DISABLED)
185                 {
186                         GetFooter()->SetItemEnabled(0, false);
187                 }
188         }
189         else
190         {
191                 if (itemStatus == FOOTER_ITEM_STATUS_DISABLED)
192                 {
193                         GetFooter()->SetItemEnabled(0, true);
194                 }
195         }
196         GetFooter()->Invalidate(true);
197
198         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
199 }
200
201 void
202 AlbumNameEditorForm::OnTextValueChangeCanceled(const Control& source)
203 {
204         AppLogDebug("ENTER");
205         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
206 }
207
208 result
209 AlbumNameEditorForm::OnTerminating(void)
210 {
211         AppLogDebug("ENTER");
212         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
213
214         return E_SUCCESS;
215 }
216
217 void AlbumNameEditorForm::OnFileOpInvalidate(enum FileActionMode actionId)
218 {
219         Invalidate(true);
220 }
221
222 void AlbumNameEditorForm::OnFileOpComplete(enum FileActionMode actionId, enum FileActionCompleteRes res)
223 {
224         _overlayMsg = false;
225         if (res != COMPLETE_SUCCESS)
226         {
227                 __pNameEditField->SetText(__nameEditFieldPreText);
228                 __pNameEditField->HideKeypad();
229                 __pNameEditField->Draw();
230                 SceneManager* pSceneManager = SceneManager::GetInstance();
231                 pSceneManager->GoBackward(BackwardSceneTransition(__callerSceneId));
232         }
233 }
234
235 void
236 AlbumNameEditorForm::OnRenameAlbum(String& strOri)
237 {
238         AppLogDebug("Rename Album");
239         String path;
240         int index = 0;
241         strOri.Reverse();
242         strOri.IndexOf(DIRECTORY_SEPARATOR, 0, index);
243         strOri.Reverse();
244         strOri.SubString(0, (strOri.GetLength() - index), path);
245         path.Append(__nameEditFieldText);
246
247         //Rename
248         result r = __pPresentationModel->RenameAlbum(strOri, path, this);
249
250         if (IsFailed(r))
251         {
252                 AppLogDebug("Unable to rename Album %s", GetErrorMessage(r));
253                 if (__pMessageBox == null)
254                 {
255                         __pMessageBox = new (std::nothrow) MessageBox();
256                         if (r == E_FILE_ALREADY_EXIST)
257                         {
258                                 __pMessageBox->Construct(L"", ResourceManager::GetString(L"IDS_MEDIABR_BODY_UNABLE_TO_RENAME_ALBUM_NAME_ALREADY_IN_USE_ABB"),
259                                                           MSGBOX_STYLE_NONE, 3000);
260                         }
261                         else
262                         {
263                                 __pMessageBox->Construct(L"", ResourceManager::GetString(L"IDS_MEDIABR_POP_UNABLE_TO_RENAME"),
264                                                           MSGBOX_STYLE_NONE, 3000);
265                         }
266                         int modalResult = 0;
267                         _overlayMsg = true;
268                         __pMessageBox->ShowAndWait(modalResult);
269                         _overlayMsg = false;
270                         delete __pMessageBox;
271                         __pMessageBox = null;
272                 }
273         }
274         else
275         {
276                 //Add a popup here
277                 Rectangle clientRect= GetClientAreaBounds();
278                 Rectangle rect(0, 0, clientRect.width, clientRect.height);
279                 __pProgressAnim = new (std::nothrow) ProgressAnimation;
280                 __pProgressAnim->Construct(rect);
281                 AddControl(__pProgressAnim);
282                 __pProgressAnim->SetShowState(true);
283                 GetFooter()->SetEnabled(false);
284                 __pProgressAnim->AnimationStart();
285                 __pProgressAnim->Draw();
286                 _overlayMsg = true;
287         }
288 }
289
290 void
291 AlbumNameEditorForm::OnCreateAlbum(void)
292 {
293         String path;
294         __pNameEditField->HideKeypad();
295         __pNameEditField->RequestRedraw();
296         path.Append(Tizen::System::Environment::GetMediaPath());
297         path.Append(__nameEditFieldText);
298
299         if (__previousSceneId != IDSCN_ALL_LIST_EDITOR && __previousSceneId != IDSCN_ALL_LIST_SELECTION)
300         {
301                 Directory::Create(path, true);
302         }
303
304         __nameEditFieldPreText = __nameEditFieldText;
305
306         if (path.IsEmpty())
307         {
308                 AppLogDebug("EXIT 1(%s)", GetErrorMessage(GetLastResult()));
309                 delete __pMoveIndexList;
310                 __pMoveIndexList = null;
311                 return ;
312         }
313
314         if (__pMoveIndexList == null || __pMoveIndexList->GetCount() <= 0)
315         {
316                 AppLogDebug("EXIT 2(%s)", GetErrorMessage(GetLastResult()));
317                 delete __pMoveIndexList;
318                 __pMoveIndexList = null;
319                 return ;
320         }
321
322         delete __pFileMove;
323         GetMoveFileIndexList(path, __pMoveIndexList, __pFilePresentationModel);
324         __pFileMove = new (std::nothrow) FileMoveTimer(path, __pMoveIndexList, __pFilePresentationModel, this);
325         if (__fileActionMode == FILE_ACTION_COPY)
326         {
327                 AppLogDebug("Rash: Setting to Copy in AlbumNameEditor");
328                 __pFileMove->SetCopy();
329         }
330         else
331         {
332                 __pFileMove->ClearCopy();
333         }
334         result r = __pFileMove->StartTimer();
335
336         if (IsFailed(r))
337         {
338                 delete __pFileMove;
339                 __pFileMove = null;
340                 __pNameEditField->SetText(__nameEditFieldPreText);
341                 __pNameEditField->HideKeypad();
342                 __pNameEditField->Draw();
343                 AppLogDebug("MoveToContentFileList content failed (%s)", GetErrorMessage(r));
344                 SceneManager* pSceneManager = SceneManager::GetInstance();
345                 pSceneManager->GoBackward(BackwardSceneTransition(__callerSceneId));
346         }
347         else
348         {
349                 AppLogDebug("MoveToContentFileList content in progress");
350                 _overlayMsg = true;
351                 __fileActionMode = FILE_ACTION_MOVE;
352         }
353 }
354
355 void AlbumNameEditorForm::OnAlbumRenameComplete(void)
356 {
357         //Hide popup here..
358         __pProgressAnim->AnimationStop();
359         __pProgressAnim->SetShowState(false);
360         RemoveControl(__pProgressAnim);
361         _overlayMsg = false;
362         GetFooter()->SetEnabled(true);
363         SceneManager* pSceneManager = SceneManager::GetInstance();
364         pSceneManager->GoBackward((BackwardSceneTransition(IDSCN_ALBUM_LIST)));
365 }
366
367 void
368 AlbumNameEditorForm::OnActionPerformed(const Control& source, int actionId)
369 {
370         AppLogDebug("ENTER");
371         String path;
372         __nameEditFieldText = __pNameEditField->GetText();
373
374         if (__albumNameEditorMode == ALBUM_NAME_EDITOR_MODE_RENAME)
375         {
376                 IList* pAlbumInfoList = __pPresentationModel->GetAlbumInfoList();
377                 AlbumInfo* pAlbumInfo = static_cast<AlbumInfo*>(pAlbumInfoList->GetAt(__folderIndex));
378                 path = pAlbumInfo->GetDirectory(0);
379         }
380
381         switch (actionId)
382         {
383         case IDA_BUTTON_CREATE_NAME_SAVE:
384         {
385                 if (__albumNameEditorMode == ALBUM_NAME_EDITOR_MODE_CREATE)
386                 {
387                         OnCreateAlbum();
388                 }
389                 else if (__albumNameEditorMode == ALBUM_NAME_EDITOR_MODE_RENAME)
390                 {
391                         String albumName = __pNameEditField->GetText();
392                         if (__originalText.Equals(albumName , true))
393                         {
394                                 SceneManager* pSceneManager = SceneManager::GetInstance();
395                                 pSceneManager->GoBackward(BackwardSceneTransition());
396                         }
397                         else
398                         {
399                                 OnRenameAlbum(path);
400                         }
401                 }
402                 break;
403         }
404
405         default:
406                 break;
407         }
408         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
409 }
410
411 void
412 AlbumNameEditorForm::OnFormBackRequested(Form& source)
413 {
414         AppLogDebug("ENTER");
415         if (__albumNameEditorMode == ALBUM_NAME_EDITOR_MODE_CREATE)
416         {
417                 SceneManager* pSceneManager = SceneManager::GetInstance();
418                 pSceneManager->GoForward(ForwardSceneTransition(__callerSceneId));
419         }
420         else
421         {
422                 SceneManager* pSceneManager = SceneManager::GetInstance();
423                 pSceneManager->GoForward(ForwardSceneTransition(IDSCN_ALBUM_LIST_EDITOR));
424         }
425         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
426 }
427
428 void
429 AlbumNameEditorForm::OnSceneActivatedN(const SceneId& previousSceneId,
430                 const SceneId& currentSceneId, IList* pArgs)
431 {
432         AppLogDebug("ENTER");
433         __pPresentationModel = AlbumListPresentationModel::GetInstance();
434         __previousSceneId = previousSceneId;
435
436         __pPresentationModel->AddContentEventListener(this);
437
438         if (pArgs != null)
439         {
440                 Integer* pAlbumTypePtr = static_cast<Integer*>(pArgs->GetAt(ALBUM_FIRST_ELEMENT));
441                 if (pAlbumTypePtr->ToInt() == ALBUM_CREATE)
442                 {
443                         AppLogDebug("ALBUM_NAME_EDITOR_MODE_CREATE");
444                         __albumNameEditorMode = ALBUM_NAME_EDITOR_MODE_CREATE;
445                 }
446                 else if (pAlbumTypePtr->ToInt() == ALBUM_RENAME)
447                 {
448                         AppLogDebug("ALBUM_NAME_EDITOR_MODE_RENAME");
449                         __albumNameEditorMode = ALBUM_NAME_EDITOR_MODE_RENAME;
450                 }
451                 pArgs->RemoveAt(ALBUM_FIRST_ELEMENT, true);
452         }
453
454         if (pArgs != null)
455         {
456                 Integer* operationTypePtr = static_cast<Integer*>(pArgs->GetAt(ALBUM_FIRST_ELEMENT));
457                 if (operationTypePtr->ToInt() == FILE_ACTION_MOVE)
458                 {
459                         AppLogDebug("Rash: FILE_MOVE_ACTION");
460                         __fileActionMode = FILE_ACTION_MOVE;
461                 }
462                 else if (operationTypePtr->ToInt() == FILE_ACTION_COPY)
463                 {
464                         AppLogDebug("Rash: FILE_COPY_ACTION");
465                         __fileActionMode = FILE_ACTION_COPY;
466                 }
467                 pArgs->RemoveAt(ALBUM_FIRST_ELEMENT, true);
468         }
469
470         if (previousSceneId == IDSCN_ALBUM_LIST_EDITOR)
471         {
472                 if (pArgs != null)
473                 {
474                         IEnumerator* pEnum = pArgs->GetEnumeratorN();
475                         if (pEnum->MoveNext() == E_SUCCESS)
476                         {
477                                 Integer *index = static_cast<Integer*>(pEnum->GetCurrent());
478                                 if (index != null)
479                                 {
480                                         __folderIndex = index->ToInt();
481                                 }
482                         }
483                         delete pEnum;
484                         delete pArgs;
485                 }
486         }
487
488         if (previousSceneId == IDSCN_ALL_LIST_EDITOR
489                         || previousSceneId == IDSCN_IMAGE_LIST_EDITOR
490                         || previousSceneId == IDSCN_VIDEO_LIST_EDITOR
491                         || previousSceneId == IDSCN_ALL_LIST_SELECTION)
492         {
493                 if (previousSceneId == IDSCN_ALL_LIST_EDITOR)
494                 {
495                         AppLogDebug("[MOVE] previousSceneId = IDSCN_ALL_LIST_EDITOR");
496                 }
497                 else if (previousSceneId == IDSCN_ALL_LIST_SELECTION)
498                 {
499                         AppLogDebug("[MOVE] previousSceneId = IDSCN_ALL_LIST_SELECTION");
500                 }
501
502                 __callerSceneId = previousSceneId;
503         }
504         else
505         {
506                 __callerSceneId = EMPTY_SPACE;
507         }
508
509         if (__albumNameEditorMode == ALBUM_NAME_EDITOR_MODE_CREATE)
510         {
511                 __nameEditFieldText = EMPTY_SPACE;
512                 __nameEditFieldPreText = EMPTY_SPACE;
513                 __pNameEditField->SetText(__nameEditFieldText);
514                 __nameEditFieldTempText = __nameEditFieldText;
515                 __pNameEditField->ShowKeypad();
516                 __pNameEditField->SetFocus();
517
518                 GetHeader()->SetTitleText(ResourceManager::GetString(L"IDS_MEDIABR_OPT_CREATE_ALBUM"));
519                 GetFooter()->SetItemEnabled(0, false);
520                 GetFooter()->Invalidate(true);
521         }
522         else
523         {
524
525                 IList* pAlbumInfoList = __pPresentationModel->GetAlbumInfoList();
526                 AlbumInfo* pAlbumInfo = static_cast<AlbumInfo*>(pAlbumInfoList->GetAt(__folderIndex));
527
528                 String strName = pAlbumInfo->GetAlbumName();
529                 __nameEditFieldText = strName;
530                 __nameEditFieldPreText = strName;
531                 __pNameEditField->SetText(__nameEditFieldText);
532                 __nameEditFieldTempText = __nameEditFieldText;
533                 GetHeader()->SetTitleText(ResourceManager::GetString(L"IDS_COM_BODY_RENAME"));
534                 __pNameEditField->SetOverlayKeypadCommandButtonVisible(false);
535                 __pNameEditField->ShowKeypad();
536                 __pNameEditField->SetFocus();
537                 __originalText = __pNameEditField->GetText();
538         }
539
540         if (pArgs != NULL)
541         {
542                 __pMoveIndexList = pArgs;
543         }
544
545         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
546 }
547
548 void
549 AlbumNameEditorForm::OnSceneDeactivated(const SceneId& currentSceneId,
550                 const SceneId& nextSceneId)
551 {
552         AppLogDebug("ENTER");
553         __pPresentationModel->RemoveContentEventListener(*this);
554         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
555 }
556
557 void
558 AlbumNameEditorForm::OnContentUpdated(void)
559 {
560         AppLogDebug("ENTER");
561
562         if ( __mountState == false )
563         {
564                 SceneManager* pSceneManager = SceneManager::GetInstance();
565                 pSceneManager->GoForward(ForwardSceneTransition(IDSCN_ALBUM_LIST));
566                 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
567         }
568         else
569         {
570                 __mountState = false;
571         }
572 }
573
574 void
575 AlbumNameEditorForm::OnDeviceStateChanged(DeviceType deviceType, const Tizen::Base::String& state)
576 {
577         AppLogDebug("ENTER");
578
579         if (deviceType == DEVICE_TYPE_STORAGE_CARD && state == DEVICE_STORAGE_CARD_UNMOUNTED)
580         {
581                 __mountState = false;
582                 SceneManager* pSceneManager = SceneManager::GetInstance();
583                 pSceneManager->GoForward(ForwardSceneTransition(IDSCN_ALBUM_LIST));
584                 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
585         }
586         else
587         {
588                 __mountState = true;
589         }
590         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
591 }