Applied latest source code
[apps/native/preloaded/Settings.git] / src / StCertificateUserInstallForm.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Flora License, Version 1.1 (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://floralicense.org/license/
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                StCertificateUserInstallForm.cpp
19  * @brief               This is the implementation file for CertificateUserInstallForm class.
20  */
21
22 #include <FSystem.h>
23 #include "StCertificateUserInstallForm.h"
24 #include "StResourceManager.h"
25 #include "StSettingScenesList.h"
26 #include "StTypes.h"
27
28 using namespace Tizen::Base;
29 using namespace Tizen::Graphics;
30 using namespace Tizen::Security::Cert;
31 using namespace Tizen::System;
32 using namespace Tizen::Ui;
33 using namespace Tizen::Ui::Controls;
34 using namespace Tizen::Ui::Scenes;
35
36 static const int ID_GROUP_COUNT = 1;
37
38 CertificateUserInstallForm::CertificateUserInstallForm(void)
39         : __pCertificatePresentationModelInstance(null)
40 {
41 }
42
43 CertificateUserInstallForm::~CertificateUserInstallForm(void)
44 {
45 }
46
47 void
48 CertificateUserInstallForm::CreateFooter(void)
49 {
50         Footer* pFooter = GetFooter();
51         AppAssert(pFooter);
52
53         pFooter->SetStyle(FOOTER_STYLE_BUTTON_TEXT);
54         pFooter->AddActionEventListener(*this);
55
56         SetFormBackEventListener(this);
57 }
58
59 result
60 CertificateUserInstallForm::OnInitializing(void)
61 {
62         DeviceManager::AddDeviceEventListener(DEVICE_TYPE_STORAGE_CARD, *this);
63         return InitCertificatePresentationModelInstance();
64 }
65
66 result
67 CertificateUserInstallForm::OnTerminating(void)
68 {
69         ReleaseCertificatePresentationModelInstance();
70
71         DeviceManager::RemoveDeviceEventListener(DEVICE_TYPE_STORAGE_CARD, *this);
72         __pTableView = null;
73
74         SetFormBackEventListener(null);
75         return E_SUCCESS;
76 }
77
78 void
79 CertificateUserInstallForm::OnSceneActivatedN(const Tizen::Ui::Scenes::SceneId& previousSceneId, const Tizen::Ui::Scenes::SceneId& currentSceneId, Tizen::Base::Collection::IList* pArgs)
80 {
81         CreateHeader(ResourceManager::GetString(L"IDS_ST_BODY_SELECT_CERTIFICATE_TO_INSTALL"));
82
83         if (__pTableView != null)
84         {
85                 int tableViewControlCount = __pTableView->GetControlCount();
86                 for (int i = 0; i < tableViewControlCount; i++)
87                 {
88                         __pTableView->RemoveControl(__pTableView->GetControl(0));
89                 }
90 //              __pTableView->RemoveAllControls();
91                 __pTableView = null;
92         }
93         int controlCount = GetControlCount();
94         for (int i = 0; i < controlCount; i++)
95         {
96                 RemoveControl(GetControl(0));
97         }
98 //      RemoveAllControls();
99         result r = __pCertificatePresentationModelInstance->FindUserCertificateToInstallFromStorage();
100         if (IsFailed(r))
101         {
102                 AppLogDebug("FindUserCertificateToInstallFromStorage is error %s", GetErrorMessage(r));
103                 return;
104         }
105
106         if (__pCertificatePresentationModelInstance->GetFoundUserCertificateToInstallListCount() != 0)
107         {
108                 CreateTableView();
109                 __pTableView->UpdateTableView();
110         }
111         else
112         {
113                 Label* pLabel = static_cast<Label*>(this->GetControl(NO_CONTENTS, false));
114                 if (pLabel == null)
115                 {
116                         Rectangle clientRect = GetClientAreaBounds();
117                         Bitmap* pBitmp = ResourceManager::GetBitmapN(IDB_NO_CONTENTS);
118
119                         int bitmapWidth = pBitmp->GetWidth();
120                         int bitmapHeight = pBitmp->GetHeight();
121                         String labelText = ResourceManager::GetString(L"IDS_ST_BODY_NO_CONTENT");
122                         Color textColor = COLOR_HELP_TEXT_TYPE_03_NORMAL;
123                         int noContentTextHeight = GetHeightForStringArea(labelText, bitmapWidth, 32);
124
125                         int xPos = (clientRect.width / LINE_COUNT_2) - (bitmapWidth / DIVIDE_BY_TWO);
126                         int yPos = (clientRect.height / LINE_COUNT_2) - ((bitmapHeight + noContentTextHeight) / DIVIDE_BY_TWO);
127
128                         Label* pLabel = new (std::nothrow) Label();
129                         pLabel->Construct(Rectangle(xPos, yPos, bitmapWidth, bitmapHeight), L"");
130                         pLabel->SetName(NO_CONTENTS);
131                         pLabel->SetBackgroundBitmap(*pBitmp);
132
133                         yPos = yPos + bitmapHeight;
134
135                         Label* pLabelNoContents = new (std::nothrow) Label();
136                         pLabelNoContents->Construct(Rectangle(0, yPos, clientRect.width, noContentTextHeight), L"");
137                         pLabelNoContents->SetName(NO_CONTENTS_TEXT);
138                         pLabelNoContents->SetTextColor(textColor);
139                         pLabelNoContents->SetText(labelText);
140
141                         AddControl(pLabel);
142                         AddControl(pLabelNoContents);
143                 }
144         }
145 }
146
147 void
148 CertificateUserInstallForm::OnSceneDeactivated(const Tizen::Ui::Scenes::SceneId& currentSceneId, const Tizen::Ui::Scenes::SceneId& nextSceneId)
149 {
150 }
151
152 void
153 CertificateUserInstallForm::OnFormBackRequested(Tizen::Ui::Controls::Form& source)
154 {
155         SceneManager* pSceneManager = SceneManager::GetInstance();
156         AppAssert(pSceneManager);
157
158         pSceneManager->GoBackward(BackwardSceneTransition(SCENE_TRANSITION_ANIMATION_TYPE_RIGHT), null);
159 }
160
161 int
162 CertificateUserInstallForm::GetGroupCount(void)
163 {
164         AppLogDebug("ENTER");
165         return ID_GROUP_COUNT;
166 }
167
168 int
169 CertificateUserInstallForm::GetItemCount(int groupIndex)
170 {
171         return __pCertificatePresentationModelInstance->GetFoundUserCertificateToInstallListCount();
172 }
173
174 TableViewGroupItem*
175 CertificateUserInstallForm::CreateGroupItem(int groupIndex, int itemWidth)
176 {
177         int itemHeight = 0;
178
179         TableViewGroupItem* pItem = new (std::nothrow) TableViewGroupItem();
180
181         pItem->Construct(Dimension(itemWidth, itemHeight));
182         pItem->SetBackgroundColor(COLOR_BG_GROUP_INDEX_DEFAULT);
183         pItem->SetEnabled(false);
184
185         return pItem;
186 }
187
188 TableViewItem*
189 CertificateUserInstallForm::CreateItem(int groupIndex, int itemIndex, int itemWidth)
190 {
191         Rectangle itemMainRectangle;
192         TableViewAnnexStyle style = TABLE_VIEW_ANNEX_STYLE_NORMAL;
193         int fontSize = GetFontSize();
194
195         String itemMainText = __pCertificatePresentationModelInstance->GetFileNameInFoundUserCertificateToInstallListAt(itemIndex);
196         if (IsFailed(GetLastResult()))
197         {
198                 AppLogDebug("GetFileNameInFoundUserCertificateToInstallListAt failed(%s)", GetErrorMessage(GetLastResult()));
199                 return null;
200         }
201         TableViewItem* pItem = new (std::nothrow) TableViewItem();
202
203         RelativeLayout relativeLayout;
204         relativeLayout.Construct();
205
206         result r = pItem->Construct(relativeLayout, Dimension(itemWidth, H_GROUP_ITEM_DEFAULT), style);
207         if (IsFailed(r))
208         {
209                 AppLogDebug("Construct failed(%s)", GetErrorMessage(r));
210                 delete pItem;
211                 return null;
212         }
213         pItem->SetBackgroundColor(COLOR_BG_GROUP_ITEM_DEFAULT);
214
215         ItemTypeOneLine(itemMainRectangle);
216         Label* pLabel = new (std::nothrow) Label();
217         r = pLabel->Construct(itemMainRectangle, itemMainText);
218         if (IsFailed(r))
219         {
220                 AppLogDebug("Construct failed(%s)", GetErrorMessage(r));
221                 delete pItem;
222                 return null;
223         }
224         pLabel->SetTextHorizontalAlignment(ALIGNMENT_LEFT);
225         pLabel->SetTextConfig(fontSize, LABEL_TEXT_STYLE_NORMAL);
226         pLabel->SetTextColor(COLOR_MAIN_TEXT);
227
228         pItem->AddControl(pLabel);
229         relativeLayout.SetMargin(*pLabel, 0, RELATIVE_LAYOUT_RIGHT_MARGIN, 0, 0);
230         relativeLayout.SetRelation(*pLabel, pItem, RECT_EDGE_RELATION_LEFT_TO_LEFT);
231         relativeLayout.SetRelation(*pLabel, pItem, RECT_EDGE_RELATION_RIGHT_TO_RIGHT);
232
233         return pItem;
234 }
235
236 bool
237 CertificateUserInstallForm::DeleteGroupItem(int groupIndex, TableViewGroupItem* pItem)
238 {
239         AppLogDebug("ENTER");
240
241         delete pItem;
242         pItem = null;
243
244         return true;
245 }
246
247 bool
248 CertificateUserInstallForm::DeleteItem(int groupIndex, int itemIndex, TableViewItem* pItem)
249 {
250         AppLogDebug("ENTER");
251
252         delete pItem;
253         pItem = null;
254
255         return true;
256 }
257
258 void
259 CertificateUserInstallForm::OnGroupedTableViewItemStateChanged(Tizen::Ui::Controls::GroupedTableView& tableView, int groupIndex, int itemIndex, Tizen::Ui::Controls::TableViewItem* pItem, Tizen::Ui::Controls::TableViewItemStatus status)
260 {
261         SceneManager* pSceneManager = SceneManager::GetInstance();
262         AppAssert(pSceneManager);
263         String fileName = __pCertificatePresentationModelInstance->GetFileNameInFoundUserCertificateToInstallListAt(itemIndex);
264         if (IsFailed(GetLastResult()))
265         {
266                 AppLogDebug("GetFileNameInFoundUserCertificateToInstallListAt failed(%s)", GetErrorMessage(GetLastResult()));
267                 return;
268         }
269         String extrnalStoragePath = Environment::GetExternalStoragePath();
270         extrnalStoragePath.Append(fileName);
271         __pCertificatePresentationModelInstance->ClearPendingUserCertificateFileNameToInstall();
272         bool isPending = __pCertificatePresentationModelInstance->PushPendingUserCertificateFileNameToInstall(extrnalStoragePath);
273         if (isPending == false)
274         {
275                 AppLogDebug("PushPendingCertificate failed");
276                 return;
277         }
278         pSceneManager->GoForward(ForwardSceneTransition(IDSCN_CERTIFICATE_USER_INSTALL_PASSWORD, SCENE_TRANSITION_ANIMATION_TYPE_LEFT,
279                                                                                                                         SCENE_HISTORY_OPTION_ADD_HISTORY, SCENE_DESTROY_OPTION_KEEP), null);
280 }
281
282 int
283 CertificateUserInstallForm::GetDefaultGroupItemHeight(void)
284 {
285         return H_GROUP_INDEX_NO_TITLE_DEFAULT;
286 }
287
288 int
289 CertificateUserInstallForm::GetDefaultItemHeight(void)
290 {
291         return H_GROUP_ITEM_DEFAULT;
292 }
293
294 void
295 CertificateUserInstallForm::UpdateGroupItem(int groupIndex, Tizen::Ui::Controls::TableViewGroupItem* pItem)
296 {
297         Rectangle itemBounds = pItem->GetBounds();
298         Rectangle areaBounds = GetClientAreaBounds();
299         itemBounds.width = areaBounds.width;
300         pItem->SetBounds(itemBounds);
301         pItem->SetBackgroundColor(COLOR_BG_GROUP_INDEX_DEFAULT);
302         pItem->SetEnabled(false);
303         pItem->Invalidate(false);
304 }
305
306 void
307 CertificateUserInstallForm::UpdateItem(int groupIndex, int itemIndex, Tizen::Ui::Controls::TableViewItem* pItem)
308 {
309         Rectangle itemMainRectangle;
310         Rectangle itemBounds = pItem->GetBounds();
311         Rectangle areaBounds = GetClientAreaBounds();
312         itemBounds.width = areaBounds.width;
313         Rectangle pLabelBounds;
314         int fontSize = GetFontSize();
315
316         String itemMainText = __pCertificatePresentationModelInstance->GetFileNameInFoundUserCertificateToInstallListAt(itemIndex);
317         if (IsFailed(GetLastResult()))
318         {
319                 AppLogDebug("GetFileNameInFoundUserCertificateToInstallListAt failed(%s)", GetErrorMessage(GetLastResult()));
320                 return;
321         }
322         itemBounds.height = H_ITEM_TYPE_1_LABEL;
323         Label* pLabel = static_cast<Label*>(pItem->GetControl(0));
324         pItem->SetBounds(itemBounds);
325
326         pItem->SetBackgroundColor(COLOR_BG_GROUP_ITEM_DEFAULT);
327         pLabelBounds = pLabel->GetBounds();
328         pLabelBounds.width = areaBounds.width;
329         pLabelBounds.height = itemBounds.height;
330
331         pLabel->SetBounds(pLabelBounds);
332         pLabel->SetTextHorizontalAlignment(ALIGNMENT_LEFT);
333         pLabel->SetTextConfig(fontSize, LABEL_TEXT_STYLE_NORMAL);
334         pLabel->SetTextColor(COLOR_MAIN_TEXT);
335         pLabel->SetText(itemMainText);
336         pLabel->Invalidate(false);
337         pItem->Invalidate(false);
338 }
339
340 void
341 CertificateUserInstallForm::OnGroupedTableViewContextItemActivationStateChanged(Tizen::Ui::Controls::GroupedTableView& tableView, int groupIndex, int itemIndex, Tizen::Ui::Controls::TableViewContextItem* pContextItem, bool activated)
342 {
343 }
344
345 void
346 CertificateUserInstallForm::OnGroupedTableViewGroupItemStateChanged(Tizen::Ui::Controls::GroupedTableView& tableView, int groupIndex, Tizen::Ui::Controls::TableViewGroupItem* pItem, Tizen::Ui::Controls::TableViewItemStatus status)
347 {
348 }
349
350 void
351 CertificateUserInstallForm::OnOrientationChanged(const Tizen::Ui::Control& source, Tizen::Ui::OrientationStatus orientationStatus)
352 {
353         BaseForm::OnOrientationChanged(source, orientationStatus);
354         Label* pLabel = static_cast<Label*>(this->GetControl(NO_CONTENTS, false));
355         if (pLabel != null)
356         {
357                 Rectangle clientRect = GetClientAreaBounds();
358                 Rectangle labelBound = pLabel->GetBounds();
359
360                 int xPos = (clientRect.width / LINE_COUNT_2) - (labelBound.width / LINE_COUNT_2);
361                 int yPos = (clientRect.height / LINE_COUNT_2) -(labelBound.height / LINE_COUNT_2);
362
363                 pLabel->SetBounds(Rectangle(xPos, yPos, labelBound.width, labelBound.height));
364                 yPos = yPos + pLabel->GetBounds().height;
365                 Label* pTextLabel = static_cast<Label*>(this->GetControl(NO_CONTENTS_TEXT, false));
366                 if (pTextLabel != null)
367                 {
368                         pTextLabel->SetBounds(Rectangle(0, yPos, clientRect.width, pTextLabel->GetBounds().height));
369                 }
370                 return;
371         }
372         __pTableView->RefreshAllItems();
373         Invalidate(true);
374 }
375
376 result
377 CertificateUserInstallForm::InitCertificatePresentationModelInstance(void)
378 {
379         __pCertificatePresentationModelInstance = CertificatePresentationModel::GetInstance();
380         if (__pCertificatePresentationModelInstance == null)
381         {
382                 return E_FAILURE;
383         }
384         CertificateType certufucatetype = __pCertificatePresentationModelInstance->GetCertificateSelectorType();
385         if (certufucatetype != USER_CERT)
386         {
387                 result r = __pCertificatePresentationModelInstance->SetCertificateSelector(USER_CERT);
388                 if (IsFailed(r))
389                 {
390                         AppLogDebug("SetCertificateSelector failed(%s)", GetErrorMessage(r));
391                         return r;
392                 }
393         }
394         return E_SUCCESS;
395 }
396
397 void
398 CertificateUserInstallForm::ReleaseCertificatePresentationModelInstance(void)
399 {
400         __pCertificatePresentationModelInstance = null;
401 }
402
403 void
404 CertificateUserInstallForm::OnDeviceStateChanged(Tizen::System::DeviceType deviceType, const Tizen::Base::String& state)
405 {
406         AppLog("Enter");
407         if (deviceType == DEVICE_TYPE_STORAGE_CARD)
408         {
409                 result r = __pCertificatePresentationModelInstance->FindUserCertificateToInstallFromStorage();
410                 if (IsFailed(r))
411                 {
412                         AppLogDebug("FindUserCertificateToInstallFromStorage is error %s", GetErrorMessage(r));
413                         return;
414                 }
415                 if (__pCertificatePresentationModelInstance->GetFoundUserCertificateToInstallListCount() != 0)
416                 {
417                         CreateTableView();
418                         __pTableView->UpdateTableView();
419                         Label* pLabel = static_cast<Label*>(this->GetControl(NO_CONTENTS, false));
420                         if (pLabel != null)
421                         {
422                                 pLabel->SetShowState(false);
423                                 Label* pTextLabel = static_cast<Label*>(this->GetControl(NO_CONTENTS_TEXT, false));
424                                 if (pTextLabel != null)
425                                 {
426                                         pTextLabel->SetShowState(false);
427                                 }
428                         }
429                 }
430                 else
431                 {
432                         Label* pLabel = static_cast<Label*>(this->GetControl(NO_CONTENTS, false));
433                         if (pLabel != null)
434                         {
435                                 pLabel->SetShowState(true);
436                         }
437                         Label* pTextLabel = static_cast<Label*>(this->GetControl(NO_CONTENTS_TEXT, false));
438                         if (pTextLabel != null)
439                         {
440                                 pTextLabel->SetShowState(true);
441                         }
442                 }
443                 if (__pTableView != null)
444                 {
445                         __pTableView->UpdateTableView();
446                 }
447         }
448         AppLog("Exit");
449 }