private void DeleteMemberAsync(Contact contact, ServiceName serviceName, RoleLists list, DeleteMemberCompletedEventHandler callback) { if (NSMessageHandler.MSNTicket == MSNTicket.Empty || AddressBook == null) { OnServiceOperationFailed(this, new ServiceOperationFailedEventArgs("DeleteMember", new MSNPSharpException("You don't have access right on this action anymore."))); return; } // check whether the update is necessary if (!contact.HasLists(list)) { return; } RoleId memberRole = ContactList.GetMemberRole(list); if (memberRole == RoleId.None) { return; } Service service = AddressBook.SelectTargetService(serviceName); if (service == null) { AddServiceAsync(serviceName, delegate { // RESURSIVE CALL DeleteMemberAsync(contact, serviceName, list, callback); }); return; } DeleteMemberRequestType deleteMemberRequest = new DeleteMemberRequestType(); deleteMemberRequest.serviceHandle = new HandleType(); deleteMemberRequest.serviceHandle.Id = service.Id.ToString(); deleteMemberRequest.serviceHandle.Type = serviceName; Membership memberShip = new Membership(); memberShip.MemberRole = memberRole; BaseMember deleteMember = null; // BaseMember is an abstract type, so we cannot create a new instance. // If we have a MembershipId different from 0, just use it. Otherwise, use email or phone number. BaseMember baseMember = AddressBook.SelectBaseMember(serviceName, contact.Account, contact.ClientType, memberRole); int membershipId = (baseMember == null) ? 0 : baseMember.MembershipId; switch (contact.ClientType) { case IMAddressInfoType.WindowsLive: deleteMember = new PassportMember(); deleteMember.Type = (baseMember == null) ? MembershipType.Passport : baseMember.Type; deleteMember.State = (baseMember == null) ? MemberState.Accepted : baseMember.State; if (membershipId == 0) { (deleteMember as PassportMember).PassportName = contact.Account; } else { deleteMember.MembershipId = membershipId; deleteMember.MembershipIdSpecified = true; } break; case IMAddressInfoType.Yahoo: case IMAddressInfoType.OfficeCommunicator: deleteMember = new EmailMember(); deleteMember.Type = MembershipType.Email; deleteMember.State = (baseMember == null) ? MemberState.Accepted : baseMember.State; if (membershipId == 0) { (deleteMember as EmailMember).Email = contact.Account; } else { deleteMember.MembershipId = membershipId; deleteMember.MembershipIdSpecified = true; } break; case IMAddressInfoType.Telephone: deleteMember = new PhoneMember(); deleteMember.Type = MembershipType.Phone; deleteMember.State = (baseMember == null) ? MemberState.Accepted : baseMember.State; if (membershipId == 0) { (deleteMember as PhoneMember).PhoneNumber = contact.Account; } else { deleteMember.MembershipId = membershipId; deleteMember.MembershipIdSpecified = true; } break; case IMAddressInfoType.Circle: deleteMember = new CircleMember(); deleteMember.Type = MembershipType.Circle; deleteMember.State = (baseMember == null) ? MemberState.Accepted : baseMember.State; (deleteMember as CircleMember).CircleId = contact.AddressBookId.ToString("D").ToLowerInvariant(); break; } memberShip.Members = new BaseMember[] { deleteMember }; deleteMemberRequest.memberships = new Membership[] { memberShip }; MsnServiceState DeleteMemberObject = new MsnServiceState(PartnerScenario.ContactMsgrAPI, "DeleteMember", true); SharingServiceBinding sharingService = (SharingServiceBinding)CreateService(MsnServiceType.Sharing, DeleteMemberObject); sharingService.DeleteMemberCompleted += delegate(object srv, DeleteMemberCompletedEventArgs e) { OnAfterCompleted(new ServiceOperationEventArgs(sharingService, MsnServiceType.Sharing, e)); if (e.Cancelled || NSMessageHandler.MSNTicket == MSNTicket.Empty) { return; } // Update AB AddressBook.RemoveMemberhip(serviceName, contact.Account, contact.ClientType, memberRole); if (callback != null) { callback(srv, e); } }; RunAsyncMethod(new BeforeRunAsyncMethodEventArgs(sharingService, MsnServiceType.Sharing, DeleteMemberObject, deleteMemberRequest)); }
private void AddMemberAsync(Contact contact, ServiceName serviceName, RoleLists list, AddMemberCompletedEventHandler callback) { if (NSMessageHandler.MSNTicket == MSNTicket.Empty || AddressBook == null) { OnServiceOperationFailed(this, new ServiceOperationFailedEventArgs("AddMember", new MSNPSharpException("You don't have access right on this action anymore."))); return; } // check whether the update is necessary if (contact.HasLists(list)) { return; } RoleId memberRole = ContactList.GetMemberRole(list); if (memberRole == RoleId.None) { return; } Service service = AddressBook.SelectTargetService(serviceName); if (service == null) { AddServiceAsync(serviceName, delegate { // RESURSIVE CALL AddMemberAsync(contact, serviceName, list, callback); }); return; } AddMemberRequestType addMemberRequest = new AddMemberRequestType(); addMemberRequest.serviceHandle = new HandleType(); addMemberRequest.serviceHandle.Id = service.Id.ToString(); addMemberRequest.serviceHandle.Type = serviceName; Membership memberShip = new Membership(); memberShip.MemberRole = memberRole; BaseMember member = null; // Abstract if (contact.ClientType == IMAddressInfoType.WindowsLive) { member = new PassportMember(); PassportMember passportMember = member as PassportMember; passportMember.PassportName = contact.Account; passportMember.State = MemberState.Accepted; passportMember.Type = MembershipType.Passport; } else if (contact.ClientType == IMAddressInfoType.Yahoo || contact.ClientType == IMAddressInfoType.OfficeCommunicator) { member = new EmailMember(); EmailMember emailMember = member as EmailMember; emailMember.State = MemberState.Accepted; emailMember.Type = MembershipType.Email; emailMember.Email = contact.Account; emailMember.Annotations = new Annotation[] { new Annotation() }; emailMember.Annotations[0].Name = AnnotationNames.MSN_IM_BuddyType; emailMember.Annotations[0].Value = (contact.ClientType == IMAddressInfoType.Yahoo) ? "32:" : "02:"; } else if (contact.ClientType == IMAddressInfoType.Telephone) { member = new PhoneMember(); PhoneMember phoneMember = member as PhoneMember; phoneMember.State = MemberState.Accepted; phoneMember.Type = MembershipType.Phone; phoneMember.PhoneNumber = contact.Account; } else if (contact.ClientType == IMAddressInfoType.Circle) { member = new CircleMember(); CircleMember circleMember = member as CircleMember; circleMember.Type = MembershipType.Circle; circleMember.State = MemberState.Accepted; circleMember.CircleId = contact.AddressBookId.ToString("D").ToLowerInvariant(); } if (member == null) { return; } memberShip.Members = new BaseMember[] { member }; addMemberRequest.memberships = new Membership[] { memberShip }; MsnServiceState AddMemberObject = new MsnServiceState(PartnerScenario.ContactMsgrAPI, "AddMember", true); SharingServiceBinding sharingService = (SharingServiceBinding)CreateService(MsnServiceType.Sharing, AddMemberObject); sharingService.AddMemberCompleted += delegate(object srv, AddMemberCompletedEventArgs e) { OnAfterCompleted(new ServiceOperationEventArgs(sharingService, MsnServiceType.Sharing, e)); if (e.Cancelled || NSMessageHandler.MSNTicket == MSNTicket.Empty) { return; } // Update AB AddressBook.AddMemberhip(serviceName, contact.Account, contact.ClientType, memberRole, member); if (callback != null) { callback(srv, e); } }; RunAsyncMethod(new BeforeRunAsyncMethodEventArgs(sharingService, MsnServiceType.Sharing, AddMemberObject, addMemberRequest)); }