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