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 System.Runtime.InteropServices;
21 namespace Tizen.Pims.Calendar
24 /// A record represents an actual record in the database
27 /// A record represents an actual record in the database,
28 /// but you can also consider it a piece of information, such as an alarm, attendee and extended.
29 /// A record can be a complex set of data, containing other data.
30 /// For example, a calendar record contains the alarm property, which is a reference to an alarm record.
31 /// An alarm record could belong to a event record,
32 /// and its alarm id property is set to the identifier of the corresponding event.
33 /// In this case, the alarm is the child record of the event and the event is the parent record.
35 public class CalendarRecord : IDisposable
39 private Int64 _memoryPressure = CalendarViews.AverageSizeOfRecord;
40 internal IntPtr _recordHandle;
42 internal CalendarRecord(IntPtr handle)
44 _recordHandle = handle;
47 int error = Interop.Calendar.Record.GetUriPointer(handle, out viewUri);
48 if (CalendarError.None != (CalendarError)error)
50 Log.Error(Globals.LogTag, "GetUriPointer Failed with error " + error);
51 throw CalendarErrorFactory.GetException(error);
53 _uri = Marshal.PtrToStringAnsi(viewUri);
54 GC.AddMemoryPressure(_memoryPressure);
57 internal CalendarRecord(IntPtr handle, bool disposedValue)
59 _recordHandle = handle;
60 _disposedValue = disposedValue;
63 int error = Interop.Calendar.Record.GetUriPointer(handle, out viewUri);
64 if (CalendarError.None != (CalendarError)error)
66 Log.Error(Globals.LogTag, "GetUriPointer Failed with error " + error);
67 throw CalendarErrorFactory.GetException(error);
69 _uri = Marshal.PtrToStringAnsi(viewUri);
71 GC.AddMemoryPressure(_memoryPressure);
74 internal CalendarRecord(IntPtr handle, int id)
76 _recordHandle = handle;
80 int error = Interop.Calendar.Record.GetUriPointer(handle, out viewUri);
81 if (CalendarError.None != (CalendarError)error)
83 Log.Error(Globals.LogTag, "GetUriPointer Failed with error " + error);
84 throw CalendarErrorFactory.GetException(error);
86 _uri = Marshal.PtrToStringAnsi(viewUri);
87 GC.AddMemoryPressure(_memoryPressure);
93 /// <param name="viewUri">The view URI</param>
94 /// <exception cref="NotSupportedException">Thrown when an invoked method is not supported</exception>
95 /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
96 /// <exception cref="OutOfMemoryException">Thrown when failed due to out of memory</exception>
97 public CalendarRecord(string viewUri)
100 error = Interop.Calendar.Record.Create(viewUri, out _recordHandle);
101 if (CalendarError.None != (CalendarError)error)
103 Log.Error(Globals.LogTag, "CalendarRecord Failed with error " + error);
104 throw CalendarErrorFactory.GetException(error);
107 GC.AddMemoryPressure(_memoryPressure);
115 #region IDisposable Support
116 /// To detect redundant calls
117 internal bool _disposedValue = false;
119 protected virtual void Dispose(bool disposing)
123 Log.Debug(Globals.LogTag, "Dispose :" + disposing);
125 int error = Interop.Calendar.Record.Destroy(_recordHandle, false);
126 if (CalendarError.None != (CalendarError)error)
128 Log.Error(Globals.LogTag, "Destroy Failed with error " + error);
129 throw CalendarErrorFactory.GetException(error);
131 _disposedValue = true;
132 GC.RemoveMemoryPressure(_memoryPressure);
137 /// Releases all resources used by the CalendarRecord.
138 /// It should be called after finished using of the object.
140 public void Dispose()
146 internal static Interop.Calendar.Record.DateTime ConvertCalendarTimeToStruct(CalendarTime value)
148 Interop.Calendar.Record.DateTime time = new Interop.Calendar.Record.DateTime();
149 time.type = value._type;
151 if ((int)CalendarTime.Type.Utc == value._type)
153 DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0);
154 time.utime = (value.UtcTime.Ticks - epoch.Ticks) / 10000000;
158 time.year = value.LocalTime.Year;
159 time.month = value.LocalTime.Month;
160 time.mday = value.LocalTime.Day;
161 time.hour = value.LocalTime.Hour;
162 time.minute = value.LocalTime.Minute;
163 time.second = value.LocalTime.Second;
168 internal static CalendarTime ConvertIntPtrToCalendarTime(Interop.Calendar.Record.DateTime time)
171 if ((int)CalendarTime.Type.Utc == time.type)
173 DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0);
174 value = new CalendarTime(time.utime * 10000000 + epoch.Ticks);
178 value = new CalendarTime(time.year, time.month, time.mday, time.hour, time.minute, time.second);
180 value._type = time.type;
185 /// Makes a clone of a record.
190 /// <exception cref="OutOfMemoryException">Thrown when failed due to out of memory</exception>
191 public CalendarRecord Clone()
193 IntPtr _clonedRecordHandle;
194 int error = Interop.Calendar.Record.Clone(_recordHandle, out _clonedRecordHandle);
195 if (CalendarError.None != (CalendarError)error)
197 Log.Error(Globals.LogTag, "Clone Failed with error " + error);
198 throw CalendarErrorFactory.GetException(error);
200 return new CalendarRecord(_clonedRecordHandle, (int)_id);
204 /// The URI of the record
215 /// Gets a object from a record.
217 /// <param name="propertyId">The property ID</param>
219 /// The value of the property corresponding to property id.
221 /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
222 public T Get<T>(uint propertyId)
224 object parsedValue = null;
225 if (typeof(T) == typeof(string))
228 int error = Interop.Calendar.Record.GetString(_recordHandle, propertyId, out val);
229 if (CalendarError.None != (CalendarError)error)
231 Log.Error(Globals.LogTag, "Get String Failed [" + error + "]" + String.Format("{0:X}", propertyId));
232 throw CalendarErrorFactory.GetException(error);
234 parsedValue = Convert.ChangeType(val, typeof(T));
236 else if (typeof(T) == typeof(int))
239 int error = Interop.Calendar.Record.GetInteger(_recordHandle, propertyId, out val);
240 if (CalendarError.None != (CalendarError)error)
242 Log.Error(Globals.LogTag, "Get Intger Failed [" + error + "]" + String.Format("{0:X}", propertyId));
243 throw CalendarErrorFactory.GetException(error);
245 parsedValue = Convert.ChangeType(val, typeof(T));
247 else if (typeof(T) == typeof(long))
250 int error = Interop.Calendar.Record.GetLli(_recordHandle, propertyId, out val);
251 if (CalendarError.None != (CalendarError)error)
253 Log.Error(Globals.LogTag, "Get Long Failed [" + error + "]" + String.Format("{0:X}", propertyId));
254 throw CalendarErrorFactory.GetException(error);
256 parsedValue = Convert.ChangeType(val, typeof(T));
258 else if (typeof(T) == typeof(double))
261 int error = Interop.Calendar.Record.GetDouble(_recordHandle, propertyId, out val);
262 if (CalendarError.None != (CalendarError)error)
264 Log.Error(Globals.LogTag, "Get Double Failed [" + error + "]" + String.Format("{0:X}", propertyId));
265 throw CalendarErrorFactory.GetException(error);
267 parsedValue = Convert.ChangeType(val, typeof(T));
269 else if (typeof(T) == typeof(CalendarTime))
271 Interop.Calendar.Record.DateTime time;
272 int error = Interop.Calendar.Record.GetCalendarTime(_recordHandle, propertyId, out time);
273 if (CalendarError.None != (CalendarError)error)
275 Log.Error(Globals.LogTag, "Get CalendarTime Failed [" + error + "]" + String.Format("{0:X}", propertyId));
276 throw CalendarErrorFactory.GetException(error);
278 CalendarTime val = ConvertIntPtrToCalendarTime(time);
279 parsedValue = Convert.ChangeType(val, typeof(T));
283 Log.Error(Globals.LogTag, "Not supported Data T/ype");
284 throw CalendarErrorFactory.GetException((int)CalendarError.NotSupported);
286 return (T)parsedValue;
290 /// Sets a value of the property to a record.
292 /// <param name="propertyId">The property ID</param>
293 /// <param name="value">value</param>
294 /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
295 public void Set<T>(uint propertyId, T value)
297 if (typeof(T) == typeof(string))
299 string val = Convert.ToString(value);
300 int error = Interop.Calendar.Record.SetString(_recordHandle, propertyId, val);
301 if (CalendarError.None != (CalendarError)error)
303 Log.Error(Globals.LogTag, "Set String Failed [" + error + "]" + String.Format("{0:X}", propertyId));
304 throw CalendarErrorFactory.GetException(error);
307 else if (typeof(T) == typeof(int))
309 int val = Convert.ToInt32(value);
310 int error = Interop.Calendar.Record.SetInteger(_recordHandle, propertyId, val);
311 if (CalendarError.None != (CalendarError)error)
313 Log.Error(Globals.LogTag, "Set Integer Failed [" + error + "]" + String.Format("{0:X}", propertyId));
314 throw CalendarErrorFactory.GetException(error);
317 else if (typeof(T) == typeof(long))
319 long val = Convert.ToInt64(value);
320 int error = Interop.Calendar.Record.SetLli(_recordHandle, propertyId, val);
321 if (CalendarError.None != (CalendarError)error)
323 Log.Error(Globals.LogTag, "Set Long Failed [" + error + "]" + String.Format("{0:X}", propertyId));
324 throw CalendarErrorFactory.GetException(error);
327 else if (typeof(T) == typeof(double))
329 double val = Convert.ToDouble(value);
330 int error = Interop.Calendar.Record.SetDouble(_recordHandle, propertyId, val);
331 if (CalendarError.None != (CalendarError)error)
333 Log.Error(Globals.LogTag, "Set Double Failed [" + error + "]" + String.Format("{0:X}", propertyId));
334 throw CalendarErrorFactory.GetException(error);
337 else if (typeof(T) == typeof(CalendarTime))
339 CalendarTime time = (CalendarTime)Convert.ChangeType(value, typeof(CalendarTime));
340 Interop.Calendar.Record.DateTime val = ConvertCalendarTimeToStruct(time);
341 int error = Interop.Calendar.Record.SetCalendarTime(_recordHandle, propertyId, val);
342 if (CalendarError.None != (CalendarError)error)
344 Log.Error(Globals.LogTag, "Set CalendarTime Failed [" + error + "]" + String.Format("{0:X}", propertyId));
345 throw CalendarErrorFactory.GetException(error);
350 Log.Error(Globals.LogTag, "Not supported Data T/ype");
351 throw CalendarErrorFactory.GetException((int)CalendarError.NotSupported);
356 /// Adds a child record to the parent record.
358 /// <param name="propertyId">The property ID</param>
359 /// <param name="childRecord">The child record</param>
360 /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
361 public void AddChildRecord(uint propertyId, CalendarRecord childRecord)
363 int error = Interop.Calendar.Record.AddChildRecord(_recordHandle, propertyId, childRecord._recordHandle);
364 if (CalendarError.None != (CalendarError)error)
366 Log.Error(Globals.LogTag, "AddChildRecord Failed [" + error + "]" + String.Format("{0:X}", propertyId));
367 throw CalendarErrorFactory.GetException(error);
369 childRecord._disposedValue = true;
373 /// Removes a child record from the parent record.
375 /// <param name="propertyId">The property ID</param>
376 /// <param name="childRecord">The child record</param>
377 /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
378 public void RemoveChildRecord(uint propertyId, CalendarRecord childRecord)
380 int error = Interop.Calendar.Record.RemoveChildRecord(_recordHandle, propertyId, childRecord._recordHandle);
381 if (CalendarError.None != (CalendarError)error)
383 Log.Error(Globals.LogTag, "RemoveChildRecord Failed [" + error + "]" + String.Format("{0:X}", propertyId));
384 throw CalendarErrorFactory.GetException(error);
386 childRecord._disposedValue = false;
390 /// Gets a child record from the parent record
392 /// <param name="propertyId">The property ID</param>
394 /// The number of child records corresponding to property ID
396 /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
397 public int GetChildRecordCount(uint propertyId)
400 int error = Interop.Calendar.Record.GetChildRecordCount(_recordHandle, propertyId, out count);
401 if (CalendarError.None != (CalendarError)error)
403 Log.Error(Globals.LogTag, "GetChildRecordCount Failed [" + error + "]" + String.Format("{0:X}", propertyId));
404 throw CalendarErrorFactory.GetException(error);
410 /// Gets a child record from the parent record
412 /// <param name="propertyId">The property ID</param>
413 /// <param name="index">The child record index</param>
417 /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
418 public CalendarRecord GetChildRecord(uint propertyId, int index)
422 int error = Interop.Calendar.Record.GetChildRecordPointer(_recordHandle, propertyId, index, out handle);
423 if (CalendarError.None != (CalendarError)error)
425 Log.Error(Globals.LogTag, "GetChildRecord Failed [" + error + "]" + String.Format("{0:X}", propertyId));
426 throw CalendarErrorFactory.GetException(error);
428 return new CalendarRecord(handle, true);
432 /// Clones a child record list corresponding to property ID
434 /// <param name="propertyId">The property ID</param>
438 /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
439 public CalendarList CloneChildRecordList(uint propertyId)
442 int error = Interop.Calendar.Record.CloneChildRecordList(_recordHandle, propertyId, out listHandle);
443 if (CalendarError.None != (CalendarError)error)
445 Log.Error(Globals.LogTag, "CloneChildRecordList Failed with [" + error + "]" + String.Format("{0:X}", propertyId));
446 throw CalendarErrorFactory.GetException(error);
448 CalendarList list = new CalendarList(listHandle);