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);
}
}
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);
}
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);
}
}
});
}
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;
}
}
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);
}
}
});
}
Aggregations