Ejemplo n.º 1
0
        /// <summary>
        /// Abstract read method for full list of elements - this is part of the minimum that needs to be overridden
        /// </summary>
        /// <param name="clientFolderName">
        /// the information from where inside the source the elements should be read -
        ///   This does not need to be a real "path", but need to be something that can be expressed as a string
        /// </param>
        /// <param name="result">
        /// The list of elements that should get the elements. The elements should be added to
        ///   the list instead of replacing it.
        /// </param>
        /// <returns>
        /// The list with the newly added elements
        /// </returns>
        protected override List <StdElement> ReadFullList(string clientFolderName, List <StdElement> result)
        {
            var currentElementName = string.Empty;
            var minimumDate        = DateTime.Now;

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

            // we need to log off from outlook in order to clean up the session
            try
            {
                if (!string.IsNullOrEmpty(clientFolderName) && clientFolderName.Contains(":"))
                {
                    clientFolderName = clientFolderName.Substring(
                        0, clientFolderName.IndexOf(":", StringComparison.Ordinal));
                }

                // select a folder
                var outlookFolder = OutlookClient.GetOutlookMapiFolder(
                    outlookNamespace, clientFolderName, OlDefaultFolders.olFolderCalendar);

                // if no folder has been selected, we will leave here
                if (outlookFolder == null)
                {
                    this.LogProcessingEvent(Resources.UserInfoNoOutlookFolderSelected);
                }
                else
                {
                    // get all the calendar items from the calendar Folder
                    var calendarItems = outlookFolder.Items;

                    // iterate through the calendarFolder
                    for (var itemIndex = 1; itemIndex <= calendarItems.Count; itemIndex++)
                    {
                        // in case of problems with a single item, we will continue with the next
                        try
                        {
                            var calendarStdItem = calendarItems[itemIndex] as AppointmentItem;
                            if (calendarStdItem != null && calendarStdItem.Start > minimumDate)
                            {
                                currentElementName =
                                    calendarStdItem.Start.ToString("yyyy-MM-dd hh:mm:ss", CultureInfo.CurrentCulture) +
                                    " - " + calendarStdItem.Subject;

                                this.LogProcessingEvent(Resources.UserInfoReading + currentElementName);

                                result.Add(
                                    OutlookClient.ConvertToStandardCalendarItem(
                                        calendarStdItem, result.ToOtherType <StdElement, StdCalendarItem>()));
                            }
                        }
                        catch (COMException ex)
                        {
                            if (ex.ErrorCode == -1285291755 || ex.ErrorCode == -2147221227)
                            {
                                this.LogProcessingEvent(
                                    string.Format(
                                        CultureInfo.CurrentCulture,
                                        Resources.UserInfoProblemAccessingStore,
                                        currentElementName,
                                        ex.Message));
                            }
                            else
                            {
                                throw;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                this.LogProcessingEvent(
                    string.Format(
                        CultureInfo.CurrentCulture, Resources.UserInfoErrorAtName, currentElementName, ex.Message));
            }
            finally
            {
                outlookNamespace.Logoff();
            }

            return(result);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// detects duplicates and removes them from the calendar
        /// </summary>
        /// <param name="clientFolderName">
        /// the path to the outlook folder to process
        /// </param>
        public override void RemoveDuplicates(string clientFolderName)
        {
            var currentElementName = string.Empty;

            // get a connection to outlook
            this.LogProcessingEvent(Resources.UserInfoLoggingOn);
            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.olFolderCalendar);

                this.LogProcessingEvent(Resources.UserInfoPreparingList);
                var outlookItemList = from a in calendarItems.Items.OfType <AppointmentItem>()
                                      orderby a.Subject, a.Start
                select a;

                AppointmentItem lastItem       = null;
                var             lastPersonName = string.Empty;
                foreach (
                    var item in
                    outlookItemList.OrderBy(x => x.Start.ToString("MM.dd-hh:mm", CultureInfo.InvariantCulture)))
                {
                    var subject = item.Subject;

                    var personName = string.IsNullOrEmpty(subject)
                                         ? string.Empty
                                         : subject.StartsWith("Geburtstag von ", StringComparison.OrdinalIgnoreCase)
                                               ? subject.Substring(15)
                                               : subject.EndsWith("'s Birthday", StringComparison.OrdinalIgnoreCase)
                                                     ? subject.Substring(0, subject.Length - 11)
                                                     : string.Empty;

                    if (lastItem != null)
                    {
                        var stdItem = OutlookClient.ConvertToStandardCalendarItem(item, null);
                        this.LogProcessingEvent(stdItem, "comparing ...");

                        if (!string.IsNullOrEmpty(personName))
                        {
                            if (lastPersonName == personName && lastItem.Start == item.Start &&
                                lastItem.Body == item.Body)
                            {
                                this.LogProcessingEvent(stdItem, Resources.UserInfoRemoving);

                                item.Delete();
                                continue;
                            }
                        }
                    }

                    lastItem       = item;
                    lastPersonName = personName;
                }
            }
            catch (Exception ex)
            {
                this.LogProcessingEvent(
                    string.Format(
                        CultureInfo.CurrentCulture, Resources.UserInfoErrorAtName, currentElementName, ex.Message));
            }
            finally
            {
                outlookNamespace.Logoff();
            }

            this.LogProcessingEvent(Resources.UserInfoRemoveDuplicatesFinished);
        }