/* * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved * * Licensed under the Apache License, Version 2.0 (the License); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ using System; using System.Collections.Generic; namespace Tizen.Account.AccountManager { /// /// Represents the account information. /// /// 3 public class Account : IDisposable { private readonly SafeAccountHandle _handle; /// /// Account constructor. /// /// 4 /// The account handle. public Account(SafeAccountHandle handle) { _handle = handle; } /// /// Account destructor. /// /// 4 ~Account() { Dispose(false); } /// /// Creates a new account instance. /// /// 3 /// Account Instance. public static Account CreateAccount() { SafeAccountHandle handle; AccountError err = (AccountError)Interop.Account.Create(out handle); if (err != AccountError.None) { throw AccountErrorFactory.CreateException(err, "Failed to create new error."); } return new Account(handle); } /// /// Id of the Account. /// /// 3 /// Account ID shall be created only when the account is added to the database. public int AccountId { get { int id = 0; AccountError res = (AccountError)Interop.Account.GetAccountId(_handle, out id); if (res != AccountError.None) { Log.Warn(AccountErrorFactory.LogTag, "Failed to get Id for the Account"); } return id; } } /// /// UserName of the account. /// /// 3 /// User name of the account. public string UserName { get { string name = ""; AccountError res = (AccountError)Interop.Account.GetAccountUserName(_handle, out name); if (res != AccountError.None) { Log.Warn(AccountErrorFactory.LogTag, "Failed to get UserName for the Account"); } return name; } set { AccountError res = (AccountError)Interop.Account.SetAccountUserName(_handle, value); if (res != AccountError.None) { throw AccountErrorFactory.CreateException(res, "Failed to Set UserName for Account"); } } } /// /// Display name of the account. /// /// 3 /// Display name of the account. public string DisplayName { get { string name = ""; AccountError res = (AccountError)Interop.Account.GetAccountDisplayName(_handle, out name); if (res != AccountError.None) { Log.Warn(AccountErrorFactory.LogTag, "Failed to get DisplayName for the Account"); } return name; } set { AccountError res = (AccountError)Interop.Account.SetAccountDisplayName(_handle, value); if (res != AccountError.None) { throw AccountErrorFactory.CreateException(res, "Failed to Set DisplayName for Account"); } } } /// /// Icon path of the account. /// /// 3 /// Icon path of the account. public string IconPath { get { string path = ""; AccountError res = (AccountError)Interop.Account.GetAccountIconPath(_handle, out path); if (res != AccountError.None) { Log.Warn(AccountErrorFactory.LogTag, "Failed to get IconPath for the Account"); } return path; } set { AccountError res = (AccountError)Interop.Account.SetAccountIconPath(_handle, value); if (res != AccountError.None) { throw AccountErrorFactory.CreateException(res, "Failed to Set IconPath for Account"); } } } /// /// Domain name of the account. /// /// 3 /// Domain name of the account. public string DomainName { get { string name = ""; AccountError res = (AccountError)Interop.Account.GetAccountDomainName(_handle, out name); if (res != AccountError.None) { Log.Warn(AccountErrorFactory.LogTag, "Failed to get DomainName for the Account"); } return name; } set { AccountError res = (AccountError)Interop.Account.SetAccountDomainName(_handle, value); if (res != AccountError.None) { throw AccountErrorFactory.CreateException(res, "Failed to Set DomainName for Account"); } } } /// /// Email ID of the account. /// /// 3 /// Email ID of the account. public string EmailId { get { string email = ""; AccountError res = (AccountError)Interop.Account.GetAccountEmail(_handle, out email); if (res != AccountError.None) { Log.Warn(AccountErrorFactory.LogTag, "Failed to get email for the Account"); } return email; } set { AccountError res = (AccountError)Interop.Account.SetAccountEmail(_handle, value); if (res != AccountError.None) { throw AccountErrorFactory.CreateException(res, "Failed to Set email for Account"); } } } /// /// Package name of the account. /// /// 3 /// Package name of the account. public string PackageName { get { string name = ""; AccountError res = (AccountError)Interop.Account.GetAccountPackageName(_handle, out name); if (res != AccountError.None) { Log.Warn(AccountErrorFactory.LogTag, "Failed to get PacakageName for the Account"); } return name; } set { AccountError res = (AccountError)Interop.Account.SetAccountPackageName(_handle, value); if (res != AccountError.None) { throw AccountErrorFactory.CreateException(res, "Failed to Set PacakageName for Account"); } } } /// /// Access token of the account. /// /// 3 /// Access token of the account. public string AccessToken { get { string token = ""; AccountError res = (AccountError)Interop.Account.GetAccountAccessToken(_handle, out token); if (res != AccountError.None) { Log.Warn(AccountErrorFactory.LogTag, "Failed to get token for the Account"); } return token; } set { AccountError res = (AccountError)Interop.Account.SetAccountAccessToken(_handle, value); if (res != AccountError.None) { throw AccountErrorFactory.CreateException(res, "Failed to Set token for Account"); } } } /// /// Authentication type of the account. /// /// 3 /// Authentication type of the account. public AccountAuthType AuthType { get { int user; AccountError res = (AccountError)Interop.Account.GetAccountAuthType(_handle, out user); if (res != AccountError.None) { Log.Warn(AccountErrorFactory.LogTag, "Failed to get AuthType for the Account"); } return (AccountAuthType)user; } set { AccountError res = (AccountError)Interop.Account.SetAccountAuthType(_handle, (int)value); if (res != AccountError.None) { throw AccountErrorFactory.CreateException(res, "Failed to Set AuthType for Account"); } } } /// /// Secrecy state of the account. /// /// 3 /// Secrecy state of the account. public AccountSecrecyState SecrecyState { get { int state; AccountError res = (AccountError)Interop.Account.GetAccountSercet(_handle, out state); if (res != AccountError.None) { Log.Warn(AccountErrorFactory.LogTag, "Failed to get User Secret for the Account"); } return (AccountSecrecyState)state; } set { AccountError res = (AccountError)Interop.Account.SetAccountSecret(_handle, (int)value); if (res != AccountError.None) { throw AccountErrorFactory.CreateException(res, "Failed to Set User Secret for Account"); } } } /// /// Sync state of the account. /// /// 3 /// Sync state of the account. public AccountSyncState SyncState { get { int supported; AccountError res = (AccountError)Interop.Account.GetAccountSyncSupport(_handle, out supported); if (res != AccountError.None) { Log.Warn(AccountErrorFactory.LogTag, "Failed to get AuthType for the Account"); } return (AccountSyncState)supported; } set { AccountError res = (AccountError)Interop.Account.SetAccountSyncSupport(_handle, (int)value); if (res != AccountError.None) { throw AccountErrorFactory.CreateException(res, "Failed to Set AuthType for Account"); } } } /// /// Source of the account. /// /// 3 /// Account source. public string Source { get { string text = ""; AccountError res = (AccountError)Interop.Account.GetAccountSource(_handle, out text); if (res != AccountError.None) { Log.Warn(AccountErrorFactory.LogTag, "Failed to get User Secret for the Account"); } return text; } set { AccountError res = (AccountError)Interop.Account.SetAccountSource(_handle, value); if (res != AccountError.None) { throw AccountErrorFactory.CreateException(res, "Failed to Set User Secret for Account"); } } } /// /// Handle of the account. /// /// 4 /// Account handle. public SafeAccountHandle SafeAccountHandle { get { return _handle; } } /// /// Sets the account capability. /// /// 3 /// The account capability type. /// The account capability state. /// In case of an invalid parameter. public void SetCapability(string capabilityType, CapabilityState state) { AccountError ret = (AccountError)Interop.Account.SetAccountCapability(_handle, capabilityType, (int)state); if (ret != AccountError.None) { throw AccountErrorFactory.CreateException(ret, "failed to set account capability"); } } /// /// Gets all the capabilities of the account. /// /// 3 /// The capability type to get the capability value. /// The capability value (on/off) of the specified CapabilityState. /// In case of an invalid parameter. public CapabilityState GetCapability(string capabilityType) { int type; AccountError res = (AccountError)Interop.Account.GetAccountCapability(_handle, capabilityType, out type); if (res != AccountError.None) { throw AccountErrorFactory.CreateException(res, "Failed to GetCapability for Account"); } return (CapabilityState)type; } /// /// Gets all the capabilities of the account. /// /// 3 /// List of capabilities as dictionary. public Dictionary GetAllCapabilities() { AccountError res = AccountError.None; Dictionary list = new Dictionary(); Interop.Account.AccountCapabilityCallback capabilityCallback = (string type, int state, IntPtr data) => { list.Add(type, (CapabilityState)state); return true; }; res = (AccountError)Interop.Account.GetAllAccountCapabilities(_handle, capabilityCallback, IntPtr.Zero); if (res != AccountError.None) { throw AccountErrorFactory.CreateException(res, "Failed to get account capabilities"); } return list; } /// /// Sets the custom value to the account. /// /// 3 /// Key to be added to the account. /// Value to be updated for respective key for the account. /// In case of an invalid parameter. public void SetCustomValue(string key, string value) { AccountError err = (AccountError)Interop.Account.SetAccountCustomValue(_handle, key, value); if (err != AccountError.None) { throw AccountErrorFactory.CreateException(err, "Failed to set the value for : " + key); } } /// /// Gets the user specific custom text of an account key. /// /// 3 /// The key to retrieve custom text. /// The text of the given key. /// In case of an invalid parameter. /// If there is no given capability type in the account. public string GetCustomValue(string key) { string result = ""; AccountError err = (AccountError)Interop.Account.GetAccountCustomValue(_handle, key, out result); if (err != AccountError.None) { throw AccountErrorFactory.CreateException(err, "Failed to get the value for : " + key); } return result; } /// /// Gets all the custom values. /// /// 3 /// List of custom key, value pairs as dictionary. public Dictionary GetAllCustomValues() { AccountError res = AccountError.None; Dictionary list = new Dictionary(); Interop.Account.AccountCustomCallback customCallback = (string key, string value, IntPtr data) => { list.Add(key, value); return true; }; res = (AccountError)Interop.Account.GetAllAccountCustomValues(_handle, customCallback, IntPtr.Zero); if (res != AccountError.None) { throw AccountErrorFactory.CreateException(res, "Failed to get account capabilities"); } return list; } /// /// Sets the user text. /// /// 3 /// The index of the user text (must be in range from 0 to 4). /// The text string to set as the user text. /// In case of an invalid parameter. public void SetUserText(int index, string text) { AccountError err = (AccountError)Interop.Account.SetAccountUserText(_handle, index, text); if (err != AccountError.None) { throw AccountErrorFactory.CreateException(err, "Failed to get the value for : " + index); } } /// /// Gets the user text. /// /// 3 /// The index of the user text (must be in range from 0 to 4). /// The user text of the given key. /// In case of an invalid parameter. /// In case of out of memory. public string GetUserText(int index) { string result = ""; AccountError err = (AccountError)Interop.Account.GetAccountUserText(_handle, index, out result); if (err != AccountError.None) { throw AccountErrorFactory.CreateException(err, "Failed to get the value for : " + index); } return result; } /// /// Gets the user integer value. /// /// 3 /// The index of the user integer (must be in range from 0 to 4). /// The user integer of the given key. /// In case of an invalid parameter. public int GetUserInt(int index) { int result = -1; AccountError err = (AccountError)Interop.Account.GetAccountUserInt(_handle, index, out result); if (err != AccountError.None) { throw AccountErrorFactory.CreateException(err, "Failed to get the value for : " + index); } return result; } /// /// Sets the user integer value. /// /// 3 /// The index of the user integer (must be in range from 0 to 4). /// The integer to set as the user integer. /// In case of an invalid parameter. public void SetUserInt(int index, int value) { AccountError err = (AccountError)Interop.Account.SetAccountUserInt(_handle, index, value); if (err != AccountError.None) { throw AccountErrorFactory.CreateException(err, "Failed to get the value for : " + index); } } /// /// Overloaded Dispose API for destroying the account handle. /// /// 3 public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } /// /// Dispose API for destroying the account handle. /// /// 3 /// The boolean value for destoying account handle. protected virtual void Dispose(bool disposing) { if (!disposing) { _handle.Dispose(); } } } }