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