added feature not supported exception in account-provider (#772)
[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             AccountErrorFactory.CheckAccountFeature();
231
232             Dictionary<string, string> labels = new Dictionary<string, string>();
233             Interop.AccountProvider.LabelCallback callback = (string applicationId, string label, string locale, IntPtr userData) =>
234             {
235                 labels.Add(locale, label);
236                 return true;
237             };
238
239             AccountError err = (AccountError)Interop.AccountProvider.GetLablesByAppId(callback, appId, IntPtr.Zero);
240             if (err != AccountError.None)
241             {
242                 throw AccountErrorFactory.CreateException(err, "Failed to GetLablesByAppId");
243             }
244
245             return labels;
246         }
247
248         /// <summary>
249         /// Gets the label information detail of the account provider.
250         /// </summary>
251         /// <since_tizen> 3 </since_tizen>
252         /// <returns> All the labels information for the given account provider.</returns>
253         /// <privilege>http://tizen.org/privilege/account.read</privilege>
254         /// <feature>http://tizen.org/feature/account</feature>
255         /// <exception cref="InvalidOperationException">In case of any DB error.</exception>
256         /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
257         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
258         public Dictionary<string, string> GetLabels()
259         {
260
261             Dictionary<string, string> labels = new Dictionary<string, string>();
262             Interop.AccountProvider.LabelCallback callback = (string applicationId, string label, string locale, IntPtr userData) =>
263             {
264                 labels.Add(locale, label);
265                 return true;
266             };
267
268             AccountError err = (AccountError)Interop.AccountProvider.GetAccountProviderLabels(Handle, callback, IntPtr.Zero);
269             if (err != AccountError.None)
270             {
271                 throw AccountErrorFactory.CreateException(err, "Failed to GetAccountProviderLabels");
272             }
273
274             return labels;
275         }
276
277         /// <summary>
278         /// Checks whether the given appId exists in the account provider DB.
279         /// </summary>
280         /// <since_tizen> 3 </since_tizen>
281         /// <param name="appId">The application ID to check.</param>
282         /// <returns>returns true If App is supported </returns>
283         /// <privilege>http://tizen.org/privilege/account.read</privilege>
284         /// <feature>http://tizen.org/feature/account</feature>
285         /// <exception cref="InvalidOperationException">In case of any DB error or record not found for the given application ID.</exception>
286         /// <exception cref="ArgumentException"> In case of an invalid parameter.</exception>
287         /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
288         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
289         public bool IsAppSupported(string appId)
290         {
291             bool isSupported = false;
292             AccountError res = (AccountError)Interop.AccountProvider.GetAppIdExists(appId);
293
294             if (res != AccountError.None)
295             {
296                 throw AccountErrorFactory.CreateException(res, "Failed to GetLabel for AccountProvider");
297             }
298             else
299             {
300                 isSupported = true;
301             }
302
303             return isSupported;
304         }
305
306         /// <summary>
307         /// Checks whether the given application ID supports the capability.
308         /// </summary>
309         /// <since_tizen> 3 </since_tizen>
310         /// <param name="appId">The application ID.</param>
311         /// <param name="capability">The capability information.</param>
312         /// <returns>
313         /// TRUE if the application supports the given capability,
314         /// otherwise FALSE if the application does not support the given capability
315         /// </returns>
316         /// <privilege>http://tizen.org/privilege/account.read</privilege>
317         /// <feature>http://tizen.org/feature/account</feature>
318         /// <exception cref="InvalidOperationException">In case of any DB error.</exception>
319         /// <exception cref="ArgumentException"> In case of an invalid parameter.</exception>
320         /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
321         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
322         public static bool IsFeatureSupportedByApp(string appId, string capability)
323         {
324             AccountErrorFactory.CheckAccountFeature();
325
326             bool supported = Interop.AccountProvider.IsFeatureSupported(appId, capability);
327             if (!supported)
328             {
329                 //Get last result and validate error code.
330                 AccountError err = (AccountError)ErrorFacts.GetLastResult();
331                 if ((err != AccountError.None) && (err != AccountError.RecordNotFound))
332                 {
333                     throw AccountErrorFactory.CreateException(err, "Failed to get IsFeatureSupported");
334                 }
335             }
336
337             return supported;
338         }
339
340         /// <summary>
341         /// Retrieves capability information with the application ID.
342         /// </summary>
343         /// <since_tizen> 3 </since_tizen>
344         /// <param name="appId">The application ID.</param>
345         /// <returns> Capability information list for the given appId.</returns>
346         /// <privilege>http://tizen.org/privilege/account.read</privilege>
347         /// <feature>http://tizen.org/feature/account</feature>
348         /// <exception cref="InvalidOperationException">In case of any DB error or record not found for the given application ID.</exception>
349         /// <exception cref="ArgumentException"> In case of an invalid parameter.</exception>
350         /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
351         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
352         public static IEnumerable<string> GetFeaturesByAppId(string appId)
353         {
354             AccountErrorFactory.CheckAccountFeature();
355
356             List<string> features = new List<string>();
357             Interop.AccountProvider.AccountProviderFeatureCallback callback = (string applicationId, string key, IntPtr userData) =>
358             {
359                 features.Add(key);
360                 return true;
361             };
362
363             AccountError err = (AccountError)Interop.AccountProvider.GetAccountProviderFeaturesByAppId(callback, appId, IntPtr.Zero);
364             if (err != AccountError.None)
365             {
366                 throw AccountErrorFactory.CreateException(err, "Failed to GetAccountProviderFeaturesByAppId");
367             }
368
369             return (IEnumerable<string>)features;
370         }
371
372         /// <summary>
373         /// Overloaded Dispose API for destroying the AccountProvider Handle.
374         /// </summary>
375         /// <since_tizen> 3 </since_tizen>
376         public void Dispose()
377         {
378             Dispose(true);
379             GC.SuppressFinalize(this);
380         }
381
382         /// <summary>
383         /// Dispose API for destroying the AccountProvider handle.
384         /// </summary>
385         /// <since_tizen> 3 </since_tizen>
386         /// <param name="disposing">The boolean value for destoying AccountProvider handle.</param>
387         protected virtual void Dispose(bool disposing)
388         {
389             if (!disposing)
390             {
391                 if (_handle != IntPtr.Zero)
392                 {
393                     Interop.AccountProvider.Destroy(_handle);
394                     _handle = IntPtr.Zero;
395                 }
396             }
397         }
398     }
399 }