4c82b8543985827df27d8214644ff1ef82696636
[platform/core/csapi/tizenfx.git] / src / Tizen.Account.SyncManager / Tizen.Account.SyncManager / SyncClient.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 Tizen.Applications;
20 using Tizen.Account.AccountManager;
21
22 namespace Tizen.Account.SyncManager
23 {
24     /// <summary>
25     /// The SyncClient APIs for managing the sync operations. Applications will call these APIs to schedule their sync operations.
26     /// Sync service maintains sync requests from all the applications and invokes their respective callback methods to perform account synchronization operations.
27     /// </summary>
28     public static class SyncClient
29     {
30         /// <summary>
31         /// The constructor
32         /// </summary>
33         static SyncClient()
34         {
35         }
36
37         /// <summary>
38         /// Requests Sync Manager to perform one time sync operation
39         /// </summary>
40         /// <param name="request"> Sync job information of the sync job request. </param>
41         /// <param name="syncOptions"> sync options determine an way to operate sync job and can be used as ORing. </param>
42         /// <exception cref="ArgumentNullException"> Thrown when any of the arugments are null. </exception>
43         /// <exception cref="InvalidOperationException"> Thrown when the application calling this api doesn't have a sync adapter. </exception>
44         /// <returns> A unique value which can manage sync jobs. The number of sync job id is limited as less than a hundred. </returns>
45         public static int RequestOnDemandSyncJob(SyncJobData request, SyncOption syncOptions)
46         {
47             if (request == null || request.SyncJobName == null)
48             {
49                 throw new ArgumentNullException();
50             }
51
52             SafeAccountHandle accountHandle = (request.Account != null) ? request.Account.SafeAccountHandle : new SafeAccountHandle();
53             SafeBundleHandle bundleHandle = (request.UserData != null) ? request.UserData.SafeBundleHandle : new SafeBundleHandle();
54
55             int id = 0;
56             int ret = Interop.Manager.RequestOnDemandSyncJob(accountHandle, request.SyncJobName, (int)syncOptions, bundleHandle, out id);
57             if (ret != (int)SyncManagerErrorCode.None)
58             {
59                 Log.Error(ErrorFactory.LogTag, "Failed to request on demand sync job");
60                 throw ErrorFactory.GetException(ret);
61             }
62             return id;
63         }
64
65         /// <summary>
66         /// Requests Sync Manager to perform periodic sync operations
67         /// </summary>
68         /// <param name="request"> Sync job information of the sync job request. </param>
69         /// <param name="period"> Determines time interval of periodic sync. The periodic sync operation can be triggered in that interval, but it does not guarantee exact time. The minimum value is 30 minutes. </param>
70         /// <param name="syncOptions"> sync options determine an way to operate sync job and can be used as ORing. </param>
71         /// <privilege>http://tizen.org/privilege/alarm.set</privilege>
72         /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined. </exception>
73         /// <exception cref="ArgumentNullException"> Thrown when any of the arguments are null. </exception>
74         /// <exception cref="InvalidOperationException"> Thrown when the application calling this API doesn't have a sync adapter. </exception>
75         /// <returns> A unique value which can manage sync jobs. The number of sync job id is limited as less than a hundred. </returns>
76         public static int AddPeriodicSyncJob(SyncJobData request, SyncPeriod period, SyncOption syncOptions)
77         {
78             if (request == null || request.SyncJobName == null)
79             {
80                 throw new ArgumentNullException();
81             }
82
83             SafeAccountHandle accountHandle = (request.Account != null) ? request.Account.SafeAccountHandle : new SafeAccountHandle();
84             SafeBundleHandle bundleHandle = (request.UserData != null) ? request.UserData.SafeBundleHandle : new SafeBundleHandle();
85
86             int id = 0;
87             int ret = Interop.Manager.AddPeriodicSyncJob(accountHandle, request.SyncJobName, (int) period, (int)syncOptions, bundleHandle, out id);
88             if (ret != (int)SyncManagerErrorCode.None)
89             {
90                 Log.Error(ErrorFactory.LogTag, "Failed to add periodic sync job");
91                 throw ErrorFactory.GetException(ret);
92             }
93             return id;
94         }
95
96         /// <summary>
97         /// Requests Sync Manager to perform sync operations whenever corresponding DB changed
98         /// </summary>
99         /// <param name="request"> Sync job information of the sync job request. </param>
100         /// <param name="syncOptions"> sync options determine an way to operate sync job and can be used as ORing. </param>
101         /// <privilege>http://tizen.org/privilege/calendar.read</privilege>
102         /// <privilege>http://tizen.org/privilege/contact.read</privilege>
103         /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined. </exception>
104         /// <exception cref="ArgumentNullException"> Thrown when any of the arguments are null. </exception>
105         /// <exception cref="InvalidOperationException"> Thrown when the application calling this API doesn't have a sync adapter. </exception>
106         /// <returns> A unique value which can manage sync jobs. The number of sync job id is limited as less than a hundred. </returns>
107         public static int AddDataChangeSyncJob(SyncJobData request, SyncOption syncOptions)
108         {
109             if (request == null || request.SyncJobName == null)
110             {
111                 throw new ArgumentNullException();
112             }
113
114             SafeAccountHandle accountHandle = (request.Account != null) ? request.Account.SafeAccountHandle : new SafeAccountHandle();
115             SafeBundleHandle bundleHandle = (request.UserData != null) ? request.UserData.SafeBundleHandle : new SafeBundleHandle();
116
117             int id = 0;
118             int ret = Interop.Manager.AddDataChangeSyncJob(accountHandle, request.SyncJobName, (int)syncOptions, bundleHandle, out id);
119             if (ret != (int)SyncManagerErrorCode.None)
120             {
121                 Log.Error(ErrorFactory.LogTag, "Failed to add data change sync job");
122                 throw ErrorFactory.GetException(ret);
123             }
124             return id;
125         }
126
127         /// <summary>
128         /// Get all the sync jobs registered with the sync manager
129         /// </summary>
130         /// <returns>
131         /// Returns the list of SyncJobData corresponding to sync requests.
132         /// </returns>
133         public static IEnumerable<KeyValuePair<int, SyncJobData>> GetAllSyncJobs()
134         {
135             IDictionary<int, SyncJobData> syncJobs = new Dictionary<int, SyncJobData>();
136             Interop.Manager.SyncManagerSyncJobCallback cb = (IntPtr accountHandle, string syncJobName, string syncCapability, int syncJobId, IntPtr syncJobUserData, IntPtr userData) =>
137             {
138                 AccountManager.Account account = new AccountManager.Account(new SafeAccountHandle(accountHandle, true));
139                 Bundle bundle = new Bundle(new SafeBundleHandle(syncJobUserData, true));
140
141                 SyncJobData syncJobData = new SyncJobData();
142                 syncJobData.Account = account;
143                 if (syncJobName != null)
144                     syncJobData.SyncJobName = syncJobName;
145                 else
146                     syncJobData.SyncJobName = syncCapability;
147                 syncJobData.UserData = bundle;
148
149                 syncJobs.Add(syncJobId, syncJobData);
150                 return true;
151             };
152
153             int ret = Interop.Manager.ForeachSyncJob(cb, IntPtr.Zero);
154             if (ret != (int)SyncManagerErrorCode.None)
155             {
156                 Log.Error(ErrorFactory.LogTag, "Failed to get registered sync job");
157                 throw ErrorFactory.GetException(ret);
158             }
159             return syncJobs;
160         }
161
162         /// <summary>
163         /// Requests Sync Manager to remove corresponding sync job job based on id
164         /// </summary>
165         /// <param name="id"> A unique value of each sync job, it can be used to search specific sync job and remove it. </param>
166         /// <exception cref="ArgumentException"> Thrown if the input arugments is invalid. </exception>
167         public static void RemoveSyncJob(int id)
168         {
169             int ret = Interop.Manager.RemoveSyncJob(id);
170             if (ret != (int)SyncManagerErrorCode.None)
171             {
172                 Log.Error(ErrorFactory.LogTag, "Failed to remove sync job");
173                 throw ErrorFactory.GetException(ret);
174             }
175         }
176     }
177 }
178