private void Register(AccountWrapper account)
 {
     // Register the new account
     _accountsBySmtp.Add(account.SmtpAddress, account);
     _accountsByStoreId.Add(account.StoreID, account);
     Logger.Instance.Trace(this, "Account registered: {0} -> {1}", account.DisplayName, account.StoreID);
 }
        /// <summary>
        /// Performs the actions required to handle a new store.
        /// </summary>
        /// <param name="rawStore">The new store. Ownership is transferred</param>
        private void StoreAdded(NSOutlook.Store rawStore)
        {
            IStore store = new StoreWrapper(rawStore);

            try
            {
                Logger.Instance.Trace(this, "New store: {0}", rawStore.DisplayName);
                AccountWrapper account = TryCreateFromRegistry(store);
                if (account == null)
                {
                    // Add it to the cache so it is not evaluated again.
                    _accountsByStoreId.Add(store.StoreID, null);
                    Logger.Instance.Trace(this, "Not an account store: {0}", store.DisplayName);
                }
                else
                {
                    Logger.Instance.Trace(this, "New account store: {0}: {1}", store.DisplayName, account);
                    // Account has taken ownership of the store
                    store = null;
                    OnAccountDiscovered(account);
                }
            }
            catch (System.Exception e)
            {
                Logger.Instance.Error(this, "Event_StoreAdded Exception: {0}", e);
            }
            finally
            {
                store?.Dispose();
            }
        }
 /// <summary>
 /// Creates the AccountWrapper for the store, from the registry.
 /// </summary>
 /// <param name="store">The store. Ownership is transferred to the AccountWrapper. If the account is not created, the store is NOT disposed</param>
 /// <returns>The AccountWrapper, or null if no account is associated with the store</returns>
 private AccountWrapper TryCreateFromRegistry(IStore store)
 {
     using (RegistryKey baseKey = FindRegistryKey(store))
     {
         if (baseKey == null)
         {
             return(null);
         }
         AccountWrapper account = new AccountWrapper(_item.Application, baseKey.Name, store);
         Register(account);
         return(account);
     }
 }
 /// <summary>
 /// Finds the registry key for the account associated with the store.
 /// </summary>
 /// <returns>The registry key, or null if it cannot be found</returns>
 private RegistryKey FindRegistryKey(IStore store)
 {
     // Find the registry key by store id
     using (RegistryKey key = OpenBaseKey())
     {
         if (key != null)
         {
             foreach (string subkey in key.GetSubKeyNames())
             {
                 RegistryKey accountKey = key.OpenSubKey(subkey);
                 string      storeId    = AccountWrapper.GetStoreId(accountKey.Name);
                 if (storeId != null && storeId == store.StoreID)
                 {
                     return(accountKey);
                 }
                 accountKey.Dispose();
             }
         }
     }
     return(null);
 }
 private void OnAccountRemoved(AccountWrapper account)
 {
     AccountRemoved?.Invoke(account);
 }
 private void OnAccountDiscovered(AccountWrapper account)
 {
     AccountDiscovered?.Invoke(account);
 }