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                         delete __pConfirmationPopup;
268                         __pConfirmationPopup = null;
269                 }
270                 if (!__pConfirmationPopup)
271                 {
272                         __pConfirmationPopup = new(std::nothrow) ConfirmationPopup();
273                         __pConfirmationPopup->Initialize();
274
275                         __pConfirmationPopup->RemoveActionListener(*this);
276                         __pConfirmationPopup->AddActionListener(*this);
277                         __pConfirmationPopup->setMessage(closeWarning);
278                         __pConfirmationPopup->Show();
279                 }
280         }
281         break;
282         case IDA_CONFIRMATION_NO:
283         {
284                 __pConfirmationPopup->SetShowState(false);
285                 __pConfirmationPopup->Show();
286
287                 delete __pConfirmationPopup;
288                 __pConfirmationPopup = null;
289
290         }
291         break;
292         case IDA_CONFIRMATION_YES:
293         {
294                 ArrayList* pAllWindowList = MultipleWindowPresentationModel::GetInstance()->GetAllWindowArrayList();
295                 if (pAllWindowList == NULL)
296                 {
297                         return;
298                 }
299                 int totalCount = pAllWindowList->GetCount();
300
301                 for (int count = 0; count < totalCount; count++)
302                 {
303                         WindowInfo* pWindowInfo = dynamic_cast< WindowInfo* >(pAllWindowList->GetAt(count));
304                         SceneRegister::DestroyAndUnRegisterScene(pWindowInfo->sceneID);
305                 }
306                 pAllWindowList->RemoveAll(true);
307                 WindowInfo* pNewWindowInfo = MultipleWindowPresentationModel::GetInstance()->CreateNewMainViewSceneN();
308
309                 if (pNewWindowInfo == null)
310                 {
311                         return;
312                 }
313                 result r = E_SUCCESS;
314                 SceneManager* pSceneManager = SceneManager::GetInstance();
315                 if (pSceneManager == NULL)
316                 {
317                         return;
318                 }
319                 ArrayList* pArgList = new(std::nothrow) ArrayList();
320                 if (pArgList == NULL)
321                 {
322                         return;
323                 }
324                 r = pArgList->Construct();
325                 if (r == E_SUCCESS)
326                 {
327                         pArgList->Add(*pNewWindowInfo);
328                         result r = pSceneManager->GoForward(ForwardSceneTransition(pNewWindowInfo->sceneID, SCENE_TRANSITION_ANIMATION_TYPE_NONE),pArgList);
329                         if (pArgList != null)
330                         {
331                                 delete pArgList;
332                                 pArgList = null;
333                         }
334                         if(IsFailed(r))
335                         {
336                                 AppLogDebug("MultipleWindowForm::OnActionPerformed GoForward failed %s",GetErrorMessage(r));
337                                 return;
338                         }
339                 }
340                 __pConfirmationPopup->SetShowState(false);
341                 __pConfirmationPopup->Show();
342
343                 delete __pConfirmationPopup;
344                 __pConfirmationPopup = null;
345
346         }
347         break;
348         default:
349                 break;
350         }
351
352 }
353
354 void
355 MultipleWindowForm::OnFormBackRequested(Form& source)
356 {
357
358         String* pSelectedScene = NULL;
359         Object* value = NULL;
360
361         SceneManager* pSceneManager = SceneManager::GetInstance();
362         if (pSceneManager == null)
363         {
364                 return;
365         }
366
367         MultipleWindowPresentationModel::GetInstance()->GetValue(SELECTED_SCENE_ID, &value);
368         pSelectedScene = (String*) value;
369         if (pSelectedScene != NULL)
370         {
371                 result r = pSceneManager->GoBackward(BackwardSceneTransition(*pSelectedScene, SCENE_TRANSITION_ANIMATION_TYPE_NONE));
372 //              delete pSelectedScene;
373                 if(IsFailed(r))
374                 {
375                         AppLogDebug("MultipleWindowForm::OnFormBackRequested GoForward failed %s",GetErrorMessage(r));
376                         return;
377                 }
378         }
379         return;
380 }
381
382 void
383 MultipleWindowForm::OnListViewContextItemStateChanged(ListView& listView, int index, int elementId, ListContextItemStatus state)
384 {
385         return;
386 }
387
388 void
389 MultipleWindowForm::OnListViewItemStateChanged(ListView& listView, int index, int elementId, ListItemStatus status)
390 {
391         result r = E_SUCCESS;
392         ArrayList* pAllWindowList = MultipleWindowPresentationModel::GetInstance()->GetAllWindowArrayList();
393         int count = 0;
394
395         if (elementId == IDA_FORMAT_DELETE_BITMAP)
396         {
397                 // get current scene ID and check if it is matching with deleted scene id
398                 WindowInfo* pWindowInfo = dynamic_cast< WindowInfo* >(pAllWindowList->GetAt(index));
399                 String* pSelectedScene = NULL;
400                 Object* pValue = NULL;
401
402                 if (pWindowInfo)
403                 {
404                         SceneRegister::DestroyAndUnRegisterScene(pWindowInfo->sceneID);
405                         MultipleWindowPresentationModel::GetInstance()->GetValue(SELECTED_SCENE_ID, &pValue);
406                         pSelectedScene = (String*) pValue;
407                         pAllWindowList->RemoveAt(index, true);
408                         if (pSelectedScene->CompareTo(pWindowInfo->sceneID) == 0)
409                         {
410                                 //set current scene id to last scene id
411                                 int totalCount = pAllWindowList->GetCount() - 1;
412                                 WindowInfo* pLastIndexWindowInfo = dynamic_cast< WindowInfo* >(pAllWindowList->GetAt(totalCount));
413                                 if (pLastIndexWindowInfo != NULL)
414                                 {
415                                         String* pSelectedSceneID = new(std::nothrow) String(pLastIndexWindowInfo->sceneID);
416                                         if (pSelectedSceneID != NULL)
417                                         {
418                                                 MultipleWindowPresentationModel::GetInstance()->SetValue(SELECTED_SCENE_ID, (Object*) pSelectedSceneID);
419
420                                         }
421                                 }
422                         }
423                 }
424
425                 count = MultipleWindowPresentationModel::GetInstance()->GetInstance()->GetAllWindowArrayList()->GetCount();
426
427                 if (count >= 9)
428                 {
429                         GetFooter()->SetItemEnabled(1,false);
430                 }
431                 else
432                 {
433                         GetFooter()->SetItemEnabled(1,true);
434                 }
435
436                 if ( count <= 1)
437                 {
438                         GetFooter()->SetItemEnabled(0,false);
439                 }
440
441                 GetFooter()->Invalidate(true);
442                 __pList->RefreshList(index, LIST_REFRESH_TYPE_ITEM_REMOVE);
443                 __pList->UpdateList();
444         }
445         else if(elementId == IDA_FORMAT_BITMAP || elementId == IDA_FORMAT_TITLE_STRING
446                         || elementId == IDA_FORMAT_URL_STRING || elementId == -1)
447         {
448
449                 // launch the browser
450                 WindowInfo* pWindowInfo = dynamic_cast< WindowInfo* >(pAllWindowList->GetAt(index));
451                 if (pWindowInfo == NULL)
452                 {
453                         return;
454                 }
455                 SceneManager* pSceneManager = SceneManager::GetInstance();
456                 if (pSceneManager == NULL)
457                 {
458                         return;
459                 }
460                 ArrayList* pArgList = new(std::nothrow) ArrayList();
461                 if (pArgList == NULL)
462                 {
463                         return;
464                 }
465                 r = pArgList->Construct();
466                 if (IsFailed(r))
467                 {
468                         return;
469                 }
470                 pArgList->Add(*pWindowInfo);
471                 result r = pSceneManager->GoForward(ForwardSceneTransition(pWindowInfo->sceneID, SCENE_TRANSITION_ANIMATION_TYPE_NONE),pArgList);
472
473                 if (r == E_SUCCESS)
474                 {
475                         AppLogDebug("go forward success");
476                 }
477                 else
478                 {
479                         AppLogDebug("go forward failed");
480                 }
481                 if (pArgList != null)
482                 {
483                         delete pArgList;
484                         pArgList = null;
485                 }
486
487         }
488
489         return;
490 }
491
492 void
493 MultipleWindowForm::OnListViewItemSwept(ListView& listView, int index, SweepDirection direction)
494 {
495         return;
496 }
497
498 void
499 MultipleWindowForm::OnListViewItemLongPressed(ListView& listView, int index, int elementId, bool& invokeListViewItemCallback)
500 {
501         return;
502 }
503
504 ListItemBase*
505 MultipleWindowForm::CreateItem(int index, int itemWidth)
506 {
507         Rectangle listImageRect;
508         Rectangle pagetTitleRect;
509         Rectangle pageURLRect;
510         Rectangle deleteImageRect;
511         String pageTitle(L"");
512         String pageURL(L"");
513         result r = E_SUCCESS;
514         int width = W_ICON_IMAGE_WIDTH;
515
516         AppResource* pAppResource = Application::GetInstance()->GetAppResource();
517         ListAnnexStyle style = LIST_ANNEX_STYLE_NORMAL;
518         ArrayList* pAllWindowList = MultipleWindowPresentationModel::GetInstance()->GetAllWindowArrayList();
519
520         if (pAllWindowList == null)
521         {
522                 return null;
523         }
524         WindowInfo* pWindowInfo = dynamic_cast< WindowInfo* >(pAllWindowList->GetAt(index));
525         if(pWindowInfo == null)
526         {
527                 return NULL;
528         }
529         CustomItem* pItem = new(std::nothrow) CustomItem();
530         pageTitle = pWindowInfo->pageTitle;
531         pageURL = pWindowInfo->pageUrl;
532         if (pageURL.GetLength() == 0)
533         {
534                 String nourl;
535                 pAppResource->GetString(L"IDS_BR_BODY_BLANK_PAGE", nourl);
536                 pageURL = L"<"+ nourl +">";
537         }
538
539         if (pageTitle.GetLength() == 0)
540         {
541                 String nourl;
542                 pAppResource->GetString(L"IDS_BR_BODY_BLANK_PAGE", pageTitle);
543         }
544
545         Rectangle screenBounds = GetBounds();
546         r = pItem->Construct(Dimension(itemWidth, 128 + (__fontSize - 44)), style);
547         if (IsFailed(r))
548         {
549                 return NULL;
550         }
551
552         if (pWindowInfo->pFavicon)
553         {
554
555                 if( __pListIconImage != NULL)
556                 {
557                         delete __pListIconImage;
558                         __pListIconImage = null;
559                 }
560
561                 __pListIconImage = new Bitmap();
562                 __pListIconImage->Construct(*(pWindowInfo->pFavicon),Rectangle(0,0,pWindowInfo->pFavicon->GetWidth(),pWindowInfo->pFavicon->GetHeight()));
563         }
564         else
565         {
566                 if( __pListIconImage != NULL)
567                 {
568                         delete __pListIconImage;
569                         __pListIconImage = null;
570                 }
571
572                 __pListIconImage = pAppResource->GetBitmapN(L"I01_icon_default_favicon.png");
573         }
574
575
576         listImageRect.SetBounds(screenBounds.x + 16, screenBounds.y + 28 + (__fontSize - 44)/2,width, 72  );
577         pagetTitleRect.SetBounds(listImageRect.x + width + 16,10, screenBounds.width - 2 * width - 24 - 64, 72 + (__fontSize - 44));
578         pageURLRect.SetBounds(pagetTitleRect.x, pagetTitleRect.y + pagetTitleRect.height, screenBounds.width - 2 * width - 120, 48);
579         if(__pListDeleteImage != null)
580         {
581                 deleteImageRect.SetBounds(screenBounds.width - __pListDeleteImage->GetWidth() - 24, (128 + (__fontSize - 44) - __pListDeleteImage->GetHeight() - 8)/2, __pListDeleteImage->GetWidth() + 8, __pListDeleteImage->GetHeight() + 8);
582         }
583         pItem->AddElement(listImageRect, IDA_FORMAT_BITMAP, *__pListIconImage, null, null);
584
585         if (pageTitle.CompareTo(L"") != 0)
586         {
587                 pItem->AddElement(pagetTitleRect, IDA_FORMAT_TITLE_STRING, pageTitle, __fontSize, CUSTOM_COLOR_TRANSPARENT, CUSTOM_COLOR_TRANSPARENT, CUSTOM_COLOR_TRANSPARENT, true);
588         }
589
590         if (pageURL.CompareTo(L"") != 0)
591         {
592                 pItem->AddElement(pageURLRect, IDA_FORMAT_URL_STRING, pageURL, 32, CUSTOM_COLOR_GREY, CUSTOM_COLOR_GREY, CUSTOM_COLOR_GREY, true);
593         }
594
595         if (pAllWindowList)
596         {
597                 if (pAllWindowList->GetCount() > 1)
598                 {
599                         pItem->AddElement(deleteImageRect, IDA_FORMAT_DELETE_BITMAP, *__pListDeleteImage, null, null);
600                         pItem->SetElementSelectionEnabled(IDA_FORMAT_DELETE_BITMAP, true);                      
601                 }
602         }
603         return pItem;
604 }
605
606 bool
607 MultipleWindowForm::DeleteItem(int index, ListItemBase* pItem, int itemWidth)
608 {
609         delete pItem;
610         pItem = null;
611         return true;
612 }
613
614 int
615 MultipleWindowForm::GetItemCount(void)
616 {
617         ArrayList* pAllWindowList = MultipleWindowPresentationModel::GetInstance()->GetAllWindowArrayList();
618         int windowCount = 0;
619
620         if (pAllWindowList != null)
621         {
622                 windowCount = pAllWindowList->GetCount();
623         }
624         if (windowCount < 2)
625         {
626                 GetFooter()->SetItemEnabled(0, false);
627         }
628         else
629         {
630                 GetFooter()->SetItemEnabled(0, true);
631         }
632         GetFooter()->Invalidate(true);
633         return windowCount;
634 }
635
636 void
637 MultipleWindowForm::OnOrientationChanged(const Control& source, OrientationStatus orientationStatus)
638 {
639         if (__pList)
640         {
641                 __pList->UpdateList();
642         }
643 }