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)
46 /// Creates a new account instance.
48 /// <since_tizen> 3 </since_tizen>
49 /// <returns>Account Instance.</returns>
50 public static Account CreateAccount()
52 SafeAccountHandle handle;
53 AccountError err = (AccountError)Interop.Account.Create(out handle);
54 if (err != AccountError.None)
56 throw AccountErrorFactory.CreateException(err, "Failed to create new error.");
59 return new Account(handle);
63 /// Id of the Account.
65 /// <since_tizen> 3 </since_tizen>
66 /// <remarks>Account ID shall be created only when the account is added to the database.</remarks>
72 AccountError res = (AccountError)Interop.Account.GetAccountId(_handle, out id);
73 if (res != AccountError.None)
75 Log.Warn(AccountErrorFactory.LogTag, "Failed to get Id for the Account");
83 /// UserName of the account.
85 /// <since_tizen> 3 </since_tizen>
86 /// <value>User name of the account.</value>
87 public string UserName
92 AccountError res = (AccountError)Interop.Account.GetAccountUserName(_handle, out name);
93 if (res != AccountError.None)
95 Log.Warn(AccountErrorFactory.LogTag, "Failed to get UserName for the Account");
103 AccountError res = (AccountError)Interop.Account.SetAccountUserName(_handle, value);
104 if (res != AccountError.None)
106 throw AccountErrorFactory.CreateException(res, "Failed to Set UserName for Account");
112 /// Display name of the account.
114 /// <since_tizen> 3 </since_tizen>
115 /// <value>Display name of the account.</value>
116 public string DisplayName
121 AccountError res = (AccountError)Interop.Account.GetAccountDisplayName(_handle, out name);
122 if (res != AccountError.None)
124 Log.Warn(AccountErrorFactory.LogTag, "Failed to get DisplayName for the Account");
132 AccountError res = (AccountError)Interop.Account.SetAccountDisplayName(_handle, value);
133 if (res != AccountError.None)
135 throw AccountErrorFactory.CreateException(res, "Failed to Set DisplayName for Account");
141 /// Icon path of the account.
143 /// <since_tizen> 3 </since_tizen>
144 /// <value>Icon path of the account.</value>
145 public string IconPath
150 AccountError res = (AccountError)Interop.Account.GetAccountIconPath(_handle, out path);
151 if (res != AccountError.None)
153 Log.Warn(AccountErrorFactory.LogTag, "Failed to get IconPath for the Account");
161 AccountError res = (AccountError)Interop.Account.SetAccountIconPath(_handle, value);
162 if (res != AccountError.None)
164 throw AccountErrorFactory.CreateException(res, "Failed to Set IconPath for Account");
170 /// Domain name of the account.
172 /// <since_tizen> 3 </since_tizen>
173 /// <value>Domain name of the account.</value>
174 public string DomainName
179 AccountError res = (AccountError)Interop.Account.GetAccountDomainName(_handle, out name);
180 if (res != AccountError.None)
182 Log.Warn(AccountErrorFactory.LogTag, "Failed to get DomainName for the Account");
190 AccountError res = (AccountError)Interop.Account.SetAccountDomainName(_handle, value);
191 if (res != AccountError.None)
193 throw AccountErrorFactory.CreateException(res, "Failed to Set DomainName for Account");
199 /// Email ID of the account.
201 /// <since_tizen> 3 </since_tizen>
202 /// <value>Email ID of the account.</value>
203 public string EmailId
208 AccountError res = (AccountError)Interop.Account.GetAccountEmail(_handle, out email);
209 if (res != AccountError.None)
211 Log.Warn(AccountErrorFactory.LogTag, "Failed to get email for the Account");
219 AccountError res = (AccountError)Interop.Account.SetAccountEmail(_handle, value);
220 if (res != AccountError.None)
222 throw AccountErrorFactory.CreateException(res, "Failed to Set email for Account");
228 /// Package name of the account.
230 /// <since_tizen> 3 </since_tizen>
231 /// <value>Package name of the account.</value>
232 public string PackageName
237 AccountError res = (AccountError)Interop.Account.GetAccountPackageName(_handle, out name);
238 if (res != AccountError.None)
240 Log.Warn(AccountErrorFactory.LogTag, "Failed to get PacakageName for the Account");
248 AccountError res = (AccountError)Interop.Account.SetAccountPackageName(_handle, value);
249 if (res != AccountError.None)
251 throw AccountErrorFactory.CreateException(res, "Failed to Set PacakageName for Account");
257 /// Access token of the account.
259 /// <since_tizen> 3 </since_tizen>
260 /// <value>Access token of the account.</value>
261 public string AccessToken
266 AccountError res = (AccountError)Interop.Account.GetAccountAccessToken(_handle, out token);
267 if (res != AccountError.None)
269 Log.Warn(AccountErrorFactory.LogTag, "Failed to get token for the Account");
277 AccountError res = (AccountError)Interop.Account.SetAccountAccessToken(_handle, value);
278 if (res != AccountError.None)
280 throw AccountErrorFactory.CreateException(res, "Failed to Set token for Account");
286 /// Authentication type of the account.
288 /// <since_tizen> 3 </since_tizen>
289 /// <value>Authentication type of the account.</value>
290 public AccountAuthType AuthType
295 AccountError res = (AccountError)Interop.Account.GetAccountAuthType(_handle, out user);
296 if (res != AccountError.None)
298 Log.Warn(AccountErrorFactory.LogTag, "Failed to get AuthType for the Account");
301 return (AccountAuthType)user;
306 AccountError res = (AccountError)Interop.Account.SetAccountAuthType(_handle, (int)value);
307 if (res != AccountError.None)
309 throw AccountErrorFactory.CreateException(res, "Failed to Set AuthType for Account");
315 /// Secrecy state of the account.
317 /// <since_tizen> 3 </since_tizen>
318 /// <value>Secrecy state of the account.</value>
319 public AccountSecrecyState SecrecyState
324 AccountError res = (AccountError)Interop.Account.GetAccountSercet(_handle, out state);
325 if (res != AccountError.None)
327 Log.Warn(AccountErrorFactory.LogTag, "Failed to get User Secret for the Account");
330 return (AccountSecrecyState)state;
335 AccountError res = (AccountError)Interop.Account.SetAccountSecret(_handle, (int)value);
336 if (res != AccountError.None)
338 throw AccountErrorFactory.CreateException(res, "Failed to Set User Secret for Account");
344 /// Sync state of the account.
346 /// <since_tizen> 3 </since_tizen>
347 /// <value>Sync state of the account.</value>
348 public AccountSyncState SyncState
353 AccountError res = (AccountError)Interop.Account.GetAccountSyncSupport(_handle, out supported);
354 if (res != AccountError.None)
356 Log.Warn(AccountErrorFactory.LogTag, "Failed to get AuthType for the Account");
359 return (AccountSyncState)supported;
364 AccountError res = (AccountError)Interop.Account.SetAccountSyncSupport(_handle, (int)value);
365 if (res != AccountError.None)
367 throw AccountErrorFactory.CreateException(res, "Failed to Set AuthType for Account");
373 /// Source of the account.
375 /// <since_tizen> 3 </since_tizen>
376 /// <value>Account source.</value>
382 AccountError res = (AccountError)Interop.Account.GetAccountSource(_handle, out text);
383 if (res != AccountError.None)
385 Log.Warn(AccountErrorFactory.LogTag, "Failed to get User Secret for the Account");
393 AccountError res = (AccountError)Interop.Account.SetAccountSource(_handle, value);
394 if (res != AccountError.None)
396 throw AccountErrorFactory.CreateException(res, "Failed to Set User Secret for Account");
402 /// Handle of the account.
404 /// <since_tizen> 4 </since_tizen>
405 /// <value>Account handle.</value>
406 public SafeAccountHandle SafeAccountHandle
414 /// Sets the account capability.
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)
422 AccountError ret = (AccountError)Interop.Account.SetAccountCapability(_handle, capabilityType, (int)state);
423 if (ret != AccountError.None)
425 throw AccountErrorFactory.CreateException(ret, "failed to set account capability");
429 /// Gets all the capabilities of the account.
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)
438 AccountError res = (AccountError)Interop.Account.GetAccountCapability(_handle, capabilityType, out type);
439 if (res != AccountError.None)
441 throw AccountErrorFactory.CreateException(res, "Failed to GetCapability for Account");
444 return (CapabilityState)type;
448 /// Gets all the capabilities of the account.
450 /// <since_tizen> 3 </since_tizen>
451 /// <returns>List of capabilities as dictionary.</returns>
452 public Dictionary<string, CapabilityState> GetAllCapabilities()
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) =>
459 list.Add(type, (CapabilityState)state);
463 res = (AccountError)Interop.Account.GetAllAccountCapabilities(_handle, capabilityCallback, IntPtr.Zero);
464 if (res != AccountError.None)
466 throw AccountErrorFactory.CreateException(res, "Failed to get account capabilities");
473 /// Sets the custom value to the account.
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)
481 AccountError err = (AccountError)Interop.Account.SetAccountCustomValue(_handle, key, value);
482 if (err != AccountError.None)
484 throw AccountErrorFactory.CreateException(err, "Failed to set the value for : " + key);
489 /// Gets the user specific custom text of an account key.
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)
499 AccountError err = (AccountError)Interop.Account.GetAccountCustomValue(_handle, key, out result);
500 if (err != AccountError.None)
502 throw AccountErrorFactory.CreateException(err, "Failed to get the value for : " + key);
509 /// Gets all the custom values.
511 /// <since_tizen> 3 </since_tizen>
512 /// <returns>List of custom key, value pairs as dictionary.</returns>
513 public Dictionary<string, string> GetAllCustomValues()
515 AccountError res = AccountError.None;
516 Dictionary<string, string> list = new Dictionary<string, string>();
518 Interop.Account.AccountCustomCallback customCallback = (string key, string value, IntPtr data) =>
520 list.Add(key, value);
524 res = (AccountError)Interop.Account.GetAllAccountCustomValues(_handle, customCallback, IntPtr.Zero);
526 if (res != AccountError.None)
528 throw AccountErrorFactory.CreateException(res, "Failed to get account capabilities");
535 /// Sets the user text.
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)
543 AccountError err = (AccountError)Interop.Account.SetAccountUserText(_handle, index, text);
544 if (err != AccountError.None)
546 throw AccountErrorFactory.CreateException(err, "Failed to get the value for : " + index);
551 /// Gets the user text.
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)
561 AccountError err = (AccountError)Interop.Account.GetAccountUserText(_handle, index, out result);
562 if (err != AccountError.None)
564 throw AccountErrorFactory.CreateException(err, "Failed to get the value for : " + index);
571 /// Gets the user integer value.
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)
580 AccountError err = (AccountError)Interop.Account.GetAccountUserInt(_handle, index, out result);
581 if (err != AccountError.None)
583 throw AccountErrorFactory.CreateException(err, "Failed to get the value for : " + index);
590 /// Sets the user integer value.
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)
598 AccountError err = (AccountError)Interop.Account.SetAccountUserInt(_handle, index, value);
599 if (err != AccountError.None)
601 throw AccountErrorFactory.CreateException(err, "Failed to get the value for : " + index);
606 /// Overloaded Dispose API for destroying the account handle.
608 /// <since_tizen> 3 </since_tizen>
609 public void Dispose()
612 GC.SuppressFinalize(this);
615 protected virtual void Dispose(bool disposing)