2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
19 * @file CertificateListForm.cpp
20 * @brief This file contains the implementation of CertificateListForm class.
23 #include <unique_ptr.h>
24 #include <FBaseDataType.h>
25 #include <FBaseErrors.h>
26 #include <FBaseErrorDefine.h>
27 #include <FBaseString.h>
28 #include <FBaseSysLog.h>
29 #include <FGrpDimension.h>
30 #include <FGrpRectangle.h>
31 #include <FUiCtrlCustomItem.h>
32 #include <FUiCtrlFooter.h>
33 #include <FUiCtrlForm.h>
34 #include <FUiCtrlFooter.h>
35 #include <FUiCtrlListItemBase.h>
36 #include <FUiCtrlListView.h>
37 #include <FSecCert_CertService.h>
38 #include <FSecCert_CertTypes.h>
39 #include "CertificateListForm.h"
42 using namespace Tizen::Base;
43 using namespace Tizen::Ui::Controls;
44 using namespace Tizen::Graphics;
45 using namespace Tizen::Security::Cert;
49 void Finish(bool isSelected, int certId);
52 static const int HD_RESOLUTION_WIDTH = 720;
53 static const int ITEM_HEIGHT = 112; // in 720X1280px resolution
54 static const int ITEM_LEFT_MARGIN = 26; // in 720X1280px resolution
55 static const wchar_t EMPTY_LIST_STRING[] = L"No Certificate";
56 static const wchar_t TITLE_STRING[] = L"USER CERTIFICATES";
58 CertificateListForm::CertificateListForm(void)
62 , __itemLeftMargin(ITEM_LEFT_MARGIN)
63 , __itemHeight(ITEM_HEIGHT)
67 CertificateListForm::~CertificateListForm(void)
72 CertificateListForm::Initialize(void)
76 r = Construct(FORM_STYLE_INDICATOR | FORM_STYLE_TITLE | FORM_STYLE_FOOTER);
77 SysTryReturnResult(NID_SEC_CERT, !IsFailed(r), E_SYSTEM, "Failed to construct CertificateListForm.");
83 CertificateListForm::OnInitializing(void)
90 Footer* pFooter = null;
92 r = SetTitleText(TITLE_STRING, ALIGNMENT_LEFT);
93 SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
96 pFooter = GetFooter();
97 SysTryReturnResult(NID_SEC_CERT, pFooter != null, E_SYSTEM, "Footer instance is not available.");
99 r = pFooter->SetStyle(FOOTER_STYLE_SEGMENTED_TEXT);
100 SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
102 r = pFooter->SetBackButton();
103 SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
105 SetFormBackEventListener(this);
107 // load user certificates
108 _CertFieldInfos* pRawCertList = null;
109 certCount = _CertService::GetUserCertFieldInfoN(pRawCertList);
111 std::unique_ptr<_CertFieldInfos[]> pCertList(pRawCertList);
112 SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
115 SysAssertf(((certCount > 0) && (pCertList != null)) || ((certCount == 0) && (pCertList == null))
116 , "A system error has been occurred. Because invalid cert count was returned.");
118 // cert index : count only for valid certificate
119 std::unique_ptr<int[]> pCertIndex(new (std::nothrow) int[certCount]);
120 SysTryReturnResult(NID_SEC_CERT, pCertIndex != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
122 // assign number of certificate which is valid(certTitle or subjectName must be exist)
123 for (count = 0; count < certCount; count++)
125 if (!pCertList[count].certTitle[0] && !pCertList[count].subjectName[0])
130 pCertIndex[count - invalidCount] = count;
132 __certCount = certCount - invalidCount;
135 std::unique_ptr<ListView> pListView(new (std::nothrow) ListView());
136 SysTryReturnResult(NID_SEC_CERT, pListView != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
138 if (__itemHeight == ITEM_HEIGHT && __itemLeftMargin == ITEM_LEFT_MARGIN)
141 double logicalWidth = HD_RESOLUTION_WIDTH;
142 Dimension tempDim = CoordinateSystem::GetLogicalResolution();
143 logicalWidth = tempDim.width;
144 SysAssertf(logicalWidth > 0.0, "invalid logical resolution (%lf)", logicalWidth);
146 ratio = logicalWidth / HD_RESOLUTION_WIDTH;
148 // __itemHeight : 112 = logicalWidth : HD_RESOLUTION_WIDTH(720)
149 __itemHeight = static_cast<int>(__itemHeight * ratio);
151 // __itemLeftMargin : 26 = logicalWidth : HD_RESOLUTION_WIDTH(720)
152 __itemLeftMargin = static_cast<int>(__itemLeftMargin * ratio);
155 Rectangle rect(0, 0, GetClientAreaBounds().width, GetClientAreaBounds().height);
156 r = pListView->Construct(rect, true, false);
157 SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
159 r = pListView->SetItemProvider(*this);
160 SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
162 r = pListView->SetTextOfEmptyList(EMPTY_LIST_STRING);
163 SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
165 pListView->AddListViewItemEventListener(*this);
167 r = AddControl(*(pListView.release()));
168 SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
171 __pCertIndex = std::move(pCertIndex);
172 __pCertList = std::move(pCertList);
178 CertificateListForm::OnTerminating(void)
183 // IFormBackEventListener
185 CertificateListForm::OnFormBackRequested(Form& source)
190 // IListViewItemProvider
192 CertificateListForm::CreateItem(int index, int itemWidth)
194 result r = E_SUCCESS;
196 ListAnnexStyle style = LIST_ANNEX_STYLE_NORMAL;
198 Rectangle rect(__itemLeftMargin, 0, itemWidth - (__itemLeftMargin * 2), __itemHeight);
199 Dimension dim(itemWidth, __itemHeight);
201 SysLog(NID_SEC_CERT, "The current value of index is %d", index);
203 // convert to valid index
204 SysAssertf((index >= 0) && (index < __certCount), "index (%d) is out of range.", index);
205 SysAssertf(__pCertIndex != null, "__pCertIndex instance must not be null.");
206 validIndex = __pCertIndex[index];
207 SysLog(NID_SEC_CERT, "The current value of validIndex is %d", validIndex);
210 std::unique_ptr<CustomItem> pItem(new (std::nothrow) CustomItem());
211 SysTryReturn(NID_SEC_CERT, pItem != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
213 r = pItem->Construct(dim, style);
214 SysTryReturn(NID_SEC_CERT, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
217 __pCertList[validIndex].certTitle[_MAX_ISSUER_SUBJECT_NAME_SIZE] = '\0';
218 __pCertList[validIndex].subjectName[_MAX_ISSUER_SUBJECT_NAME_SIZE] = '\0';
220 SysLog(NID_SEC_CERT, "The current value of certFileId is %d", __pCertList[validIndex].certFileId);
222 // add to item, certFileId become elementId
223 if (__pCertList[validIndex].certTitle[0])
224 { // it has valid certTitle
225 SysLog(NID_SEC_CERT, "The current value of certTitle is %s", __pCertList[validIndex].certTitle);
226 r = pItem->AddElement(rect, __pCertList[validIndex].certFileId, __pCertList[validIndex].certTitle);
227 SysTryReturn(NID_SEC_CERT, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
230 { // it is not reached because it already checked by OnInitializing method.
231 SysAssertf(__pCertList[validIndex].subjectName[0], "_CertFieldInfos instance must not be null.");
233 // it has valid subjectName
234 SysLog(NID_SEC_CERT, "The current value of subjectName is %s", __pCertList[validIndex].subjectName);
235 r = pItem->AddElement(rect, __pCertList[validIndex].certFileId, __pCertList[validIndex].subjectName);
236 SysTryReturn(NID_SEC_CERT, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
239 return pItem.release();
243 CertificateListForm::DeleteItem(int index, ListItemBase* pItem, int itemWidth)
250 CertificateListForm::GetItemCount(void)
252 SysLog(NID_SEC_CERT, "The current value of __certCount is %d", __certCount);
256 // IListViewItemEventListener
258 CertificateListForm::OnListViewItemStateChanged(ListView &listView, int index, int elementId, ListItemStatus status)
260 SysLog(NID_SEC_CERT, "The current value of elementId(certFileId) is %d", elementId);
261 Finish(true, elementId);
265 CertificateListForm::OnListViewContextItemStateChanged(ListView& listView, int index, int elementId, ListContextItemStatus state)
270 CertificateListForm::OnListViewItemSwept(ListView& listView, int index, SweepDirection direction)