8a59593dad12c043c283aa5a98b060a66bc7d692
[platform/core/csapi/tizenfx.git] / src / Tizen.Account.SyncManager / Tizen.Account.SyncManager / SyncAdapter.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 Tizen.Applications;
19 using Tizen.Account.AccountManager;
20
21 namespace Tizen.Account.SyncManager
22 {
23     /// <summary>
24     /// The class contains the delegates to be called upon scheduling a sync operation
25     /// </summary>
26     public class SyncAdapter
27     {
28         Interop.Adapter.SyncAdapterStartSyncCallback _startSyncCallback;
29         Interop.Adapter.SyncAdapterCancelSyncCallback _cancelSyncCallback;
30
31         /// <summary>
32         /// Callback function for Sync Adapter's start sync request
33         /// </summary>
34         /// <param name="syncParameters"> The sync job parameters corresponding to the sync request. </param>
35         /// <returns> true if sync operation is success, @c false otherwise. </returns>
36         public delegate bool StartSyncCallback(SyncJobData syncParameters);
37
38         /// <summary>
39         /// Callback function for Sync Adapter's cancel sync request.
40         /// </summary>
41         /// <param name="syncParameters"> The sync job parameters corresponding to the sync request. </param>
42         public delegate void CancelSyncCallback(SyncJobData syncParameters);
43
44         /// <summary>
45         /// Sets client (Sync Adapter) callback functions
46         /// </summary>
47         /// <param name="startSyncCb"> A callback function to be called by Sync Manager for performing sync operation. </param>
48         /// <param name="cancelSyncCb"> A callback function to be called by Sync Manager for cancelling sync operation. </param>
49         /// <exception cref="ArgumentNullException"> Thrown when any of the arguments are null. </exception>
50         /// <exception cref="InvalidOperationException"> Thrown when the application calling this API cannot be a sync adapter. </exception>
51         public void SetSyncEventCallbacks(StartSyncCallback startSyncCb, CancelSyncCallback cancelSyncCb)
52         {
53             if (startSyncCb == null || cancelSyncCb == null)
54             {
55                 throw new ArgumentNullException();
56             }
57
58             _startSyncCallback = (IntPtr accountHandle, string syncJobName, string syncCapability, IntPtr syncJobUserData) =>
59             {
60                 Log.Debug(ErrorFactory.LogTag, "Start sync event received");
61
62                 AccountManager.Account account = new AccountManager.Account(new SafeAccountHandle(accountHandle, true));
63                 Bundle bundle = new Bundle(new SafeBundleHandle(syncJobUserData, true));
64
65                 SyncJobData syncJobData = new SyncJobData()
66                 {
67                     Account = account,
68                     SyncJobName = syncJobName,
69                     UserData = bundle
70                 };
71
72                 return startSyncCb(syncJobData);
73             };
74
75             _cancelSyncCallback = (IntPtr accountHandle, string syncJobName, string syncCapability, IntPtr syncJobUserData) =>
76             {
77                 Log.Debug(ErrorFactory.LogTag, "cancel sync event received");
78
79                 AccountManager.Account account = new AccountManager.Account(new SafeAccountHandle(accountHandle, true));
80                 Bundle bundle = new Bundle(new SafeBundleHandle(syncJobUserData, true));
81
82                 SyncJobData syncJobData = new SyncJobData()
83                 {
84                     Account = account,
85                     SyncJobName = syncJobName,
86                     UserData = bundle
87                 };
88
89                 cancelSyncCb(syncJobData);
90             };
91
92             int ret = Interop.Adapter.SetCallbacks(_startSyncCallback, _cancelSyncCallback);
93             if (ret != (int)SyncManagerErrorCode.None)
94             {
95                 Log.Error(ErrorFactory.LogTag, "Failed to set callbacks");
96                 throw ErrorFactory.GetException(ret);
97             }
98         }
99
100         /// <summary>
101         /// Unsets client (Sync Adapter) callback functions
102         /// </summary>
103         /// <exception cref="System.Exception"> Thrown when sync manager internal error occurs. </exception>
104         public void UnsetSyncEventCallbacks()
105         {
106             int ret = Interop.Adapter.UnsetCallbacks();
107             if (ret != (int)SyncManagerErrorCode.None)
108             {
109                 Log.Error(ErrorFactory.LogTag, "Failed to unset callbacks");
110                 throw ErrorFactory.GetException(ret);
111             }
112         }
113     }
114 }
115