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;
22 /// The Calendar Service API provides functions, enumerations used in the entire Content Service.
25 /// The Calendar Service API provides functions and ienumerations used in the entire Content Service.
26 /// The Information about calendar items i.e. book, event, todo, alarm, attendee and extended are managed in the database
27 /// and operations that involve database requires an active connection with the calendar service.
30 namespace Tizen.Pims.Calendar
33 /// CalendarDatabase provides methods to manage calendar information from/to the database.
36 /// This class allows usre to access/create/update db operations for calendar information.
38 public class CalendarDatabase
41 /// Delegete for detecting the calendar database changes.
43 /// <param name="uri">The record uri</param>
45 /// The delegate must be registered using AddDBChangedDelegate.
46 /// It's invoked when the designated view changes.
48 /// <see cref="AddDBChangedDelegate"/>
49 public delegate void CalendarDBChangedDelegate(string uri);
51 private Object thisLock = new Object();
52 private Dictionary<string, CalendarDBChangedDelegate> _callbackMap = new Dictionary<string, CalendarDBChangedDelegate>();
53 private Dictionary<string, Interop.Calendar.Database.DBChangedCallback> _delegateMap = new Dictionary<string, Interop.Calendar.Database.DBChangedCallback>();
54 private Interop.Calendar.Database.DBChangedCallback _dbChangedDelegate;
56 internal CalendarDatabase()
58 ///To be created in CalendarManager only
62 /// The calendar database version.
69 int error = Interop.Calendar.Database.GetCurrentVersion(out version);
70 if (CalendarError.None != (CalendarError)error)
72 Log.Error(Globals.LogTag, "Version Failed with error " + error);
79 /// Gets last successful changed calendar database version on the current connection.
81 /// <returns>The last successful changed calendar database version on the current connection</returns>
82 /// <privilege>http://tizen.org/privilege/calendar.read</privilege>
83 /// <exception cref="InvalidOperationException">Thrown when method failed due to invalid operation</exception>
84 /// <exception cref="OutOfMemoryException">Thrown when failed due to out of memory</exception>
85 /// <exception cref="UnauthorizedAccessException">Thrown when application does not have proper privileges</exception>
86 public int LastChangeVersion
91 int error = Interop.Calendar.Database.GetLastChangeVersion(out version);
92 if (CalendarError.None != (CalendarError)error)
94 Log.Error(Globals.LogTag, "LastChangeVersion Failed with error " + error);
101 /// Inserts a record into the calendar database.
103 /// <param name="record">The record to be inserted</param>
104 /// <returns>The ID of inserted record</returns>
105 /// <privilege>http://tizen.org/privilege/calendar.write</privilege>
106 /// <exception cref="InvalidOperationException">Thrown when method failed due to invalid operation</exception>
107 /// <exception cref="NotSupportedException">Thrown when an invoked method is not supported</exception>
108 /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
109 /// <exception cref="OutOfMemoryException">Thrown when failed due to out of memory</exception>
110 /// <exception cref="UnauthorizedAccessException">Thrown when application does not have proper privileges</exception>
111 public int Insert(CalendarRecord record)
114 int error = Interop.Calendar.Database.Insert(record._recordHandle, out id);
115 if (CalendarError.None != (CalendarError)error)
117 Log.Error(Globals.LogTag, "Insert Failed with error " + error);
118 throw CalendarErrorFactory.GetException(error);
124 /// Gets a record from the calendar database.
126 /// <param name="viewUri">The view URI of a record</param>
127 /// <param name="recordId">The record ID</param>
129 /// The record associated with the record ID
131 /// <privilege>http://tizen.org/privilege/calendar.read</privilege>
132 /// <exception cref="InvalidOperationException">Thrown when method failed due to invalid operation</exception>
133 /// <exception cref="NotSupportedException">Thrown when an invoked method is not supported</exception>
134 /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
135 /// <exception cref="OutOfMemoryException">Thrown when failed due to out of memory</exception>
136 /// <exception cref="UnauthorizedAccessException">Thrown when application does not have proper privileges</exception>
137 public CalendarRecord Get(string viewUri, int recordId)
140 int error = Interop.Calendar.Database.Get(viewUri, recordId, out handle);
141 if (CalendarError.None != (CalendarError)error)
143 if (CalendarError.DBNotFound == (CalendarError)error)
145 Log.Error(Globals.LogTag, "No data" + error);
148 Log.Error(Globals.LogTag, "Get Failed with error " + error);
149 throw CalendarErrorFactory.GetException(error);
151 return new CalendarRecord(handle);
155 /// Updates a record in the calendar database.
157 /// <param name="record">The record to be updated</param>
158 /// <privilege>http://tizen.org/privilege/calendar.write</privilege>
159 /// <exception cref="InvalidOperationException">Thrown when method failed due to invalid operation</exception>
160 /// <exception cref="NotSupportedException">Thrown when an invoked method is not supported</exception>
161 /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
162 /// <exception cref="OutOfMemoryException">Thrown when failed due to out of memory</exception>
163 /// <exception cref="UnauthorizedAccessException">Thrown when application does not have proper privileges</exception>
164 public void Update(CalendarRecord record)
166 int error = Interop.Calendar.Database.Update(record._recordHandle);
167 if (CalendarError.None != (CalendarError)error)
169 Log.Error(Globals.LogTag, "Update Failed with error " + error);
170 throw CalendarErrorFactory.GetException(error);
175 /// Deletes a record from the calendar database with related child records.
177 /// <param name="viewUri">The view URI of a record</param>
178 /// <param name="recordId">The record ID to be deleted</param>
179 /// <privilege>http://tizen.org/privilege/calendar.write</privilege>
180 /// <exception cref="InvalidOperationException">Thrown when method failed due to invalid operation</exception>
181 /// <exception cref="NotSupportedException">Thrown when an invoked method is not supported</exception>
182 /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
183 /// <exception cref="OutOfMemoryException">Thrown when failed due to out of memory</exception>
184 /// <exception cref="UnauthorizedAccessException">Thrown when application does not have proper privileges</exception>
185 public void Delete(string viewUri, int recordId)
187 int error = Interop.Calendar.Database.Delete(viewUri, recordId);
188 if (CalendarError.None != (CalendarError)error)
190 Log.Error(Globals.LogTag, "Delete Failed with error " + error);
191 throw CalendarErrorFactory.GetException(error);
196 /// Replaces a record in the calendar database.
198 /// <param name="record">The record to be replaced</param>
199 /// <param name="id">the record id</param>
200 /// <privilege>http://tizen.org/privilege/calendar.write</privilege>
201 /// <exception cref="InvalidOperationException">Thrown when method failed due to invalid operation</exception>
202 /// <exception cref="NotSupportedException">Thrown when an invoked method is not supported</exception>
203 /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
204 /// <exception cref="OutOfMemoryException">Thrown when failed due to out of memory</exception>
205 /// <exception cref="UnauthorizedAccessException">Thrown when application does not have proper privileges</exception>
206 public void Replace(CalendarRecord record, int id)
208 int error = Interop.Calendar.Database.Replace(record._recordHandle, id);
209 if (CalendarError.None != (CalendarError)error)
211 Log.Error(Globals.LogTag, "Replace Failed with error " + error);
212 throw CalendarErrorFactory.GetException(error);
217 /// Retrieves all records as a list.
219 /// <param name="viewUri">The view URI to get records from</param>
220 /// <param name="offset">The index from which results are received</param>
221 /// <param name="limit">The maximum number of results(value 0 is used for all records)</param>
225 /// <privilege>http://tizen.org/privilege/calendar.read</privilege>
226 /// <exception cref="InvalidOperationException">Thrown when method failed due to invalid operation</exception>
227 /// <exception cref="NotSupportedException">Thrown when an invoked method is not supported</exception>
228 /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
229 /// <exception cref="OutOfMemoryException">Thrown when failed due to out of memory</exception>
230 /// <exception cref="UnauthorizedAccessException">Thrown when application does not have proper privileges</exception>
231 public CalendarList GetAll(string viewUri, int offset, int limit)
234 int error = Interop.Calendar.Database.GetAllRecords(viewUri, offset, limit, out handle);
235 if (CalendarError.None != (CalendarError)error)
237 Log.Error(Globals.LogTag, "GetAll Failed with error " + error);
238 throw CalendarErrorFactory.GetException(error);
240 return new CalendarList(handle);
244 /// Retrieves records using a query.
246 /// <param name="query">The query used to filter results</param>
247 /// <param name="offset">The index from which results are received</param>
248 /// <param name="limit">The maximum number of results(value 0 is used for all records)</param>
252 /// <privilege>http://tizen.org/privilege/calendar.read</privilege>
253 /// <exception cref="InvalidOperationException">Thrown when method failed due to invalid operation</exception>
254 /// <exception cref="NotSupportedException">Thrown when an invoked method is not supported</exception>
255 /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
256 /// <exception cref="OutOfMemoryException">Thrown when failed due to out of memory</exception>
257 /// <exception cref="UnauthorizedAccessException">Thrown when application does not have proper privileges</exception>
258 public CalendarList GetRecordsWithQuery(CalendarQuery query, int offset, int limit)
261 int error = Interop.Calendar.Database.GetRecords(query._queryHandle, offset, limit, out handle);
262 if (CalendarError.None != (CalendarError)error)
264 Log.Error(Globals.LogTag, "GetAllWithQuery Failed with error " + error);
265 throw CalendarErrorFactory.GetException(error);
267 return new CalendarList(handle);
271 /// Inserts multiple records into the calendar database as a batch operation.
273 /// <param name="list">The record list</param>
275 /// The inserted record id array
277 /// <privilege>http://tizen.org/privilege/calendar.write</privilege>
278 /// <exception cref="InvalidOperationException">Thrown when method failed due to invalid operation</exception>
279 /// <exception cref="NotSupportedException">Thrown when an invoked method is not supported</exception>
280 /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
281 /// <exception cref="OutOfMemoryException">Thrown when failed due to out of memory</exception>
282 /// <exception cref="UnauthorizedAccessException">Thrown when application does not have proper privileges</exception>
283 public int[] Insert(CalendarList list)
287 int error = Interop.Calendar.Database.InsertRecords(list._listHandle, out ids, out count);
288 if (CalendarError.None != (CalendarError)error)
290 Log.Error(Globals.LogTag, "Insert Failed with error " + error);
291 throw CalendarErrorFactory.GetException(error);
293 int[] idArr = new int[count];
294 Marshal.Copy(ids, idArr, 0, count);
300 /// Updates multiple records into the calendar database as a batch operation.
302 /// <param name="list">The record list</param>
303 /// <privilege>http://tizen.org/privilege/calendar.write</privilege>
304 /// <exception cref="InvalidOperationException">Thrown when method failed due to invalid operation</exception>
305 /// <exception cref="NotSupportedException">Thrown when an invoked method is not supported</exception>
306 /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
307 /// <exception cref="OutOfMemoryException">Thrown when failed due to out of memory</exception>
308 /// <exception cref="UnauthorizedAccessException">Thrown when application does not have proper privileges</exception>
309 public void Update(CalendarList list)
311 int error = Interop.Calendar.Database.UpdateRecords(list._listHandle);
312 if (CalendarError.None != (CalendarError)error)
314 Log.Error(Globals.LogTag, "Update Failed with error " + error);
315 throw CalendarErrorFactory.GetException(error);
320 /// Deletes multiple records with related child records from the calendar database as a batch operation.
322 /// <param name="viewUri">The view URI of the records to delete</param>
323 /// <param name="idArray">The record IDs to delete</param>
324 /// <privilege>http://tizen.org/privilege/calendar.write</privilege>
325 /// <exception cref="InvalidOperationException">Thrown when method failed due to invalid operation</exception>
326 /// <exception cref="NotSupportedException">Thrown when an invoked method is not supported</exception>
327 /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
328 /// <exception cref="OutOfMemoryException">Thrown when failed due to out of memory</exception>
329 /// <exception cref="UnauthorizedAccessException">Thrown when application does not have proper privileges</exception>
330 public void Delete(string viewUri, int[] idArray)
332 int error = Interop.Calendar.Database.DeleteRecords(viewUri, idArray, idArray.Length);
333 if (CalendarError.None != (CalendarError)error)
335 Log.Error(Globals.LogTag, "Delete Failed with error " + error);
336 throw CalendarErrorFactory.GetException(error);
341 /// Deletes multiple records with related child records from the calendar database as a batch operation.
343 /// <param name="list">The record list</param>
344 /// <privilege>http://tizen.org/privilege/calendar.write</privilege>
345 /// <exception cref="InvalidOperationException">Thrown when method failed due to invalid operation</exception>
346 /// <exception cref="NotSupportedException">Thrown when an invoked method is not supported</exception>
347 /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
348 /// <exception cref="OutOfMemoryException">Thrown when failed due to out of memory</exception>
349 /// <exception cref="UnauthorizedAccessException">Thrown when application does not have proper privileges</exception>
350 public void Delete(CalendarList list)
352 CalendarRecord record = null;
356 int[] ids = new int[list.Count];
360 for (i = 0; i < list.Count; i++)
362 record = list.GetCurrentRecord();
365 if (0 == String.Compare(CalendarViews.Book.Uri, record.Uri))
366 propertyId = CalendarViews.Book.Id;
367 else if (0 == String.Compare(CalendarViews.Event.Uri, record.Uri))
368 propertyId = CalendarViews.Event.Id;
369 else if (0 == String.Compare(CalendarViews.Todo.Uri, record.Uri))
370 propertyId = CalendarViews.Todo.Id;
371 else if (0 == String.Compare(CalendarViews.Timezone.Uri, record.Uri))
372 propertyId = CalendarViews.Timezone.Id;
373 else if (0 == String.Compare(CalendarViews.Extended.Uri, record.Uri))
374 propertyId = CalendarViews.Extended.Id;
377 Log.Error(Globals.LogTag, "Invalid uri [" + record.Uri + "]");
381 ids[i] = record.Get<int>(propertyId);
384 Delete(record.Uri, ids);
388 /// Replaces multiple records in the calendar database as a batch operation.
390 /// <param name="list">The record list</param>
391 /// <param name="idArray">The record IDs</param>
392 /// <privilege>http://tizen.org/privilege/calendar.write</privilege>
393 /// <exception cref="InvalidOperationException">Thrown when method failed due to invalid operation</exception>
394 /// <exception cref="NotSupportedException">Thrown when an invoked method is not supported</exception>
395 /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
396 /// <exception cref="OutOfMemoryException">Thrown when failed due to out of memory</exception>
397 /// <exception cref="UnauthorizedAccessException">Thrown when application does not have proper privileges</exception>
398 public void Replace(CalendarList list, int[] idArray)
400 int error = Interop.Calendar.Database.ReplaceRecords(list._listHandle, idArray, idArray.Length);
401 if (CalendarError.None != (CalendarError)error)
403 Log.Error(Globals.LogTag, "Replace Failed with error " + error);
404 throw CalendarErrorFactory.GetException(error);
409 /// Retrieves records with the given calendar database version.
411 /// <param name="viewUri">The view URI to get records from</param>
412 /// <param name="BookId">The calendar book ID to filter</param>
413 /// <param name="calendarDBVersion">The calendar database version</param>
414 /// <param name="currentDBVersion"The current calendar database versio></param>
418 /// <privilege>http://tizen.org/privilege/calendar.read</privilege>
419 /// <exception cref="InvalidOperationException">Thrown when method failed due to invalid operation</exception>
420 /// <exception cref="NotSupportedException">Thrown when an invoked method is not supported</exception>
421 /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
422 /// <exception cref="OutOfMemoryException">Thrown when failed due to out of memory</exception>
423 /// <exception cref="UnauthorizedAccessException">Thrown when application does not have proper privileges</exception>
424 public CalendarList GetChangesByVersion(string viewUri, int BookId, int calendarDBVersion, out int currentDBVersion)
427 int error = Interop.Calendar.Database.GetChangesByVersion(viewUri, BookId, calendarDBVersion, out recordList, out currentDBVersion);
428 if (CalendarError.None != (CalendarError)error)
430 Log.Error(Globals.LogTag, "GetChangesByVersion Failed with error " + error);
431 throw CalendarErrorFactory.GetException(error);
433 return new CalendarList(recordList);
437 /// Gets the record count of a specific view.
439 /// <param name="viewUri">The view URI to get records from</param>
441 /// The count of records
443 /// <privilege>http://tizen.org/privilege/calendar.read</privilege>
444 public int GetCount(string viewUri)
447 int error = Interop.Calendar.Database.GetCount(viewUri, out count);
448 if (CalendarError.None != (CalendarError)error)
450 Log.Error(Globals.LogTag, "GetCount Failed with error " + error);
451 throw CalendarErrorFactory.GetException(error);
457 /// Gets the record count with a query.
459 /// <param name="query">The query used for filtering the results</param>
461 /// The count of records
463 /// <privilege>http://tizen.org/privilege/calendar.read</privilege>
464 public int GetCount(CalendarQuery query)
467 int error = Interop.Calendar.Database.GetCountWithQuery(query._queryHandle, out count);
468 if (CalendarError.None != (CalendarError)error)
470 Log.Error(Globals.LogTag, "GetCount Failed with error " + error);
471 throw CalendarErrorFactory.GetException(error);
477 /// Registers a callback function to be invoked when a record changes.
479 /// <param name="viewUri">The view URI of the record to subscribe for change notifications</param>
480 /// <param name="callback">The callback function to register</param>
481 /// <privilege>http://tizen.org/privilege/calendar.read</privilege>
482 public void AddDBChangedDelegate(string viewUri, CalendarDBChangedDelegate callback)
484 Log.Debug(Globals.LogTag, "AddDBChangedDelegate");
486 _dbChangedDelegate = (string uri, IntPtr userData) =>
488 _callbackMap[uri](uri);
490 int error = Interop.Calendar.Database.AddChangedCallback(viewUri, _dbChangedDelegate, IntPtr.Zero);
491 if (CalendarError.None != (CalendarError)error)
493 Log.Error(Globals.LogTag, "AddDBChangedDelegate Failed with error " + error);
494 throw CalendarErrorFactory.GetException(error);
496 _callbackMap[viewUri] = callback;
497 _delegateMap[viewUri] = _dbChangedDelegate;
501 /// Unregisters a callback function.
503 /// <param name="viewUri">The view URI of the record to subscribe for change notifications</param>
504 /// <param name="callback">The callback function to register</param>
505 /// <privilege>http://tizen.org/privilege/calendar.read</privilege>
506 public void RemoveDBChangedDelegate(string viewUri, CalendarDBChangedDelegate callback)
508 Log.Debug(Globals.LogTag, "RemoveDBChangedDelegate");
510 int error = Interop.Calendar.Database.RemoveChangedCallback(viewUri, _delegateMap[viewUri], IntPtr.Zero);
511 if (CalendarError.None != (CalendarError)error)
513 Log.Error(Globals.LogTag, "RemoveDBChangedDelegate Failed with error " + error);
514 throw CalendarErrorFactory.GetException(error);
516 _callbackMap.Remove(viewUri);
517 _delegateMap.Remove(viewUri);
521 /// Link a record to another record.
523 /// <param name="baseId">The base record ID</param>
524 /// <param name="recordId">The record ID to link to</param>
525 /// <privilege>http://tizen.org/privilege/calendar.write</privilege>
526 /// <exception cref="InvalidOperationException">Thrown when method failed due to invalid operation</exception>
527 /// <exception cref="NotSupportedException">Thrown when an invoked method is not supported</exception>
528 /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
529 /// <exception cref="OutOfMemoryException">Thrown when failed due to out of memory</exception>
530 /// <exception cref="UnauthorizedAccessException">Thrown when application does not have proper privileges</exception>
531 public void LinkRecord(int baseId, int recordId)
533 Log.Debug(Globals.LogTag, "LinkRecord");
534 int error = Interop.Calendar.Database.LinkRecord(baseId, recordId);
535 if (CalendarError.None != (CalendarError)error)
537 Log.Error(Globals.LogTag, "LinkRecor Failed with error " + error);
538 throw CalendarErrorFactory.GetException(error);
543 /// Unlink a record from base record.
545 /// <param name="recordId">The record ID to unlink</param>
546 /// <privilege>http://tizen.org/privilege/calendar.write</privilege>
547 /// <exception cref="InvalidOperationException">Thrown when method failed due to invalid operation</exception>
548 /// <exception cref="NotSupportedException">Thrown when an invoked method is not supported</exception>
549 /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
550 /// <exception cref="OutOfMemoryException">Thrown when failed due to out of memory</exception>
551 /// <exception cref="UnauthorizedAccessException">Thrown when application does not have proper privileges</exception>
552 public void UnlinkRecord(int recordId)
554 Log.Debug(Globals.LogTag, "UnlinkRecord");
555 int error = Interop.Calendar.Database.UnlinkRecord(recordId);
556 if (CalendarError.None != (CalendarError)error)
558 Log.Error(Globals.LogTag, "UnlinkRecor Failed with error " + error);
559 throw CalendarErrorFactory.GetException(error);