示例#1
0
        /// <summary>
        /// detects duplicates and removes them from the contacts
        /// </summary>
        /// <param name="clientFolderName">
        /// the outlook path that should be searched for duplicates
        /// </param>
        public override void RemoveDuplicates(string clientFolderName)
        {
            var currentElementName = string.Empty;

            // get a connection to outlook
            this.LogProcessingEvent(Resources.uiLogginIn);
            var outlookNamespace = OutlookClient.GetNamespace();

            // we need to log off from outlook in order to clean up the session
            try
            {
                var calendarItems = OutlookClient.GetOutlookMapiFolder(
                    outlookNamespace, clientFolderName, OlDefaultFolders.olFolderContacts);

                this.LogProcessingEvent(Resources.uiPreparingList);
                var outlookItemList = from a in calendarItems.Items.OfType <ContactItem>()
                                      orderby a.LastName, a.FirstName
                select a;

                ContactItem lastItem    = null;
                var         contactList = new List <StdContact>(300);
                foreach (var item in outlookItemList)
                {
                    currentElementName = item.LastName + ", " + item.FirstName;

                    if (lastItem != null)
                    {
                        var stdItem = OutlookClient.ConvertToStandardContact(item, contactList);
                        contactList.Add(stdItem);
                        this.LogProcessingEvent(stdItem, Resources.uiComparing);

                        if (lastItem.LastName == item.LastName && lastItem.FirstName == item.FirstName &&
                            lastItem.MiddleName == item.MiddleName)
                        {
                            this.LogProcessingEvent(stdItem, Resources.uiRemoving);

                            item.Delete();
                            continue;
                        }
                    }

                    lastItem = item;
                }
            }
            catch (Exception ex)
            {
                this.LogProcessingEvent(Resources.uiErrorAtName, currentElementName, ex.Message);
            }
            finally
            {
                outlookNamespace.Logoff();
            }

            this.LogProcessingEvent(Resources.uiRemoveDuplicates);
        }
示例#2
0
        /// <summary>
        /// Overrides the method to read the full list of data.
        /// </summary>
        /// <param name="clientFolderName">
        /// the name of the outlook folder that does contain the contacts that will be read.
        /// </param>
        /// <param name="result">
        /// A list of StdElements that will get the new imported entries.
        /// </param>
        /// <returns>
        /// The list with the added contacts
        /// </returns>
        protected override List <StdElement> ReadFullList(string clientFolderName, List <StdElement> result)
        {
            if (result == null)
            {
                throw new ArgumentNullException("result");
            }

            var currentElementName = string.Empty;

            // get a connection to outlook
            this.LogProcessingEvent(Resources.uiLogginIn);
            var outlookNamespace = OutlookClient.GetNamespace();

            // we need to log off from outlook in order to clean up the session
            try
            {
                // select a folder
                var outlookFolder = OutlookClient.GetOutlookMapiFolder(
                    outlookNamespace, clientFolderName, OlDefaultFolders.olFolderContacts);

                // if no folder has been selected, we will leave here
                if (outlookFolder == null)
                {
                    this.LogProcessingEvent(Resources.uiNoFolderSelected);
                }
                else
                {
                    // get all the Contacts from the Contacts Folder
                    var contactItems = outlookFolder.Items;
                    var itemsToDo    = contactItems.Count;

                    // iterate through the contacts
                    for (var itemIndex = 1; itemIndex <= itemsToDo; itemIndex++)
                    {
                        // in case of problems with a single item, we will continue with the next
                        try
                        {
                            var contactItem = contactItems[itemIndex] as ContactItem;
                            if (contactItem != null)
                            {
                                currentElementName = contactItem.LastName + ", " + contactItem.FirstName;

                                var newContact = OutlookClient.ConvertToStandardContact(
                                    contactItem, result.ToStdContacts());
                                if (newContact != null)
                                {
                                    result.Add(newContact);
                                    this.LogProcessingEvent(
                                        newContact,
                                        string.Format(
                                            CultureInfo.CurrentCulture, Resources.uiReadingContact, currentElementName));
                                }
                            }
                        }
                        catch (COMException ex)
                        {
                            if (ex.ErrorCode == -1285291755 || ex.ErrorCode == -2147221227)
                            {
                                this.LogProcessingEvent(
                                    string.Format(
                                        CultureInfo.CurrentCulture,
                                        Resources.uiProblemAccessingOutlookStore,
                                        currentElementName,
                                        ex.Message));
                            }
                            else
                            {
                                throw;
                            }
                        }

                        this.UpdateProgress(itemIndex * 100 / itemsToDo);
                    }
                }
            }
            catch (Exception ex)
            {
                this.LogProcessingEvent(
                    string.Format(CultureInfo.CurrentCulture, Resources.uiErrorAtName, currentElementName, ex.Message));
            }
            finally
            {
                outlookNamespace.Logoff();
            }

            return(result);
        }