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