Release 4.0.0-preview1-00097
[platform/core/csapi/tizenfx.git] / src / Tizen.Pims.Contacts / Tizen.Pims.Contacts / ContactsVcard.cs
1 /*
2 * Copyright (c) 2016 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 using System;
18
19 namespace Tizen.Pims.Contacts
20 {
21     using ContactsViews;
22
23     /// <summary>
24     /// Delegate for getting a record parsed from a vCard file
25     /// </summary>
26     /// <param name="record">The contacts record</param>
27     /// <returns></returns>
28     public delegate bool ParseCallback(ContactsRecord record);
29
30     /// <summary>
31     /// A class for parsing and making vCards.
32     /// </summary>
33     /// <remarks>
34     /// It's based on the vCard v3.0 specification
35     /// </remarks>
36     public static class ContactsVcard
37     {
38         /// <summary>
39         /// Retrieves the vCard stream from a contacts record.
40         /// </summary>
41         /// <param name="record">The contacts record</param>
42         /// <returns>
43         /// The vCard stream.
44         /// </returns>
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)
49         {
50             int error = 0;
51             string stream = null;
52
53             if (record.Uri.Equals(Person.Uri))
54             {
55                 error = Interop.Vcard.ContactsVcardMakeFromPerson(record._recordHandle, out stream);
56             }
57             else if (record.Uri.Equals(Contact.Uri))
58             {
59                 error = Interop.Vcard.ContactsVcardMakeFromContact(record._recordHandle, out stream);
60             }
61             else if (record.Uri.Equals(MyProfile.Uri))
62             {
63                 error = Interop.Vcard.ContactsVcardMakeFromMyProfile(record._recordHandle, out stream);
64             }
65             else
66             {
67                 throw new ArgumentException("Invalid Parameters Provided");
68             }
69
70             if ((int)ContactsError.None != error)
71             {
72                 Log.Error(Globals.LogTag, "Compose Failed with error " + error);
73                 throw ContactsErrorFactory.CheckAndCreateException(error);
74             }
75
76             return stream;
77         }
78
79         /// <summary>
80         /// Retrieves all contacts with a contacts list from a vCard stream
81         /// </summary>
82         /// <param name="stream">The vCard stream</param>
83         /// <returns>
84         /// The contacts list
85         /// </returns>
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)
89         {
90             IntPtr listHandle;
91
92             int error = Interop.Vcard.ContactsVcardParseToContacts(stream, out listHandle);
93             if ((int)ContactsError.None != error)
94             {
95                 Log.Error(Globals.LogTag, "Parse Failed with error " + error);
96                 throw ContactsErrorFactory.CheckAndCreateException(error);
97             }
98
99             return new ContactsList(listHandle);
100         }
101
102         /// <summary>
103         /// Retrieves all contacts with a record from a vCard file.
104         /// </summary>
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)
111         {
112             Interop.Vcard.ContactsVcardParseCallback cb = (IntPtr handle, IntPtr data) =>
113             {
114                 return callback(new ContactsRecord(handle, true));
115             };
116
117             int error = Interop.Vcard.ContactsVcardParseToContactForeach(path, cb, IntPtr.Zero);
118             if ((int)ContactsError.None != error)
119             {
120                 Log.Error(Globals.LogTag, "ParseForEach Failed with error " + error);
121                 throw ContactsErrorFactory.CheckAndCreateException(error);
122             }
123         }
124     }
125 }