2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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.
19 * @author Kisub Song (kisubs.song@samsung.com)
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"
38 using namespace TizenApis::Api::Contact;
39 using namespace WrtDeviceApis::Commons;
52 contacts_svc_struct_free(m_cts);
55 string Contact::convertToString(const string &format)
58 char *vcard_stream = NULL;
60 if(format != "VCARD_21" && format != "VCARD_30")
61 ThrowMsg(ConversionException, "format must be one of 'VCARD_21' or 'VCARD_30'");
63 if(format != "VCARD_30")
64 ThrowMsg(UnsupportedException, "Only support vCard 3.0.");
66 ContactPtr thisObj = SharedFromThis();
67 ContactObjectA2PConverterPtr contactObjConverter(NULL);
69 CTSstruct *platformContact = NULL;
71 contactObjConverter = ContactObjectA2PConverterPtr(new ContactObjectA2PConverter(thisObj, true) );
72 platformContact = contactObjConverter->getPlatformContact();
74 ThrowMsg(PlatformException, "Fail to extract contact to platform object.");
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.");
80 string result((char *)vcard_stream);
87 void Contact::setContactFromString(const std::string &vObjectStr, const std::string &format)
90 const char *vcard_stream = vObjectStr.c_str();
93 if(format != "VCARD_21" && format != "VCARD_30")
94 ThrowMsg(ConversionException, "format must be one of 'VCARD_21' or 'VCARD_30'");
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.");
102 ContactObjectP2AConverterPtr contactObjConverter(NULL);
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.");