Fixed Nabi Issues, Changes for tizen-service implementation and DB Changes
[apps/osp/Internet.git] / src / IntMultipleWindowForm.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 //!Internet MultipleWindowForm class
18 /*@file:        MultipleWindowForm.cpp
19  *@brief:       The MultipleWindowForm provides multiple instance of mainscenes and displays them in a Listview
20  *
21  */
22
23 #include <FAppUiApp.h>
24 #include <FUiScenes.h>
25
26 #include "IntCommonLib.h"
27 #include "IntConfirmationPopup.h"
28 #include "IntMainForm.h"
29 #include "IntMultipleWindowForm.h"
30 #include "IntMultipleWindowPresentationModel.h"
31 #include "IntSceneRegister.h"
32
33 using namespace Tizen::App;
34 using namespace Tizen::Base;
35 using namespace Tizen::Base::Collection;
36 using namespace Tizen::Graphics;
37 using namespace Tizen::Ui;
38 using namespace Tizen::Ui::Controls;
39 using namespace Tizen::Ui::Scenes;
40
41 static const int W_ICON_IMAGE_WIDTH = 72;
42
43 MultipleWindowForm::MultipleWindowForm(void)
44 : __pList(null)
45 , __pListIconImage(null)
46 , __pListDeleteImage(null)
47 {
48 }
49
50 MultipleWindowForm::~MultipleWindowForm(void)
51 {
52         if (__pListIconImage)
53         {
54                 delete __pListIconImage;
55                 __pListIconImage = null;
56         }
57         if (__pListDeleteImage)
58         {
59                 delete __pListDeleteImage;
60                 __pListDeleteImage = null;
61         }
62 }
63
64 bool
65 MultipleWindowForm::Initialize(void)
66 {
67         result r = E_SUCCESS;
68
69         r = Construct(L"IDL_MULTIPLE_WINDOW");
70         if (r == E_SUCCESS)
71         {
72                 return true;
73         }
74         else
75         {
76                 return false;
77         }
78 }
79
80 result
81 MultipleWindowForm::OnInitializing(void)
82 {
83         result r = E_SUCCESS;
84         FooterItem closeButton;
85         FooterItem newButton;
86
87         SceneManager* pSceneManager = SceneManager::GetInstance();
88
89         if (pSceneManager == null)
90         {
91                 return E_FAILURE;
92         }
93
94         Footer* pFooter = GetFooter();
95
96         if (pFooter == NULL)
97         {
98                 return E_FAILURE;
99         }
100
101         SetFormStyle(FORM_STYLE_NORMAL | FORM_STYLE_INDICATOR | FORM_STYLE_HEADER | FORM_STYLE_FOOTER);
102
103         r = pSceneManager->AddSceneEventListener(IDSCN_MULTIPLE_WINDOW, *this);
104
105         // Setup back event listener
106         SetFormBackEventListener(this);
107         pFooter->AddActionEventListener(*this);
108
109         AppResource* pAppResource = Application::GetInstance()->GetAppResource();
110
111         if (pAppResource)
112         {
113                 __pListIconImage = pAppResource->GetBitmapN(L"ListIcon.png");
114                 __pListDeleteImage = pAppResource->GetBitmapN(L"deleteIcon.png");
115         }
116
117         __pList = static_cast< ListView* >(GetControl(L"IDC_LISTVIEW"));
118
119         if (__pList != null)
120         {
121                 __pList->SetItemProvider(*this);
122                 __pList->AddListViewItemEventListener(*this);
123         }
124
125         AddOrientationEventListener(*this);
126 //      r = pFooter->SetStyle(FOOTER_STYLE_BUTTON_TEXT);
127         r = closeButton.Construct(IDA_BUTTON_CLOSEALL);
128         r = closeButton.SetText(CommonUtil::GetString(L"IDS_BR_CLOSE_ALL"));
129         r = pFooter->AddItem(closeButton);
130         r = newButton.Construct(IDA_BUTTON_NEW);
131         r = newButton.SetText(CommonUtil::GetString(L"IDS_BR_SK1_NEW_WINDOW"));
132         r = pFooter->AddItem(newButton);
133
134         return r;
135 }
136
137 void
138 MultipleWindowForm::OnSceneActivatedN(const SceneId& previousSceneId, const SceneId& currentSceneId, IList* pArgs)
139 {
140         if (MultipleWindowPresentationModel::GetInstance()->GetAllWindowArrayList()->GetCount() >= 9)
141         {
142                 GetFooter()->SetItemEnabled(1,false);
143         }
144         else
145         {
146                 GetFooter()->SetItemEnabled(1,true);
147         }
148         if (__pList)
149         {
150                 __pList->UpdateList();
151         }
152         GetFooter()->Invalidate(true);
153         return;
154 }
155
156 void
157 MultipleWindowForm::OnSceneDeactivated(const SceneId& currentSceneId, const SceneId& nextSceneId)
158 {
159         if (__pListIconImage)
160         {
161                 delete __pListIconImage;
162                 __pListIconImage = NULL;
163         }
164         return;
165 }
166
167 result
168 MultipleWindowForm::OnTerminating(void)
169 {
170         result r = E_SUCCESS;
171
172         if (__pListIconImage)
173         {
174                 delete __pListIconImage;
175                 __pListIconImage = NULL;
176         }
177         if (__pListDeleteImage)
178         {
179                 delete __pListDeleteImage;
180                 __pListDeleteImage = NULL;
181         }
182
183         return r;
184 }
185
186 void
187 MultipleWindowForm::OnActionPerformed(const Control& source, int actionId)
188 {
189         SceneManager* pSceneManager = SceneManager::GetInstance();
190
191         if (pSceneManager == null)
192         {
193                 return;
194         }
195
196         String closeWarning = CommonUtil::GetString(L"IDS_BR_CLOSE_ALL_WINDOWS_Q");
197
198         switch (actionId)
199         {
200         case IDA_BUTTON_NEW:
201         {
202                 WindowInfo* pNewWindowInfo = MultipleWindowPresentationModel::GetInstance()->CreateNewMainViewScene();
203
204                 if (pNewWindowInfo == null)
205                 {
206                         return;
207                 }
208                 result r = E_SUCCESS;
209                 SceneManager* pSceneManager = SceneManager::GetInstance();
210                 if (pSceneManager == NULL)
211                 {
212                         return;
213                 }
214                 ArrayList* pArgList = new(std::nothrow) ArrayList();
215                 if (pArgList == NULL)
216                 {
217                         return;
218                 }
219                 pArgList->Construct();
220
221                 pArgList->Add(*pNewWindowInfo);
222                 r = pSceneManager->GoForward(ForwardSceneTransition(pNewWindowInfo->sceneID, SCENE_TRANSITION_ANIMATION_TYPE_NONE),pArgList);
223                 if (pArgList != null)
224                 {
225                         delete pArgList;
226                         pArgList = null;
227                 }
228                 if(IsFailed(r))
229                 {
230                         AppLogDebug("MultipleWindowForm::OnActionPerformed GoForward failed %s",GetErrorMessage(r));
231                         return;
232                 }
233         }
234         break;
235
236         case IDA_BUTTON_BACK:
237         {
238                 String* pSelectedScene = NULL;
239                 Object* value = NULL;
240                 MultipleWindowPresentationModel::GetInstance()->GetValue(SELECTED_SCENE_ID, &value);
241                 pSelectedScene = (String*) value;
242
243                 if (pSelectedScene)
244                 {
245                         result r = pSceneManager->GoBackward(BackwardSceneTransition(*pSelectedScene, SCENE_TRANSITION_ANIMATION_TYPE_NONE));
246                         delete pSelectedScene;
247                         pSelectedScene = null;
248                         if(IsFailed(r))
249                         {
250                                 AppLogDebug("MultipleWindowForm::OnActionPerformed GoForward failed %s",GetErrorMessage(r));
251                                 return;
252                         }
253                 }
254         }
255         break;
256
257         case IDA_BUTTON_CLOSEALL:
258         {
259                 __pConfirmationPopup = new(std::nothrow) ConfirmationPopup();
260                 __pConfirmationPopup->Initialize();
261                 __pConfirmationPopup->RemoveActionListener(*this);
262                 __pConfirmationPopup->AddActionListener(*this);
263                 __pConfirmationPopup->setMessage(closeWarning);
264                 __pConfirmationPopup->Show();
265
266         }
267         break;
268         case IDA_CONFIRMATION_NO:
269         {
270                 __pConfirmationPopup->SetShowState(false);
271                 __pConfirmationPopup->Show();
272         }
273         break;
274         case IDA_CONFIRMATION_YES:
275         {
276                 ArrayList* pAllWindowList = MultipleWindowPresentationModel::GetInstance()->GetAllWindowArrayList();
277                 if (pAllWindowList == NULL)
278                 {
279                         return;
280                 }
281                 int totalCount = pAllWindowList->GetCount();
282
283                 for (int count = 0; count < totalCount; count++)
284                 {
285                         WindowInfo* pWindowInfo = dynamic_cast< WindowInfo* >(pAllWindowList->GetAt(count));
286                         SceneRegister::DestroyAndUnRegisterScene(pWindowInfo->sceneID);
287                 }
288                 pAllWindowList->RemoveAll(true);
289                 WindowInfo* pNewWindowInfo = MultipleWindowPresentationModel::GetInstance()->CreateNewMainViewScene();
290
291                 if (pNewWindowInfo == null)
292                 {
293                         return;
294                 }
295                 result r = E_SUCCESS;
296                 SceneManager* pSceneManager = SceneManager::GetInstance();
297                 if (pSceneManager == NULL)
298                 {
299                         return;
300                 }
301                 ArrayList* pArgList = new(std::nothrow) ArrayList();
302                 if (pArgList == NULL)
303                 {
304                         return;
305                 }
306                 r = pArgList->Construct();
307                 if (r == E_SUCCESS)
308                 {
309                         pArgList->Add(*pNewWindowInfo);
310                         result r = pSceneManager->GoForward(ForwardSceneTransition(pNewWindowInfo->sceneID, SCENE_TRANSITION_ANIMATION_TYPE_NONE),pArgList);
311                         if (pArgList != null)
312                         {
313                                 delete pArgList;
314                                 pArgList = null;
315                         }
316                         if(IsFailed(r))
317                         {
318                                 AppLogDebug("MultipleWindowForm::OnActionPerformed GoForward failed %s",GetErrorMessage(r));
319                                 return;
320                         }
321                 }
322                 __pConfirmationPopup->SetShowState(false);
323                 __pConfirmationPopup->Show();
324         }
325         break;
326         default:
327                 break;
328         }
329
330 }
331
332 void
333 MultipleWindowForm::OnFormBackRequested(Form& source)
334 {
335
336         String* pSelectedScene = NULL;
337         Object* value = NULL;
338
339         SceneManager* pSceneManager = SceneManager::GetInstance();
340         if (pSceneManager == null)
341         {
342                 return;
343         }
344
345         MultipleWindowPresentationModel::GetInstance()->GetValue(SELECTED_SCENE_ID, &value);
346         pSelectedScene = (String*) value;
347         if (pSelectedScene != NULL)
348         {
349                 result r = pSceneManager->GoBackward(BackwardSceneTransition(*pSelectedScene, SCENE_TRANSITION_ANIMATION_TYPE_NONE));
350 //              delete pSelectedScene;
351                 if(IsFailed(r))
352                 {
353                         AppLogDebug("MultipleWindowForm::OnFormBackRequested GoForward failed %s",GetErrorMessage(r));
354                         return;
355                 }
356         }
357         return;
358 }
359
360 void
361 MultipleWindowForm::OnListViewContextItemStateChanged(ListView& listView, int index, int elementId, ListContextItemStatus state)
362 {
363         return;
364 }
365
366 void
367 MultipleWindowForm::OnListViewItemStateChanged(ListView& listView, int index, int elementId, ListItemStatus status)
368 {
369         result r = E_SUCCESS;
370         ArrayList* pAllWindowList = MultipleWindowPresentationModel::GetInstance()->GetAllWindowArrayList();
371         int count = 0;
372
373         if (elementId == IDA_FORMAT_DELETE_BITMAP)
374         {
375                 // get current scene ID and check if it is matching with deleted scene id
376                 WindowInfo* pWindowInfo = dynamic_cast< WindowInfo* >(pAllWindowList->GetAt(index));
377                 String* pSelectedScene = NULL;
378                 Object* pValue = NULL;
379
380                 if (pWindowInfo)
381                 {
382                         SceneRegister::DestroyAndUnRegisterScene(pWindowInfo->sceneID);
383                         MultipleWindowPresentationModel::GetInstance()->GetValue(SELECTED_SCENE_ID, &pValue);
384                         pSelectedScene = (String*) pValue;
385                         pAllWindowList->RemoveAt(index, true);
386                         if (pSelectedScene->CompareTo(pWindowInfo->sceneID) == 0)
387                         {
388                                 //set current scene id to last scene id
389                                 int totalCount = pAllWindowList->GetCount() - 1;
390                                 WindowInfo* pLastIndexWindowInfo = dynamic_cast< WindowInfo* >(pAllWindowList->GetAt(totalCount));
391                                 if (pLastIndexWindowInfo != NULL)
392                                 {
393                                         String* pSelectedSceneID = new(std::nothrow) String(pLastIndexWindowInfo->sceneID);
394                                         if (pSelectedSceneID != NULL)
395                                         {
396                                                 MultipleWindowPresentationModel::GetInstance()->SetValue(SELECTED_SCENE_ID, (Object*) pSelectedSceneID);
397
398                                         }
399                                 }
400                         }
401                 }
402
403                 count = MultipleWindowPresentationModel::GetInstance()->GetInstance()->GetAllWindowArrayList()->GetCount();
404
405                 if (count >= 9)
406                 {
407                         GetFooter()->SetItemEnabled(1,false);
408                 }
409                 else
410                 {
411                         GetFooter()->SetItemEnabled(1,true);
412                 }
413
414                 if ( count <= 1)
415                 {
416                         GetFooter()->SetItemEnabled(0,false);
417                 }
418
419                 GetFooter()->Invalidate(true);
420                 __pList->RefreshList(index, LIST_REFRESH_TYPE_ITEM_REMOVE);
421                 __pList->UpdateList();
422         }
423         else if(elementId == IDA_FORMAT_BITMAP || elementId == IDA_FORMAT_TITLE_STRING
424                         || elementId == IDA_FORMAT_URL_STRING || elementId == -1)
425         {
426
427                 // launch the browser
428                 WindowInfo* pWindowInfo = dynamic_cast< WindowInfo* >(pAllWindowList->GetAt(index));
429                 if (pWindowInfo == NULL)
430                 {
431                         return;
432                 }
433                 SceneManager* pSceneManager = SceneManager::GetInstance();
434                 if (pSceneManager == NULL)
435                 {
436                         return;
437                 }
438                 ArrayList* pArgList = new(std::nothrow) ArrayList();
439                 if (pArgList == NULL)
440                 {
441                         return;
442                 }
443                 r = pArgList->Construct();
444                 if (IsFailed(r))
445                 {
446                         return;
447                 }
448                 pArgList->Add(*pWindowInfo);
449                 result r = pSceneManager->GoForward(ForwardSceneTransition(pWindowInfo->sceneID, SCENE_TRANSITION_ANIMATION_TYPE_NONE),pArgList);
450
451                 if (r == E_SUCCESS)
452                 {
453                         AppLogDebug("go forward success");
454                 }
455                 else
456                 {
457                         AppLogDebug("go forward failed");
458                 }
459                 if (pArgList != null)
460                 {
461                         delete pArgList;
462                         pArgList = null;
463                 }
464
465         }
466
467         return;
468 }
469
470 void
471 MultipleWindowForm::OnListViewItemSwept(ListView& listView, int index, SweepDirection direction)
472 {
473         return;
474 }
475
476 void
477 MultipleWindowForm::OnListViewItemLongPressed(ListView& listView, int index, int elementId, bool& invokeListViewItemCallback)
478 {
479         return;
480 }
481
482 ListItemBase*
483 MultipleWindowForm::CreateItem(int index, int itemWidth)
484 {
485         Rectangle listImageRect;
486         Rectangle pagetTitleRect;
487         Rectangle pageURLRect;
488         Rectangle deleteImageRect;
489         String pageTitle(L"");
490         String pageURL(L"");
491         result r = E_SUCCESS;
492         int width = W_ICON_IMAGE_WIDTH;
493
494         AppResource* pAppResource = Application::GetInstance()->GetAppResource();
495         ListAnnexStyle style = LIST_ANNEX_STYLE_NORMAL;
496         ArrayList* pAllWindowList = MultipleWindowPresentationModel::GetInstance()->GetAllWindowArrayList();
497
498         if (pAllWindowList == null)
499         {
500                 return null;
501         }
502         WindowInfo* pWindowInfo = dynamic_cast< WindowInfo* >(pAllWindowList->GetAt(index));
503         if(pWindowInfo == null)
504         {
505                 return NULL;
506         }
507         CustomItem* pItem = new(std::nothrow) CustomItem();
508         pageTitle = pWindowInfo->pageTitle;
509         pageURL = pWindowInfo->pageUrl;
510         if (pageURL.GetLength() == 0)
511         {
512                 String nourl;
513                 pAppResource->GetString(L"IDS_BR_BODY_ABOUT_C_BLANK", nourl);
514                 pageURL = L"<"+ nourl +">";
515         }
516
517         Rectangle screenBounds = GetBounds();
518         r = pItem->Construct(Dimension(itemWidth, 128), style);
519         if (IsFailed(r))
520         {
521                 return NULL;
522         }
523
524         if (pWindowInfo->pFavicon)
525         {
526
527                 if( __pListIconImage != NULL)
528                 {
529                         delete __pListIconImage;
530                         __pListIconImage = null;
531                 }
532
533                 __pListIconImage = new Bitmap();
534                 __pListIconImage->Construct(*(pWindowInfo->pFavicon),Rectangle(0,0,pWindowInfo->pFavicon->GetWidth(),pWindowInfo->pFavicon->GetHeight()));
535         }
536         else
537         {
538                 if( __pListIconImage != NULL)
539                 {
540                         delete __pListIconImage;
541                         __pListIconImage = null;
542                 }
543
544                 __pListIconImage = pAppResource->GetBitmapN(L"I01_icon_default_favicon.png");
545         }
546         AppLog("deleteimg %d %d",__pListDeleteImage->GetWidth(),__pListDeleteImage->GetHeight());
547
548         listImageRect.SetBounds(screenBounds.x + 16, screenBounds.y + 28,width, 72);
549         pagetTitleRect.SetBounds(listImageRect.x + width + 16,10, screenBounds.width - 2 * width - 64, 60);
550         pageURLRect.SetBounds(pagetTitleRect.x, pagetTitleRect.y + pagetTitleRect.height, screenBounds.width - 2 * width - 120, 48);
551         deleteImageRect.SetBounds(screenBounds.width - __pListDeleteImage->GetWidth() - 24, (128 - __pListDeleteImage->GetHeight() - 8)/2, __pListDeleteImage->GetWidth() + 8, __pListDeleteImage->GetHeight() + 8);
552         pItem->AddElement(listImageRect, IDA_FORMAT_BITMAP, *__pListIconImage, null, null);
553
554         if (pageTitle.CompareTo(L"") != 0)
555         {
556                 pItem->AddElement(pagetTitleRect, IDA_FORMAT_TITLE_STRING, pageTitle, 44, CUSTOM_COLOR_TRANSPARENT, CUSTOM_COLOR_TRANSPARENT, CUSTOM_COLOR_TRANSPARENT, true);
557         }
558
559         if (pageURL.CompareTo(L"") != 0)
560         {
561                 pItem->AddElement(pageURLRect, IDA_FORMAT_URL_STRING, pageURL, 32, CUSTOM_COLOR_GREY, CUSTOM_COLOR_GREY, CUSTOM_COLOR_GREY, true);
562         }
563
564         if (pAllWindowList)
565         {
566                 if (pAllWindowList->GetCount() > 1)
567                 {
568                         pItem->AddElement(deleteImageRect, IDA_FORMAT_DELETE_BITMAP, *__pListDeleteImage, null, null);
569                         pItem->SetElementSelectionEnabled(IDA_FORMAT_DELETE_BITMAP, true);                      
570                 }
571         }
572         return pItem;
573 }
574
575 bool
576 MultipleWindowForm::DeleteItem(int index, ListItemBase* pItem, int itemWidth)
577 {
578         delete pItem;
579         pItem = null;
580         return true;
581 }
582
583 int
584 MultipleWindowForm::GetItemCount(void)
585 {
586         ArrayList* pAllWindowList = MultipleWindowPresentationModel::GetInstance()->GetAllWindowArrayList();
587         int windowCount = 0;
588
589         if (pAllWindowList != null)
590         {
591                 windowCount = pAllWindowList->GetCount();
592         }
593         if (windowCount < 2)
594         {
595                 GetFooter()->SetItemEnabled(0, false);
596         }
597         else
598         {
599                 GetFooter()->SetItemEnabled(0, true);
600         }
601         GetFooter()->Invalidate(true);
602         return windowCount;
603 }
604
605 void
606 MultipleWindowForm::OnOrientationChanged(const Control& source, OrientationStatus orientationStatus)
607 {
608         if (__pList)
609         {
610                 __pList->UpdateList();
611         }
612 }