Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.Account.AccountManager / Tizen.Account.AccountManager / AccountProvider.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.Internals.Errors;
20
21 namespace Tizen.Account.AccountManager
22 {
23     /// <summary>
24     ///  Account Id.
25     /// </summary>
26     /// <since_tizen> 3 </since_tizen>
27     public class AccountProvider : IDisposable
28     {
29         internal IntPtr _handle;
30         internal AccountProvider(IntPtr handle)
31         {
32             Handle = handle;
33         }
34
35         ~AccountProvider()
36         {
37             Dispose(false);
38         }
39
40         internal IntPtr Handle
41         {
42             get
43             {
44                 return _handle;
45             }
46
47             set
48             {
49                 _handle = value;
50             }
51         }
52         /// <summary>
53         ///  Account Id.
54         /// </summary>
55         /// <since_tizen> 3 </since_tizen>
56         public string AppId
57         {
58             get
59             {
60                 string id = "";
61                 AccountError res = (AccountError)Interop.AccountProvider.GetAppId(Handle, out id);
62                 if (res != AccountError.None)
63                 {
64                     Log.Warn(AccountErrorFactory.LogTag, "Failed to get AppId for the AccountProvider");
65                 }
66
67                 return id;
68             }
69         }
70
71         /// <summary>
72         ///  Serviceprovider Id of the account provider.
73         /// </summary>
74         /// <since_tizen> 3 </since_tizen>
75         public string ServiceProviderId
76         {
77             get
78             {
79                 string id = "";
80                 AccountError res = (AccountError)Interop.AccountProvider.GetServiceProviderId(Handle, out id);
81                 if (res != AccountError.None)
82                 {
83                     Log.Warn(AccountErrorFactory.LogTag, "Failed to get ServiceProviderId for the AccountProvider");
84                 }
85
86                 return id;
87             }
88         }
89
90         /// <summary>
91         ///  Icon path of an account provider.
92         /// </summary>
93         /// <since_tizen> 3 </since_tizen>
94         public string IconPath
95         {
96             get
97             {
98                 string path = "";
99                 AccountError res = (AccountError)Interop.AccountProvider.GetAccountProviderIconPath(Handle, out path);
100                 if (res != AccountError.None)
101                 {
102                     Log.Warn(AccountErrorFactory.LogTag, "Failed to get IconPath for the AccountProvider");
103                 }
104
105                 return path;
106             }
107         }
108
109         /// <summary>
110         ///  Small icon path of an account provider.
111         /// </summary>
112         /// <since_tizen> 3 </since_tizen>
113         public string SmallIconPath
114         {
115             get
116             {
117                 string path = "";
118                 AccountError res = (AccountError)Interop.AccountProvider.GetAccountProviderSmallIconPath(Handle, out path);
119                 if (res != AccountError.None)
120                 {
121                     Log.Warn(AccountErrorFactory.LogTag, "Failed to get SmallIconPath for the AccountProvider");
122                 }
123
124                 return path;
125             }
126         }
127
128         /// <summary>
129         ///  Flag for account provider If supports multiple accounts.
130         /// </summary>
131         /// <since_tizen> 3 </since_tizen>
132         public bool MultipleAccountSupport
133         {
134             get
135             {
136                 int multiple = 0;
137                 AccountError res = (AccountError)Interop.AccountProvider.GetMultipleAccountSupport(Handle, out multiple);
138                 if (res != AccountError.None)
139                 {
140                     Log.Warn(AccountErrorFactory.LogTag, "Failed to get SmallIconPath for the AccountProvider");
141                 }
142
143                 return (multiple == 0) ? false : true;
144             }
145         }
146
147         /// <summary>
148         /// Retrieves all the capability information of the account provider.
149         /// </summary>
150         /// <since_tizen> 3 </since_tizen>
151         /// <privilege>http://tizen.org/privilege/account.read</privilege>
152         /// <returns>
153         /// list of capability information.
154         /// </returns>
155         /// <exception cref="InvalidOperationException">In case of any DB error</exception>
156         /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
157         public IEnumerable<string> GetAllCapabilities()
158         {
159             List<string> capabilities = new List<string>();
160             AccountError res;
161             Interop.AccountProvider.AccountProviderFeatureCallback callback = (string appId, string key, IntPtr data) =>
162             {
163                 capabilities.Add(key);
164                 return true;
165             };
166
167             res = (AccountError)Interop.AccountProvider.GetAccountProviderFeatures(Handle, callback, IntPtr.Zero);
168             if (res != AccountError.None)
169             {
170                 throw AccountErrorFactory.CreateException(res, "Failed to GetAllCapabilities for AccountProvider");
171             }
172
173             return capabilities;
174         }
175
176         /// <summary>
177         /// Gets the specific label information detail of an account provider.
178         /// </summary>
179         /// <since_tizen> 3 </since_tizen>
180         /// <param name="locale">
181         /// The locale is specified as an ISO 3166 alpha-2 two letter country-code followed by ISO 639-1 for the two-letter language code.
182         /// For example, "ko_KR" or "ko-kr" for Korean, "en_US" or "en-us" for American English.
183         /// </param>
184         /// <returns>The label text given for the locale</returns>
185         /// <privilege>http://tizen.org/privilege/account.read</privilege>
186         /// <exception cref="InvalidOperationException">In case of any DB error or record not found for given locale</exception>
187         /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
188         public string GetLabel(string locale)
189         {
190             string label;
191             AccountError res = (AccountError)Interop.AccountProvider.GetlabelbyLocale(Handle, locale, out label);
192             if (res != AccountError.None)
193             {
194                 throw AccountErrorFactory.CreateException(res, "Failed to GetLabel for AccountProvider");
195             }
196
197             return label;
198         }
199
200         /// <summary>
201         /// Gets the specific label information detail of an account provider.
202         /// </summary>
203         /// <since_tizen> 3 </since_tizen>
204         /// <param name="appId">
205         /// The application ID to search
206         /// </param>
207         /// <returns> All the labels information for the given application Id.</returns>
208         /// <privilege>http://tizen.org/privilege/account.read</privilege>
209         /// <exception cref="InvalidOperationException">In case of any DB error or record not found for given appid</exception>
210         /// <exception cref="ArgumentException"> In case of invalid parameter</exception>
211         /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
212         public static Dictionary<string, string> GetLabelsByAppId(string appId)
213         {
214
215             Dictionary<string, string> labels = new Dictionary<string, string>();
216             Interop.AccountProvider.LabelCallback callback = (string applicationId, string label, string locale, IntPtr userData) =>
217             {
218                 labels.Add(locale, label);
219                 return true;
220             };
221
222             AccountError err = (AccountError)Interop.AccountProvider.GetLablesByAppId(callback, appId, IntPtr.Zero);
223             if (err != AccountError.None)
224             {
225                 throw AccountErrorFactory.CreateException(err, "Failed to GetLablesByAppId");
226             }
227
228             return labels;
229         }
230
231         /// <summary>
232         /// Gets the label information detail of an account provider.
233         /// </summary>
234         /// <since_tizen> 3 </since_tizen>
235         /// <returns> All the labels information for the given account provider.</returns>
236         /// <privilege>http://tizen.org/privilege/account.read</privilege>
237         /// <exception cref="InvalidOperationException">In case of any DB error</exception>
238         /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
239         public Dictionary<string, string> GetLabels()
240         {
241
242             Dictionary<string, string> labels = new Dictionary<string, string>();
243             Interop.AccountProvider.LabelCallback callback = (string applicationId, string label, string locale, IntPtr userData) =>
244             {
245                 labels.Add(locale, label);
246                 return true;
247             };
248
249             AccountError err = (AccountError)Interop.AccountProvider.GetAccountProviderLabels(Handle, callback, IntPtr.Zero);
250             if (err != AccountError.None)
251             {
252                 throw AccountErrorFactory.CreateException(err, "Failed to GetAccountProviderLabels");
253             }
254
255             return labels;
256         }
257
258         /// <summary>
259         /// Checks whether the given appId exists in the account provider DB.
260         /// </summary>
261         /// <since_tizen> 3 </since_tizen>
262         /// <param name="appId">The application ID to check.</param>
263         /// <returns>returns true If App is supported </returns>
264         /// <privilege>http://tizen.org/privilege/account.read</privilege>
265         /// <exception cref="InvalidOperationException">In case of any DB error or record not found for given appid</exception>
266         /// <exception cref="ArgumentException"> In case of invalid parameter</exception>
267         /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
268         public bool IsAppSupported(string appId)
269         {
270             bool isSupported = false;
271             AccountError res = (AccountError)Interop.AccountProvider.GetAppIdExists(appId);
272
273             if (res != AccountError.None)
274             {
275                 throw AccountErrorFactory.CreateException(res, "Failed to GetLabel for AccountProvider");
276             }
277             else
278             {
279                 isSupported = true;
280             }
281
282             return isSupported;
283         }
284
285         /// <summary>
286         /// Checks whether the given application ID supports the capability.
287         /// </summary>
288         /// <since_tizen> 3 </since_tizen>
289         /// <param name="appId">The application Id</param>
290         /// <param name="capability">The capability information</param>
291         /// <returns>
292         /// TRUE if the application supports the given capability,
293         /// otherwise FALSE if the application does not support the given capability
294         /// </returns>
295         /// <privilege>http://tizen.org/privilege/account.read</privilege>
296         /// <exception cref="InvalidOperationException">In case of any DB error</exception>
297         /// <exception cref="ArgumentException"> In case of invalid parameter</exception>
298         /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
299         public static bool IsFeatureSupportedByApp(string appId, string capability)
300         {
301             bool supported = Interop.AccountProvider.IsFeatureSupported(appId, capability);
302             if (!supported)
303             {
304                 //Get last result and validate error code.
305                 AccountError err = (AccountError)ErrorFacts.GetLastResult();
306                 if ((err != AccountError.None) && (err != AccountError.RecordNotFound))
307                 {
308                     throw AccountErrorFactory.CreateException(err, "Failed to get IsFeatureSupported");
309                 }
310             }
311
312             return supported;
313         }
314
315         /// <summary>
316         ///     Retrieves capability information with application ID.
317         /// </summary>
318         /// <since_tizen> 3 </since_tizen>
319         /// <param name="appId">application Id</param>
320         /// <returns> Capability information list for the given appId.</returns>
321         /// <privilege>http://tizen.org/privilege/account.read</privilege>
322         /// <exception cref="InvalidOperationException">In case of any DB error or record not found for given appid</exception>
323         /// <exception cref="ArgumentException"> In case of invalid parameter</exception>
324         /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
325         public static IEnumerable<string> GetFeaturesByAppId(string appId)
326         {
327
328             List<string> features = new List<string>();
329             Interop.AccountProvider.AccountProviderFeatureCallback callback = (string applicationId, string key, IntPtr userData) =>
330             {
331                 features.Add(key);
332                 return true;
333             };
334
335             AccountError err = (AccountError)Interop.AccountProvider.GetAccountProviderFeaturesByAppId(callback, appId, IntPtr.Zero);
336             if (err != AccountError.None)
337             {
338                 throw AccountErrorFactory.CreateException(err, "Failed to GetAccountProviderFeaturesByAppId");
339             }
340
341             return (IEnumerable<string>)features;
342         }
343
344         /// <summary>
345         /// Overloaded Dispose API for destroying the AccountProvider Handle.
346         /// </summary>
347         /// <since_tizen> 3 </since_tizen>
348         public void Dispose()
349         {
350             Dispose(true);
351             GC.SuppressFinalize(this);
352         }
353
354         private void Dispose(bool disposing)
355         {
356             if (!disposing)
357             {
358                 if (_handle != IntPtr.Zero)
359                 {
360                     Interop.AccountProvider.Destroy(_handle);
361                     _handle = IntPtr.Zero;
362                 }
363             }
364         }
365     }
366 }