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