org.jivesoftware.smack.roster.RosterGroup Java Examples

The following examples show how to use org.jivesoftware.smack.roster.RosterGroup. 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: RosterExchangeManager.java    From Smack with Apache License 2.0 6 votes vote down vote up
/**
 * Sends a roster group to userID. All the entries of the group will be sent to the
 * target user.
 *
 * @param rosterGroup the roster group to send
 * @param targetUserID the user that will receive the roster entries
 * @throws NotConnectedException if the XMPP connection is not connected.
 * @throws InterruptedException if the calling thread was interrupted.
 */
public void send(RosterGroup rosterGroup, Jid targetUserID) throws NotConnectedException, InterruptedException {
    XMPPConnection connection = weakRefConnection.get();

    // Create a new message to send the roster
    MessageBuilder msg = connection.getStanzaFactory().buildMessageStanza().to(targetUserID);
    // Create a RosterExchange Package and add it to the message
    RosterExchange rosterExchange = new RosterExchange();
    for (RosterEntry entry : rosterGroup.getEntries()) {
        rosterExchange.addRosterEntry(entry);
    }
    msg.addExtension(rosterExchange);

    // Send the message that contains the roster
    connection.sendStanza(msg.build());
}
 
Example #2
Source File: RosterExchange.java    From Smack with Apache License 2.0 6 votes vote down vote up
/**
 * Adds a roster entry to the packet.
 *
 * @param rosterEntry a roster entry to add.
 */
public void addRosterEntry(RosterEntry rosterEntry) {
    // Obtain a String[] from the roster entry groups name
    List<String> groupNamesList = new ArrayList<>();
    String[] groupNames;
    for (RosterGroup group : rosterEntry.getGroups()) {
        groupNamesList.add(group.getName());
    }
    groupNames = groupNamesList.toArray(new String[groupNamesList.size()]);

    // Create a new Entry based on the rosterEntry and add it to the packet
    RemoteRosterEntry remoteRosterEntry = new RemoteRosterEntry(rosterEntry.getJid(),
            rosterEntry.getName(), groupNames);

    addRosterEntry(remoteRosterEntry);
}
 
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: TransferGroupUI.java    From Spark with Apache License 2.0 6 votes vote down vote up
public TransferGroupUI(String groupName) {
    setLayout(new VerticalFlowLayout(VerticalFlowLayout.TOP, 0, 0, true, false));
    setBackground(Color.white);

    final Roster roster = Roster.getInstanceFor( SparkManager.getConnection() );
    final RosterGroup rosterGroup = roster.getGroup(groupName);

    final List<RosterEntry> entries = new ArrayList<RosterEntry>(rosterGroup.getEntries());

    Collections.sort(entries, entryComparator);

    for (RosterEntry entry : entries) {
        final UserEntry userEntry = new UserEntry(entry);
        userEntries.add(userEntry);
        add(userEntry);
    }
}
 
Example #5
Source File: RosterManager.java    From mangosta-android with Apache License 2.0 5 votes vote down vote up
public HashMap<Jid, Presence.Type> getContacts()
        throws SmackException.NotLoggedInException, InterruptedException,
        SmackException.NotConnectedException {
    Roster roster = Roster.getInstanceFor(XMPPSession.getInstance().getXMPPConnection());
    if (!roster.isLoaded()) {
        roster.reloadAndWait();
    }

    String groupName = "Buddies";

    RosterGroup group = roster.getGroup(groupName);

    if (group == null) {
        roster.createGroup(groupName);
        group = roster.getGroup(groupName);
    }

    HashMap<Jid, Presence.Type> buddies = new HashMap<>();

    List<RosterEntry> entries = group.getEntries();
    for (RosterEntry entry : entries) {
        BareJid jid = entry.getJid();
        Presence.Type status = roster.getPresence(jid).getType();
        buddies.put(jid, status);
    }

    return buddies;
}
 
Example #6
Source File: RosterManager.java    From mangosta-android with Apache License 2.0 5 votes vote down vote up
public HashMap<Jid, Presence.Type> getContactsWithSubscriptionPending()
        throws SmackException.NotLoggedInException, InterruptedException,
        SmackException.NotConnectedException {
    Roster roster = Roster.getInstanceFor(XMPPSession.getInstance().getXMPPConnection());
    if (!roster.isLoaded()) {
        roster.reloadAndWait();
    }

    String groupName = "Buddies";

    RosterGroup group = roster.getGroup(groupName);

    if (group == null) {
        roster.createGroup(groupName);
        group = roster.getGroup(groupName);
    }

    HashMap<Jid, Presence.Type> buddiesPending = new HashMap<>();

    List<RosterEntry> entries = group.getEntries();
    for (RosterEntry entry : entries) {
        if (entry.isSubscriptionPending()) {
            BareJid jid = entry.getJid();
            Presence.Type status = roster.getPresence(jid).getType();
            buddiesPending.put(jid, status);
        }
    }

    return buddiesPending;
}
 
Example #7
Source File: MucManager.java    From Yahala-Messenger with MIT License 5 votes vote down vote up
public List<RosterGroup> getGroups(Roster roster) {
    List<RosterGroup> groupList = new ArrayList<RosterGroup>();
    Collection<RosterGroup> rosterGroups = roster.getGroups();
    Iterator<RosterGroup> i = rosterGroups.iterator();
    while (i.hasNext()) {
        groupList.add(i.next());
    }
    return groupList;
}
 
Example #8
Source File: MucManager.java    From Yahala-Messenger with MIT License 5 votes vote down vote up
public List<RosterEntry> getEntriesByGroup(Roster roster, String groupName) {
    List<RosterEntry> entriesList = new ArrayList<RosterEntry>();
    RosterGroup rosterGroup = roster.getGroup(groupName);
    Collection<RosterEntry> rosterEntries = rosterGroup.getEntries();
    Iterator<RosterEntry> i = rosterEntries.iterator();
    while (i.hasNext()) {
        entriesList.add(i.next());
    }
    return entriesList;
}
 
Example #9
Source File: ContactList.java    From Spark with Apache License 2.0 5 votes vote down vote up
/**
 * Adds a single user to the ContactList.
 *
 * @param entry the <code>RosterEntry</code> of the the user.
 */
private void addUser(RosterEntry entry) {
    ContactItem newContactItem = UIComponentRegistry.createContactItem(entry.getName(), null, entry.getJid());

    if (entry.getType() == RosterPacket.ItemType.none || entry.getType() == RosterPacket.ItemType.from) {
        // Ignore, since the new user is pending to be added.
        for (RosterGroup group : entry.getGroups()) {
            ContactGroup contactGroup = getContactGroup(group.getName());
            if (contactGroup == null) {
                contactGroup = addContactGroup(group.getName());
            }

            boolean isPending = entry.getType() == RosterPacket.ItemType.none || entry.getType() == RosterPacket.ItemType.from
                && entry.isSubscriptionPending();
            if (isPending) {
                contactGroup.setVisible(true);
            }
            contactGroup.addContactItem(newContactItem);

        }
        return;
    }
    else {
        moveToOffline(newContactItem);
    }

    // Update users icon
    Presence presence = Roster.getInstanceFor( SparkManager.getConnection() ).getPresence(entry.getJid());
    try {
        updateUserPresence(presence);
    }
    catch (Exception e) {
        Log.error(e);
    }
}
 
Example #10
Source File: XmppConnection.java    From Zom-Android-XMPP with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected void doRemoveContactFromListAsync(Contact contact, ContactList list) {
    // FIXME synchronize this to executor thread
    if (mConnection == null)
        return;

    String address = contact.getAddress().getAddress();

    //otherwise, send unsub message and delete from local contact database
    org.jivesoftware.smack.packet.Presence presence = new org.jivesoftware.smack.packet.Presence(
            org.jivesoftware.smack.packet.Presence.Type.unsubscribe);
    presence.setTo(address);
    sendPacket(presence);

    presence = new org.jivesoftware.smack.packet.Presence(
            org.jivesoftware.smack.packet.Presence.Type.unsubscribed);
    presence.setTo(address);
    sendPacket(presence);

    try {
        RosterEntry entry = mRoster.getEntry(JidCreate.bareFrom(address));
        RosterGroup group = mRoster.getGroup(list.getName());

        if (entry != null) {
            if (group == null) {
                debug(TAG, "could not find group " + list.getName() + " in roster");
                if (mRoster != null)
                    mRoster.removeEntry(entry);

            } else {
                group.removeEntry(entry);
                entry = mRoster.getEntry(JidCreate.bareFrom(address));
                // Remove from Roster if this is the last group
                if (entry != null && entry.getGroups().size() <= 1)
                    mRoster.removeEntry(entry);

            }
        }
    } catch (Exception e) {
        debug(TAG, "remove entry failed: " + e.getMessage());
        throw new RuntimeException(e);
    }


    notifyContactListUpdated(list, ContactListListener.LIST_CONTACT_REMOVED, contact);
}
 
Example #11
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 #12
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 #13
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 #14
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 #15
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 #16
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;
}