Tizen 2.0 Release
[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.0 (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 = "-1";
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_HEADER_EDIT_FOLDER");
124
125         String folder01 = CommonUtil::GetString(L"IDS_BR_BODY_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_HEADER_EDIT_FOLDER"));
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(SCENE_TRANSITION_ANIMATION_TYPE_RIGHT));
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 = "-1";
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, false) == true)
310                         {
311                                 flag = true;
312                                 break;
313                         }
314                 }
315
316                 if (flag == true && ( __ret == true || __previousScene == IDSCN_EDIT_BOOKMARK_LIST))
317                 {
318                         SceneManager::GetInstance()->GoBackward(BackwardSceneTransition(SCENE_TRANSITION_ANIMATION_TYPE_RIGHT));
319                         folderList.RemoveAll(true);
320                         return;
321                 }
322
323                 if (flag == true)
324                 {
325                         String message = " Folder already exists ";
326                         CreateMessage(message);                   // Message alert saying Folder already exists
327                 }
328                 else
329                 {
330                         if (__previousScene == IDSCN_EDIT_BOOKMARK_LIST  || __ret == true )
331                         {
332                                 __pBookmark->SetBookmarkTitle(folderTitle);
333                                 BookmarkPresentationModel::GetInstance()->SaveFolder(*__pBookmark);
334
335                                 r = SceneManager::GetInstance()->GoBackward(BackwardSceneTransition(SCENE_TRANSITION_ANIMATION_TYPE_RIGHT));
336                                 AppLogDebug("SCENE_ADD_BOOKMARK error = %s",GetErrorMessage(r));
337
338                         }
339                         else
340                         {
341                                 ArrayList* pArgList = null;
342                                 pArgList = new(std::nothrow) ArrayList();
343                                 if (pArgList != null)
344                                 {
345                                         pArgList->Construct();
346                                         pArgList->Add(*new(std::nothrow) String(L"2"));
347                                 }
348
349                                 BookmarkData bookmark;
350                                 bookmark.SetBookmarkTitle(folderTitle);
351                                 BookmarkPresentationModel::GetInstance()->SaveFolder(bookmark);
352                                 result r = pSceneManager->GoBackward(BackwardSceneTransition(SCENE_TRANSITION_ANIMATION_TYPE_RIGHT),pArgList);
353                                 AppLogDebug("SCENE_ADD_BOOKMARK error = %s",GetErrorMessage(r));
354                                 if (pArgList != null)
355                                 {
356                                         pArgList->RemoveAll(true);
357                                         delete pArgList;
358                                 }
359                                 TryCatch( !IsFailed(r),L"BookmarkPresentationModel::GetFolder failed %s",GetErrorMessage(r));
360                         }
361
362                 }
363
364                 folderList.RemoveAll(true);
365         }
366         break;
367
368         default:
369                 break;
370         }
371
372         CATCH:  return;
373 }
374
375 void
376 CreateBookmarkFolderForm::OnKeypadActionPerformed(Control& source, KeypadAction keypadAction)
377 {
378
379 }
380
381 void
382 CreateBookmarkFolderForm::OnKeypadClosed(Control& source)
383 {
384
385 }
386
387 void
388 CreateBookmarkFolderForm::OnKeypadOpened(Control& source)
389 {
390
391 }
392
393 void
394 CreateBookmarkFolderForm::OnKeypadWillOpen(Control& source)
395 {
396
397 }
398
399 void
400 CreateBookmarkFolderForm::OnTextValueChanged(const Control& source)
401 {
402         String title = L"";
403         Header* pHeader = GetHeader();
404
405         if (pHeader == NULL)
406         {
407                 return;
408         }
409
410         if (__pFolderTitle == NULL)
411         {
412                 return;
413         }
414
415         title = __pFolderTitle->GetText();
416         title.Trim();
417
418         // To disable done button when folder title edit-field is empty
419         if (__pFolderTitle && title.GetLength() == 0)
420         {
421                 pHeader->SetButtonEnabled(BUTTON_POSITION_LEFT, false);
422         }
423         else
424         {
425                 pHeader->SetButtonEnabled(BUTTON_POSITION_LEFT, true);
426         }
427         pHeader->Invalidate(true);
428         return;
429 }
430
431 void
432 CreateBookmarkFolderForm::OnTextValueChangeCanceled(const Control& source)
433 {
434
435 }
436
437 void
438 CreateBookmarkFolderForm::CreateMessage(String& msg)
439 {
440
441         if (__pMsgBox != null)
442         {
443                 delete __pMsgBox;
444                 __pMsgBox = null;
445                 __modalMsgBoxResult = 0;
446         }
447
448         __pMsgBox = new(std::nothrow) MessageBox;
449
450         if (__pMsgBox != null)
451         {
452                 __pMsgBox->Construct(L"", msg, MSGBOX_STYLE_OK, 3000);
453         }
454
455         if (__pMsgBox != null)
456         {
457                 __pMsgBox->ShowAndWait(__modalMsgBoxResult);
458                 if(__pFolderTitle != null)
459                         __pFolderTitle->SetFocus();
460                 /*if (__modalMsgBoxResult == MSGBOX_RESULT_OK)
461                 {
462                         AppLog("CreateBookmarkFolderForm::CreateMessage MSGBOX_RESULT_OK");
463                         if (__pMsgBox != null)
464                         {
465                                 delete __pMsgBox;
466                                 __pMsgBox = null;
467                         }
468                 }*/
469                 if (__pMsgBox != null)
470                 {
471                         delete __pMsgBox;
472                         __pMsgBox = null;
473                 }
474                 __modalMsgBoxResult = 0;
475         }
476
477         return;
478 }