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