Release 4.0.0-preview1-00051
[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 using System.Collections.Generic;
19 using static Interop.Contacts;
20
21 namespace Tizen.Pims.Contacts
22 {
23     using static ContactsViews;
24     /// <summary>
25     /// A class for parsing and making vCards.
26     /// </summary>
27     /// <remarks>
28     /// It's based on the vCard v3.0 specification
29     /// </remarks>
30     public static class ContactsVcard
31     {
32         public delegate bool ParseDelegate(ContactsRecord record);
33
34         /// <summary>
35         /// Retrieves the vCard stream from a contacts record.
36         /// </summary>
37         /// <param name="record">The contacts record</param>
38         /// <returns>
39         /// The vCard stream.
40         /// </returns>
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)
45         {
46             int error = 0;
47             string stream = null;
48
49             if (record.Uri.Equals(Person.Uri))
50             {
51                 error = Interop.Vcard.ContactsVcardMakeFromPerson(record._recordHandle, out stream);
52             }
53             else if (record.Uri.Equals(Contact.Uri))
54             {
55                 error = Interop.Vcard.ContactsVcardMakeFromContact(record._recordHandle, out stream);
56             }
57             else if (record.Uri.Equals(MyProfile.Uri))
58             {
59                 error = Interop.Vcard.ContactsVcardMakeFromMyProfile(record._recordHandle, out stream);
60             }
61             else
62             {
63                 throw new ArgumentException("Invalid Parameters Provided");
64             }
65
66             if ((int)ContactsError.None != error)
67             {
68                 Log.Error(Globals.LogTag, "Compose Failed with error " + error);
69                 throw ContactsErrorFactory.CheckAndCreateException(error);
70             }
71
72             return stream;
73         }
74
75         /// <summary>
76         /// Retrieves all contacts with a contacts list from a vCard stream
77         /// </summary>
78         /// <param name="stream">The vCard stream</param>
79         /// <returns>
80         /// The contacts list
81         /// </returns>
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)
85         {
86             IntPtr listHandle;
87
88             int error = Interop.Vcard.ContactsVcardParseToContacts(stream, out listHandle);
89             if ((int)ContactsError.None != error)
90             {
91                 Log.Error(Globals.LogTag, "Parse Failed with error " + error);
92                 throw ContactsErrorFactory.CheckAndCreateException(error);
93             }
94
95             return new ContactsList(listHandle);
96         }
97
98         /// <summary>
99         /// Retrieves all contacts with a record from a vCard file.
100         /// </summary>
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)
107         {
108             Interop.Vcard.ContactsVcardParseCallback cb = (IntPtr handle, IntPtr data) =>
109             {
110                 return callback(new ContactsRecord(handle, true));
111             };
112
113             int error = Interop.Vcard.ContactsVcardParseToContactForeach(path, cb, IntPtr.Zero);
114             if ((int)ContactsError.None != error)
115             {
116                 Log.Error(Globals.LogTag, "ParseForEach Failed with error " + error);
117                 throw ContactsErrorFactory.CheckAndCreateException(error);
118             }
119         }
120     }
121 }