Search in sources :

Example 6 with Transport

use of org.jivesoftware.sparkimpl.plugin.gateways.transports.Transport in project Spark by igniterealtime.

the class SubscriptionDialog method invoke.

public void invoke(final String jid) throws SmackException.NotConnectedException {
    this.jid = jid;
    final Roster roster = Roster.getInstanceFor(SparkManager.getConnection());
    // If User is already in roster, do not show.
    RosterEntry entry = roster.getEntry(jid);
    if (entry != null && entry.getType() == RosterPacket.ItemType.to) {
        Presence response = new Presence(Presence.Type.subscribed);
        response.setTo(jid);
        SparkManager.getConnection().sendStanza(response);
        return;
    }
    String message = Res.getString("message.approve.subscription", UserManager.unescapeJID(jid));
    Transport transport = TransportUtils.getTransport(XmppStringUtils.parseDomain(jid));
    Icon icon = null;
    if (transport != null) {
        icon = transport.getIcon();
    }
    TitlePanel messageLabel = new TitlePanel("", message, icon, true);
    // Add Message Label
    mainPanel.add(messageLabel, new GridBagConstraints(0, 0, 6, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
    UserManager userManager = SparkManager.getUserManager();
    String username = userManager.getNickname(userManager.getFullJID(jid));
    username = username == null ? XmppStringUtils.parseLocalpart(UserManager.unescapeJID(jid)) : username;
    usernameLabelValue.setText(UserManager.unescapeJID(jid));
    nicknameField.setText(username);
    acceptButton.addActionListener(e -> {
        if (!rosterBox.isSelected()) {
            Presence response = new Presence(Presence.Type.subscribed);
            response.setTo(jid);
            try {
                SparkManager.getConnection().sendStanza(response);
            } catch (SmackException.NotConnectedException e1) {
                Log.warning("Unable to send stanza accepting subscription from " + jid, e1);
            }
            dialog.dispose();
            return;
        }
        boolean addEntry = addEntry();
        if (addEntry) {
            Presence response = new Presence(Presence.Type.subscribed);
            response.setTo(jid);
            try {
                SparkManager.getConnection().sendStanza(response);
            } catch (SmackException.NotConnectedException e1) {
                Log.warning("Unable to send stanza accepting subscription from " + jid, e1);
            }
        } else {
            dialog.dispose();
        }
    });
    denyButton.addActionListener(e -> {
        // Send subscribed
        unsubscribeAndClose();
    });
    viewInfoButton.addActionListener(e -> SparkManager.getVCardManager().viewProfile(jid, mainPanel));
    dialog = new JFrame(Res.getString("title.subscription.request")) {

        private static final long serialVersionUID = 5713933518069623228L;

        public Dimension getPreferredSize() {
            final Dimension dim = super.getPreferredSize();
            dim.width = 400;
            return dim;
        }
    };
    dialog.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    dialog.addWindowListener(new WindowAdapter() {

        @Override
        public void windowClosing(WindowEvent e) {
            super.windowClosing(e);
            unsubscribeAndClose();
        }
    });
    KeyStroke key = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
    ActionListener action = e -> unsubscribeAndClose();
    dialog.getRootPane().registerKeyboardAction(action, key, JComponent.WHEN_FOCUSED);
    dialog.setIconImage(SparkManager.getApplicationImage().getImage());
    dialog.getContentPane().add(mainPanel);
    dialog.pack();
    dialog.setLocationRelativeTo(SparkManager.getMainWindow());
    if (SparkManager.getMainWindow().isFocused()) {
        dialog.setState(Frame.NORMAL);
        dialog.setVisible(true);
    } else if (!SparkManager.getMainWindow().isVisible() || !SparkManager.getMainWindow().isFocused()) {
        if (Spark.isWindows()) {
            dialog.setFocusable(false);
            dialog.setState(Frame.ICONIFIED);
        }
        SparkManager.getNativeManager().flashWindowStopOnFocus(dialog);
        dialog.setVisible(true);
    }
}
Also used : SmackException(org.jivesoftware.smack.SmackException) UserManager(org.jivesoftware.spark.UserManager) UIManager(javax.swing.UIManager) Insets(java.awt.Insets) ActionListener(java.awt.event.ActionListener) RosterPacket(org.jivesoftware.smack.roster.packet.RosterPacket) JTextField(javax.swing.JTextField) Res(org.jivesoftware.resource.Res) Transport(org.jivesoftware.sparkimpl.plugin.gateways.transports.Transport) Log(org.jivesoftware.spark.util.log.Log) ResourceUtils(org.jivesoftware.spark.util.ResourceUtils) RosterGroup(org.jivesoftware.smack.roster.RosterGroup) JComboBox(javax.swing.JComboBox) JFrame(javax.swing.JFrame) RolloverButton(org.jivesoftware.spark.component.RolloverButton) JComponent(javax.swing.JComponent) KeyStroke(javax.swing.KeyStroke) Presence(org.jivesoftware.smack.packet.Presence) ModelUtil(org.jivesoftware.spark.util.ModelUtil) Frame(java.awt.Frame) BorderFactory(javax.swing.BorderFactory) Icon(javax.swing.Icon) JOptionPane(javax.swing.JOptionPane) GridBagConstraints(java.awt.GridBagConstraints) KeyEvent(java.awt.event.KeyEvent) WindowAdapter(java.awt.event.WindowAdapter) TitlePanel(org.jivesoftware.spark.component.TitlePanel) XmppStringUtils(org.jxmpp.util.XmppStringUtils) WindowEvent(java.awt.event.WindowEvent) Spark(org.jivesoftware.Spark) TransportUtils(org.jivesoftware.sparkimpl.plugin.gateways.transports.TransportUtils) Dimension(java.awt.Dimension) Roster(org.jivesoftware.smack.roster.Roster) SparkManager(org.jivesoftware.spark.SparkManager) JLabel(javax.swing.JLabel) JCheckBox(javax.swing.JCheckBox) RosterEntry(org.jivesoftware.smack.roster.RosterEntry) GridBagLayout(java.awt.GridBagLayout) JPanel(javax.swing.JPanel) XMPPException(org.jivesoftware.smack.XMPPException) ComponentTitledBorder(org.jivesoftware.spark.component.borders.ComponentTitledBorder) GridBagConstraints(java.awt.GridBagConstraints) Insets(java.awt.Insets) SmackException(org.jivesoftware.smack.SmackException) WindowAdapter(java.awt.event.WindowAdapter) Dimension(java.awt.Dimension) TitlePanel(org.jivesoftware.spark.component.TitlePanel) Roster(org.jivesoftware.smack.roster.Roster) ActionListener(java.awt.event.ActionListener) JFrame(javax.swing.JFrame) UserManager(org.jivesoftware.spark.UserManager) WindowEvent(java.awt.event.WindowEvent) KeyStroke(javax.swing.KeyStroke) Presence(org.jivesoftware.smack.packet.Presence) RosterEntry(org.jivesoftware.smack.roster.RosterEntry) Icon(javax.swing.Icon) Transport(org.jivesoftware.sparkimpl.plugin.gateways.transports.Transport)

Aggregations

Transport (org.jivesoftware.sparkimpl.plugin.gateways.transports.Transport)6 SmackException (org.jivesoftware.smack.SmackException)4 GridBagConstraints (java.awt.GridBagConstraints)2 GridBagLayout (java.awt.GridBagLayout)2 Insets (java.awt.Insets)2 ActionListener (java.awt.event.ActionListener)2 JCheckBox (javax.swing.JCheckBox)2 JLabel (javax.swing.JLabel)2 JOptionPane (javax.swing.JOptionPane)2 JPanel (javax.swing.JPanel)2 UIManager (javax.swing.UIManager)2 Res (org.jivesoftware.resource.Res)2 Presence (org.jivesoftware.smack.packet.Presence)2 SparkManager (org.jivesoftware.spark.SparkManager)2 RolloverButton (org.jivesoftware.spark.component.RolloverButton)2 Log (org.jivesoftware.spark.util.log.Log)2 TransportUtils (org.jivesoftware.sparkimpl.plugin.gateways.transports.TransportUtils)2 Color (java.awt.Color)1 Dimension (java.awt.Dimension)1 EventQueue (java.awt.EventQueue)1