d3567067b1659011c8f7baf02c1c650adf9b2c09
[framework/osp/app-controls.git] / src / contact-app-control / ContactAppControlDllEntry.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16 //
17
18 /**
19  * @file         ContactAppControlDllEntry.cpp
20  * @brief       This is the implementation for the ContactAppControlDllEntry.cpp class.
21  */
22
23 #include <unique_ptr.h>
24 #include <appsvc/appsvc.h>
25 #include <contacts.h>
26
27 #include <FBaseSysLog.h>
28 #include <FBaseInteger.h>
29 #include <FBaseUtilStringUtil.h>
30 #include <FAppAppControl.h>
31 #include <FBaseColHashMap.h>
32 #include <FBaseColAllElementsDeleter.h>
33
34 #include <FBase_StringConverter.h>
35 #include <FIo_FileImpl.h>
36 #include <FSys_EnvironmentImpl.h>
37 #include <FApp_AppControlManager.h>
38 #include <FApp_AppMessageImpl.h>
39 #include <FApp_Aul.h>
40
41 using namespace Tizen::App;
42 using namespace Tizen::Base;
43 using namespace Tizen::Base::Utility;
44 using namespace Tizen::Base::Collection;
45 using namespace Tizen::Io;
46 using namespace Tizen::System;
47 using namespace Tizen::Social;
48
49 #ifdef __cplusplus
50 extern "C" {
51 #endif
52
53 static char PICK_OP_SEL_MODE_KEY[] = "selectionMode";
54 static char PICK_OP_RET_KEY_RET_TYPE[] = "returnType";
55 static char PICK_OP_RET_KEY_VALUE[] = "value";
56
57 static char EDIT_OP_KEY_CONTACT_ID[] = "contactId";
58 static char EDIT_OP_KEY_PHONE[] = "phone";
59 static char EDIT_OP_KEY_EMAIL[] = "email";
60 static char EDIT_OP_KEY_URL[] = "url";
61
62 static char VIEW_OP_KEY_VIEW_TYPE[] = "viewType";
63 static char VIEW_OP_KEY_VCARD_PATH[] = "path";
64 static char VIEW_OP_KEY_CONTACT_ID[] = "contactId";
65
66 static char VIEW_OP_VALUE_CONTACT[] = "contact";
67 static char VIEW_OP_VALUE_VCARD[] = "vcf";
68 static int  VIEW_OP_VALUE_LENGTH_MAX = 7;
69
70 static char PICK_OP_SEL_MODE_VALUE_SINGLE[] = "single";
71 static char PICK_OP_SEL_MODE_VALUE_MULTIPLE[] = "multiple";
72
73 static char PICK_OP_RET_VALUE_PHONE[] = "phone";
74 static char PICK_OP_RET_VALUE_EMAIL[] = "email";
75 static char PICK_OP_RET_VALUE_VCARD[] = "vcard";
76 static char PICK_OP_RET_VALUE_CONTACT_ID[] = "contactId";
77
78 static int PICK_OP_SEL_MODE_VALUE_LENGTH_MAX = 8;
79 static int PICK_OP_RET_VALUE_LENGTH_MAX = 10;
80
81 static char ADD_OP_KEY_NUMBER[] = "phone";
82 static char ADD_OP_KEY_EMAIL[] = "email";
83 static char ADD_OP_KEY_URL[] = "url";
84
85
86 static char CORE_OP_KEY_TYPE[] = "type";
87 static char CORE_OP_KEY_PERSON_ID[] = "ct_person_id";
88 static char CORE_OP_KEY_NUMBER[] = "ct_num";
89 static char CORE_OP_KEY_EMAIL[] = "ct_email";
90 static char CORE_OP_KEY_URL[] = "ct_web";
91 static char CORE_OP_KEY_VCARD[] = "ct_vcard";
92
93 static char CORE_OP_VALUE_EDIT_PERSON[] = "11";
94 static char CORE_OP_VALUE_EDIT_PERSON_BY_SEL[] = "20";
95
96 static char CORE_OP_VALUE_VIEW_PERSON[] = "0";
97
98 // PICK
99 static char CORE_OP_VALUE_PICK_PERSON_ID[] = "11";
100 static char CORE_OP_VALUE_PICK_NUMBER[] = "12";
101 static char CORE_OP_VALUE_PICK_EMAIL[] = "13";
102 static char CORE_OP_VALUE_PICK_VCARD[] = "14";
103 static char CORE_OP_VALUE_PICK_PERSON_ID_LIST[] = "31";
104 static char CORE_OP_VALUE_PICK_NUMBER_LIST[] = "32";
105 static char CORE_OP_VALUE_PICK_EMAIL_LIST[] = "33";
106
107 static char CORE_OP_VALUE_ADD_CONTACT[] = "21";
108
109 static char CORE_RESULT_TYPE_KEY_NUMBER[] = "num_id";
110 static char CORE_RESULT_TYPE_KEY_NUMBER_LIST[] = "num_id_list";
111 static char CORE_RESULT_TYPE_KEY_EMAIL[] = "email_id";
112 static char CORE_RESULT_TYPE_KEY_EMAIL_LIST[] = "email_id_list";
113 static char CORE_RESULT_TYPE_KEY_PERSON[] = "person_id";
114 static char CORE_RESULT_TYPE_KEY_PERSON_LIST[] = "person_id_list";
115 static char CORE_RESULT_TYPE_KEY_VCARD[] = "vcard";
116
117 result _OSP_EXPORT_ StartAppControl(int req, const String&, const String&, const String*, const String*, const IMap*);
118 result _OSP_EXPORT_ TerminateAppControl(int req);
119 void OnAppControlResult(void*, int, service_result_e, void*);
120
121 static int __req = -1;
122 static int __processId = -1;
123
124 static const wchar_t __allowedAppControlPickTable[][2][64] =
125 {
126         {L"osp.appcontrol.CONTACT", L"osp.appcontrol.operation.PICK"},
127         {L"osp.appcontrol.provider.contact", L"osp.appcontrol.operation.pick"},
128         {L"http://tizen.org/appcontrol/provider/contact", L"http://tizen.org/appcontrol/operation/pick"},
129         {L"tizen.contacts", L"http://tizen.org/appcontrol/operation/pick"},
130 };
131
132 static const wchar_t __allowedAppControlAddTable[][2][64] =
133 {
134         {L"osp.appcontrol.CONTACT", L"osp.appcontrol.operation.ADD"},
135         {L"osp.appcontrol.provider.contact", L"osp.appcontrol.operation.add"},
136         {L"http://tizen.org/appcontrol/provider/contact", L"http://tizen.org/appcontrol/operation/add"},
137         {L"tizen.contacts", L"http://tizen.org/appcontrol/operation/add"},
138 };
139
140 static const wchar_t __allowedAppControlEditTable[][2][64] =
141 {
142         {L"osp.appcontrol.CONTACT", L"osp.appcontrol.operation.EDIT"},
143         {L"osp.appcontrol.provider.contact", L"osp.appcontrol.operation.edit"},
144         {L"http://tizen.org/appcontrol/provider/contact", L"http://tizen.org/appcontrol/operation/edit"},
145         {L"tizen.contacts", L"http://tizen.org/appcontrol/operation/edit"},
146 };
147
148 static const wchar_t __allowedAppControlViewTable[][2][64] =
149 {
150         {L"osp.appcontrol.CONTACT", L"osp.appcontrol.operation.VIEW"},
151         {L"osp.appcontrol.provider.contact", L"osp.appcontrol.operation.view"},
152         {L"http://tizen.org/appcontrol/provider/contact", L"http://tizen.org/appcontrol/operation/view"},
153         {L"tizen.contacts", L"http://tizen.org/appcontrol/operation/view"},
154 };
155
156 class ScopedConnection
157 {
158 public:
159         ScopedConnection(void)
160         : __connected(false)
161         {
162                 int ret = contacts_connect2();
163                 if (ret == CONTACTS_ERROR_NONE)
164                 {
165                         __connected = true;
166                 }
167         }
168
169         ~ScopedConnection(void)
170         {
171                 if (__connected)
172                 {
173                         contacts_disconnect2();
174                 }
175         }
176
177         bool IsConnected(void) const
178         {
179                 return __connected;
180         }
181
182 private:
183         bool __connected;
184 };
185
186
187 class ScopedHandle
188 {
189 public:
190         ScopedHandle(contacts_record_h handle)
191         : __handle(handle)
192         {
193         }
194
195         ~ScopedHandle(void)
196         {
197                 if (__handle != null)
198                 {
199                         contacts_record_destroy(__handle, true);
200                 }
201         }
202
203         contacts_record_h Get(void)
204         {
205                 return __handle;
206         }
207
208         contacts_record_h Release(void)
209         {
210                 contacts_record_h handle = __handle;
211                 __handle = null;
212
213                 return handle;
214         }
215
216 private:
217         contacts_record_h __handle;
218 };
219
220 class ScopedList
221 {
222 public:
223         ScopedList(contacts_list_h handle)
224         : __handle(handle)
225         {
226         }
227
228         ~ScopedList(void)
229         {
230                 if (__handle != null)
231                 {
232                         contacts_list_destroy(__handle, true);
233                 }
234         }
235
236         contacts_list_h Get(void)
237         {
238                 return __handle;
239         }
240
241 private:
242         contacts_list_h __handle;
243 };
244
245 class ScopedFilter
246 {
247 public:
248         ScopedFilter(contacts_filter_h filter)
249         : __filter(filter)
250         {
251         }
252
253         ~ScopedFilter(void)
254         {
255                 if (__filter != null)
256                 {
257                         contacts_filter_destroy(__filter);
258                 }
259         }
260
261         contacts_filter_h Get(void)
262         {
263                 return __filter;
264         }
265
266 private:
267         contacts_filter_h __filter;
268 };
269
270 class ScopedQuery
271 {
272 public:
273         ScopedQuery(contacts_query_h query)
274         : __query(query)
275         {
276         }
277
278         ~ScopedQuery(void)
279         {
280                 if (__query != null)
281                 {
282                         contacts_query_destroy(__query);
283                 }
284         }
285
286         contacts_query_h Get(void)
287         {
288                 return __query;
289         }
290
291 private:
292         contacts_query_h __query;
293 };
294 result
295 StartAppControlForPick(const String* pUri, const String* pMime, const IMap* pMap)
296 {
297         _AppMessageImpl msg;
298         msg.AddData(pMap);
299
300         const char* pBuf = appsvc_get_data(msg.GetBundle(), PICK_OP_SEL_MODE_KEY);
301         if (pBuf == null)
302         {
303                 SysLog(NID_APP, "selectionMode has not been set");
304                 return E_INVALID_ARG;
305         }
306
307         if (strncmp(pBuf, PICK_OP_SEL_MODE_VALUE_SINGLE, PICK_OP_SEL_MODE_VALUE_LENGTH_MAX) == 0)
308         {
309                 pBuf = appsvc_get_data(msg.GetBundle(), PICK_OP_RET_KEY_RET_TYPE);
310                 if (pBuf == null)
311                 {
312                         SysLog(NID_APP, "returnType has not been set");
313                         return E_INVALID_ARG;
314                 }
315
316                 if (strncmp(pBuf, PICK_OP_RET_VALUE_PHONE, PICK_OP_RET_VALUE_LENGTH_MAX) == 0)
317                 {
318                         msg.AddData(CORE_OP_KEY_TYPE, CORE_OP_VALUE_PICK_NUMBER);
319                 }
320                 else if (strncmp(pBuf, PICK_OP_RET_VALUE_EMAIL, PICK_OP_RET_VALUE_LENGTH_MAX) == 0)
321                 {
322                         msg.AddData(CORE_OP_KEY_TYPE, CORE_OP_VALUE_PICK_EMAIL);
323                 }
324                 else if (strncmp(pBuf, PICK_OP_RET_VALUE_VCARD, PICK_OP_RET_VALUE_LENGTH_MAX) == 0)
325                 {
326                         msg.AddData(CORE_OP_KEY_TYPE, CORE_OP_VALUE_PICK_VCARD);
327                 }
328                 else if (strncmp(pBuf, PICK_OP_RET_VALUE_CONTACT_ID, PICK_OP_RET_VALUE_LENGTH_MAX) == 0)
329                 {
330                         msg.AddData(CORE_OP_KEY_TYPE, CORE_OP_VALUE_PICK_PERSON_ID);
331                 }
332                 else
333                 {
334                         SysLog(NID_APP, "returnType must be phone, email, contactId, or vcard.");
335                         return E_INVALID_ARG;
336                 }
337         }
338         else if (strncmp(pBuf, PICK_OP_SEL_MODE_VALUE_MULTIPLE, PICK_OP_SEL_MODE_VALUE_LENGTH_MAX) == 0)
339         {
340                 pBuf = appsvc_get_data(msg.GetBundle(), PICK_OP_RET_KEY_RET_TYPE);
341                 if (pBuf == null)
342                 {
343                         SysLog(NID_APP, "returnType has not been set");
344                         return E_INVALID_ARG;
345                 }
346
347                 if (strncmp(pBuf, PICK_OP_RET_VALUE_PHONE, PICK_OP_RET_VALUE_LENGTH_MAX) == 0)
348                 {
349                         msg.AddData(CORE_OP_KEY_TYPE, CORE_OP_VALUE_PICK_NUMBER_LIST);
350                 }
351                 else if (strncmp(pBuf, PICK_OP_RET_VALUE_EMAIL, PICK_OP_RET_VALUE_LENGTH_MAX) == 0)
352                 {
353                         msg.AddData(CORE_OP_KEY_TYPE, CORE_OP_VALUE_PICK_EMAIL_LIST);
354                 }
355                 else if (strncmp(pBuf, PICK_OP_RET_VALUE_CONTACT_ID, PICK_OP_RET_VALUE_LENGTH_MAX) == 0)
356                 {
357                         msg.AddData(CORE_OP_KEY_TYPE, CORE_OP_VALUE_PICK_PERSON_ID_LIST);
358                 }
359                 else
360                 {
361                         SysLog(NID_APP, "returnType must be phone, email, or contactId.");
362                         return E_INVALID_ARG;
363                 }
364         }
365         else
366         {
367                 SysLog(NID_APP, "selectionMode must be either single or multiple.");
368                 return E_INVALID_ARG;
369         }
370
371         __processId = _AppControlManager::GetInstance()->Launch(msg, "contacts-list-efl", NULL, NULL, NULL, OnAppControlResult, 0);
372         
373         SysTryReturnResult(NID_APP, __processId >= 0, E_SYSTEM, "StartAppControl: Launching Contacts List AppControl is failed.");
374
375         return E_SUCCESS;
376 }
377
378 result
379 StartAppControlForAdd(const String* pUri, const String* pMime, const IMap* pMap)
380 {
381         _AppMessageImpl msg;
382         msg.AddData(pMap);
383
384         msg.AddData(CORE_OP_KEY_TYPE, CORE_OP_VALUE_ADD_CONTACT);
385
386         const char* pBuf = appsvc_get_data(msg.GetBundle(), ADD_OP_KEY_NUMBER);
387         if (pBuf != null)
388         {
389                 msg.AddData(CORE_OP_KEY_NUMBER, String(pBuf));
390         }
391
392         pBuf = appsvc_get_data(msg.GetBundle(), ADD_OP_KEY_EMAIL);
393         if (pBuf != null)
394         {
395                 msg.AddData(CORE_OP_KEY_EMAIL, String(pBuf));
396         }
397
398         pBuf = appsvc_get_data(msg.GetBundle(), ADD_OP_KEY_URL);
399         if (pBuf != null)
400         {
401                 msg.AddData(CORE_OP_KEY_URL, String(pBuf));
402         }
403
404         __processId = _AppControlManager::GetInstance()->Launch(msg, "contacts-details-efl", NULL, NULL, NULL, NULL, 0);
405
406         SysTryReturnResult(NID_APP, __processId >= 0, E_SYSTEM, "StartAppControl: Launching Contacts Details AppControl is failed.");
407
408         return E_SUCCESS;
409 }
410
411 result
412 StartAppControlForEdit(const String* pUri, const String* pMime, const IMap* pMap)
413 {
414         _AppMessageImpl msg;
415         msg.AddData(pMap);
416
417         const char* pBuf = appsvc_get_data(msg.GetBundle(), EDIT_OP_KEY_CONTACT_ID);
418         if (pBuf != null)
419         {
420                 int contactId = atoi(pBuf);
421                 int personId = -1;
422
423                 ScopedConnection conn;
424                 if (conn.IsConnected())
425                 {
426                         contacts_record_h contactHandle = null;
427                         int ret = contacts_db_get_record(_contacts_simple_contact._uri, contactId, &contactHandle);
428                         if (ret == CONTACTS_ERROR_NONE)
429                         {
430                                 contacts_record_get_int(contactHandle, _contacts_simple_contact.person_id, &personId);
431                                 contacts_record_destroy(contactHandle, true);
432                         }
433
434                         SysLog(NID_APP, "contact ID(%s) -> person ID(%d).", pBuf, personId);
435                 }
436
437                 msg.AddData(CORE_OP_KEY_TYPE, CORE_OP_VALUE_EDIT_PERSON);
438                 msg.AddData(CORE_OP_KEY_PERSON_ID, Integer::ToString(personId));
439
440                 pBuf = appsvc_get_data(msg.GetBundle(), EDIT_OP_KEY_PHONE);
441                 if (pBuf != null)
442                 {
443                         msg.AddData(CORE_OP_KEY_NUMBER, String(pBuf));
444                 }
445
446                 pBuf = appsvc_get_data(msg.GetBundle(), EDIT_OP_KEY_EMAIL);
447                 if (pBuf != null)
448                 {
449                         msg.AddData(CORE_OP_KEY_EMAIL, String(pBuf));
450                 }
451
452                 pBuf = appsvc_get_data(msg.GetBundle(), EDIT_OP_KEY_URL);
453                 if (pBuf != null)
454                 {
455                         msg.AddData(CORE_OP_KEY_URL, String(pBuf));
456                 }
457
458                 __processId = _AppControlManager::GetInstance()->Launch(msg, "contacts-details-efl", NULL, NULL, NULL, NULL, 0);
459                 
460                 SysTryReturnResult(NID_APP, __processId >= 0, E_SYSTEM, "StartAppControl: Launching Contacts Details AppControl is failed.");
461         }
462         else
463         {
464                 msg.AddData(CORE_OP_KEY_TYPE, CORE_OP_VALUE_EDIT_PERSON_BY_SEL);
465
466                 pBuf = appsvc_get_data(msg.GetBundle(), EDIT_OP_KEY_PHONE);
467                 if (pBuf != null)
468                 {
469                         msg.AddData(CORE_OP_KEY_NUMBER, String(pBuf));
470                 }
471
472                 pBuf = appsvc_get_data(msg.GetBundle(), EDIT_OP_KEY_EMAIL);
473                 if (pBuf != null)
474                 {
475                         msg.AddData(CORE_OP_KEY_EMAIL, String(pBuf));
476                 }
477
478                 pBuf = appsvc_get_data(msg.GetBundle(), EDIT_OP_KEY_URL);
479                 if (pBuf != null)
480                 {
481                         msg.AddData(CORE_OP_KEY_URL, String(pBuf));
482                 }
483
484                 __processId = _AppControlManager::GetInstance()->Launch(msg, "contacts-list-efl", NULL, NULL, NULL, NULL, 0);
485                 
486                 SysTryReturnResult(NID_APP, __processId >= 0, E_SYSTEM, "StartAppControl: Launching Contacts List AppControl is failed.");
487         }
488
489         return E_SUCCESS;
490 }
491
492 result
493 StartAppControlForView( const String* pUri, const String* pMime, const IMap* pMap)
494 {
495
496         _AppMessageImpl msg;
497         msg.AddData(pMap);
498
499         msg.AddData(CORE_OP_KEY_TYPE, CORE_OP_VALUE_VIEW_PERSON);
500
501         const char* pBuf = appsvc_get_data(msg.GetBundle(), VIEW_OP_KEY_VIEW_TYPE);
502         if (pBuf == null)
503         {
504                 SysLog(NID_APP, "viewType has not been set");
505                 return E_SUCCESS;
506         }
507
508         if (strncmp(pBuf, VIEW_OP_VALUE_CONTACT, VIEW_OP_VALUE_LENGTH_MAX) == 0)
509         {
510                 int contactId = -1;
511                 int personId = -1;
512
513                 ScopedConnection conn;
514                 
515                 pBuf = appsvc_get_data(msg.GetBundle(), VIEW_OP_KEY_CONTACT_ID);
516                 if (pBuf != null)
517                 {
518                         contactId = atoi(pBuf);
519
520                         if (conn.IsConnected())
521                         {
522                                 contacts_record_h contactHandle = null;
523                                 int ret = contacts_db_get_record(_contacts_simple_contact._uri, contactId, &contactHandle);
524                                 if (ret == CONTACTS_ERROR_NONE)
525                                 {
526                                         contacts_record_get_int(contactHandle, _contacts_simple_contact.person_id, &personId);
527                                         contacts_record_destroy(contactHandle, true);
528
529                                 }
530                         }
531                 }
532                 else
533                 {
534                         SysLog(NID_APP, "The value for contactId is not set.");
535                         return E_SUCCESS;
536                 }
537
538                 msg.AddData(CORE_OP_KEY_PERSON_ID, Integer::ToString(personId));
539         }
540         else if (strncmp(pBuf, VIEW_OP_VALUE_VCARD, VIEW_OP_VALUE_LENGTH_MAX) == 0)
541         {
542                 pBuf = appsvc_get_data(msg.GetBundle(), VIEW_OP_KEY_VCARD_PATH);
543                 if (pBuf != null)
544                 {
545                         msg.AddData(CORE_OP_KEY_VCARD, pBuf);
546                 }
547                 else
548                 {
549                         SysLog(NID_APP, "The value for path is not set.");
550                         return E_SUCCESS;
551                 }
552
553         }
554         else
555         {
556                 SysLog(NID_APP, "viewType must be either vcf or contact.");
557                 return E_SUCCESS;
558         }
559         
560         __processId = _AppControlManager::GetInstance()->Launch(msg, "contacts-details-efl", NULL, NULL, NULL, NULL, 0);
561
562         SysTryReturnResult(NID_APP, __processId >= 0, E_SYSTEM, "StartAppControl: Launching Contacts Details AppControl is failed.");
563
564         return E_SUCCESS;
565 }
566
567 result
568 StartAppControl(int req, const String& aId, const String& oId, const String* pUri, const String* pMime, const IMap* pMap)
569 {
570         SysLog(NID_APP, "StartAppControl: Entry to Contact AppControl");
571
572         result r = E_SUCCESS;
573
574         const bool isContactPick = _AppControlManager::IsAllowedAppControl(__allowedAppControlPickTable, 4, aId, oId);
575         const bool isContactAdd = _AppControlManager::IsAllowedAppControl(__allowedAppControlAddTable, 4, aId, oId);
576         const bool isContactEdit = _AppControlManager::IsAllowedAppControl(__allowedAppControlEditTable, 4, aId, oId);
577         const bool isContactView = _AppControlManager::IsAllowedAppControl(__allowedAppControlViewTable, 4, aId, oId);
578
579         SysTryReturnResult(NID_APP, isContactPick || isContactAdd || isContactEdit || isContactView, E_SYSTEM, "Invalid AppControl entry for (%ls, %ls).", aId.GetPointer(), oId.GetPointer());
580
581         SysLog(NID_APP, "Contact AppControl : %ls operation.", oId.GetPointer());
582
583         if (isContactPick)
584         {
585                 SysLog(NID_APP, "Contact AppControl : PICK operation.");
586
587                 __req = req;
588
589                 r = StartAppControlForPick(pUri, pMime, pMap);
590                 if (r == E_INVALID_ARG)
591                 {
592                         SysLog(NID_APP, "Finish pick operation.");
593                         _AppControlManager::GetInstance()->FinishAppControl(__req, APP_CTRL_RESULT_FAILED, null);
594
595                         __req = -1;
596
597                         return E_SUCCESS;
598                 }
599                 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] System error.", GetErrorMessage(r));
600
601                 SysLog(NID_APP, "StartAppControl: Launching Contact AppControl succeeded");
602         }
603         else if (isContactAdd)
604         {
605                 SysLog(NID_APP, "Contact AppControl : ADD operation.");
606
607                 r = StartAppControlForAdd(pUri, pMime, pMap);
608                 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] System error.", GetErrorMessage(r));
609
610                 SysLog(NID_APP, "StartAppControl: Launching Contact AppControl succeeded");
611         }
612         else if (isContactEdit)
613         {
614                 SysLog(NID_APP, "Contact AppControl : EDIT operation.");
615
616                 r = StartAppControlForEdit(pUri, pMime, pMap);
617                 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] System error.", GetErrorMessage(r));
618
619                 SysLog(NID_APP, "StartAppControl: Launching Contact AppControl succeeded");
620         }
621         else if (isContactView)
622         {
623                 SysLog(NID_APP, "Contact AppControl : VIEW operation.");
624
625                 r = StartAppControlForView(pUri, pMime, pMap);
626                 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] System error.", GetErrorMessage(r));
627
628                 SysLog(NID_APP, "StartAppControl: Launching Contact AppControl succeeded");
629         }
630
631         return r;
632
633 CATCH:
634         __req = -1;
635         return r;
636 }
637
638 result
639 TerminateAppControl(int req)
640 {
641         if (__processId >= 0)
642         {
643                 _Aul::TerminateApplicationByPid(__processId);           
644         }
645         return E_SUCCESS;
646 }
647
648 void
649 OnAppControlResultForPickNumber(const char* pRawData, HashMap* pResult)
650 {
651         ScopedConnection conn;
652         if (!conn.IsConnected())
653         {
654                 return;
655         }
656
657         int numberId = atoi(pRawData);
658         char* pPhoneNumber = null;
659
660         contacts_record_h recordHandle = null;
661
662         int ret = contacts_db_get_record(_contacts_number._uri, numberId, &recordHandle);
663         if (ret == CONTACTS_ERROR_NONE)
664         {
665                 ScopedHandle numberHandle(recordHandle);
666                 contacts_record_get_str_p(numberHandle.Get(), _contacts_number.number, &pPhoneNumber);
667
668                 pResult->Add(new (std::nothrow) String(PICK_OP_RET_KEY_RET_TYPE), new (std::nothrow) String(PICK_OP_RET_VALUE_PHONE));
669                 pResult->Add(new (std::nothrow) String(PICK_OP_RET_KEY_VALUE), new (std::nothrow) String(pPhoneNumber));
670         }
671 }
672
673 void
674 OnAppControlResultForPickNumberList(const char* pRawData, HashMap* pResult)
675 {
676         ScopedConnection conn;
677         if (!conn.IsConnected())
678         {
679                 return;
680         }
681
682         int id = 0;
683         bool isFirst = true;
684         String numbers;
685         String encodedString(pRawData);
686         std::unique_ptr<ByteBuffer> pDecodeByteBuffer(StringUtil::DecodeBase64StringN(encodedString));
687         
688         while (pDecodeByteBuffer->GetInt(id) == E_SUCCESS)
689         {
690                 char* pNumber = null;
691                 contacts_record_h recordHandle = null;
692                 int ret = contacts_db_get_record(_contacts_number._uri, id, &recordHandle);
693                 if (ret != CONTACTS_ERROR_NONE)
694                 {
695                         continue;
696                 }
697         
698                 ScopedHandle numberHandle(recordHandle);
699                 
700                 contacts_record_get_str_p(numberHandle.Get(), _contacts_number.number, &pNumber);
701
702                 if(!isFirst)
703                 {
704                         numbers.Append(L";");
705                 }
706                 else
707                 {
708                         isFirst = false;
709                 }
710
711                 numbers.Append(pNumber);
712         }
713
714         pResult->Add(new (std::nothrow) String(PICK_OP_RET_KEY_RET_TYPE), new (std::nothrow) String(L"phone"));
715         pResult->Add(new (std::nothrow) String(PICK_OP_RET_KEY_VALUE), new (std::nothrow) String(numbers));
716 }
717
718
719 void
720 OnAppControlResultForPickEmail(const char* pRawData, HashMap* pResult)
721 {
722         ScopedConnection conn;
723         if (!conn.IsConnected())
724         {
725                 return;
726         }
727
728         char* pEmail = null;
729         int emailId = atoi(pRawData);
730         contacts_record_h recordHandle = null;
731
732         int ret = contacts_db_get_record(_contacts_email._uri, emailId, &recordHandle);
733         if (ret != CONTACTS_ERROR_NONE)
734         {
735                 return;
736         }
737
738         ScopedHandle emailHandle(recordHandle);
739
740         contacts_record_get_str_p(emailHandle.Get(), _contacts_email.email, &pEmail);
741
742         pResult->Add(new (std::nothrow) String(PICK_OP_RET_KEY_RET_TYPE), new (std::nothrow) String(PICK_OP_RET_VALUE_EMAIL));
743         pResult->Add(new (std::nothrow) String(PICK_OP_RET_KEY_VALUE), new (std::nothrow) String(pEmail));
744 }
745
746 void
747 OnAppControlResultForPickEmailList(const char* pRawData, HashMap* pResult)
748 {
749         ScopedConnection conn;
750         if (!conn.IsConnected())
751         {
752                 return;
753         }
754
755         int id = 0;
756         bool isFirst = true;
757         String emailIds;
758         String encodedString(pRawData);
759         std::unique_ptr<ByteBuffer> pDecodeByteBuffer(StringUtil::DecodeBase64StringN(encodedString));
760
761         while (pDecodeByteBuffer->GetInt(id) == E_SUCCESS)
762         {
763                 char* pEmail = null;
764                 contacts_record_h recordHandle = null;
765                 int ret = contacts_db_get_record(_contacts_email._uri, id, &recordHandle);
766                 if (ret != CONTACTS_ERROR_NONE)
767                 {
768                         continue;
769                 }
770
771                 ScopedHandle emailHandle(recordHandle);
772                 contacts_record_get_str_p(emailHandle.Get(), _contacts_email.email, &pEmail);
773
774                 if(!isFirst)
775                 {
776                         emailIds.Append(L";");
777                 }
778                 else
779                 {
780                         isFirst = false;
781                 }
782
783                 emailIds.Append(pEmail);
784         }
785
786         pResult->Add(new (std::nothrow) String(PICK_OP_RET_KEY_RET_TYPE), new (std::nothrow) String(L"email"));
787         pResult->Add(new (std::nothrow) String(PICK_OP_RET_KEY_VALUE), new (std::nothrow) String(emailIds));
788 }
789
790 void
791 OnAppControlResultForPickContact(const char* pRawData, HashMap* pResult)
792 {
793         ScopedConnection conn;
794         if (!conn.IsConnected())
795         {
796                 return;
797         }
798
799         int personId = atoi(pRawData);
800         int contactId = 0;
801
802         unsigned int simple_contact_projection[] = {
803                 _contacts_simple_contact.id,
804                 _contacts_simple_contact.address_book_id,
805         };
806
807         contacts_filter_h filterHandle = null;
808         int ret = contacts_filter_create(_contacts_simple_contact._uri, &filterHandle);
809         if (ret != CONTACTS_ERROR_NONE)
810         {
811                 return;
812         }
813
814         ScopedFilter filter(filterHandle);
815         contacts_filter_add_int(filter.Get(), _contacts_simple_contact.person_id, CONTACTS_MATCH_EQUAL, personId);
816
817         contacts_query_h queryHandle = null;
818         ret = contacts_query_create(_contacts_simple_contact._uri, &queryHandle);
819         if (ret != CONTACTS_ERROR_NONE)
820         {
821                 return;
822         }
823
824         ScopedQuery query(queryHandle);
825         ret = contacts_query_set_filter(query.Get(), filter.Get());
826         if (ret != CONTACTS_ERROR_NONE)
827         {
828                 return;
829         }
830
831         ret = contacts_query_set_projection(query.Get(), simple_contact_projection, sizeof(simple_contact_projection)/sizeof(unsigned int));
832         if (ret != CONTACTS_ERROR_NONE)
833         {
834                 return;
835         }
836
837         ret = contacts_query_set_sort(query.Get(), _contacts_simple_contact.address_book_id, true);
838         if (ret != CONTACTS_ERROR_NONE)
839         {
840                 return;
841         }
842
843
844         contacts_list_h listHandle = null;
845         ret = contacts_db_get_records_with_query(query.Get(), 0, 0, &listHandle);
846         if (ret != CONTACTS_ERROR_NONE)
847         {
848                 return;
849         }
850
851         ScopedList contactList(listHandle);
852
853         unsigned int count = 0;
854         contacts_list_get_count(contactList.Get(), &count);
855         if (count <= 0)
856         {
857                 return;
858         }
859
860         contacts_record_h contactHandle = null;
861         contacts_list_get_current_record_p(contactList.Get(), &contactHandle);
862         contacts_record_get_int(contactHandle, _contacts_simple_contact.id, &contactId);
863
864         pResult->Add(new (std::nothrow) String(PICK_OP_RET_KEY_RET_TYPE), new (std::nothrow) String(PICK_OP_RET_VALUE_CONTACT_ID));
865         pResult->Add(new (std::nothrow) String(PICK_OP_RET_KEY_VALUE), new (std::nothrow) String(Integer::ToString(contactId)));
866 }
867
868 void
869 OnAppControlResultForPickContactList(const char* pRawData, HashMap* pResult)
870 {
871         ScopedConnection conn;
872         if (!conn.IsConnected())
873         {
874                 return;
875         }
876
877         String personIds;
878         int id = 0;
879         int contactId;
880         bool isFirst = true;
881         String encodedString(pRawData);
882         std::unique_ptr<ByteBuffer> pDecodeByteBuffer(StringUtil::DecodeBase64StringN(encodedString));
883
884         while (pDecodeByteBuffer->GetInt(id) == E_SUCCESS)
885         {
886                 unsigned int simple_contact_projection[] = {
887                         _contacts_simple_contact.id,
888                         _contacts_simple_contact.address_book_id,
889                 };
890
891                 contacts_record_h contactHandle = null;
892
893                 contacts_filter_h filterHandle = null;
894                 int ret = contacts_filter_create(_contacts_simple_contact._uri, &filterHandle);
895                 if (ret != CONTACTS_ERROR_NONE)
896                 {
897                         break;
898                 }
899
900                 ScopedFilter filter(filterHandle);
901                 
902
903                 contacts_filter_add_int(filter.Get(), _contacts_simple_contact.person_id, CONTACTS_MATCH_EQUAL, id);
904
905                 contacts_query_h queryHandle = null;
906                 ret = contacts_query_create(_contacts_simple_contact._uri, &queryHandle);
907                 if (ret != CONTACTS_ERROR_NONE)
908                 {
909                         break;
910                 }
911
912                 ScopedQuery query(queryHandle);
913
914                 ret = contacts_query_set_filter(query.Get(), filter.Get());
915                 if (ret != CONTACTS_ERROR_NONE)
916                 {
917                         break;
918                 }
919
920                 ret = contacts_query_set_projection(query.Get(), simple_contact_projection, sizeof(simple_contact_projection)/sizeof(unsigned int));
921                 if (ret != CONTACTS_ERROR_NONE)
922                 {
923                         break;
924                 }
925
926                 ret = contacts_query_set_sort(query.Get(), _contacts_simple_contact.address_book_id, true);
927                 if (ret != CONTACTS_ERROR_NONE)
928                 {
929                         break;
930                 }
931
932
933                 contacts_list_h listHandle = null;
934                 ret = contacts_db_get_records_with_query(query.Get(), 0, 0, &listHandle);
935                 if (ret != CONTACTS_ERROR_NONE)
936                 {
937                         break;
938                 }
939
940                 ScopedList contactList(listHandle);
941                 unsigned int count = 0;
942                 contacts_list_get_count(contactList.Get(), &count);
943                 if (count > 0)
944                 {
945                         contacts_list_get_current_record_p(contactList.Get(), &contactHandle);
946                         contacts_record_get_int(contactHandle, _contacts_simple_contact.id, &contactId);
947                         if (!isFirst)
948                         {
949                                 personIds.Append(L";");
950                         }
951                         else
952                         {
953                                 isFirst = false;
954                         }
955
956                         personIds.Append(id);
957
958                 }
959         }
960
961         pResult->Add(new (std::nothrow) String(PICK_OP_RET_KEY_RET_TYPE), new (std::nothrow) String(PICK_OP_RET_VALUE_CONTACT_ID));
962         pResult->Add(new (std::nothrow) String(PICK_OP_RET_KEY_VALUE), new (std::nothrow) String(personIds));
963
964         contacts_disconnect2();
965
966 }
967
968 void
969 OnAppControlResultForPickVcard(const char* pRawData, HashMap* pResult)
970 {
971         pResult->Add(new (std::nothrow) String(PICK_OP_RET_KEY_RET_TYPE), new (std::nothrow) String(PICK_OP_RET_VALUE_VCARD));
972         pResult->Add(new (std::nothrow) String(PICK_OP_RET_KEY_VALUE), new (std::nothrow) String(pRawData));
973 }
974
975 void
976 OnAppControlResult(void* b, int requestCode, service_result_e res, void* userData)
977 {
978         result r = E_SYSTEM;
979         bundle* pBundle = static_cast<bundle*>(b);
980         AppCtrlResult appControlResult = APP_CTRL_RESULT_SUCCEEDED;
981         std::unique_ptr<HashMap, AllElementsDeleter> pResult(null);
982
983         switch (res)
984         {   
985         case SERVICE_RESULT_SUCCEEDED:
986                 {   
987                         pResult.reset(new (std::nothrow) HashMap());
988                         SysTryCatch(NID_APP, pResult != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
989
990                         r = pResult->Construct();
991                         SysTryCatch(NID_APP, !IsFailed(r), , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
992
993                         const char* pBuf = appsvc_get_data(pBundle, CORE_RESULT_TYPE_KEY_NUMBER);
994                         if (pBuf != null)
995                         {
996                                 OnAppControlResultForPickNumber(pBuf, pResult.get());
997                                 break;
998                         }
999
1000                         pBuf = appsvc_get_data(pBundle, CORE_RESULT_TYPE_KEY_EMAIL);
1001                         if (pBuf != null)
1002                         {
1003                                 OnAppControlResultForPickEmail(pBuf, pResult.get());
1004                                 break;
1005                         }
1006
1007                         pBuf = appsvc_get_data(pBundle, CORE_RESULT_TYPE_KEY_PERSON);
1008                         if (pBuf != null)
1009                         {
1010                                 OnAppControlResultForPickContact(pBuf, pResult.get());
1011                                 break;
1012                         }
1013                                 
1014                         pBuf = appsvc_get_data(pBundle, CORE_RESULT_TYPE_KEY_VCARD);
1015                         if (pBuf != null)
1016                         {
1017                                 OnAppControlResultForPickVcard(pBuf, pResult.get());
1018                                 break;
1019                         }
1020
1021                         pBuf = appsvc_get_data(pBundle, CORE_RESULT_TYPE_KEY_NUMBER_LIST);
1022                         if (pBuf != null)
1023                         {
1024                                 OnAppControlResultForPickNumberList(pBuf, pResult.get());
1025                                 break;
1026                         }
1027
1028                         pBuf = appsvc_get_data(pBundle, CORE_RESULT_TYPE_KEY_EMAIL_LIST);
1029                         if (pBuf != null)
1030                         {
1031                                 OnAppControlResultForPickEmailList(pBuf, pResult.get());
1032                                 break;
1033                         }
1034
1035                         pBuf = appsvc_get_data(pBundle, CORE_RESULT_TYPE_KEY_PERSON_LIST);
1036                         if (pBuf != null)
1037                         {
1038                                 OnAppControlResultForPickContactList(pBuf, pResult.get());
1039                                 break;
1040                         }
1041
1042                 }
1043                 break;
1044         case SERVICE_RESULT_FAILED:
1045         case SERVICE_RESULT_CANCELED:
1046         default:
1047                 appControlResult = APP_CTRL_RESULT_FAILED;
1048                 break;
1049         }
1050
1051         _AppControlManager::GetInstance()->FinishAppControl(__req, appControlResult, pResult.release());
1052
1053 CATCH:
1054         __req = -1;
1055 }
1056
1057 #ifdef __cplusplus
1058 }
1059 #endif