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.
18 using System.Collections.Generic;
19 using static Interop.Contacts;
21 namespace Tizen.Pims.Contacts
23 using static ContactsViews;
25 /// A class for parsing and making vCards.
28 /// It's based on the vCard v3.0 specification
30 public static class ContactsVcard
32 public delegate bool ParseDelegate(ContactsRecord record);
35 /// Retrieves the vCard stream from a contacts record.
37 /// <param name="record">The contacts record</param>
41 /// <privilege>http://tizen.org/privilege/contact.read</privilege>
42 /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
43 /// <exception cref="OutOfMemoryException">Thrown when failed due to out of memory</exception>
44 public static string Compose(ContactsRecord record)
49 if (record.Uri.Equals(Person.Uri))
51 error = Interop.Vcard.ContactsVcardMakeFromPerson(record._recordHandle, out stream);
53 else if (record.Uri.Equals(Contact.Uri))
55 error = Interop.Vcard.ContactsVcardMakeFromContact(record._recordHandle, out stream);
57 else if (record.Uri.Equals(MyProfile.Uri))
59 error = Interop.Vcard.ContactsVcardMakeFromMyProfile(record._recordHandle, out stream);
63 throw new ArgumentException("Invalid Parameters Provided");
66 if ((int)ContactsError.None != error)
68 Log.Error(Globals.LogTag, "Compose Failed with error " + error);
69 throw ContactsErrorFactory.CheckAndCreateException(error);
76 /// Retrieves all contacts with a contacts list from a vCard stream
78 /// <param name="stream">The vCard stream</param>
82 /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
83 /// <exception cref="OutOfMemoryException">Thrown when failed due to out of memory</exception>
84 public static ContactsList Parse(string stream)
88 int error = Interop.Vcard.ContactsVcardParseToContacts(stream, out listHandle);
89 if ((int)ContactsError.None != error)
91 Log.Error(Globals.LogTag, "Parse Failed with error " + error);
92 throw ContactsErrorFactory.CheckAndCreateException(error);
95 return new ContactsList(listHandle);
99 /// Retrieves all contacts with a record from a vCard file.
101 /// <param name="path">The file path of vCard stream file</param>
102 /// <param name="callback">The callback function to invoke</param>
103 /// <exception cref="InvalidOperationException">Thrown when method failed due to invalid operation</exception>
104 /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
105 /// <exception cref="OutOfMemoryException">Thrown when failed due to out of memory</exception>
106 public static void ParseForEach(string path, ParseDelegate callback)
108 Interop.Vcard.ContactsVcardParseCallback cb = (IntPtr handle, IntPtr data) =>
110 return callback(new ContactsRecord(handle, true));
113 int error = Interop.Vcard.ContactsVcardParseToContactForeach(path, cb, IntPtr.Zero);
114 if ((int)ContactsError.None != error)
116 Log.Error(Globals.LogTag, "ParseForEach Failed with error " + error);
117 throw ContactsErrorFactory.CheckAndCreateException(error);