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