2 * Copyright (c) 2016 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 namespace Tizen.Pims.Contacts
24 /// Delegate for getting a record parsed from a vCard file
26 /// <param name="record">The contacts record</param>
27 /// <returns> true to continue with the next iteration of the loop, otherwise false to break out of the loop</returns>
28 public delegate bool ParseCallback(ContactsRecord record);
31 /// A class for parsing and making vCards.
34 /// It's based on the vCard v3.0 specification
36 public static class ContactsVcard
39 /// Retrieves the vCard stream from a contacts record.
41 /// <param name="record">The contacts record</param>
45 /// <privilege>http://tizen.org/privilege/contact.read</privilege>
46 /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
47 /// <exception cref="OutOfMemoryException">Thrown when failed due to out of memory</exception>
48 public static string Compose(ContactsRecord record)
53 if (record.Uri.Equals(Person.Uri))
55 error = Interop.Vcard.ContactsVcardMakeFromPerson(record._recordHandle, out stream);
57 else if (record.Uri.Equals(Contact.Uri))
59 error = Interop.Vcard.ContactsVcardMakeFromContact(record._recordHandle, out stream);
61 else if (record.Uri.Equals(MyProfile.Uri))
63 error = Interop.Vcard.ContactsVcardMakeFromMyProfile(record._recordHandle, out stream);
67 throw new ArgumentException("Invalid Parameters Provided");
70 if ((int)ContactsError.None != error)
72 Log.Error(Globals.LogTag, "Compose Failed with error " + error);
73 throw ContactsErrorFactory.CheckAndCreateException(error);
80 /// Retrieves all contacts with a contacts list from a vCard stream
82 /// <param name="stream">The vCard stream</param>
86 /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
87 /// <exception cref="OutOfMemoryException">Thrown when failed due to out of memory</exception>
88 public static ContactsList Parse(string stream)
92 int error = Interop.Vcard.ContactsVcardParseToContacts(stream, out listHandle);
93 if ((int)ContactsError.None != error)
95 Log.Error(Globals.LogTag, "Parse Failed with error " + error);
96 throw ContactsErrorFactory.CheckAndCreateException(error);
99 return new ContactsList(listHandle);
103 /// Retrieves all contacts with a record from a vCard file.
105 /// <param name="path">The file path of vCard stream file</param>
106 /// <param name="callback">The callback function to invoke</param>
107 /// <exception cref="InvalidOperationException">Thrown when method failed due to invalid operation</exception>
108 /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
109 /// <exception cref="OutOfMemoryException">Thrown when failed due to out of memory</exception>
110 public static void ParseForEach(string path, ParseCallback callback)
112 Interop.Vcard.ContactsVcardParseCallback cb = (IntPtr handle, IntPtr data) =>
114 return callback(new ContactsRecord(handle, true));
117 int error = Interop.Vcard.ContactsVcardParseToContactForeach(path, cb, IntPtr.Zero);
118 if ((int)ContactsError.None != error)
120 Log.Error(Globals.LogTag, "ParseForEach Failed with error " + error);
121 throw ContactsErrorFactory.CheckAndCreateException(error);