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 CertificateSelectorForm.cpp
20 * @brief This is the implementation for the %CertificateSelectorForm class.
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>
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"
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;
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";
56 CertificateSelectorForm::CertificateSelectorForm(void)
58 , __itemLeftMargin(ITEM_LEFT_MARGIN)
59 , __itemHeight(ITEM_HEIGHT)
66 CertificateSelectorForm::~CertificateSelectorForm(void)
71 CertificateSelectorForm::Finish(bool isSelected, int certId)
75 AppLog("The current value of certId is %d.", certId);
77 UiApp* pApp = UiApp::GetInstance();
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));
87 AppLog("The current value of isSelected is true.");
88 TryCatchResult(certId >= 0, , E_INVALID_ARG, "[%s] Invalid Certificate ID.", GetErrorMessage(E_INVALID_ARG));
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));
94 AppControlProviderManager::GetInstance()->SendAppControlResult(__reqId, APP_CTRL_RESULT_SUCCEEDED, pMap.release());
98 AppLog("The current value of isSelected is false.");
101 AppControlProviderManager::GetInstance()->SendAppControlResult(__reqId, APP_CTRL_RESULT_CANCELED, null);
108 AppControlProviderManager::GetInstance()->SendAppControlResult(__reqId, APP_CTRL_RESULT_FAILED, null);
113 CertificateSelectorForm::Initialize(RequestId reqId)
115 result r = E_SUCCESS;
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));
126 CertificateSelectorForm::OnInitializing(void)
128 result r = E_SUCCESS;
132 int invalidCount = 0;
133 Footer* pFooter = null;
135 r = SetTitleText(TITLE_STRING, ALIGNMENT_LEFT);
136 TryReturnResult(!IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
139 pFooter = GetFooter();
140 TryReturnResult(pFooter != null, E_SYSTEM, E_SYSTEM, "[%s] Footer instance is not available.", GetErrorMessage(E_SYSTEM));
142 r = pFooter->SetStyle(FOOTER_STYLE_SEGMENTED_TEXT);
143 TryReturnResult(!IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
145 r = pFooter->SetBackButton();
146 TryReturnResult(!IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
148 SetFormBackEventListener(this);
150 // load user certificates
151 _CertFieldInfos* pRawCertList = null;
152 certCount = _CertService::GetUserCertFieldInfoN(pRawCertList);
154 std::unique_ptr<_CertFieldInfos[]> pCertList(pRawCertList);
155 TryReturnResult(!IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
158 AppAssertf(((certCount > 0) && (pCertList != null)) || ((certCount == 0) && (pCertList == null))
159 , "A system error has been occurred. Because invalid cert count was returned.");
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));
165 // assign number of certificate which is valid(certTitle or subjectName must be exist)
166 for (count = 0; count < certCount; count++)
168 if (!pCertList[count].certTitle[0] && !pCertList[count].subjectName[0])
173 pCertIndex[count - invalidCount] = count;
175 __certCount = certCount - invalidCount;
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));
181 if (__itemHeight == ITEM_HEIGHT && __itemLeftMargin == ITEM_LEFT_MARGIN)
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);
189 ratio = logicalWidth / HD_RESOLUTION_WIDTH;
191 // __itemHeight : 112 = logicalWidth : HD_RESOLUTION_WIDTH(720)
192 __itemHeight = static_cast<int>(__itemHeight * ratio);
194 // __itemLeftMargin : 26 = logicalWidth : HD_RESOLUTION_WIDTH(720)
195 __itemLeftMargin = static_cast<int>(__itemLeftMargin * ratio);
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));
202 r = pListView->SetItemProvider(*this);
203 TryReturnResult(!IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
205 r = pListView->SetTextOfEmptyList(EMPTY_LIST_STRING);
206 TryReturnResult(!IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
208 pListView->AddListViewItemEventListener(*this);
210 r = AddControl(*(pListView.release()));
211 TryReturnResult(!IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
213 __pCertIndex = std::move(pCertIndex);
214 __pCertList = std::move(pCertList);
220 CertificateSelectorForm::OnTerminating(void)
225 // IFormBackEventListener
227 CertificateSelectorForm::OnFormBackRequested(Form& source)
232 // IListViewItemProvider
234 CertificateSelectorForm::CreateItem(int index, int itemWidth)
236 result r = E_SUCCESS;
238 ListAnnexStyle style = LIST_ANNEX_STYLE_NORMAL;
240 Rectangle rect(__itemLeftMargin, 0, itemWidth - (__itemLeftMargin * 2), __itemHeight);
241 Dimension dim(itemWidth, __itemHeight);
243 AppLog("The current value of index is %d", index);
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);
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));
255 r = pItem->Construct(dim, style);
256 TryReturnResult(!IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
259 __pCertList[validIndex].certTitle[_MAX_ISSUER_SUBJECT_NAME_SIZE] = '\0';
260 __pCertList[validIndex].subjectName[_MAX_ISSUER_SUBJECT_NAME_SIZE] = '\0';
262 AppLog("The current value of certFileId is %d", __pCertList[validIndex].certFileId);
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));
272 { // it is not reached because it already checked by OnInitializing method.
273 AppAssertf(__pCertList[validIndex].subjectName[0], "_CertFieldInfos instance must not be null.");
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));
281 return pItem.release();
285 CertificateSelectorForm::DeleteItem(int index, ListItemBase* pItem, int itemWidth)
292 CertificateSelectorForm::GetItemCount(void)
294 AppLog("The current value of __certCount is %d", __certCount);
298 // IListViewItemEventListener
300 CertificateSelectorForm::OnListViewItemStateChanged(ListView &listView, int index, int elementId, ListItemStatus status)
302 AppLog("The current value of elementId(certFileId) is %d", elementId);
303 Finish(true, elementId);
307 CertificateSelectorForm::OnListViewContextItemStateChanged(ListView& listView, int index, int elementId, ListContextItemStatus state)
312 CertificateSelectorForm::OnListViewItemSwept(ListView& listView, int index, SweepDirection direction)