Esempio n. 1
0
        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));
        }