Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.Pims.Calendar / Tizen.Pims.Calendar / CalendarVcalendar.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 System.Threading.Tasks;
20
21 namespace Tizen.Pims.Calendar
22 {
23     /// <summary>
24     /// A class for parsing and composing vCalendar.
25     /// </summary>
26     /// <remarks>
27     /// It's based on the vCalendar v2.0 specification
28     /// </remarks>
29     public class CalendarVcalendar
30     {
31         internal CalendarVcalendar()
32         {
33         }
34
35         public delegate bool ParseDelegate(CalendarRecord record);
36
37         /// <summary>
38         /// Retrieves a vcalendar stream from a calendar list.
39         /// </summary>
40         /// <param name="list">The calendar list</param>
41         /// <returns>
42         /// The composed stream.
43         /// </returns>
44         /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
45         /// <exception cref="OutOfMemoryException">Thrown when failed due to out of memory</exception>
46         public static string Compose(CalendarList list)
47         {
48             string stream;
49             int error = Interop.Calendar.Vcalendar.Compose(list._listHandle, out stream);
50             if (CalendarError.None != (CalendarError)error)
51             {
52                 Log.Error(Globals.LogTag, "MakeVcalendar Failed with error " + error);
53                 throw CalendarErrorFactory.GetException(error);
54             }
55             return stream;
56         }
57
58         /// <summary>
59         /// Retrieves all calendars from a vcalendar stream.
60         /// </summary>
61         /// <param name="stream">The vcalendar stream</param>
62         /// <returns>
63         /// the record list
64         /// </returns>
65         /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
66         /// <exception cref="OutOfMemoryException">Thrown when failed due to out of memory</exception>
67         public static CalendarList Parse(string stream)
68         {
69             int error = 0;
70             IntPtr _listHandle;
71             error = Interop.Calendar.Vcalendar.Parse(stream, out _listHandle);
72             if (CalendarError.None != (CalendarError)error)
73             {
74                 Log.Error(Globals.LogTag, "Parse Vcalendar Failed [" + error + "]");
75                 throw CalendarErrorFactory.GetException(error);
76             }
77             return new CalendarList(_listHandle);
78         }
79
80         /// <summary>
81         /// Parse vcalendar file with foreach
82         /// </summary>
83         /// <param name="path">The file path of the vCalendar stream file</param>
84         /// <param name="callback">he callback function to invoke</param>
85         /// <exception cref="InvalidOperationException">Thrown when method failed due to invalid operation</exception>
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 void ParseForEach(string path, ParseDelegate callback)
89         {
90             int error = 0;
91
92             Interop.Calendar.Vcalendar.ParseCallback cb = (IntPtr handle, IntPtr data) =>
93             {
94                 return callback(new CalendarRecord(handle, true));
95             };
96
97             error = Interop.Calendar.Vcalendar.ParseForEach(path, cb, IntPtr.Zero);
98             if (CalendarError.None != (CalendarError)error)
99             {
100                 Log.Error(Globals.LogTag, "Parse foreach Vcalendar Failed [" + error + "]");
101                 throw CalendarErrorFactory.GetException(error);
102             }
103         }
104     }
105 }