Fixed Nabi Issues
[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         SetFormBackEventListener(this);
78
79         __pFolderTitle = static_cast< EditField* >(GetControl(L"IDC_EDITFIELD_FOLDER_TITLE"));
80
81         if (__pFolderTitle == null)
82         {
83                 return E_FAILURE;
84         }
85
86         __pFolderTitle->AddKeypadEventListener(*this);
87         __pFolderTitle->AddTextEventListener(*this);
88         __pFolderTitle->SetOverlayKeypadCommandButtonVisible(false);
89
90         pHeader = GetHeader();
91
92         if (pHeader != NULL)
93         {
94                 pHeader->AddActionEventListener(*this);
95         }
96
97         return r;
98 }
99
100 result
101 CreateBookmarkFolderForm::OnTerminating(void)
102 {
103         result r = E_SUCCESS;
104         return r;
105 }
106
107 void
108 CreateBookmarkFolderForm::OnSceneActivatedN(const SceneId& previousSceneId, const SceneId& currentSceneId, IList* pArgs)
109 {
110         AppLogDebug("CreateBookmarkFolderForm::OnSceneActivatedN");
111
112         result r = E_SUCCESS;
113         __previousScene = previousSceneId;
114         bool flag = false;
115         ArrayList folderList;
116         String parentID = "0";
117         String title ;
118         String folderTitle ;
119         int count = 0;
120         int foldercount = 0;
121         int counter = 0;
122         __ret = false;
123
124         String pStr = CommonUtil::GetString(L"IDS_BR_SK_EDIT");
125
126         String folder01 = CommonUtil::GetString(L"IDS_BR_HEADER_FOLDER");
127         folder01.Append(L"_");
128
129         String* pEvent = null;
130         if (pArgs != NULL)
131         {
132                 pEvent = dynamic_cast<String*>(pArgs->GetAt(1));
133         }
134
135         if (pEvent != null)
136         {
137                 if (pEvent->CompareTo(pStr) == 0)
138                 {
139                         __ret = true;
140                 }
141                 delete pEvent;
142         }
143
144         Header* pHeader = GetHeader();
145
146         if (pHeader != null)
147         {
148                 if (previousSceneId ==IDSCN_EDIT_BOOKMARK_LIST || __ret == true)
149                 {
150                         pHeader->SetTitleText(CommonUtil::GetString(L"IDS_BR_SK_EDIT"));
151                 }
152                 else
153                 {
154                         pHeader->SetTitleText(CommonUtil::GetString(L"IDS_BR_SK3_CREATE_FOLDER"));
155                 }
156         }
157
158         if (__pFolderTitle == NULL)
159         {
160                 return;
161         }
162
163         if (previousSceneId == IDSCN_EDIT_BOOKMARK_LIST || __ret == true)
164         {
165                 if (__pBookmark != NULL )
166                 {
167                         delete __pBookmark;
168                         __pBookmark = null;
169                 }
170
171                 if ( pArgs != NULL )
172                 {
173                         __pBookmark = dynamic_cast<BookmarkData*>(pArgs->GetAt(0));
174                 }
175
176                 if (__pBookmark != null)
177                 {
178                         __pFolderTitle->SetText(__pBookmark->GetBookmarkTitle());
179                         __pFolderTitle->SetFocus();
180                         __pFolderTitle->ShowKeypad();
181                 }
182         }
183         else
184         {
185                 __pFolderTitle->SetText(folder01);
186                 __pFolderTitle->SetFocus();
187                 __pFolderTitle->ShowKeypad();
188
189                 r = BookmarkPresentationModel::GetInstance()->GetFolderCount(parentID, foldercount);   // to get count of existing folders
190                 TryCatch( r == E_SUCCESS,,"BookmarkPresentationModel::GetInstance()->GetFolderCount failed  %s",GetErrorMessage(r));
191
192                 r = BookmarkPresentationModel::GetInstance()->GetFolder(parentID, 0, foldercount, folderList);   // to get list of existing folders
193                 TryCatch( r == E_SUCCESS,,"BookmarkPresentationModel::GetInstance()->GetFolder failed  %s",GetErrorMessage(r));
194
195                 while (true)              // to provide user with unique folder title when he creates a new(std::nothrow) folder
196                 {                         // to generate folder with default name of type Folder_##
197                         flag = false ;
198                         title = L"";
199                         count = 0;
200                         counter++;
201                         title.Append(folder01);
202
203                         if (counter/10 == 0)
204                         {
205                                 title.Append(0);
206                         }
207
208                         title.Append(counter);
209
210                         for (count = 0;count < foldercount ; count++)
211                         {
212                                 BookmarkData* pBookmark = dynamic_cast<BookmarkData*>(folderList.GetAt(count));
213                                 folderTitle = L"";
214                                 if ( pBookmark != NULL)
215                                 {
216                                         folderTitle = pBookmark->GetBookmarkTitle();
217                                 }
218
219                                 if (title.Equals(folderTitle,false) == true)
220                                 {
221                                         flag = true;    // if flag is true folder title already exists
222                                         break;          // break from for loop
223                                 }
224
225                         }    // end of for loop
226
227                         if ( flag == false)
228                         {
229                                 __pFolderTitle->SetText(title);      // if flag is false folder title is unique
230                                 break;                               // break from while loop
231                         }
232
233                 }  // end of while loop
234         }
235
236         CATCH:
237
238         folderList.RemoveAll(true);
239         return;
240 }
241
242 void
243 CreateBookmarkFolderForm::OnSceneDeactivated(const SceneId& currentSceneId, const SceneId& nextSceneId)
244 {
245
246 }
247
248 void
249 CreateBookmarkFolderForm::OnActionPerformed(const Control& source, int actionId)
250 {
251         int count = 0;
252         SceneManager* pSceneManager = SceneManager::GetInstance();
253
254         if (pSceneManager == null)
255         {
256                 return;
257         }
258
259         switch (actionId)
260         {
261         case IDA_BUTTON_CANCEL:
262         {
263                 result r;
264                 if (__pFolderTitle != NULL)
265                 {
266                         __pFolderTitle->Clear();
267                         __pFolderTitle->HideKeypad();
268                 }
269
270                 r = pSceneManager->GoBackward(BackwardSceneTransition());
271                 AppLogDebug("SCENE_ADD_BOOKMARK error = %s",GetErrorMessage(r));
272                 TryCatch(!IsFailed(r),L"BookmarkPresentationModel::GetFolderCount failed %s",GetErrorMessage(r));
273
274         }
275         break;
276
277         case IDA_BUTTON_DONE:
278         {
279                 result r = E_SUCCESS;
280                 bool flag = false;
281                 ArrayList folderList;
282                 String parentID = "0";
283                 int foldercount = 0;
284                 String title;
285                 String folderTitle;
286                 folderTitle = __pFolderTitle->GetText();
287                 folderTitle.Trim();
288
289                 r = BookmarkPresentationModel::GetInstance()->GetFolderCount(parentID, foldercount);
290                 TryCatch( !IsFailed(r),,"BookmarkPresentationModel::GetInstance()->GetFolderCount failed %s",GetErrorMessage(r));
291
292                 r = BookmarkPresentationModel::GetInstance()->GetFolder(parentID, 0, foldercount, folderList);
293                 TryCatch( !IsFailed(r),,"BookmarkPresentationModel::GetInstance()->GetFolder failed %s",GetErrorMessage(r));
294
295                 // To check if folder with same name exists
296                 for (count = 0; count < foldercount; count++)
297                 {
298                         BookmarkData* pBookmark = dynamic_cast< BookmarkData* >(folderList.GetAt(count));
299                         if ( pBookmark == NULL )
300                         {
301                                 return;
302                         }
303                         title = pBookmark->GetBookmarkTitle();
304                         title.Trim();
305
306                         if (folderTitle.Equals(title, true) == true)
307                         {
308                                 flag = true;
309                                 break;
310                         }
311                 }
312
313                 if (flag == true && ( __ret == true || __previousScene == IDSCN_EDIT_BOOKMARK_LIST) )
314                 {
315
316                         if(__pBookmark && (__pBookmark->GetBookmarkTitle() == folderTitle ))
317                         {
318                                 SceneManager::GetInstance()->GoBackward(BackwardSceneTransition());
319                                 folderList.RemoveAll(false);
320                                 return;
321                         }
322                 }
323
324                 if (flag == true)
325                 {
326                         String message = " Folder already exists ";
327                         CreateMessage(message);                   // Message alert saying Folder already exists
328                 }
329                 else
330                 {
331                         if (__previousScene == IDSCN_EDIT_BOOKMARK_LIST  || __ret == true )
332                         {
333                                 __pBookmark->SetBookmarkTitle(folderTitle);
334                                 BookmarkPresentationModel::GetInstance()->SaveFolder(*__pBookmark);
335
336                                 r = SceneManager::GetInstance()->GoBackward(BackwardSceneTransition(SCENE_TRANSITION_ANIMATION_TYPE_RIGHT));
337                                 AppLogDebug("SCENE_ADD_BOOKMARK error = %s",GetErrorMessage(r));
338
339                         }
340                         else
341                         {
342                                 ArrayList* pArgList = null;
343                                 pArgList = new(std::nothrow) ArrayList();
344                                 if (pArgList != null)
345                                 {
346                                         pArgList->Construct();
347                                         pArgList->Add(*new(std::nothrow) String(L"2"));
348                                 }
349
350                                 BookmarkData bookmark;
351                                 bookmark.SetBookmarkTitle(folderTitle);
352                                 BookmarkPresentationModel::GetInstance()->SaveFolder(bookmark);
353                                 result r = pSceneManager->GoBackward(BackwardSceneTransition(SCENE_TRANSITION_ANIMATION_TYPE_RIGHT),pArgList);
354                                 AppLogDebug("SCENE_ADD_BOOKMARK error = %s",GetErrorMessage(r));
355                                 if (pArgList != null)
356                                 {
357                                         pArgList->RemoveAll(false);
358                                         delete pArgList;
359                                 }
360                                 TryCatch( !IsFailed(r),L"BookmarkPresentationModel::GetFolder failed %s",GetErrorMessage(r));
361                         }
362
363                 }
364
365                 folderList.RemoveAll(false);
366         }
367         break;
368
369         default:
370                 break;
371         }
372
373         CATCH:  return;
374 }
375
376 void
377 CreateBookmarkFolderForm::OnFormBackRequested(Tizen::Ui::Controls::Form& source)
378 {
379         SceneManager* pSceneManager = SceneManager::GetInstance();
380         if (pSceneManager == null)
381         {
382                 return;
383         }
384         result r;
385         if (__pFolderTitle != NULL)
386         {
387                 __pFolderTitle->Clear();
388                 __pFolderTitle->HideKeypad();
389         }
390
391         r = pSceneManager->GoBackward(BackwardSceneTransition());
392         AppLogDebug("SCENE_ADD_BOOKMARK error = %s",GetErrorMessage(r));
393
394
395 }
396
397 void
398 CreateBookmarkFolderForm::OnKeypadActionPerformed(Control& source, KeypadAction keypadAction)
399 {
400
401 }
402
403 void
404 CreateBookmarkFolderForm::OnKeypadClosed(Control& source)
405 {
406 //      __pFolderTitle->SetFocus();
407 //      __pFolderTitle->HideKeypad();
408 }
409
410 void
411 CreateBookmarkFolderForm::OnKeypadOpened(Control& source)
412 {
413
414 }
415
416 void
417 CreateBookmarkFolderForm::OnKeypadWillOpen(Control& source)
418 {
419
420 }
421
422 void
423 CreateBookmarkFolderForm::OnTextValueChanged(const Control& source)
424 {
425         String title = L"";
426         Header* pHeader = GetHeader();
427
428         if (pHeader == NULL)
429         {
430                 return;
431         }
432
433         if (__pFolderTitle == NULL)
434         {
435                 return;
436         }
437
438         title = __pFolderTitle->GetText();
439         title.Trim();
440
441         // To disable done button when folder title edit-field is empty
442         if (__pFolderTitle && title.GetLength() == 0)
443         {
444                 pHeader->SetButtonEnabled(BUTTON_POSITION_LEFT, false);
445         }
446         else
447         {
448                 pHeader->SetButtonEnabled(BUTTON_POSITION_LEFT, true);
449         }
450         pHeader->Invalidate(true);
451         return;
452 }
453
454 void
455 CreateBookmarkFolderForm::OnTextValueChangeCanceled(const Control& source)
456 {
457
458 }
459
460 void
461 CreateBookmarkFolderForm::CreateMessage(String& msg)
462 {
463
464         if (__pMsgBox != null)
465         {
466                 delete __pMsgBox;
467                 __pMsgBox = null;
468                 __modalMsgBoxResult = 0;
469         }
470
471         __pMsgBox = new(std::nothrow) MessageBox;
472
473         if (__pMsgBox != null)
474         {
475                 __pMsgBox->Construct(L"", msg, MSGBOX_STYLE_OK, 3000);
476         }
477
478         if (__pMsgBox != null)
479         {
480                 __pMsgBox->ShowAndWait(__modalMsgBoxResult);
481                 if(__pFolderTitle != null)
482                         __pFolderTitle->SetFocus();
483                 /*if (__modalMsgBoxResult == MSGBOX_RESULT_OK)
484                 {
485                         AppLog("CreateBookmarkFolderForm::CreateMessage MSGBOX_RESULT_OK");
486                         if (__pMsgBox != null)
487                         {
488                                 delete __pMsgBox;
489                                 __pMsgBox = null;
490                         }
491                 }*/
492                 if (__pMsgBox != null)
493                 {
494                         delete __pMsgBox;
495                         __pMsgBox = null;
496                 }
497                 __modalMsgBoxResult = 0;
498         }
499
500         return;
501 }