/// <summary>
        /// Checks if the folder is a relevant GAB contacts folder.
        /// </summary>
        /// <param name="folder">The folder.</param>
        /// <param name="accounts">If specified, the folder is considered relevant only if it is the GAB for one of the specified
        /// accounts. Otherwise, any GAB folder is considered relevant.</param>
        public static bool IsGABContactsFolder(IFolder folder, ZPushAccount[] accounts)
        {
            // Check if this is a GAB folder at all
            GABInfo gab = GetGABContactsFolderInfo(folder);

            if (gab == null)
            {
                return(false);
            }

            // If we don't have a list of accounts, it is what we're looking for
            if (accounts == null)
            {
                return(true);
            }

            // Check if the domain is specified
            foreach (ZPushAccount account in accounts)
            {
                if (account.Account.DomainName == gab.Domain)
                {
                    return(true);
                }
            }
            return(false);
        }
 private IAddressBook FindGABForDomain(IFolder root, string domain)
 {
     // Scan all subfolders for the GAB
     foreach (IAddressBook subfolder in root.GetSubFolders <IAddressBook>())
     {
         try
         {
             if (subfolder.DefaultMessageClass == OutlookConstants.MESSAGE_CLASS_CONTACTS)
             {
                 GABInfo gabInfo = GABInfo.Get(subfolder);
                 if (gabInfo != null && gabInfo.IsForDomain(domain))
                 {
                     Logger.Instance.Debug(this, "Found existing GAB: {0}", gabInfo);
                     return(subfolder);
                 }
             }
         }
         catch (System.Exception e)
         {
             Logger.Instance.Warning(this, "Exception scanning GABs: {0}", e);
         }
         Logger.Instance.Debug(this, "Skipping GAB folder: {0}", subfolder.Name);
         subfolder.Dispose();
     }
     return(null);
 }
        private static GABInfo GetExisting(IFolder folder)
        {
            string subject = null;

            try
            {
                subject = (string)folder.GetProperty(OutlookConstants.PR_SUBJECT);
            }
            catch (System.Exception) { }
            if (string.IsNullOrEmpty(subject))
            {
                return(null);
            }

            string[] parts = subject.Split(';');
            if (parts.Length < 1 || !parts[0].StartsWith(ID))
            {
                return(null);
            }

            string  domain = parts[0].Substring(ID.Length);
            GABInfo gab    = new GABInfo(domain);

            return(gab);
        }
        public static GABInfo GetGABContactsFolderInfo(IFolder folder)
        {
            if (folder.DefaultMessageClass != OutlookConstants.MESSAGE_CLASS_CONTACTS)
            {
                return(null);
            }

            return(GABInfo.Get(folder));
        }
        /// <summary>
        /// Retrieves the GAB info for the folder.
        /// </summary>
        /// <param name="folder">The folder</param>
        /// <param name="forDomain">The domain name. If this is not null, and a GAB info is not found, it is created</param>
        /// <returns></returns>
        public static GABInfo Get(IFolder folder, string forDomain = null)
        {
            GABInfo gab = GetExisting(folder);

            if (gab == null && forDomain != null)
            {
                gab = new GABInfo(forDomain);
            }
            return(gab);
        }
        /// <summary>
        /// Checks and removes any GAB folders that are not registered to accounts. This
        /// happens if an account is removed while Outlook is closed.
        /// </summary>
        private void CheckGABUnused()
        {
            if (!CheckUnused)
            {
                return;
            }

            if (_store != null)
            {
                _store.Dispose();
                _store = null;
            }
            _store = ZPushLocalStore.GetInstance(ThisAddIn.Instance);
            if (_store == null)
            {
                return;
            }

            bool deletedSomething = false;

            using (IFolder root = _store.GetRootFolder())
            {
                foreach (IFolder subfolder in root.GetSubFolders <IFolder>())
                {
                    using (subfolder)
                    {
                        // Remove any contacts folder that is not registered for GAB
                        GABInfo info = GetGABContactsFolderInfo(subfolder);
                        if (info != null && !_domains.Contains(info.Domain))
                        {
                            Logger.Instance.Info(this, "Unused GAB folder: {0} - {1}", subfolder.EntryID, subfolder.Name);
                            try
                            {
                                deletedSomething = true;
                                subfolder.Delete();
                            }
                            catch (System.Exception e)
                            {
                                Logger.Instance.Error(this, "Error removing GAB folder: {0}", e);
                            }
                        }
                    }
                }
            }

            if (deletedSomething)
            {
                DoEmptyDeletedItems();
            }
        }
        private IAddressBook CreateGABContacts(string domainName)
        {
            if (_store != null)
            {
                _store.Dispose();
                _store = null;
            }
            _store = ZPushLocalStore.GetInstance(ThisAddIn.Instance);
            if (_store == null)
            {
                return(null);
            }

            // Try to find the existing GAB
            using (IFolder root = _store.GetRootFolder())
            {
                IAddressBook gab = FindGABForDomain(root, domainName);
                if (gab == null)
                {
                    Logger.Instance.Debug(this, "Creating new GAB folder for {0}", domainName);
                    string name = string.Format(Properties.Resources.GAB_FolderFormat, domainName);
                    gab = root.CreateFolder <IAddressBook>(name);
                }
                else
                {
                    Logger.Instance.Debug(this, "Found existing GAB folder for {0}", domainName);
                }

                // The local folders are hidden, unhide tha GAB folder
                gab.AttrHidden = false;

                // Update admin
                _gabFolders.Add(gab.EntryID);
                GABInfo gabInfo = GABInfo.Get(gab, domainName);
                gabInfo.Store(gab);

                if (SuppressModifications)
                {
                    // Hook BeforeMove event to prevent modifications
                    // TODO: use ZPushWatcher for this?
                    gab.BeforeItemMove += SuppressMoveEventHandler;
                }

                return(gab);
            }
        }