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