bc0ff3e96f1ecf265f0d2ec21e4af19f0bdd6b2b
[apps/osp/MyFiles.git] / src / MfCreateFolderForm.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: MfCreateFolderForm.cpp
19  * @brief:This file contains implementation of CreateFolderForm class, which implements the Create Folder and rename file/folder functionality
20  * and contains methods that handle the same. Also contains the ResultValues enum
21  */
22
23 #include "MfCreateFolderForm.h"
24 #include "MfDirectoryEntry.h"
25 #include "MfSceneRegister.h"
26 #include "MfTypes.h"
27
28 using namespace Tizen::App;
29 using namespace Tizen::Base;
30 using namespace Tizen::Base::Collection;
31 using namespace Tizen::Base::Utility;
32 using namespace Tizen::Graphics;
33 using namespace Tizen::Io;
34 using namespace Tizen::Locales;
35 using namespace Tizen::System;
36 using namespace Tizen::Ui;
37 using namespace Tizen::Ui::Controls;
38 using namespace Tizen::Ui::Scenes;
39 using namespace Tizen::Text;
40
41 CreateFolderForm::CreateFolderForm(void)
42 : __pFileManager(null)
43 , __pDirectory(null)
44 , __launchSource(-1)
45 , __pHeaderCancel(null)
46 , __pHeaderSave(null)
47 , __pEditFieldFolderName(null)
48 , __pHeader(null)
49 , _pFileProgressingPopup(null)
50 {
51         entryIsDirectory = 0;
52         __textLength = 0;
53         __isCurrentDirectioryToBeRenamed = false;
54 }
55
56 CreateFolderForm::~CreateFolderForm(void)
57 {
58         AppLogDebug("Entry");
59         if (__pHeaderCancel != null)
60         {
61                 delete __pHeaderCancel;
62         }
63
64         if (__pHeaderSave != null)
65         {
66                 delete __pHeaderSave;
67         }
68
69         if (_pFileProgressingPopup != null)
70         {
71                 delete _pFileProgressingPopup;
72         }
73 }
74
75 CreateFolderForm::CreateFolderForm(const CreateFolderForm& createFolderForm)
76 {
77         //Do Nothing
78 }
79
80 CreateFolderForm&
81 CreateFolderForm::operator =(const CreateFolderForm& createFolderForm)
82 {
83         AppLogDebug("Entry");
84
85         if (this != &createFolderForm)
86         {
87                 __pEditFieldFolderName = createFolderForm.__pEditFieldFolderName;
88                 __pHeader = createFolderForm.__pHeader;
89                 __pHeaderSave = createFolderForm.__pHeaderSave;
90                 __pHeaderCancel = createFolderForm.__pHeaderCancel;
91                 __pFileManager = createFolderForm.__pFileManager;
92                 __rootPath = createFolderForm.__rootPath;
93         }
94         return *this;
95 }
96
97 bool
98 CreateFolderForm::Initialize(void)
99 {
100         AppLogDebug("Entry");
101         //Construct the xml from the Application Resource.
102         Form::Construct(L"IDL_CREATE_FOLDER");
103         return true;
104 }
105
106 result
107 CreateFolderForm::OnInitializing(void)
108 {
109         //Set some of the default properties of the
110         AppLogDebug("Entry");
111
112         result r = E_SUCCESS;
113
114         String btnSaveText;
115         String btnCancelText;
116         String btnDoneText;
117
118         //Bitmap* pBackButtonNormal = null;
119         //Bitmap* pBackButtonPressed = null;
120
121         Color editFieldBackground(0x18, 0x1C, 0x1F, 0xFF);
122
123         AppResource* pAppResource = null;
124         pAppResource = UiApp::GetInstance()->GetAppResource();
125
126         if (pAppResource != null)
127         {
128                 pAppResource->GetString(L"IDS_COM_OPT_SAVE", btnSaveText);
129                 pAppResource->GetString(L"IDS_COM_POP_CANCEL", btnCancelText);
130                 pAppResource->GetString(L"IDS_COM_BODY_DONE", btnDoneText);
131                 //pBackButtonNormal = pAppResource->GetBitmapN(IDB_BACK_BUTTON_NORMAL,BITMAP_PIXEL_FORMAT_ARGB8888);
132                 //pBackButtonPressed = pAppResource->GetBitmapN(IDB_BACK_BUTTON_PRESSED,BITMAP_PIXEL_FORMAT_ARGB8888);
133         }
134
135         SetFormBackEventListener(this);
136         AddTouchEventListener(*this);
137         SetActionBarsVisible(FORM_ACTION_BAR_FOOTER, true);
138
139         __previousFormPath = FolderNavigationPresentationModel::GetCurrentPath();
140         __previousFormTab = FolderNavigationPresentationModel::GetStorageType();
141         __previousSceneId = SceneManager::GetInstance()->GetCurrentSceneId();
142
143         __pFooter = GetFooter();
144         if (__pFooter != null)
145         {
146                 __pFooter->SetBackButtonEnabled(true);
147                 __pFooter->SetBackButton();
148
149                 __pFooterItemSave = new (std::nothrow) FooterItem();
150                 __pFooterItemSave->Construct(IDA_BUTTON_SAVE);
151                 __pFooterItemSave->SetText(btnSaveText);
152
153                 __pFooter->AddItem(*__pFooterItemSave);
154                 __pFooter->AddActionEventListener(*this);
155                 __pFooter->Invalidate(true);
156         }
157
158 #if 0
159         __pHeader = GetHeader();
160         if (__pHeader != null)
161         {
162                 __pHeader->SetStyle(HEADER_STYLE_BUTTON);
163
164                 __pHeaderSave = new (std::nothrow) HeaderItem();
165                 __pHeaderSave->Construct(IDA_BUTTON_SAVE);
166                 __pHeaderSave->SetText(btnSaveText);
167
168                 __pHeaderCancel = new (std::nothrow) ButtonItem();
169                 __pHeaderCancel->Construct(BUTTON_ITEM_STYLE_ICON, IDA_BUTTON_CANCEL);
170
171                 if (pBackButtonNormal != null)
172                 {
173                         __pHeaderCancel->SetIcon(BUTTON_ITEM_STATUS_NORMAL, pBackButtonNormal);
174                 }
175
176                 if (pBackButtonPressed != null)
177                 {
178                         __pHeaderCancel->SetIcon(BUTTON_ITEM_STATUS_PRESSED, pBackButtonPressed);
179                 }
180
181                 __pHeader->AddItem(*__pHeaderSave);
182                 __pHeader->SetButton(BUTTON_POSITION_RIGHT, *__pHeaderCancel);
183                 __pHeader->AddActionEventListener(*this);
184         }
185 #endif
186
187         //Get the EditFieldControl from the form and set a few properties, set the overlay command buttons
188         //Show the keypad on form initializing.
189         //Set the action and text event listeners to it.
190         __pEditFieldFolderName = static_cast< EditField* >(GetControl(L"IDC_EDITFIELD_FOLDER_NAME", true));
191         if (__pEditFieldFolderName != null)
192         {
193                 __pEditFieldFolderName->SetKeypadEnabled(true);
194                 __pEditFieldFolderName->SetOverlayKeypadCommandButtonVisible(false);
195                 __pEditFieldFolderName->AddKeypadEventListener(*this);
196                 __pEditFieldFolderName->ShowKeypad();
197                 __pEditFieldFolderName->AddTextEventListener(*this);
198                 __pEditFieldFolderName->AddActionEventListener(*this);
199         }
200         //Create New instance of the Filemanager
201         __pFileManager = FolderEntryEditorPresentationModel::GetInstance();
202         TryCatch(__pFileManager != null, , "Failed to allocate file manager");
203
204         return r;
205
206         CATCH:
207         if (__pHeaderSave != null)
208         {
209                 delete __pHeaderSave;
210                 __pHeaderSave = null;
211         }
212
213         if (__pHeaderCancel != null)
214         {
215                 delete __pHeaderCancel;
216                 __pHeaderCancel = null;
217         }
218
219         r = E_FAILURE;
220         return r;
221 }
222
223 result
224 CreateFolderForm::OnTerminating(void)
225 {
226         AppLogDebug("Entry");
227         result r = E_SUCCESS;
228
229         __pFooter = GetFooter();
230
231         if (__pFooter != null)
232         {
233                 __pFooter->RemoveAllItems();
234                 __pFooter->RemoveAllButtons();
235         }
236
237         return r;
238 }
239
240 void
241 CreateFolderForm::OnFormBackRequested(Tizen::Ui::Controls::Form& source)
242 {
243         AppLogDebug("Entry");
244         //Navigates to the previous folder.
245         TransitionScene();
246 }
247
248 void
249 CreateFolderForm::OnActionPerformed(const Tizen::Ui::Control& source, int actionId)
250 {
251         AppLogDebug("Entry");
252
253         result r = E_SUCCESS;
254         String folderName;
255         String filePath;
256
257         MessageBox messageBox;
258         ResultValues resultValidity = RESULT_VALUES_SUCCESS;
259
260         switch (actionId)
261         {
262         case IDA_BUTTON_SAVE:
263         {
264                 if (__pEditFieldFolderName != null)
265                 {
266                         SetFocus();
267                 }
268
269                 if (__launchSource.ToInt() == SELECT_CREATE_FOLDER)
270                 {
271                         if (__pEditFieldFolderName != null)
272                         {
273                                 folderName.Append(__pEditFieldFolderName->GetText());
274                                 folderName.Trim();
275                                 AppLogDebug("folder value is %ls",folderName.GetPointer());
276                                 __rootPath = FolderNavigationPresentationModel::GetCurrentPath();
277
278                                 filePath.Append(__rootPath);
279
280                                 __fileName.Clear();
281                                 __fileName.Append(__rootPath);
282                                 __fileName.Append(folderName);
283
284
285                                 if (folderName.CompareTo(".") != 0 && folderName.CompareTo("..") != 0 && folderName.CompareTo("/") != 0)
286                                 {
287                                         if (File::IsFileExist(__fileName))
288                                         {
289                                                 resultValidity = RESULT_VALUES_FILE_EXISTS;
290                                         }
291                                 }
292
293                                 //if (__pEditFieldFolderName->GetText().Trim() == "")
294                                 if(folderName == "")
295                                 {
296                                         resultValidity = RESULT_VALUES_NULL;
297                                 }
298
299                                 if (resultValidity == RESULT_VALUES_SUCCESS)
300                                 {
301                                         r = __pFileManager->CreateFolder(filePath, folderName);
302                                         if (r != E_SUCCESS)
303                                         {
304                                                 MessageBox errorMessage;
305                                                 String msgString;
306                                                 //String invalidCharacterString;
307                                                 AppResource::GetInstance()->GetString(L"IDS_CREATE_FOLDER_FAILED", msgString);
308                                                 errorMessage.Construct(L"", msgString, MSGBOX_STYLE_NONE, 1000);
309                                         }
310                                         FolderNavigationPresentationModel::SetCurrentFilePath(filePath);
311                                         TransitionScene();
312                                 }
313                                 else
314                                 {
315                                         String messageString;
316                                         int modalResult = 0;
317
318                                         if (resultValidity == RESULT_VALUES_FILE_EXISTS)
319                                         {
320                                                 //messageString = "Name already exists!\nRetry?";
321                                                 AppResource::GetInstance()->GetString(L"IDS_MF_POP_SAME_NAME_ALREADY_IN_USE", messageString);
322                                         }
323                                         else if (resultValidity == RESULT_VALUES_NULL)
324                                         {
325
326                                                 //messageString = "Name can't be empty!\nRetry?";
327                                                 AppResource::GetInstance()->GetString(L"IDS_NAME_CANT_EMPTY", messageString);
328                                         }
329
330                                         messageBox.Construct(L"", messageString, MSGBOX_STYLE_YESNO, 0);
331
332                                         // Call ShowAndWait - draw, show itself and process events
333
334                                         messageBox.ShowAndWait(modalResult);
335
336                                         switch (modalResult)
337                                         {
338                                         case MSGBOX_RESULT_YES:
339                                         {
340                                                 if (__pEditFieldFolderName != null)
341                                                 {
342                                                         __pEditFieldFolderName->ShowKeypad();
343                                                 }
344                                         }
345                                         break;
346
347                                         case MSGBOX_RESULT_NO:
348                                         {
349                                                 TransitionScene();
350                                         }
351                                         break;
352
353                                         default:
354                                                 break;
355                                         }
356                                 }
357                         }
358                 }
359                 else
360                 {
361                         __updatedFilePath.Clear();
362                         __fileName.Clear();
363
364                         if (__pEditFieldFolderName != null)
365                         {
366                                 __fileName = __pEditFieldFolderName->GetText();
367                                 __fileName.Trim();
368                         }
369                         String OriginalName = __pDirectory->GetFileName();
370
371                         if (OriginalName.CompareTo(__fileName) == 0)
372                         {
373                                 TransitionScene();
374                                 return;
375                         }
376
377                         String newRenameValue;
378                         ResultValues resultValidity = RESULT_VALUES_SUCCESS;
379
380                         String delim(L"/");
381                         String token;
382                         int tokenCount;
383                         StringTokenizer tokenizer(__filePath, delim);
384                         int fwdSlashCount = 1;
385
386                         tokenCount = tokenizer.GetTokenCount();
387
388                         while (tokenizer.HasMoreTokens() && fwdSlashCount < tokenCount)
389                         {
390                                 fwdSlashCount++;
391                                 tokenizer.GetNextToken(token);
392                                 __updatedFilePath.Append("/");
393                                 __updatedFilePath.Append(token);
394                         }
395
396                         if (__pEditFieldFolderName != null)
397                         {
398                                 __updatedFilePath.Append("/");
399                                 //updatedFilePath.Append(__pEditFieldFolderName->GetText());
400                                 __updatedFilePath.Append(__fileName);
401
402                                 if (entryIsDirectory == 0)
403                                 {
404                                         __updatedFilePath.Append(".");
405                                         __updatedFilePath.Append(__fileExtension);
406                                 }
407
408                                 if (__fileName.CompareTo(".") != 0 && __fileName.CompareTo("..") != 0 && __fileName.CompareTo("/") != 0)
409                                 {
410                                         if (!__fileName.Equals(__pDirectory->GetFileName()) && (File::IsFileExist(__updatedFilePath)))
411                                         {
412                                                 resultValidity = RESULT_VALUES_FILE_EXISTS;
413                                         }
414                                 }
415
416
417                                 if (__fileName == "")
418                                 {
419                                         resultValidity = RESULT_VALUES_NULL;
420                                 }
421
422
423                                 if (resultValidity == RESULT_VALUES_SUCCESS)
424                                 {
425                                         String filePath;
426                                         filePath.Append(__filePath);
427
428                                         ((MyFilesApp*) Application::GetInstance())->SetNotifyListener(this);
429                                         __pFileManager->SetFileManagingListener(this);
430                                         r = __pFileManager->RenameFolderEntry(filePath, __updatedFilePath);
431                                 }
432                                 else
433                                 {
434                                         String messageString;
435                                         int modalResult = 0;
436
437                                         if (resultValidity == RESULT_VALUES_FILE_EXISTS)
438                                         {
439                                                 AppResource::GetInstance()->GetString(L"IDS_MF_POP_SAME_NAME_ALREADY_IN_USE", messageString);
440                                         }
441                                         else if (resultValidity == RESULT_VALUES_NULL)
442                                         {
443                                                 AppResource::GetInstance()->GetString(L"IDS_NAME_CANT_EMPTY", messageString);
444                                         }
445
446                                         messageBox.Construct(L"", messageString, MSGBOX_STYLE_YESNO, 0);
447
448                                         // Call ShowAndWait - draw, show itself and process events
449
450                                         messageBox.ShowAndWait(modalResult);
451
452                                         switch (modalResult)
453                                         {
454                                         case MSGBOX_RESULT_YES:
455                                         {
456                                                 __pEditFieldFolderName->ShowKeypad();
457                                         }
458                                         break;
459
460                                         case MSGBOX_RESULT_NO:
461                                         {
462                                                 TransitionScene();
463                                         }
464                                         break;
465
466                                         default:
467                                         {
468                                                 //Empty Implementation.
469                                         }
470                                         break;
471                                         }
472                                 }
473                         }
474                 }
475         }
476
477         break;
478
479         case IDA_BUTTON_CANCEL:
480         {
481                 //Closes the view and navigates to the previous form.
482                 TransitionScene();
483         }
484         break;
485
486         case IDA_KEYPAD_BUTTON_DONE:
487         {
488                 //Hides the Keypad
489                 if (__pEditFieldFolderName != null)
490                 {
491                         __pEditFieldFolderName->HideKeypad();
492                 }
493         }
494         break;
495
496         case IDA_KEYPAD_BUTTON_CANCEL:
497         {
498                 //Hides the keypad
499                 if (__pEditFieldFolderName != null)
500                 {
501                         __pEditFieldFolderName->HideKeypad();
502                 }
503         }
504         break;
505
506         default:
507         {
508                 //Empty Implementation.
509         }
510         break;
511         }
512 }
513
514 void
515 CreateFolderForm::OnSceneActivatedN(const Tizen::Ui::Scenes::SceneId& previousSceneId,
516                 const Tizen::Ui::Scenes::SceneId& currentSceneId, Tizen::Base::Collection::IList* pArgs)
517 {
518         AppLogDebug("Entry");
519         Integer* pSourceForm = null;
520         String renameHeaderText;
521         String createFolderText;
522         String newName;
523         String footerText;
524         int byteCount = 0;
525         Utf8Encoding utf8;
526 result r = E_SUCCESS;
527         Label* pFolderName = null;
528         Panel* pFolderPanel = null;
529         AppResource* pAppResource = null;
530
531         if (__pEditFieldFolderName != null)
532         {
533                 __pEditFieldFolderName->ShowKeypad();
534         }
535
536         DeviceManager::AddDeviceEventListener(DEVICE_TYPE_STORAGE_CARD, *this);
537 #if 0
538         DeviceManager::AddDeviceEventListener(DEVICE_TYPE_USB_CLIENT, *this);
539 #endif
540
541         SceneManager::GetInstance()->AddSceneManagerEventListener(*this);
542
543         __rootPath = FolderNavigationPresentationModel::GetCurrentPath();
544
545         if (pArgs != null)
546         {
547                 pSourceForm = static_cast< Integer* >(pArgs->GetAt(0));
548                 __launchSource = *pSourceForm;
549
550                 pAppResource = Application::GetInstance()->GetAppResource();
551
552                 if (__launchSource.ToInt() == SELECT_RENAME)
553                 {
554                         pFolderName = static_cast< Label* >(GetControl("IDC_LABEL_FOLDER_NAME", true));
555                         pFolderPanel = static_cast< Panel* >(GetControl("IDC_PANEL_EDIT_FIELD", true));
556
557                         if (__pEditFieldFolderName != null)
558                         {
559                                 if (pFolderName != null)
560                                 {
561                                         pFolderName->SetShowState(false);
562                                 }
563
564                                 if (pFolderPanel != null)
565                                 {
566                                         pFolderPanel->SetBounds(Rectangle(X_DEFAULT, Y_DEFAULT, GetClientAreaBounds().width, H_RENAME_PANEL));
567                                         __pEditFieldFolderName->SetBounds(Rectangle(X_DEFAULT, Y_DEFAULT, GetClientAreaBounds().width, H_RENAME_EDITFIELD));
568                                 }
569                         }
570
571                         if (pAppResource != null)
572                         {
573                                 pAppResource->GetString(L"IDS_MF_BODY_RENAME_ABB", renameHeaderText);
574                                 pAppResource->GetString(L"IDS_COM_BODY_DONE", footerText);
575                         }
576
577                         __pDirectory = static_cast< DirectoryEntry* >(pArgs->GetAt(1));
578                         if (__pDirectory != null)
579                         {
580                                 __filePath.Append(__pDirectory->GetFullFilePath());
581                                 if (__pDirectory->IsDirectory())
582                                 {
583                                         if (__filePath == __rootPath)
584                                                 __isCurrentDirectioryToBeRenamed = true;
585
586                                         if (__pEditFieldFolderName != null)
587                                         {
588                                                 __pEditFieldFolderName->SetText(__pDirectory->GetFileName());
589                                                 r = utf8.GetByteCount(__pDirectory->GetFileName(), byteCount);
590                                                 AppLogDebug("bytecount is %d",byteCount);
591                                                 __textLength = byteCount;
592                                         }
593                                         entryIsDirectory = 1;
594                                 }
595                                 else
596                                 {
597                                         __fileExtension = File::GetFileExtension(__filePath);
598                                         if (__pEditFieldFolderName != null)
599                                         {
600                                                 __pEditFieldFolderName->SetText(__pDirectory->GetFileName());
601                                                 r = utf8.GetByteCount(__pDirectory->GetFileName(), byteCount);
602                                                 AppLogDebug("bytecount is %d",byteCount);
603                                                 __textLength = byteCount;
604                                         }
605                                         entryIsDirectory = 0;
606                                 }
607                         }
608                         __pHeader = GetHeader();
609                         __pHeader->SetTitleText(renameHeaderText);
610                         __pHeader->Invalidate(true);
611
612                         __pFooter = GetFooter();
613                         if (__pFooter != null)
614                         {
615                                 __pFooterItemSave->SetText(footerText);
616                                 __pFooter->Invalidate(true);
617                         }
618                 }
619                 else
620                 {
621
622                         pFolderName = static_cast< Label* >(GetControl("IDC_LABEL_FOLDER_NAME", true));
623                         pFolderPanel = static_cast< Panel* >(GetControl("IDC_PANEL_EDIT_FIELD", true));
624
625                         if (__pEditFieldFolderName != null)
626                         {
627                                 if (pFolderName != null)
628                                 {
629                                         pFolderName->SetShowState(true);
630                                 }
631
632                                 if (pFolderPanel != null)
633                                 {
634                                         pFolderPanel->SetBounds(Rectangle(X_DEFAULT, Y_DEFAULT, GetClientAreaBounds().width, H_RENAME_PANEL));
635                                         __pEditFieldFolderName->SetBounds(Rectangle(X_DEFAULT, Y_CREATE_FOLDER_EDITFIELD, GetClientAreaBounds().width, H_CREATE_FOLDER_EDITFIELD));
636                                 }
637                         }
638
639                         if (pAppResource != null)
640                         {
641                                 pAppResource->GetString(L"IDS_COM_BODY_CREATE_FOLDER", createFolderText);
642                                 pAppResource->GetString(L"IDS_COM_OPT_SAVE", footerText);
643
644                         }
645                         __pHeader = GetHeader();
646                         __pHeader->SetTitleText(createFolderText);
647                         __pHeader->Invalidate(true);
648
649                         __pFooter = GetFooter();
650                         if (__pFooter != null)
651                         {
652                                 __pFooterItemSave->SetText(footerText);
653                                 __pFooter->Invalidate(true);
654                         }
655
656                         if (__pEditFieldFolderName != null)
657                         {
658                                 GenerateFolderName(newName);
659                                 __pEditFieldFolderName->SetText(newName);
660                                 r = utf8.GetByteCount(newName, byteCount);
661                                 //__textLength = newName.GetLength();
662                                 __textLength = byteCount;
663                         }
664                 }
665
666
667                 if (pSourceForm != null)
668                 {
669                         delete pSourceForm;
670                 }
671
672                 pArgs->RemoveAll(false);
673                 delete pArgs;
674                 pArgs = null;
675         }
676
677         AppLogDebug("Exit %s", GetErrorMessage(GetLastResult()));
678 }
679
680 void
681 CreateFolderForm::OnSceneDeactivated(const Tizen::Ui::Scenes::SceneId& currentSceneId,
682                 const Tizen::Ui::Scenes::SceneId& nextSceneId)
683 {
684         DeviceManager::RemoveAllDeviceEventListeners();
685 }
686
687 void
688 CreateFolderForm::OnTextValueChanged(const Tizen::Ui::Control& source)
689 {
690         String currentInput;
691         //wchar_t characterToCompare;
692         String currentText;
693         String specialCharacters(L"/");
694         String byteCountForInput;
695         //int specialCharCount = 0;
696         int modalResult = 0;
697         int byteCount = 0;
698         int inputLength;
699
700         int textToCheck = 0;
701
702         MessageBox messageBox;
703         Utf8Encoding utf8;
704
705
706         FooterItemStatus currentStatus = FOOTER_ITEM_STATUS_NORMAL;
707
708         result r = E_SUCCESS;
709
710         if (__pEditFieldFolderName != null)
711         {
712                 byteCountForInput = __pEditFieldFolderName->GetText();
713                 r = utf8.GetByteCount(byteCountForInput, byteCount);
714                 AppLogDebug("result is %s",GetErrorMessage(r));
715                 AppLogDebug("Bye count is %d",byteCount);
716         }
717
718         if (__pEditFieldFolderName != null)
719         {
720                 currentText = __pEditFieldFolderName->GetText();
721                 textToCheck = currentText.GetLength() - __textLength;
722                 if (currentText.IsEmpty())
723                 {
724                         if (__pFooter != null)
725                         {
726                                 __pFooter->SetItemEnabled(0, false);
727                                 __pFooter->Invalidate(true);
728                         }
729                 }
730                 else
731                 {
732                         if (__pFooter != null)
733                         {
734                                 __pFooter->GetItemStatus(0, currentStatus);
735
736                                 if (currentStatus == FOOTER_ITEM_STATUS_DISABLED)
737                                 {
738                                         __pFooter->SetItemEnabled(0,true);
739                                         __pFooter->Invalidate(true);
740                                 }
741
742                         }
743                 }
744
745                 inputLength = byteCount;
746                 if (__fileExtension.GetLength() >0)
747                 {
748                         inputLength = inputLength + __fileExtension.GetLength() +1;
749                 }
750
751                 {
752                         if (currentText.StartsWith(".", 0))
753                         {
754                                 String invalidCharacterString;
755                                 String validString;
756                                 AppResource::GetInstance()->GetString(L"IDS_COM_POP_INVALID_CHARACTERS", invalidCharacterString);
757                                 messageBox.Construct(L"", invalidCharacterString, MSGBOX_STYLE_NONE, ID_MESSAGE_BOX_TIMEOUT_ONE_SECOND);
758                                 messageBox.ShowAndWait(modalResult);
759
760                                 validString = __pEditFieldFolderName->GetText();
761
762                                 if (validString.GetLength() > 1)
763                                 {
764                                         validString.Remove(0,1);
765                                         __pEditFieldFolderName->SetText(validString);
766                                 }
767                                 else
768                                 {
769                                         __pEditFieldFolderName->SetText("");
770                                         if (__pFooter != null)
771                                         {
772                                                 __pFooter->SetItemEnabled(0, false);
773                                                 __pFooter->Invalidate(true);
774                                         }
775                                 }
776                                 __pEditFieldFolderName->ShowKeypad();
777                         }
778                 }
779
780                 int tempLength = 0;
781
782                 if (__fileExtension.GetLength() == 0 )//folder case
783                 {
784                         tempLength = ID_FOLDER_NAME_LENGTH_MAX;
785                 }
786                 else//file case
787                 {
788                         tempLength = ID_FILE_NAME_LENGTH_MAX;
789                 }
790
791                 textToCheck =  __pEditFieldFolderName->GetText().GetLength() - __textLength;
792                 r = currentText.SubString(__pEditFieldFolderName->GetCursorPosition() - textToCheck,currentInput);
793                 for(int i = 0 ; i < currentInput.GetLength() ; i++)
794                 {
795                         if(specialCharacters.Contains(currentInput[i]))
796                         {
797                                 String invalidCharacterString;
798                                 AppResource::GetInstance()->GetString(L"IDS_COM_POP_INVALID_CHARACTERS", invalidCharacterString);
799                                 messageBox.Construct(L"", invalidCharacterString, MSGBOX_STYLE_NONE, ID_MESSAGE_BOX_TIMEOUT_ONE_SECOND);
800                                 messageBox.ShowAndWait(modalResult);
801                                 currentText.Remove(__pEditFieldFolderName->GetCursorPosition()-textToCheck,textToCheck);
802                                 __pEditFieldFolderName->SetText(currentText);
803                                 __pEditFieldFolderName->SetFocus();
804                                 break;
805                         }
806                 }
807
808                 inputLength = byteCount;
809                 AppLogDebug("input length and temp legth is %d %d",inputLength,tempLength);
810                 if (inputLength  >= tempLength)
811                 {
812                         String maxCharacterString;
813                         String currentInputCharacter;
814                         int checkByteCount = 0;
815                         int lengthOfFinalString = 0;
816                         int currentCharacter = 0;
817                         AppResource::GetInstance()->GetString(L"IDS_COM_POP_MAXIMUM_NUMBER_OF_CHARACTERS_REACHED", maxCharacterString);
818                         messageBox.Construct(L"", maxCharacterString, MSGBOX_STYLE_NONE, ID_MESSAGE_BOX_TIMEOUT_ONE_SECOND);
819                         messageBox.ShowAndWait(modalResult);
820                         currentText = __pEditFieldFolderName->GetText();
821
822                         for (currentCharacter=0;checkByteCount < tempLength;currentCharacter++)
823                         {
824                                 currentInputCharacter.Clear();
825                                 currentInputCharacter = currentText[currentCharacter];
826                                 r = utf8.GetByteCount(currentInputCharacter, byteCount);
827                                 if (r != E_SUCCESS)
828                                 {
829                                         break;
830                                 }
831                                 AppLogDebug("result of byte count is %s",GetErrorMessage(r));
832                                 checkByteCount = checkByteCount + byteCount;
833                                 lengthOfFinalString++;
834                         }
835
836                         r = __pEditFieldFolderName->GetText().SubString(0,lengthOfFinalString,currentInput);
837                         AppLogDebug("current input is %S",currentInput.GetPointer());
838                         r = __pEditFieldFolderName->SetText(currentInput);
839                         __pEditFieldFolderName->ShowKeypad();
840                         return;
841                 }
842
843                 r = utf8.GetByteCount(__pEditFieldFolderName->GetText(), byteCount);
844                 __textLength = byteCount;
845         }
846         return;
847 }
848 void
849 CreateFolderForm::OnTextValueChangeCanceled(const Tizen::Ui::Control& source)
850 {
851         if (__pFooter != null)
852         {
853                 __pFooter->SetItemEnabled(0, false);
854                 __pFooter->Invalidate(true);
855         }
856 }
857
858 void
859 CreateFolderForm::OnOrientationChanged(const Tizen::Ui::Control& source, Tizen::Ui::OrientationStatus orientationStatus)
860 {
861         Rectangle editFieldBounds(GetClientAreaBounds().x, 0, GetClientAreaBounds().width, H_RENAME_EDITFIELD);
862
863         switch (orientationStatus)
864         {
865         case ORIENTATION_STATUS_PORTRAIT:
866         {
867                 if (__pEditFieldFolderName != null)
868                 {
869                         __pEditFieldFolderName->SetBounds(editFieldBounds);
870                 }
871         }
872         break;
873
874         case ORIENTATION_STATUS_LANDSCAPE:
875         {
876                 if (__pEditFieldFolderName != null)
877                 {
878                         __pEditFieldFolderName->SetBounds(editFieldBounds);
879                 }
880         }
881         break;
882
883         default:
884         {
885                 //Empty Implementation.
886         }
887         break;
888         }
889
890         Invalidate(true);
891 }
892
893 void
894 CreateFolderForm::OnKeypadWillOpen(Tizen::Ui::Control& source)
895 {
896         //Empty Implementation.
897         SetActionBarsVisible(FORM_ACTION_BAR_FOOTER, true);
898
899 }
900
901 void
902 CreateFolderForm::OnKeypadOpened(Tizen::Ui::Control& source)
903 {
904         SetActionBarsVisible(FORM_ACTION_BAR_FOOTER, true);
905 }
906 void
907 CreateFolderForm::OnKeypadClosed(Tizen::Ui::Control& source)
908 {
909         SetActionBarsVisible(FORM_ACTION_BAR_FOOTER, true);
910 }
911 void
912 CreateFolderForm::OnKeypadActionPerformed(Tizen::Ui::Control& source, Tizen::Ui::KeypadAction keypadAction)
913 {
914         //Empty Implementation.
915 }
916
917 void
918 CreateFolderForm::OnTouchCanceled(const Tizen::Ui::Control& source, const Tizen::Graphics::Point& currentPosition, const Tizen::Ui::TouchEventInfo& touchInfo)
919 {
920         //Empty Implementation
921 }
922
923 void
924 CreateFolderForm::OnTouchFocusIn(const Tizen::Ui::Control& source, const Tizen::Graphics::Point& currentPosition, const Tizen::Ui::TouchEventInfo& touchInfo)
925 {
926         //Empty Implementation
927 }
928
929 void
930 CreateFolderForm::OnTouchFocusOut(const Tizen::Ui::Control& source, const Tizen::Graphics::Point& currentPosition, const Tizen::Ui::TouchEventInfo& touchInfo)
931 {
932         //Empty Implementation
933 }
934
935 void
936 CreateFolderForm::OnTouchMoved(const Tizen::Ui::Control& source, const Tizen::Graphics::Point& currentPosition, const Tizen::Ui::TouchEventInfo& touchInfo)
937 {
938         //Empty Implementation
939 }
940
941 void
942 CreateFolderForm::OnTouchPressed(const Tizen::Ui::Control& source, const Tizen::Graphics::Point& currentPosition, const Tizen::Ui::TouchEventInfo& touchInfo)
943 {
944         //Empty Implementation
945 }
946
947 void
948 CreateFolderForm::OnTouchReleased(const Tizen::Ui::Control& source, const Tizen::Graphics::Point& currentPosition, const Tizen::Ui::TouchEventInfo& touchInfo)
949 {
950         if (!(source.Equals(*__pEditFieldFolderName)))
951         {
952                 __pEditFieldFolderName->HideKeypad();
953         }
954 }
955
956 void
957 CreateFolderForm::OnDeviceChange(ComponentType deviceType, const Tizen::Base::String& state)
958 {
959         bool setMediaPath = false;
960         bool searchHistoryStatus = false;
961         MemoryType currentMemoryType = MEMORY_TYPE_PHONE;
962         String unmounted = sdCardStatusUnmounted;
963         String removed = usbDeviceStatusRemoved;
964         String currentPath;
965         String searchScene;
966         SceneManager* pSceneManager = null;
967         IListT< SceneId >* pSceneList = null;
968         int itemCount = 0;
969
970         pSceneManager = SceneManager::GetInstance();
971
972         if (state == unmounted || state == removed)
973         {
974                 currentPath = FolderNavigationPresentationModel::GetCurrentPath();
975                 if (state == unmounted)
976                 {
977                         if (currentPath.Contains(BasePresentationModel::GetSdCardPath()))
978                         {
979                                 FolderNavigationPresentationModel::SetCurrentFilePath(BasePresentationModel::GetMediaPath());
980                                 FolderNavigationPresentationModel::SetStorageType(currentMemoryType);
981                                 setMediaPath = true;
982                         }
983                 }
984
985                 if (state == removed)
986                 {
987                         if (currentPath.Contains(BasePresentationModel::GetUsbPath()))
988                         {
989                                 FolderNavigationPresentationModel::SetCurrentFilePath(BasePresentationModel::GetMediaPath());
990                                 FolderNavigationPresentationModel::SetStorageType(currentMemoryType);
991                                 setMediaPath = true;
992                         }
993                 }
994
995                 if (setMediaPath)
996                 {
997                         if (pSceneManager != null)
998                         {
999                                 pSceneList = pSceneManager->GetSceneHistoryN();
1000                         }
1001
1002                         if (pSceneList != null)
1003                         {
1004                                 itemCount = pSceneList->GetCount() - 1;
1005                                 pSceneList->GetAt(itemCount, searchScene);
1006                                 while (searchScene != IDSCN_SEARCH_FORM && itemCount >= 0)
1007                                 {
1008                                         itemCount--;
1009                                         pSceneList->GetAt(itemCount, searchScene);
1010                                 }
1011                         }
1012
1013                         if (searchScene == IDSCN_SEARCH_FORM)
1014                         {
1015                                 AppLogDebug("Search scene present");
1016                                 searchHistoryStatus = true;
1017                         }
1018                         else
1019                         {
1020                                 AppLogDebug("Search scene not present");
1021                                 searchHistoryStatus = false;
1022                         }
1023
1024                         if(pSceneList)
1025                         {
1026                                 pSceneList->RemoveAll();
1027                                 delete pSceneList;
1028                                 pSceneList = null;
1029                         }
1030
1031                         if (searchHistoryStatus == true)
1032                         {
1033                                 if (pSceneManager != null)
1034                                 {
1035                                         pSceneManager->DestroyScene(IDSCN_SEARCH_FORM);
1036                                         pSceneManager->ClearSceneHistory();
1037                                 }
1038                         }
1039
1040                         if (pSceneManager != null)
1041                         {
1042                                 pSceneManager->GoForward(ForwardSceneTransition(IDSCN_TOP_LEVEL_DIRECTORY_FORM, SCENE_TRANSITION_ANIMATION_TYPE_NONE, SCENE_HISTORY_OPTION_NO_HISTORY, SCENE_DESTROY_OPTION_DESTROY), null);                    }
1043                 }
1044         }
1045 }
1046
1047 void
1048 CreateFolderForm::TransitionScene(void)
1049 {
1050         SceneManager* pSceneManager = SceneManager::GetInstance();
1051         if (__previousFormPath.CompareTo("") != 0)
1052         {
1053                 FolderNavigationPresentationModel::SetCurrentFilePath(__previousFormPath);
1054                 FolderNavigationPresentationModel::SetStorageType(__previousFormTab);
1055         }
1056         pSceneManager->GoBackward(BackwardSceneTransition(__previousSceneId, SCENE_TRANSITION_ANIMATION_TYPE_RIGHT), null);
1057 }
1058
1059 void
1060 CreateFolderForm::OnDeviceStateChanged(Tizen::System::DeviceType deviceType, const Tizen::Base::String& state)
1061 {
1062
1063         ComponentType currentDeviceType;
1064         switch (deviceType)
1065         {
1066         case StorageCard:
1067         {
1068                 currentDeviceType = COMPONENT_TYPE_STORAGE_CARD;
1069         }
1070         break;
1071
1072         case UsbClient:
1073         {
1074                 currentDeviceType = COMPONENT_TYPE_USB;
1075         }
1076         break;
1077
1078         default:
1079         {
1080                 //Do Nothing.
1081         }
1082         break;
1083         }
1084         OnDeviceChange(currentDeviceType, state);
1085 }
1086
1087 void
1088 CreateFolderForm::GenerateFolderName(String& newName)
1089 {
1090         String newFolderName;
1091         AppResource::GetInstance()->GetString(L"IDS_COM_POP_FOLDER", newFolderName);
1092         AppLogDebug("Entry");
1093         String entryValue;
1094         int currentNameCount = 0;
1095         bool checkFileAvailability = false;
1096         String directoryName = FolderNavigationPresentationModel::GetCurrentPath();
1097
1098         directoryName.Append(newFolderName);
1099
1100         checkFileAvailability = File::IsFileExist(directoryName);
1101
1102         while (checkFileAvailability)
1103         {
1104                 currentNameCount++;
1105                 newFolderName.Clear();
1106                 AppResource::GetInstance()->GetString(L"IDS_COM_POP_FOLDER", newFolderName);
1107                 newFolderName.Append("_");
1108                 newFolderName.Append(currentNameCount);
1109                 directoryName.Clear();
1110                 directoryName = FolderNavigationPresentationModel::GetCurrentPath();
1111                 directoryName.Append(newFolderName);
1112                 checkFileAvailability = File::IsFileExist(directoryName);
1113         }
1114
1115         newName = newFolderName;
1116 }
1117
1118
1119 void
1120 CreateFolderForm::OnSceneTransitionCompleted(const Tizen::Ui::Scenes::SceneId &previousSceneId,
1121                 const Tizen::Ui::Scenes::SceneId &currentSceneId)
1122 {
1123         SceneManager::GetInstance()->RemoveSceneManagerEventListener(*this);
1124 }
1125
1126 void
1127 CreateFolderForm::OnSceneTransitionStarted(const Tizen::Ui::Scenes::SceneId &currentSceneId,
1128                 const Tizen::Ui::Scenes::SceneId &nextSceneId)
1129 {
1130         //Empty Implementation.
1131 }
1132
1133 result
1134 CreateFolderForm::CreateFileProgressingPopup(void)
1135 {
1136         AppLogDebug("Entry");
1137         result r = E_SUCCESS;
1138         _pFileProgressingPopup = new (std::nothrow) Popup();
1139         _pFileProgressingPopup->Construct(L"IDL_FILE_PROGRESSING_POPUP");
1140         if (_pFileProgressingPopup != null)
1141         {
1142                 _pFileProgressingPopup->SetTitleText(L"FileProcessing");
1143
1144                 _pFileProgressingCancelButton = static_cast< Button* >(_pFileProgressingPopup->GetControl(L"IDC_CANCEL_BUTTON", true));
1145                 if (_pFileProgressingCancelButton != null)
1146                 {
1147                         _pFileProgressingCancelButton->SetActionId(IDA_BTN_ANIMATION_POPUP_CANCEL);
1148                         _pFileProgressingCancelButton->AddActionEventListener(*this);
1149                         _pFileProgressingCancelButton->SetShowState(false);
1150                 }
1151
1152                 _pFileProgressingProgress = static_cast< Progress* >(_pFileProgressingPopup->GetControl(L"IDC_PROGRESS_BAR", true));
1153                 if (_pFileProgressingProgress != null)
1154                 {
1155                         _pFileProgressingProgress->SetName(L"IDC_ANIMATION_PROGRESS");
1156                         _pFileProgressingProgress->SetValue(0);
1157                         _pFileProgressingProgress->SetShowState(false);
1158                 }
1159
1160                 _pFileProgressingHeaderLabel = static_cast< Label* >(_pFileProgressingPopup->GetControl(L"IDC_ANIMATION_HEADER", true));
1161                 if (_pFileProgressingHeaderLabel != null)
1162                 {
1163                         _pFileProgressingHeaderLabel->SetName(L"IDC_ANIMATION_HEADER");
1164                         _pFileProgressingHeaderLabel->SetShowState(true);
1165                 }
1166
1167                 _pFileProgressingLabel = static_cast< Label* >(_pFileProgressingPopup->GetControl(L"IDC_ANIMATION_LABEL", true));
1168                 if (_pFileProgressingLabel != null)
1169                 {
1170                         _pFileProgressingLabel->SetName(L"IDC_FILE_COUNT_LABEL");
1171                         _pFileProgressingLabel->SetShowState(true);
1172                 }
1173         }
1174         AppLogDebug("EXIT: r = %s", GetErrorMessage(r));
1175         return r;
1176 }
1177
1178 void
1179 CreateFolderForm::HideFileProgressingPopup(void)
1180 {
1181         if (_pFileProgressingPopup != null && _pFileProgressingPopup->IsVisible())
1182         {
1183                 _pFileProgressingPopup->SetShowState(false);
1184
1185                 delete _pFileProgressingPopup;
1186                 _pFileProgressingPopup = null;
1187         }
1188 }
1189
1190 void
1191 CreateFolderForm::ShowFileProgressingPopup(void)
1192 {
1193         String renamingLabelText;
1194         String copyingLabelText;
1195
1196         AppResource* pAppResource = null;
1197         pAppResource = Application::GetInstance()->GetAppResource();
1198
1199         pAppResource->GetString("IDS_MF_POP_RENAMING", renamingLabelText);
1200
1201         renamingLabelText.Append(L"...");
1202         CreateFileProgressingPopup();
1203
1204         if (_pFileProgressingPopup != null)
1205         {
1206                 if (_pFileProgressingHeaderLabel != null)
1207                 {
1208                         _pFileProgressingHeaderLabel->SetBounds(X_DEFAULT,Y_DEFAULT,_pFileProgressingPopup->GetBounds().width - 40, _pFileProgressingPopup->GetBounds().height);
1209                         _pFileProgressingHeaderLabel->SetTextConfig(40, LABEL_TEXT_STYLE_BOLD);
1210                         _pFileProgressingHeaderLabel->SetTextHorizontalAlignment(ALIGNMENT_CENTER);
1211                         _pFileProgressingHeaderLabel->SetTextVerticalAlignment(ALIGNMENT_MIDDLE);
1212                         _pFileProgressingHeaderLabel->SetText(renamingLabelText);
1213                         _pFileProgressingHeaderLabel->Invalidate(true);
1214                 }
1215
1216                 _pFileProgressingPopup->SetShowState(true);
1217                 _pFileProgressingPopup->Show();
1218         }
1219 }
1220
1221 void
1222 CreateFolderForm::OnFileManagingStart(void)
1223 {
1224         ShowFileProgressingPopup();
1225 }
1226
1227 void
1228 CreateFolderForm::OnFileManagingStop(FileManagingResult fileProcessingResult)
1229 {
1230
1231 }
1232
1233 void
1234 CreateFolderForm::OnNotificationReceived(Tizen::Base::Collection::ArrayList* pArgs)
1235 {
1236         AppLogDebug("ENTER");
1237
1238         int fileProcessingResult = 0;
1239         SceneManager* pSceneManager = null;
1240         HashMap* pCacheList = null;
1241         String oldCachePath;
1242         bool cacheKeyStatus = false;
1243         pSceneManager = SceneManager::GetInstance();
1244         Integer* pfileProcessingResult = null;
1245         TryCatch(pArgs != null, , "Argument list received is invalid");
1246
1247         pfileProcessingResult = (Integer*) pArgs->GetAt(0);
1248         if (pfileProcessingResult != null)
1249         {
1250                 fileProcessingResult = pfileProcessingResult->ToInt();
1251                 delete pfileProcessingResult;
1252         }
1253         switch (fileProcessingResult)
1254         {
1255         case FILE_MANAGING_RENAME_SUCCESS:
1256         {
1257                 AppLogDebug("FILE_MANAGING_RENAME_SUCCESS");
1258                 pCacheList = CacheManager::GetInstance()->GetCacheHashMap();
1259                 HideFileProgressingPopup();
1260
1261                 pSceneManager = SceneManager::GetInstance();
1262
1263                 if (pCacheList != null)
1264                 {
1265                         oldCachePath = __pDirectory->GetFullFilePath();
1266                         cacheKeyStatus = pCacheList->ContainsKey(oldCachePath);
1267
1268                         if (cacheKeyStatus)
1269                         {
1270                                 CacheManager::GetInstance()->RenameCacheEntry(oldCachePath,__updatedFilePath);
1271                         }
1272                 }
1273                 __pEditFieldFolderName->HideKeypad();
1274
1275                 if (pSceneManager != null)
1276                 {
1277                         String fileName = __pEditFieldFolderName->GetText();
1278                         fileName.Trim();
1279                         if(__pDirectory != null)
1280                         {
1281                                 __pDirectory->SetFileName(fileName);
1282                         }
1283
1284                         if (entryIsDirectory == 1)
1285                         {
1286                                 __updatedFilePath.Append(L"/");
1287                         }
1288                         if(__pDirectory != null)
1289                         {
1290                                 __pDirectory->SetFullFilePath(__updatedFilePath);
1291                         }
1292
1293                         if (__isCurrentDirectioryToBeRenamed == true)
1294                                 FolderNavigationPresentationModel::SetCurrentFilePath(__updatedFilePath);
1295
1296                         //pSceneManager->GoBackward(BackwardSceneTransition(), null);
1297                         TransitionScene();
1298                 }
1299         }
1300         break;
1301         case FILE_MANAGING_RENAME_FAILED:
1302         {
1303                 AppLogDebug("FILE_MANAGING_RENAME_FAILED");
1304                 HideFileProgressingPopup();
1305                 AppResource* pAppResource = null;
1306                 MessageBox messageBox;
1307                 String renameFailed;
1308
1309                 pAppResource = UiApp::GetInstance()->GetAppResource();
1310                 TryReturn(pAppResource != null, , "Failed to fetch AppResource");
1311
1312                 pAppResource->GetString(L"IDS_RENAME_FAILED", renameFailed);
1313                 messageBox.Construct(L"",renameFailed, MSGBOX_STYLE_NONE, 1000);
1314                 int modalResult = 0;
1315                 messageBox.ShowAndWait(modalResult);
1316
1317                 __pEditFieldFolderName->HideKeypad();
1318
1319                 if (pSceneManager != null)
1320                 {
1321                         //pSceneManager->GoBackward(BackwardSceneTransition(), null);
1322                         TransitionScene();
1323                 }
1324         }
1325         break;
1326         default:
1327         {
1328
1329         }
1330         break;
1331         }
1332
1333         CATCH:
1334         if (pArgs != null)
1335         {
1336                 pArgs->RemoveAll(false);
1337                 delete pArgs;
1338                 pArgs = null;
1339         }
1340
1341         AppLogDebug("EXIT");
1342 }