Flora license update
[apps/osp/Home.git] / src / HmHomeForm.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 /**
18  * @file        HmHomeForm.cpp
19  * @brief       Keeps implementation of Main Form of the application
20  */
21
22 #include <FApp.h>
23 #include <FIo.h>
24 #include <FSystem.h>
25 #include <FUiAnimations.h>
26 #include "HmApplicationUtils.h"
27 #include "HmHomeForm.h"
28 #include "HmHomeItemInfo.h"
29 #include "HmProgressPanel.h"
30 #include "HmTypes.h"
31
32 using namespace Tizen::App;
33 using namespace Tizen::Base;
34 using namespace Tizen::Base::Collection;
35 using namespace Tizen::Graphics;
36 using namespace Tizen::System;
37 using namespace Tizen::Ui;
38 using namespace Tizen::Ui::Animations;
39 using namespace Tizen::Ui::Controls;
40
41 HomeForm::HomeForm(void)
42         : __isEditEnabled(false)
43         , __isLanguageChanged(false)
44         , __pPageControl(null)
45         , __pPageMarker(null)
46         , __pHomePresentationModel(null)
47         , __pProgressPanel(null)
48         , __pBackgroundBitmap(null)
49         , __pSettingsBitmap(null)
50         , __pSettingsPressedBitmap(null)
51         , __pBackgroundLabel(null)
52         , __pSettingsPanel(null)
53 {
54 }
55
56 HomeForm::~HomeForm(void)
57 {
58         SettingInfo::RemoveSettingEventListener(*this);
59
60         if (__pHomePresentationModel != null)
61         {
62                 __pHomePresentationModel->SetHomePackageEventListener(null);
63                 delete __pHomePresentationModel;
64         }
65
66         if (__pBackgroundBitmap != null)
67         {
68                 delete __pBackgroundBitmap;
69                 __pBackgroundBitmap = null;
70         }
71
72         if (__pSettingsBitmap != null)
73         {
74                 delete __pSettingsBitmap;
75         }
76
77         if (__pSettingsPressedBitmap != null)
78         {
79                 delete __pSettingsPressedBitmap;
80         }
81 }
82
83 bool
84 HomeForm::Initialize(void)
85 {
86         Construct(L"IDL_FORM");
87         return true;
88 }
89
90 result
91 HomeForm::InitializePageControls(HomePresentationModel* pPresentationModel)
92 {
93         result r = E_SUCCESS;
94
95         if (__pHomePresentationModel == null)
96         {
97                 int pageCount = 0;
98                 IconListView* pIconListView = null;
99                 __pHomePresentationModel = pPresentationModel;
100                 __pHomePresentationModel->SetHomePackageEventListener(this);
101
102                 pageCount = __pHomePresentationModel->GetPageCount();
103
104                 while (__pPageControl->GetPageCount() < pageCount)
105                 {
106                         __pPageControl->AddPage();
107                 }
108
109                 for (int iconListCount = 0; iconListCount < __pPageControl->GetIconLists()->GetCount(); iconListCount++)
110                 {
111                         pIconListView = static_cast<IconListView*>(__pPageControl->GetIconLists()->GetAt(iconListCount));
112                         pIconListView->UpdateList();
113                 }
114
115
116                 if (__pProgressPanel != null)
117                 {
118                         //return value ignored as this is just for showing wait cursor and should not disturb application flow
119                         __pProgressPanel->StopAnimation();
120                 }
121
122                 Invalidate(true);
123         }
124         return r;
125 }
126
127 void
128 HomeForm::UpdatePage(ArrayList* pPageList)
129 {
130         ArrayList* pIconLists = __pPageControl->GetIconLists();
131
132         for (int updateCount = 0; updateCount < pPageList->GetCount(); updateCount++)
133         {
134                 IconListView* pIconList = null;
135                 int pageNumber = static_cast<Integer*>(pPageList->GetAt(updateCount))->ToInt();
136
137                 if ((pageNumber) == pIconLists->GetCount() + 1)
138                 {
139                         __pPageControl->AddPage();
140                 }
141
142                 pIconList = static_cast<IconListView*>(pIconLists->GetAt(pageNumber - 1));
143
144                 if (pIconList != null)
145                 {
146                         pIconList->UpdateList();
147                 }
148         }
149
150 }
151
152 result
153 HomeForm::OnInitializing(void)
154 {
155         result r = E_SUCCESS;
156         String wallPaperPath;
157         Rectangle clientRectangle = GetClientAreaBounds();
158         Rectangle markerRect = GetBounds();
159         Rectangle pageRect = GetBounds();
160         Label* pSettingLabel = null;
161         Label* pDoneLabel = null;
162         __pSettingsPanel = static_cast<Panel*>(GetControl(L"IDC_SETTING_PANEL"));
163         __pSettingsPanel->SetBounds(X_SETTING_PANEL, Y_SETTING_PANEL, W_SETTING_PANEL, H_SETTING_PANEL);
164         __pSettingsPanel->SetBackgroundColor(Color(0, 0, 0, 0));
165
166         SetControlAlwaysOnTop(*__pSettingsPanel, true);
167         r = SettingInfo::GetValue(SETTINGS_KEY_WALLPAPER, wallPaperPath);
168
169         if (!wallPaperPath.IsEmpty())
170         {
171                 __pBackgroundBitmap = ApplicationUtils::GetBitmapN(wallPaperPath);
172         }
173         else
174         {
175                 __pBackgroundBitmap = ApplicationUtils::GetResourceBitmapN(IDB_BACKGROUND_IMAGE);
176         }
177
178         SettingInfo::GetValue(SETTINGS_KEY_LANGUAGE, __currentLanguage);
179         r = SettingInfo::AddSettingEventListener(*this);
180
181         if (__pBackgroundBitmap != null)
182         {
183                 clientRectangle.x = clientRectangle.y = 0;
184                 __pBackgroundLabel = static_cast<Label*>(GetControl(L"IDC_BACKGROUND_LABEL"));
185                 __pBackgroundLabel->SetBounds(clientRectangle);
186                 __pBackgroundLabel->SetBackgroundBitmap(*__pBackgroundBitmap);
187         }
188
189         pDoneLabel = static_cast<Label*>(__pSettingsPanel->GetControl(L"IDC_DONE_LABEL"));
190         pDoneLabel->SetShowState(false);
191         pDoneLabel->AddTouchEventListener(*this);
192         pSettingLabel = static_cast<Label*>(__pSettingsPanel->GetControl(L"IDC_SETTING_LABEL"));
193         pSettingLabel->AddTouchEventListener(*this);
194         __pSettingsPanel->SetControlAlwaysOnTop(*pSettingLabel, true);
195
196         clientRectangle.x = clientRectangle.y = 0;
197         __pProgressPanel = new (std::nothrow) ProgressPanel();
198         r = __pProgressPanel->Construct(clientRectangle);
199         r = AddControl(__pProgressPanel);
200         //return value egnored as this is just for showing wait curser and should not disturb app flow
201         __pProgressPanel->StartAnimation();
202
203         clientRectangle = __pSettingsPanel->GetBounds();
204         markerRect = GetClientAreaBounds();
205         markerRect.height = H_PAGE_MARKER;
206         markerRect.y = 0;
207         markerRect.x = X_MARKER_POSITION; //rect.x + rect.width + 2;
208         markerRect.width -= (2 * W_MARKER_OFFSET);
209         __pPageMarker = new (std::nothrow) CustomPageMarker();
210         r = __pPageMarker->Construct(markerRect, 1);
211         TryCatch(r == E_SUCCESS, delete __pPageMarker;
212                          __pPageMarker = null, "__pPageMarker->Construct failed with error %s", GetErrorMessage(r));
213         __pPageMarker->SetSelectedBubble(1);
214         __pPageMarker->SetPageMarkerEventListener(this);
215         r = AddControl(__pPageMarker);
216         TryCatch(r == E_SUCCESS, delete __pPageMarker;
217                          __pPageMarker = null, "AddControl failed");
218
219         pageRect = GetClientAreaBounds();
220         pageRect.y = markerRect.height;
221         pageRect.x = 0;
222         pageRect.height -= markerRect.height;
223
224         __pPageControl = new (std::nothrow) CustomPageControl();
225         r = __pPageControl->Construct(pageRect, 1, pageRect.width);
226         TryCatch(r == E_SUCCESS, delete __pPageControl;
227                          __pPageControl = null, "__pPageMarker->Construct failed with error %s", GetErrorMessage(r));
228         __pPageControl->SetMaxNumberOfIcons(MAX_ICONS_PER_PAGE);
229         __pPageControl->SetPageControlEventListener(this);
230         __pPageControl->SetPageControlItemProvider(this);
231         r = AddControl(__pPageControl);
232         TryCatch(r == E_SUCCESS, delete __pPageControl;
233                          __pPageControl = null, "AddControl failed");
234
235         return r;
236
237 CATCH:
238
239         if (__pPageControl != null)
240         {
241                 delete __pPageControl;
242                 __pPageControl = null;
243         }
244
245         if (__pPageMarker != null)
246         {
247                 delete __pPageMarker;
248                 __pPageMarker = null;
249         }
250
251         return r;
252 }
253
254 result
255 HomeForm::OnTerminating(void)
256 {
257         __pHomePresentationModel->UpdatePageData();
258         return E_SUCCESS;
259 }
260
261 void
262 HomeForm::OnSettingChanged(String& key)
263 {
264
265         if (key.CompareTo(SETTINGS_KEY_WALLPAPER) == 0)
266         {
267                 result r = E_SUCCESS;
268                 String wallPaperPath;
269
270                 r = SettingInfo::GetValue(SETTINGS_KEY_WALLPAPER, wallPaperPath);
271
272                 if (!wallPaperPath.IsEmpty() && r == E_SUCCESS)
273                 {
274                         if (__pBackgroundBitmap != null)
275                         {
276                                 delete __pBackgroundBitmap;
277                                 __pBackgroundBitmap = null;
278                         }
279
280                         __pBackgroundBitmap = ApplicationUtils::GetBitmapN(wallPaperPath);
281
282                         if (__pBackgroundBitmap != null)
283                         {
284                                 __pBackgroundLabel->SetBackgroundBitmap(*__pBackgroundBitmap);
285                                 __pBackgroundLabel->Invalidate(false);
286                         }
287                 }
288         }
289         else if (key.CompareTo(SETTINGS_KEY_LANGUAGE) == 0)
290         {
291                 String selectedLang;
292                 SettingInfo::GetValue(SETTINGS_KEY_LANGUAGE, selectedLang);
293
294                 if (selectedLang.CompareTo(__currentLanguage) != 0)
295                 {
296                         __currentLanguage = selectedLang;
297                         __isLanguageChanged = true;
298                         __pHomePresentationModel->LanguageChanged();
299
300                         if (UiApp::GetInstance()->GetAppUiState() != APP_UI_STATE_BACKGROUND)
301                         {
302                                 ArrayList* pIconLists = null;
303                                 Label* pDoneLabel = static_cast<Label*>(__pSettingsPanel->GetControl(L"IDC_DONE_LABEL"));
304
305                                 if (pDoneLabel != null)
306                                 {
307                                         String doneString;
308                                         ApplicationUtils::GetStringResource(L"IDS_COM_POP_DONE", doneString);
309                                         pDoneLabel->SetText(doneString);
310                                         pDoneLabel->Invalidate(false);
311                                 }
312
313                                 pIconLists = __pPageControl->GetIconLists();
314
315                                 if (pIconLists != null)
316                                 {
317                                         for (int iconListCount = 0; iconListCount < pIconLists->GetCount(); iconListCount++)
318                                         {
319                                                 IconListView* pIconListView = static_cast<IconListView*>(pIconLists->GetAt(iconListCount));
320                                                 if (pIconListView)
321                                                 {
322                                                         pIconListView->UpdateList();
323                                                 }
324                                         }
325                                 }
326
327                         }
328                 }
329         }
330
331         return;
332 }
333
334 void
335 HomeForm::OnTouchReleased(const Control& source, const Point& currentPosition, const TouchEventInfo& touchInfo)
336 {
337         if (source.GetName().CompareTo(L"IDC_SETTING_LABEL") == 0
338                 || source.GetName().CompareTo(L"IDC_DONE_LABEL") == 0)
339         {
340                 Label* pDoneLabel = null;
341                 Label* pSettingLabel = null;
342                 ControlAnimator* pDoneAnimator = null;
343                 ControlAnimator* pSettingLabelAnimator = null;
344                 Bitmap* pBitmap = null;
345                 Point settingPosition(0, 0);
346
347                 __isEditEnabled = !__isEditEnabled;
348                 __pPageControl->ToggleEditMode();
349                 pDoneLabel = static_cast<Label*>(__pSettingsPanel->GetControl(L"IDC_DONE_LABEL"));
350                 pDoneAnimator = pDoneLabel->GetControlAnimator();
351                 pDoneAnimator->SetShowState(__isEditEnabled);
352                 pSettingLabel = static_cast<Label*>(__pSettingsPanel->GetControl(L"IDC_SETTING_LABEL"));
353                 pSettingLabelAnimator = pSettingLabel->GetControlAnimator();
354                 settingPosition = pSettingLabel->GetPosition();
355
356                 if (__isEditEnabled)
357                 {
358                         settingPosition.x = 0;
359                 }
360                 else
361                 {
362                         settingPosition.x = X_SETTING_PANEL_PRESSED_OFFSET;
363                 }
364
365                 if (__isEditEnabled)
366                 {
367                         Canvas* pCanvas = null;
368                         Rectangle settingRectangle = __pSettingsPanel->GetBounds();
369                         int pointOffset = 0;
370                         int sizeOffset = SIZE_SETTING_LABEL_OFFSET;
371
372                         if (__pSettingsPressedBitmap == null)
373                         {
374                                 __pSettingsPressedBitmap = ApplicationUtils::GetResourceBitmapN(IDB_SETTING_BUTTON_PRESSED);
375                         }
376                         pBitmap = __pSettingsPressedBitmap;
377
378                         settingRectangle.x = pointOffset;
379                         settingRectangle.y = pointOffset;
380                         settingRectangle.width -= sizeOffset;
381                         settingRectangle.height -= sizeOffset;
382
383                         pCanvas = __pSettingsPanel->GetCanvasN();
384
385                         if (pCanvas != null)
386                         {
387                                 pCanvas->SetBackgroundColor(Color(0, 0, 0, 0));
388                                 pCanvas->Clear();
389                                 pCanvas->SetBackgroundColor(Color::GetColor(COLOR_ID_BLACK));
390                                 pCanvas->SetLineWidth(1);
391                                 pCanvas->SetForegroundColor(Color::GetColor(COLOR_ID_BLACK));
392                                 pCanvas->DrawRectangle(settingRectangle);
393                                 pCanvas->SetForegroundColor(COLOR_SETTING_BOUNDARY_INNER);
394
395                                 pointOffset = sizeOffset;
396                                 sizeOffset = 2 * SIZE_SETTING_LABEL_OFFSET;
397                                 settingRectangle.x = pointOffset;
398                                 settingRectangle.y = pointOffset;
399                                 settingRectangle.width -= sizeOffset;
400                                 settingRectangle.height -= sizeOffset;
401                                 pCanvas->DrawRectangle(settingRectangle);
402
403                                 pointOffset = sizeOffset;
404                                 settingRectangle.x = pointOffset;
405                                 settingRectangle.y = pointOffset;
406                                 settingRectangle.width -= sizeOffset;
407                                 settingRectangle.height -= sizeOffset;
408                                 pCanvas->FillRectangle(Color::GetColor(COLOR_ID_BLACK), settingRectangle);
409                                 delete pCanvas;
410                         }
411                 }
412                 else
413                 {
414                         Canvas* pCanvas = null;
415
416                         if (__pSettingsBitmap == null)
417                         {
418                                 __pSettingsBitmap = ApplicationUtils::GetResourceBitmapN(IDB_SETTING_BUTTON_PRESSED);
419                         }
420
421                         pBitmap = __pSettingsBitmap;
422                         pCanvas = __pSettingsPanel->GetCanvasN();
423
424                         if (pCanvas != null)
425                         {
426                                 pCanvas->SetBackgroundColor(Color(0, 0, 0, 0));
427                                 pCanvas->Clear();
428                                 delete pCanvas;
429                         }
430                 }
431
432                 pSettingLabel->SetBackgroundBitmap(*pBitmap);
433                 pSettingLabel->Invalidate(false);
434                 pSettingLabelAnimator->SetPosition(settingPosition);
435                 settingPosition.x += pSettingLabel->GetSize().width;
436                 pDoneAnimator->SetPosition(settingPosition);
437         }
438 }
439
440 void
441 HomeForm::OnPageSwept(int pageNumber)
442 {
443         if (pageNumber > 0)
444         {
445                 __pPageMarker->SetSelectedBubble(pageNumber);
446         }
447
448         return;
449 }
450
451 void
452 HomeForm::OnPageAdded(int pageNumber)
453 {
454         // adds a new bubble and sets the newly added page as the current page
455         if (__pPageMarker != null)
456         {
457                 __pPageMarker->AddBubble();
458         }
459
460         return;
461 }
462
463 void
464 HomeForm::OnPageRemoved(int pageNumber)
465 {
466         if (__pPageMarker != null)
467         {
468                 __pPageMarker->RemoveBubble(pageNumber);
469         }
470
471         return;
472 }
473
474 void
475 HomeForm::OnMarkerBubbleMoved(int bubbleNumber)
476 {
477         if (bubbleNumber > 0)
478         {
479                 __pPageMarker->SetSelectedBubble(bubbleNumber);
480                 __pPageControl->SetPageSelected(bubbleNumber);
481         }
482
483         return;
484 }
485
486 void
487 HomeForm::OnInstallationCompleted(const HomeItemInfo& pItemInfo)
488 {
489         ArrayList* pIconViews = null;
490         int pageNumber = 0;
491         int position = 0;
492
493         if (__pHomePresentationModel->GetPageCount() > __pPageControl->GetPageCount())
494         {
495                 //__pPageControl->SetAppInfoList(__pDataModel->GetApplicationsList());
496
497                 if (IsFailed(__pPageControl->AddPage()))
498                 {
499                         return;
500                 }
501         }
502
503         pIconViews = __pPageControl->GetIconLists();
504
505         if (pIconViews != null)
506         {
507                 IconListView* pListView = null;
508                 pItemInfo.GetPositionInHomeScreen(pageNumber, position);
509                 pListView = static_cast<IconListView*>(pIconViews->GetAt(pageNumber - 1));
510
511                 if (pListView != null)
512                 {
513                         pListView->UpdateList();
514                 }
515         }
516
517         return;
518 }
519
520 void
521 HomeForm::OnAppUninstallRequested(HomeItemInfo* pItemInfo)
522 {
523         if (__pProgressPanel != null)
524         {
525                 __pProgressPanel->StartAnimation();
526         }
527
528         return;
529 }
530
531 void
532 HomeForm::OnUnInstallationCompleted(const HomeItemInfo& pItemInfo)
533 {
534         if (__pHomePresentationModel != null)
535         {
536                 int pageNumber = 0;
537                 int appIndex = 0;
538                 IconListView* pIconList = null;
539                 ArrayList* pIconLists = __pPageControl->GetIconLists();
540
541                 if (__pProgressPanel != null)
542                 {
543                         __pProgressPanel->StopAnimation();
544                 }
545
546                 pItemInfo.GetPositionInHomeScreen(pageNumber, appIndex);
547                 pIconList = static_cast<IconListView*>(pIconLists->GetAt(pageNumber - 1));
548
549                 if (pIconList != null)
550                 {
551                         pIconList->UpdateList();
552                 }
553         }
554
555         return;
556 }
557
558 void
559 HomeForm::OnUpdatePageRequestedN(Tizen::Base::Collection::ArrayList* pPageList)
560 {
561         if (pPageList != null)
562         {
563                 UpdatePage(pPageList);
564                 pPageList->RemoveAll(true);
565                 delete pPageList;
566                 pPageList = null;
567         }
568         return;
569 }
570
571 // from IPageControlItemProvider
572 int
573 HomeForm::GetPageCount(void)
574 {
575         int pageCount = 0;
576
577         if (__pHomePresentationModel != null)
578         {
579                 pageCount = __pHomePresentationModel->GetPageCount();
580         }
581         return pageCount;
582 }
583
584 int
585 HomeForm::GetItemCount(int pageNumber)
586 {
587         int itemCount = 0;
588
589         if (__pHomePresentationModel != null)
590         {
591                 itemCount = __pHomePresentationModel->GetItemCount(pageNumber);
592
593 //              if(itemCount == 0)
594 //              {
595 //                      if(pageNumber > 1)
596 //                      {
597 //                              __pPageControl->MarkPageToDelete(pageNumber);
598 //                      }
599 //              }
600         }
601
602         return itemCount;
603 }
604
605 HomeItemInfo*
606 HomeForm::GetItem(int pageNumber, int index)
607 {
608         HomeItemInfo* itemInfo = null;
609
610         if (__pHomePresentationModel != null)
611         {
612                 itemInfo = __pHomePresentationModel->GetItem(pageNumber, index);
613         }
614
615         return itemInfo;
616 }
617
618 void
619 HomeForm::RearrangeItems(HomeItemInfo* pMovedAppInfo, int newPageNumber, int newIndex)
620 {
621         __pHomePresentationModel->RearrangeItems(pMovedAppInfo, newPageNumber, newIndex);
622         return;
623 }
624
625 void
626 HomeForm::UpdateAllPages(void)
627 {
628         if (__isLanguageChanged)
629         {
630                 Label* pDoneLabel = static_cast<Label*>(__pSettingsPanel->GetControl(L"IDC_DONE_LABEL"));
631
632                 if (pDoneLabel != null)
633                 {
634                         String doneString;
635                         ApplicationUtils::GetStringResource(L"IDS_COM_POP_DONE", doneString);
636                         pDoneLabel->SetText(doneString);
637                         pDoneLabel->Invalidate(false);
638                 }
639
640                 if (__pPageControl != null)
641                 {
642                         ArrayList* pIconLists = __pPageControl->GetIconLists();
643
644                         if (pIconLists != null)
645                         {
646                                 for (int iconListCount = 0; iconListCount < pIconLists->GetCount(); iconListCount++)
647                                 {
648                                         IconListView* pIconListView = static_cast<IconListView*>(pIconLists->GetAt(iconListCount));
649                                         if (pIconListView)
650                                         {
651                                                 pIconListView->UpdateList();
652                                         }
653                                 }
654                         }
655                 }
656
657                 __isLanguageChanged = false;
658         }
659 }