Applied latest source code
[apps/native/preloaded/Settings.git] / src / StNfcPresentationModel.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                StNfcPresentationModel.cpp
19  * @brief               This is the implementation file for NfcPresentationModel class.
20  */
21
22 #include <FApp.h>
23 #include "StNfcPresentationModel.h"
24
25 using namespace Tizen::App;
26 using namespace Tizen::App::Package;
27 using namespace Tizen::Base;
28 using namespace Tizen::Base::Collection;
29 using namespace Tizen::Net::Nfc;
30
31 NfcPresentationModel* NfcPresentationModel::__pNfcPresentationModelInstance = null;
32
33 NfcPresentationModel::NfcPresentationModel(void)
34         : __pNfcManager(null)
35         , __pINfcPresentationModelEventListener(null)
36 {
37 }
38
39 NfcPresentationModel::~NfcPresentationModel(void)
40 {
41         if (__pNfcManager != null)
42         {
43                 delete __pNfcManager;
44                 __pNfcManager = null;
45         }
46 }
47
48 result
49 NfcPresentationModel::ActivateNfc(void)
50 {
51         if (IsNfcActivated() == true)
52         {
53                 return E_FAILURE;
54         }
55
56         return __pNfcManager->Activate();
57 }
58
59 result
60 NfcPresentationModel::DeactivateNfc(void)
61 {
62         if (IsNfcActivated() == false)
63         {
64                 return E_FAILURE;
65         }
66
67         return __pNfcManager->Deactivate();
68 }
69
70 bool
71 NfcPresentationModel::IsNfcActivated(void)
72 {
73         return __pNfcManager->IsActivated();
74 }
75
76 void
77 NfcPresentationModel::OnNfcActivated(result r)
78 {
79         FireNfcEvnet(NFC_PRESENTATION_MODEL_EVENT_ACTIVATED, r);
80 }
81
82 void
83 NfcPresentationModel::OnNfcDeactivated(result r)
84 {
85         FireNfcEvnet(NFC_PRESENTATION_MODEL_EVENT_DEACTIVATED, r);
86 }
87
88 void
89 NfcPresentationModel::SetNfcEventListener(const INfcPresentationModelEventListener* pNfcEventListener)
90 {
91         __pINfcPresentationModelEventListener = pNfcEventListener;
92 }
93
94 NfcPresentationModel*
95 NfcPresentationModel::GetInstance(void)
96 {
97         result r = E_SUCCESS;
98         if (__pNfcPresentationModelInstance == null)
99         {
100                 r = CreateInstance();
101         }
102
103         return __pNfcPresentationModelInstance;
104 }
105
106 result
107 NfcPresentationModel::Construct(void)
108 {
109         __pNfcManager = new (std::nothrow) NfcManager();
110         result r = __pNfcManager->Construct(*this);
111
112 //      AppAssertf(r == E_SUCCESS, "Failed to construct NfcManager (%s)", GetErrorMessage(r));
113
114         if (IsFailed(r))
115         {
116                 AppLogDebug("Construct failed(%s)", GetErrorMessage(r));
117                 delete __pNfcManager;
118                 __pNfcManager = null;
119
120                 return r;
121         }
122         return r;
123 }
124
125 result
126 NfcPresentationModel::CreateInstance(void)
127 {
128         __pNfcPresentationModelInstance = new (std::nothrow) NfcPresentationModel();
129         result r = __pNfcPresentationModelInstance->Construct();
130         if (IsFailed(r))
131         {
132                 delete __pNfcPresentationModelInstance;
133                 __pNfcPresentationModelInstance = null;
134                 return r;
135         }
136
137         std::atexit(DestroyInstance);
138         return r;
139 }
140
141 void
142 NfcPresentationModel::DestroyInstance(void)
143 {
144         if (__pNfcPresentationModelInstance != null)
145         {
146                 delete __pNfcPresentationModelInstance;
147                 __pNfcPresentationModelInstance = null;
148         }
149 }
150
151 result
152 NfcPresentationModel::FireNfcEvnet(NfcPresentationModelEvent nfcEvent, result r)
153 {
154         if (__pINfcPresentationModelEventListener == null)
155         {
156                 AppLogDebug("__pINfcPresentationModelEventListener is null");
157                 return E_FAILURE;
158         }
159
160         (const_cast <INfcPresentationModelEventListener*>(__pINfcPresentationModelEventListener))->OnNfcPresentationModelEventCompleted(nfcEvent, r);
161
162         return E_SUCCESS;
163 }
164
165 NfcPredefinedItemPresentationModel* NfcPredefinedItemPresentationModel::__pNfcPredefinedItemPresentationModelInstance = null;
166
167 NfcPredefinedItemPresentationModel::NfcPredefinedItemPresentationModel(void)
168         : __pNdefPushManager(null)
169         , __pReservedPushCandidatesMap(null)
170         , __pReservedPushCandidatesAppIdList(null)
171         , __pReservedPushCandidatesDescList(null)
172 {
173 }
174
175 NfcPredefinedItemPresentationModel::~NfcPredefinedItemPresentationModel(void)
176 {
177         delete __pNdefPushManager;
178         delete __pReservedPushCandidatesMap;
179         delete __pReservedPushCandidatesAppIdList;
180         delete __pReservedPushCandidatesDescList;
181 }
182
183 void
184 NfcPredefinedItemPresentationModel::InitReservedPushCandidatesList(void)
185 {
186         if (__pNdefPushManager == null)
187         {
188                 AppLogDebug("NdefPushManager is null");
189                 return;
190         }
191
192         delete __pReservedPushCandidatesMap;
193         __pReservedPushCandidatesMap = __pNdefPushManager->GetReservedPushCandidatesN();
194
195         if(__pReservedPushCandidatesMap == null)
196         {
197                 AppLogDebug("__pReservedPushCandidatesMap is null");
198                 return;
199         }
200
201         delete __pReservedPushCandidatesAppIdList;
202         __pReservedPushCandidatesAppIdList = new (std::nothrow) ArrayList(SingleObjectDeleter);
203         __pReservedPushCandidatesAppIdList->Construct();
204
205         delete __pReservedPushCandidatesDescList;
206         __pReservedPushCandidatesDescList = new (std::nothrow) ArrayList(SingleObjectDeleter);
207         __pReservedPushCandidatesDescList->Construct();
208
209         PackageManager* pPackageManager = PackageManager::GetInstance();
210         IMapEnumerator* pEnum = null;
211         pEnum = __pReservedPushCandidatesMap->GetMapEnumeratorN();
212
213         while (pEnum->MoveNext() == E_SUCCESS)
214         {
215                 PackageInfo* pPackageInfo = pPackageManager->GetPackageInfoN(*static_cast<AppId*>(pEnum->GetKey()));
216                 if (pPackageInfo)
217                 {
218                         __pReservedPushCandidatesAppIdList->Add(new (std::nothrow) String(pPackageInfo->GetDisplayName()));
219                         delete pPackageInfo;
220                         __pReservedPushCandidatesDescList->Add(new (std::nothrow) String(*static_cast<String*>(pEnum->GetValue())));
221                 }
222         }
223
224         delete pEnum;
225 }
226
227 int
228 NfcPredefinedItemPresentationModel::GetSelectNfcReservedPushCandidatesAppIdIndex(void)
229 {
230         PackageId pPackageId = __pNdefPushManager->GetPickedReservedPushMessage();
231         ArrayList* pKeyList = static_cast<ArrayList*>(__pReservedPushCandidatesMap->GetKeysN());
232         int checkIndex = 0;
233         for (checkIndex = 0; checkIndex < pKeyList->GetCount(); checkIndex++)
234         {
235                 String* pKeyId = static_cast<String*>(pKeyList->GetAt(checkIndex));
236                 if (pKeyId->Equals(pPackageId, false) == true)
237                 {
238                         break;
239                 }
240         }
241         return checkIndex;
242 }
243
244 result
245 NfcPredefinedItemPresentationModel::SetSelectNfcReservedPushCandidatesAppIdIndex(int itemIndex)
246 {
247         result r = E_FAILURE;
248         ArrayList* pKeyList = static_cast<ArrayList*>(__pReservedPushCandidatesMap->GetKeysN());
249         PackageId* pPackageId = static_cast<PackageId*>(pKeyList->GetAt(itemIndex));
250         r = __pNdefPushManager->PickReservedPushMessage(*pPackageId);
251         return r;
252 }
253
254 bool
255 NfcPredefinedItemPresentationModel::IsNfcReservedPushActivated(void)
256 {
257         if (__pNdefPushManager == null)
258         {
259                 AppLogDebug("__pNdefPushManager is null");
260                 return false;
261         }
262         return __pNdefPushManager->IsReservedPushActivated();
263 }
264
265 result
266 NfcPredefinedItemPresentationModel::NfcReservedPushActivated(void)
267 {
268         if (__pNdefPushManager == null)
269         {
270                 AppLogDebug("__pNdefPushManager is null");
271                 return E_FAILURE;
272         }
273         return __pNdefPushManager->ActivateReservedPush();
274 }
275
276 result
277 NfcPredefinedItemPresentationModel::NfcReservedPushDeactivated(void)
278 {
279         if (__pNdefPushManager == null)
280         {
281                 AppLogDebug("__pNdefPushManager is null");
282                 return E_FAILURE;
283         }
284         return __pNdefPushManager->DeactivateReservedPush();
285 }
286
287 ArrayList*
288 NfcPredefinedItemPresentationModel::GetAllListReservedPushCandidatesAppIdList(void)
289 {
290         return __pReservedPushCandidatesAppIdList;
291 }
292
293 ArrayList*
294 NfcPredefinedItemPresentationModel::GetAllListReservedPushCandidatesDescList(void)
295 {
296         return __pReservedPushCandidatesDescList;
297 }
298
299 void
300 NfcPredefinedItemPresentationModel::OnNdefPushMessageSent(result r)
301 {
302 }
303
304 NfcPredefinedItemPresentationModel*
305 NfcPredefinedItemPresentationModel::GetInstance(void)
306 {
307         if (__pNfcPredefinedItemPresentationModelInstance == null)
308         {
309                 CreateInstance();
310         }
311
312         return __pNfcPredefinedItemPresentationModelInstance;
313 }
314
315 result
316 NfcPredefinedItemPresentationModel::Construct(void)
317 {
318         result r = E_SUCCESS;
319         __pNdefPushManager = new (std::nothrow) NdefPushManager();
320
321         AppAssertf(__pNdefPushManager, "Failed to create NdefPushManager");
322
323         r = __pNdefPushManager->SetNdefPushManagerListener(this);
324
325         return r;
326 }
327
328 void
329 NfcPredefinedItemPresentationModel::CreateInstance(void)
330 {
331         __pNfcPredefinedItemPresentationModelInstance = new (std::nothrow) NfcPredefinedItemPresentationModel();
332         result r = __pNfcPredefinedItemPresentationModelInstance->Construct();
333         if (IsFailed(r))
334         {
335                 delete __pNfcPredefinedItemPresentationModelInstance;
336                 __pNfcPredefinedItemPresentationModelInstance = null;
337                 return;
338         }
339
340         std::atexit(DestroyInstance);
341 }
342
343 void
344 NfcPredefinedItemPresentationModel::DestroyInstance(void)
345 {
346         if (__pNfcPredefinedItemPresentationModelInstance != null)
347         {
348                 delete __pNfcPredefinedItemPresentationModelInstance;
349                 __pNfcPredefinedItemPresentationModelInstance = null;
350         }
351 }