/* * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved * * Licensed under the Apache License, Version 2.0 (the License); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ using System; using System.Collections.Generic; using System.Runtime.InteropServices; using System.Diagnostics.CodeAnalysis; namespace Tizen.Pims.Calendar { /// /// CalendarDatabase provides methods to manage calendar information from/to the database. /// /// /// This class allows user to access/create/update/delete db operations for calendar information. /// CalendarDatabase is created by CalendarManager. /// /// 4 public class CalendarDatabase { private Object thisLock = new Object(); private Dictionary> _eventHandlerMap = new Dictionary>(); private Dictionary _callbackMap = new Dictionary(); internal CalendarDatabase() { /*To be created in CalendarManager only*/ } /// /// The calendar database version. /// /// 4 /// The current calendar database version. [SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic")] public int Version { get { int version = -1; int error = Interop.Database.GetCurrentVersion(out version); if (CalendarError.None != (CalendarError)error) { Log.Error(Globals.LogTag, "Version Failed with error " + error); } return version; } } /// /// Gets last successful changed calendar database version on the current connection. /// /// 4 /// The last successful changed calendar database version on the current connection /// http://tizen.org/privilege/calendar.read /// Thrown when method failed due to invalid operation /// Thrown when failed due to out of memory /// Thrown when application does not have proper privileges /// The last successful changed calendar database version on the current connection. [SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic")] public int LastChangeVersion { get { int version = -1; int error = Interop.Database.GetLastChangeVersion(out version); if (CalendarError.None != (CalendarError)error) { Log.Error(Globals.LogTag, "LastChangeVersion Failed with error " + error); } return version; } } /// /// Inserts a record into the calendar database. /// /// 4 /// The record to be inserted /// The ID of inserted record /// http://tizen.org/privilege/calendar.write /// http://tizen.org/feature/calendar /// Thrown when feature is not supported /// Thrown when method failed due to invalid operation /// Thrown when one of the arguments provided to a method is not valid /// Thrown when failed due to out of memory /// Thrown when application does not have proper privileges [SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic")] public int Insert(CalendarRecord record) { int id = -1; int error = Interop.Database.Insert(record._recordHandle, out id); if (CalendarError.None != (CalendarError)error) { Log.Error(Globals.LogTag, "Insert Failed with error " + error); throw CalendarErrorFactory.GetException(error); } return id; } /// /// Gets a record from the calendar database. /// /// 4 /// The view URI of a record /// The record ID /// /// The record associated with the record ID /// /// http://tizen.org/privilege/calendar.read /// http://tizen.org/feature/calendar /// Thrown when feature is not supported /// Thrown when method failed due to invalid operation /// Thrown when one of the arguments provided to a method is not valid /// Thrown when failed due to out of memory /// Thrown when application does not have proper privileges [SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic")] [SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings")] public CalendarRecord Get(string viewUri, int recordId) { IntPtr handle; int error = Interop.Database.Get(viewUri, recordId, out handle); if (CalendarError.None != (CalendarError)error) { if (CalendarError.DBNotFound == (CalendarError)error) { Log.Error(Globals.LogTag, "No data" + error); return null; } Log.Error(Globals.LogTag, "Get Failed with error " + error); throw CalendarErrorFactory.GetException(error); } return new CalendarRecord(handle); } /// /// Updates a record in the calendar database. /// /// 4 /// The record to be updated /// http://tizen.org/privilege/calendar.write /// http://tizen.org/feature/calendar /// Thrown when feature is not supported /// Thrown when method failed due to invalid operation /// Thrown when one of the arguments provided to a method is not valid /// Thrown when failed due to out of memory /// Thrown when application does not have proper privileges [SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic")] public void Update(CalendarRecord record) { int error = Interop.Database.Update(record._recordHandle); if (CalendarError.None != (CalendarError)error) { Log.Error(Globals.LogTag, "Update Failed with error " + error); throw CalendarErrorFactory.GetException(error); } } /// /// Deletes a record from the calendar database with related child records. /// /// 4 /// The view URI of a record /// The record ID to be deleted /// http://tizen.org/privilege/calendar.write /// http://tizen.org/feature/calendar /// Thrown when feature is not supported /// Thrown when method failed due to invalid operation /// Thrown when one of the arguments provided to a method is not valid /// Thrown when failed due to out of memory /// Thrown when application does not have proper privileges [SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic")] [SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings")] public void Delete(string viewUri, int recordId) { int error = Interop.Database.Delete(viewUri, recordId); if (CalendarError.None != (CalendarError)error) { Log.Error(Globals.LogTag, "Delete Failed with error " + error); throw CalendarErrorFactory.GetException(error); } } /// /// Replaces a record in the calendar database. /// /// 4 /// The record to be replaced /// the record id /// http://tizen.org/privilege/calendar.write /// http://tizen.org/feature/calendar /// Thrown when feature is not supported /// Thrown when method failed due to invalid operation /// Thrown when one of the arguments provided to a method is not valid /// Thrown when failed due to out of memory /// Thrown when application does not have proper privileges [SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic")] public void Replace(CalendarRecord record, int id) { int error = Interop.Database.Replace(record._recordHandle, id); if (CalendarError.None != (CalendarError)error) { Log.Error(Globals.LogTag, "Replace Failed with error " + error); throw CalendarErrorFactory.GetException(error); } } /// /// Retrieves all records as a list. /// /// 4 /// The view URI to get records from /// The index from which results are received /// The maximum number of results(value 0 is used for all records) /// /// The record list /// /// http://tizen.org/privilege/calendar.read /// http://tizen.org/feature/calendar /// Thrown when feature is not supported /// Thrown when method failed due to invalid operation /// Thrown when one of the arguments provided to a method is not valid /// Thrown when failed due to out of memory /// Thrown when application does not have proper privileges [SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic")] [SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings")] public CalendarList GetAll(string viewUri, int offset, int limit) { IntPtr handle; int error = Interop.Database.GetAllRecords(viewUri, offset, limit, out handle); if (CalendarError.None != (CalendarError)error) { Log.Error(Globals.LogTag, "GetAll Failed with error " + error); throw CalendarErrorFactory.GetException(error); } return new CalendarList(handle); } /// /// Retrieves records using a query. /// /// 4 /// The query used to filter results /// The index from which results are received /// The maximum number of results(value 0 is used for all records) /// /// CalendarList /// /// http://tizen.org/privilege/calendar.read /// http://tizen.org/feature/calendar /// Thrown when feature is not supported /// Thrown when method failed due to invalid operation /// Thrown when failed due to out of memory /// Thrown when application does not have proper privileges [SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic")] public CalendarList GetRecordsWithQuery(CalendarQuery query, int offset, int limit) { IntPtr handle; int error = Interop.Database.GetRecords(query._queryHandle, offset, limit, out handle); if (CalendarError.None != (CalendarError)error) { Log.Error(Globals.LogTag, "GetAllWithQuery Failed with error " + error); throw CalendarErrorFactory.GetException(error); } return new CalendarList(handle); } /// /// Inserts multiple records into the calendar database as a batch operation. /// /// 4 /// The record list /// /// The inserted record id array /// /// http://tizen.org/privilege/calendar.write /// http://tizen.org/feature/calendar /// Thrown when feature is not supported /// Thrown when method failed due to invalid operation /// Thrown when one of the arguments provided to a method is not valid /// Thrown when failed due to out of memory /// Thrown when application does not have proper privileges [SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic")] public int[] Insert(CalendarList list) { IntPtr ids; int count; int error = Interop.Database.InsertRecords(list._listHandle, out ids, out count); if (CalendarError.None != (CalendarError)error) { Log.Error(Globals.LogTag, "Insert Failed with error " + error); throw CalendarErrorFactory.GetException(error); } int[] idArr = new int[count]; Marshal.Copy(ids, idArr, 0, count); return idArr; } /// /// Updates multiple records into the calendar database as a batch operation. /// /// 4 /// The record list /// http://tizen.org/privilege/calendar.write /// http://tizen.org/feature/calendar /// Thrown when feature is not supported /// Thrown when method failed due to invalid operation /// Thrown when one of the arguments provided to a method is not valid /// Thrown when failed due to out of memory /// Thrown when application does not have proper privileges [SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic")] public void Update(CalendarList list) { int error = Interop.Database.UpdateRecords(list._listHandle); if (CalendarError.None != (CalendarError)error) { Log.Error(Globals.LogTag, "Update Failed with error " + error); throw CalendarErrorFactory.GetException(error); } } /// /// Deletes multiple records with related child records from the calendar database as a batch operation. /// /// 4 /// The view URI of the records to delete /// The record IDs to delete /// http://tizen.org/privilege/calendar.write /// http://tizen.org/feature/calendar /// Thrown when feature is not supported /// Thrown when method failed due to invalid operation /// Thrown when one of the arguments provided to a method is not valid /// Thrown when failed due to out of memory /// Thrown when application does not have proper privileges [SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic")] [SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings")] public void Delete(string viewUri, int[] idArray) { int error = Interop.Database.DeleteRecords(viewUri, idArray, idArray.Length); if (CalendarError.None != (CalendarError)error) { Log.Error(Globals.LogTag, "Delete Failed with error " + error); throw CalendarErrorFactory.GetException(error); } } /// /// Deletes multiple records with related child records from the calendar database as a batch operation. /// /// 4 /// The record list /// http://tizen.org/privilege/calendar.write /// http://tizen.org/feature/calendar /// Thrown when feature is not supported /// Thrown when method failed due to invalid operation /// Thrown when failed due to out of memory /// Thrown when application does not have proper privileges public void Delete(CalendarList list) { CalendarRecord record = null; if (list.Count <= 0) return; int[] ids = new int[list.Count]; int i; uint propertyId = 0; list.MoveFirst(); for (i = 0; i < list.Count; i++) { record = list.GetCurrentRecord(); if (0 == propertyId) { if (0 == String.Compare(CalendarViews.Book.Uri, record.Uri)) propertyId = CalendarViews.Book.Id; else if (0 == String.Compare(CalendarViews.Event.Uri, record.Uri)) propertyId = CalendarViews.Event.Id; else if (0 == String.Compare(CalendarViews.Todo.Uri, record.Uri)) propertyId = CalendarViews.Todo.Id; else if (0 == String.Compare(CalendarViews.Timezone.Uri, record.Uri)) propertyId = CalendarViews.Timezone.Id; else if (0 == String.Compare(CalendarViews.Extended.Uri, record.Uri)) propertyId = CalendarViews.Extended.Id; else { Log.Error(Globals.LogTag, "Invalid uri [" + record.Uri + "]"); continue; } } ids[i] = record.Get(propertyId); list.MoveNext(); } Delete(record.Uri, ids); } /// /// Replaces multiple records in the calendar database as a batch operation. /// /// 4 /// The record list /// The record IDs /// http://tizen.org/privilege/calendar.write /// http://tizen.org/feature/calendar /// Thrown when feature is not supported /// Thrown when method failed due to invalid operation /// Thrown when one of the arguments provided to a method is not valid /// Thrown when failed due to out of memory /// Thrown when application does not have proper privileges [SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic")] public void Replace(CalendarList list, int[] idArray) { int error = Interop.Database.ReplaceRecords(list._listHandle, idArray, idArray.Length); if (CalendarError.None != (CalendarError)error) { Log.Error(Globals.LogTag, "Replace Failed with error " + error); throw CalendarErrorFactory.GetException(error); } } /// /// Retrieves records with the given calendar database version. /// /// 4 /// The view URI to get records from /// The calendar book ID to filter /// The calendar database version /// The current calendar database version /// /// The record list /// /// http://tizen.org/privilege/calendar.read /// http://tizen.org/feature/calendar /// Thrown when feature is not supported /// Thrown when method failed due to invalid operation /// Thrown when one of the arguments provided to a method is not valid /// Thrown when failed due to out of memory /// Thrown when application does not have proper privileges [SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic")] [SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings")] public CalendarList GetChangesByVersion(string viewUri, int BookId, int calendarDBVersion, out int currentDBVersion) { IntPtr recordList; int error = Interop.Database.GetChangesByVersion(viewUri, BookId, calendarDBVersion, out recordList, out currentDBVersion); if (CalendarError.None != (CalendarError)error) { Log.Error(Globals.LogTag, "GetChangesByVersion Failed with error " + error); throw CalendarErrorFactory.GetException(error); } return new CalendarList(recordList); } /// /// Gets the record count of a specific view. /// /// 4 /// The view URI to get records from /// /// The count of records /// /// http://tizen.org/privilege/calendar.read [SuppressMessage("Microsoft.Design", "CA1822:MarkMembersAsStatic")] [SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings")] public int GetCount(string viewUri) { int count = -1; int error = Interop.Database.GetCount(viewUri, out count); if (CalendarError.None != (CalendarError)error) { Log.Error(Globals.LogTag, "GetCount Failed with error " + error); throw CalendarErrorFactory.GetException(error); } return count; } /// /// Gets the record count with a query. /// /// 4 /// The query used for filtering the results /// /// The count of records /// /// http://tizen.org/privilege/calendar.read [SuppressMessage("Microsoft.Design", "CA1822:MarkMembersAsStatic")] public int GetCount(CalendarQuery query) { int count = -1; int error = Interop.Database.GetCountWithQuery(query._queryHandle, out count); if (CalendarError.None != (CalendarError)error) { Log.Error(Globals.LogTag, "GetCount Failed with error " + error); throw CalendarErrorFactory.GetException(error); } return count; } /// /// Registers a callback function to be invoked when a record changes. /// 4 /// /// The view URI of the record to subscribe for change notifications /// The EventHandler to register /// http://tizen.org/privilege/calendar.read /// http://tizen.org/feature/calendar /// Thrown when feature is not supported /// Thrown when method failed due to invalid operation /// Thrown when one of the arguments provided to a method is not valid /// Thrown when failed due to out of memory /// Thrown when application does not have proper privileges [SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings")] public void AddDBChangedDelegate(string viewUri, EventHandler DBChanged) { Log.Debug(Globals.LogTag, "AddDBChangedDelegate"); if (!_callbackMap.ContainsKey(viewUri)) { _callbackMap[viewUri] = (string uri, IntPtr userData) => { DBChangedEventArgs args = new DBChangedEventArgs(uri); _eventHandlerMap[uri]?.Invoke(this, args); }; int error = Interop.Database.AddChangedCallback(viewUri, _callbackMap[viewUri], IntPtr.Zero); if (CalendarError.None != (CalendarError)error) { Log.Error(Globals.LogTag, "AddDBChangedDelegate Failed with error " + error); throw CalendarErrorFactory.GetException(error); } } EventHandler handler = null; if (!_eventHandlerMap.TryGetValue(viewUri, out handler)) _eventHandlerMap.Add(viewUri, null); _eventHandlerMap[viewUri] = handler + DBChanged; } /// /// Deregisters a callback function. /// /// 4 /// The view URI of the record to subscribe for change notifications /// The EventHandler to deregister /// http://tizen.org/privilege/calendar.read /// http://tizen.org/feature/calendar /// Thrown when feature is not supported /// Thrown when method failed due to invalid operation /// Thrown when failed due to out of memory /// Thrown when application does not have proper privileges [SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings")] public void RemoveDBChangedDelegate(string viewUri, EventHandler DBChanged) { Log.Debug(Globals.LogTag, "RemoveDBChangedDelegate"); EventHandler handler = null; if (!_eventHandlerMap.TryGetValue(viewUri, out handler)) _eventHandlerMap.Add(viewUri, null); else _eventHandlerMap[viewUri] = handler - DBChanged; if (_eventHandlerMap[viewUri] == null) { int error = Interop.Database.RemoveChangedCallback(viewUri, _callbackMap[viewUri], IntPtr.Zero); if (CalendarError.None != (CalendarError)error) { Log.Error(Globals.LogTag, "RemoveDBChangedDelegate Failed with error " + error); throw CalendarErrorFactory.GetException(error); } _callbackMap.Remove(viewUri); } } /// /// Link a record to another record. /// /// 4 /// The base record ID /// The record ID to link to /// http://tizen.org/privilege/calendar.write /// http://tizen.org/feature/calendar /// Thrown when feature is not supported /// Thrown when method failed due to invalid operation /// Thrown when one of the arguments provided to a method is not valid /// Thrown when failed due to out of memory /// Thrown when application does not have proper privileges [SuppressMessage("Microsoft.Design", "CA1822:MarkMembersAsStatic")] public void LinkRecord(int baseId, int recordId) { Log.Debug(Globals.LogTag, "LinkRecord"); int error = Interop.Database.LinkRecord(baseId, recordId); if (CalendarError.None != (CalendarError)error) { Log.Error(Globals.LogTag, "LinkRecor Failed with error " + error); throw CalendarErrorFactory.GetException(error); } } /// /// Unlink a record from base record. /// /// 4 /// The record ID to unlink /// http://tizen.org/privilege/calendar.write /// http://tizen.org/feature/calendar /// Thrown when feature is not supported /// Thrown when method failed due to invalid operation /// Thrown when one of the arguments provided to a method is not valid /// Thrown when failed due to out of memory /// Thrown when application does not have proper privileges [SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic")] public void UnlinkRecord(int recordId) { Log.Debug(Globals.LogTag, "UnlinkRecord"); int error = Interop.Database.UnlinkRecord(recordId); if (CalendarError.None != (CalendarError)error) { Log.Error(Globals.LogTag, "UnlinkRecor Failed with error " + error); throw CalendarErrorFactory.GetException(error); } } } }