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