Fixed N_SE-38563, N_SE-38552
[apps/osp/Settings.git] / src / StCertificateUserInstallPasswordForm.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                StCertificateUserInstallPasswordForm.cpp
19  * @brief               This is the implementation file for CertificateUserInstallPasswordForm class.
20  */
21
22 #include "StCertificateUserInstallPasswordForm.h"
23 #include "StResourceManager.h"
24 #include "StSettingScenesList.h"
25 #include "StTypes.h"
26
27 using namespace Tizen::Base;
28 using namespace Tizen::Base::Utility;
29 using namespace Tizen::Graphics;
30 using namespace Tizen::Security;
31 using namespace Tizen::Security::Cert;
32 using namespace Tizen::Ui;
33 using namespace Tizen::Ui::Controls;
34 using namespace Tizen::Ui::Scenes;
35
36 static const int ID_GROUP_INSTALL_PASSWORD_MAIN = 0;
37 static const int ID_GROUP_INSTALL_PASSWORD_MAIN_ITEM_COUNT = 3;
38
39 static const int ID_ITEM_INSTALL_PASSWORD_INPUT = 0;
40 static const int ID_ITEM_INSTALL_PASSWORD_OK = 1;
41 static const int ID_ITEM_INSTALL_PASSWORD_CANCEL = 2;
42
43 static const int ID_GROUP_COUNT = 1;
44 static const int ID_GROUP_DEFAULT_ITEM_COUNT = ID_GROUP_INSTALL_PASSWORD_MAIN_ITEM_COUNT;
45
46 static const int IDA_PASSWORD_BUTTON_OK = 200;
47 static const int IDA_PASSWORD_BUTTON_CANCEL = 300;
48
49 static const int IDA_INSTALL_RESULT_SUCCESS = 210;
50 static const int IDA_INSTALL_RESULT_FAIL = 220;
51
52 static const int LIMIT_EDITFIELD = 100;
53 static const int ITEM_RECT_BUTTON_DEFAULT_X = 20;
54 static const int ITEM_RECT_BUTTON_DEFAULT_WIDTH_MULTIPLY = 2;
55 static const int ITEM_RECTANGLE_RESIZE_5 = 5;
56
57 static const int Y_INSERT_RESULT_POPUP_CLIENT_RECT = 160;
58 static const int H_INSERT_RESULT_POPUP_CLIENT_RECT = 380;
59 static const int W_INSERT_RESULT_POPUP_CLIENT_RECT = 700;
60
61 static const int H_INSERT_RESULT_POPUP_ITEM_RECT_LABEL = 340;
62 static const int X_INSERT_RESULT_POPUP_ITEM_RECT_LHS_BUTTON = 180;
63 static const int Y_INSERT_RESULT_POPUP_ITEM_RECT_LHS_BUTTON = 250;
64 static const int W_INSERT_RESULT_POPUP_ITEM_RECT_LHS_BUTTON = 310;
65 static const int H_INSERT_RESULT_POPUP_ITEM_RECT_LHS_BUTTON = 80;
66 static const int INSERT_RESULT_POPUP_FONT_SIZE = 40;
67
68 static const int W_ITEM_RESIZE_GAP = 16;
69
70 CertificateUserInstallPasswordForm::CertificateUserInstallPasswordForm(void)
71         : __pCertificatePresentationModelInstance(null)
72         , __pInstallFileName(null)
73         , __pEditField(null)
74         , __pInstallResultPopup(null)
75         , __pPassword(null)
76 {
77 }
78
79 CertificateUserInstallPasswordForm::~CertificateUserInstallPasswordForm(void)
80 {
81 }
82
83 void
84 CertificateUserInstallPasswordForm::CreateFooter(void)
85 {
86         Footer* pFooter = GetFooter();
87         AppAssert(pFooter);
88
89         pFooter->SetStyle(FOOTER_STYLE_BUTTON_TEXT);
90         pFooter->SetBackButton();
91         pFooter->AddActionEventListener(*this);
92
93         SetFormBackEventListener(this);
94 }
95
96 void
97 CertificateUserInstallPasswordForm::CreateTableView(void)
98 {
99         Rectangle tableViewBounds = GetClientAreaBounds();
100         tableViewBounds.y = Y_GROUP_DEFAULT;
101
102         __pTableView = new (std::nothrow) GroupedTableView();
103
104         __pTableView->Construct(tableViewBounds, true, TABLE_VIEW_SCROLL_BAR_STYLE_NONE);
105         __pTableView->SetItemProvider(this);
106
107         AddControl(__pTableView);
108         __pTableView->SetGroupedLookEnabled(true);
109         __pTableView->AddGroupedTableViewItemEventListener(*this);
110         RelativeLayout* pRelativeLayout = dynamic_cast<RelativeLayout*>(this->GetLayoutN());
111         if (pRelativeLayout != null)
112         {
113                 pRelativeLayout->SetHorizontalFitPolicy(*__pTableView, FIT_POLICY_PARENT);
114                 pRelativeLayout->SetVerticalFitPolicy(*__pTableView, FIT_POLICY_PARENT);
115                 delete pRelativeLayout;
116         }
117 }
118
119 result
120 CertificateUserInstallPasswordForm::OnInitializing(void)
121 {
122         CreateHeader(ResourceManager::GetString(L"IDS_ST_BODY_PASSWORD"));
123         CreateFooter();
124         CreateTableView();
125
126         result r = InitCertificatePresentationModelInstance();
127         if (IsFailed(r))
128         {
129                 return r;
130         }
131
132         return E_SUCCESS;
133 }
134
135 result
136 CertificateUserInstallPasswordForm::OnTerminating(void)
137 {
138         __pTableView->SetItemProvider(null);
139
140         ReleaseCertificatePresentationModelInstance();
141
142         delete __pInstallResultPopup;
143         __pInstallResultPopup = null;
144
145         SetFormBackEventListener(null);
146         return E_SUCCESS;
147 }
148
149 void
150 CertificateUserInstallPasswordForm::OnSceneActivatedN(const Tizen::Ui::Scenes::SceneId& previousSceneId, const Tizen::Ui::Scenes::SceneId& currentSceneId, Tizen::Base::Collection::IList* pArgs)
151 {
152         __pTableView->UpdateTableView();
153 }
154
155 void
156 CertificateUserInstallPasswordForm::OnSceneDeactivated(const Tizen::Ui::Scenes::SceneId& currentSceneId, const Tizen::Ui::Scenes::SceneId& nextSceneId)
157 {
158 }
159
160 void
161 CertificateUserInstallPasswordForm::OnFormBackRequested(Tizen::Ui::Controls::Form& source)
162 {
163         SceneManager* pSceneManager = SceneManager::GetInstance();
164         AppAssert(pSceneManager);
165
166         pSceneManager->GoBackward(BackwardSceneTransition(SCENE_TRANSITION_ANIMATION_TYPE_RIGHT), null);
167 }
168
169 int
170 CertificateUserInstallPasswordForm::GetGroupCount(void)
171 {
172         AppLogDebug("ENTER");
173         return ID_GROUP_COUNT;
174 }
175
176 int
177 CertificateUserInstallPasswordForm::GetItemCount(int groupIndex)
178 {
179         return ID_GROUP_DEFAULT_ITEM_COUNT;
180 }
181
182 TableViewGroupItem*
183 CertificateUserInstallPasswordForm::CreateGroupItem(int groupIndex, int itemWidth)
184 {
185         int itemHeight = H_GROUP_INDEX_NO_TITLE_DEFAULT;
186         int yItemOffset = H_GROUP_INDEX_HELP_TEXT_TOP_GAP;
187         LabelTextStyle style = LABEL_TEXT_STYLE_BOLD;
188         Rectangle itemMainRectangle;
189         String groupText = ResourceManager::GetString(L"IDS_WIFI_BODY_ENTER_PASSWORD");
190         Label* pLabel = null;
191
192         TableViewGroupItem* pItem = new (std::nothrow) TableViewGroupItem();
193
194         yItemOffset = H_GROUP_INDEX_HELP_TEXT_TOP_GAP;
195         itemHeight = H_GROUP_INDEX_DEFAULT;
196
197         itemMainRectangle.x = X_GROUP_ITEM_DEFAULT_LABEL;
198         itemMainRectangle.y = yItemOffset;
199         itemMainRectangle.width = itemWidth;
200         itemMainRectangle.height = itemHeight;
201
202         RelativeLayout relativeLayout;
203         relativeLayout.Construct();
204
205         pItem->Construct(relativeLayout, Dimension(itemWidth, itemHeight));
206         pItem->SetBackgroundColor(COLOR_BG_GROUP_INDEX_DEFAULT);
207
208         pLabel = new (std::nothrow) Label();
209         pLabel->Construct(itemMainRectangle, groupText);
210         pLabel->SetTextHorizontalAlignment(ALIGNMENT_LEFT);
211         pLabel->SetTextVerticalAlignment(ALIGNMENT_TOP);
212         pLabel->SetTextConfig(FONT_SIZE_GROUP_TITLE_TEXT, style);
213         pLabel->SetTextColor(COLOR_GROUP_TITLE_TEXT);
214
215         pItem->AddControl(pLabel);
216         relativeLayout.SetMargin(*pLabel, itemMainRectangle.x, RELATIVE_LAYOUT_RIGHT_MARGIN, 0, 0);
217         relativeLayout.SetRelation(*pLabel, pItem, RECT_EDGE_RELATION_LEFT_TO_LEFT);
218         relativeLayout.SetRelation(*pLabel, pItem, RECT_EDGE_RELATION_RIGHT_TO_RIGHT);
219         pItem->SetEnabled(false);
220
221         return pItem;
222 }
223
224 TableViewItem*
225 CertificateUserInstallPasswordForm::CreateItem(int groupIndex, int itemIndex, int itemWidth)
226 {
227         Rectangle itemMainRectangle;
228         Button* pButton = null;
229
230         TableViewAnnexStyle style = TABLE_VIEW_ANNEX_STYLE_NORMAL;
231         int fontSize = GetFontSize();
232         ItemTypeOneLine(itemMainRectangle);
233         int itemHeight = (itemMainRectangle.height);
234
235         itemMainRectangle.width = (itemWidth - W_ITEM_RESIZE_GAP);
236
237         TableViewItem* pItem = new (std::nothrow) TableViewItem();
238
239         RelativeLayout relativeLayout;
240         relativeLayout.Construct();
241
242         pItem->Construct(relativeLayout, Dimension(itemWidth, itemHeight), style);
243         pItem->SetBackgroundColor(COLOR_BG_GROUP_ITEM_DEFAULT);
244
245         switch (itemIndex)
246         {
247         case ID_ITEM_INSTALL_PASSWORD_INPUT:
248                 {
249                         __pEditField = new (std::nothrow) EditField();
250                         __pEditField->Construct(itemMainRectangle, EDIT_FIELD_STYLE_PASSWORD, INPUT_STYLE_OVERLAY, EDIT_FIELD_TITLE_STYLE_NONE, false, LIMIT_EDITFIELD, GROUP_STYLE_SINGLE);
251                         __pEditField->SetTextSize(fontSize);
252                         __pEditField->SetTitleTextColor(EDIT_STATUS_NORMAL, COLOR_HELP_TEXT_TYPE_01);
253                         __pEditField->SetColor(EDIT_STATUS_NORMAL, COLOR_BG_GROUP_INDEX_DEFAULT);
254                         __pEditField->SetColor(EDIT_STATUS_PRESSED, COLOR_BG_GROUP_INDEX_DEFAULT);
255                         __pEditField->SetColor(EDIT_STATUS_HIGHLIGHTED, COLOR_BG_GROUP_INDEX_DEFAULT);
256
257                         __pEditField->SetOverlayKeypadCommandButtonVisible(false);
258                         __pEditField->AddActionEventListener(*this);
259                         __pEditField->AddKeypadEventListener(*this);
260                         __pEditField->AddTextEventListener(*this);
261                         __pEditField->SetText(__pPassword);
262                         pItem->SetIndividualSelectionEnabled(__pEditField, true);
263                         pItem->AddControl(__pEditField);
264                         relativeLayout.SetMargin(*__pEditField, 0, RELATIVE_LAYOUT_RIGHT_MARGIN, 0, 0);
265                         relativeLayout.SetRelation(*__pEditField, pItem, RECT_EDGE_RELATION_LEFT_TO_LEFT);
266                         relativeLayout.SetRelation(*__pEditField, pItem, RECT_EDGE_RELATION_RIGHT_TO_RIGHT);
267                 }
268                 break;
269
270         case ID_ITEM_INSTALL_PASSWORD_OK:
271                 {
272                         pButton = new (std::nothrow) Button();
273                         pButton->Construct(itemMainRectangle, ResourceManager::GetString(L"IDS_ST_BUTTON_OK"));
274                         pButton->SetActionId(IDA_PASSWORD_BUTTON_OK);
275                         pButton->AddActionEventListener(*this);
276
277                         pItem->SetIndividualSelectionEnabled(pButton, true);
278                         pItem->AddControl(pButton);
279                         relativeLayout.SetMargin(*pButton, 0, RELATIVE_LAYOUT_RIGHT_MARGIN, 0, 0);
280                         relativeLayout.SetRelation(*pButton, pItem, RECT_EDGE_RELATION_LEFT_TO_LEFT);
281                         relativeLayout.SetRelation(*pButton, pItem, RECT_EDGE_RELATION_RIGHT_TO_RIGHT);
282                 }
283                 break;
284
285         case ID_ITEM_INSTALL_PASSWORD_CANCEL:
286                 {
287                         pButton = new (std::nothrow) Button();
288                         pButton->Construct(itemMainRectangle, ResourceManager::GetString(L"IDS_ST_BODY_CANCEL"));
289                         pButton->SetActionId(IDA_PASSWORD_BUTTON_CANCEL);
290                         pButton->AddActionEventListener(*this);
291
292                         pItem->SetIndividualSelectionEnabled(pButton, true);
293                         pItem->AddControl(pButton);
294                         relativeLayout.SetMargin(*pButton, 0, RELATIVE_LAYOUT_RIGHT_MARGIN, 0, 0);
295                         relativeLayout.SetRelation(*pButton, pItem, RECT_EDGE_RELATION_LEFT_TO_LEFT);
296                         relativeLayout.SetRelation(*pButton, pItem, RECT_EDGE_RELATION_RIGHT_TO_RIGHT);
297                 }
298                 break;
299
300         default:
301                 break;
302         }
303         return pItem;
304 }
305
306 bool
307 CertificateUserInstallPasswordForm::DeleteGroupItem(int groupIndex, TableViewGroupItem* pItem)
308 {
309         AppLogDebug("ENTER");
310
311         delete pItem;
312         pItem = null;
313
314         return true;
315 }
316
317 bool
318 CertificateUserInstallPasswordForm::DeleteItem(int groupIndex, int itemIndex, TableViewItem* pItem)
319 {
320         AppLogDebug("ENTER");
321
322         delete pItem;
323         pItem = null;
324
325         return true;
326 }
327
328 void
329 CertificateUserInstallPasswordForm::OnGroupedTableViewItemStateChanged(Tizen::Ui::Controls::GroupedTableView& tableView, int groupIndex, int itemIndex, Tizen::Ui::Controls::TableViewItem* pItem, Tizen::Ui::Controls::TableViewItemStatus status)
330 {
331         AppLogDebug("selectItem[%d][%d]", groupIndex, itemIndex);
332 }
333
334 int
335 CertificateUserInstallPasswordForm::GetDefaultGroupItemHeight(void)
336 {
337         return H_GROUP_INDEX_NO_TITLE_DEFAULT;
338 }
339
340 int
341 CertificateUserInstallPasswordForm::GetDefaultItemHeight(void)
342 {
343         return H_GROUP_ITEM_DEFAULT;
344 }
345
346 void
347 CertificateUserInstallPasswordForm::UpdateGroupItem(int groupIndex, Tizen::Ui::Controls::TableViewGroupItem* pItem)
348 {
349 }
350
351 void
352 CertificateUserInstallPasswordForm::UpdateItem(int groupIndex, int itemIndex, Tizen::Ui::Controls::TableViewItem* pItem)
353 {
354 }
355
356 void
357 CertificateUserInstallPasswordForm::OnGroupedTableViewContextItemActivationStateChanged(Tizen::Ui::Controls::GroupedTableView& tableView, int groupIndex, int itemIndex, Tizen::Ui::Controls::TableViewContextItem* pContextItem, bool activated)
358 {
359 }
360
361 void
362 CertificateUserInstallPasswordForm::OnGroupedTableViewGroupItemStateChanged(Tizen::Ui::Controls::GroupedTableView& tableView, int groupIndex, Tizen::Ui::Controls::TableViewGroupItem* pItem, Tizen::Ui::Controls::TableViewItemStatus status)
363 {
364 }
365
366 void
367 CertificateUserInstallPasswordForm::OnActionPerformed(const Control& source, int actionId)
368 {
369         switch (actionId)
370         {
371         case IDA_PASSWORD_BUTTON_OK:
372                 {
373                         __pEditField->HideKeypad();
374                         __pEditField->Invalidate(false);
375
376                         if (__pInstallFileName == null)
377                         {
378                                 return;
379                         }
380
381                         String password = __pEditField->GetText();
382                         result r = __pCertificatePresentationModelInstance->InsertPkcs12FormatIntoCertificateStore(*__pInstallFileName, password);
383                         if (IsFailed(r))
384                         {
385                                 CreateUserInstallResultPopup(r);
386                                 ShowUserInstallResultPopup();
387                                 return;
388                         }
389
390                         //TODO : password Check...
391
392                         SceneManager* pSceneManager = SceneManager::GetInstance();
393                         AppAssert(pSceneManager);
394
395                         pSceneManager->GoBackward(BackwardSceneTransition(SCENE_TRANSITION_ANIMATION_TYPE_RIGHT), null);
396                 }
397                 break;
398
399         case IDA_PASSWORD_BUTTON_CANCEL:
400                 {
401                         SceneManager* pSceneManager = SceneManager::GetInstance();
402                         AppAssert(pSceneManager);
403
404                         String password = __pEditField->GetText();
405
406                         __pEditField->SetText(L"");
407                         __pPassword = L"";
408                         __pEditField->HideKeypad();
409                         __pEditField->Invalidate(false);
410
411                         pSceneManager->GoBackward(BackwardSceneTransition(SCENE_TRANSITION_ANIMATION_TYPE_RIGHT), null);
412                 }
413                 break;
414
415         case IDA_INSTALL_RESULT_SUCCESS:
416                 {
417                         HideUserInstallResultPopup();
418                 }
419                 break;
420
421         case IDA_INSTALL_RESULT_FAIL:
422                 {
423                         HideUserInstallResultPopup();
424                 }
425                 break;
426
427         default:
428                 break;
429         }
430 }
431
432 void
433 CertificateUserInstallPasswordForm::OnTextValueChanged(const Tizen::Ui::Control& source)
434 {
435         __pPassword = __pEditField->GetText();
436 }
437
438 result
439 CertificateUserInstallPasswordForm::InitCertificatePresentationModelInstance(void)
440 {
441         __pCertificatePresentationModelInstance = CertificatePresentationModel::GetInstance();
442         if (__pCertificatePresentationModelInstance == null)
443         {
444                 return E_FAILURE;
445         }
446
447         __pInstallFileName = __pCertificatePresentationModelInstance->PopPendingUserCertificateFileNameToInstallN();
448         if (__pInstallFileName == null)
449         {
450                 return E_FAILURE;
451         }
452
453         return E_SUCCESS;
454 }
455
456 void
457 CertificateUserInstallPasswordForm::ReleaseCertificatePresentationModelInstance(void)
458 {
459         __pCertificatePresentationModelInstance = null;
460 }
461
462 void
463 CertificateUserInstallPasswordForm::CreateUserInstallResultPopup(result r)
464 {
465         int buttonId = IDA_INSTALL_RESULT_FAIL;
466         String buttonText;
467         String popupText;
468         Label* pLabel = null;
469         Button* pLhsButton = null;
470
471         Rectangle clientRect = GetClientAreaBounds();
472         Rectangle itemRectLabel;
473         Rectangle itemRectLhsButton;
474         clientRect.x = 0;
475         clientRect.y = Y_INSERT_RESULT_POPUP_CLIENT_RECT;
476         clientRect.width = W_INSERT_RESULT_POPUP_CLIENT_RECT;
477         clientRect.height = H_INSERT_RESULT_POPUP_CLIENT_RECT;
478
479         itemRectLabel.x = 0;
480         itemRectLabel.y = 0;
481         itemRectLabel.width = clientRect.width;
482         itemRectLabel.height = H_INSERT_RESULT_POPUP_ITEM_RECT_LABEL;
483
484         itemRectLhsButton.x = X_INSERT_RESULT_POPUP_ITEM_RECT_LHS_BUTTON;
485         itemRectLhsButton.y = Y_INSERT_RESULT_POPUP_ITEM_RECT_LHS_BUTTON;
486         itemRectLhsButton.width = W_INSERT_RESULT_POPUP_ITEM_RECT_LHS_BUTTON;
487         itemRectLhsButton.height = H_INSERT_RESULT_POPUP_ITEM_RECT_LHS_BUTTON;
488
489         Dimension bounds = Dimension(clientRect.width, clientRect.height);
490
491         __pInstallResultPopup = new (std::nothrow) Popup();
492         __pInstallResultPopup->Construct(false, bounds);
493         __pInstallResultPopup->SetTitleTextColor(COLOR_SUB_TEXT);
494
495         buttonId = IDA_INSTALL_RESULT_FAIL;
496         popupText = ResourceManager::GetString(L"IDS_COM_POP_FAILED");
497
498         pLabel = new (std::nothrow) Label();
499         pLabel->Construct(itemRectLabel, popupText);
500         pLabel->SetTextHorizontalAlignment(ALIGNMENT_CENTER);
501         pLabel->SetTextVerticalAlignment(ALIGNMENT_MIDDLE);
502         pLabel->SetTextConfig(INSERT_RESULT_POPUP_FONT_SIZE, LABEL_TEXT_STYLE_BOLD);
503         __pInstallResultPopup->AddControl(pLabel);
504
505         buttonText = ResourceManager::GetString(L"IDS_ST_BUTTON_OK");
506         pLhsButton = new (std::nothrow) Button();
507         pLhsButton->Construct(itemRectLhsButton, buttonText);
508         pLhsButton->SetActionId(buttonId);
509         pLhsButton->AddActionEventListener(*this);
510
511         __pInstallResultPopup->AddControl(pLhsButton);
512 }
513
514 void
515 CertificateUserInstallPasswordForm::ShowUserInstallResultPopup(void)
516 {
517         __pInstallResultPopup->SetShowState(true);
518         __pInstallResultPopup->Show();
519 }
520
521 void
522 CertificateUserInstallPasswordForm::HideUserInstallResultPopup(void)
523 {
524         __pInstallResultPopup->SetShowState(false);
525         Invalidate(true);
526 }