Java Code Examples for org.jivesoftware.smack.roster.Roster#getEntry()

The following examples show how to use org.jivesoftware.smack.roster.Roster#getEntry() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Client.java    From desktopclient-java with GNU General Public License v3.0 6 votes vote down vote up
public boolean removeFromRoster(JID jid) {
    if (!this.isConnected()) {
        LOGGER.info("not connected");
        return false;
    }
    Roster roster = Roster.getInstanceFor(mConn);
    RosterEntry entry = roster.getEntry(jid.toBareSmack());
    if (entry == null) {
        LOGGER.info("can't find roster entry for jid: "+jid);
        return true;
    }
    try {
        // blocking
        roster.removeEntry(entry);
    } catch (SmackException.NotLoggedInException |
            SmackException.NoResponseException |
            XMPPException.XMPPErrorException |
            SmackException.NotConnectedException |
            InterruptedException ex) {
        LOGGER.log(Level.WARNING, "can't remove contact from roster", ex);
        return false;
    }
    return true;
}
 
Example 2
Source File: Client.java    From desktopclient-java with GNU General Public License v3.0 6 votes vote down vote up
public void updateRosterEntry(JID jid, String newName) {
    if (!this.isConnected()) {
        LOGGER.info("not connected");
        return;
    }
    Roster roster = Roster.getInstanceFor(mConn);
    RosterEntry entry = roster.getEntry(jid.toBareSmack());
    if (entry == null) {
        LOGGER.warning("can't find roster entry for jid: "+jid);
        return;
    }
    try {
        entry.setName(newName);
    } catch (SmackException.NotConnectedException |
            SmackException.NoResponseException |
            XMPPException.XMPPErrorException |
            InterruptedException ex) {
        LOGGER.log(Level.WARNING, "can't set name for entry", ex);
    }
}
 
Example 3
Source File: ContactList.java    From Spark with Apache License 2.0 6 votes vote down vote up
/**
 * Removes a contact item from the group.
 *
 * @param item the ContactItem to remove.
 */
private void removeContactFromGroup(ContactItem item) {
    String groupName = item.getGroupName();
    ContactGroup contactGroup = getContactGroup(groupName);
    Roster roster = Roster.getInstanceFor( SparkManager.getConnection() );
    RosterEntry entry = roster.getEntry(item.getJid().asBareJid());
    if (entry != null && contactGroup != offlineGroup) {
        try {
            RosterGroup rosterGroup = roster.getGroup(groupName);
            if (rosterGroup != null) {
                RosterEntry rosterEntry = rosterGroup.getEntry(entry.getJid());
                if (rosterEntry != null) {
                    rosterGroup.removeEntry(rosterEntry);
                }
            }
            contactGroup.removeContactItem(contactGroup.getContactItemByJID(item.getJid()));
            checkGroup(contactGroup);
        }
        catch (Exception e) {
            Log.error("Error removing user from contact list.", e);
        }
    }
}
 
Example 4
Source File: IntegrationTestRosterUtil.java    From Smack with Apache License 2.0 5 votes vote down vote up
private static void notInRoster(XMPPConnection c1, XMPPConnection c2) throws NotLoggedInException,
        NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
    Roster roster = Roster.getInstanceFor(c1);
    RosterEntry c2Entry = roster.getEntry(c2.getUser().asBareJid());
    if (c2Entry == null) {
        return;
    }
    roster.removeEntry(c2Entry);
}
 
Example 5
Source File: UriManager.java    From Spark with Apache License 2.0 5 votes vote down vote up
/**
    * Handles the ?remove URI
    * 
    * @param uri
    *            the decoded uri
    * @throws Exception
    */
   public void handleRemove(URI uri) throws Exception {
// xmpp:[email protected]?remove

   BareJid jid;
   try {
       jid = JidCreate.bareFrom(retrieveJID(uri));
   } catch (XmppStringprepException e) {
       throw new IllegalStateException(e);
   }

   Roster roster = Roster.getInstanceFor( SparkManager.getConnection() );
RosterEntry entry = roster.getEntry(jid);
roster.removeEntry(entry);
   }
 
Example 6
Source File: ContactList.java    From Spark with Apache License 2.0 5 votes vote down vote up
/**
 * Updates the users presence.
 *
 * @param presence the user to update.
 * @throws Exception if there is a problem while updating the user's presence.
 */
private synchronized void updateUserPresence(Presence presence) throws Exception {
    if (presence.getError() != null) {
        // We ignore this.
        return;
    }

    final Roster roster = Roster.getInstanceFor( SparkManager.getConnection() );

    final BareJid bareJID = presence.getFrom().asBareJid();

    RosterEntry entry = roster.getEntry(bareJID);
    boolean isPending = entry != null && (entry.getType() == RosterPacket.ItemType.none || entry.getType() == RosterPacket.ItemType.from)
        && entry.isSubscriptionPending();

    // If online, check to see if they are in the offline group.
    // If so, remove from offline group and add to all groups they
    // belong to.

    if (presence.getType() == Presence.Type.available && offlineGroup.getContactItemByJID(bareJID) != null || ( presence.getFrom().toString().contains( "workgroup." ) )) {
        changeOfflineToOnline(bareJID, entry, presence);
    }
    else if (presence.getType() == Presence.Type.available) {
        updateContactItemsPresence(presence, entry, bareJID);
    }
    else if (presence.getType() == Presence.Type.unavailable && !isPending) {
        // If not available, move to offline group.
        Presence rosterPresence = PresenceManager.getPresence(bareJID);
        if (!rosterPresence.isAvailable()) {
            moveToOfflineGroup(presence, bareJID);
        }
        else {
            updateContactItemsPresence(rosterPresence, entry, bareJID);
        }
    }
    
}
 
Example 7
Source File: ContactList.java    From Spark with Apache License 2.0 5 votes vote down vote up
private void removeContactFromRoster(ContactItem item) {
    Roster roster = Roster.getInstanceFor( SparkManager.getConnection() );
    RosterEntry entry = roster.getEntry(item.getJid().asBareJid());
    if (entry != null) {
        try {
            roster.removeEntry(entry);
        }
        catch (XMPPException | SmackException | InterruptedException e) {
            Log.warning("Unable to remove roster entry.", e);
        }
    }
}
 
Example 8
Source File: ContactGroupTransferHandler.java    From Spark with Apache License 2.0 4 votes vote down vote up
private void addContactItem(final ContactGroup contactGroup, final ContactItem item) {
     ContactItem newContact = UIComponentRegistry.createContactItem(Res.getString("group.empty"), null, null);
     newContact.setPresence(item.getPresence());
     newContact.setIcon(item.getIcon());
     newContact.getNicknameLabel().setFont(item.getNicknameLabel().getFont());

     if (!PresenceManager.isOnline(item.getJid().asBareJid())) {
         contactGroup.addOfflineContactItem(item.getAlias(), item.getNickname(), item.getJid(), null);
     }
     else {
         contactGroup.addContactItem(newContact);
     }
     contactGroup.clearSelection();
     contactGroup.fireContactGroupUpdated(); //Updating group title

     final ContactGroup oldGroup = getContactGroup(item.getGroupName());

     SwingWorker worker = new SwingWorker() {
         @Override
public Object construct() {
             Roster roster = Roster.getInstanceFor( SparkManager.getConnection() );
             RosterEntry entry = roster.getEntry(item.getJid().asBareJid());

             RosterGroup groupFound = null;

             for (RosterGroup group : roster.getGroups()) {
                 if (group.getName().equals(contactGroup.getGroupName())) {
                     try {
                         groupFound = group;
                         group.addEntry(entry);
                     }
                     catch (XMPPException | SmackException | InterruptedException e1) {
                         Log.error(e1);
                         return false;
                     }
                 }
             }

             // This is a new group
             if (groupFound == null) {
                 groupFound = roster.createGroup(contactGroup.getGroupName());
                 try {
                     groupFound.addEntry(entry);
                 }
                 catch (XMPPException | SmackException | InterruptedException e) {
                     Log.error(e);
                 }
             }
             return true;
         }

         @Override
public void finished() {
             if ((Boolean)get()) {
                 // Now try and remove the group from the old one.
                 removeContactItem(oldGroup, item);
             }
         }

     };

     worker.start();
 }
 
Example 9
Source File: ContactGroupTransferHandler.java    From Spark with Apache License 2.0 4 votes vote down vote up
public boolean removeContactItem(ContactGroup contactGroup, ContactItem item) {
    if (contactGroup.isSharedGroup()) {
        return false;
    }

    if (contactGroup.isUnfiledGroup()) {
        contactGroup.removeContactItem(item);
        contactGroup.fireContactGroupUpdated();
        return true;
    }

    // Remove entry from Roster Group
    Roster roster = Roster.getInstanceFor( SparkManager.getConnection() );
    RosterEntry entry = roster.getEntry(item.getJid().asBareJid());

    RosterGroup rosterGroup = null;

    for (RosterGroup group : roster.getGroups()) {
        if (group.getName().equals(contactGroup.getGroupName())) {
            try {
                rosterGroup = group;
                group.removeEntry(entry);
            }
            catch (XMPPException | SmackException | InterruptedException e1) {
                return false;
            }
        }
    }

    if (rosterGroup == null) {
        return false;
    }

    if (!rosterGroup.contains(entry)) {
        contactGroup.removeContactItem(item);
        contactGroup.fireContactGroupUpdated(); //Updating group title
        return true;
    }

    return false;
}
 
Example 10
Source File: SubscriptionDialog.java    From Spark with Apache License 2.0 4 votes vote down vote up
/**
 * Adds a new entry to the users Roster.
 *
 * @param jid      the jid.
 * @param nickname the nickname.
 * @param group    the contact group.
 * @return the new RosterEntry.
 */
public RosterEntry addEntry(BareJid jid, String nickname, String group) {
    String[] groups = {group};

    Roster roster = Roster.getInstanceFor( SparkManager.getConnection() );
    RosterEntry userEntry = roster.getEntry(jid);

    boolean isSubscribed = true;
    if (userEntry != null) {
        isSubscribed = userEntry.getGroups().size() == 0;
    }

    if (isSubscribed) {
        try {
            roster.createEntry(jid, nickname, new String[]{group});
        }
        catch (XMPPException | SmackException | InterruptedException e) {
            Log.error("Unable to add new entry " + jid, e);
        }
        return roster.getEntry(jid);
    }


    try {
        RosterGroup rosterGroup = roster.getGroup(group);
        if (rosterGroup == null) {
            rosterGroup = roster.createGroup(group);
        }

        if (userEntry == null) {
            roster.createEntry(jid, nickname, groups);
            userEntry = roster.getEntry(jid);
        }
        else {
            userEntry.setName(nickname);
            rosterGroup.addEntry(userEntry);
        }

        userEntry = roster.getEntry(jid);
    }
    catch (XMPPException | SmackException | InterruptedException ex) {
        Log.error(ex);
    }
    return userEntry;
}
 
Example 11
Source File: ContactList.java    From Spark with Apache License 2.0 4 votes vote down vote up
@Override
public void actionPerformed(ActionEvent e) {
       if (e.getSource() == addingGroupButton) {
           new RosterDialog().showRosterDialog();
       }
       else if (e.getSource() == chatMenu) {
           if (activeItem != null) {
               SparkManager.getChatManager().activateChat(activeItem.getJid(), activeItem.getDisplayName());
           }
       }
       else if (e.getSource() == addContactMenu) {
           RosterDialog rosterDialog = new RosterDialog();
           if (activeGroup != null) {
               rosterDialog.setDefaultGroup(activeGroup);
           }
           rosterDialog.showRosterDialog();
       }
       else if (e.getSource() == removeContactFromGroupMenu) {
           if (activeItem != null) {
               removeContactFromGroup(activeItem);
           }
       }
       else if (e.getSource() == renameMenu) {
           if (activeItem == null) {
               return;
           }

           String oldAlias = activeItem.getAlias();
           String newAlias = JOptionPane.showInputDialog(this, Res.getString("label.rename.to") + ":", oldAlias);
           
           // if user pressed 'cancel', output will be null.
       	// if user removed alias, output will be an empty String.
           if (newAlias != null) {
            if (!ModelUtil.hasLength(newAlias)) {
            	newAlias = null; // allows you to remove an alias.
            }
            
            BareJid address = activeItem.getJid().asBareJid();
            ContactGroup contactGroup = getContactGroup(activeItem.getGroupName());
            ContactItem contactItem = contactGroup.getContactItemByDisplayName(activeItem.getDisplayName());
            contactItem.setAlias(newAlias);

            final Roster roster = Roster.getInstanceFor( SparkManager.getConnection() );
            RosterEntry entry = roster.getEntry(address);
               try
               {
                   entry.setName(newAlias);

                   final BareJid user = address.asBareJid();
                   for ( ContactGroup cg : groupList ) {
                       ContactItem ci = cg.getContactItemByJID(user);
                       if (ci != null) {
                           ci.setAlias(newAlias);
                       }
                   }
               }
               catch ( XMPPException.XMPPErrorException| SmackException.NotConnectedException | SmackException.NoResponseException | InterruptedException e1 )
               {
                   Log.warning( "Unable to set new alias '" + newAlias + "' for roster entry " + address, e1 );
               }
           }
       }
   }
 
Example 12
Source File: RosterDialog.java    From Spark with Apache License 2.0 4 votes vote down vote up
/**
 * Adds a new entry to the users Roster.
 *
 * @param jid      the jid.
 * @param nickname the nickname.
 * @param group    the contact group.
 * @return the new RosterEntry.
 */
public RosterEntry addEntry(BareJid jid, String nickname, String group) {
    String[] groups = {group};

    Roster roster = Roster.getInstanceFor( SparkManager.getConnection() );
    RosterEntry userEntry = roster.getEntry(jid);

    boolean isSubscribed = true;
    if (userEntry != null) {
        isSubscribed = userEntry.getGroups().size() == 0;
    }

    if (isSubscribed) {
        try {
            roster.createEntry(jid, nickname, new String[]{group});
        }
        catch (XMPPException | SmackException | InterruptedException e) {
            Log.error("Unable to add new entry " + jid, e);
        }
        return roster.getEntry(jid);
    }


    try {
        RosterGroup rosterGroup = roster.getGroup(group);
        if (rosterGroup == null) {
            rosterGroup = roster.createGroup(group);
        }

        if (userEntry == null) {
            roster.createEntry(jid, nickname, groups);
            userEntry = roster.getEntry(jid);
        }
        else {
            userEntry.setName(nickname);
            rosterGroup.addEntry(userEntry);
        }

        userEntry = roster.getEntry(jid);
    }
    catch (XMPPException | SmackException | InterruptedException ex) {
        Log.error(ex);
    }
    return userEntry;
}
 
Example 13
Source File: ContactListAssistantPlugin.java    From Spark with Apache License 2.0 4 votes vote down vote up
/**
 * Copies or moves a new <code>ContactItem</code> into the <code>ContactGroup</code>.
 *
 * @param contactGroup the ContactGroup.
 * @param item         the ContactItem to move.
 * @param move         true if the ContactItem should be moved, otherwise false.
 */
private void addContactItem(final ContactGroup contactGroup, final ContactItem item, final boolean move) {
    ContactItem newContact = UIComponentRegistry.createContactItem(item.getAlias(), item.getNickname(), item.getJid());
    newContact.setPresence(item.getPresence());
    newContact.setIcon(item.getIcon());
    newContact.getNicknameLabel().setFont(item.getNicknameLabel().getFont());
    boolean groupHadAvailableContacts = false;
    
    // Do not copy/move a contact item only if it is not already in the Group.
    if (contactGroup.getContactItemByJID(item.getJid().asBareJid(), true) != null) {
        return;
    }

    if (!PresenceManager.isOnline(item.getJid().asBareJid())) {
        contactGroup.addOfflineContactItem(item.getAlias(), item.getNickname(), item.getJid(), null);
    }
    else {
        groupHadAvailableContacts = contactGroup.hasAvailableContacts();
        contactGroup.addContactItem(newContact);
    }
    contactGroup.clearSelection();
    contactGroup.fireContactGroupUpdated(); //Updating group title

    final ContactGroup oldGroup = getContactGroup(item.getGroupName());

    
    final boolean groupAvailableContacts = groupHadAvailableContacts;
    SwingWorker worker = new SwingWorker() {
        @Override
        public Object construct() {
            Roster roster = Roster.getInstanceFor( SparkManager.getConnection() );
            RosterEntry entry = roster.getEntry(item.getJid().asBareJid());

            RosterGroup groupFound = null;

            for (RosterGroup group : roster.getGroups()) {
                if (group.getName().equals(contactGroup.getGroupName())) {
                    try {
                        groupFound = group;
                        if (!groupAvailableContacts)
                        {
                    	SparkManager.getContactList().toggleGroupVisibility(groupFound.getName(), true);
                        }
                        group.addEntry(entry);
                    }
                    catch (XMPPException | SmackException | InterruptedException e1) {
                        Log.error(e1);
                        return false;
                    }
                }
            }

            // This is a new group
            if (groupFound == null) {
                groupFound = roster.createGroup(contactGroup.getGroupName());
                try {
            	groupFound.addEntry(entry);
                    if (!groupAvailableContacts)
                    {
                	SparkManager.getContactList().toggleGroupVisibility(groupFound.getName(), true);
                    }  
                }
                catch (XMPPException | SmackException | InterruptedException e) {
                    Log.error(e);
                }
            }
            return true;
        }

        @Override
        public void finished() {
            if ((Boolean)get()) {
                // Now try and remove the group from the old one.
                if (move) {
                    removeContactItem(oldGroup, item);
                   if (!localPreferences.isEmptyGroupsShown() && !oldGroup.hasAvailableContacts())
                    {
                      SparkManager.getContactList().toggleGroupVisibility(oldGroup.getGroupName(),false);
                    }
                }
            }
        }

    };

    worker.start();
}
 
Example 14
Source File: ContactListAssistantPlugin.java    From Spark with Apache License 2.0 4 votes vote down vote up
public boolean removeContactItem(ContactGroup contactGroup, ContactItem item) {
    if (contactGroup.isSharedGroup()) {
        return false;
    }

    if (contactGroup.isUnfiledGroup()) {
        contactGroup.removeContactItem(item);
        contactGroup.fireContactGroupUpdated();
        return true;
    }

    // Remove entry from Roster Group
    Roster roster = Roster.getInstanceFor( SparkManager.getConnection() );
    RosterEntry entry = roster.getEntry(item.getJid().asBareJid());

    RosterGroup rosterGroup = null;

    for (RosterGroup group : roster.getGroups()) {
        if (group.getName().equals(contactGroup.getGroupName())) {
            try {
                rosterGroup = group;
                group.removeEntry(entry);
            }
            catch (XMPPException | SmackException | InterruptedException e1) {
                return false;
            }
        }
    }

    if (rosterGroup == null) {
        return false;
    }

    if (!rosterGroup.contains(entry)) {
        contactGroup.removeContactItem(item);
        contactGroup.fireContactGroupUpdated(); //Updating group title
        return true;
    }

    return false;
}