add H/W backkey action
[framework/osp/web.git] / src / controls / FWebCtrl_AuthConfirmPopup.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
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 /**
19  * @file                FWebCtrl_AuthConfirmPopup.cpp
20  * @brief               The file contains the definition of _AuthConfirmPopup class.
21  */
22 #include <FBaseColArrayList.h>
23 #include <FBaseSysLog.h>
24 #include <FGrpDimension.h>
25 #include <FGrpRectangle.h>
26 #include <FSysVibrator.h>
27 #include <FUiCtrlButton.h>
28 #include <FUiCtrlEditField.h>
29 #include <FUiCtrlLabel.h>
30 #include <FUiCtrlPanel.h>
31 #include <FUiKeyEventInfo.h>
32 #include <FUiLayout.h>
33 #include <FUiVerticalBoxLayout.h>
34 #include <FWebCtrlAuthenticationChallenge.h>
35 #include <FSys_SystemResource.h>
36 #include <FSys_VibratorImpl.h>
37 #include <FUi_ControlManager.h>
38 #include <FUi_ResourceManager.h>
39 #include "FWebCtrl_AuthConfirmPopup.h"
40 #include "FWebCtrl_WebImpl.h"
41
42
43 using namespace Tizen::Base;
44 using namespace Tizen::Base::Collection;
45 using namespace Tizen::Graphics;
46 using namespace Tizen::System;
47 using namespace Tizen::Ui;
48 using namespace Tizen::Ui::Controls;
49
50
51 namespace Tizen { namespace Web { namespace Controls
52 {
53
54
55 static const int EDIT_TEXT_SIZE = 35;
56
57
58 _AuthConfirmPopup::_AuthConfirmPopup(void)
59         : __pIdEditField(null)
60         , __pPwdEditField(null)
61         , __pAuthHandler(null)
62 {
63 }
64
65
66 _AuthConfirmPopup::~_AuthConfirmPopup(void)
67 {
68 }
69
70
71 // Authentication popup
72 result
73 _AuthConfirmPopup::Construct(const String& host, const String& realm, AuthenticationChallenge* pAuth)
74 {
75         SysTryReturnResult(NID_WEB_CTRL, pAuth != null, E_INVALID_ARG, "invalid argument(s) is used. The AuthenticationChallenge is null.");
76
77         __pAuthHandler = pAuth;
78
79         _SystemResource* pSysResource = _SystemResource::GetInstance();
80         SysAssertf(pSysResource != null, "Failed to get _SystemResource instance");
81
82         result r = E_SUCCESS;
83         _WebPopupData* pPopupData = _WebPopup::GetPopupData();
84         SysTryReturn(NID_WEB_CTRL, pPopupData, r = GetLastResult(), r, "[%s] Propagating.", GetErrorMessage(r));
85
86         Rectangle rect(0, 0, 0, 0);
87         rect.width = pPopupData->popupDim.width;
88         rect.height = (2*pPopupData->spacePad) + (pPopupData->editDim.height*2) + pPopupData->btnDim.height + (2*pPopupData->labelDim.height);
89         r = _WebPopup::Construct(true, Dimension(rect.width, rect.height));
90         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
91
92         SetTitleText(pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_BR_BODY_DESTINATIONS_AUTHENTICATION_REQUIRED"));
93
94         //host realm label
95         rect.height = 2*pPopupData->labelDim.height;
96
97         std::unique_ptr<Label> pLabel(new (std::nothrow) Label());
98         SysTryReturnResult(NID_WEB_CTRL, pLabel.get(), E_OUT_OF_MEMORY, "Memory Allocation failed.");
99
100         String hostRealm(host);
101         hostRealm.Append(L": \n ");
102         hostRealm.Append(realm);
103         hostRealm.Append(L"\"");
104         r = pLabel->Construct(rect, hostRealm);
105         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
106
107         pLabel->SetTextConfig(pPopupData->labelFontSize, LABEL_TEXT_STYLE_NORMAL);
108
109         r = AddControl(*pLabel);
110         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
111
112         Label* pHostLabel = pLabel.release();
113
114         // Id Edit Field
115         rect.x = 0;
116         rect.y = 0;
117         rect.height = pPopupData->editDim.height;
118         rect.width = pPopupData->labelDim.width;
119
120         std::unique_ptr<EditField> pIdEditField(new (std::nothrow) EditField());
121         SysTryReturnResult(NID_WEB_CTRL, pIdEditField.get(), E_OUT_OF_MEMORY, "Memory Allocation failed.");
122
123         r = pIdEditField->Construct(rect, EDIT_FIELD_STYLE_NORMAL);
124         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
125
126         pIdEditField->SetGuideText(pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_BR_BODY_AUTHUSERNAME"));
127         pIdEditField->SetGuideTextColor(Color::GetColor(COLOR_ID_GREY));
128         pIdEditField->SetTextSize(EDIT_TEXT_SIZE);;
129
130         r = AddControl(*pIdEditField);
131         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
132
133         __pIdEditField = pIdEditField.release();
134
135         std::unique_ptr<EditField> pPwdEditField(new (std::nothrow) EditField());
136         SysTryReturnResult(NID_WEB_CTRL, pPwdEditField.get(), E_OUT_OF_MEMORY, "Memory Allocation failed.");
137
138         r = pPwdEditField->Construct(rect, EDIT_FIELD_STYLE_PASSWORD);
139         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
140
141         pPwdEditField->SetGuideText(pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_BR_BODY_AUTHPASSWORD"));
142         pPwdEditField->SetGuideTextColor(Color::GetColor(COLOR_ID_GREY));
143         pPwdEditField->SetTextSize(EDIT_TEXT_SIZE);;
144
145         r = AddControl(*pPwdEditField);
146         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
147
148         __pPwdEditField = pPwdEditField.release();
149         __pIdEditField->SetFocus();
150
151         Panel* pButtonPanel = CreateAndAddPanel();
152         SysTryReturn(NID_WEB_CTRL, pButtonPanel, r = GetLastResult(), r, "[%s] Propagating.", GetErrorMessage(r));
153
154         //Add Buttons
155         ArrayList idList;
156         r = idList.Construct();
157         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
158         idList.Add(*(new Integer(ID_BUTTON_AUTH_PROCESS)));
159         idList.Add(*(new Integer(ID_BUTTON_AUTH_CANCEL)));
160
161         ArrayList titleList;
162         r = titleList.Construct();
163         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
164         titleList.Add(*(new String(pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_COM_BUTTON_LOGIN"))));
165         titleList.Add(*(new String(pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_COM_SK_CANCEL"))));
166
167         r = CreateAndAddButtons(idList, titleList, pButtonPanel);
168         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
169
170         std::unique_ptr<VerticalBoxLayout> pLayout(dynamic_cast< VerticalBoxLayout* >(GetLayoutN()));
171         SysTryReturn(NID_WEB_CTRL, pLayout.get(), GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
172
173         pLayout->SetHorizontalAlignment(*pHostLabel, LAYOUT_HORIZONTAL_ALIGN_CENTER);
174         pLayout->SetHorizontalAlignment(*__pIdEditField, LAYOUT_HORIZONTAL_ALIGN_CENTER);
175         pLayout->SetHorizontalAlignment(*__pPwdEditField, LAYOUT_HORIZONTAL_ALIGN_CENTER);
176         pLayout->SetHorizontalAlignment(*pButtonPanel, LAYOUT_HORIZONTAL_ALIGN_CENTER);
177
178         pLayout->SetSpacing(*__pPwdEditField, pPopupData->spacePad);
179         pLayout->SetSpacing(*pButtonPanel, pPopupData->spacePad);
180
181         SetPropagatedKeyEventListener(this);
182
183         return E_SUCCESS;
184 }
185
186
187 void
188 _AuthConfirmPopup::OnActionPerformed(const Control& source, int actionId)
189 {
190         result r = E_SUCCESS;
191
192         switch (actionId)
193         {
194         case ID_BUTTON_AUTH_PROCESS:
195                 __pAuthHandler->Process(__pIdEditField->GetText(), __pPwdEditField->GetText());
196                 break;
197
198         case ID_BUTTON_AUTH_CANCEL:
199                 __pAuthHandler->Cancel();
200                 break;
201
202         default:
203                 break;
204         }
205
206         r = HidePopup();
207         SysTryReturnVoidResult(NID_WEB_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
208 }
209
210 bool
211 _AuthConfirmPopup::OnKeyPressed(Control& source, const KeyEventInfo& keyEventInfo)
212 {
213         return true;
214 }
215
216 bool
217 _AuthConfirmPopup::OnKeyReleased(Control& source, const KeyEventInfo& keyEventInfo)
218 {
219         result r = E_SUCCESS;
220         if ((keyEventInfo.GetKeyCode() == KEY_ESC || keyEventInfo.GetKeyCode() == KEY_BACK) && source.GetShowState() == true)
221         {
222                 __pAuthHandler->Cancel();
223                 r = HidePopup();
224                 if (IsFailed(r))
225                 {
226                         SysLogException(NID_WEB_CTRL, r, "[%s] Propagating.", GetErrorMessage(r));
227                 }
228         }
229
230         return true;
231 }
232
233 bool
234 _AuthConfirmPopup::OnPreviewKeyPressed(Control& source, const KeyEventInfo& keyEventInfo)
235 {
236         return false;
237 }
238
239 bool
240 _AuthConfirmPopup::OnPreviewKeyReleased(Control& source, const KeyEventInfo& keyEventInfo)
241 {
242         return false;
243 }
244
245 bool
246 _AuthConfirmPopup::TranslateKeyEventInfo(Control& source, KeyEventInfo& keyEventInfo)
247 {
248         return false;
249 }
250
251
252 }}} // Tizen::Web::Controls