04af81c912ee021a7d7c5aca6d308a4a40332a3a
[platform/core/csapi/tizenfx.git] / src / Tizen.Account.AccountManager / Tizen.Account.AccountManager / Account.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
20 namespace Tizen.Account.AccountManager
21 {
22     /// <summary>
23     /// Represents the account information.
24     /// </summary>
25     /// <since_tizen> 3 </since_tizen>
26     public class Account : IDisposable
27     {
28         private readonly SafeAccountHandle _handle;
29
30         /// <summary>
31         /// Account constructor.
32         /// </summary>
33         /// <since_tizen> 4 </since_tizen>
34         /// <param name="handle"> The account handle.</param>
35         public Account(SafeAccountHandle handle)
36         {
37             _handle = handle;
38         }
39
40         /// <summary>
41         /// Account destructor.
42         /// </summary>
43         /// <since_tizen> 4 </since_tizen>
44         ~Account()
45         {
46             Dispose(false);
47         }
48
49         /// <summary>
50         /// Creates a new account instance.
51         /// </summary>
52         /// <since_tizen> 3 </since_tizen>
53         /// <returns>Account Instance.</returns>
54         public static Account CreateAccount()
55         {
56             SafeAccountHandle handle;
57             AccountError err = (AccountError)Interop.Account.Create(out handle);
58             if (err != AccountError.None)
59             {
60                 throw AccountErrorFactory.CreateException(err, "Failed to create new error.");
61             }
62
63             return new Account(handle);
64         }
65
66         /// <summary>
67         /// Id of the Account.
68         /// </summary>
69         /// <since_tizen> 3 </since_tizen>
70         /// <remarks>Account ID shall be created only when the account is added to the database.</remarks>
71         public int AccountId
72         {
73             get
74             {
75                 int id = 0;
76                 AccountError res = (AccountError)Interop.Account.GetAccountId(_handle, out id);
77                 if (res != AccountError.None)
78                 {
79                     Log.Warn(AccountErrorFactory.LogTag, "Failed to get Id for the Account");
80                 }
81
82                 return id;
83             }
84         }
85
86         /// <summary>
87         /// UserName of the account.
88         /// </summary>
89         /// <since_tizen> 3 </since_tizen>
90         /// <value>User name of the account.</value>
91         public string UserName
92         {
93             get
94             {
95                 string name = "";
96                 AccountError res = (AccountError)Interop.Account.GetAccountUserName(_handle, out name);
97                 if (res != AccountError.None)
98                 {
99                     Log.Warn(AccountErrorFactory.LogTag, "Failed to get UserName for the Account");
100                 }
101
102                 return name;
103             }
104
105             set
106             {
107                 AccountError res = (AccountError)Interop.Account.SetAccountUserName(_handle, value);
108                 if (res != AccountError.None)
109                 {
110                     throw AccountErrorFactory.CreateException(res, "Failed to Set UserName for Account");
111                 }
112             }
113         }
114
115         /// <summary>
116         /// Display name of the account.
117         /// </summary>
118         /// <since_tizen> 3 </since_tizen>
119         /// <value>Display name of the account.</value>
120         public string DisplayName
121         {
122             get
123             {
124                 string name = "";
125                 AccountError res = (AccountError)Interop.Account.GetAccountDisplayName(_handle, out name);
126                 if (res != AccountError.None)
127                 {
128                     Log.Warn(AccountErrorFactory.LogTag, "Failed to get DisplayName for the Account");
129                 }
130
131                 return name;
132             }
133
134             set
135             {
136                 AccountError res = (AccountError)Interop.Account.SetAccountDisplayName(_handle, value);
137                 if (res != AccountError.None)
138                 {
139                     throw AccountErrorFactory.CreateException(res, "Failed to Set DisplayName for Account");
140                 }
141             }
142         }
143
144         /// <summary>
145         /// Icon path of the account.
146         /// </summary>
147         /// <since_tizen> 3 </since_tizen>
148         /// <value>Icon path of the account.</value>
149         public string IconPath
150         {
151             get
152             {
153                 string path = "";
154                 AccountError res = (AccountError)Interop.Account.GetAccountIconPath(_handle, out path);
155                 if (res != AccountError.None)
156                 {
157                     Log.Warn(AccountErrorFactory.LogTag, "Failed to get IconPath for the Account");
158                 }
159
160                 return path;
161             }
162
163             set
164             {
165                 AccountError res = (AccountError)Interop.Account.SetAccountIconPath(_handle, value);
166                 if (res != AccountError.None)
167                 {
168                     throw AccountErrorFactory.CreateException(res, "Failed to Set IconPath for Account");
169                 }
170             }
171         }
172
173         /// <summary>
174         /// Domain name of the account.
175         /// </summary>
176         /// <since_tizen> 3 </since_tizen>
177         /// <value>Domain name of the account.</value>
178         public string DomainName
179         {
180             get
181             {
182                 string name = "";
183                 AccountError res = (AccountError)Interop.Account.GetAccountDomainName(_handle, out name);
184                 if (res != AccountError.None)
185                 {
186                     Log.Warn(AccountErrorFactory.LogTag, "Failed to get DomainName for the Account");
187                 }
188
189                 return name;
190             }
191
192             set
193             {
194                 AccountError res = (AccountError)Interop.Account.SetAccountDomainName(_handle, value);
195                 if (res != AccountError.None)
196                 {
197                     throw AccountErrorFactory.CreateException(res, "Failed to Set DomainName for Account");
198                 }
199             }
200         }
201
202         /// <summary>
203         /// Email ID of the account.
204         /// </summary>
205         /// <since_tizen> 3 </since_tizen>
206         /// <value>Email ID of the account.</value>
207         public string EmailId
208         {
209             get
210             {
211                 string email = "";
212                 AccountError res = (AccountError)Interop.Account.GetAccountEmail(_handle, out email);
213                 if (res != AccountError.None)
214                 {
215                     Log.Warn(AccountErrorFactory.LogTag, "Failed to get email for the Account");
216                 }
217
218                 return email;
219             }
220
221             set
222             {
223                 AccountError res = (AccountError)Interop.Account.SetAccountEmail(_handle, value);
224                 if (res != AccountError.None)
225                 {
226                     throw AccountErrorFactory.CreateException(res, "Failed to Set email for Account");
227                 }
228             }
229         }
230
231         /// <summary>
232         /// Package name of the account.
233         /// </summary>
234         /// <since_tizen> 3 </since_tizen>
235         /// <value>Package name of the account.</value>
236         public string PackageName
237         {
238             get
239             {
240                 string name = "";
241                 AccountError res = (AccountError)Interop.Account.GetAccountPackageName(_handle, out name);
242                 if (res != AccountError.None)
243                 {
244                     Log.Warn(AccountErrorFactory.LogTag, "Failed to get PacakageName for the Account");
245                 }
246
247                 return name;
248             }
249
250             set
251             {
252                 AccountError res = (AccountError)Interop.Account.SetAccountPackageName(_handle, value);
253                 if (res != AccountError.None)
254                 {
255                     throw AccountErrorFactory.CreateException(res, "Failed to Set PacakageName for Account");
256                 }
257             }
258         }
259
260         /// <summary>
261         /// Access token of the account.
262         /// </summary>
263         /// <since_tizen> 3 </since_tizen>
264         /// <value>Access token of the account.</value>
265         public string AccessToken
266         {
267             get
268             {
269                 string token = "";
270                 AccountError res = (AccountError)Interop.Account.GetAccountAccessToken(_handle, out token);
271                 if (res != AccountError.None)
272                 {
273                     Log.Warn(AccountErrorFactory.LogTag, "Failed to get token for the Account");
274                 }
275
276                 return token;
277             }
278
279             set
280             {
281                 AccountError res = (AccountError)Interop.Account.SetAccountAccessToken(_handle, value);
282                 if (res != AccountError.None)
283                 {
284                     throw AccountErrorFactory.CreateException(res, "Failed to Set token for Account");
285                 }
286             }
287         }
288
289         /// <summary>
290         /// Authentication type of the account.
291         /// </summary>
292         /// <since_tizen> 3 </since_tizen>
293         /// <value>Authentication type of the account.</value>
294         public AccountAuthType AuthType
295         {
296             get
297             {
298                 int user;
299                 AccountError res = (AccountError)Interop.Account.GetAccountAuthType(_handle, out user);
300                 if (res != AccountError.None)
301                 {
302                     Log.Warn(AccountErrorFactory.LogTag, "Failed to get AuthType for the Account");
303                 }
304
305                 return (AccountAuthType)user;
306             }
307
308             set
309             {
310                 AccountError res = (AccountError)Interop.Account.SetAccountAuthType(_handle, (int)value);
311                 if (res != AccountError.None)
312                 {
313                     throw AccountErrorFactory.CreateException(res, "Failed to Set AuthType for Account");
314                 }
315             }
316         }
317
318         /// <summary>
319         /// Secrecy state of the account.
320         /// </summary>
321         /// <since_tizen> 3 </since_tizen>
322         /// <value>Secrecy state of the account.</value>
323         public AccountSecrecyState SecrecyState
324         {
325             get
326             {
327                 int state;
328                 AccountError res = (AccountError)Interop.Account.GetAccountSercet(_handle, out state);
329                 if (res != AccountError.None)
330                 {
331                     Log.Warn(AccountErrorFactory.LogTag, "Failed to get User Secret for the Account");
332                 }
333
334                 return (AccountSecrecyState)state;
335             }
336
337             set
338             {
339                 AccountError res = (AccountError)Interop.Account.SetAccountSecret(_handle, (int)value);
340                 if (res != AccountError.None)
341                 {
342                     throw AccountErrorFactory.CreateException(res, "Failed to Set User Secret for Account");
343                 }
344             }
345         }
346
347         /// <summary>
348         /// Sync state of the account.
349         /// </summary>
350         /// <since_tizen> 3 </since_tizen>
351         /// <value>Sync state of the account.</value>
352         public AccountSyncState SyncState
353         {
354             get
355             {
356                 int supported;
357                 AccountError res = (AccountError)Interop.Account.GetAccountSyncSupport(_handle, out supported);
358                 if (res != AccountError.None)
359                 {
360                     Log.Warn(AccountErrorFactory.LogTag, "Failed to get AuthType for the Account");
361                 }
362
363                 return (AccountSyncState)supported;
364             }
365
366             set
367             {
368                 AccountError res = (AccountError)Interop.Account.SetAccountSyncSupport(_handle, (int)value);
369                 if (res != AccountError.None)
370                 {
371                     throw AccountErrorFactory.CreateException(res, "Failed to Set AuthType for Account");
372                 }
373             }
374         }
375
376         /// <summary>
377         /// Source of the account.
378         /// </summary>
379         /// <since_tizen> 3 </since_tizen>
380         /// <value>Account source.</value>
381         public string Source
382         {
383             get
384             {
385                 string text = "";
386                 AccountError res = (AccountError)Interop.Account.GetAccountSource(_handle, out text);
387                 if (res != AccountError.None)
388                 {
389                     Log.Warn(AccountErrorFactory.LogTag, "Failed to get User Secret for the Account");
390                 }
391
392                 return text;
393             }
394
395             set
396             {
397                 AccountError res = (AccountError)Interop.Account.SetAccountSource(_handle, value);
398                 if (res != AccountError.None)
399                 {
400                     throw AccountErrorFactory.CreateException(res, "Failed to Set User Secret for Account");
401                 }
402             }
403         }
404
405                 /// <summary>
406         /// Handle of the account.
407         /// </summary>
408         /// <since_tizen> 4 </since_tizen>
409         /// <value>Account handle.</value>
410         public SafeAccountHandle SafeAccountHandle
411         {
412             get
413             {
414                 return _handle;
415             }
416         }
417         /// <summary>
418         /// Sets the account capability.
419         /// </summary>
420         /// <since_tizen> 3 </since_tizen>
421         /// <param name="capabilityType"> The account capability type.</param>
422         /// <param name="state">The account capability state.</param>
423         /// <exception cref="ArgumentException">In case of an invalid parameter.</exception>
424         public void SetCapability(string capabilityType, CapabilityState state)
425         {
426             AccountError ret = (AccountError)Interop.Account.SetAccountCapability(_handle, capabilityType, (int)state);
427             if (ret != AccountError.None)
428             {
429                 throw AccountErrorFactory.CreateException(ret, "failed to set account capability");
430             }
431         }
432         /// <summary>
433         /// Gets all the capabilities of the account.
434         /// </summary>
435         /// <since_tizen> 3 </since_tizen>
436         /// <param name="capabilityType"> The capability type to get the capability value.</param>
437         /// <returns>The capability value (on/off) of the specified CapabilityState.</returns>
438         /// <exception cref="ArgumentException">In case of an invalid parameter.</exception>
439         public CapabilityState GetCapability(string capabilityType)
440         {
441             int type;
442             AccountError res = (AccountError)Interop.Account.GetAccountCapability(_handle, capabilityType, out type);
443             if (res != AccountError.None)
444             {
445                 throw AccountErrorFactory.CreateException(res, "Failed to GetCapability for Account");
446             }
447
448             return (CapabilityState)type;
449         }
450
451         /// <summary>
452         /// Gets all the capabilities of the account.
453         /// </summary>
454         /// <since_tizen> 3 </since_tizen>
455         /// <returns>List of capabilities as dictionary.</returns>
456         public Dictionary<string, CapabilityState> GetAllCapabilities()
457         {
458
459             AccountError res = AccountError.None;
460             Dictionary<string, CapabilityState> list = new Dictionary<string, CapabilityState>();
461             Interop.Account.AccountCapabilityCallback capabilityCallback = (string type, int state, IntPtr data) =>
462             {
463                 list.Add(type, (CapabilityState)state);
464                 return true;
465             };
466
467             res = (AccountError)Interop.Account.GetAllAccountCapabilities(_handle, capabilityCallback, IntPtr.Zero);
468             if (res != AccountError.None)
469             {
470                 throw AccountErrorFactory.CreateException(res, "Failed to get account capabilities");
471             }
472
473             return list;
474         }
475
476         /// <summary>
477         /// Sets the custom value to the account.
478         /// </summary>
479         /// <since_tizen> 3 </since_tizen>
480         /// <param name="key">Key to be added to the account.</param>
481         /// <param name="value">Value to be updated for respective key for the account.</param>
482         /// <exception cref="ArgumentException">In case of an invalid parameter.</exception>
483         public void SetCustomValue(string key, string value)
484         {
485             AccountError err = (AccountError)Interop.Account.SetAccountCustomValue(_handle, key, value);
486             if (err != AccountError.None)
487             {
488                 throw AccountErrorFactory.CreateException(err, "Failed to set the value for : " + key);
489             }
490         }
491
492         /// <summary>
493         /// Gets the user specific custom text of an account key.
494         /// </summary>
495         /// <since_tizen> 3 </since_tizen>
496         /// <param name="key">The key to retrieve custom text.</param>
497         /// <returns>The text of the given key.</returns>
498         /// <exception cref="ArgumentException">In case of an invalid parameter.</exception>
499         /// <exception cref="InvalidOperationException">If there is no given capability type in the account.</exception>
500         public string GetCustomValue(string key)
501         {
502             string result = "";
503             AccountError err = (AccountError)Interop.Account.GetAccountCustomValue(_handle, key, out result);
504             if (err != AccountError.None)
505             {
506                 throw AccountErrorFactory.CreateException(err, "Failed to get the value for : " + key);
507             }
508
509             return result;
510         }
511
512         /// <summary>
513         /// Gets all the custom values.
514         /// </summary>
515         /// <since_tizen> 3 </since_tizen>
516         /// <returns>List of custom key, value pairs as dictionary.</returns>
517         public Dictionary<string, string> GetAllCustomValues()
518         {
519             AccountError res = AccountError.None;
520             Dictionary<string, string> list = new Dictionary<string, string>();
521
522             Interop.Account.AccountCustomCallback customCallback = (string key, string value, IntPtr data) =>
523             {
524                 list.Add(key, value);
525                 return true;
526             };
527
528             res = (AccountError)Interop.Account.GetAllAccountCustomValues(_handle, customCallback, IntPtr.Zero);
529
530             if (res != AccountError.None)
531             {
532                 throw AccountErrorFactory.CreateException(res, "Failed to get account capabilities");
533             }
534
535             return list;
536         }
537
538         /// <summary>
539         /// Sets the user text.
540         /// </summary>
541         /// <since_tizen> 3 </since_tizen>
542         /// <param name="index">The index of the user text (must be in range from 0 to 4).</param>
543         /// <param name="text">The text string to set as the user text.</param>
544         /// <exception cref="ArgumentException">In case of an invalid parameter.</exception>
545         public void SetUserText(int index, string text)
546         {
547             AccountError err = (AccountError)Interop.Account.SetAccountUserText(_handle, index, text);
548             if (err != AccountError.None)
549             {
550                 throw AccountErrorFactory.CreateException(err, "Failed to get the value for : " + index);
551             }
552         }
553
554         /// <summary>
555         /// Gets the user text.
556         /// </summary>
557         /// <since_tizen> 3 </since_tizen>
558         /// <param name="index">The index of the user text (must be in range from 0 to 4).</param>
559         /// <returns>The user text of the given key.</returns>
560         /// <exception cref="ArgumentException">In case of an invalid parameter.</exception>
561         /// <exception cref="OutOfMemoryException">In case of out of memory.</exception>
562         public string GetUserText(int index)
563         {
564             string result = "";
565             AccountError err = (AccountError)Interop.Account.GetAccountUserText(_handle, index, out result);
566             if (err != AccountError.None)
567             {
568                 throw AccountErrorFactory.CreateException(err, "Failed to get the value for : " + index);
569             }
570
571             return result;
572         }
573
574         /// <summary>
575         /// Gets the user integer value.
576         /// </summary>
577         /// <since_tizen> 3 </since_tizen>
578         /// <param name="index">The index of the user integer (must be in range from 0 to 4).</param>
579         /// <returns>The user integer of the given key.</returns>
580         /// <exception cref="ArgumentException">In case of an invalid parameter.</exception>
581         public int GetUserInt(int index)
582         {
583             int result = -1;
584             AccountError err = (AccountError)Interop.Account.GetAccountUserInt(_handle, index, out result);
585             if (err != AccountError.None)
586             {
587                 throw AccountErrorFactory.CreateException(err, "Failed to get the value for : " + index);
588             }
589
590             return result;
591         }
592
593         /// <summary>
594         /// Sets the user integer value.
595         /// </summary>
596         /// <since_tizen> 3 </since_tizen>
597         /// <param name="index">The index of the user integer (must be in range from 0 to 4).</param>
598         /// <param name="value">The integer to set as the user integer.</param>
599         /// <exception cref="ArgumentException">In case of an invalid parameter.</exception>
600         public void SetUserInt(int index, int value)
601         {
602             AccountError err = (AccountError)Interop.Account.SetAccountUserInt(_handle, index, value);
603             if (err != AccountError.None)
604             {
605                 throw AccountErrorFactory.CreateException(err, "Failed to get the value for : " + index);
606             }
607         }
608
609         /// <summary>
610         /// Overloaded Dispose API for destroying the account handle.
611         /// </summary>
612         /// <since_tizen> 3 </since_tizen>
613         public void Dispose()
614         {
615             Dispose(true);
616             GC.SuppressFinalize(this);
617         }
618
619         /// <summary>
620         /// Dispose API for destroying the account handle.
621         /// </summary>
622         /// <since_tizen> 3 </since_tizen>
623         /// <param name="disposing">The boolean value for destoying account handle.</param>
624         protected virtual void Dispose(bool disposing)
625         {
626             if (!disposing)
627             {
628                 _handle.Dispose();
629             }
630         }
631     }
632 }