Applied latest source code
[apps/native/preloaded/Settings.git] / src / StCertificateTrustedRootForm.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                StCertificateTrustedRootForm.cpp
19  * @brief               This is the implementation file for CertificateTrustedRootForm class.
20  */
21
22 #include "StCertificateTrustedRootForm.h"
23 #include "StResourceManager.h"
24 #include "StSettingScenesList.h"
25 #include "StTypes.h"
26
27 using namespace Tizen::Base;
28 using namespace Tizen::Graphics;
29 using namespace Tizen::Security::Cert;
30 using namespace Tizen::Ui;
31 using namespace Tizen::Ui::Controls;
32 using namespace Tizen::Ui::Scenes;
33
34 static const int ID_GROUP_COUNT = 1;
35
36 CertificateTrustedRootForm::CertificateTrustedRootForm(void)
37         : __pCertificatePresentationModelInstance(null)
38 {
39 }
40
41 CertificateTrustedRootForm::~CertificateTrustedRootForm(void)
42 {
43 }
44
45 void
46 CertificateTrustedRootForm::CreateFooter(void)
47 {
48         Footer* pFooter = GetFooter();
49         AppAssert(pFooter);
50
51         pFooter->SetStyle(FOOTER_STYLE_BUTTON_TEXT);
52         pFooter->AddActionEventListener(*this);
53
54         SetFormBackEventListener(this);
55 }
56
57 result
58 CertificateTrustedRootForm::OnInitializing(void)
59 {
60         CreateHeader(ResourceManager::GetString(L"IDS_ST_BODY_TRUSTED_ROOT_CERTIFICATES"));
61
62         result r = InitCertificatePresentationModelInstance();
63         if (r == E_FAILURE)
64         {
65                 __pCertificatePresentationModelInstance = null;
66         }
67
68         if (GetTrustedRootCertificateStoreCount() == 0)
69         {
70                 CreateNoContents();
71         }
72         else
73         {
74                 CreateTableView();
75         }
76         if (__pTableView != null)
77         {
78                 __pTableView->UpdateTableView();
79         }
80         return E_SUCCESS;
81 }
82
83 void
84 CertificateTrustedRootForm::CreateTableView(void)
85 {
86         Rectangle tableViewBounds = GetClientAreaBounds();
87         tableViewBounds.y = Y_GROUP_DEFAULT;
88
89         __pTableView = new (std::nothrow) GroupedTableView();
90
91         __pTableView->Construct(tableViewBounds, true, TABLE_VIEW_SCROLL_BAR_STYLE_FADE_OUT);
92         __pTableView->SetItemProvider(this);
93
94         AddControl(__pTableView);
95         __pTableView->SetGroupedLookEnabled(true);
96         __pTableView->AddGroupedTableViewItemEventListener(*this);
97         RelativeLayout* pRelativeLayout = dynamic_cast<RelativeLayout*>(this->GetLayoutN());
98         if (pRelativeLayout != null)
99         {
100                 pRelativeLayout->SetHorizontalFitPolicy(*__pTableView, FIT_POLICY_PARENT);
101                 pRelativeLayout->SetVerticalFitPolicy(*__pTableView, FIT_POLICY_PARENT);
102                 delete pRelativeLayout;
103         }
104 }
105
106 result
107 CertificateTrustedRootForm::OnTerminating(void)
108 {
109         __pTableView = null;
110
111         SetFormBackEventListener(null);
112         return E_SUCCESS;
113 }
114
115 void
116 CertificateTrustedRootForm::OnSceneActivatedN(const Tizen::Ui::Scenes::SceneId& previousSceneId, const Tizen::Ui::Scenes::SceneId& currentSceneId, Tizen::Base::Collection::IList* pArgs)
117 {
118         if (GetTrustedRootCertificateStoreCount() != 0 && previousSceneId == IDSCN_CERTIFICATE_DETAIL)
119         {
120                 __pTableView->RefreshAllItems();
121         }
122 }
123
124 void
125 CertificateTrustedRootForm::OnSceneDeactivated(const Tizen::Ui::Scenes::SceneId& currentSceneId, const Tizen::Ui::Scenes::SceneId& nextSceneId)
126 {
127 }
128
129 void
130 CertificateTrustedRootForm::OnFormBackRequested(Tizen::Ui::Controls::Form& source)
131 {
132         SceneManager* pSceneManager = SceneManager::GetInstance();
133         AppAssert(pSceneManager);
134
135         pSceneManager->GoBackward(BackwardSceneTransition(SCENE_TRANSITION_ANIMATION_TYPE_RIGHT), null);
136 }
137
138 int
139 CertificateTrustedRootForm::GetGroupCount(void)
140 {
141         AppLogDebug("ENTER");
142         return ID_GROUP_COUNT;
143 }
144
145 int
146 CertificateTrustedRootForm::GetItemCount(int groupIndex)
147 {
148         int itemCount = 0;
149         if (groupIndex == 0)
150         {
151                 itemCount = GetTrustedRootCertificateStoreCount();
152         }
153
154         return itemCount;
155 }
156
157 TableViewGroupItem*
158 CertificateTrustedRootForm::CreateGroupItem(int groupIndex, int itemWidth)
159 {
160         AppLogDebug("ENTER");
161
162         int itemHeight = H_GROUP_INDEX_NO_TITLE_DEFAULT;
163         int yItemOffset = H_GROUP_INDEX_HELP_TEXT_TOP_GAP;
164         LabelTextStyle style = LABEL_TEXT_STYLE_BOLD;
165         Rectangle itemMainRectangle;
166         String groupText;
167         Label* pLabel = null;
168
169         TableViewGroupItem* pItem = new (std::nothrow) TableViewGroupItem();
170
171         yItemOffset = H_GROUP_INDEX_HELP_TEXT_TOP_GAP;
172         itemHeight = H_GROUP_INDEX_NO_TITLE_DEFAULT;
173
174         itemMainRectangle.x = X_GROUP_ITEM_DEFAULT_LABEL;
175         itemMainRectangle.y = yItemOffset;
176         itemMainRectangle.width = itemWidth;
177         itemMainRectangle.height = itemHeight;
178
179         pItem->Construct(Dimension(itemWidth, itemHeight));
180         pItem->SetBackgroundColor(COLOR_BG_GROUP_INDEX_DEFAULT);
181
182         pLabel = new (std::nothrow) Label();
183         pLabel->Construct(itemMainRectangle, groupText);
184         pLabel->SetTextHorizontalAlignment(ALIGNMENT_LEFT);
185         pLabel->SetTextVerticalAlignment(ALIGNMENT_TOP);
186         pLabel->SetTextConfig(FONT_SIZE_GROUP_TITLE_TEXT, style);
187         pLabel->SetTextColor(COLOR_GROUP_TITLE_TEXT);
188
189         pItem->AddControl(pLabel);
190         pItem->SetEnabled(false);
191
192         return pItem;
193 }
194
195 TableViewItem*
196 CertificateTrustedRootForm::CreateItem(int groupIndex, int itemIndex, int itemWidth)
197 {
198         AppLogDebug("GroupIndex %d, ItemIndex %d, ItemWidth %d", groupIndex, itemIndex, itemWidth);
199         Rectangle itemMainRectangle;
200         TableViewAnnexStyle style = TABLE_VIEW_ANNEX_STYLE_NORMAL;
201         int fontSize = GetFontSize();
202
203         String itemMainText = L"";
204         if (__pCertificatePresentationModelInstance != null)
205         {
206                 itemMainText = CertificatePresentationModelUtility::GetCommonName(
207                                 __pCertificatePresentationModelInstance->GetSubjectNameFromCertifcateStore(itemIndex));
208                 if (IsFailed(GetLastResult()))
209                 {
210                         AppLogDebug("GetCommonName failed(%s)", GetErrorMessage(GetLastResult()));
211                         itemMainText = CertificatePresentationModelUtility::GetOrganisation(
212                                         __pCertificatePresentationModelInstance->GetSubjectNameFromCertifcateStore(itemIndex));
213
214                         if (IsFailed(GetLastResult()))
215                         {
216                                 AppLogDebug("GetOrganisation failed(%s)", GetErrorMessage(GetLastResult()));
217                         }
218                 }
219         }
220
221         TableViewItem* pItem = new (std::nothrow) TableViewItem();
222
223         ItemTypeOneLine(itemMainRectangle);
224
225         RelativeLayout relativeLayout;
226         relativeLayout.Construct();
227
228         pItem->Construct(relativeLayout, Dimension(itemWidth, H_GROUP_ITEM_DEFAULT), style);
229         pItem->SetBackgroundColor(COLOR_BG_GROUP_ITEM_DEFAULT);
230
231         Label* pLabel = new (std::nothrow) Label();
232         pLabel->Construct(itemMainRectangle, itemMainText);
233         pLabel->SetTextHorizontalAlignment(ALIGNMENT_LEFT);
234         pLabel->SetTextConfig(fontSize, LABEL_TEXT_STYLE_NORMAL);
235         pLabel->SetTextColor(COLOR_MAIN_TEXT);
236
237         pItem->AddControl(pLabel);
238         relativeLayout.SetMargin(*pLabel, 0, RELATIVE_LAYOUT_RIGHT_MARGIN, 0, 0);
239         relativeLayout.SetRelation(*pLabel, pItem, RECT_EDGE_RELATION_LEFT_TO_LEFT);
240         relativeLayout.SetRelation(*pLabel, pItem, RECT_EDGE_RELATION_RIGHT_TO_RIGHT);
241
242         return pItem;
243 }
244
245 bool
246 CertificateTrustedRootForm::DeleteGroupItem(int groupIndex, TableViewGroupItem* pItem)
247 {
248         AppLogDebug("ENTER");
249
250         delete pItem;
251         pItem = null;
252
253         return true;
254 }
255
256 bool
257 CertificateTrustedRootForm::DeleteItem(int groupIndex, int itemIndex, TableViewItem* pItem)
258 {
259         AppLogDebug("ENTER");
260
261         delete pItem;
262         pItem = null;
263
264         return true;
265 }
266
267 void
268 CertificateTrustedRootForm::OnGroupedTableViewItemStateChanged(Tizen::Ui::Controls::GroupedTableView& tableView, int groupIndex, int itemIndex, Tizen::Ui::Controls::TableViewItem* pItem, Tizen::Ui::Controls::TableViewItemStatus status)
269 {
270         if (__pCertificatePresentationModelInstance == null)
271         {
272                 AppLogDebug("__pCertificatePresentationModelInstance is null");
273                 return;
274         }
275
276         SceneManager* pSceneManager = SceneManager::GetInstance();
277         AppAssert(pSceneManager);
278
279         ICertificate* pICertificate = __pCertificatePresentationModelInstance->GetCertificateFromCertificateStoreN(itemIndex);
280         if (pICertificate == null)
281         {
282                 AppLogDebug("GetCertificateFromCertificateStoreN is null");
283                 return;
284         }
285
286         __pCertificatePresentationModelInstance->ClearPendingCertificate();
287         bool isPending = __pCertificatePresentationModelInstance->PushPendingCertificate(*pICertificate);
288         if (isPending == false)
289         {
290                 AppLogDebug("PushPendingCertificate failed");
291                 return;
292         }
293
294         pSceneManager->GoForward(ForwardSceneTransition(IDSCN_CERTIFICATE_DETAIL, SCENE_TRANSITION_ANIMATION_TYPE_LEFT), null);
295 }
296
297 int
298 CertificateTrustedRootForm::GetDefaultGroupItemHeight(void)
299 {
300         return H_GROUP_INDEX_NO_TITLE_DEFAULT;
301 }
302
303 int
304 CertificateTrustedRootForm::GetDefaultItemHeight(void)
305 {
306         return H_GROUP_ITEM_DEFAULT;
307 }
308
309 void
310 CertificateTrustedRootForm::UpdateGroupItem(int groupIndex, Tizen::Ui::Controls::TableViewGroupItem* pItem)
311 {
312         AppLogDebug("ENTER");
313         LabelTextStyle style = LABEL_TEXT_STYLE_BOLD;
314         Rectangle itemMainRectangle;
315
316         itemMainRectangle =  pItem->GetBounds();
317         itemMainRectangle.width = GetClientAreaBounds().width;
318
319         pItem->SetBounds(itemMainRectangle);
320         pItem->SetBackgroundColor(COLOR_BG_GROUP_INDEX_DEFAULT);
321
322         Label* pLabel = static_cast<Label*>(pItem->GetControl(0));
323         itemMainRectangle = pLabel->GetBounds();
324         itemMainRectangle.width = GetClientAreaBounds().width;
325         pLabel->SetBounds(itemMainRectangle);
326         pLabel->SetTextHorizontalAlignment(ALIGNMENT_LEFT);
327         pLabel->SetTextVerticalAlignment(ALIGNMENT_TOP);
328         pLabel->SetTextConfig(FONT_SIZE_GROUP_TITLE_TEXT, style);
329         pLabel->SetTextColor(COLOR_GROUP_TITLE_TEXT);
330         pItem->SetEnabled(false);
331         pLabel->Invalidate(false);
332         pItem->Invalidate(false);
333 }
334
335 void
336 CertificateTrustedRootForm::UpdateItem(int groupIndex, int itemIndex, Tizen::Ui::Controls::TableViewItem* pItem)
337 {
338         AppLogDebug("GroupIndex %d, ItemIndex %d", groupIndex, itemIndex);
339         Rectangle itemMainRectangle;
340         Rectangle itemRectangle = pItem->GetBounds();
341         int fontSize = GetFontSize();
342
343         String itemMainText = L"";
344         if (__pCertificatePresentationModelInstance != null)
345         {
346                 itemMainText = CertificatePresentationModelUtility::GetCommonName(
347                                 __pCertificatePresentationModelInstance->GetSubjectNameFromCertifcateStore(itemIndex));
348                 if (IsFailed(GetLastResult()))
349                 {
350                         AppLogDebug("GetCommonName failed(%s)", GetErrorMessage(GetLastResult()));
351                         itemMainText = CertificatePresentationModelUtility::GetOrganisation(
352                                         __pCertificatePresentationModelInstance->GetSubjectNameFromCertifcateStore(itemIndex));
353
354                         if (IsFailed(GetLastResult()))
355                         {
356                                 AppLogDebug("GetOrganisation failed(%s)", GetErrorMessage(GetLastResult()));
357                         }
358                 }
359         }
360
361         itemMainRectangle =  pItem->GetBounds();
362         itemMainRectangle.width = GetClientAreaBounds().width;
363
364         pItem->SetBounds(itemMainRectangle);
365         pItem->SetBackgroundColor(COLOR_BG_GROUP_ITEM_DEFAULT);
366
367         Label* pLabel = static_cast<Label*>(pItem->GetControl(0));
368         if (pLabel)
369         {
370                 itemMainRectangle = pLabel->GetBounds();
371                 itemMainRectangle.width = GetClientAreaBounds().width;
372                 pLabel->SetBounds(itemMainRectangle);
373                 pLabel->SetText(itemMainText);
374                 pLabel->SetTextHorizontalAlignment(ALIGNMENT_LEFT);
375                 pLabel->SetTextConfig(fontSize, LABEL_TEXT_STYLE_NORMAL);
376                 pLabel->SetTextColor(COLOR_MAIN_TEXT);
377         }       
378 }
379
380 void
381 CertificateTrustedRootForm::OnGroupedTableViewContextItemActivationStateChanged(Tizen::Ui::Controls::GroupedTableView& tableView, int groupIndex, int itemIndex, Tizen::Ui::Controls::TableViewContextItem* pContextItem, bool activated)
382 {
383 }
384
385 void
386 CertificateTrustedRootForm::OnGroupedTableViewGroupItemStateChanged(Tizen::Ui::Controls::GroupedTableView& tableView, int groupIndex, Tizen::Ui::Controls::TableViewGroupItem* pItem, Tizen::Ui::Controls::TableViewItemStatus status)
387 {
388 }
389
390 result
391 CertificateTrustedRootForm::InitCertificatePresentationModelInstance(void)
392 {
393         __pCertificatePresentationModelInstance = CertificatePresentationModel::GetInstance();
394         if (__pCertificatePresentationModelInstance == null)
395         {
396                 return E_FAILURE;
397         }
398
399         result r = __pCertificatePresentationModelInstance->SetCertificateSelector(ROOT_CA);
400         if (IsFailed(r))
401         {
402                 AppLogDebug("SetCertificateSelector failed(%s", GetErrorMessage(r));
403                 return E_FAILURE;
404         }
405         return __pCertificatePresentationModelInstance->UpdateCertificateListFromCertificateStore();
406 }
407
408 int
409 CertificateTrustedRootForm::GetTrustedRootCertificateStoreCount(void)
410 {
411         if (__pCertificatePresentationModelInstance == null)
412         {
413                 AppLogDebug("__pCertificatePresentationModelInstance is null");
414                 return 0;
415         }
416         return __pCertificatePresentationModelInstance->GetCertificateStoreCount();
417 }
418
419 void
420 CertificateTrustedRootForm::OnOrientationChanged(const Tizen::Ui::Control& source, Tizen::Ui::OrientationStatus orientationStatus)
421 {
422         if (__pTableView == null)
423         {
424                 if (GetTrustedRootCertificateStoreCount() == 0)
425                 {
426                         Label* pLabel = static_cast<Label*>(this->GetControl(NO_CONTENTS, false));
427                         if (pLabel != null)
428                         {
429                                 Rectangle clientRect = GetClientAreaBounds();
430                                 Rectangle labelBound = pLabel->GetBounds();
431
432                                 int xPos = (clientRect.width / LINE_COUNT_2) - (labelBound.width / LINE_COUNT_2);
433                                 int yPos = (clientRect.height / LINE_COUNT_2) -(labelBound.height / LINE_COUNT_2);
434
435                                 pLabel->SetBounds(Rectangle(xPos, yPos, labelBound.width, labelBound.height));
436                                 yPos = yPos + pLabel->GetBounds().height;
437                                 Label* pTextLabel = static_cast<Label*>(this->GetControl(NO_CONTENTS_TEXT, false));
438                                 if (pTextLabel != null)
439                                 {
440                                         pTextLabel->SetBounds(Rectangle(0, yPos, clientRect.width, pTextLabel->GetBounds().height));
441                                 }
442                         }
443                 }
444                 return;
445         }
446         __pTableView->RefreshAllItems();
447 }
448
449 void
450 CertificateTrustedRootForm::CreateNoContents(void)
451 {
452         int controlCount = GetControlCount();
453         for (int i = 0; i < controlCount; i++)
454         {
455                 RemoveControl(GetControl(0));
456         }
457         Label* pLabel = static_cast<Label*>(this->GetControl(NO_CONTENTS, false));
458         if (pLabel == null)
459         {
460                 Rectangle clientRect = GetClientAreaBounds();
461                 Bitmap* pBitmp = ResourceManager::GetBitmapN(IDB_NO_CONTENTS);
462
463                 int bitmapWidth = pBitmp->GetWidth();
464                 int bitmapHeight = pBitmp->GetHeight();
465                 String labelText = ResourceManager::GetString(L"IDS_ST_BODY_NO_CONTENT");
466                 Color textColor = COLOR_HELP_TEXT_TYPE_03_NORMAL;
467                 int noContentTextHeight = GetHeightForStringArea(labelText, bitmapWidth, 32);
468
469                 int xPos = (clientRect.width / LINE_COUNT_2) - (bitmapWidth / DIVIDE_BY_TWO);
470                 int yPos = (clientRect.height / LINE_COUNT_2) - ((bitmapHeight + noContentTextHeight) / DIVIDE_BY_TWO);
471
472                 Label* pLabel = new (std::nothrow) Label();
473                 pLabel->Construct(Rectangle(xPos, yPos, bitmapWidth, bitmapHeight), L"");
474                 pLabel->SetName(NO_CONTENTS);
475                 pLabel->SetBackgroundBitmap(*pBitmp);
476
477                 yPos = yPos + bitmapHeight;
478
479                 Label* pLabelNoContents = new (std::nothrow) Label();
480                 pLabelNoContents->Construct(Rectangle(0, yPos, clientRect.width, noContentTextHeight), L"");
481                 pLabelNoContents->SetName(NO_CONTENTS_TEXT);
482                 pLabelNoContents->SetTextColor(textColor);
483                 pLabelNoContents->SetText(labelText);
484
485                 AddControl(pLabel);
486                 AddControl(pLabelNoContents);
487         }
488 }