[NUI] TCSACR-226 code change (#1032)
[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     /// Delegates for getting a record parsed from a vCard file.
25     /// </summary>
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     /// <since_tizen> 4 </since_tizen>
29     public delegate bool ParseCallback(ContactsRecord record);
30
31     /// <summary>
32     /// A class for parsing and making the vCards.
33     /// </summary>
34     /// <remarks>
35     /// It's based on the vCard v3.0 specification.
36     /// </remarks>
37     /// <since_tizen> 4 </since_tizen>
38     public static class ContactsVcard
39     {
40         /// <summary>
41         /// Retrieves the vCard stream from a contacts record.
42         /// </summary>
43         /// <param name="record">The contacts record.</param>
44         /// <returns>
45         /// The vCard stream.
46         /// </returns>
47         /// <privilege>http://tizen.org/privilege/contact.read</privilege>
48         /// <feature>http://tizen.org/feature/contact</feature>
49         /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid.</exception>
50         /// <exception cref="OutOfMemoryException">Thrown when failed due to out of memory.</exception>
51         /// <exception cref="NotSupportedException">Thrown when the feature is not supported.</exception>
52         /// <since_tizen> 4 </since_tizen>
53         public static string Compose(ContactsRecord record)
54         {
55             int error = 0;
56             string stream = null;
57
58             if (record.Uri.Equals(Person.Uri))
59             {
60                 error = Interop.Vcard.ContactsVcardMakeFromPerson(record._recordHandle, out stream);
61             }
62             else if (record.Uri.Equals(Contact.Uri))
63             {
64                 error = Interop.Vcard.ContactsVcardMakeFromContact(record._recordHandle, out stream);
65             }
66             else if (record.Uri.Equals(MyProfile.Uri))
67             {
68                 error = Interop.Vcard.ContactsVcardMakeFromMyProfile(record._recordHandle, out stream);
69             }
70             else
71             {
72                 throw new ArgumentException("Invalid Parameters Provided");
73             }
74
75             if ((int)ContactsError.None != error)
76             {
77                 Log.Error(Globals.LogTag, "Compose Failed with error " + error);
78                 throw ContactsErrorFactory.CheckAndCreateException(error);
79             }
80
81             return stream;
82         }
83
84         /// <summary>
85         /// Retrieves all the contacts with a contacts list from a vCard stream.
86         /// </summary>
87         /// <param name="stream">The vCard stream.</param>
88         /// <returns>
89         /// The contacts list.
90         /// </returns>
91         /// <feature>http://tizen.org/feature/contact</feature>
92         /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid.</exception>
93         /// <exception cref="OutOfMemoryException">Thrown when failed due to out of memory.</exception>
94         /// <exception cref="NotSupportedException">Thrown when the feature is not supported.</exception>
95         /// <since_tizen> 4 </since_tizen>
96         public static ContactsList Parse(string stream)
97         {
98             IntPtr listHandle;
99
100             int error = Interop.Vcard.ContactsVcardParseToContacts(stream, out listHandle);
101             if ((int)ContactsError.None != error)
102             {
103                 Log.Error(Globals.LogTag, "Parse Failed with error " + error);
104                 throw ContactsErrorFactory.CheckAndCreateException(error);
105             }
106
107             return new ContactsList(listHandle);
108         }
109
110         /// <summary>
111         /// Retrieves all the contacts with a record from a vCard file.
112         /// </summary>
113         /// <param name="path">The file path of a vCard stream file.</param>
114         /// <param name="callback">The callback function to invoke.</param>
115         /// <feature>http://tizen.org/feature/contact</feature>
116         /// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
117         /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid.</exception>
118         /// <exception cref="OutOfMemoryException">Thrown when failed due to out of memory.</exception>
119         /// <exception cref="NotSupportedException">Thrown when the feature is not supported.</exception>
120         /// <since_tizen> 4 </since_tizen>
121         public static void ParseForEach(string path, ParseCallback callback)
122         {
123             Interop.Vcard.ContactsVcardParseCallback cb = (IntPtr handle, IntPtr data) =>
124             {
125                 return callback(new ContactsRecord(handle, true));
126             };
127
128             int error = Interop.Vcard.ContactsVcardParseToContactForeach(path, cb, IntPtr.Zero);
129             if ((int)ContactsError.None != error)
130             {
131                 Log.Error(Globals.LogTag, "ParseForEach Failed with error " + error);
132                 throw ContactsErrorFactory.CheckAndCreateException(error);
133             }
134         }
135     }
136 }