[TCSACR-81][ADD] Added new property to get account handle
[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     /// The 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         /// The 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 the 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 the 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 the account provider if it 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         /// <feature>http://tizen.org/feature/account</feature>
153         /// <returns>
154         /// The list of capability information.
155         /// </returns>
156         /// <exception cref="InvalidOperationException">In case of any DB error.</exception>
157         /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
158         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
159         public IEnumerable<string> GetAllCapabilities()
160         {
161             List<string> capabilities = new List<string>();
162             AccountError res;
163             Interop.AccountProvider.AccountProviderFeatureCallback callback = (string appId, string key, IntPtr data) =>
164             {
165                 capabilities.Add(key);
166                 return true;
167             };
168
169             res = (AccountError)Interop.AccountProvider.GetAccountProviderFeatures(Handle, callback, IntPtr.Zero);
170             if (res != AccountError.None)
171             {
172                 throw AccountErrorFactory.CreateException(res, "Failed to GetAllCapabilities for AccountProvider");
173             }
174
175             return capabilities;
176         }
177
178         /// <summary>
179         /// Gets the specific label information detail of the account provider.
180         /// </summary>
181         /// <since_tizen> 3 </since_tizen>
182         /// <param name="locale">
183         /// 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.
184         /// For example, "ko_KR" or "ko-kr" for Korean, "en_US" or "en-us" for American English.
185         /// </param>
186         /// <returns>The label text given for the locale.</returns>
187         /// <privilege>http://tizen.org/privilege/account.read</privilege>
188         /// <feature>http://tizen.org/feature/account</feature>
189         /// <exception cref="InvalidOperationException">In case of any DB error or record not found for given locale.</exception>
190         /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
191         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
192         public string GetLabel(string locale)
193         {
194             string label;
195             AccountError res = (AccountError)Interop.AccountProvider.GetlabelbyLocale(Handle, locale, out label);
196             if (res != AccountError.None)
197             {
198                 throw AccountErrorFactory.CreateException(res, "Failed to GetLabel for AccountProvider");
199             }
200
201             return label;
202         }
203
204         /// <summary>
205         /// Gets the specific label information detail of the account provider.
206         /// </summary>
207         /// <since_tizen> 3 </since_tizen>
208         /// <param name="appId">
209         /// The application ID to search.
210         /// </param>
211         /// <returns> All the labels information for the given application ID.</returns>
212         /// <privilege>http://tizen.org/privilege/account.read</privilege>
213         /// <feature>http://tizen.org/feature/account</feature>
214         /// <exception cref="InvalidOperationException">In case of any DB error or record not found for given the application ID.</exception>
215         /// <exception cref="ArgumentException"> In case of an invalid parameter.</exception>
216         /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
217         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
218         public static Dictionary<string, string> GetLabelsByAppId(string appId)
219         {
220
221             Dictionary<string, string> labels = new Dictionary<string, string>();
222             Interop.AccountProvider.LabelCallback callback = (string applicationId, string label, string locale, IntPtr userData) =>
223             {
224                 labels.Add(locale, label);
225                 return true;
226             };
227
228             AccountError err = (AccountError)Interop.AccountProvider.GetLablesByAppId(callback, appId, IntPtr.Zero);
229             if (err != AccountError.None)
230             {
231                 throw AccountErrorFactory.CreateException(err, "Failed to GetLablesByAppId");
232             }
233
234             return labels;
235         }
236
237         /// <summary>
238         /// Gets the label information detail of the account provider.
239         /// </summary>
240         /// <since_tizen> 3 </since_tizen>
241         /// <returns> All the labels information for the given account provider.</returns>
242         /// <privilege>http://tizen.org/privilege/account.read</privilege>
243         /// <feature>http://tizen.org/feature/account</feature>
244         /// <exception cref="InvalidOperationException">In case of any DB error.</exception>
245         /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
246         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
247         public Dictionary<string, string> GetLabels()
248         {
249
250             Dictionary<string, string> labels = new Dictionary<string, string>();
251             Interop.AccountProvider.LabelCallback callback = (string applicationId, string label, string locale, IntPtr userData) =>
252             {
253                 labels.Add(locale, label);
254                 return true;
255             };
256
257             AccountError err = (AccountError)Interop.AccountProvider.GetAccountProviderLabels(Handle, callback, IntPtr.Zero);
258             if (err != AccountError.None)
259             {
260                 throw AccountErrorFactory.CreateException(err, "Failed to GetAccountProviderLabels");
261             }
262
263             return labels;
264         }
265
266         /// <summary>
267         /// Checks whether the given appId exists in the account provider DB.
268         /// </summary>
269         /// <since_tizen> 3 </since_tizen>
270         /// <param name="appId">The application ID to check.</param>
271         /// <returns>returns true If App is supported </returns>
272         /// <privilege>http://tizen.org/privilege/account.read</privilege>
273         /// <feature>http://tizen.org/feature/account</feature>
274         /// <exception cref="InvalidOperationException">In case of any DB error or record not found for the given application ID.</exception>
275         /// <exception cref="ArgumentException"> In case of an invalid parameter.</exception>
276         /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
277         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
278         public bool IsAppSupported(string appId)
279         {
280             bool isSupported = false;
281             AccountError res = (AccountError)Interop.AccountProvider.GetAppIdExists(appId);
282
283             if (res != AccountError.None)
284             {
285                 throw AccountErrorFactory.CreateException(res, "Failed to GetLabel for AccountProvider");
286             }
287             else
288             {
289                 isSupported = true;
290             }
291
292             return isSupported;
293         }
294
295         /// <summary>
296         /// Checks whether the given application ID supports the capability.
297         /// </summary>
298         /// <since_tizen> 3 </since_tizen>
299         /// <param name="appId">The application ID.</param>
300         /// <param name="capability">The capability information.</param>
301         /// <returns>
302         /// TRUE if the application supports the given capability,
303         /// otherwise FALSE if the application does not support the given capability
304         /// </returns>
305         /// <privilege>http://tizen.org/privilege/account.read</privilege>
306         /// <feature>http://tizen.org/feature/account</feature>
307         /// <exception cref="InvalidOperationException">In case of any DB error.</exception>
308         /// <exception cref="ArgumentException"> In case of an invalid parameter.</exception>
309         /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
310         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
311         public static bool IsFeatureSupportedByApp(string appId, string capability)
312         {
313             bool supported = Interop.AccountProvider.IsFeatureSupported(appId, capability);
314             if (!supported)
315             {
316                 //Get last result and validate error code.
317                 AccountError err = (AccountError)ErrorFacts.GetLastResult();
318                 if ((err != AccountError.None) && (err != AccountError.RecordNotFound))
319                 {
320                     throw AccountErrorFactory.CreateException(err, "Failed to get IsFeatureSupported");
321                 }
322             }
323
324             return supported;
325         }
326
327         /// <summary>
328         /// Retrieves capability information with the application ID.
329         /// </summary>
330         /// <since_tizen> 3 </since_tizen>
331         /// <param name="appId">The application ID.</param>
332         /// <returns> Capability information list for the given appId.</returns>
333         /// <privilege>http://tizen.org/privilege/account.read</privilege>
334         /// <feature>http://tizen.org/feature/account</feature>
335         /// <exception cref="InvalidOperationException">In case of any DB error or record not found for the given application ID.</exception>
336         /// <exception cref="ArgumentException"> In case of an invalid parameter.</exception>
337         /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
338         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
339         public static IEnumerable<string> GetFeaturesByAppId(string appId)
340         {
341
342             List<string> features = new List<string>();
343             Interop.AccountProvider.AccountProviderFeatureCallback callback = (string applicationId, string key, IntPtr userData) =>
344             {
345                 features.Add(key);
346                 return true;
347             };
348
349             AccountError err = (AccountError)Interop.AccountProvider.GetAccountProviderFeaturesByAppId(callback, appId, IntPtr.Zero);
350             if (err != AccountError.None)
351             {
352                 throw AccountErrorFactory.CreateException(err, "Failed to GetAccountProviderFeaturesByAppId");
353             }
354
355             return (IEnumerable<string>)features;
356         }
357
358         /// <summary>
359         /// Overloaded Dispose API for destroying the AccountProvider Handle.
360         /// </summary>
361         /// <since_tizen> 3 </since_tizen>
362         public void Dispose()
363         {
364             Dispose(true);
365             GC.SuppressFinalize(this);
366         }
367
368         private void Dispose(bool disposing)
369         {
370             if (!disposing)
371             {
372                 if (_handle != IntPtr.Zero)
373                 {
374                     Interop.AccountProvider.Destroy(_handle);
375                     _handle = IntPtr.Zero;
376                 }
377             }
378         }
379     }
380 }