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 IntPtr _handle = IntPtr.Zero;
29 internal Account(IntPtr handle)
39 /// Creates a new Account instance.
41 /// <since_tizen> 3 </since_tizen>
42 /// <returns>Account Instance.</returns>
43 public static Account CreateAccount()
46 AccountError err = (AccountError)Interop.Account.Create(out handle);
47 if (err != AccountError.None)
49 throw AccountErrorFactory.CreateException(err, "Failed to create new error.");
52 return new Account(handle);
56 /// Id of the Account.
58 /// <since_tizen> 3 </since_tizen>
59 /// <remarks>Account Id shall be created only when account is added to the database.</remarks>
65 AccountError res = (AccountError)Interop.Account.GetAccountId(_handle, out id);
66 if (res != AccountError.None)
68 Log.Warn(AccountErrorFactory.LogTag, "Failed to get Id for the Account");
76 /// UserName of the Account.
78 /// <since_tizen> 3 </since_tizen>
79 /// <value>User Name of the Account.</value>
80 public string UserName
85 AccountError res = (AccountError)Interop.Account.GetAccountUserName(_handle, out name);
86 if (res != AccountError.None)
88 Log.Warn(AccountErrorFactory.LogTag, "Failed to get UserName for the Account");
96 AccountError res = (AccountError)Interop.Account.SetAccountUserName(_handle, value);
97 if (res != AccountError.None)
99 throw AccountErrorFactory.CreateException(res, "Failed to Set UserName for Account");
105 /// Display Name of the Account.
107 /// <since_tizen> 3 </since_tizen>
108 /// <value>DisplayName of the Account.</value>
109 public string DisplayName
114 AccountError res = (AccountError)Interop.Account.GetAccountDisplayName(_handle, out name);
115 if (res != AccountError.None)
117 Log.Warn(AccountErrorFactory.LogTag, "Failed to get DisplayName for the Account");
125 AccountError res = (AccountError)Interop.Account.SetAccountDisplayName(_handle, value);
126 if (res != AccountError.None)
128 throw AccountErrorFactory.CreateException(res, "Failed to Set DisplayName for Account");
134 /// Icon path of the Account.
136 /// <since_tizen> 3 </since_tizen>
137 /// <value>Icon path of the Account.</value>
138 public string IconPath
143 AccountError res = (AccountError)Interop.Account.GetAccountIconPath(_handle, out path);
144 if (res != AccountError.None)
146 Log.Warn(AccountErrorFactory.LogTag, "Failed to get IconPath for the Account");
154 AccountError res = (AccountError)Interop.Account.SetAccountIconPath(_handle, value);
155 if (res != AccountError.None)
157 throw AccountErrorFactory.CreateException(res, "Failed to Set IconPath for Account");
163 /// Domain name of the Account.
165 /// <since_tizen> 3 </since_tizen>
166 /// <value>Domain name of the Account.</value>
167 public string DomainName
172 AccountError res = (AccountError)Interop.Account.GetAccountDomainName(_handle, out name);
173 if (res != AccountError.None)
175 Log.Warn(AccountErrorFactory.LogTag, "Failed to get DomainName for the Account");
183 AccountError res = (AccountError)Interop.Account.SetAccountDomainName(_handle, value);
184 if (res != AccountError.None)
186 throw AccountErrorFactory.CreateException(res, "Failed to Set DomainName for Account");
192 /// Email Id of the Account.
194 /// <since_tizen> 3 </since_tizen>
195 /// <value>Email Id of the Account.</value>
196 public string EmailId
201 AccountError res = (AccountError)Interop.Account.GetAccountEmail(_handle, out email);
202 if (res != AccountError.None)
204 Log.Warn(AccountErrorFactory.LogTag, "Failed to get email for the Account");
212 AccountError res = (AccountError)Interop.Account.SetAccountEmail(_handle, value);
213 if (res != AccountError.None)
215 throw AccountErrorFactory.CreateException(res, "Failed to Set email for Account");
221 /// Package Name of the Account.
223 /// <since_tizen> 3 </since_tizen>
224 /// <value>Package Name.</value>
225 public string PackageName
230 AccountError res = (AccountError)Interop.Account.GetAccountPackageName(_handle, out name);
231 if (res != AccountError.None)
233 Log.Warn(AccountErrorFactory.LogTag, "Failed to get PacakageName for the Account");
241 AccountError res = (AccountError)Interop.Account.SetAccountPackageName(_handle, value);
242 if (res != AccountError.None)
244 throw AccountErrorFactory.CreateException(res, "Failed to Set PacakageName for Account");
250 /// Access Token of the Account.
252 /// <since_tizen> 3 </since_tizen>
253 /// <value>Access Token.</value>
254 public string AccessToken
259 AccountError res = (AccountError)Interop.Account.GetAccountAccessToken(_handle, out token);
260 if (res != AccountError.None)
262 Log.Warn(AccountErrorFactory.LogTag, "Failed to get token for the Account");
270 AccountError res = (AccountError)Interop.Account.SetAccountAccessToken(_handle, value);
271 if (res != AccountError.None)
273 throw AccountErrorFactory.CreateException(res, "Failed to Set token for Account");
279 /// Authentication type of the Account.
281 /// <since_tizen> 3 </since_tizen>
282 /// <value>Authentication type.</value>
283 public AccountAuthType AuthType
288 AccountError res = (AccountError)Interop.Account.GetAccountAuthType(_handle, out user);
289 if (res != AccountError.None)
291 Log.Warn(AccountErrorFactory.LogTag, "Failed to get AuthType for the Account");
294 return (AccountAuthType)user;
299 AccountError res = (AccountError)Interop.Account.SetAccountAuthType(_handle, (int)value);
300 if (res != AccountError.None)
302 throw AccountErrorFactory.CreateException(res, "Failed to Set AuthType for Account");
308 /// Secrecy State of the Account.
310 /// <since_tizen> 3 </since_tizen>
311 /// <value>Secrecy State.</value>
312 public AccountSecrecyState SecrecyState
317 AccountError res = (AccountError)Interop.Account.GetAccountSercet(_handle, out state);
318 if (res != AccountError.None)
320 Log.Warn(AccountErrorFactory.LogTag, "Failed to get User Secret for the Account");
323 return (AccountSecrecyState)state;
328 AccountError res = (AccountError)Interop.Account.SetAccountSecret(_handle, (int)value);
329 if (res != AccountError.None)
331 throw AccountErrorFactory.CreateException(res, "Failed to Set User Secret for Account");
337 /// Sync State of the Account.
339 /// <since_tizen> 3 </since_tizen>
340 /// <value>Sync State.</value>
341 public AccountSyncState SyncState
346 AccountError res = (AccountError)Interop.Account.GetAccountSyncSupport(_handle, out supported);
347 if (res != AccountError.None)
349 Log.Warn(AccountErrorFactory.LogTag, "Failed to get AuthType for the Account");
352 return (AccountSyncState)supported;
357 AccountError res = (AccountError)Interop.Account.SetAccountSyncSupport(_handle, (int)value);
358 if (res != AccountError.None)
360 throw AccountErrorFactory.CreateException(res, "Failed to Set AuthType for Account");
366 /// Source of the Account .
368 /// <since_tizen> 3 </since_tizen>
369 /// <value>Account Source.</value>
375 AccountError res = (AccountError)Interop.Account.GetAccountSource(_handle, out text);
376 if (res != AccountError.None)
378 Log.Warn(AccountErrorFactory.LogTag, "Failed to get User Secret for the Account");
386 AccountError res = (AccountError)Interop.Account.SetAccountSource(_handle, value);
387 if (res != AccountError.None)
389 throw AccountErrorFactory.CreateException(res, "Failed to Set User Secret for Account");
394 internal IntPtr Handle
407 /// Sets the account capability.
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)
415 AccountError ret = (AccountError)Interop.Account.SetAccountCapability(_handle, capabilityType, (int)state);
416 if (ret != AccountError.None)
418 throw AccountErrorFactory.CreateException(ret, "failed to set account capability");
422 /// Gets all the capabilities of an account.
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)
431 AccountError res = (AccountError)Interop.Account.GetAccountCapability(_handle, capabilityType, out type);
432 if (res != AccountError.None)
434 throw AccountErrorFactory.CreateException(res, "Failed to GetCapability for Account");
437 return (CapabilityState)type;
441 /// Gets all the capabilities of an account.
443 /// <since_tizen> 3 </since_tizen>
444 /// <returns>List of Cpabailities as Dictionary</returns>
445 public Dictionary<string, CapabilityState> GetAllCapabilities()
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) =>
452 list.Add(type, (CapabilityState)state);
456 res = (AccountError)Interop.Account.GetAllAccountCapabilities(_handle, capabilityCallback, IntPtr.Zero);
457 if (res != AccountError.None)
459 throw AccountErrorFactory.CreateException(res, "Failed to get account capabilities");
466 /// Sets the Custom Value to the Account.
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)
474 AccountError err = (AccountError)Interop.Account.SetAccountCustomValue(_handle, key, value);
475 if (err != AccountError.None)
477 throw AccountErrorFactory.CreateException(err, "Failed to set the value for : " + key);
482 /// Gets the user specific custom text of an account key.
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)
492 AccountError err = (AccountError)Interop.Account.GetAccountCustomValue(_handle, key, out result);
493 if (err != AccountError.None)
495 throw AccountErrorFactory.CreateException(err, "Failed to get the value for : " + key);
502 /// Gets All the custome values.
504 /// <since_tizen> 3 </since_tizen>
505 /// <returns>List of custom key, value pairs as Dictionary.</returns>
506 public Dictionary<string, string> GetAllCustomValues()
508 AccountError res = AccountError.None;
509 Dictionary<string, string> list = new Dictionary<string, string>();
511 Interop.Account.AccountCustomCallback customCallback = (string key, string value, IntPtr data) =>
513 list.Add(key, value);
517 res = (AccountError)Interop.Account.GetAllAccountCustomValues(_handle, customCallback, IntPtr.Zero);
519 if (res != AccountError.None)
521 throw AccountErrorFactory.CreateException(res, "Failed to get account capabilities");
528 /// Sets the user text.
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)
536 AccountError err = (AccountError)Interop.Account.SetAccountUserText(_handle, index, text);
537 if (err != AccountError.None)
539 throw AccountErrorFactory.CreateException(err, "Failed to get the value for : " + index);
544 /// Gets the user text.
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)
554 AccountError err = (AccountError)Interop.Account.GetAccountUserText(_handle, index, out result);
555 if (err != AccountError.None)
557 throw AccountErrorFactory.CreateException(err, "Failed to get the value for : " + index);
564 /// Gets the user int value.
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)
573 AccountError err = (AccountError)Interop.Account.GetAccountUserInt(_handle, index, out result);
574 if (err != AccountError.None)
576 throw AccountErrorFactory.CreateException(err, "Failed to get the value for : " + index);
583 /// Sets the user integer value.
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)
591 AccountError err = (AccountError)Interop.Account.SetAccountUserInt(_handle, index, value);
592 if (err != AccountError.None)
594 throw AccountErrorFactory.CreateException(err, "Failed to get the value for : " + index);
599 /// Overloaded Dispose API for destroying the Account Handle.
601 /// <since_tizen> 3 </since_tizen>
602 public void Dispose()
605 GC.SuppressFinalize(this);
608 private void Dispose(bool disposing)
612 if (_handle != IntPtr.Zero)
614 _handle = IntPtr.Zero;