Remove ini file
[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 "GlResourceManager.h"
28 #include "GlTypes.h"
29 #include "GlFileMoveTimer.h"
30
31 using namespace Tizen::App;
32 using namespace Tizen::Base;
33 using namespace Tizen::Base::Collection;
34 using namespace Tizen::Base::Runtime;
35 using namespace Tizen::Base::Utility;
36 using namespace Tizen::Content;
37 using namespace Tizen::Graphics;
38 using namespace Tizen::Media;
39 using namespace Tizen::Io;
40 using namespace Tizen::System;
41 using namespace Tizen::Ui;
42 using namespace Tizen::Ui::Controls;
43 using namespace Tizen::Ui::Scenes;
44
45 static const Rectangle RECT_NAME_EDIT_FIELD (0, 0, 720, 100);
46 static const unsigned int COLOR_NAME_EDIT_FIELD = Color32<255, 255, 255>::Value;
47 static const int ALBUM_MAX_LENGTH = 255;
48
49 AlbumNameEditorForm::AlbumNameEditorForm()
50 : __pNameEditField(null)
51 , __folderIndex(0)
52 , __albumNameEditorMode(ALBUM_NAME_EDITOR_MODE_RENAME)
53 , __pMessageBox(null)
54 , __pPresentationModel(null)
55 , __pFilePresentationModel(null)
56 , __pMoveIndexList(null)
57 ,__pFileMove(null)
58 {
59         AppLogDebug("ENTER");
60         __overlayMsg = false;
61         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
62 }
63
64 AlbumNameEditorForm::~AlbumNameEditorForm()
65 {
66         delete __pFileMove;
67         AppLogDebug("ENTER");
68         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
69 }
70
71 result
72 AlbumNameEditorForm::Initialize(void)
73 {
74         AppLogDebug("ENTER");
75         result r = Construct(FORM_STYLE_HEADER | FORM_STYLE_NORMAL | FORM_STYLE_INDICATOR);
76         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
77
78         return r;
79 }
80
81 result
82 AlbumNameEditorForm::OnInitializing(void)
83 {
84         AppLogDebug("ENTER");
85         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
86         __pFilePresentationModel = FileListPresentationModel::GetInstance();
87         return E_SUCCESS;
88 }
89
90 void
91 AlbumNameEditorForm::OnTextValueChanged(const Control& source)
92 {
93         AppLogDebug("ENTER");
94         String inputString = __pNameEditField->GetText();
95
96         if (inputString.Contains(DIRECTORY_SEPARATOR) == true)
97         {
98                 if (__pMessageBox == null)
99                 {
100                         __pMessageBox = new (std::nothrow) MessageBox();
101                         __pMessageBox->Construct(L"", ResourceManager::GetString(L"IDS_COM_POP_INVALID_CHARACTERS"),
102                                         MSGBOX_STYLE_NONE, 3000);
103                         int modalResult = 0;
104                         __overlayMsg = true;
105                         __pMessageBox->ShowAndWait(modalResult);
106                         __overlayMsg = false;
107                         delete __pMessageBox;
108                         __pMessageBox = null;
109                 }
110
111                 __pNameEditField->SetText(__nameEditFieldTempText);
112                 __pNameEditField->SetFocus();
113
114                 return;
115         }
116
117         ByteBuffer* buffer = StringUtil::StringToUtf8N(inputString);
118         int remaining = 0;
119         if (buffer != null)
120         {
121                 remaining = buffer->GetRemaining();
122         }
123
124         if (remaining >= ALBUM_MAX_LENGTH)
125         {
126                 if (__pMessageBox == null)
127                 {
128                         __pMessageBox = new (std::nothrow) MessageBox();
129                         __pMessageBox->Construct(L"",
130                                         ResourceManager::GetString(L"IDS_COM_POP_MAXIMUM_NUMBER_OF_CHARACTERS_REACHED"),
131                                         MSGBOX_STYLE_NONE, 3000);
132                         int modalResult = 0;
133                         __pMessageBox->ShowAndWait(modalResult);
134                         delete __pMessageBox;
135                         __pMessageBox = null;
136                 }
137
138                 __pNameEditField->SetText(__nameEditFieldTempText);
139                 __pNameEditField->SetFocus();
140
141                 return;
142         }
143         __nameEditFieldTempText = inputString;
144
145         inputString.Trim();
146
147         Header* header = GetHeader();
148         HeaderItemStatus itemStatus;
149         header->GetItemStatus(0, itemStatus);
150         if (inputString.GetLength() == 0)
151         {
152                 if (itemStatus != HEADER_ITEM_STATUS_DISABLED)
153                 {
154                         header->SetItemEnabled(0, false);
155                         header->RequestRedraw(true);
156                 }
157         }
158         else
159         {
160                 if (itemStatus == HEADER_ITEM_STATUS_DISABLED)
161                 {
162                         header->SetItemEnabled(0, true);
163                         header->RequestRedraw(true);
164                 }
165         }
166         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
167 }
168
169 void
170 AlbumNameEditorForm::OnTextValueChangeCanceled(const Control& source)
171 {
172         AppLogDebug("ENTER");
173         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
174 }
175
176 result
177 AlbumNameEditorForm::OnTerminating(void)
178 {
179         AppLogDebug("ENTER");
180         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
181
182         return E_SUCCESS;
183 }
184
185 void AlbumNameEditorForm::OnFileOpInvalidate(enum FileActionMode actionId)
186 {
187         Invalidate(true);
188
189 }
190 void AlbumNameEditorForm::OnFileOpComplete(enum FileActionMode actionId, enum FileActionCompleteRes res)
191 {
192         __overlayMsg = false;
193         if (res != COMPLETE_SUCCESS)
194         {
195                 __pNameEditField->SetText(__nameEditFieldPreText);
196                 __pNameEditField->HideKeypad();
197                 __pNameEditField->Draw();
198                 SceneManager* pSceneManager = SceneManager::GetInstance();
199                 pSceneManager->GoBackward(BackwardSceneTransition(__callerSceneId));
200         }
201 }
202
203 void
204 AlbumNameEditorForm::OnActionPerformed(const Control& source, int actionId)
205 {
206         AppLogDebug("ENTER");
207         SceneManager* pSceneManager = SceneManager::GetInstance();
208
209         String pPath;
210         String strOri;
211         __nameEditFieldText = __pNameEditField->GetText();
212
213         if (__albumNameEditorMode == ALBUM_NAME_EDITOR_MODE_RENAME)
214         {
215                 IList* pAlbumInfoList = __pPresentationModel->GetAlbumInfoList();
216                 AlbumInfo* pAlbumInfo = static_cast<AlbumInfo*>(pAlbumInfoList->GetAt(__folderIndex));
217                 pPath = pAlbumInfo->GetDirectory(0);
218                 strOri = pPath;
219         }
220
221         switch (actionId)
222         {
223         case ACTION_ID_BUTTON_CREATE_NAME_DONE:
224         {
225                 __pNameEditField->HideKeypad();
226                 __pNameEditField->RequestRedraw();
227                 pPath.Append(Tizen::System::Environment::GetMediaPath());
228                 pPath.Append(__nameEditFieldText);
229
230                 if (__previousSceneId != IDSCN_ALL_LIST_EDITOR && __previousSceneId != IDSCN_ALL_LIST_SELECTION)
231                 {
232                         Directory::Create( pPath, true);
233                 }
234
235                 __nameEditFieldPreText = __nameEditFieldText;
236                 ArrayList argsList;
237                 argsList.Construct();
238                 argsList.Add(new (std::nothrow) String(pPath));
239
240
241                 if (pPath.IsEmpty())
242                 {
243                         AppLogDebug("EXIT 1(%s)", GetErrorMessage(GetLastResult()));
244                         delete __pMoveIndexList;
245                         __pMoveIndexList = null;
246                         return ;
247                 }
248
249                 if (__pMoveIndexList == null || __pMoveIndexList->GetCount() <= 0)
250                 {
251                         AppLogDebug("EXIT 2(%s)", GetErrorMessage(GetLastResult()));
252                         delete __pMoveIndexList;
253                         __pMoveIndexList = null;
254                         return ;
255                 }
256
257                 delete __pFileMove;
258                 GetMoveFileIndexList(pPath, __pMoveIndexList, __pFilePresentationModel);
259                 __pFileMove = new FileMoveTimer(pPath, __pMoveIndexList,
260                                 __pFilePresentationModel,
261                                 this );
262                 result r = __pFileMove->StartTimer();
263                 
264                 if (IsFailed(r))
265                 {
266                         delete __pFileMove;
267                         __pFileMove = null;
268                         __pNameEditField->SetText(__nameEditFieldPreText);
269                         __pNameEditField->HideKeypad();
270                         __pNameEditField->Draw();
271                         pSceneManager->GoBackward(BackwardSceneTransition(__callerSceneId));
272                 }
273                 else
274                 {
275                         __overlayMsg = true;
276                 }
277
278                 AppLogDebug("MoveToContentFileList content failed (%s)", GetErrorMessage(r));
279
280                 break;
281         }
282         case ACTION_ID_BUTTON_CREATE_NAME_CANCEL:
283         {
284                 __pNameEditField->SetText(__nameEditFieldPreText);
285                 __pNameEditField->HideKeypad();
286                 __pNameEditField->Draw();
287                 pSceneManager->GoBackward(BackwardSceneTransition(__callerSceneId));
288                 break;
289         }
290
291         default:
292                 break;
293         }
294         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
295 }
296
297 void
298 AlbumNameEditorForm::OnFormBackRequested(Form& source)
299 {
300         AppLogDebug("ENTER");
301         if (__albumNameEditorMode == ALBUM_NAME_EDITOR_MODE_CREATE)
302         {
303                 SceneManager* pSceneManager = SceneManager::GetInstance();
304                 pSceneManager->GoForward(ForwardSceneTransition(__callerSceneId));
305         }
306         else
307         {
308                 SceneManager* pSceneManager = SceneManager::GetInstance();
309                 pSceneManager->GoForward(ForwardSceneTransition(IDSCN_ALBUM_LIST_EDITOR));
310         }
311         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
312 }
313
314 void
315 AlbumNameEditorForm::OnSceneActivatedN(const SceneId& previousSceneId,
316                 const SceneId& currentSceneId, IList* pArgs)
317 {
318         AppLogDebug("ENTER");
319         __pPresentationModel = AlbumListPresentationModel::GetInstance();
320         __previousSceneId = previousSceneId;
321
322         if (previousSceneId == IDSCN_ALBUM_LIST_EDITOR)
323         {
324                 if (pArgs != null)
325                 {
326                         IEnumerator* pEnum = pArgs->GetEnumeratorN();
327                         if (pEnum->MoveNext() == E_SUCCESS)
328                         {
329                                 Integer *index = static_cast<Integer*>(pEnum->GetCurrent());
330                                 if (index != null)
331                                 {
332                                         __folderIndex = index->ToInt();
333                                 }
334                         }
335                         delete pEnum;
336                         delete pArgs;
337                 }
338         }
339
340         if (previousSceneId == IDSCN_ALL_LIST_EDITOR
341                         || previousSceneId == IDSCN_IMAGE_LIST_EDITOR
342                         || previousSceneId == IDSCN_VIDEO_LIST_EDITOR
343                         || previousSceneId == IDSCN_ALL_LIST_SELECTION)
344         {
345                 if (previousSceneId == IDSCN_ALL_LIST_EDITOR)
346                 {
347                         AppLogDebug("[MOVE] previousSceneId = IDSCN_ALL_LIST_EDITOR");
348                 }
349                 else if (previousSceneId == IDSCN_ALL_LIST_SELECTION)
350                 {
351                         AppLogDebug("[MOVE] previousSceneId = IDSCN_ALL_LIST_SELECTION");
352                 }
353
354                 __albumNameEditorMode = ALBUM_NAME_EDITOR_MODE_CREATE;
355                 __callerSceneId = previousSceneId;
356         }
357         else
358         {
359                 __albumNameEditorMode = ALBUM_NAME_EDITOR_MODE_RENAME;
360                 __callerSceneId = EMPTY_SPACE;
361         }
362
363         if (__albumNameEditorMode == ALBUM_NAME_EDITOR_MODE_CREATE)
364         {
365                 Header* pHeader = GetHeader();
366                 AppAssert(pHeader);
367                 pHeader->SetStyle(HEADER_STYLE_BUTTON);
368
369                 HeaderItem doneItem;
370                 HeaderItem cancelItem;
371                 doneItem.Construct(ACTION_ID_BUTTON_CREATE_NAME_DONE);
372                 doneItem.SetText(ResourceManager::GetString(L"IDS_COM_SK_DONE"));
373                 cancelItem.Construct(ACTION_ID_BUTTON_CREATE_NAME_CANCEL);
374                 cancelItem.SetText(ResourceManager::GetString(L"IDS_COM_POP_CANCEL"));
375
376                 pHeader->SetStyle(HEADER_STYLE_BUTTON);
377                 pHeader->AddItem(doneItem);
378                 pHeader->AddItem(cancelItem);
379                 pHeader->AddActionEventListener(*this);
380                 pHeader->SetItemEnabled(0, false);
381
382                 __pNameEditField = new (std::nothrow) EditField();
383                 __pNameEditField->Construct(RECT_NAME_EDIT_FIELD, EDIT_FIELD_STYLE_NORMAL, INPUT_STYLE_OVERLAY,
384                                 EDIT_FIELD_TITLE_STYLE_NONE, true, ALBUM_MAX_LENGTH);
385                 __pNameEditField->AddTextEventListener(*this);
386                 __pNameEditField->SetColor(EDIT_STATUS_NORMAL, COLOR_NAME_EDIT_FIELD);
387
388                 AddControl(*__pNameEditField);
389                 __pNameEditField->AddActionEventListener(*this);
390
391                 __nameEditFieldText = EMPTY_SPACE;
392                 __nameEditFieldPreText = EMPTY_SPACE;
393                 __pNameEditField->SetText(__nameEditFieldText);
394                 __nameEditFieldTempText = __nameEditFieldText;
395
396                 __pNameEditField->SetOverlayKeypadCommandButtonVisible(false);
397                 __pNameEditField->SetFocus();
398
399         }
400         else
401         {
402                 Header* pHeader = GetHeader();
403                 AppAssert(pHeader);
404                 pHeader->SetTitleText(ResourceManager::GetString(L"IDS_COM_BODY_CHANGE_NAME"));
405                 pHeader->SetStyle(HEADER_STYLE_BUTTON);
406
407                 HeaderItem doneItem;
408                 HeaderItem cancelItem;
409                 doneItem.Construct(ACTION_ID_BUTTON_CREATE_NAME_DONE);
410                 doneItem.SetText(ResourceManager::GetString(L"IDS_COM_SK_DONE"));
411                 cancelItem.Construct(ACTION_ID_BUTTON_CREATE_NAME_CANCEL);
412                 cancelItem.SetText(ResourceManager::GetString(L"IDS_COM_POP_CANCEL"));
413
414                 pHeader->SetStyle(HEADER_STYLE_BUTTON);
415                 pHeader->AddItem(doneItem);
416                 pHeader->AddItem(cancelItem);
417                 pHeader->AddActionEventListener(*this);
418
419                 __pNameEditField = new (std::nothrow) EditField();
420                 __pNameEditField->Construct(RECT_NAME_EDIT_FIELD, EDIT_FIELD_STYLE_NORMAL, INPUT_STYLE_OVERLAY,
421                                 EDIT_FIELD_TITLE_STYLE_NONE, true, ALBUM_MAX_LENGTH);
422                 __pNameEditField->AddTextEventListener(*this);
423                 __pNameEditField->SetColor(EDIT_STATUS_NORMAL, COLOR_NAME_EDIT_FIELD);
424
425                 AddControl(*__pNameEditField);
426                 __pNameEditField->AddActionEventListener(*this);
427                 String strName;
428
429                 IList* pAlbumInfoList = __pPresentationModel->GetAlbumInfoList();
430                 AlbumInfo* pAlbumInfo = static_cast<AlbumInfo*>(pAlbumInfoList->GetAt(__folderIndex));
431
432                 strName = pAlbumInfo->GetAlbumName();
433                 __nameEditFieldText = strName;
434                 __nameEditFieldPreText = strName;
435                 __pNameEditField->SetText(__nameEditFieldText);
436
437                 __pNameEditField->SetOverlayKeypadCommandButtonVisible(false);
438                 __pNameEditField->SetFocus();
439         }
440
441         if (__albumNameEditorMode == ALBUM_NAME_EDITOR_MODE_CREATE)
442         {
443                 __nameEditFieldText = EMPTY_SPACE;
444                 __nameEditFieldPreText = EMPTY_SPACE;
445                 __pNameEditField->SetText(__nameEditFieldText);
446         }
447         else
448         {
449                 IList* pAlbumInfoList = __pPresentationModel->GetAlbumInfoList();
450                 AlbumInfo* pAlbumInfo = static_cast<AlbumInfo*>(pAlbumInfoList->GetAt(__folderIndex));
451                 if (pAlbumInfo != null)
452                 {
453                         String strName;
454                         strName = pAlbumInfo->GetAlbumName();
455
456                         __nameEditFieldText = strName;
457                         __nameEditFieldPreText = strName;
458                         __pNameEditField->SetText(__nameEditFieldText);
459                 }
460         }
461
462         if (pArgs != NULL)
463         {
464                 __pMoveIndexList = pArgs;
465         }
466
467         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
468 }
469
470 void
471 AlbumNameEditorForm::OnSceneDeactivated(const SceneId& currentSceneId,
472                 const SceneId& nextSceneId)
473 {
474         AppLogDebug("ENTER");
475         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
476 }