Java Code Examples for org.jivesoftware.smack.roster.Roster#getInstanceFor()
The following examples show how to use
org.jivesoftware.smack.roster.Roster#getInstanceFor() .
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: RosterManager.java From mangosta-android with Apache License 2.0 | 6 votes |
public void addContact(String jidString) throws SmackException.NotLoggedInException, InterruptedException, SmackException.NotConnectedException, XMPPException.XMPPErrorException, SmackException.NoResponseException, XmppStringprepException { Roster roster = Roster.getInstanceFor(XMPPSession.getInstance().getXMPPConnection()); if (!roster.isLoaded()) { roster.reloadAndWait(); } BareJid jid = JidCreate.bareFrom(jidString); String name = XMPPUtils.fromJIDToUserName(jidString); String[] groups = new String[]{"Buddies"}; roster.createEntry(jid, name, groups); roster.sendSubscriptionRequest(jid); }
Example 2
Source File: Client.java From desktopclient-java with GNU General Public License v3.0 | 6 votes |
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 3
Source File: ContactList.java From Spark with Apache License 2.0 | 6 votes |
/** * 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: OmemoManagerSetupHelper.java From Smack with Apache License 2.0 | 6 votes |
public static void trustAllIdentities(OmemoManager alice, OmemoManager bob) throws InterruptedException, SmackException.NotConnectedException, SmackException.NotLoggedInException, SmackException.NoResponseException, CannotEstablishOmemoSessionException, CorruptedOmemoKeyException, XMPPException.XMPPErrorException, PubSubException.NotALeafNodeException, IOException { Roster roster = Roster.getInstanceFor(alice.getConnection()); if (alice.getOwnJid() != bob.getOwnJid() && (!roster.iAmSubscribedTo(bob.getOwnJid()) || !roster.isSubscribedToMyPresence(bob.getOwnJid()))) { throw new IllegalStateException("Before trusting identities of a user, we must be subscribed to one another."); } alice.requestDeviceListUpdateFor(bob.getOwnJid()); HashMap<OmemoDevice, OmemoFingerprint> fingerprints = alice.getActiveFingerprints(bob.getOwnJid()); for (OmemoDevice device : fingerprints.keySet()) { OmemoFingerprint fingerprint = fingerprints.get(device); alice.trustOmemoIdentity(device, fingerprint); } }
Example 5
Source File: TransferGroupUI.java From Spark with Apache License 2.0 | 6 votes |
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 6
Source File: SoftPhonePlugin.java From Spark with Apache License 2.0 | 5 votes |
private void loadVCards() { // Load vCard information. final Roster roster = Roster.getInstanceFor( SparkManager.getConnection() ); for (RosterEntry entry : roster.getEntries()) { SparkManager.getVCardManager().getVCardFromMemory(entry.getUser()); } }
Example 7
Source File: RosterManager.java From mangosta-android with Apache License 2.0 | 5 votes |
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 8
Source File: UriManager.java From Spark with Apache License 2.0 | 5 votes |
/** * 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 9
Source File: OmemoManagerSetupHelper.java From Smack with Apache License 2.0 | 5 votes |
public static void cleanUpRoster(OmemoManager omemoManager) { Roster roster = Roster.getInstanceFor(omemoManager.getConnection()); for (RosterEntry r : roster.getEntries()) { try { roster.removeEntry(r); } catch (InterruptedException | SmackException.NoResponseException | SmackException.NotConnectedException | XMPPException.XMPPErrorException | SmackException.NotLoggedInException e) { // Silent } } }
Example 10
Source File: PresenceManager.java From Spark with Apache License 2.0 | 5 votes |
/** * Returns the presence of a user. * * @param jidString the users jid. * @return the users presence. */ public static Presence getPresence(BareJid jid) { if (jid.equals(SparkManager.getSessionManager().getBareAddress())) { return SparkManager.getWorkspace().getStatusBar().getPresence(); } else { final Roster roster = Roster.getInstanceFor( SparkManager.getConnection() ); return roster.getPresence(jid); } }
Example 11
Source File: IoT.java From Smack with Apache License 2.0 | 5 votes |
@Override public void iotScenario(XMPPTCPConnection dataThingConnection, XMPPTCPConnection readingThingConnection) throws TimeoutException, Exception { ThingState dataThingState = actAsDataThing(dataThingConnection); final SimpleResultSyncPoint syncPoint = new SimpleResultSyncPoint(); dataThingState.setThingStateChangeListener(new AbstractThingStateChangeListener() { @Override public void owned(BareJid jid) { syncPoint.signal(); } }); // Wait until the thing is owned. syncPoint.waitForResult(TIMEOUT); printStatus("OWNED - Thing now owned by " + dataThingState.getOwner()); // Make sure things are befriended. IoTProvisioningManager readingThingProvisioningManager = IoTProvisioningManager.getInstanceFor(readingThingConnection); readingThingProvisioningManager.sendFriendshipRequestIfRequired(dataThingConnection.getUser().asBareJid()); Roster dataThingRoster = Roster.getInstanceFor(dataThingConnection); RosterUtil.waitUntilOtherEntityIsSubscribed(dataThingRoster, readingThingConnection.getUser().asBareJid(), TIMEOUT); printStatus("FRIENDSHIP ACCEPTED - Trying to read out data"); IoTDataManager readingThingDataManager = IoTDataManager.getInstanceFor(readingThingConnection); List<IoTFieldsExtension> values = readingThingDataManager.requestMomentaryValuesReadOut(dataThingConnection.getUser()); if (values.size() != 1) { throw new IllegalStateException("Unexpected number of values returned: " + values.size()); } IoTFieldsExtension field = values.get(0); printStatus("DATA READ-OUT SUCCESS: " + field.toXML()); printStatus("IoT SCENARIO FINISHED SUCCESSFULLY"); }
Example 12
Source File: ContactList.java From Spark with Apache License 2.0 | 5 votes |
/** * 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 13
Source File: ContactListAssistantPlugin.java From Spark with Apache License 2.0 | 4 votes |
/** * 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: ContactGroupTransferHandler.java From Spark with Apache License 2.0 | 4 votes |
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 15
Source File: RosterDialog.java From Spark with Apache License 2.0 | 4 votes |
/** * 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 16
Source File: ContactList.java From Spark with Apache License 2.0 | 4 votes |
@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 17
Source File: IntegrationTestRosterUtil.java From Smack with Apache License 2.0 | 4 votes |
public static void ensureSubscribedTo(final XMPPConnection presenceRequestReceiverConnection, final XMPPConnection presenceRequestingConnection, long timeout) throws TimeoutException, Exception { final Roster presenceRequestReceiverRoster = Roster.getInstanceFor(presenceRequestReceiverConnection); final Roster presenceRequestingRoster = Roster.getInstanceFor(presenceRequestingConnection); final EntityFullJid presenceRequestReceiverAddress = presenceRequestReceiverConnection.getUser(); final EntityFullJid presenceRequestingAddress = presenceRequestingConnection.getUser(); if (presenceRequestReceiverRoster.isSubscribedToMyPresence(presenceRequestingAddress)) { return; } final SubscribeListener subscribeListener = new SubscribeListener() { @Override public SubscribeAnswer processSubscribe(Jid from, Presence subscribeRequest) { if (from.equals(presenceRequestingConnection.getUser().asBareJid())) { return SubscribeAnswer.Approve; } return SubscribeAnswer.Deny; } }; presenceRequestReceiverRoster.addSubscribeListener(subscribeListener); final SimpleResultSyncPoint syncPoint = new SimpleResultSyncPoint(); final PresenceEventListener presenceEventListener = new AbstractPresenceEventListener() { @Override public void presenceSubscribed(BareJid address, Presence subscribedPresence) { if (!address.equals(presenceRequestReceiverAddress.asBareJid())) { return; } syncPoint.signal(); } }; presenceRequestingRoster.addPresenceEventListener(presenceEventListener); try { presenceRequestingRoster.sendSubscriptionRequest(presenceRequestReceiverAddress.asBareJid()); syncPoint.waitForResult(timeout); } finally { presenceRequestReceiverRoster.removeSubscribeListener(subscribeListener); presenceRequestingRoster.removePresenceEventListener(presenceEventListener); } }
Example 18
Source File: XMPPManager.java From Yahala-Messenger with MIT License | 4 votes |
public void createEntry(String user, String name) throws Exception { // FileLog.e("Test", String.format("Creating entry for buddy '%1$s' with name %2$s", user, name)); Roster roster = Roster.getInstanceFor(connection); roster.createEntry(JidCreate.bareFrom(user), name, null); }
Example 19
Source File: ChatRoomImpl.java From Spark with Apache License 2.0 | 4 votes |
/** * Constructs a 1-to-1 ChatRoom. * * Note that the participantJID value can be a bare JID, or a full JID. In regular one-on-one chats, a bare JID is * expected. This instance will then display relevant data sent by any of the (full) JIDs associated to the bare JID. * When this instance is created to reflect a private message in MUC context, a full JID is expected to be provided * as the participantJID value (room@service/nick). In such case, only data sent from that full JID is displayed. * * @param participantJID the participants jid to chat with. * @param participantNickname the nickname of the participant. * @param title the title of the room. */ public ChatRoomImpl(final EntityJid participantJID, Resourcepart participantNickname, CharSequence title, boolean initUi) { this.active = true; //activateNotificationTime = System.currentTimeMillis(); setparticipantJID(participantJID); this.participantNickname = participantNickname; // Loads the current history for this user. loadHistory(); // Register StanzaListeners final StanzaFilter directFilter = new AndFilter( FromMatchesFilter.create( participantJID ), new OrFilter( new StanzaTypeFilter( Presence.class ), new StanzaTypeFilter( Message.class ) ) ); final StanzaFilter carbonFilter = new AndFilter( FromMatchesFilter.create( SparkManager.getSessionManager().getBareUserAddress() ), // Security Consideration, see https://xmpp.org/extensions/xep-0280.html#security new StanzaTypeFilter( Message.class ), new OrFilter( new StanzaExtensionFilter( "sent", CarbonExtension.NAMESPACE ), new StanzaExtensionFilter( "received", CarbonExtension.NAMESPACE ) ) ); SparkManager.getConnection().addSyncStanzaListener( this, new OrFilter( directFilter, carbonFilter ) ); // Use the agents username as the Tab Title this.tabTitle = title.toString(); // The name of the room will be the node of the user jid + conversation. this.roomTitle = participantNickname.toString(); // Add RoomInfo this.getSplitPane().setRightComponent(null); getSplitPane().setDividerSize(0); presence = PresenceManager.getPresence(participantJID.asBareJid()); roster = Roster.getInstanceFor( SparkManager.getConnection() ); RosterEntry entry = roster.getEntry(participantJID.asBareJid()); tabIcon = PresenceManager.getIconFromPresence(presence); if (initUi) { // Create toolbar buttons. infoButton = new ChatRoomButton("", SparkRes.getImageIcon(SparkRes.PROFILE_IMAGE_24x24)); infoButton.setToolTipText(Res.getString("message.view.information.about.this.user")); // Create basic toolbar. addChatRoomButton(infoButton); // Show VCard. infoButton.addActionListener(this); } // If this is a private chat from a group chat room, do not show toolbar. privateChat = participantNickname.equals(participantJID.getResourceOrNull()); if ( privateChat ) { getToolBar().setVisible(false); } // If the user is not in the roster, then allow user to add them. addToRosterButton = new ChatRoomButton("", SparkRes.getImageIcon(SparkRes.ADD_IMAGE_24x24)); if (entry == null && !privateChat) { addToRosterButton.setToolTipText(Res.getString("message.add.this.user.to.your.roster")); if (!Default.getBoolean("ADD_CONTACT_DISABLED") && Enterprise.containsFeature(Enterprise.ADD_CONTACTS_FEATURE)) addChatRoomButton(addToRosterButton); addToRosterButton.addActionListener(this); } lastActivity = System.currentTimeMillis(); }
Example 20
Source File: PresenceManager.java From Spark with Apache License 2.0 | 2 votes |
/** * Returns true if the user is online. * * @param jidString the jid of the user. * @return true if online. */ public static boolean isOnline(BareJid jid) { final Roster roster = Roster.getInstanceFor( SparkManager.getConnection() ); Presence presence = roster.getPresence(jid); return presence.isAvailable(); }