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