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