Tizen 2.1 base
[sdk/ide/native-sample.git] / samples / native / partner / cpp / Sample / Tizen C++ / PushClient / PushClient / project / src / SettingForm.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Flora License, Version 1.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.tizenopensource.org/license
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an AS IS BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 #include <FIo.h>
19 #include <FAppApp.h>
20 #include "SettingForm.h"
21 #include "PushClient.h"
22
23 using namespace Osp::App;
24 using namespace Osp::Base;
25 using namespace Osp::Io;
26 using namespace Osp::Graphics;
27 using namespace Osp::Ui::Controls;
28
29 #define PUSH_CLIENT_REGISTRY_FILE                               L"/Home/setting.ini"
30 #define PUSH_CLIENT_REGISTRY_SETTING_SECTION    L"Setting"
31 #define PUSH_CLIENT_REGISTRY_USE_PUSH_ENTRY             L"UsePush"
32
33 SettingForm::SettingForm(void) :
34 __pSettingButton(null)
35 {
36 }
37
38 SettingForm::~SettingForm(void)
39 {
40 }
41
42 bool
43 SettingForm::Initialize(void)
44 {
45         Form::Construct(FORM_STYLE_NORMAL | FORM_STYLE_HEADER | FORM_STYLE_INDICATOR | FORM_STYLE_FOOTER);
46
47         return true;
48 }
49
50 result
51 SettingForm::OnInitializing(void)
52 {
53         result r = E_SUCCESS;
54
55         Header* pHeader = GetHeader();
56         if (pHeader)
57         {
58                 pHeader->SetStyle(HEADER_STYLE_TITLE);
59                 pHeader->SetName(L"SettingForm");
60                 pHeader->SetTitleText(L"PushClient - Setting");
61         }
62
63         Footer* pFooter = GetFooter();
64         if (pFooter)
65         {
66                 pFooter->SetStyle(FOOTER_STYLE_TAB);
67
68                 FooterItem footerItem1;
69                 footerItem1.Construct(ID_FOOTER_MAIN);
70                 footerItem1.SetText(L"Main");
71                 pFooter->AddItem(footerItem1);
72                 pFooter->AddActionEventListener(*this);
73         }
74
75         __pSettingButton = new CheckButton();
76         __pSettingButton->Construct(Rectangle(20, 20, 440, 100), CHECK_BUTTON_STYLE_ONOFF_SLIDING, BACKGROUND_STYLE_DEFAULT, false, L"Use Push Notification");
77         __pSettingButton->SetActionId(ID_BUTTON_CHECKED, ID_BUTTON_UNCHECKED, ID_BUTTON_SELECTED);
78         __pSettingButton->AddActionEventListener(*this);
79
80         AddControl(*__pSettingButton);
81
82         CheckRegistry();
83
84         return r;
85 }
86
87 result
88 SettingForm::OnTerminating(void)
89 {
90         result r = E_SUCCESS;
91
92         return r;
93 }
94
95 void
96 SettingForm::OnActionPerformed(const Osp::Ui::Control& source, int actionId)
97 {
98         AppLog("####### OnActionPerformed called with ActionId(%d)! #######", actionId);
99
100         switch(actionId)
101         {
102         case ID_FOOTER_MAIN:
103                 {
104                         AppLog("####### Switch to MainForm. #######");
105
106                         PushClient* pApp = static_cast<PushClient*>(Application::GetInstance());
107                         pApp->ShowMainForm();
108                 }
109                 break;
110         case ID_BUTTON_CHECKED:
111                 {
112                         AppLog("####### Enable push notification service. #######");
113
114                         UpdateSetting(true);
115
116                         PushClient* pApp = static_cast<PushClient*>(Application::GetInstance());
117                         pApp->EnablePush();
118                 }
119                 break;
120         case ID_BUTTON_UNCHECKED:
121                 {
122                         AppLog("####### Disable push notification service. #######");
123
124                         UpdateSetting(false);
125
126                         PushClient* pApp = static_cast<PushClient*>(Application::GetInstance());
127                         pApp->DisablePush();
128                 }
129                 break;
130         default:
131                 break;
132         }
133 }
134
135 bool
136 SettingForm::GetSetting(void)
137 {
138         return __pSettingButton->IsSelected();
139 }
140
141 void
142 SettingForm::UpdateSetting(bool value)
143 {
144         UpdateRegistry(value);
145
146         __pSettingButton->SetSelected(value);
147
148         Frame* pFrame = Application::GetInstance()->GetAppFrame()->GetFrame();
149         if(this == pFrame->GetCurrentForm())
150         {
151                 pFrame->Draw();
152
153         }
154 }
155
156 void
157 SettingForm::CheckRegistry(void)
158 {
159         Registry reg;
160         String regFile(PUSH_CLIENT_REGISTRY_FILE);
161         String section(PUSH_CLIENT_REGISTRY_SETTING_SECTION);
162         String entry(PUSH_CLIENT_REGISTRY_USE_PUSH_ENTRY);
163
164         if (true == File::IsFileExist(regFile))
165         {
166                 int value;
167
168                 reg.Construct(regFile, false);
169                 reg.GetValue(section, entry, value);
170
171                 if (true == (bool)value)
172                         UpdateSetting(true);
173                 else
174                         UpdateSetting(false);
175         }
176         else
177         {
178                 reg.Construct(regFile, true);
179                 reg.AddSection(section);
180                 reg.AddValue(section, entry, (int)true);
181                 reg.Flush();
182
183                 UpdateSetting(true);
184         }
185 }
186
187 void
188 SettingForm::UpdateRegistry(bool value)
189 {
190         Registry reg;
191         String regFile(PUSH_CLIENT_REGISTRY_FILE);
192         String section(PUSH_CLIENT_REGISTRY_SETTING_SECTION);
193         String entry(PUSH_CLIENT_REGISTRY_USE_PUSH_ENTRY);
194
195         reg.Construct(regFile, false);
196         reg.SetValue(section, entry, (int)value);
197         reg.Flush();
198 }