4a2c84b6efe6004f8131120bfda15b3273dcf525
[framework/web/wrt-plugins-tizen.git] / src / Contact / Contact.cpp
1 //
2 // Tizen Web Device API
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        Contact.cpp
20  * @author      Kisub Song (kisubs.song@samsung.com)
21  * @version     0.1
22  * @brief
23  */
24
25 #include "Contact.h"
26
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 DeviceAPI {
35 namespace Contact {
36
37 using namespace WrtDeviceApis::Commons;
38 using namespace std;
39
40 Contact::Contact()
41         : IContact()
42         , m_platformContactObject(NULL)
43         , m_platformContactObjectList(NULL)
44 {
45 //      LogDebug("entered");
46
47         m_notesJSObjIsSet = false;
48         m_groupIdsJSObjIsSet = false;
49         m_context = NULL;
50         m_groupIdsObj = NULL;
51         m_groupIdsJSValue = NULL;
52 }
53
54 Contact::~Contact()
55 {
56         if (m_platformContactObjectList != NULL)
57                 contacts_list_destroy(m_platformContactObjectList, true);
58
59         if(groupIdsJSObjIsSet()){
60                 JSContextRef contextRef = getContext();
61                 JSObjectRef tempJSObject = getGroupIdsJSObj();
62                 JSValueUnprotect(contextRef, tempJSObject);
63         }
64
65         if(notesJSObjIsSet()){
66                 JSContextRef contextRef = getContext();
67                 JSObjectRef tempJSObject = getNotesJSObj();
68                 JSValueUnprotect(contextRef, tempJSObject);
69         }
70 }
71
72 string Contact::convertToString(const string &format)
73 {
74         LogDebug("entered");
75         int errorCode = 0;
76         char *vcard_stream = NULL;
77
78         if(!format.empty() && format != "VCARD_30")
79                 ThrowMsg(ConversionException, "format must be 'VCARD_30'");
80
81         ContactPtr thisObj = SharedFromThis();
82         ContactObjectA2PConverterPtr contactObjConverter(NULL);
83
84         contacts_record_h contacts_record = NULL;
85         Try {
86                 contactObjConverter = ContactObjectA2PConverterPtr(new ContactObjectA2PConverter(thisObj, true) );
87                 contacts_record = contactObjConverter->getPlatformContact();
88         } Catch (Exception) {
89                 ThrowMsg(PlatformException, "Fail to extract contact to platform object.");
90         }
91         errorCode = contacts_vcard_make_from_contact(contacts_record, &vcard_stream);
92         if(errorCode != CONTACTS_ERROR_NONE)
93                 ThrowMsg(PlatformException, "Fail to convert to vCard.");
94
95         string result((char *)vcard_stream);
96
97         free(vcard_stream);
98
99         return result;
100 }
101
102 void Contact::setContactFromString(const std::string &vObjectStr)
103 {
104         LogDebug("entered");
105         int errorCode = 0;
106         const char *vcard_stream = vObjectStr.c_str();
107
108         contacts_list_h contacts_list = NULL;
109         errorCode = contacts_vcard_parse_to_contacts(vcard_stream, &contacts_list);
110         if(errorCode == CONTACTS_ERROR_INVALID_PARAMETER)
111                 ThrowMsg(InvalidArgumentException, "Invalid vCard string.");
112         else if(errorCode != CONTACTS_ERROR_NONE)
113                 ThrowMsg(PlatformException, "Fail to convert contact from vCard.");
114
115         unsigned int record_count = 0;
116         errorCode = contacts_list_get_count(contacts_list, &record_count);
117         if(errorCode != CONTACTS_ERROR_NONE || record_count == 0)
118         {
119                 contacts_list_destroy(contacts_list, true);
120                 ThrowMsg(InvalidArgumentException, "Invalid vCard string. (2)");
121         }
122
123         contacts_record_h contacts_record;
124         contacts_list_first(contacts_list);
125         errorCode = contacts_list_get_current_record_p(contacts_list, &contacts_record);
126         if(errorCode != CONTACTS_ERROR_NONE || contacts_record == NULL)
127         {
128                 contacts_list_destroy(contacts_list, true);
129                 ThrowMsg(InvalidArgumentException, "Invalid vCard string. (3)");
130         }
131
132         ContactObjectP2AConverterPtr contactObjConverter(NULL);
133         Try {
134                 ContactPtr thisObj = SharedFromThis();
135                 contactObjConverter = ContactObjectP2AConverterPtr(new ContactObjectP2AConverter(contacts_record, true, thisObj) );
136                 contactObjConverter->getAbstractContact();
137         } Catch (Exception) {
138                 ThrowMsg(PlatformException, "Fail to extract contact from platform object.");
139         }
140
141         m_platformContactObject = contacts_record;
142         m_platformContactObjectList = contacts_list;
143 }
144
145 void Contact::setNotesJSObj(bool value, JSObjectRef initObj)
146 {
147         m_notesJSObjIsSet = value;
148         m_notesObj = initObj;
149 }
150
151 bool Contact::notesJSObjIsSet() const
152 {
153         return m_notesJSObjIsSet;
154 }
155
156 JSObjectRef Contact::getNotesJSObj()
157 {
158         return m_notesObj;
159 }
160
161 JSValueRef Contact::getNotesJSValue()
162 {
163         return m_notesJSValue;
164 }
165
166 void Contact::setGroupIdsJSObj(bool value, JSObjectRef initObj)
167 {
168         m_groupIdsJSObjIsSet = value;
169         m_groupIdsObj = initObj;
170 }
171
172 bool Contact::groupIdsJSObjIsSet() const
173 {
174         return m_groupIdsJSObjIsSet;
175 }
176
177 JSObjectRef Contact::getGroupIdsJSObj()
178 {
179         return m_groupIdsObj;
180 }
181
182 JSValueRef Contact::getGroupIdsJSValue()
183 {
184         return m_groupIdsJSValue;
185 }
186
187 void Contact::setContext(JSContextRef contextRef)
188 {
189         if(m_context == NULL)
190                 m_context = contextRef;
191 }
192
193 JSContextRef Contact::getContext()
194 {
195         return m_context;
196 }
197
198 } // Contact
199 } // DeviceAPI