Ejemplo n.º 1
0
        /**
         * List, add, or delete Contacts.
         *
         * @param mySession
         *	Populated session object.
         * @param pstn
         *  Properly formatted phone number of the target PSTN Contact.
         * @param displayName
         *  Display name of the target PSTN Contact (used by add <em>only</em>).
         * @param commandOpt
         *  Command string, which must be one of:
         *  <ul>
         *    <li>{@link #ADD_CONTACT}</li>
         *    <li>{@link #DELETE_CONTACT}</li>
         *    <li>{@link #LIST_CONTACTS}</li>
         *  </ul>
         *
         * @since 1.0
         */
        static void doPstnContact(MySession mySession, String pstn, String displayName, String commandOpt)
        {
            // Verify that our command option is valid -- something our invoker should have already done!
            if ((!(commandOpt.Equals(LIST_CONTACTS))) && (!(commandOpt.Equals(ADD_CONTACT))) &&
                (!(commandOpt.Equals(DELETE_CONTACT))))
            {
                // We shouldn't get here -- the invoker should have already validated the command.
                MySession.myConsole.printf("%s: Unrecognized command option: %s%n", mySession.myTutorialTag, commandOpt);
                return;
            }

            int contactIdx;

            Skype.NormalizeIdentityResponse nrmlResponse = null;

            ContactGroup soContactGroup = mySession.mySkype.getHardwiredContactGroup(ContactGroup.Type.SKYPEOUT_BUDDIES);

            Contact[] soContactList = soContactGroup.getContacts();
            int       contactCount  = soContactList.Length;

            // Handle list operations...
            if (commandOpt.Equals(LIST_CONTACTS))
            {
                // Make sure there's something to list!
                if (contactCount == 0)
                {
                    MySession.myConsole.printf("%s: There are no PSTN contacts.%n", mySession.myTutorialTag);
                    return;
                }
                MySession.myConsole.printf("%s: Current list of PSTN contacts:%n", mySession.myTutorialTag);
                for (contactIdx = 0; contactIdx < contactCount; contactIdx++)
                {
                    MySession.myConsole.printf("%s: %d. %s (%s)%n", mySession.myTutorialTag, (contactIdx + 1),
                                               soContactList[contactIdx].getPstnNumber(),
                                               soContactList[contactIdx].getDisplayName());
                }
                return;
            }

            //Handle add & delete operations...
            String contactPstn;
            bool   contactAlreadyListed = false;

            // Ensure that the pstn argument contains a valid contact identity
            nrmlResponse = getNormalizationStr(pstn);
            if (nrmlResponse.result != Skype.NormalizeResult.IDENTITY_OK)
            {
                MySession.myConsole.printf("%s: Cannot normalize pstn %s using %s%n",
                                           mySession.myTutorialTag, pstn,
                                           mySession.mySkype.getIsoCountryInfo().countryPrefixList[0]);
                return;
            }

            // Check whether the PSTN contact already exists, which is relevant to both
            // adding and removing contacts. In current wrapper version, the only way to do this
            // is to loop over a contact group.
            for (contactIdx = 0; contactIdx < contactCount; contactIdx++)
            {
                contactPstn = soContactList[contactIdx].getPstnNumber();
                if (contactPstn.Equals(nrmlResponse.normalized))
                {
                    contactAlreadyListed = true;
                }
            }

            // Handle adding a Contact. The Contact must not exist in that group!
            if (commandOpt.Equals(ADD_CONTACT))
            {
                if (contactAlreadyListed)
                {
                    MySession.myConsole.printf("%s: Error: %s already present in ContactGroup.%n",
                                               mySession.myTutorialTag, nrmlResponse.normalized);
                    return;
                }
                MySession.myConsole.printf("%s: Adding PSTN Contact...%n", mySession.myTutorialTag);
                Contact newContact = mySession.mySkype.getContact(nrmlResponse.normalized);
                if ((newContact != null) && (soContactGroup.canAddContact(newContact)))
                {
                    newContact.giveDisplayName(displayName);
                    soContactGroup.addContact(newContact);
                    MySession.myConsole.printf("%s: Contact %s (%s) added.%n",
                                               mySession.myTutorialTag, nrmlResponse.normalized, displayName);
                }
                else
                {
                    ContactGroup.Type soContactGroupType = soContactGroup.getType();
                    MySession.myConsole.printf("%s: Cannot add Contact %s (%s) to ContactGroup %s (\"%s\") using AddContact():%n",
                                               mySession.myTutorialTag, nrmlResponse.normalized, displayName,
                                               soContactGroupType.toString(),
                                               soContactGroup.getGivenDisplayName());
                    if (newContact == null)
                    {
                        MySession.myConsole.println("\tCould not create new Contact (normalized PSTN likely invalid)");
                    }
                    else if (!(soContactGroup.canAddContact(newContact)))
                    {
                        MySession.myConsole.println("\tCannot add Contacts to target ContactGroup");
                    }
                    else
                    {
                        MySession.myConsole.println("\tReason unknown?!?%n");
                    }
                }
                return;
            }

            // Handle deleting a Contact. The Contact must exist in that group!
            if (!contactAlreadyListed)
            {
                MySession.myConsole.printf("%s: PSTN Contact %s not present in ContactGroup.%n",
                                           mySession.myTutorialTag, nrmlResponse.normalized);
                return;
            }

            MySession.myConsole.printf("%s: Removing PSTN Contact...%n", mySession.myTutorialTag);
            Contact removableContact = mySession.mySkype.getContact(nrmlResponse.normalized);

            if ((removableContact != null) && (soContactGroup.canRemoveContact()))
            {
                String removableDisplayName = removableContact.getDisplayName();
                soContactGroup.removeContact(removableContact);
                // Can't include any Contact-specific identifying information in the message that we haven't already
                // extracted since RemoveContact leaves the target Contact instance in an undefined (mostly nulled-out) state!
                MySession.myConsole.printf("%s: Removed PSTN Contact %s (\"%s\").%n",
                                           mySession.myTutorialTag, nrmlResponse.normalized, removableDisplayName);
            }
            else
            {
                ContactGroup.Type soContactGroupType = soContactGroup.getType();
                MySession.myConsole.printf("%s: Cannot remove Contact %s from ContactGroup %s (\"%s\") using RemoveContact():%n",
                                           mySession.myTutorialTag, nrmlResponse.normalized,
                                           soContactGroupType.toString(),
                                           soContactGroup.getGivenDisplayName());
                if (removableContact == null)
                {
                    MySession.myConsole.println("\tCould not remove Contact (normalized PSTN likely invalid)");
                }
                else if (!(soContactGroup.canRemoveContact()))
                {
                    MySession.myConsole.println("\tCannot remove Contacts from target ContactGroup");
                }
                else
                {
                    MySession.myConsole.println("\tReason unknown?!?%n");
                }
            }

            return;
        }