Fixed jira issue and fixed prevent issue
[apps/osp/MusicPlayer.git] / src / MpSettingForm.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 /**
18  * @file                MpSettingForm.cpp
19  * @brief               This is the implementation file for SettingForm class.
20  */
21
22 #include <FApp.h>
23 #include <FBase.h>
24 #include <FUi.h>
25 #include <FUiScenes.h>
26 #include "MpAllListPresentationModel.h"
27 #include "MpNowPlayContentPanel.h"
28 #include "MpSceneRegister.h"
29 #include "MpSettingForm.h"
30 #include "MpSettingPresentationModel.h"
31 #include "MpTypes.h"
32
33 using namespace Tizen::App;
34 using namespace Tizen::Base;
35 using namespace Tizen::Graphics;
36 using namespace Tizen::Ui;
37 using namespace Tizen::Ui::Controls;
38 using namespace Tizen::Ui::Scenes;
39
40 static const int SETTING_ITEM_HEIGHT = 112;
41
42 static const int ID_MAIN_DEFAULT_PLAYLIST_ITEM = 1;
43 static const int ID_SUB_MOST_PLAYED_ITEM = 0;
44 static const int ID_SUB_RECENTLY_PLAYED_ITEM = 1;
45 static const int ID_SUB_RECENTLY_ADD_ITEM = 2;
46
47 static const int ID_MAIN_AUTO_OFF_ITEM = 0;
48 static const int ID_SUB_ZERO_ITEM = 0;
49 static const int ID_SUB_FIFTEEN_ITEM = 1;
50 static const int ID_SUB_THIRTY_ITEM = 2;
51 static const int ID_SUB_ONE_HOUR_ITEM = 3;
52 static const int ID_SUB_ONE_HOUR_THIRTY_ITEM = 4;
53 static const int ID_SUB_TWO_HOURS_ITEM = 5;
54
55 static const int ID_MAIN_DEFAULT_PLAYLIST_ITEM_COUNT = 3;
56 static const int ID_MAIN_AUTO_OFF_ITEM_COUNT = 6;
57
58 SettingForm::SettingForm(void)
59         : __pSettingPresentationModel(null)
60         , __pSettingTableView(null)
61 {
62         AppLogDebug("ENTER");
63         AppLogDebug("EXIT");
64 }
65
66 SettingForm::~SettingForm(void)
67 {
68         AppLogDebug("ENTER");
69         AppLogDebug("EXIT");
70 }
71
72 void
73 SettingForm::Initialize(void)
74 {
75         AppLogDebug("ENTER");
76         Construct(IDL_SETTING_FORM);
77         AppLogDebug("EXIT");
78 }
79
80 result
81 SettingForm::OnInitializing(void)
82 {
83         AppLogDebug("ENTER");
84         result r = E_SUCCESS;
85
86         Footer* pFooter = null;
87         pFooter = GetFooter();
88         pFooter->AddActionEventListener(*this);
89         SetFormBackEventListener(this);
90
91         __pSettingTableView = static_cast<GroupedTableView*>(GetControl(IDC_GROUPEDTABLEVIEW_CONTENT_LIST));
92         __pSettingTableView->SetItemProvider(this);
93         __pSettingTableView->AddGroupedTableViewItemEventListener(*this);
94
95         __pSettingPresentationModel = SettingPresentationModel::GetInstance();
96         __pSettingPresentationModel->InitializeSettingValue();
97         __pPresentationModel = AllListPresentationModel::GetInstance();
98         __pSettingTableView->UpdateTableView();
99         __pSettingTableView->CollapseAllGroup();
100 //      for (int itemIndex = 0; itemIndex < ID_MAIN_DEFAULT_PLAYLIST_ITEM_COUNT; itemIndex++)
101 //      {
102 //              __pSettingTableView->SetItemChecked(0, itemIndex, __pSettingPresentationModel->IsTabEnable(itemIndex));
103 //      }
104         __pSettingTableView->SetItemChecked(ID_MAIN_AUTO_OFF_ITEM, __pSettingPresentationModel->GetAutoOffTime(), true);
105
106         Invalidate(true);
107         AppLogDebug("EXIT");
108         return r;
109 }
110
111 result
112 SettingForm::OnTerminating(void)
113 {
114         AppLogDebug("ENTER");
115         result r = E_SUCCESS;
116
117         /*Frame* pFrame = Application::GetInstance()->GetAppFrame()->GetFrame();
118         Form* pForm = static_cast<Form*>(pFrame->GetControl(CONTENT_FORM_REQUSEST_ID));
119         if (pForm != null)
120         {
121                 AppLogDebug("AForm");
122                 pForm->SendUserEvent(0, null);
123         }*/
124 //      __pSettingPresentationModel->DestroySettingPresentationModel();
125
126         AppLogDebug("EXIT");
127         return r;
128 }
129
130 int
131 SettingForm::GetGroupCount(void)
132 {
133         AppLogDebug("ENTER");
134         AppLogDebug("EXIT");
135         return 1;
136 }
137
138 int
139 SettingForm::GetItemCount(int groupIndex)
140 {
141         AppLogDebug("ENTER");
142         int itemCount = 0;
143
144         switch (groupIndex)
145         {
146         case ID_MAIN_DEFAULT_PLAYLIST_ITEM:
147                 {
148                         itemCount = ID_MAIN_DEFAULT_PLAYLIST_ITEM_COUNT;
149                 }
150                 break;
151
152         case ID_MAIN_AUTO_OFF_ITEM:
153                 {
154                         itemCount = ID_MAIN_AUTO_OFF_ITEM_COUNT;
155                 }
156                 break;
157
158         default:
159                 break;
160         }
161         AppLogDebug("EXIT");
162         return itemCount;
163 }
164
165 Tizen::Ui::Controls::TableViewGroupItem*
166 SettingForm::CreateGroupItem(int groupIndex, int itemWidth)
167 {
168         AppLogDebug("ENTER %d", groupIndex);
169         RelativeLayout layout;
170         layout.Construct();
171
172         TableViewGroupItem* pItem = new (std::nothrow) TableViewGroupItem();
173         pItem->Construct(layout, Dimension(itemWidth, SETTING_ITEM_HEIGHT));
174         Panel* pGroupTableViewItem =  new (std::nothrow) Panel();
175         switch (groupIndex)
176         {
177         case ID_MAIN_DEFAULT_PLAYLIST_ITEM:
178                 {
179                         if (IsFailed(pGroupTableViewItem->Construct(IDL_SETTING_PLAYLIST_SET_ITEM_PANEL)))
180                         {
181                                 AppLogDebug("Construct(IDL_SETTING_PLAYLIST_SET_ITEM_PANEL) failed(%s)", GetErrorMessage(GetLastResult()));
182                                 delete pGroupTableViewItem;
183                                 delete pItem;
184                                 return null;
185                         }
186                 }
187                 break;
188
189         case ID_MAIN_AUTO_OFF_ITEM:
190                 {
191                         if (IsFailed(pGroupTableViewItem->Construct(IDL_SETTING_OFF_SET_ITEM_PANEL)))
192                         {
193                                 AppLogDebug("Construct(IDL_SETTING_PLAYLIST_SET_ITEM_PANEL) failed(%s)", GetErrorMessage(GetLastResult()));
194                                 delete pGroupTableViewItem;
195                                 delete pItem;
196                                 return null;
197                         }
198                         Label* pSubText = static_cast<Label*>(pGroupTableViewItem->GetControl(IDC_LABEL_SUB_TEXT));
199                         pSubText->SetText(GetAutoOffString());
200                 }
201                 break;
202
203         default:
204                 break;
205         }
206
207         pItem->AddControl(*pGroupTableViewItem);
208         pItem->SetIndividualSelectionEnabled(pGroupTableViewItem, true);
209         CommonUtil::SetLayoutFitToContainer(*pItem, *pGroupTableViewItem);
210
211         AppLogDebug("EXIT");
212         return pItem;
213 }
214
215 bool
216 SettingForm::DeleteGroupItem(int groupIndex, TableViewGroupItem* pItem)
217 {
218         AppLogDebug("ENTER");
219         delete pItem;
220         AppLogDebug("EXIT");
221         return true;
222 }
223
224 Tizen::Ui::Controls::TableViewItem*
225 SettingForm::CreateItem(int groupIndex, int itemIndex, int itemWidth)
226 {
227         AppLogDebug("ENTER");
228         RelativeLayout layout;
229         layout.Construct();
230
231         TableViewItem* pItem = new (std::nothrow) TableViewItem();
232         Panel* pGroupTableViewItem =  new (std::nothrow) Panel();
233
234         result r = pGroupTableViewItem->Construct(IDL_GROUPEDTABLEVIEW_ITEM_PANEL);
235         TryCatch(r == E_SUCCESS, delete pItem, "pGroupTableViewItem->Construct(%s)", GetErrorMessage(r));
236
237         if (groupIndex == ID_MAIN_DEFAULT_PLAYLIST_ITEM)
238         {
239                 r = pItem->Construct(layout, Tizen::Graphics::Dimension(W_CLIENT_AREA, SETTING_ITEM_HEIGHT), TABLE_VIEW_ANNEX_STYLE_MARK);
240                 TryCatch(r == E_SUCCESS, delete pItem, "pItem->Construct(%s)", GetErrorMessage(r));
241
242                 Label* pMainText = static_cast<Label*>(pGroupTableViewItem->GetControl(IDC_LABEL_MAIN_TEXT));
243
244                 switch (itemIndex)
245                 {
246                 case ID_SUB_MOST_PLAYED_ITEM:
247                         {
248                                 pMainText->SetText(ResourceManager::GetString(L"IDS_MUSIC_BODY_MOST_PLAYED"));
249                         }
250                         break;
251
252                 case ID_SUB_RECENTLY_PLAYED_ITEM:
253                         {
254                                 pMainText->SetText(ResourceManager::GetString(L"IDS_MUSIC_BODY_PLAYLIST_RECENTLY_PLAYED"));
255                         }
256                         break;
257
258                 case ID_SUB_RECENTLY_ADD_ITEM:
259                         {
260                                 pMainText->SetText(ResourceManager::GetString(L"IDS_MUSIC_BODY_RECENTLY_ADDED"));
261                         }
262                         break;
263
264                 default:
265                         break;
266                 }
267         }
268         else
269         {
270                 r = pItem->Construct(layout, Tizen::Graphics::Dimension(W_CLIENT_AREA, SETTING_ITEM_HEIGHT), TABLE_VIEW_ANNEX_STYLE_RADIO);
271                 TryCatch(r == E_SUCCESS, delete pItem, "pItem->Construct(%s)", GetErrorMessage(r));
272
273                 Label* pMainText = static_cast<Label*>(pGroupTableViewItem->GetControl(IDC_LABEL_MAIN_TEXT));
274
275                 switch (itemIndex)
276                 {
277                 case ID_SUB_ZERO_ITEM:
278                         {
279                                 pMainText->SetText(ResourceManager::GetString(L"IDS_MUSIC_BODY_NOT_USED"));
280                         }
281                         break;
282
283                 case ID_SUB_FIFTEEN_ITEM:
284                         {
285                                 pMainText->SetText(ResourceManager::GetString(L"IDS_MUSIC_OPT_AFTER_15_MIN_ABB"));
286                         }
287                         break;
288
289                 case ID_SUB_THIRTY_ITEM:
290                         {
291                                 pMainText->SetText(ResourceManager::GetString(L"IDS_MUSIC_BODY_AFTER_30_MIN"));
292                         }
293                         break;
294
295                 case ID_SUB_ONE_HOUR_ITEM:
296                         {
297                                 pMainText->SetText(ResourceManager::GetString(L"IDS_MUSIC_BODY_AFTER_1_HOUR"));
298                         }
299                         break;
300
301                 case ID_SUB_ONE_HOUR_THIRTY_ITEM:
302                         {
303                                 pMainText->SetText(ResourceManager::GetString(L"IDS_MUSIC_POP_AFTER_1_HOUR_30_MIN"));
304                         }
305                         break;
306
307                 case ID_SUB_TWO_HOURS_ITEM:
308                         {
309                                 pMainText->SetText(ResourceManager::GetString(L"IDS_MUSIC_BODY_AFTER_2_HOURS"));
310                         }
311                         break;
312
313                 default:
314                         break;
315                 }
316         }
317
318         pItem->AddControl(*pGroupTableViewItem);
319         pItem->SetIndividualSelectionEnabled(pGroupTableViewItem, true);
320         CommonUtil::SetLayoutFitToContainer(*pItem, *pGroupTableViewItem);
321
322         AppLogDebug("EXIT");
323         return pItem;
324
325 CATCH:
326         AppLogDebug("EXIT(%ls)", GetErrorMessage(GetLastResult()));
327         delete pGroupTableViewItem;
328         return null;
329 }
330
331 void
332 SettingForm::UpdateItem(int groupIndex, int itemIndex, Tizen::Ui::Controls::TableViewItem* pItem)
333 {
334         AppLogDebug("ENTER");
335         AppLogDebug("EXIT");
336 }
337
338 void
339 SettingForm::UpdateGroupItem(int groupIndex, Tizen::Ui::Controls::TableViewGroupItem* pItem)
340 {
341         AppLogDebug("ENTER");
342         if (groupIndex == ID_MAIN_AUTO_OFF_ITEM)
343         {
344                 Label* pLabelSubtext = static_cast<Label*>(pItem->GetControl(IDC_LABEL_SUB_TEXT, true));
345                 pLabelSubtext->SetText(GetAutoOffString());
346                 pLabelSubtext->Invalidate(false);
347         }
348         __pSettingTableView->Invalidate(true);
349         AppLogDebug("EXIT");
350 }
351
352 int
353 SettingForm::GetDefaultItemHeight(void)
354 {
355         AppLogDebug("ENTER");
356         AppLogDebug("EXIT");
357         return SETTING_ITEM_HEIGHT;
358 }
359
360 int
361 SettingForm::GetDefaultGroupItemHeight(void)
362 {
363         AppLogDebug("ENTER");
364         AppLogDebug("EXIT");
365         return SETTING_ITEM_HEIGHT;
366 }
367
368 bool
369 SettingForm::DeleteItem(int groupIndex, int itemIndex, TableViewItem* pItem)
370 {
371         AppLogDebug("ENTER");
372         delete pItem;
373         AppLogDebug("EXIT");
374         return true;
375 }
376
377 void
378 SettingForm::OnGroupedTableViewGroupItemStateChanged(Tizen::Ui::Controls::GroupedTableView& tableView, int groupIndex, Tizen::Ui::Controls::TableViewGroupItem* pItem, Tizen::Ui::Controls::TableViewItemStatus status)
379 {
380         AppLogDebug("ENTER");
381         if (tableView.IsGroupExpanded(groupIndex) == true)
382         {
383                 Label* pLabel = static_cast<Label*>(pItem->GetControl(IDC_LABEL_EX, true));
384                 Bitmap* pBitmap = ResourceManager::GetBitmapN(L"00_button_expand_closed.png");
385                 pLabel->SetBackgroundBitmap(*pBitmap);
386                 pLabel->Invalidate(false);
387                 delete pBitmap;
388
389                 if (groupIndex == ID_MAIN_AUTO_OFF_ITEM)
390                 {
391                         Label* pLabelSubtext = static_cast<Label*>(pItem->GetControl(IDC_LABEL_SUB_TEXT, true));
392                         pLabelSubtext->SetText(GetAutoOffString());
393                         pLabelSubtext->Invalidate(false);
394                 }
395
396                 AppLogDebug("CollapseGroup %d",groupIndex);
397                 tableView.CollapseGroup(groupIndex);
398         }
399         else
400         {
401                 Label* pLabel = static_cast<Label*>(pItem->GetControl(IDC_LABEL_EX, true));
402                 Bitmap* pBitmap = ResourceManager::GetBitmapN(L"00_button_expand_opened.png");
403                 pLabel->SetBackgroundBitmap(*pBitmap);
404                 pLabel->Invalidate(false);
405                 delete pBitmap;
406
407                 AppLogDebug("ExpandGroup %d",groupIndex);
408                 tableView.ExpandGroup(groupIndex);
409         }
410         AppLogDebug("EXIT");
411 }
412
413 void
414 SettingForm::OnGroupedTableViewItemStateChanged(Tizen::Ui::Controls::GroupedTableView& tableView, int groupIndex, int itemIndex, Tizen::Ui::Controls::TableViewItem* pItem, Tizen::Ui::Controls::TableViewItemStatus status)
415 {
416         AppLogDebug("ENTER");
417         bool isChecked = false;
418         switch (groupIndex)
419         {
420         case ID_MAIN_DEFAULT_PLAYLIST_ITEM:
421                 {
422                         if (status == TABLE_VIEW_ITEM_STATUS_CHECKED)
423                         {
424                                 isChecked = true;
425                         }
426                         __pSettingPresentationModel->SetTabEnable(itemIndex, isChecked);
427                         Frame* pFrame = Application::GetInstance()->GetAppFrame()->GetFrame();
428                         Form* pForm = static_cast<Form*>(pFrame->GetControl(CONTENT_FORM_REQUSEST_ID));
429                         if (pForm != null)
430                         {
431                                 AppLogDebug("AForm");
432                                 pForm->SendUserEvent(0, null);
433                         }
434                 }
435                 break;
436
437         case ID_MAIN_AUTO_OFF_ITEM:
438                 {
439                         for (int i = 0; i < __pSettingTableView->GetItemCountAt(ID_MAIN_AUTO_OFF_ITEM); i++)
440                         {
441                                 if (i == itemIndex)
442                                 {
443                                         AppLogDebug("true : %d", i);
444                                         __pSettingTableView->SetItemChecked(groupIndex, i, true);
445                                 }
446                                 else
447                                 {
448                                         AppLogDebug("false : %d", i);
449                                         __pSettingTableView->SetItemChecked(groupIndex, i, false);
450                                 }
451                         }
452                         __pPresentationModel->StartAutoOffTimer(__pSettingPresentationModel->SetAutoOffTime(itemIndex));
453                         tableView.RefreshItem(groupIndex, -1, TABLE_VIEW_REFRESH_TYPE_ITEM_MODIFY);
454                 }
455                 break;
456
457         default:
458                 break;
459         }
460         AppLogDebug("EXIT");
461 }
462
463 void
464 SettingForm::OnGroupedTableViewContextItemActivationStateChanged(Tizen::Ui::Controls::GroupedTableView& tableView, int groupIndex, int itemIndex, Tizen::Ui::Controls::TableViewContextItem* pContextItem, bool activated)
465 {
466         AppLogDebug("ENTER");
467         AppLogDebug("EXIT");
468 }
469
470 void
471 SettingForm::OnActionPerformed(const Tizen::Ui::Control& source, int actionId)
472 {
473         AppLogDebug("ENTER");
474         AppLogDebug("EXIT");
475 }
476
477 void
478 SettingForm::OnFormBackRequested(Tizen::Ui::Controls::Form& source)
479 {
480         AppLogDebug("ENTER");
481         SceneManager* pSceneManager = SceneManager::GetInstance();
482         AppAssert(pSceneManager);
483         result r = pSceneManager->GoBackward(BackwardSceneTransition());
484         if (r != E_SUCCESS)
485         {
486                 __pSettingPresentationModel->DestroySettingPresentationModel();
487                 UiApp* pApp = UiApp::GetInstance();
488                 pApp->Terminate();
489         }
490         AppLogDebug("EXIT");
491 }
492
493 void
494 SettingForm::OnSceneActivatedN(const Tizen::Ui::Scenes::SceneId& previousSceneId,
495                                                                 const Tizen::Ui::Scenes::SceneId& currentSceneId,
496                                                                 Tizen::Base::Collection::IList* pArgs)
497 {
498         AppLogDebug("ENTER");
499         SceneManager* pSceneManager = SceneManager::GetInstance();
500         AppAssert(pSceneManager);
501
502         CommonUtil::SetSimpleTitleStyle(*GetHeader(), ResourceManager::GetString(L"IDS_MUSIC_BODY_MUSIC_SETTINGS"));
503
504         if (pArgs != null)
505         {
506                 pArgs->RemoveAll(true);
507                 delete pArgs;
508         }
509         AppLogDebug("EXIT");
510 }
511
512 void
513 SettingForm::OnSceneDeactivated(const Tizen::Ui::Scenes::SceneId& currentSceneId,
514                                         const Tizen::Ui::Scenes::SceneId& nextSceneId)
515 {
516         AppLogDebug("ENTER");
517         AppLogDebug("EXIT");
518 }
519
520 String
521 SettingForm::GetAutoOffString(void)
522 {
523         AppLogDebug("ENTER");
524         switch (__pSettingPresentationModel->GetAutoOffTime())
525         {
526         case ID_SUB_ZERO_ITEM:
527                 {
528                         return ResourceManager::GetString(L"IDS_MUSIC_BODY_NOT_USED");
529                 }
530                 break;
531
532         case ID_SUB_FIFTEEN_ITEM:
533                 {
534                         return ResourceManager::GetString(L"IDS_MUSIC_OPT_AFTER_15_MIN_ABB");
535                 }
536                 break;
537
538         case ID_SUB_THIRTY_ITEM:
539                 {
540                         return ResourceManager::GetString(L"IDS_MUSIC_BODY_AFTER_30_MIN");
541                 }
542                 break;
543
544         case ID_SUB_ONE_HOUR_ITEM:
545                 {
546                         return ResourceManager::GetString(L"IDS_MUSIC_BODY_AFTER_1_HOUR");
547                 }
548                 break;
549
550         case ID_SUB_ONE_HOUR_THIRTY_ITEM:
551                 {
552                         return ResourceManager::GetString(L"IDS_MUSIC_POP_AFTER_1_HOUR_30_MIN");
553                 }
554                 break;
555
556         case ID_SUB_TWO_HOURS_ITEM:
557                 {
558                         return ResourceManager::GetString(L"IDS_MUSIC_BODY_AFTER_2_HOURS");
559                 }
560                 break;
561
562         default:
563                 break;
564         }
565
566         AppLogDebug("EXIT");
567         return ResourceManager::GetString(L"IDS_MUSIC_BODY_NOT_USED");
568 }