Fixed Nabi issue N_SE-40542,N_SE-39949,N_SE-40229
[apps/osp/Internet.git] / src / IntCreateBookmarkFolderForm.cpp
1 //
2
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Flora License, Version 1.1 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://floralicense.org/license/
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an AS IS BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 // !Internet
19 /*@file:    IntCreateBookmarkFolderForm
20  *@brief:       This class creates Bookmark folder
21  *
22  */
23
24 #include <FAppUiApp.h>
25 #include <FUi.h>
26 #include "IntBookmarkPresentationModel.h"
27 #include "IntBookmarkData.h"
28 #include "IntCommonLib.h"
29 #include "IntCreateBookmarkFolderForm.h"
30 #include "IntSceneRegister.h"
31
32 using namespace Tizen::App;
33 using namespace Tizen::Base;
34 using namespace Tizen::Base::Collection;
35 using namespace Tizen::Graphics;
36 using namespace Tizen::Ui;
37 using namespace Tizen::Ui::Controls;
38 using namespace Tizen::Ui::Scenes;
39
40
41 const int CreateBookmarkFolderForm::IDA_BUTTON_DONE = 101;
42 const int CreateBookmarkFolderForm::IDA_BUTTON_CANCEL = 102;
43
44 CreateBookmarkFolderForm::CreateBookmarkFolderForm(void)
45 : __pFolderTitle(null)
46 , __pMsgBox(null)
47 , __modalMsgBoxResult(0)
48 , __pBookmark(null)
49 , __ret(false)
50 {
51
52 }
53
54 CreateBookmarkFolderForm::~CreateBookmarkFolderForm(void)
55 {
56
57 }
58
59 bool
60 CreateBookmarkFolderForm::Initialize(void)
61 {
62         result r = Construct(L"IDL_CREATE_BOOKMARK_FOLDER");
63         AppLogDebug("CreateBookmarkFolderForm Construct returned %s",GetErrorMessage(r));
64         return true;
65 }
66
67 result
68 CreateBookmarkFolderForm::OnInitializing(void)
69 {
70         // Add your initialization code here
71
72         result r = E_SUCCESS;
73         Header* pHeader = null;
74         SceneManager* pSceneManager = SceneManager::GetInstance();
75         AppAssert(pSceneManager);
76         pSceneManager->AddSceneEventListener(IDSCN_CREATE_BOOKMARK_FOLDER, *this);
77
78         __pFolderTitle = static_cast< EditField* >(GetControl(L"IDC_EDITFIELD_FOLDER_TITLE"));
79
80         if (__pFolderTitle == null)
81         {
82                 return E_FAILURE;
83         }
84
85         __pFolderTitle->AddKeypadEventListener(*this);
86         __pFolderTitle->AddTextEventListener(*this);
87         __pFolderTitle->SetOverlayKeypadCommandButtonVisible(false);
88
89         pHeader = GetHeader();
90
91         if (pHeader != NULL)
92         {
93                 pHeader->AddActionEventListener(*this);
94         }
95
96         return r;
97 }
98
99 result
100 CreateBookmarkFolderForm::OnTerminating(void)
101 {
102         result r = E_SUCCESS;
103         return r;
104 }
105
106 void
107 CreateBookmarkFolderForm::OnSceneActivatedN(const SceneId& previousSceneId, const SceneId& currentSceneId, IList* pArgs)
108 {
109         AppLogDebug("CreateBookmarkFolderForm::OnSceneActivatedN");
110
111         result r = E_SUCCESS;
112         __previousScene = previousSceneId;
113         bool flag = false;
114         ArrayList folderList;
115         String parentID = "0";
116         String title ;
117         String folderTitle ;
118         int count = 0;
119         int foldercount = 0;
120         int counter = 0;
121         __ret = false;
122
123         String pStr = CommonUtil::GetString(L"IDS_BR_SK_EDIT");
124
125         String folder01 = CommonUtil::GetString(L"IDS_BR_HEADER_FOLDER");
126         folder01.Append(L"_");
127
128         String* pEvent = null;
129         if (pArgs != NULL)
130         {
131                 pEvent = dynamic_cast<String*>(pArgs->GetAt(1));
132         }
133
134         if (pEvent != null)
135         {
136                 if (pEvent->CompareTo(pStr) == 0)
137                 {
138                         __ret = true;
139                 }
140                 delete pEvent;
141         }
142
143         Header* pHeader = GetHeader();
144
145         if (pHeader != null)
146         {
147                 if (previousSceneId ==IDSCN_EDIT_BOOKMARK_LIST || __ret == true)
148                 {
149                         pHeader->SetTitleText(CommonUtil::GetString(L"IDS_BR_SK_EDIT"));
150                 }
151                 else
152                 {
153                         pHeader->SetTitleText(CommonUtil::GetString(L"IDS_BR_SK3_CREATE_FOLDER"));
154                 }
155         }
156
157         if (__pFolderTitle == NULL)
158         {
159                 return;
160         }
161
162         if (previousSceneId == IDSCN_EDIT_BOOKMARK_LIST || __ret == true)
163         {
164                 if (__pBookmark != NULL )
165                 {
166                         delete __pBookmark;
167                         __pBookmark = null;
168                 }
169
170                 if ( pArgs != NULL )
171                 {
172                         __pBookmark = dynamic_cast<BookmarkData*>(pArgs->GetAt(0));
173                 }
174
175                 if (__pBookmark != null)
176                 {
177                         __pFolderTitle->SetText(__pBookmark->GetBookmarkTitle());
178                         __pFolderTitle->SetFocus();
179                         __pFolderTitle->ShowKeypad();
180                 }
181         }
182         else
183         {
184                 __pFolderTitle->SetText(folder01);
185                 __pFolderTitle->SetFocus();
186                 __pFolderTitle->ShowKeypad();
187
188                 r = BookmarkPresentationModel::GetInstance()->GetFolderCount(parentID, foldercount);   // to get count of existing folders
189                 TryCatch( r == E_SUCCESS,,"BookmarkPresentationModel::GetInstance()->GetFolderCount failed  %s",GetErrorMessage(r));
190
191                 r = BookmarkPresentationModel::GetInstance()->GetFolder(parentID, 0, foldercount, folderList);   // to get list of existing folders
192                 TryCatch( r == E_SUCCESS,,"BookmarkPresentationModel::GetInstance()->GetFolder failed  %s",GetErrorMessage(r));
193
194                 while (true)              // to provide user with unique folder title when he creates a new(std::nothrow) folder
195                 {                         // to generate folder with default name of type Folder_##
196                         flag = false ;
197                         title = L"";
198                         count = 0;
199                         counter++;
200                         title.Append(folder01);
201
202                         if (counter/10 == 0)
203                         {
204                                 title.Append(0);
205                         }
206
207                         title.Append(counter);
208
209                         for (count = 0;count < foldercount ; count++)
210                         {
211                                 BookmarkData* pBookmark = dynamic_cast<BookmarkData*>(folderList.GetAt(count));
212                                 folderTitle = L"";
213                                 if ( pBookmark != NULL)
214                                 {
215                                         folderTitle = pBookmark->GetBookmarkTitle();
216                                 }
217
218                                 if (title.Equals(folderTitle,false) == true)
219                                 {
220                                         flag = true;    // if flag is true folder title already exists
221                                         break;          // break from for loop
222                                 }
223
224                         }    // end of for loop
225
226                         if ( flag == false)
227                         {
228                                 __pFolderTitle->SetText(title);      // if flag is false folder title is unique
229                                 break;                               // break from while loop
230                         }
231
232                 }  // end of while loop
233         }
234
235         CATCH:
236
237         folderList.RemoveAll(true);
238         return;
239 }
240
241 void
242 CreateBookmarkFolderForm::OnSceneDeactivated(const SceneId& currentSceneId, const SceneId& nextSceneId)
243 {
244         if (__pBookmark)
245         {
246                 delete __pBookmark;
247                 __pBookmark= null;
248         }
249 }
250
251 void
252 CreateBookmarkFolderForm::OnActionPerformed(const Control& source, int actionId)
253 {
254         int count = 0;
255         SceneManager* pSceneManager = SceneManager::GetInstance();
256
257         if (pSceneManager == null)
258         {
259                 return;
260         }
261
262         switch (actionId)
263         {
264         case IDA_BUTTON_CANCEL:
265         {
266                 result r;
267                 if (__pFolderTitle != NULL)
268                 {
269                         __pFolderTitle->Clear();
270                         __pFolderTitle->HideKeypad();
271                 }
272
273                 r = pSceneManager->GoBackward(BackwardSceneTransition());
274                 AppLogDebug("SCENE_ADD_BOOKMARK error = %s",GetErrorMessage(r));
275                 TryCatch(!IsFailed(r),L"BookmarkPresentationModel::GetFolderCount failed %s",GetErrorMessage(r));
276
277         }
278         break;
279
280         case IDA_BUTTON_DONE:
281         {
282                 result r = E_SUCCESS;
283                 bool flag = false;
284                 ArrayList folderList;
285                 String parentID = "0";
286                 int foldercount = 0;
287                 String title;
288                 String folderTitle;
289                 folderTitle = __pFolderTitle->GetText();
290                 folderTitle.Trim();
291
292                 r = BookmarkPresentationModel::GetInstance()->GetFolderCount(parentID, foldercount);
293                 TryCatch( !IsFailed(r),,"BookmarkPresentationModel::GetInstance()->GetFolderCount failed %s",GetErrorMessage(r));
294
295                 r = BookmarkPresentationModel::GetInstance()->GetFolder(parentID, 0, foldercount, folderList);
296                 TryCatch( !IsFailed(r),,"BookmarkPresentationModel::GetInstance()->GetFolder failed %s",GetErrorMessage(r));
297
298                 // To check if folder with same name exists
299                 for (count = 0; count < foldercount; count++)
300                 {
301                         BookmarkData* pBookmark = dynamic_cast< BookmarkData* >(folderList.GetAt(count));
302                         if ( pBookmark == NULL )
303                         {
304                                 return;
305                         }
306                         title = pBookmark->GetBookmarkTitle();
307                         title.Trim();
308
309                         if (folderTitle.Equals(title, true) == true)
310                         {
311                                 flag = true;
312                                 break;
313                         }
314                 }
315
316                 if (flag == true && ( __ret == true || __previousScene == IDSCN_EDIT_BOOKMARK_LIST) )
317                 {
318
319                         if(__pBookmark && (__pBookmark->GetBookmarkTitle() == folderTitle ))
320                         {
321                                 SceneManager::GetInstance()->GoBackward(BackwardSceneTransition(SCENE_TRANSITION_ANIMATION_TYPE_RIGHT));
322                                 folderList.RemoveAll(false);
323                                 return;
324                         }
325                 }
326
327                 if (flag == true)
328                 {
329                         String message = " Folder already exists ";
330                         CreateMessage(message);                   // Message alert saying Folder already exists
331                 }
332                 else
333                 {
334                         if (__previousScene == IDSCN_EDIT_BOOKMARK_LIST  || __ret == true )
335                         {
336                                 __pBookmark->SetBookmarkTitle(folderTitle);
337                                 BookmarkPresentationModel::GetInstance()->SaveFolder(*__pBookmark);
338
339                                 r = SceneManager::GetInstance()->GoBackward(BackwardSceneTransition(SCENE_TRANSITION_ANIMATION_TYPE_RIGHT));
340                                 AppLogDebug("SCENE_ADD_BOOKMARK error = %s",GetErrorMessage(r));
341
342                         }
343                         else
344                         {
345                                 ArrayList* pArgList = null;
346                                 pArgList = new(std::nothrow) ArrayList();
347                                 if (pArgList != null)
348                                 {
349                                         pArgList->Construct();
350                                         pArgList->Add(*new(std::nothrow) String(L"2"));
351                                 }
352
353                                 BookmarkData bookmark;
354                                 bookmark.SetBookmarkTitle(folderTitle);
355                                 BookmarkPresentationModel::GetInstance()->SaveFolder(bookmark);
356                                 result r = pSceneManager->GoBackward(BackwardSceneTransition(SCENE_TRANSITION_ANIMATION_TYPE_RIGHT),pArgList);
357                                 AppLogDebug("SCENE_ADD_BOOKMARK error = %s",GetErrorMessage(r));
358                                 if (pArgList != null)
359                                 {
360                                         pArgList->RemoveAll(false);
361                                         delete pArgList;
362                                 }
363                                 TryCatch( !IsFailed(r),L"BookmarkPresentationModel::GetFolder failed %s",GetErrorMessage(r));
364                         }
365
366                 }
367
368                 folderList.RemoveAll(false);
369         }
370         break;
371
372         default:
373                 break;
374         }
375
376         CATCH:  return;
377 }
378
379 void
380 CreateBookmarkFolderForm::OnKeypadActionPerformed(Control& source, KeypadAction keypadAction)
381 {
382
383 }
384
385 void
386 CreateBookmarkFolderForm::OnKeypadClosed(Control& source)
387 {
388 //      __pFolderTitle->SetFocus();
389 //      __pFolderTitle->HideKeypad();
390 }
391
392 void
393 CreateBookmarkFolderForm::OnKeypadOpened(Control& source)
394 {
395
396 }
397
398 void
399 CreateBookmarkFolderForm::OnKeypadWillOpen(Control& source)
400 {
401
402 }
403
404 void
405 CreateBookmarkFolderForm::OnTextValueChanged(const Control& source)
406 {
407         String title = L"";
408         Header* pHeader = GetHeader();
409
410         if (pHeader == NULL)
411         {
412                 return;
413         }
414
415         if (__pFolderTitle == NULL)
416         {
417                 return;
418         }
419
420         title = __pFolderTitle->GetText();
421         title.Trim();
422
423         // To disable done button when folder title edit-field is empty
424         if (__pFolderTitle && title.GetLength() == 0)
425         {
426                 pHeader->SetButtonEnabled(BUTTON_POSITION_LEFT, false);
427         }
428         else
429         {
430                 pHeader->SetButtonEnabled(BUTTON_POSITION_LEFT, true);
431         }
432         pHeader->Invalidate(true);
433         return;
434 }
435
436 void
437 CreateBookmarkFolderForm::OnTextValueChangeCanceled(const Control& source)
438 {
439
440 }
441
442 void
443 CreateBookmarkFolderForm::CreateMessage(String& msg)
444 {
445
446         if (__pMsgBox != null)
447         {
448                 delete __pMsgBox;
449                 __pMsgBox = null;
450                 __modalMsgBoxResult = 0;
451         }
452
453         __pMsgBox = new(std::nothrow) MessageBox;
454
455         if (__pMsgBox != null)
456         {
457                 __pMsgBox->Construct(L"", msg, MSGBOX_STYLE_OK, 3000);
458         }
459
460         if (__pMsgBox != null)
461         {
462                 __pMsgBox->ShowAndWait(__modalMsgBoxResult);
463                 if(__pFolderTitle != null)
464                         __pFolderTitle->SetFocus();
465                 /*if (__modalMsgBoxResult == MSGBOX_RESULT_OK)
466                 {
467                         AppLog("CreateBookmarkFolderForm::CreateMessage MSGBOX_RESULT_OK");
468                         if (__pMsgBox != null)
469                         {
470                                 delete __pMsgBox;
471                                 __pMsgBox = null;
472                         }
473                 }*/
474                 if (__pMsgBox != null)
475                 {
476                         delete __pMsgBox;
477                         __pMsgBox = null;
478                 }
479                 __modalMsgBoxResult = 0;
480         }
481
482         return;
483 }