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