Fixed N_SE-38563, N_SE-38552
[apps/osp/Settings.git] / src / StCertificateDetailForm.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                StCertificateDetailForm.cpp
19  * @brief               This is the implementation file for CertificateDetailForm class.
20  */
21
22 #include "StCertificateDetailForm.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_COUNT = 1;
37 static const int ID_GROUP_DEFAULT_ITEM_COUNT = 10;
38 static const int OWNER_TEXT_RESIZE = 1;
39
40 static const int CERTIFICATE_DETAIL_ITEM_FIRST_LABEL = 0;
41 static const int CERTIFICATE_DETAIL_ITEM_SECOND_LABEL = 1;
42
43 enum CertificateDetailItem
44 {
45         CERTIFICATE_DETAIL_ITEM_BASE,
46         CERTIFICATE_DETAIL_ITEM_OWNER_COMMON_NAME = CERTIFICATE_DETAIL_ITEM_BASE,
47         CERTIFICATE_DETAIL_ITEM_OWNER_ORGANISATION,
48         CERTIFICATE_DETAIL_ITEM_ISSUER_COMMON_NAME,
49         CERTIFICATE_DETAIL_ITEM_ISSUER_ORGANISATION,
50         CERTIFICATE_DETAIL_ITEM_CERTIFICATE_INFORMATION_VERSION,
51         CERTIFICATE_DETAIL_ITEM_VALID_FROM,
52         CERTIFICATE_DETAIL_ITEM_VALID_TO,
53         CERTIFICATE_DETAIL_ITEM_SERIAL_NUMBER,
54         CERTIFICATE_DETAIL_ITEM_SIGNATURE_ALGORITHM,
55         CERTIFICATE_DETAIL_ITEM_PUBLIC_KEY,
56         CERTIFICATE_DETAIL_ITEM_MAX
57 };
58
59 CertificateDetailForm::CertificateDetailForm(void)
60         : __pCertificatePresentationModelInstance(null)
61         , __pX509Certificate(null)
62 {
63 }
64
65 CertificateDetailForm::~CertificateDetailForm(void)
66 {
67 }
68
69 void
70 CertificateDetailForm::CreateFooter(void)
71 {
72         Footer* pFooter = GetFooter();
73         AppAssert(pFooter);
74
75         pFooter->SetStyle(FOOTER_STYLE_BUTTON_TEXT);
76         pFooter->SetBackButton();
77         pFooter->AddActionEventListener(*this);
78
79         SetFormBackEventListener(this);
80 }
81
82 void
83 CertificateDetailForm::CreateTableView(void)
84 {
85         Rectangle tableViewBounds = GetClientAreaBounds();
86         tableViewBounds.y = Y_GROUP_DEFAULT;
87
88         __pTableView = new (std::nothrow) GroupedTableView();
89
90         __pTableView->Construct(tableViewBounds, true, TABLE_VIEW_SCROLL_BAR_STYLE_FADE_OUT);
91         __pTableView->SetItemProvider(this);
92
93         AddControl(__pTableView);
94         __pTableView->SetGroupedLookEnabled(true);
95         __pTableView->AddGroupedTableViewItemEventListener(*this);
96         RelativeLayout* pRelativeLayout = dynamic_cast<RelativeLayout*>(this->GetLayoutN());
97         if (pRelativeLayout != null)
98         {
99                 pRelativeLayout->SetHorizontalFitPolicy(*__pTableView, FIT_POLICY_PARENT);
100                 pRelativeLayout->SetVerticalFitPolicy(*__pTableView, FIT_POLICY_PARENT);
101                 delete pRelativeLayout;
102         }
103 }
104
105 result
106 CertificateDetailForm::OnInitializing(void)
107 {
108         CreateHeader(ResourceManager::GetString(L"IDS_ST_BODY_CERTIFICATE_DETAILS"));
109         CreateFooter();
110         CreateTableView();
111
112         result r = InitCertificatePresentationModelInstance();
113         if (IsFailed(r))
114         {
115                 return r;
116         }
117
118         return LoadCertificate();
119 }
120
121 result
122 CertificateDetailForm::OnTerminating(void)
123 {
124         __pTableView = null;
125
126         SetFormBackEventListener(null);
127         return E_SUCCESS;
128 }
129
130 void
131 CertificateDetailForm::OnSceneActivatedN(const Tizen::Ui::Scenes::SceneId& previousSceneId, const Tizen::Ui::Scenes::SceneId& currentSceneId, Tizen::Base::Collection::IList* pArgs)
132 {
133         __pTableView->UpdateTableView();
134 }
135
136 void
137 CertificateDetailForm::OnSceneDeactivated(const Tizen::Ui::Scenes::SceneId& currentSceneId, const Tizen::Ui::Scenes::SceneId& nextSceneId)
138 {
139 }
140
141 void
142 CertificateDetailForm::OnFormBackRequested(Tizen::Ui::Controls::Form& source)
143 {
144         SceneManager* pSceneManager = SceneManager::GetInstance();
145         AppAssert(pSceneManager);
146
147         pSceneManager->GoBackward(BackwardSceneTransition(SCENE_TRANSITION_ANIMATION_TYPE_RIGHT), null);
148 }
149
150 int
151 CertificateDetailForm::GetGroupCount(void)
152 {
153         AppLogDebug("ENTER");
154         return ID_GROUP_COUNT;
155 }
156
157 int
158 CertificateDetailForm::GetItemCount(int groupIndex)
159 {
160         return ID_GROUP_DEFAULT_ITEM_COUNT;
161 }
162
163 Tizen::Ui::Controls::TableViewGroupItem*
164 CertificateDetailForm::CreateGroupItem(int groupIndex, int itemWidth)
165 {
166         int itemHeight = H_GROUP_INDEX_NO_TITLE_DEFAULT;
167         int yItemOffset = H_GROUP_INDEX_HELP_TEXT_TOP_GAP;
168         LabelTextStyle style = LABEL_TEXT_STYLE_BOLD;
169         Rectangle itemMainRectangle;
170         String groupText;
171         Label* pLabel = null;
172
173         TableViewGroupItem* pItem = new (std::nothrow) TableViewGroupItem();
174
175         yItemOffset = H_GROUP_INDEX_HELP_TEXT_TOP_GAP;
176         itemHeight = H_GROUP_INDEX_NO_TITLE_DEFAULT;
177
178         itemMainRectangle.x = X_GROUP_ITEM_DEFAULT_LABEL;
179         itemMainRectangle.y = yItemOffset;
180         itemMainRectangle.width = itemWidth;
181         itemMainRectangle.height = itemHeight;
182
183         RelativeLayout relativeLayout;
184         relativeLayout.Construct();
185
186         pItem->Construct(relativeLayout, Dimension(itemWidth, itemHeight));
187         pItem->SetBackgroundColor(COLOR_BG_GROUP_INDEX_DEFAULT);
188
189         pLabel = new (std::nothrow) Label();
190         pLabel->Construct(itemMainRectangle, groupText);
191         pLabel->SetTextHorizontalAlignment(ALIGNMENT_LEFT);
192         pLabel->SetTextVerticalAlignment(ALIGNMENT_TOP);
193         pLabel->SetTextConfig(FONT_SIZE_GROUP_TITLE_TEXT, style);
194         pLabel->SetTextColor(COLOR_GROUP_TITLE_TEXT);
195
196         pItem->AddControl(pLabel);
197         pItem->SetEnabled(false);
198         relativeLayout.SetMargin(*pLabel, itemMainRectangle.x, RELATIVE_LAYOUT_RIGHT_MARGIN, 0, 0);
199         relativeLayout.SetRelation(*pLabel, pItem, RECT_EDGE_RELATION_LEFT_TO_LEFT);
200         relativeLayout.SetRelation(*pLabel, pItem, RECT_EDGE_RELATION_RIGHT_TO_RIGHT);
201
202         return pItem;
203 }
204
205 Tizen::Ui::Controls::TableViewItem*
206 CertificateDetailForm::CreateItem(int groupIndex, int itemIndex, int itemWidth)
207 {
208         Rectangle itemMainRectangle;
209         Rectangle itemSubRectangle;
210         TableViewAnnexStyle style = TABLE_VIEW_ANNEX_STYLE_NORMAL;
211         String itemMainText;
212         String itemSubText;
213
214         int fontSize = GetFontSize();
215         ItemTypeTwoLine(itemMainRectangle, itemSubRectangle, fontSize);
216         int itemHeight = (itemMainRectangle.height + itemSubRectangle.height);
217
218         switch (itemIndex)
219         {
220         case CERTIFICATE_DETAIL_ITEM_OWNER_COMMON_NAME:
221                 {
222                         itemMainText = ResourceManager::GetString(L"IDS_ST_BODY_OWNER_C");
223                         itemMainText.Remove(itemMainText.GetLength() - OWNER_TEXT_RESIZE, OWNER_TEXT_RESIZE);
224                         itemMainText.Append(L"/");
225                         itemMainText.Append(ResourceManager::GetString(L"IDS_ST_BODY_COMMON_NAME_C"));
226                         itemSubText = CertificatePresentationModelUtility::GetCommonName(__pX509Certificate->GetSubject());
227                         if (IsFailed(GetLastResult()))
228                         {
229                                 AppLogDebug("GetCommonName failed(%s)", GetErrorMessage(GetLastResult()));
230                                 itemSubText = CertificatePresentationModelUtility::GetOrganisation(__pX509Certificate->GetSubject());
231                                 if (IsFailed(GetLastResult()))
232                                 {
233                                         AppLogDebug("GetOrganisation failed(%s)", GetErrorMessage(GetLastResult()));
234                                         itemSubText = ResourceManager::GetString(L"IDS_ST_BODY_NO_DATA");
235                                 }
236                         }
237                 }
238                 break;
239
240         case CERTIFICATE_DETAIL_ITEM_OWNER_ORGANISATION:
241                 {
242                         itemMainText = ResourceManager::GetString(L"IDS_ST_BODY_ORGANISATION_C");
243                         itemSubText = CertificatePresentationModelUtility::GetOrganisation(__pX509Certificate->GetSubject());
244                         if (IsFailed(GetLastResult()))
245                         {
246                                 AppLogDebug("GetOrganisation failed(%s)", GetErrorMessage(GetLastResult()));
247                                 itemSubText = ResourceManager::GetString(L"IDS_ST_BODY_NO_DATA");
248                         }
249                 }
250                 break;
251
252         case CERTIFICATE_DETAIL_ITEM_ISSUER_COMMON_NAME:
253                 {
254                         itemMainText = ResourceManager::GetString(L"IDS_ST_BODY_ISSUER");
255                         itemMainText.Append(L"/");
256                         itemMainText.Append(ResourceManager::GetString(L"IDS_ST_BODY_COMMON_NAME_C"));
257                         itemSubText = CertificatePresentationModelUtility::GetCommonName(__pX509Certificate->GetIssuer());
258                         if (IsFailed(GetLastResult()))
259                         {
260                                 AppLogDebug("GetCommonName failed(%s)", GetErrorMessage(GetLastResult()));
261                                 itemSubText = CertificatePresentationModelUtility::GetOrganisation(__pX509Certificate->GetIssuer());
262                                 if (IsFailed(GetLastResult()))
263                                 {
264                                         AppLogDebug("GetOrganisation failed(%s)", GetErrorMessage(GetLastResult()));
265                                         itemSubText = ResourceManager::GetString(L"IDS_ST_BODY_NO_DATA");
266                                 }
267                         }
268                 }
269                 break;
270
271         case CERTIFICATE_DETAIL_ITEM_ISSUER_ORGANISATION:
272                 {
273                         itemMainText = ResourceManager::GetString(L"IDS_ST_BODY_ORGANISATION_C");
274                         itemSubText = CertificatePresentationModelUtility::GetOrganisation(__pX509Certificate->GetIssuer());
275                         if (IsFailed(GetLastResult()))
276                         {
277                                 AppLogDebug("GetOrganisation failed(%s)", GetErrorMessage(GetLastResult()));
278                                 itemSubText = ResourceManager::GetString(L"IDS_ST_BODY_NO_DATA");
279                         }
280                 }
281                 break;
282
283         case CERTIFICATE_DETAIL_ITEM_CERTIFICATE_INFORMATION_VERSION:
284                 {
285                         itemMainText = ResourceManager::GetString(L"IDS_ST_BODY_CERTIFICATE_INFORMATION");
286                         itemMainText.Append(L"/");
287                         itemMainText.Append(ResourceManager::GetString(L"IDS_ST_BODY_VERSION"));
288                         itemMainText.Append(L":");
289                         itemSubText = Integer::ToString(__pX509Certificate->GetSpecVersion());
290                 }
291                 break;
292
293         case CERTIFICATE_DETAIL_ITEM_VALID_FROM:
294                 {
295                         itemMainText = ResourceManager::GetString(L"IDS_ST_BODY_VALID_FROM");
296                         itemMainText.Append(L":");
297                         itemSubText = __pX509Certificate->GetNotBefore();
298                         if (IsFailed(GetLastResult()))
299                         {
300                                 AppLogDebug("GetNotBefore failed(%s)", GetErrorMessage(GetLastResult()));
301                                 itemSubText = ResourceManager::GetString(L"IDS_ST_BODY_NO_DATA");
302                         }
303                 }
304                 break;
305
306         case CERTIFICATE_DETAIL_ITEM_VALID_TO:
307                 {
308                         itemMainText = ResourceManager::GetString(L"IDS_ST_BODY_VALID_TO_C");
309                         itemSubText = __pX509Certificate->GetNotAfter();
310                         if (IsFailed(GetLastResult()))
311                         {
312                                 AppLogDebug("GetNotAfter failed(%s)", GetErrorMessage(GetLastResult()));
313                                 itemSubText = ResourceManager::GetString(L"IDS_ST_BODY_NO_DATA");
314                         }
315                 }
316                 break;
317
318         case CERTIFICATE_DETAIL_ITEM_SERIAL_NUMBER:
319                 {
320                         itemMainText = ResourceManager::GetString(L"IDS_ST_BODY_SERIAL_NUMBER");
321                         itemMainText.Append(L":");
322                         itemSubText = __pX509Certificate->GetSerialNumber();
323                         if (IsFailed(GetLastResult()))
324                         {
325                                 AppLogDebug("GetSerialNumber failed(%s)", GetErrorMessage(GetLastResult()));
326                                 itemSubText = ResourceManager::GetString(L"IDS_ST_BODY_NO_DATA");
327                         }
328                 }
329                 break;
330
331         case CERTIFICATE_DETAIL_ITEM_SIGNATURE_ALGORITHM:
332                 {
333                         itemMainText = ResourceManager::GetString(L"IDS_ST_BODY_SIGNATURE_ALGORITHM");
334                         itemMainText.Append(L":");
335                         itemSubText = __pX509Certificate->GetSignatureAlgorithm();
336                         if (IsFailed(GetLastResult()))
337                         {
338                                 AppLogDebug("GetSignatureAlgorithm failed(%s)", GetErrorMessage(GetLastResult()));
339                                 itemSubText = ResourceManager::GetString(L"IDS_ST_BODY_NO_DATA");
340                         }
341                 }
342                 break;
343
344         case CERTIFICATE_DETAIL_ITEM_PUBLIC_KEY:
345                 {
346                         itemMainText = ResourceManager::GetString(L"IDS_ST_BODY_PUBLIC_KEY_C");
347                         IPublicKey* publicKey = __pX509Certificate->GetPublicKeyN();
348
349                         itemSubText = CertificatePresentationModelUtility::GetHexPublicKeyString(*publicKey);
350                         if (IsFailed(GetLastResult()))
351                         {
352                                 AppLogDebug("GetHexPublicKeyString failed(%s)", GetErrorMessage(GetLastResult()));
353                                 itemSubText = ResourceManager::GetString(L"IDS_ST_BODY_NO_DATA");
354                         }
355                         delete publicKey;
356
357                         itemSubRectangle.width = itemWidth;
358                         itemSubRectangle.height = GetHeightForStringArea(itemSubText, itemSubRectangle.width, H_GROUP_INDEX_TEXT_GAP);
359                         itemHeight = (itemMainRectangle.height + itemSubRectangle.height);
360                 }
361                 break;
362
363         default:
364                 break;
365         }
366
367         TableViewItem* pItem = new (std::nothrow) TableViewItem();
368
369         RelativeLayout relativeLayout;
370         relativeLayout.Construct();
371
372         pItem->Construct(relativeLayout, Dimension(itemWidth, itemHeight), style);
373         pItem->SetBackgroundColor(COLOR_BG_GROUP_ITEM_DEFAULT);
374
375         Label* pLabel = new (std::nothrow) Label();
376         pLabel->Construct(itemMainRectangle, itemMainText);
377         pLabel->SetTextHorizontalAlignment(ALIGNMENT_LEFT);
378         pLabel->SetTextConfig(fontSize, LABEL_TEXT_STYLE_NORMAL);
379         pLabel->SetTextColor(COLOR_MAIN_TEXT);
380
381         pItem->AddControl(pLabel);
382         relativeLayout.SetMargin(*pLabel, 0, RELATIVE_LAYOUT_RIGHT_MARGIN, 0, 0);
383         relativeLayout.SetRelation(*pLabel, pItem, RECT_EDGE_RELATION_LEFT_TO_LEFT);
384         relativeLayout.SetRelation(*pLabel, pItem, RECT_EDGE_RELATION_RIGHT_TO_RIGHT);
385
386         Label* pSecondLabel = new (std::nothrow) Label();
387         result r = pSecondLabel->Construct(itemSubRectangle, itemSubText);
388         if (IsFailed(r))
389         {
390                 AppLogDebug("Construct fail [%s]", GetErrorMessage(r));
391                 delete pSecondLabel;
392                 return pItem;
393         }
394
395         pSecondLabel->SetTextHorizontalAlignment(ALIGNMENT_LEFT);
396         pSecondLabel->SetTextVerticalAlignment(ALIGNMENT_TOP);
397         pSecondLabel->SetTextColor(COLOR_SUB_TEXT);
398         pSecondLabel->SetTextConfig(H_GROUP_INDEX_TEXT_GAP, LABEL_TEXT_STYLE_NORMAL);
399
400         pItem->SetEnabled(false);
401         pItem->AddControl(pSecondLabel);
402         relativeLayout.SetMargin(*pSecondLabel, 0, RELATIVE_LAYOUT_RIGHT_MARGIN, 0, 0);
403         relativeLayout.SetRelation(*pSecondLabel, pItem, RECT_EDGE_RELATION_LEFT_TO_LEFT);
404         relativeLayout.SetRelation(*pSecondLabel, pItem, RECT_EDGE_RELATION_RIGHT_TO_RIGHT);
405
406         return pItem;
407 }
408
409 bool
410 CertificateDetailForm::DeleteGroupItem(int groupIndex, Tizen::Ui::Controls::TableViewGroupItem* pItem)
411 {
412         AppLogDebug("ENTER");
413
414         delete pItem;
415         pItem = null;
416
417         return true;
418 }
419
420 bool
421 CertificateDetailForm::DeleteItem(int groupIndex, int itemIndex,Tizen::Ui::Controls::TableViewItem* pItem)
422 {
423         AppLogDebug("ENTER");
424
425         delete pItem;
426         pItem = null;
427
428         return true;
429 }
430
431 void
432 CertificateDetailForm::OnGroupedTableViewItemStateChanged(Tizen::Ui::Controls::GroupedTableView& tableView, int groupIndex, int itemIndex, Tizen::Ui::Controls::TableViewItem* pItem, Tizen::Ui::Controls::TableViewItemStatus status)
433 {
434 }
435
436 int
437 CertificateDetailForm::GetDefaultGroupItemHeight(void)
438 {
439         return H_GROUP_INDEX_NO_TITLE_DEFAULT;
440 }
441
442 int
443 CertificateDetailForm::GetDefaultItemHeight(void)
444 {
445         return H_GROUP_ITEM_DEFAULT;
446 }
447
448 void
449 CertificateDetailForm::UpdateGroupItem(int groupIndex, Tizen::Ui::Controls::TableViewGroupItem* pItem)
450 {
451 }
452
453 void
454 CertificateDetailForm::UpdateItem(int groupIndex, int itemIndex, Tizen::Ui::Controls::TableViewItem* pItem)
455 {
456         if (itemIndex != CERTIFICATE_DETAIL_ITEM_PUBLIC_KEY)
457         {
458                 AppLogDebug("itemIndex is not CERTIFICATE_DETAIL_ITEM_PUBLIC_KEY");
459                 return;
460         }
461
462         Label* pLabel = static_cast<Label*>(pItem->GetControl(CERTIFICATE_DETAIL_ITEM_FIRST_LABEL));
463         if (pLabel == null)
464         {
465                 AppLogDebug("pLabel is null");
466                 return;
467         }
468
469         Label* pSecondLabel = static_cast<Label*>(pItem->GetControl(CERTIFICATE_DETAIL_ITEM_SECOND_LABEL));
470         if (pSecondLabel == null)
471         {
472                 AppLogDebug("pSecondLabel is null");
473                 return;
474         }
475
476         Rectangle clientRect = GetClientAreaBounds();
477         Rectangle itemRect = pItem->GetBounds();
478         Rectangle itemMainRectangle;
479         Rectangle itemSubRectangle;
480         int fontSize = GetFontSize();
481         String groupText = pSecondLabel->GetText();
482
483         ItemTypeTwoLine(itemMainRectangle, itemSubRectangle, fontSize);
484
485         int itemHeight = GetHeightForStringArea(groupText, clientRect.width, H_GROUP_INDEX_TEXT_GAP);
486
487         itemRect.height = (itemMainRectangle.height + itemHeight);
488
489         pSecondLabel->SetBounds(clientRect.x, itemMainRectangle.height, clientRect.width, itemHeight);
490         pItem->SetBounds(itemRect);
491 }
492
493 void
494 CertificateDetailForm::OnGroupedTableViewContextItemActivationStateChanged(Tizen::Ui::Controls::GroupedTableView& tableView, int groupIndex, int itemIndex, Tizen::Ui::Controls::TableViewContextItem* pContextItem, bool activated)
495 {
496 }
497
498 void
499 CertificateDetailForm::OnGroupedTableViewGroupItemStateChanged(Tizen::Ui::Controls::GroupedTableView& tableView, int groupIndex, Tizen::Ui::Controls::TableViewGroupItem* pItem, Tizen::Ui::Controls::TableViewItemStatus status)
500 {
501 }
502
503 result
504 CertificateDetailForm::InitCertificatePresentationModelInstance(void)
505 {
506         __pCertificatePresentationModelInstance = CertificatePresentationModel::GetInstance();
507         if (__pCertificatePresentationModelInstance == null)
508         {
509                 return E_FAILURE;
510         }
511
512         return E_SUCCESS;
513 }
514
515 result
516 CertificateDetailForm::LoadCertificate(void)
517 {
518         __pX509Certificate = dynamic_cast<X509Certificate*>(__pCertificatePresentationModelInstance->PopPendingCertificateN());
519         if (__pX509Certificate == null)
520         {
521                 AppLogDebug("PopPendingCertificateN is null");
522                 return E_FAILURE;
523         }
524
525         return E_SUCCESS;
526 }
527
528 void
529 CertificateDetailForm::OnOrientationChanged(const Tizen::Ui::Control& source, Tizen::Ui::OrientationStatus orientationStatus)
530 {
531         __pTableView->RefreshItem(0, CERTIFICATE_DETAIL_ITEM_PUBLIC_KEY, TABLE_VIEW_REFRESH_TYPE_ITEM_MODIFY);
532         Invalidate(true);
533 }