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