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;
30 public Account(SafeAccountHandle handle)
41 /// Creates a new account instance.
43 /// <since_tizen> 3 </since_tizen>
44 /// <returns>Account Instance.</returns>
45 public static Account CreateAccount()
47 SafeAccountHandle handle;
48 AccountError err = (AccountError)Interop.Account.Create(out handle);
49 if (err != AccountError.None)
51 throw AccountErrorFactory.CreateException(err, "Failed to create new error.");
54 return new Account(handle);
58 /// Id of the Account.
60 /// <since_tizen> 3 </since_tizen>
61 /// <remarks>Account ID shall be created only when the account is added to the database.</remarks>
67 AccountError res = (AccountError)Interop.Account.GetAccountId(_handle, out id);
68 if (res != AccountError.None)
70 Log.Warn(AccountErrorFactory.LogTag, "Failed to get Id for the Account");
78 /// UserName of the account.
80 /// <since_tizen> 3 </since_tizen>
81 /// <value>User name of the account.</value>
82 public string UserName
87 AccountError res = (AccountError)Interop.Account.GetAccountUserName(_handle, out name);
88 if (res != AccountError.None)
90 Log.Warn(AccountErrorFactory.LogTag, "Failed to get UserName for the Account");
98 AccountError res = (AccountError)Interop.Account.SetAccountUserName(_handle, value);
99 if (res != AccountError.None)
101 throw AccountErrorFactory.CreateException(res, "Failed to Set UserName for Account");
107 /// Display name of the account.
109 /// <since_tizen> 3 </since_tizen>
110 /// <value>Display name of the account.</value>
111 public string DisplayName
116 AccountError res = (AccountError)Interop.Account.GetAccountDisplayName(_handle, out name);
117 if (res != AccountError.None)
119 Log.Warn(AccountErrorFactory.LogTag, "Failed to get DisplayName for the Account");
127 AccountError res = (AccountError)Interop.Account.SetAccountDisplayName(_handle, value);
128 if (res != AccountError.None)
130 throw AccountErrorFactory.CreateException(res, "Failed to Set DisplayName for Account");
136 /// Icon path of the account.
138 /// <since_tizen> 3 </since_tizen>
139 /// <value>Icon path of the account.</value>
140 public string IconPath
145 AccountError res = (AccountError)Interop.Account.GetAccountIconPath(_handle, out path);
146 if (res != AccountError.None)
148 Log.Warn(AccountErrorFactory.LogTag, "Failed to get IconPath for the Account");
156 AccountError res = (AccountError)Interop.Account.SetAccountIconPath(_handle, value);
157 if (res != AccountError.None)
159 throw AccountErrorFactory.CreateException(res, "Failed to Set IconPath for Account");
165 /// Domain name of the account.
167 /// <since_tizen> 3 </since_tizen>
168 /// <value>Domain name of the account.</value>
169 public string DomainName
174 AccountError res = (AccountError)Interop.Account.GetAccountDomainName(_handle, out name);
175 if (res != AccountError.None)
177 Log.Warn(AccountErrorFactory.LogTag, "Failed to get DomainName for the Account");
185 AccountError res = (AccountError)Interop.Account.SetAccountDomainName(_handle, value);
186 if (res != AccountError.None)
188 throw AccountErrorFactory.CreateException(res, "Failed to Set DomainName for Account");
194 /// Email ID of the account.
196 /// <since_tizen> 3 </since_tizen>
197 /// <value>Email ID of the account.</value>
198 public string EmailId
203 AccountError res = (AccountError)Interop.Account.GetAccountEmail(_handle, out email);
204 if (res != AccountError.None)
206 Log.Warn(AccountErrorFactory.LogTag, "Failed to get email for the Account");
214 AccountError res = (AccountError)Interop.Account.SetAccountEmail(_handle, value);
215 if (res != AccountError.None)
217 throw AccountErrorFactory.CreateException(res, "Failed to Set email for Account");
223 /// Package name of the account.
225 /// <since_tizen> 3 </since_tizen>
226 /// <value>Package name of the account.</value>
227 public string PackageName
232 AccountError res = (AccountError)Interop.Account.GetAccountPackageName(_handle, out name);
233 if (res != AccountError.None)
235 Log.Warn(AccountErrorFactory.LogTag, "Failed to get PacakageName for the Account");
243 AccountError res = (AccountError)Interop.Account.SetAccountPackageName(_handle, value);
244 if (res != AccountError.None)
246 throw AccountErrorFactory.CreateException(res, "Failed to Set PacakageName for Account");
252 /// Access token of the account.
254 /// <since_tizen> 3 </since_tizen>
255 /// <value>Access token of the account.</value>
256 public string AccessToken
261 AccountError res = (AccountError)Interop.Account.GetAccountAccessToken(_handle, out token);
262 if (res != AccountError.None)
264 Log.Warn(AccountErrorFactory.LogTag, "Failed to get token for the Account");
272 AccountError res = (AccountError)Interop.Account.SetAccountAccessToken(_handle, value);
273 if (res != AccountError.None)
275 throw AccountErrorFactory.CreateException(res, "Failed to Set token for Account");
281 /// Authentication type of the account.
283 /// <since_tizen> 3 </since_tizen>
284 /// <value>Authentication type of the account.</value>
285 public AccountAuthType AuthType
290 AccountError res = (AccountError)Interop.Account.GetAccountAuthType(_handle, out user);
291 if (res != AccountError.None)
293 Log.Warn(AccountErrorFactory.LogTag, "Failed to get AuthType for the Account");
296 return (AccountAuthType)user;
301 AccountError res = (AccountError)Interop.Account.SetAccountAuthType(_handle, (int)value);
302 if (res != AccountError.None)
304 throw AccountErrorFactory.CreateException(res, "Failed to Set AuthType for Account");
310 /// Secrecy state of the account.
312 /// <since_tizen> 3 </since_tizen>
313 /// <value>Secrecy state of the account.</value>
314 public AccountSecrecyState SecrecyState
319 AccountError res = (AccountError)Interop.Account.GetAccountSercet(_handle, out state);
320 if (res != AccountError.None)
322 Log.Warn(AccountErrorFactory.LogTag, "Failed to get User Secret for the Account");
325 return (AccountSecrecyState)state;
330 AccountError res = (AccountError)Interop.Account.SetAccountSecret(_handle, (int)value);
331 if (res != AccountError.None)
333 throw AccountErrorFactory.CreateException(res, "Failed to Set User Secret for Account");
339 /// Sync state of the account.
341 /// <since_tizen> 3 </since_tizen>
342 /// <value>Sync state of the account.</value>
343 public AccountSyncState SyncState
348 AccountError res = (AccountError)Interop.Account.GetAccountSyncSupport(_handle, out supported);
349 if (res != AccountError.None)
351 Log.Warn(AccountErrorFactory.LogTag, "Failed to get AuthType for the Account");
354 return (AccountSyncState)supported;
359 AccountError res = (AccountError)Interop.Account.SetAccountSyncSupport(_handle, (int)value);
360 if (res != AccountError.None)
362 throw AccountErrorFactory.CreateException(res, "Failed to Set AuthType for Account");
368 /// Source of the account.
370 /// <since_tizen> 3 </since_tizen>
371 /// <value>Account source.</value>
377 AccountError res = (AccountError)Interop.Account.GetAccountSource(_handle, out text);
378 if (res != AccountError.None)
380 Log.Warn(AccountErrorFactory.LogTag, "Failed to get User Secret for the Account");
388 AccountError res = (AccountError)Interop.Account.SetAccountSource(_handle, value);
389 if (res != AccountError.None)
391 throw AccountErrorFactory.CreateException(res, "Failed to Set User Secret for Account");
397 /// Handle of the account.
399 /// <since_tizen> 4 </since_tizen>
400 /// <value>Account handle.</value>
401 public SafeAccountHandle SafeAccountHandle
409 /// Sets the account capability.
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)
417 AccountError ret = (AccountError)Interop.Account.SetAccountCapability(_handle, capabilityType, (int)state);
418 if (ret != AccountError.None)
420 throw AccountErrorFactory.CreateException(ret, "failed to set account capability");
424 /// Gets all the capabilities of the account.
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)
433 AccountError res = (AccountError)Interop.Account.GetAccountCapability(_handle, capabilityType, out type);
434 if (res != AccountError.None)
436 throw AccountErrorFactory.CreateException(res, "Failed to GetCapability for Account");
439 return (CapabilityState)type;
443 /// Gets all the capabilities of the account.
445 /// <since_tizen> 3 </since_tizen>
446 /// <returns>List of capabilities as dictionary.</returns>
447 public Dictionary<string, CapabilityState> GetAllCapabilities()
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) =>
454 list.Add(type, (CapabilityState)state);
458 res = (AccountError)Interop.Account.GetAllAccountCapabilities(_handle, capabilityCallback, IntPtr.Zero);
459 if (res != AccountError.None)
461 throw AccountErrorFactory.CreateException(res, "Failed to get account capabilities");
468 /// Sets the custom value to the account.
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)
476 AccountError err = (AccountError)Interop.Account.SetAccountCustomValue(_handle, key, value);
477 if (err != AccountError.None)
479 throw AccountErrorFactory.CreateException(err, "Failed to set the value for : " + key);
484 /// Gets the user specific custom text of an account key.
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)
494 AccountError err = (AccountError)Interop.Account.GetAccountCustomValue(_handle, key, out result);
495 if (err != AccountError.None)
497 throw AccountErrorFactory.CreateException(err, "Failed to get the value for : " + key);
504 /// Gets all the custom values.
506 /// <since_tizen> 3 </since_tizen>
507 /// <returns>List of custom key, value pairs as dictionary.</returns>
508 public Dictionary<string, string> GetAllCustomValues()
510 AccountError res = AccountError.None;
511 Dictionary<string, string> list = new Dictionary<string, string>();
513 Interop.Account.AccountCustomCallback customCallback = (string key, string value, IntPtr data) =>
515 list.Add(key, value);
519 res = (AccountError)Interop.Account.GetAllAccountCustomValues(_handle, customCallback, IntPtr.Zero);
521 if (res != AccountError.None)
523 throw AccountErrorFactory.CreateException(res, "Failed to get account capabilities");
530 /// Sets the user text.
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)
538 AccountError err = (AccountError)Interop.Account.SetAccountUserText(_handle, index, text);
539 if (err != AccountError.None)
541 throw AccountErrorFactory.CreateException(err, "Failed to get the value for : " + index);
546 /// Gets the user text.
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)
556 AccountError err = (AccountError)Interop.Account.GetAccountUserText(_handle, index, out result);
557 if (err != AccountError.None)
559 throw AccountErrorFactory.CreateException(err, "Failed to get the value for : " + index);
566 /// Gets the user integer value.
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)
575 AccountError err = (AccountError)Interop.Account.GetAccountUserInt(_handle, index, out result);
576 if (err != AccountError.None)
578 throw AccountErrorFactory.CreateException(err, "Failed to get the value for : " + index);
585 /// Sets the user integer value.
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)
593 AccountError err = (AccountError)Interop.Account.SetAccountUserInt(_handle, index, value);
594 if (err != AccountError.None)
596 throw AccountErrorFactory.CreateException(err, "Failed to get the value for : " + index);
601 /// Overloaded Dispose API for destroying the account handle.
603 /// <since_tizen> 3 </since_tizen>
604 public void Dispose()
607 GC.SuppressFinalize(this);
610 private void Dispose(bool disposing)
614 //if (_handle != IntPtr.Zero)
616 // _handle = IntPtr.Zero;
619 GC.SuppressFinalize(this);