94c031da7efe7004a936a1eb22c0fae498a6f6e1
[apps/osp/CertificateSelector.git] / src / CertificateSelectorForm.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        CertificateSelectorForm.cpp
20  * @brief       This is the implementation for the %CertificateSelectorForm class.
21  */
22
23 #include <unique_ptr.h>
24 #include <FAppUiApp.h>
25 #include <FAppAppControlProviderManager.h>
26 #include <FBaseDataType.h>
27 #include <FBaseErrors.h>
28 #include <FBaseString.h>
29 #include <FBaseLog.h>
30 #include <FBaseColHashMap.h>
31 #include <FGrpDimension.h>
32 #include <FGrpRectangle.h>
33 #include <FUiCtrlCustomItem.h>
34 #include <FUiCtrlFooter.h>
35 #include <FUiCtrlForm.h>
36 #include <FUiCtrlFooter.h>
37 #include <FUiCtrlListItemBase.h>
38 #include <FUiCtrlListView.h>
39 #include <FSecCert_CertService.h>
40 #include <FSecCert_CertTypes.h>
41 #include "CertificateSelectorForm.h"
42
43 using namespace Tizen::App;
44 using namespace Tizen::Base;
45 using namespace Tizen::Base::Collection;
46 using namespace Tizen::Ui::Controls;
47 using namespace Tizen::Graphics;
48 using namespace Tizen::Security::Cert;
49
50 static const int HD_RESOLUTION_WIDTH = 720;
51 static const int ITEM_HEIGHT = 112; // in 720X1280px resolution
52 static const int ITEM_LEFT_MARGIN = 26; // in 720X1280px resolution
53 static const wchar_t EMPTY_LIST_STRING[] = L"No Certificate";
54 static const wchar_t TITLE_STRING[] = L"USER CERTIFICATES";
55
56 CertificateSelectorForm::CertificateSelectorForm(void)
57         : __reqId(-1)
58         , __itemLeftMargin(ITEM_LEFT_MARGIN)
59         , __itemHeight(ITEM_HEIGHT)
60         , __certCount(0)
61         , __pCertList(null)
62         , __pCertIndex(null)
63 {
64 }
65
66 CertificateSelectorForm::~CertificateSelectorForm(void)
67 {
68 }
69
70 void
71 CertificateSelectorForm::Finish(bool isSelected, int certId)
72 {
73         result r = E_SUCCESS;
74
75         AppLog("The current value of certId is %d.", certId);
76
77         UiApp* pApp = UiApp::GetInstance();
78         AppAssert(pApp);
79
80         std::unique_ptr<HashMap, AllElementsDeleter> pMap(new (std::nothrow) HashMap());
81         TryCatchResult(pMap != null, , E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
82         r = pMap->Construct(2);
83         TryCatchResult(!IsFailed(r), , r, "[%s] Propagating.", GetErrorMessage(r));
84
85         if (isSelected)
86         {
87                 AppLog("The current value of isSelected is true.");
88                 TryCatchResult(certId >= 0, , E_INVALID_ARG, "[%s] Invalid Certificate ID.", GetErrorMessage(E_INVALID_ARG));
89
90                 pMap->Add(new (std::nothrow) String(L"Id"), new (std::nothrow) String(Integer(certId).ToString()));
91                 TryCatchResult(!IsFailed(r), , r, "[%s] Propagating.", GetErrorMessage(r));
92
93                 // send result
94                 AppControlProviderManager::GetInstance()->SendAppControlResult(__reqId, APP_CTRL_RESULT_SUCCEEDED, pMap.release());
95         }
96         else
97         {
98                 AppLog("The current value of isSelected is false.");
99
100                 // send result
101                 AppControlProviderManager::GetInstance()->SendAppControlResult(__reqId, APP_CTRL_RESULT_CANCELED, null);
102         }
103
104         pApp->Terminate();
105         return;
106
107 CATCH:
108         AppControlProviderManager::GetInstance()->SendAppControlResult(__reqId, APP_CTRL_RESULT_FAILED, null);
109         pApp->Terminate();
110 }
111
112 result
113 CertificateSelectorForm::Initialize(RequestId reqId)
114 {
115         result r = E_SUCCESS;
116
117         r = Construct(FORM_STYLE_INDICATOR | FORM_STYLE_TITLE | FORM_STYLE_FOOTER);
118         TryReturnResult(!IsFailed(r), E_SYSTEM, E_SYSTEM, "[%s] Failed to construct CertificateSelectorForm.", GetErrorMessage(E_SYSTEM));
119
120         __reqId = reqId;
121
122         return r;
123 }
124
125 result
126 CertificateSelectorForm::OnInitializing(void)
127 {
128         result r = E_SUCCESS;
129
130         int count = 0;
131         int certCount = 0;
132         int invalidCount = 0;
133         Footer* pFooter = null;
134
135         r = SetTitleText(TITLE_STRING, ALIGNMENT_LEFT);
136         TryReturnResult(!IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
137
138         // footer
139         pFooter = GetFooter();
140         TryReturnResult(pFooter != null, E_SYSTEM, E_SYSTEM, "[%s] Footer instance is not available.", GetErrorMessage(E_SYSTEM));
141
142         r = pFooter->SetStyle(FOOTER_STYLE_SEGMENTED_TEXT);
143         TryReturnResult(!IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
144
145         r = pFooter->SetBackButton();
146         TryReturnResult(!IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
147
148         SetFormBackEventListener(this);
149
150         // load user certificates
151         _CertFieldInfos* pRawCertList = null;
152         certCount = _CertService::GetUserCertFieldInfoN(pRawCertList);
153         r = GetLastResult();
154         std::unique_ptr<_CertFieldInfos[]> pCertList(pRawCertList);
155         TryReturnResult(!IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
156
157         // trivial condition
158         AppAssertf(((certCount > 0) && (pCertList != null)) || ((certCount == 0) && (pCertList == null))
159                 , "A system error has been occurred. Because invalid cert count was returned.");
160
161         // cert index : count only for valid certificate
162         std::unique_ptr<int[]> pCertIndex(new (std::nothrow) int[certCount]);
163         TryReturnResult(pCertIndex != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
164
165         // assign number of certificate which is valid(certTitle or subjectName must be exist)
166         for (count = 0; count < certCount; count++)
167         {
168                 if (!pCertList[count].certTitle[0] && !pCertList[count].subjectName[0])
169                 {
170                         invalidCount++;
171                         continue;
172                 }
173                 pCertIndex[count - invalidCount] = count;
174         }
175         __certCount = certCount - invalidCount;
176
177         // listview
178         std::unique_ptr<ListView> pListView(new (std::nothrow) ListView());
179         TryReturnResult(pListView != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
180
181         if (__itemHeight == ITEM_HEIGHT && __itemLeftMargin == ITEM_LEFT_MARGIN)
182         {
183                 double ratio = 1.0;
184                 double logicalWidth = HD_RESOLUTION_WIDTH;
185                 Dimension tempDim = CoordinateSystem::GetLogicalResolution();
186                 logicalWidth = tempDim.width;
187                 AppAssertf(logicalWidth > 0.0, "invalid logical resolution (%lf)", logicalWidth);
188
189                 ratio = logicalWidth / HD_RESOLUTION_WIDTH;
190
191                 // __itemHeight : 112 = logicalWidth : HD_RESOLUTION_WIDTH(720)
192                 __itemHeight = static_cast<int>(__itemHeight * ratio);
193
194                 // __itemLeftMargin : 26 = logicalWidth : HD_RESOLUTION_WIDTH(720)
195                 __itemLeftMargin = static_cast<int>(__itemLeftMargin * ratio);
196         }
197
198         Rectangle rect(0, 0, GetClientAreaBounds().width, GetClientAreaBounds().height);
199         r = pListView->Construct(rect, true, false);
200         TryReturnResult(!IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
201
202         r = pListView->SetItemProvider(*this);
203         TryReturnResult(!IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
204
205         r = pListView->SetTextOfEmptyList(EMPTY_LIST_STRING);
206         TryReturnResult(!IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
207
208         pListView->AddListViewItemEventListener(*this);
209
210         r = AddControl(*(pListView.release()));
211         TryReturnResult(!IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
212
213         __pCertIndex = std::move(pCertIndex);
214         __pCertList = std::move(pCertList);
215
216         return r;
217 }
218
219 result
220 CertificateSelectorForm::OnTerminating(void)
221 {
222         return E_SUCCESS;
223 }
224
225 // IFormBackEventListener
226 void
227 CertificateSelectorForm::OnFormBackRequested(Form& source)
228 {
229         Finish(false, -1);
230 }
231
232 // IListViewItemProvider
233 ListItemBase*
234 CertificateSelectorForm::CreateItem(int index, int itemWidth)
235 {
236         result r = E_SUCCESS;
237         int validIndex = 0;
238         ListAnnexStyle style = LIST_ANNEX_STYLE_NORMAL;
239
240         Rectangle rect(__itemLeftMargin, 0, itemWidth - (__itemLeftMargin * 2), __itemHeight);
241         Dimension dim(itemWidth, __itemHeight);
242
243         AppLog("The current value of index is %d", index);
244
245         // convert to valid index
246         AppAssertf((index >= 0) && (index < __certCount), "index (%d) is out of range.", index);
247         AppAssertf(__pCertIndex != null, "__pCertIndex instance must not be null.");
248         validIndex = __pCertIndex[index];
249         AppLog("The current value of validIndex is %d", validIndex);
250
251         // make item
252         std::unique_ptr<CustomItem> pItem(new (std::nothrow) CustomItem());
253         TryReturnResult(pItem != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
254
255         r = pItem->Construct(dim, style);
256         TryReturnResult(!IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
257
258         // prevent overflow
259         __pCertList[validIndex].certTitle[_MAX_ISSUER_SUBJECT_NAME_SIZE] = '\0';
260         __pCertList[validIndex].subjectName[_MAX_ISSUER_SUBJECT_NAME_SIZE] = '\0';
261
262         AppLog("The current value of certFileId is %d", __pCertList[validIndex].certFileId);
263
264         // add to item, certFileId become elementId
265         if (__pCertList[validIndex].certTitle[0])
266         {       // it has valid certTitle
267                 AppLog("The current value of certTitle is %s", __pCertList[validIndex].certTitle);
268                 r = pItem->AddElement(rect, __pCertList[validIndex].certFileId, __pCertList[validIndex].certTitle);
269                 TryReturnResult(!IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
270         }
271         else
272         {       // it is not reached because it already checked by OnInitializing method.
273                 AppAssertf(__pCertList[validIndex].subjectName[0], "_CertFieldInfos instance must not be null.");
274
275                 // it has valid subjectName
276                 AppLog("The current value of subjectName is %s", __pCertList[validIndex].subjectName);
277                 r = pItem->AddElement(rect, __pCertList[validIndex].certFileId, __pCertList[validIndex].subjectName);
278                 TryReturnResult(!IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
279         }
280
281         return pItem.release();
282 }
283
284 bool
285 CertificateSelectorForm::DeleteItem(int index, ListItemBase* pItem, int itemWidth)
286 {
287         delete pItem;
288         return true;
289 }
290
291 int
292 CertificateSelectorForm::GetItemCount(void)
293 {
294         AppLog("The current value of __certCount is %d", __certCount);
295         return __certCount;
296 }
297
298 // IListViewItemEventListener
299 void
300 CertificateSelectorForm::OnListViewItemStateChanged(ListView &listView, int index, int elementId, ListItemStatus status)
301 {
302         AppLog("The current value of elementId(certFileId) is %d", elementId);
303         Finish(true, elementId);
304 }
305
306 void
307 CertificateSelectorForm::OnListViewContextItemStateChanged(ListView& listView, int index, int elementId, ListContextItemStatus state)
308 {
309 }
310
311 void
312 CertificateSelectorForm::OnListViewItemSwept(ListView& listView, int index, SweepDirection direction)
313 {
314 }