/* * 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; /// /// The Calendar Service API provides functions, enumerations used in the entire Content Service. /// /// /// The Calendar Service API provides functions and ienumerations used in the entire Content Service. /// The Information about calendar items i.e. book, event, todo, alarm, attendee and extended are managed in the database /// and operations that involve database requires an active connection with the calendar service. /// namespace Tizen.Pims.Calendar { /// /// CalendarDatabase provides methods to manage calendar information from/to the database. /// /// /// This class allows usre to access/create/update db operations for calendar information. /// public class CalendarDatabase { /// /// Delegete for detecting the calendar database changes. /// /// The record uri /// /// The delegate must be registered using AddDBChangedDelegate. /// It's invoked when the designated view changes. /// /// public delegate void CalendarDBChangedDelegate(string uri); private Object thisLock = new Object(); private Dictionary _callbackMap = new Dictionary(); private Dictionary _delegateMap = new Dictionary(); private Interop.Calendar.Database.DBChangedCallback _dbChangedDelegate; internal CalendarDatabase() { ///To be created in CalendarManager only } /// /// The calendar database version. /// public int Version { get { int version = -1; int error = Interop.Calendar.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. /// /// 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 public int LastChangeVersion { get { int version = -1; int error = Interop.Calendar.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. /// /// The record to be inserted /// The ID of inserted record /// http://tizen.org/privilege/calendar.write /// Thrown when method failed due to invalid operation /// Thrown when an invoked method is not supported /// 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 public int Insert(CalendarRecord record) { int id = -1; int error = Interop.Calendar.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. /// /// The view URI of a record /// The record ID /// /// The record associated with the record ID /// /// http://tizen.org/privilege/calendar.read /// Thrown when method failed due to invalid operation /// Thrown when an invoked method is not supported /// 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 public CalendarRecord Get(string viewUri, int recordId) { IntPtr handle; int error = Interop.Calendar.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. /// /// The record to be updated /// http://tizen.org/privilege/calendar.write /// Thrown when method failed due to invalid operation /// Thrown when an invoked method is not supported /// 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 public void Update(CalendarRecord record) { int error = Interop.Calendar.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. /// /// The view URI of a record /// The record ID to be deleted /// http://tizen.org/privilege/calendar.write /// Thrown when method failed due to invalid operation /// Thrown when an invoked method is not supported /// 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 public void Delete(string viewUri, int recordId) { int error = Interop.Calendar.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. /// /// The record to be replaced /// the record id /// http://tizen.org/privilege/calendar.write /// Thrown when method failed due to invalid operation /// Thrown when an invoked method is not supported /// 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 public void Replace(CalendarRecord record, int id) { int error = Interop.Calendar.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. /// /// 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 /// Thrown when method failed due to invalid operation /// Thrown when an invoked method is not supported /// 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 public CalendarList GetAll(string viewUri, int offset, int limit) { IntPtr handle; int error = Interop.Calendar.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. /// /// 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 /// Thrown when method failed due to invalid operation /// Thrown when an invoked method is not supported /// 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 public CalendarList GetRecordsWithQuery(CalendarQuery query, int offset, int limit) { IntPtr handle; int error = Interop.Calendar.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. /// /// The record list /// /// The inserted record id array /// /// http://tizen.org/privilege/calendar.write /// Thrown when method failed due to invalid operation /// Thrown when an invoked method is not supported /// 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 public int[] Insert(CalendarList list) { IntPtr ids; int count; int error = Interop.Calendar.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. /// /// The record list /// http://tizen.org/privilege/calendar.write /// Thrown when method failed due to invalid operation /// Thrown when an invoked method is not supported /// 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 public void Update(CalendarList list) { int error = Interop.Calendar.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. /// /// The view URI of the records to delete /// The record IDs to delete /// http://tizen.org/privilege/calendar.write /// Thrown when method failed due to invalid operation /// Thrown when an invoked method is not supported /// 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 public void Delete(string viewUri, int[] idArray) { int error = Interop.Calendar.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. /// /// The record list /// http://tizen.org/privilege/calendar.write /// Thrown when method failed due to invalid operation /// Thrown when an invoked method is not supported /// 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 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. /// /// The record list /// The record IDs /// http://tizen.org/privilege/calendar.write /// Thrown when method failed due to invalid operation /// Thrown when an invoked method is not supported /// 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 public void Replace(CalendarList list, int[] idArray) { int error = Interop.Calendar.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. /// /// The view URI to get records from /// The calendar book ID to filter /// The calendar database version /// /// /// The record list /// /// http://tizen.org/privilege/calendar.read /// Thrown when method failed due to invalid operation /// Thrown when an invoked method is not supported /// 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 public CalendarList GetChangesByVersion(string viewUri, int BookId, int calendarDBVersion, out int currentDBVersion) { IntPtr recordList; int error = Interop.Calendar.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. /// /// The view URI to get records from /// /// The count of records /// /// http://tizen.org/privilege/calendar.read public int GetCount(string viewUri) { int count = -1; int error = Interop.Calendar.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. /// /// The query used for filtering the results /// /// The count of records /// /// http://tizen.org/privilege/calendar.read public int GetCount(CalendarQuery query) { int count = -1; int error = Interop.Calendar.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. /// /// The view URI of the record to subscribe for change notifications /// The callback function to register /// http://tizen.org/privilege/calendar.read public void AddDBChangedDelegate(string viewUri, CalendarDBChangedDelegate callback) { Log.Debug(Globals.LogTag, "AddDBChangedDelegate"); _dbChangedDelegate = (string uri, IntPtr userData) => { _callbackMap[uri](uri); }; int error = Interop.Calendar.Database.AddChangedCallback(viewUri, _dbChangedDelegate, IntPtr.Zero); if (CalendarError.None != (CalendarError)error) { Log.Error(Globals.LogTag, "AddDBChangedDelegate Failed with error " + error); throw CalendarErrorFactory.GetException(error); } _callbackMap[viewUri] = callback; _delegateMap[viewUri] = _dbChangedDelegate; } /// /// Unregisters a callback function. /// /// The view URI of the record to subscribe for change notifications /// The callback function to register /// http://tizen.org/privilege/calendar.read public void RemoveDBChangedDelegate(string viewUri, CalendarDBChangedDelegate callback) { Log.Debug(Globals.LogTag, "RemoveDBChangedDelegate"); int error = Interop.Calendar.Database.RemoveChangedCallback(viewUri, _delegateMap[viewUri], IntPtr.Zero); if (CalendarError.None != (CalendarError)error) { Log.Error(Globals.LogTag, "RemoveDBChangedDelegate Failed with error " + error); throw CalendarErrorFactory.GetException(error); } _callbackMap.Remove(viewUri); _delegateMap.Remove(viewUri); } /// /// Link a record to another record. /// /// The base record ID /// The record ID to link to /// http://tizen.org/privilege/calendar.write /// Thrown when method failed due to invalid operation /// Thrown when an invoked method is not supported /// 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 public void LinkRecord(int baseId, int recordId) { Log.Debug(Globals.LogTag, "LinkRecord"); int error = Interop.Calendar.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. /// /// The record ID to unlink /// http://tizen.org/privilege/calendar.write /// Thrown when method failed due to invalid operation /// Thrown when an invoked method is not supported /// 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 public void UnlinkRecord(int recordId) { Log.Debug(Globals.LogTag, "UnlinkRecord"); int error = Interop.Calendar.Database.UnlinkRecord(recordId); if (CalendarError.None != (CalendarError)error) { Log.Error(Globals.LogTag, "UnlinkRecor Failed with error " + error); throw CalendarErrorFactory.GetException(error); } } } }