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