Java Code Examples for org.jivesoftware.smack.roster.RosterGroup#addEntry()

The following examples show how to use org.jivesoftware.smack.roster.RosterGroup#addEntry() . 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: 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 2
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 3
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 4
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();
}