/// <summary> /// Overrides the method to write the full list of data. /// </summary> /// <param name="elements"> /// The elements to be written to outlook. /// </param> /// <param name="clientFolderName"> /// the name of the outlook folder that will get the contacts while writing data. /// </param> /// <param name="skipIfExisting"> /// a value indicating whether existing entries should be added overwritten or skipped. /// </param> protected override void WriteFullList(List <StdElement> elements, string clientFolderName, bool skipIfExisting) { this.LogProcessingEvent( string.Format(CultureInfo.CurrentCulture, Resources.uiAddingXElements, elements.Count)); // create outlook instance and get the folder var outlookNamespace = OutlookClient.GetNamespace(); var contactsEnum = OutlookClient.GetOutlookMapiFolder( outlookNamespace, clientFolderName, OlDefaultFolders.olFolderContacts).Items; // extract the contacts that do already exist var contactsList = OutlookClient.GetContactsList(contactsEnum); var added = 0; foreach (var element in elements) { // find outlook contact with matching id, create new if needed this.LogProcessingEvent(element, Resources.uiSearching); if ( !OutlookClient.WriteContactToOutlook( contactsEnum, (StdContact)element, skipIfExisting, contactsList)) { continue; } this.LogProcessingEvent(element, Resources.uiContactUpdated); added++; } outlookNamespace.Logoff(); this.LogProcessingEvent(string.Format(CultureInfo.CurrentCulture, Resources.uiXElementsAdded, added)); }
/// <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); }
/// <summary> /// Abstract write method for full list of elements - this is part of the minimum that needs to be overridden /// </summary> /// <param name="elements"> /// the list of elements that should be written to the target system. /// </param> /// <param name="clientFolderName"> /// the information to where inside the source the elements should be written - /// This does not need to be a real "path", but need to be something that can be expressed as a string /// </param> /// <param name="skipIfExisting"> /// specifies whether existing elements should be updated or simply left as they are /// </param> protected override void WriteFullList(List <StdElement> elements, string clientFolderName, bool skipIfExisting) { this.LogProcessingEvent( string.Format(CultureInfo.CurrentCulture, Resources.UserInfoAddingEntries, elements.Count)); // create outlook instance and get the folder var outlookNamespace = OutlookClient.GetNamespace(); var appointmentEnum = OutlookClient.GetOutlookMapiFolder( outlookNamespace, clientFolderName, OlDefaultFolders.olFolderCalendar).Items; // extract the contacts that do already exist var appointmentList = OutlookClient.GetAppointmentsList(appointmentEnum); var added = 0; var updated = 0; foreach (var element in elements) { // find outlook contact with matching id, create new if needed this.LogProcessingEvent(element, Resources.UserInfoSearchingElementInStore); switch ( OutlookClient.WriteCalendarItemToOutlook(appointmentEnum, (StdCalendarItem)element, appointmentList) ) { case SaveAction.None: break; case SaveAction.Update: updated++; break; case SaveAction.Create: added++; break; default: throw new ArgumentOutOfRangeException(); } } outlookNamespace.Logoff(); this.LogProcessingEvent( string.Format(CultureInfo.CurrentCulture, Resources.UserInfoElementsAdded, added, updated)); }
/// <summary> /// Deletes a contact entry. /// </summary> /// <param name="elementsToDelete"> /// The elements to delete. /// </param> /// <param name="clientFolderName"> /// The client folder name. /// </param> public override void DeleteElements(List <StdElement> elementsToDelete, string clientFolderName) { if (elementsToDelete == null) { return; } var outlookNamespace = OutlookClient.GetNamespace(); var outlookFolder = OutlookClient.GetOutlookMapiFolder( outlookNamespace, clientFolderName, OlDefaultFolders.olFolderContacts); elementsToDelete.ForEach( x => { // todo: the filter needs to be corrected! var contact = outlookFolder.Items.Find(x.Id.ToString()) as ContactItem; if (contact != null) { contact.Delete(); } }); outlookNamespace.Logoff(); }
/// <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); }
/// <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) { if (result == null) { throw new ArgumentNullException("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); }
/// <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); }