Fixed N_SE-38563, N_SE-38552
[apps/osp/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->SetBackButton();
53         pFooter->AddActionEventListener(*this);
54
55         SetFormBackEventListener(this);
56 }
57
58 result
59 CertificateTrustedRootForm::OnInitializing(void)
60 {
61         CreateHeader(ResourceManager::GetString(L"IDS_ST_BODY_TRUSTED_ROOT_CERTIFICATES"));
62         CreateFooter();
63
64         result r = InitCertificatePresentationModelInstance();
65         if (r == E_FAILURE)
66         {
67                 __pCertificatePresentationModelInstance = null;
68         }
69
70         if (GetTrustedRootCertificateStoreCount() == 0)
71         {
72                 CreateNoContents();
73         }
74         else
75         {
76                 CreateTableView();
77         }
78         return E_SUCCESS;
79 }
80
81 void
82 CertificateTrustedRootForm::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 CertificateTrustedRootForm::OnTerminating(void)
106 {
107         __pTableView = null;
108
109         SetFormBackEventListener(null);
110         return E_SUCCESS;
111 }
112
113 void
114 CertificateTrustedRootForm::OnSceneActivatedN(const Tizen::Ui::Scenes::SceneId& previousSceneId, const Tizen::Ui::Scenes::SceneId& currentSceneId, Tizen::Base::Collection::IList* pArgs)
115 {
116         if (GetTrustedRootCertificateStoreCount() != 0)
117         {
118                 __pTableView->UpdateTableView();
119         }
120 }
121
122 void
123 CertificateTrustedRootForm::OnSceneDeactivated(const Tizen::Ui::Scenes::SceneId& currentSceneId, const Tizen::Ui::Scenes::SceneId& nextSceneId)
124 {
125 }
126
127 void
128 CertificateTrustedRootForm::OnFormBackRequested(Tizen::Ui::Controls::Form& source)
129 {
130         SceneManager* pSceneManager = SceneManager::GetInstance();
131         AppAssert(pSceneManager);
132
133         pSceneManager->GoBackward(BackwardSceneTransition(SCENE_TRANSITION_ANIMATION_TYPE_RIGHT), null);
134 }
135
136 int
137 CertificateTrustedRootForm::GetGroupCount(void)
138 {
139         AppLogDebug("ENTER");
140         return ID_GROUP_COUNT;
141 }
142
143 int
144 CertificateTrustedRootForm::GetItemCount(int groupIndex)
145 {
146         int itemCount = 0;
147         if (groupIndex == 0)
148         {
149                 itemCount = GetTrustedRootCertificateStoreCount();
150         }
151
152         return itemCount;
153 }
154
155 TableViewGroupItem*
156 CertificateTrustedRootForm::CreateGroupItem(int groupIndex, int itemWidth)
157 {
158         AppLogDebug("ENTER");
159
160         int itemHeight = H_GROUP_INDEX_NO_TITLE_DEFAULT;
161         int yItemOffset = H_GROUP_INDEX_HELP_TEXT_TOP_GAP;
162         LabelTextStyle style = LABEL_TEXT_STYLE_BOLD;
163         Rectangle itemMainRectangle;
164         String groupText;
165         Label* pLabel = null;
166
167         TableViewGroupItem* pItem = new (std::nothrow) TableViewGroupItem();
168
169         yItemOffset = H_GROUP_INDEX_HELP_TEXT_TOP_GAP;
170         itemHeight = H_GROUP_INDEX_NO_TITLE_DEFAULT;
171
172         itemMainRectangle.x = X_GROUP_ITEM_DEFAULT_LABEL;
173         itemMainRectangle.y = yItemOffset;
174         itemMainRectangle.width = itemWidth;
175         itemMainRectangle.height = itemHeight;
176
177         pItem->Construct(Dimension(itemWidth, itemHeight));
178         pItem->SetBackgroundColor(COLOR_BG_GROUP_INDEX_DEFAULT);
179
180         pLabel = new (std::nothrow) Label();
181         pLabel->Construct(itemMainRectangle, groupText);
182         pLabel->SetTextHorizontalAlignment(ALIGNMENT_LEFT);
183         pLabel->SetTextVerticalAlignment(ALIGNMENT_TOP);
184         pLabel->SetTextConfig(FONT_SIZE_GROUP_TITLE_TEXT, style);
185         pLabel->SetTextColor(COLOR_GROUP_TITLE_TEXT);
186
187         pItem->AddControl(pLabel);
188         pItem->SetEnabled(false);
189
190         return pItem;
191 }
192
193 TableViewItem*
194 CertificateTrustedRootForm::CreateItem(int groupIndex, int itemIndex, int itemWidth)
195 {
196         AppLogDebug("GroupIndex %d, ItemIndex %d, ItemWidth %d", groupIndex, itemIndex, itemWidth);
197         Rectangle itemMainRectangle;
198         TableViewAnnexStyle style = TABLE_VIEW_ANNEX_STYLE_NORMAL;
199         int fontSize = GetFontSize();
200
201         String itemMainText = L"";
202         if (__pCertificatePresentationModelInstance != null)
203         {
204                 itemMainText = CertificatePresentationModelUtility::GetCommonName(
205                                 __pCertificatePresentationModelInstance->GetSubjectNameFromCertifcateStore(itemIndex));
206                 if (IsFailed(GetLastResult()))
207                 {
208                         AppLogDebug("GetCommonName failed(%s)", GetErrorMessage(GetLastResult()));
209                         itemMainText = CertificatePresentationModelUtility::GetOrganisation(
210                                         __pCertificatePresentationModelInstance->GetSubjectNameFromCertifcateStore(itemIndex));
211
212                         if (IsFailed(GetLastResult()))
213                         {
214                                 AppLogDebug("GetOrganisation failed(%s)", GetErrorMessage(GetLastResult()));
215                         }
216                 }
217         }
218
219         TableViewItem* pItem = new (std::nothrow) TableViewItem();
220
221         ItemTypeOneLine(itemMainRectangle);
222
223         RelativeLayout relativeLayout;
224         relativeLayout.Construct();
225
226         pItem->Construct(relativeLayout, Dimension(itemWidth, H_GROUP_ITEM_DEFAULT), style);
227         pItem->SetBackgroundColor(COLOR_BG_GROUP_ITEM_DEFAULT);
228
229         Label* pLabel = new (std::nothrow) Label();
230         pLabel->Construct(itemMainRectangle, itemMainText);
231         pLabel->SetTextHorizontalAlignment(ALIGNMENT_LEFT);
232         pLabel->SetTextConfig(fontSize, LABEL_TEXT_STYLE_NORMAL);
233         pLabel->SetTextColor(COLOR_MAIN_TEXT);
234
235         pItem->AddControl(pLabel);
236         relativeLayout.SetMargin(*pLabel, 0, RELATIVE_LAYOUT_RIGHT_MARGIN, 0, 0);
237         relativeLayout.SetRelation(*pLabel, pItem, RECT_EDGE_RELATION_LEFT_TO_LEFT);
238         relativeLayout.SetRelation(*pLabel, pItem, RECT_EDGE_RELATION_RIGHT_TO_RIGHT);
239
240         return pItem;
241 }
242
243 bool
244 CertificateTrustedRootForm::DeleteGroupItem(int groupIndex, TableViewGroupItem* pItem)
245 {
246         AppLogDebug("ENTER");
247
248         delete pItem;
249         pItem = null;
250
251         return true;
252 }
253
254 bool
255 CertificateTrustedRootForm::DeleteItem(int groupIndex, int itemIndex, TableViewItem* pItem)
256 {
257         AppLogDebug("ENTER");
258
259         delete pItem;
260         pItem = null;
261
262         return true;
263 }
264
265 void
266 CertificateTrustedRootForm::OnGroupedTableViewItemStateChanged(Tizen::Ui::Controls::GroupedTableView& tableView, int groupIndex, int itemIndex, Tizen::Ui::Controls::TableViewItem* pItem, Tizen::Ui::Controls::TableViewItemStatus status)
267 {
268         if (__pCertificatePresentationModelInstance == null)
269         {
270                 AppLogDebug("__pCertificatePresentationModelInstance is null");
271                 return;
272         }
273
274         SceneManager* pSceneManager = SceneManager::GetInstance();
275         AppAssert(pSceneManager);
276
277         ICertificate* pICertificate = __pCertificatePresentationModelInstance->GetCertificateFromCertificateStoreN(itemIndex);
278         if (pICertificate == null)
279         {
280                 AppLogDebug("GetCertificateFromCertificateStoreN is null");
281                 return;
282         }
283
284         __pCertificatePresentationModelInstance->ClearPendingCertificate();
285         bool isPending = __pCertificatePresentationModelInstance->PushPendingCertificate(*pICertificate);
286         if (isPending == false)
287         {
288                 AppLogDebug("PushPendingCertificate failed");
289                 return;
290         }
291
292         pSceneManager->GoForward(ForwardSceneTransition(IDSCN_CERTIFICATE_DETAIL, SCENE_TRANSITION_ANIMATION_TYPE_LEFT), null);
293 }
294
295 int
296 CertificateTrustedRootForm::GetDefaultGroupItemHeight(void)
297 {
298         return H_GROUP_INDEX_NO_TITLE_DEFAULT;
299 }
300
301 int
302 CertificateTrustedRootForm::GetDefaultItemHeight(void)
303 {
304         return H_GROUP_ITEM_DEFAULT;
305 }
306
307 void
308 CertificateTrustedRootForm::UpdateGroupItem(int groupIndex, Tizen::Ui::Controls::TableViewGroupItem* pItem)
309 {
310 }
311
312 void
313 CertificateTrustedRootForm::UpdateItem(int groupIndex, int itemIndex, Tizen::Ui::Controls::TableViewItem* pItem)
314 {
315 }
316
317 void
318 CertificateTrustedRootForm::OnGroupedTableViewContextItemActivationStateChanged(Tizen::Ui::Controls::GroupedTableView& tableView, int groupIndex, int itemIndex, Tizen::Ui::Controls::TableViewContextItem* pContextItem, bool activated)
319 {
320 }
321
322 void
323 CertificateTrustedRootForm::OnGroupedTableViewGroupItemStateChanged(Tizen::Ui::Controls::GroupedTableView& tableView, int groupIndex, Tizen::Ui::Controls::TableViewGroupItem* pItem, Tizen::Ui::Controls::TableViewItemStatus status)
324 {
325 }
326
327 result
328 CertificateTrustedRootForm::InitCertificatePresentationModelInstance(void)
329 {
330         __pCertificatePresentationModelInstance = CertificatePresentationModel::GetInstance();
331         if (__pCertificatePresentationModelInstance == null)
332         {
333                 return E_FAILURE;
334         }
335
336         result r = __pCertificatePresentationModelInstance->SetCertificateSelector(ROOT_CA);
337         if (IsFailed(r))
338         {
339                 AppLogDebug("SetCertificateSelector failed(%s", GetErrorMessage(r));
340                 return r;
341         }
342         return __pCertificatePresentationModelInstance->UpdateCertificateListFromCertificateStore();
343 }
344
345 int
346 CertificateTrustedRootForm::GetTrustedRootCertificateStoreCount(void)
347 {
348         if (__pCertificatePresentationModelInstance == null)
349         {
350                 AppLogDebug("__pCertificatePresentationModelInstance is null");
351                 return 0;
352         }
353         return __pCertificatePresentationModelInstance->GetCertificateStoreCount();
354 }
355
356 void
357 CertificateTrustedRootForm::OnOrientationChanged(const Tizen::Ui::Control& source, Tizen::Ui::OrientationStatus orientationStatus)
358 {
359         if (__pTableView == null)
360         {
361                 if (GetTrustedRootCertificateStoreCount() == 0)
362                 {
363                         Label* pLabel = static_cast<Label*>(this->GetControl(NO_CONTENTS, false));
364                         if (pLabel != null)
365                         {
366                                 Rectangle clientRect = GetClientAreaBounds();
367                                 Rectangle labelBound = pLabel->GetBounds();
368
369                                 int xPos = (clientRect.width / LINE_COUNT_2) - (labelBound.width / LINE_COUNT_2);
370                                 int yPos = (clientRect.height / LINE_COUNT_2) -(labelBound.height / LINE_COUNT_2);
371
372                                 pLabel->SetBounds(Rectangle(xPos, yPos, labelBound.width, labelBound.height));
373                                 yPos = yPos + pLabel->GetBounds().height;
374                                 Label* pTextLabel = static_cast<Label*>(this->GetControl(NO_CONTENTS_TEXT, false));
375                                 if (pTextLabel != null)
376                                 {
377                                         pTextLabel->SetBounds(Rectangle(0, yPos, clientRect.width, pTextLabel->GetBounds().height));
378                                 }
379                         }
380                 }
381                 return;
382         }
383 }
384
385 void
386 CertificateTrustedRootForm::CreateNoContents(void)
387 {
388         int controlCount = GetControlCount();
389         for (int i = 0; i < controlCount; i++)
390         {
391                 RemoveControl(GetControl(0));
392         }
393         Label* pLabel = static_cast<Label*>(this->GetControl(NO_CONTENTS, false));
394         if (pLabel == null)
395         {
396                 Rectangle clientRect = GetClientAreaBounds();
397                 Bitmap* pBitmp = ResourceManager::GetBitmapN(IDB_NO_CONTENTS);
398
399                 int bitmapWidth = pBitmp->GetWidth();
400                 int bitmapHeight = pBitmp->GetHeight();
401                 String labelText = ResourceManager::GetString(L"IDS_ST_BODY_NO_CONTENT");
402                 Color textColor = COLOR_HELP_TEXT_TYPE_03_NORMAL;
403                 int noContentTextHeight = GetHeightForStringArea(labelText, bitmapWidth, 32);
404
405                 int xPos = (clientRect.width / LINE_COUNT_2) - (bitmapWidth / DIVIDE_BY_TWO);
406                 int yPos = (clientRect.height / LINE_COUNT_2) - ((bitmapHeight + noContentTextHeight) / DIVIDE_BY_TWO);
407
408                 Label* pLabel = new (std::nothrow) Label();
409                 pLabel->Construct(Rectangle(xPos, yPos, bitmapWidth, bitmapHeight), L"");
410                 pLabel->SetName(NO_CONTENTS);
411                 pLabel->SetBackgroundBitmap(*pBitmp);
412
413                 yPos = yPos + bitmapHeight;
414
415                 Label* pLabelNoContents = new (std::nothrow) Label();
416                 pLabelNoContents->Construct(Rectangle(0, yPos, clientRect.width, noContentTextHeight), L"");
417                 pLabelNoContents->SetName(NO_CONTENTS_TEXT);
418                 pLabelNoContents->SetTextColor(textColor);
419                 pLabelNoContents->SetText(labelText);
420
421                 AddControl(pLabel);
422                 AddControl(pLabelNoContents);
423         }
424 }