2 // Copyright (c) 2013 Samsung Electronics Co., Ltd.
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
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
18 * @file CertificateSelectorForm.cpp
19 * @brief This is the implementation for the %CertificateSelectorForm class.
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>
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"
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;
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";
57 CertificateSelectorForm::CertificateSelectorForm(void)
59 , __itemLeftMargin(ITEM_LEFT_MARGIN)
60 , __itemHeight(ITEM_HEIGHT)
67 CertificateSelectorForm::~CertificateSelectorForm(void)
72 CertificateSelectorForm::Finish(bool isSelected, int certId)
76 AppLog("The current value of certId is %d.", certId);
78 UiApp* pApp = UiApp::GetInstance();
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));
88 AppLog("The current value of isSelected is true.");
89 TryCatchResult(certId >= 0, , E_INVALID_ARG, "[%s] Invalid Certificate ID.", GetErrorMessage(E_INVALID_ARG));
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));
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));
98 AppControlProviderManager::GetInstance()->SendAppControlResult(__reqId, APP_CTRL_RESULT_SUCCEEDED, pMap.release());
102 AppLog("The current value of isSelected is false.");
105 AppControlProviderManager::GetInstance()->SendAppControlResult(__reqId, APP_CTRL_RESULT_CANCELED, null);
112 AppControlProviderManager::GetInstance()->SendAppControlResult(__reqId, APP_CTRL_RESULT_FAILED, null);
117 CertificateSelectorForm::Initialize(RequestId reqId)
119 result r = E_SUCCESS;
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));
130 CertificateSelectorForm::OnInitializing(void)
132 result r = E_SUCCESS;
136 int invalidCount = 0;
137 Footer* pFooter = null;
139 r = SetTitleText(TITLE_STRING, ALIGNMENT_LEFT);
140 TryReturnResult(!IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
143 pFooter = GetFooter();
144 TryReturnResult(pFooter != null, E_SYSTEM, E_SYSTEM, "[%s] Footer instance is not available.", GetErrorMessage(E_SYSTEM));
146 r = pFooter->SetStyle(FOOTER_STYLE_SEGMENTED_TEXT);
147 TryReturnResult(!IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
149 r = pFooter->SetBackButton();
150 TryReturnResult(!IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
152 SetFormBackEventListener(this);
154 // load user certificates
155 _CertFieldInfos* pRawCertList = null;
156 certCount = _CertService::GetUserCertFieldInfoN(pRawCertList);
158 std::unique_ptr<_CertFieldInfos[]> pCertList(pRawCertList);
159 TryReturnResult(!IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
162 AppAssertf(((certCount > 0) && (pCertList != null)) || ((certCount == 0) && (pCertList == null))
163 , "A system error has been occurred. Because invalid cert count was returned.");
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));
169 // assign number of certificate which is valid(certTitle or subjectName must be exist)
170 for (count = 0; count < certCount; count++)
172 if (!pCertList[count].certTitle[0] && !pCertList[count].subjectName[0])
177 pCertIndex[count - invalidCount] = count;
179 __certCount = certCount - invalidCount;
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));
185 if (__itemHeight == ITEM_HEIGHT && __itemLeftMargin == ITEM_LEFT_MARGIN)
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);
193 ratio = logicalWidth / HD_RESOLUTION_WIDTH;
195 // __itemHeight : 112 = logicalWidth : HD_RESOLUTION_WIDTH(720)
196 __itemHeight = static_cast<int>(__itemHeight * ratio);
198 // __itemLeftMargin : 26 = logicalWidth : HD_RESOLUTION_WIDTH(720)
199 __itemLeftMargin = static_cast<int>(__itemLeftMargin * ratio);
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));
206 r = pListView->SetItemProvider(*this);
207 TryReturnResult(!IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
209 r = pListView->SetTextOfEmptyList(EMPTY_LIST_STRING);
210 TryReturnResult(!IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
212 pListView->AddListViewItemEventListener(*this);
214 r = AddControl(*(pListView.release()));
215 TryReturnResult(!IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
217 __pCertIndex = std::move(pCertIndex);
218 __pCertList = std::move(pCertList);
224 CertificateSelectorForm::OnTerminating(void)
229 // IFormBackEventListener
231 CertificateSelectorForm::OnFormBackRequested(Form& source)
236 // IListViewItemProvider
238 CertificateSelectorForm::CreateItem(int index, int itemWidth)
240 result r = E_SUCCESS;
242 ListAnnexStyle style = LIST_ANNEX_STYLE_NORMAL;
244 Rectangle rect(__itemLeftMargin, 0, itemWidth - (__itemLeftMargin * 2), __itemHeight);
245 Dimension dim(itemWidth, __itemHeight);
247 AppLog("The current value of index is %d", index);
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);
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));
259 r = pItem->Construct(dim, style);
260 TryReturnResult(!IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
263 __pCertList[validIndex].certTitle[_MAX_ISSUER_SUBJECT_NAME_SIZE] = '\0';
264 __pCertList[validIndex].subjectName[_MAX_ISSUER_SUBJECT_NAME_SIZE] = '\0';
266 AppLog("The current value of certFileId is %d", __pCertList[validIndex].certFileId);
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));
276 { // it is not reached because it already checked by OnInitializing method.
277 AppAssertf(__pCertList[validIndex].subjectName[0], "_CertFieldInfos instance must not be null.");
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));
285 return pItem.release();
289 CertificateSelectorForm::DeleteItem(int index, ListItemBase* pItem, int itemWidth)
296 CertificateSelectorForm::GetItemCount(void)
298 AppLog("The current value of __certCount is %d", __certCount);
302 // IListViewItemEventListener
304 CertificateSelectorForm::OnListViewItemStateChanged(ListView &listView, int index, int elementId, ListItemStatus status)
306 AppLog("The current value of elementId(certFileId) is %d", elementId);
307 Finish(true, elementId);
311 CertificateSelectorForm::OnListViewContextItemStateChanged(ListView& listView, int index, int elementId, ListContextItemStatus state)
316 CertificateSelectorForm::OnListViewItemSwept(ListView& listView, int index, SweepDirection direction)