Tizen 2.1 base
[platform/framework/native/app-controls.git] / src / certmgr-app-control / CertificateListForm.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16 //
17
18 /**
19  * @file        CertificateListForm.cpp
20  * @brief       This file contains the implementation of CertificateListForm class.
21  */
22
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"
40
41
42 using namespace Tizen::Base;
43 using namespace Tizen::Ui::Controls;
44 using namespace Tizen::Graphics;
45 using namespace Tizen::Security::Cert;
46
47 extern "C"
48 {
49 void Finish(bool isSelected, int certId);
50 }
51
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";
57
58 CertificateListForm::CertificateListForm(void)
59         : __pCertList(null)
60         , __pCertIndex(null)
61         , __certCount(0)
62         , __itemLeftMargin(ITEM_LEFT_MARGIN)
63         , __itemHeight(ITEM_HEIGHT)
64 {
65 }
66
67 CertificateListForm::~CertificateListForm(void)
68 {
69 }
70
71 result
72 CertificateListForm::Initialize(void)
73 {
74         result r = E_SUCCESS;
75
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.");
78
79         return r;
80 }
81
82 result
83 CertificateListForm::OnInitializing(void)
84 {
85         result r = E_SUCCESS;
86
87         int count = 0;
88         int certCount = 0;
89         int invalidCount = 0;
90         Footer* pFooter = null;
91
92         r = SetTitleText(TITLE_STRING, ALIGNMENT_LEFT);
93         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
94
95         // footer
96         pFooter = GetFooter();
97         SysTryReturnResult(NID_SEC_CERT, pFooter != null, E_SYSTEM, "Footer instance is not available.");
98
99         r = pFooter->SetStyle(FOOTER_STYLE_SEGMENTED_TEXT);
100         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
101
102         r = pFooter->SetBackButton();
103         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
104
105         SetFormBackEventListener(this);
106
107         // load user certificates
108         _CertFieldInfos* pRawCertList = null;
109         certCount = _CertService::GetUserCertFieldInfoN(pRawCertList);
110         r = GetLastResult();
111         std::unique_ptr<_CertFieldInfos[]> pCertList(pRawCertList);
112         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
113
114         // trivial condition
115         SysAssertf(((certCount > 0) && (pCertList != null)) || ((certCount == 0) && (pCertList == null))
116                 , "A system error has been occurred. Because invalid cert count was returned.");
117
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.");
121
122         // assign number of certificate which is valid(certTitle or subjectName must be exist)
123         for (count = 0; count < certCount; count++)
124         {
125                 if (!pCertList[count].certTitle[0] && !pCertList[count].subjectName[0])
126                 {
127                         invalidCount++;
128                         continue;
129                 }
130                 pCertIndex[count - invalidCount] = count;
131         }
132         __certCount = certCount - invalidCount;
133
134         // listview
135         std::unique_ptr<ListView> pListView(new (std::nothrow) ListView());
136         SysTryReturnResult(NID_SEC_CERT, pListView != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
137
138         if (__itemHeight == ITEM_HEIGHT && __itemLeftMargin == ITEM_LEFT_MARGIN)
139         {
140                 double ratio = 1.0;
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);
145
146                 ratio = logicalWidth / HD_RESOLUTION_WIDTH;
147
148                 // __itemHeight : 112 = logicalWidth : HD_RESOLUTION_WIDTH(720)
149                 __itemHeight = static_cast<int>(__itemHeight * ratio);
150
151                 // __itemLeftMargin : 26 = logicalWidth : HD_RESOLUTION_WIDTH(720)
152                 __itemLeftMargin = static_cast<int>(__itemLeftMargin * ratio);
153         }
154
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));
158
159         r = pListView->SetItemProvider(*this);
160         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
161
162         r = pListView->SetTextOfEmptyList(EMPTY_LIST_STRING);
163         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
164
165         pListView->AddListViewItemEventListener(*this);
166
167         r = AddControl(*(pListView.release()));
168         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
169
170
171         __pCertIndex = std::move(pCertIndex);
172         __pCertList = std::move(pCertList);
173
174         return r;
175 }
176
177 result
178 CertificateListForm::OnTerminating(void)
179 {
180         return E_SUCCESS;
181 }
182
183 // IFormBackEventListener
184 void
185 CertificateListForm::OnFormBackRequested(Form& source)
186 {
187         Finish(false, -1);
188 }
189
190 // IListViewItemProvider
191 ListItemBase*
192 CertificateListForm::CreateItem(int index, int itemWidth)
193 {
194         result r = E_SUCCESS;
195         int validIndex = 0;
196         ListAnnexStyle style = LIST_ANNEX_STYLE_NORMAL;
197
198         Rectangle rect(__itemLeftMargin, 0, itemWidth - (__itemLeftMargin * 2), __itemHeight);
199         Dimension dim(itemWidth, __itemHeight);
200
201         SysLog(NID_SEC_CERT, "The current value of index is %d", index);
202
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);
208
209         // make item
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));
212
213         r = pItem->Construct(dim, style);
214         SysTryReturn(NID_SEC_CERT, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
215
216         // prevent overflow
217         __pCertList[validIndex].certTitle[_MAX_ISSUER_SUBJECT_NAME_SIZE] = '\0';
218         __pCertList[validIndex].subjectName[_MAX_ISSUER_SUBJECT_NAME_SIZE] = '\0';
219
220         SysLog(NID_SEC_CERT, "The current value of certFileId is %d", __pCertList[validIndex].certFileId);
221
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));
228         }
229         else
230         {       // it is not reached because it already checked by OnInitializing method.
231                 SysAssertf(__pCertList[validIndex].subjectName[0], "_CertFieldInfos instance must not be null.");
232
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));
237         }
238
239         return pItem.release();
240 }
241
242 bool
243 CertificateListForm::DeleteItem(int index, ListItemBase* pItem, int itemWidth)
244 {
245         delete pItem;
246         return true;
247 }
248
249 int
250 CertificateListForm::GetItemCount(void)
251 {
252         SysLog(NID_SEC_CERT, "The current value of __certCount is %d", __certCount);
253         return __certCount;
254 }
255
256 // IListViewItemEventListener
257 void
258 CertificateListForm::OnListViewItemStateChanged(ListView &listView, int index, int elementId, ListItemStatus status)
259 {
260         SysLog(NID_SEC_CERT, "The current value of elementId(certFileId) is %d", elementId);
261         Finish(true, elementId);
262 }
263
264 void
265 CertificateListForm::OnListViewContextItemStateChanged(ListView& listView, int index, int elementId, ListContextItemStatus state)
266 {
267 }
268
269 void
270 CertificateListForm::OnListViewItemSwept(ListView& listView, int index, SweepDirection direction)
271 {
272 }