Search in sources :

Example 21 with RosterEntry

use of org.jivesoftware.smack.roster.RosterEntry in project Spark by igniterealtime.

the class ChatRoom method handleNickNameCompletion.

/**
 * Handles the Nickname Completion dialog, when Pressing CTRL + SPACE<br>
 * it searches for matches in the current GroupchatList and also in the
 * Roster
 *
 * @throws ChatRoomNotFoundException
 *             when for some reason the GroupChatRoom cannot be found, this
 *             should <u>not</u> happen, since we retrieve it from the
 *             ActiveWindowTab and thus <u>can be ignored</u>
 */
private void handleNickNameCompletion() throws ChatRoomNotFoundException {
    // Search for a name that starts with the same word as the last word in the chat input editor.
    final String text = getChatInputEditor().getText();
    if (text == null || text.isEmpty()) {
        return;
    }
    // -1 when space does not occur.
    final int lastSpaceCharacterIndex = text.lastIndexOf(' ');
    final String needle = text.substring(lastSpaceCharacterIndex + 1);
    final Set<String> matches = new TreeSet<>(String::compareToIgnoreCase);
    if (SparkManager.getChatManager().getChatContainer().getActiveChatRoom() instanceof GroupChatRoom) {
        final GroupChatRoom activeChatRoom = (GroupChatRoom) SparkManager.getChatManager().getChatContainer().getActiveChatRoom();
        for (String participant : activeChatRoom.getParticipants()) {
            final String nickname = participant.substring(participant.lastIndexOf("/") + 1);
            if (nickname.toLowerCase().startsWith(needle.toLowerCase())) {
                matches.add(nickname);
            }
        }
    } else {
        for (RosterEntry re : Roster.getInstanceFor(SparkManager.getConnection()).getEntries()) {
            // Use the name if available, otherwise the localpart of the JID.
            final String username;
            if (re.getName() != null) {
                username = re.getName();
            } else {
                username = re.getUser().substring(0, re.getUser().indexOf('@'));
            }
            if (username.toLowerCase().startsWith(needle.toLowerCase())) {
                matches.add(username);
            }
        }
    }
    if (matches.size() == 1) {
        // If we only have 1 match, that match can be used immediately.
        getChatInputEditor().appendText(matches.iterator().next().substring(needle.length()));
    } else {
        // More than one match: create Popupmenu and let the user select one.
        final JPopupMenu popup = new JPopupMenu();
        for (final String match : matches) {
            final JMenuItem menuItem = new JMenuItem(match);
            popup.add(menuItem);
            menuItem.addActionListener(new AbstractAction() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    getChatInputEditor().appendText(match.substring(needle.length()));
                    popup.setVisible(false);
                }
            });
        }
        popup.show(SparkManager.getChatManager().getChatContainer(), getChatInputEditor().getCaret().getMagicCaretPosition().x, SparkManager.getChatManager().getChatContainer().getHeight() - 20);
    }
}
Also used : GroupChatRoom(org.jivesoftware.spark.ui.rooms.GroupChatRoom) RosterEntry(org.jivesoftware.smack.roster.RosterEntry)

Example 22 with RosterEntry

use of org.jivesoftware.smack.roster.RosterEntry in project Zom-Android by zom.

the class XmppConnection method initNewContactProcessor.

private void initNewContactProcessor() {
    mTimerNewContacts = new Timer();
    mTimerNewContacts.scheduleAtFixedRate(new TimerTask() {

        public void run() {
            try {
                Contact contact = null;
                if (qNewContact.size() > 0)
                    while (qNewContact.peek() != null) {
                        contact = qNewContact.poll();
                        if (mConnection == null || (!mConnection.isConnected())) {
                            debug(TAG, "postponed adding new contact" + " because we are not connected");
                            // return the packet to the stack
                            qNewContact.add(contact);
                            return;
                        } else {
                            try {
                                RosterEntry rEntry;
                                ContactList list = mContactListManager.getDefaultContactList();
                                String[] groups = new String[] { list.getName() };
                                BareJid jid = JidCreate.bareFrom(contact.getAddress().getBareAddress());
                                rEntry = mRoster.getEntry(jid);
                                RosterGroup rGroup = mRoster.getGroup(list.getName());
                                if (rGroup == null) {
                                    if (rEntry == null) {
                                        mRoster.createEntry(jid, contact.getName(), groups);
                                        while ((rEntry = mRoster.getEntry(jid)) == null) {
                                            try {
                                                Thread.sleep(500);
                                            } catch (Exception e) {
                                            }
                                        }
                                    }
                                } else if (rEntry == null) {
                                    mRoster.createEntry(jid, contact.getName(), groups);
                                    while ((rEntry = mRoster.getEntry(jid)) == null) {
                                        try {
                                            Thread.sleep(500);
                                        } catch (Exception e) {
                                        }
                                    }
                                }
                                int subStatus = Imps.Contacts.SUBSCRIPTION_STATUS_NONE;
                                if (rEntry.isSubscriptionPending())
                                    subStatus = Imps.Contacts.SUBSCRIPTION_STATUS_SUBSCRIBE_PENDING;
                                int subType = Imps.Contacts.SUBSCRIPTION_TYPE_NONE;
                                if (rEntry.canSeeHisPresence() && rEntry.canSeeMyPresence()) {
                                    subType = Imps.Contacts.SUBSCRIPTION_TYPE_BOTH;
                                } else if (rEntry.canSeeHisPresence()) {
                                    subType = Imps.Contacts.SUBSCRIPTION_TYPE_FROM;
                                    if (rEntry.isSubscriptionPending()) {
                                        try {
                                            handleSubscribeRequest(rEntry.getJid());
                                        } catch (Exception e) {
                                            debug(TAG, "Error requesting subscribe notification", e);
                                        }
                                    }
                                } else if (rEntry.canSeeMyPresence()) {
                                    // it is still pending
                                    subType = Imps.Contacts.SUBSCRIPTION_TYPE_TO;
                                }
                                contact.setSubscriptionType(subType);
                                contact.setSubscriptionStatus(subStatus);
                                try {
                                    mContactListManager.getSubscriptionRequestListener().onSubScriptionChanged(contact, mProviderId, mAccountId, subType, subStatus);
                                } catch (Exception e) {
                                }
                            } catch (XMPPException e) {
                                debug(TAG, "error updating remote roster", e);
                                // try again later
                                qNewContact.add(contact);
                            } catch (Exception e) {
                                String msg = "Not logged in to server while updating remote roster";
                                debug(TAG, msg, e);
                                // try again later
                                qNewContact.add(contact);
                            }
                        }
                    }
            } catch (Exception e) {
                Log.e(TAG, "error sending packet", e);
            }
        }
    }, 500, 500);
}
Also used : Timer(java.util.Timer) TimerTask(java.util.TimerTask) EntityBareJid(org.jxmpp.jid.EntityBareJid) BareJid(org.jxmpp.jid.BareJid) DomainBareJid(org.jxmpp.jid.DomainBareJid) RosterEntry(org.jivesoftware.smack.roster.RosterEntry) ContactList(org.awesomeapp.messenger.model.ContactList) XMPPException(org.jivesoftware.smack.XMPPException) KeyStoreException(java.security.KeyStoreException) UndecidedOmemoIdentityException(org.jivesoftware.smackx.omemo.exceptions.UndecidedOmemoIdentityException) XMPPException(org.jivesoftware.smack.XMPPException) RemoteException(android.os.RemoteException) IOException(java.io.IOException) ImException(org.awesomeapp.messenger.model.ImException) KeyManagementException(java.security.KeyManagementException) InvocationTargetException(java.lang.reflect.InvocationTargetException) XmppStringprepException(org.jxmpp.stringprep.XmppStringprepException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) SmackException(org.jivesoftware.smack.SmackException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) MultiUserChatException(org.jivesoftware.smackx.muc.MultiUserChatException) CryptoFailedException(org.jivesoftware.smackx.omemo.exceptions.CryptoFailedException) CertificateException(java.security.cert.CertificateException) OmemoFingerprint(org.jivesoftware.smackx.omemo.OmemoFingerprint) Contact(org.awesomeapp.messenger.model.Contact) RosterGroup(org.jivesoftware.smack.roster.RosterGroup)

Example 23 with RosterEntry

use of org.jivesoftware.smack.roster.RosterEntry in project xabber-android by redsolution.

the class RosterManager method removeContact.

/**
 * Requests contact removing.
 */
public void removeContact(AccountJid account, UserJid user) {
    final Roster roster = getRoster(account);
    if (roster == null) {
        return;
    }
    final RosterEntry entry = roster.getEntry(user.getJid().asBareJid());
    if (entry == null) {
        return;
    }
    Application.getInstance().runInBackgroundUserRequest(new Runnable() {

        @Override
        public void run() {
            try {
                roster.removeEntry(entry);
            } catch (SmackException.NotLoggedInException | SmackException.NotConnectedException e) {
                Application.getInstance().onError(R.string.NOT_CONNECTED);
            } catch (SmackException.NoResponseException e) {
                Application.getInstance().onError(R.string.CONNECTION_FAILED);
            } catch (XMPPException.XMPPErrorException e) {
                Application.getInstance().onError(R.string.XMPP_EXCEPTION);
            } catch (InterruptedException e) {
                LogManager.exception(LOG_TAG, e);
            }
        }
    });
}
Also used : Roster(org.jivesoftware.smack.roster.Roster) SmackException(org.jivesoftware.smack.SmackException) RosterEntry(org.jivesoftware.smack.roster.RosterEntry)

Example 24 with RosterEntry

use of org.jivesoftware.smack.roster.RosterEntry in project Smack by igniterealtime.

the class IntegrationTestRosterUtil method notInRoster.

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;
    }
    try {
        roster.removeEntry(c2Entry);
    } catch (XMPPErrorException e) {
        // Account for race conditions: server-sided, the item might already have been removed.
        if (e.getStanzaError().getCondition() == StanzaError.Condition.item_not_found) {
            // Trying to remove non-existing item. As it needs to be gone, this is fine.
            return;
        }
        throw e;
    }
}
Also used : XMPPErrorException(org.jivesoftware.smack.XMPPException.XMPPErrorException) Roster(org.jivesoftware.smack.roster.Roster) RosterEntry(org.jivesoftware.smack.roster.RosterEntry)

Example 25 with RosterEntry

use of org.jivesoftware.smack.roster.RosterEntry in project xabber-android by redsolution.

the class RosterManager method removeContact.

/**
     * Requests contact removing.
     *
     */
public void removeContact(String account, String bareAddress) {
    final Roster roster = getRoster(account);
    if (roster == null) {
        return;
    }
    final RosterEntry entry = roster.getEntry(bareAddress);
    if (entry == null) {
        return;
    }
    Application.getInstance().runInBackground(new Runnable() {

        @Override
        public void run() {
            try {
                roster.removeEntry(entry);
            } catch (SmackException.NotLoggedInException | SmackException.NotConnectedException e) {
                Application.getInstance().onError(R.string.NOT_CONNECTED);
            } catch (SmackException.NoResponseException e) {
                Application.getInstance().onError(R.string.CONNECTION_FAILED);
            } catch (XMPPException.XMPPErrorException e) {
                Application.getInstance().onError(R.string.XMPP_EXCEPTION);
            }
        }
    });
}
Also used : Roster(org.jivesoftware.smack.roster.Roster) SmackException(org.jivesoftware.smack.SmackException) RosterEntry(org.jivesoftware.smack.roster.RosterEntry)

Aggregations

RosterEntry (org.jivesoftware.smack.roster.RosterEntry)45 Roster (org.jivesoftware.smack.roster.Roster)35 RosterGroup (org.jivesoftware.smack.roster.RosterGroup)17 SmackException (org.jivesoftware.smack.SmackException)14 Presence (org.jivesoftware.smack.packet.Presence)13 XMPPException (org.jivesoftware.smack.XMPPException)12 IOException (java.io.IOException)6 SwingWorker (org.jivesoftware.spark.util.SwingWorker)4 RosterPacket (org.jivesoftware.smack.roster.packet.RosterPacket)3 AccountItem (com.xabber.android.data.account.AccountItem)2 ConnectionItem (com.xabber.android.data.connection.ConnectionItem)2 java.awt (java.awt)2 ActionEvent (java.awt.event.ActionEvent)2 ActionListener (java.awt.event.ActionListener)2 KeyEvent (java.awt.event.KeyEvent)2 java.util (java.util)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 javax.swing (javax.swing)2 Icon (javax.swing.Icon)2