[Contact] Refactoring API structures
[profile/ivi/wrt-plugins-tizen.git] / src / platform / Tizen / Contact / Contact.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
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        Contact.cpp
19  * @author      Kisub Song (kisubs.song@samsung.com)
20  * @version     0.1
21  * @brief
22  */
23
24 #include "Contact.h"
25
26 #include <pcrecpp.h>
27 #include <dpl/log/log.h>
28 #include <dpl/exception.h>
29 #include <Commons/Exception.h>
30 #include <Commons/Regex.h>
31 #include "ContactObjectA2PConverter.h"
32 #include "ContactObjectP2AConverter.h"
33
34 namespace TizenApis {
35 namespace Platform {
36 namespace Contact {
37
38 using namespace TizenApis::Api::Contact;
39 using namespace WrtDeviceApis::Commons;
40 using namespace std;
41
42 Contact::Contact()
43         : IContact()
44         , m_cts(NULL)
45 {
46         LogDebug("entered");
47 }
48
49 Contact::~Contact()
50 {
51         if (m_cts != NULL)
52                 contacts_svc_struct_free(m_cts);
53 }
54
55 string Contact::convertToString(const string &format)
56 {
57         LogDebug("entered");
58         char *vcard_stream = NULL;
59
60         if(format != "VCARD_21" && format != "VCARD_30")
61                 ThrowMsg(ConversionException, "format must be one of 'VCARD_21' or 'VCARD_30'");
62
63         if(format != "VCARD_30")
64                 ThrowMsg(UnsupportedException, "Only support vCard 3.0.");
65
66         ContactPtr thisObj = SharedFromThis();
67         ContactObjectA2PConverterPtr contactObjConverter(NULL);
68
69         CTSstruct *platformContact = NULL;
70         Try {
71                 contactObjConverter = ContactObjectA2PConverterPtr(new ContactObjectA2PConverter(thisObj, true) );
72                 platformContact = contactObjConverter->getPlatformContact();
73         } Catch (Exception) {
74                 ThrowMsg(PlatformException, "Fail to extract contact to platform object.");
75         }
76         int ret = contacts_svc_get_vcard_from_contact(platformContact, &vcard_stream);
77         if(ret != CTS_SUCCESS)
78                 ThrowMsg(PlatformException, "Fail to convert to vCard.");
79
80         string result((char *)vcard_stream);
81
82         free(vcard_stream);
83
84         return result;
85 }
86
87 void Contact::setContactFromString(const std::string &vObjectStr, const std::string &format)
88 {
89         LogDebug("entered");
90         const char *vcard_stream = vObjectStr.c_str();
91         CTSstruct* cts;
92
93         if(format != "VCARD_21" && format != "VCARD_30")
94                 ThrowMsg(ConversionException, "format must be one of 'VCARD_21' or 'VCARD_30'");
95
96         int ret = contacts_svc_get_contact_from_vcard(vcard_stream, &cts);
97         if(CTS_ERR_ARG_INVALID == ret)
98                 ThrowMsg(InvalidArgumentException, "Invalid vCard string.");
99         else if(CTS_SUCCESS != ret)
100                 ThrowMsg(PlatformException, "Fail to convert contact from vCard.");
101
102         ContactObjectP2AConverterPtr contactObjConverter(NULL);
103         Try {
104                 ContactPtr thisObj = SharedFromThis();
105                 contactObjConverter = ContactObjectP2AConverterPtr(new ContactObjectP2AConverter(cts, true, thisObj) );
106                 contactObjConverter->getAbstractContact();
107         } Catch (Exception) {
108                 ThrowMsg(PlatformException, "Fail to extract contact from platform object.");
109         }
110
111         m_cts = cts;
112 }
113
114 } // Contact
115 } // Platform
116 } // TizenApis