2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 using System.Collections.Generic;
20 namespace Tizen.Account.AccountManager
23 /// Represents the account information.
25 /// <since_tizen> 3 </since_tizen>
26 public class Account : IDisposable
28 private readonly SafeAccountHandle _handle;
31 /// Account constructor.
33 /// <since_tizen> 4 </since_tizen>
34 /// <param name="handle"> The account handle.</param>
35 public Account(SafeAccountHandle handle)
41 /// Account destructor.
43 /// <since_tizen> 4 </since_tizen>
50 /// Creates a new account instance.
52 /// <since_tizen> 3 </since_tizen>
53 /// <returns>Account Instance.</returns>
54 public static Account CreateAccount()
56 SafeAccountHandle handle;
57 AccountError err = (AccountError)Interop.Account.Create(out handle);
58 if (err != AccountError.None)
60 throw AccountErrorFactory.CreateException(err, "Failed to create new error.");
63 return new Account(handle);
67 /// Id of the Account.
69 /// <since_tizen> 3 </since_tizen>
70 /// <remarks>Account ID shall be created only when the account is added to the database.</remarks>
76 AccountError res = (AccountError)Interop.Account.GetAccountId(_handle, out id);
77 if (res != AccountError.None)
79 Log.Warn(AccountErrorFactory.LogTag, "Failed to get Id for the Account");
87 /// UserName of the account.
89 /// <since_tizen> 3 </since_tizen>
90 /// <value>User name of the account.</value>
91 public string UserName
96 AccountError res = (AccountError)Interop.Account.GetAccountUserName(_handle, out name);
97 if (res != AccountError.None)
99 Log.Warn(AccountErrorFactory.LogTag, "Failed to get UserName for the Account");
107 AccountError res = (AccountError)Interop.Account.SetAccountUserName(_handle, value);
108 if (res != AccountError.None)
110 throw AccountErrorFactory.CreateException(res, "Failed to Set UserName for Account");
116 /// Display name of the account.
118 /// <since_tizen> 3 </since_tizen>
119 /// <value>Display name of the account.</value>
120 public string DisplayName
125 AccountError res = (AccountError)Interop.Account.GetAccountDisplayName(_handle, out name);
126 if (res != AccountError.None)
128 Log.Warn(AccountErrorFactory.LogTag, "Failed to get DisplayName for the Account");
136 AccountError res = (AccountError)Interop.Account.SetAccountDisplayName(_handle, value);
137 if (res != AccountError.None)
139 throw AccountErrorFactory.CreateException(res, "Failed to Set DisplayName for Account");
145 /// Icon path of the account.
147 /// <since_tizen> 3 </since_tizen>
148 /// <value>Icon path of the account.</value>
149 public string IconPath
154 AccountError res = (AccountError)Interop.Account.GetAccountIconPath(_handle, out path);
155 if (res != AccountError.None)
157 Log.Warn(AccountErrorFactory.LogTag, "Failed to get IconPath for the Account");
165 AccountError res = (AccountError)Interop.Account.SetAccountIconPath(_handle, value);
166 if (res != AccountError.None)
168 throw AccountErrorFactory.CreateException(res, "Failed to Set IconPath for Account");
174 /// Domain name of the account.
176 /// <since_tizen> 3 </since_tizen>
177 /// <value>Domain name of the account.</value>
178 public string DomainName
183 AccountError res = (AccountError)Interop.Account.GetAccountDomainName(_handle, out name);
184 if (res != AccountError.None)
186 Log.Warn(AccountErrorFactory.LogTag, "Failed to get DomainName for the Account");
194 AccountError res = (AccountError)Interop.Account.SetAccountDomainName(_handle, value);
195 if (res != AccountError.None)
197 throw AccountErrorFactory.CreateException(res, "Failed to Set DomainName for Account");
203 /// Email ID of the account.
205 /// <since_tizen> 3 </since_tizen>
206 /// <value>Email ID of the account.</value>
207 public string EmailId
212 AccountError res = (AccountError)Interop.Account.GetAccountEmail(_handle, out email);
213 if (res != AccountError.None)
215 Log.Warn(AccountErrorFactory.LogTag, "Failed to get email for the Account");
223 AccountError res = (AccountError)Interop.Account.SetAccountEmail(_handle, value);
224 if (res != AccountError.None)
226 throw AccountErrorFactory.CreateException(res, "Failed to Set email for Account");
232 /// Package name of the account.
234 /// <since_tizen> 3 </since_tizen>
235 /// <value>Package name of the account.</value>
236 public string PackageName
241 AccountError res = (AccountError)Interop.Account.GetAccountPackageName(_handle, out name);
242 if (res != AccountError.None)
244 Log.Warn(AccountErrorFactory.LogTag, "Failed to get PacakageName for the Account");
252 AccountError res = (AccountError)Interop.Account.SetAccountPackageName(_handle, value);
253 if (res != AccountError.None)
255 throw AccountErrorFactory.CreateException(res, "Failed to Set PacakageName for Account");
261 /// Access token of the account.
263 /// <since_tizen> 3 </since_tizen>
264 /// <value>Access token of the account.</value>
265 public string AccessToken
270 AccountError res = (AccountError)Interop.Account.GetAccountAccessToken(_handle, out token);
271 if (res != AccountError.None)
273 Log.Warn(AccountErrorFactory.LogTag, "Failed to get token for the Account");
281 AccountError res = (AccountError)Interop.Account.SetAccountAccessToken(_handle, value);
282 if (res != AccountError.None)
284 throw AccountErrorFactory.CreateException(res, "Failed to Set token for Account");
290 /// Authentication type of the account.
292 /// <since_tizen> 3 </since_tizen>
293 /// <value>Authentication type of the account.</value>
294 public AccountAuthType AuthType
299 AccountError res = (AccountError)Interop.Account.GetAccountAuthType(_handle, out user);
300 if (res != AccountError.None)
302 Log.Warn(AccountErrorFactory.LogTag, "Failed to get AuthType for the Account");
305 return (AccountAuthType)user;
310 AccountError res = (AccountError)Interop.Account.SetAccountAuthType(_handle, (int)value);
311 if (res != AccountError.None)
313 throw AccountErrorFactory.CreateException(res, "Failed to Set AuthType for Account");
319 /// Secrecy state of the account.
321 /// <since_tizen> 3 </since_tizen>
322 /// <value>Secrecy state of the account.</value>
323 public AccountSecrecyState SecrecyState
328 AccountError res = (AccountError)Interop.Account.GetAccountSercet(_handle, out state);
329 if (res != AccountError.None)
331 Log.Warn(AccountErrorFactory.LogTag, "Failed to get User Secret for the Account");
334 return (AccountSecrecyState)state;
339 AccountError res = (AccountError)Interop.Account.SetAccountSecret(_handle, (int)value);
340 if (res != AccountError.None)
342 throw AccountErrorFactory.CreateException(res, "Failed to Set User Secret for Account");
348 /// Sync state of the account.
350 /// <since_tizen> 3 </since_tizen>
351 /// <value>Sync state of the account.</value>
352 public AccountSyncState SyncState
357 AccountError res = (AccountError)Interop.Account.GetAccountSyncSupport(_handle, out supported);
358 if (res != AccountError.None)
360 Log.Warn(AccountErrorFactory.LogTag, "Failed to get AuthType for the Account");
363 return (AccountSyncState)supported;
368 AccountError res = (AccountError)Interop.Account.SetAccountSyncSupport(_handle, (int)value);
369 if (res != AccountError.None)
371 throw AccountErrorFactory.CreateException(res, "Failed to Set AuthType for Account");
377 /// Source of the account.
379 /// <since_tizen> 3 </since_tizen>
380 /// <value>Account source.</value>
386 AccountError res = (AccountError)Interop.Account.GetAccountSource(_handle, out text);
387 if (res != AccountError.None)
389 Log.Warn(AccountErrorFactory.LogTag, "Failed to get User Secret for the Account");
397 AccountError res = (AccountError)Interop.Account.SetAccountSource(_handle, value);
398 if (res != AccountError.None)
400 throw AccountErrorFactory.CreateException(res, "Failed to Set User Secret for Account");
406 /// Handle of the account.
408 /// <since_tizen> 4 </since_tizen>
409 /// <value>Account handle.</value>
410 public SafeAccountHandle SafeAccountHandle
418 /// Sets the account capability.
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)
426 AccountError ret = (AccountError)Interop.Account.SetAccountCapability(_handle, capabilityType, (int)state);
427 if (ret != AccountError.None)
429 throw AccountErrorFactory.CreateException(ret, "failed to set account capability");
433 /// Gets all the capabilities of the account.
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)
442 AccountError res = (AccountError)Interop.Account.GetAccountCapability(_handle, capabilityType, out type);
443 if (res != AccountError.None)
445 throw AccountErrorFactory.CreateException(res, "Failed to GetCapability for Account");
448 return (CapabilityState)type;
452 /// Gets all the capabilities of the account.
454 /// <since_tizen> 3 </since_tizen>
455 /// <returns>List of capabilities as dictionary.</returns>
456 public Dictionary<string, CapabilityState> GetAllCapabilities()
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) =>
463 list.Add(type, (CapabilityState)state);
467 res = (AccountError)Interop.Account.GetAllAccountCapabilities(_handle, capabilityCallback, IntPtr.Zero);
468 if (res != AccountError.None)
470 throw AccountErrorFactory.CreateException(res, "Failed to get account capabilities");
477 /// Sets the custom value to the account.
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)
485 AccountError err = (AccountError)Interop.Account.SetAccountCustomValue(_handle, key, value);
486 if (err != AccountError.None)
488 throw AccountErrorFactory.CreateException(err, "Failed to set the value for : " + key);
493 /// Gets the user specific custom text of an account key.
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)
503 AccountError err = (AccountError)Interop.Account.GetAccountCustomValue(_handle, key, out result);
504 if (err != AccountError.None)
506 throw AccountErrorFactory.CreateException(err, "Failed to get the value for : " + key);
513 /// Gets all the custom values.
515 /// <since_tizen> 3 </since_tizen>
516 /// <returns>List of custom key, value pairs as dictionary.</returns>
517 public Dictionary<string, string> GetAllCustomValues()
519 AccountError res = AccountError.None;
520 Dictionary<string, string> list = new Dictionary<string, string>();
522 Interop.Account.AccountCustomCallback customCallback = (string key, string value, IntPtr data) =>
524 list.Add(key, value);
528 res = (AccountError)Interop.Account.GetAllAccountCustomValues(_handle, customCallback, IntPtr.Zero);
530 if (res != AccountError.None)
532 throw AccountErrorFactory.CreateException(res, "Failed to get account capabilities");
539 /// Sets the user text.
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)
547 AccountError err = (AccountError)Interop.Account.SetAccountUserText(_handle, index, text);
548 if (err != AccountError.None)
550 throw AccountErrorFactory.CreateException(err, "Failed to get the value for : " + index);
555 /// Gets the user text.
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)
565 AccountError err = (AccountError)Interop.Account.GetAccountUserText(_handle, index, out result);
566 if (err != AccountError.None)
568 throw AccountErrorFactory.CreateException(err, "Failed to get the value for : " + index);
575 /// Gets the user integer value.
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)
584 AccountError err = (AccountError)Interop.Account.GetAccountUserInt(_handle, index, out result);
585 if (err != AccountError.None)
587 throw AccountErrorFactory.CreateException(err, "Failed to get the value for : " + index);
594 /// Sets the user integer value.
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)
602 AccountError err = (AccountError)Interop.Account.SetAccountUserInt(_handle, index, value);
603 if (err != AccountError.None)
605 throw AccountErrorFactory.CreateException(err, "Failed to get the value for : " + index);
610 /// Overloaded Dispose API for destroying the account handle.
612 /// <since_tizen> 3 </since_tizen>
613 public void Dispose()
616 GC.SuppressFinalize(this);
620 /// Dispose API for destroying the account handle.
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)