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