Search in sources :

Example 1 with Default

use of org.jivesoftware.resource.Default in project Spark by igniterealtime.

the class ContactList method addSubscriptionListener.

public void addSubscriptionListener() {
    // Sometimes, presence changes happen in rapid succession (for instance, when initially connecting). To avoid
    // having a lot of UI-updates (which are costly), this queue is used to create a short buffer, allowing us to
    // group UI updates in batches.
    final ConcurrentLinkedQueue<Presence> presenceBuffer = new ConcurrentLinkedQueue<>();
    final long bufferTimeMS = 500;
    final StanzaListener subscribeListener = stanza -> {
        final Presence presence = (Presence) stanza;
        final Roster roster = Roster.getInstanceFor(SparkManager.getConnection());
        final RosterEntry entry = roster.getEntry(presence.getFrom());
        switch(presence.getType()) {
            case subscribe:
                // Someone else wants to subscribe to our presence. Ask user for approval
                SwingUtilities.invokeLater(() -> {
                    try {
                        subscriptionRequest(presence.getFrom());
                    } catch (SmackException.NotConnectedException e) {
                        Log.warning("Unable to process subscription request from: " + presence.getFrom(), e);
                    }
                });
                break;
            case unsubscribe:
                // Someone else is removing their subscription to our presence (we're removed from their roster).
                if (entry != null) {
                    try {
                        removeContactItem(presence.getFrom());
                        roster.removeEntry(entry);
                    } catch (XMPPException | SmackException e) {
                        Presence unsub = new Presence(Presence.Type.unsubscribed);
                        unsub.setTo(presence.getFrom());
                        try {
                            SparkManager.getConnection().sendStanza(unsub);
                        } catch (SmackException.NotConnectedException e1) {
                            Log.warning("Unable to unsubscribe from " + unsub.getTo(), e1);
                        }
                        Log.error(e);
                    }
                }
                break;
            case subscribed:
                // Someone else approved our request to be subscribed to their presence information.
                final String jid = XmppStringUtils.parseBareJid(presence.getFrom());
                final ContactItem item = getContactItemByJID(jid);
                // If item is not in the Contact List, add them.
                if (item == null && entry != null) {
                    final ContactItem newItem = UIComponentRegistry.createContactItem(entry.getName(), null, jid);
                    moveToOffline(newItem);
                    offlineGroup.fireContactGroupUpdated();
                }
                break;
            case unsubscribed:
                // Someone is telling us that we're no longer subscribed to their presence information.
                SwingUtilities.invokeLater(() -> {
                    if (entry != null) {
                        try {
                            removeContactItem(presence.getFrom());
                            roster.removeEntry(entry);
                        } catch (XMPPException | SmackException e) {
                            Log.error(e);
                        }
                    }
                    removeContactItem(XmppStringUtils.parseBareJid(presence.getFrom()));
                });
                break;
            default:
                // Any other presence updates. These are likely regular presence changes, not subscription-state changes.
                presenceBuffer.add(presence);
                TaskEngine.getInstance().schedule(new TimerTask() {

                    @Override
                    public void run() {
                        SwingUtilities.invokeLater(() -> {
                            final Iterator<Presence> iterator = presenceBuffer.iterator();
                            while (iterator.hasNext()) {
                                final Presence presence = iterator.next();
                                try {
                                    updateUserPresence(presence);
                                } catch (Exception e) {
                                    Log.warning("Unable to process this presence update that was received: " + presence, e);
                                } finally {
                                    iterator.remove();
                                }
                            }
                        });
                    }
                }, bufferTimeMS);
                break;
        }
    };
    SparkManager.getConnection().addAsyncStanzaListener(subscribeListener, new StanzaTypeFilter(Presence.class));
}
Also used : InputDialog(org.jivesoftware.spark.component.InputDialog) java.util(java.util) VCardManager(org.jivesoftware.sparkimpl.profile.VCardManager) ActionListener(java.awt.event.ActionListener) RosterPacket(org.jivesoftware.smack.roster.packet.RosterPacket) SettingsManager(org.jivesoftware.sparkimpl.settings.local.SettingsManager) Res(org.jivesoftware.resource.Res) Log(org.jivesoftware.spark.util.log.Log) MainWindowListener(org.jivesoftware.MainWindowListener) LastActivityManager(org.jivesoftware.smackx.iqlast.LastActivityManager) Plugin(org.jivesoftware.spark.plugin.Plugin) RosterListener(org.jivesoftware.smack.roster.RosterListener) ContextMenuListener(org.jivesoftware.spark.plugin.ContextMenuListener) VerticalFlowLayout(org.jivesoftware.spark.component.VerticalFlowLayout) Default(org.jivesoftware.resource.Default) PresenceManager(org.jivesoftware.spark.PresenceManager) RosterGroup(org.jivesoftware.smack.roster.RosterGroup) Enterprise(org.jivesoftware.sparkimpl.plugin.manager.Enterprise) RolloverButton(org.jivesoftware.spark.component.RolloverButton) org.jivesoftware.smack(org.jivesoftware.smack) Presence(org.jivesoftware.smack.packet.Presence) ChatManager(org.jivesoftware.spark.ChatManager) LocalPreferences(org.jivesoftware.sparkimpl.settings.local.LocalPreferences) SparkRes(org.jivesoftware.resource.SparkRes) FileOutputStream(java.io.FileOutputStream) SwingWorker(org.jivesoftware.spark.util.SwingWorker) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) KeyEvent(java.awt.event.KeyEvent) ActionEvent(java.awt.event.ActionEvent) XmppStringUtils(org.jxmpp.util.XmppStringUtils) MouseEvent(java.awt.event.MouseEvent) File(java.io.File) Spark(org.jivesoftware.Spark) java.awt(java.awt) Workspace(org.jivesoftware.spark.Workspace) StanzaTypeFilter(org.jivesoftware.smack.filter.StanzaTypeFilter) List(java.util.List) Roster(org.jivesoftware.smack.roster.Roster) SparkManager(org.jivesoftware.spark.SparkManager) RosterEntry(org.jivesoftware.smack.roster.RosterEntry) Message(org.jivesoftware.smack.packet.Message) SharedGroupManager(org.jivesoftware.smackx.sharedgroups.SharedGroupManager) org.jivesoftware.spark.util(org.jivesoftware.spark.util) LastActivity(org.jivesoftware.smackx.iqlast.packet.LastActivity) JivePropertiesExtension(org.jivesoftware.smackx.jiveproperties.packet.JivePropertiesExtension) javax.swing(javax.swing) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) IOException(java.io.IOException) StanzaTypeFilter(org.jivesoftware.smack.filter.StanzaTypeFilter) Roster(org.jivesoftware.smack.roster.Roster) Presence(org.jivesoftware.smack.packet.Presence) RosterEntry(org.jivesoftware.smack.roster.RosterEntry) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue)

Aggregations

java.awt (java.awt)1 ActionEvent (java.awt.event.ActionEvent)1 ActionListener (java.awt.event.ActionListener)1 KeyEvent (java.awt.event.KeyEvent)1 MouseEvent (java.awt.event.MouseEvent)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 java.util (java.util)1 List (java.util.List)1 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)1 javax.swing (javax.swing)1 MainWindowListener (org.jivesoftware.MainWindowListener)1 Spark (org.jivesoftware.Spark)1 Default (org.jivesoftware.resource.Default)1 Res (org.jivesoftware.resource.Res)1 SparkRes (org.jivesoftware.resource.SparkRes)1 org.jivesoftware.smack (org.jivesoftware.smack)1 StanzaTypeFilter (org.jivesoftware.smack.filter.StanzaTypeFilter)1