1. Fixed jira 31308 2. AlbumNameEditor look changes
[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 <FApp.h>
23 #include "GlAlbumInfo.h"
24 #include "GlAlbumNameEditorForm.h"
25 #include "GlAlbumListPresentationModel.h"
26 #include "GlFileListPresentationModel.h"
27 #include "GlProgressAnimation.h"
28 #include "GlResourceManager.h"
29 #include "GlTypes.h"
30 #include "GlFileMoveTimer.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::Media;
40 using namespace Tizen::Io;
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()
51         : __pNameEditField(null)
52         , __folderIndex(0)
53         , __albumNameEditorMode(ALBUM_NAME_EDITOR_MODE_RENAME)
54         , __pMessageBox(null)
55         , __pPresentationModel(null)
56         , __pFilePresentationModel(null)
57         , __pMoveIndexList(null)
58         ,__pFileMove(null)
59         ,__pProgressAnim(null)
60 {
61         AppLogDebug("ENTER");
62         __overlayMsg = false;
63         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
64 }
65
66 AlbumNameEditorForm::~AlbumNameEditorForm()
67 {
68         delete __pFileMove;
69         AppLogDebug("ENTER");
70         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
71 }
72
73 result
74 AlbumNameEditorForm::Initialize(void)
75 {
76         AppLogDebug("ENTER");
77         result r = Construct(FORM_STYLE_HEADER | FORM_STYLE_FOOTER | FORM_STYLE_NORMAL | FORM_STYLE_INDICATOR);
78         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
79
80         return r;
81 }
82
83 result
84 AlbumNameEditorForm::OnInitializing(void)
85 {
86         AppLogDebug("ENTER");
87         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
88         __pFilePresentationModel = FileListPresentationModel::GetInstance();
89
90         Header* pHeader = GetHeader();
91
92         pHeader->SetStyle(HEADER_STYLE_TITLE);
93
94         GetFooter()->SetBackButtonEnabled(true);
95         GetFooter()->SetBackButton();
96
97         FooterItem itemSave;
98         itemSave.Construct(ACTION_ID_BUTTON_CREATE_NAME_SAVE);
99         itemSave.SetText(L"Save");
100
101         GetFooter()->AddItem(itemSave);
102         GetFooter()->AddActionEventListener(*this);
103         GetFooter()->Invalidate(true);
104
105         SetFormBackEventListener(this);
106         SetActionBarsVisible(FORM_ACTION_BAR_FOOTER, true);
107
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* buffer = StringUtil::StringToUtf8N(inputString);
150         int remaining = 0;
151         if (buffer != null)
152         {
153                 remaining = buffer->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
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(const String& strOri)
237 {
238         AppLogDebug("Rename Album");
239         String path;
240         path.Append(Tizen::System::Environment::GetMediaPath());
241         path.Append(__nameEditFieldText);
242
243         //Rename
244         result r = __pPresentationModel->RenameAlbum(strOri, path, this);
245         if (IsFailed(r))
246         {
247                 AppLogDebug("Unable to rename Album %s", GetErrorMessage(r));
248                 if (__pMessageBox == null)
249                 {
250                         __pMessageBox = new (std::nothrow) MessageBox();
251                         __pMessageBox->Construct(L"", L"Unable to rename", MSGBOX_STYLE_NONE, 3000);
252                         int modalResult = 0;
253                         __overlayMsg = true;
254                         __pMessageBox->ShowAndWait(modalResult);
255                         __overlayMsg = false;
256                         delete __pMessageBox;
257                         __pMessageBox = null;
258                         SceneManager* pSceneManager = SceneManager::GetInstance();
259                         pSceneManager->GoBackward(BackwardSceneTransition(__callerSceneId));
260                 }
261         }
262         else
263         {
264                 //Add a popup here..
265                 Rectangle clientRect= GetClientAreaBounds();
266                 Rectangle rect(0,0,clientRect.width,clientRect.height);
267                 __pProgressAnim = new (std::nothrow) ProgressAnimation;
268                 __pProgressAnim->Construct(rect);
269                 AddControl(*__pProgressAnim);
270                 __pProgressAnim->SetShowState(true);
271                 __pProgressAnim->AnimationStart();
272                 __pProgressAnim->Draw();
273                 __overlayMsg = true;
274         }
275
276 }
277
278 void
279 AlbumNameEditorForm::OnCreateAlbum(void)
280 {
281         String path;
282         __pNameEditField->HideKeypad();
283         __pNameEditField->RequestRedraw();
284         path.Append(Tizen::System::Environment::GetMediaPath());
285         path.Append(__nameEditFieldText);
286
287         if (__previousSceneId != IDSCN_ALL_LIST_EDITOR && __previousSceneId != IDSCN_ALL_LIST_SELECTION)
288         {
289                 Directory::Create(path, true);
290         }
291
292         __nameEditFieldPreText = __nameEditFieldText;
293
294         if (path.IsEmpty())
295         {
296                 AppLogDebug("EXIT 1(%s)", GetErrorMessage(GetLastResult()));
297                 delete __pMoveIndexList;
298                 __pMoveIndexList = null;
299                 return ;
300         }
301
302         if (__pMoveIndexList == null || __pMoveIndexList->GetCount() <= 0)
303         {
304                 AppLogDebug("EXIT 2(%s)", GetErrorMessage(GetLastResult()));
305                 delete __pMoveIndexList;
306                 __pMoveIndexList = null;
307                 return ;
308         }
309
310         delete __pFileMove;
311         GetMoveFileIndexList(path, __pMoveIndexList, __pFilePresentationModel);
312         __pFileMove = new FileMoveTimer(path, __pMoveIndexList, __pFilePresentationModel, this);
313         result r = __pFileMove->StartTimer();
314
315         if (IsFailed(r))
316         {
317                 delete __pFileMove;
318                 __pFileMove = null;
319                 __pNameEditField->SetText(__nameEditFieldPreText);
320                 __pNameEditField->HideKeypad();
321                 __pNameEditField->Draw();
322                 AppLogDebug("MoveToContentFileList content failed (%s)", GetErrorMessage(r));
323                 SceneManager* pSceneManager = SceneManager::GetInstance();
324                 pSceneManager->GoBackward(BackwardSceneTransition(__callerSceneId));
325         }
326         else
327         {
328                 AppLogDebug("MoveToContentFileList content in progress");
329                 __overlayMsg = true;
330         }
331 }
332
333 void AlbumNameEditorForm::OnAlbumRenameComplete(void)
334 {
335         //Hide popup here..
336         __pProgressAnim->AnimationStop();
337         __pProgressAnim->SetShowState(false);
338         RemoveControl(*__pProgressAnim);
339         __overlayMsg = false;
340         SceneManager* pSceneManager = SceneManager::GetInstance();
341         pSceneManager->GoForward(ForwardSceneTransition(IDSCN_ALBUM_LIST));
342 }
343
344
345 void
346 AlbumNameEditorForm::OnActionPerformed(const Control& source, int actionId)
347 {
348         AppLogDebug("ENTER");
349         SceneManager* pSceneManager = SceneManager::GetInstance();
350
351         String pPath;
352         String strOri;
353         __nameEditFieldText = __pNameEditField->GetText();
354
355         if (__albumNameEditorMode == ALBUM_NAME_EDITOR_MODE_RENAME)
356         {
357                 IList* pAlbumInfoList = __pPresentationModel->GetAlbumInfoList();
358                 AlbumInfo* pAlbumInfo = static_cast<AlbumInfo*>(pAlbumInfoList->GetAt(__folderIndex));
359                 pPath = pAlbumInfo->GetDirectory(0);
360                 strOri = pPath;
361         }
362
363         switch (actionId)
364         {
365         case ACTION_ID_BUTTON_CREATE_NAME_SAVE:
366         {
367                 if (__albumNameEditorMode == ALBUM_NAME_EDITOR_MODE_CREATE)
368                 {
369                         //Create new Album
370                         OnCreateAlbum();
371                 }
372                 else if (__albumNameEditorMode == ALBUM_NAME_EDITOR_MODE_RENAME)
373                 {
374                         //Rename a album
375                         OnRenameAlbum(strOri);
376                 }
377                 break;
378         }
379
380         default:
381                 break;
382         }
383         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
384 }
385
386 void
387 AlbumNameEditorForm::OnFormBackRequested(Form& source)
388 {
389         AppLogDebug("ENTER");
390         if (__albumNameEditorMode == ALBUM_NAME_EDITOR_MODE_CREATE)
391         {
392                 SceneManager* pSceneManager = SceneManager::GetInstance();
393                 pSceneManager->GoForward(ForwardSceneTransition(__callerSceneId));
394         }
395         else
396         {
397                 SceneManager* pSceneManager = SceneManager::GetInstance();
398                 pSceneManager->GoForward(ForwardSceneTransition(IDSCN_ALBUM_LIST_EDITOR));
399         }
400         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
401 }
402
403 void
404 AlbumNameEditorForm::OnSceneActivatedN(const SceneId& previousSceneId,
405                 const SceneId& currentSceneId, IList* pArgs)
406 {
407         AppLogDebug("ENTER");
408         __pPresentationModel = AlbumListPresentationModel::GetInstance();
409         __previousSceneId = previousSceneId;
410
411         if (pArgs != null)
412         {
413                 Integer* albumTypePtr = static_cast<Integer*>(pArgs->GetAt(ALBUM_FIRST_ELEMENT));
414                 if (albumTypePtr->ToInt() == ALBUM_CREATE)
415                 {
416                         AppLogDebug("ALBUM_NAME_EDITOR_MODE_CREATE");
417                         __albumNameEditorMode = ALBUM_NAME_EDITOR_MODE_CREATE;
418                 }
419                 else if (albumTypePtr->ToInt()  == ALBUM_RENAME)
420                 {
421                         AppLogDebug("ALBUM_NAME_EDITOR_MODE_RENAME");
422                         __albumNameEditorMode = ALBUM_NAME_EDITOR_MODE_RENAME;
423                 }
424                 pArgs->RemoveAt(ALBUM_FIRST_ELEMENT, true);
425         }
426
427         if (previousSceneId == IDSCN_ALBUM_LIST_EDITOR)
428         {
429                 if (pArgs != null)
430                 {
431                         IEnumerator* pEnum = pArgs->GetEnumeratorN();
432                         if (pEnum->MoveNext() == E_SUCCESS)
433                         {
434                                 Integer *index = static_cast<Integer*>(pEnum->GetCurrent());
435                                 if (index != null)
436                                 {
437                                         __folderIndex = index->ToInt();
438                                 }
439                         }
440                         delete pEnum;
441                         delete pArgs;
442                 }
443         }
444
445         if (previousSceneId == IDSCN_ALL_LIST_EDITOR
446                         || previousSceneId == IDSCN_IMAGE_LIST_EDITOR
447                         || previousSceneId == IDSCN_VIDEO_LIST_EDITOR
448                         || previousSceneId == IDSCN_ALL_LIST_SELECTION)
449         {
450                 if (previousSceneId == IDSCN_ALL_LIST_EDITOR)
451                 {
452                         AppLogDebug("[MOVE] previousSceneId = IDSCN_ALL_LIST_EDITOR");
453                 }
454                 else if (previousSceneId == IDSCN_ALL_LIST_SELECTION)
455                 {
456                         AppLogDebug("[MOVE] previousSceneId = IDSCN_ALL_LIST_SELECTION");
457                 }
458
459                 __callerSceneId = previousSceneId;
460         }
461         else
462         {
463                 __callerSceneId = EMPTY_SPACE;
464         }
465
466
467
468         if (__albumNameEditorMode == ALBUM_NAME_EDITOR_MODE_CREATE)
469         {
470
471                 __nameEditFieldText = EMPTY_SPACE;
472                 __nameEditFieldPreText = EMPTY_SPACE;
473                 __pNameEditField->SetText(__nameEditFieldText);
474                 __nameEditFieldTempText = __nameEditFieldText;
475                 __pNameEditField->SetFocus();
476            GetHeader()->SetTitleText(L"Create album");
477        GetFooter()->SetItemEnabled(0, false);
478        GetFooter()->Invalidate(true);
479         }
480         else
481         {
482                 String strName;
483
484                 IList* pAlbumInfoList = __pPresentationModel->GetAlbumInfoList();
485                 AlbumInfo* pAlbumInfo = static_cast<AlbumInfo*>(pAlbumInfoList->GetAt(__folderIndex));
486
487                 strName = pAlbumInfo->GetAlbumName();
488                 __nameEditFieldText = strName;
489                 __nameEditFieldPreText = strName;
490                 __pNameEditField->SetText(__nameEditFieldText);
491                  GetHeader()->SetTitleText(L"Rename");
492                 __pNameEditField->SetOverlayKeypadCommandButtonVisible(false);
493                 __pNameEditField->SetFocus();
494
495         }
496
497         if (pArgs != NULL)
498         {
499                 __pMoveIndexList = pArgs;
500         }
501
502         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
503 }
504
505 void
506 AlbumNameEditorForm::OnSceneDeactivated(const SceneId& currentSceneId,
507                 const SceneId& nextSceneId)
508 {
509         AppLogDebug("ENTER");
510         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
511 }