Search in sources :

Example 1 with Transport

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

the class RosterDialog method addEntry.

private void addEntry() {
    Transport transport = null;
    AccountItem item;
    if (publicBox.isSelected()) {
        item = (AccountItem) accounts.getSelectedItem();
        transport = item.getTransport();
    }
    if (transport == null) {
        String jid = getJID();
        if (!jid.contains("@")) {
            jid = jid + "@" + SparkManager.getConnection().getServiceName();
        }
        String nickname = nicknameField.getText();
        String group = (String) groupBox.getSelectedItem();
        jid = UserManager.escapeJID(jid);
        // Add as a new entry
        addRosterEntry(jid, nickname, group);
    } else {
        String jid = getJID();
        try {
            jid = Gateway.getJID(transport.getServiceName(), jid);
        } catch (SmackException e) {
            Log.error(e);
        }
        String nickname = nicknameField.getText();
        String group = (String) groupBox.getSelectedItem();
        addRosterEntry(jid, nickname, group);
    }
}
Also used : SmackException(org.jivesoftware.smack.SmackException) Transport(org.jivesoftware.sparkimpl.plugin.gateways.transports.Transport)

Example 2 with Transport

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

the class RosterDialog method addContactButton.

/**
 * Method to handle the Add-Button
 */
private void addContactButton() {
    String errorMessage = Res.getString("title.error");
    String jid = getJID();
    UIManager.put("OptionPane.okButtonText", Res.getString("ok"));
    if (jid.length() == 0) {
        JOptionPane.showMessageDialog(dialog, Res.getString("message.invalid.jid.error"), Res.getString("title.error"), JOptionPane.ERROR_MESSAGE);
        return;
    }
    String contact = UserManager.escapeJID(jid);
    String nickname = nicknameField.getText();
    String group = (String) groupBox.getSelectedItem();
    Transport transport = null;
    if (publicBox.isSelected()) {
        AccountItem item = (AccountItem) accounts.getSelectedItem();
        transport = item.getTransport();
    }
    if (transport == null) {
        if (!contact.contains("@")) {
            contact = contact + "@" + SparkManager.getConnection().getServiceName();
        }
    } else {
        if (!contact.contains("@")) {
            contact = contact + "@" + transport.getServiceName();
        }
    }
    if (!ModelUtil.hasLength(nickname) && ModelUtil.hasLength(contact)) {
        // Try to load nickname from VCard
        VCard vcard = new VCard();
        try {
            vcard.load(SparkManager.getConnection(), contact);
            nickname = vcard.getNickName();
        } catch (XMPPException | SmackException e1) {
            Log.error(e1);
        }
        // If no nickname, use first name.
        if (!ModelUtil.hasLength(nickname)) {
            nickname = XmppStringUtils.parseLocalpart(contact);
        }
        nicknameField.setText(nickname);
    }
    ContactGroup contactGroup = contactList.getContactGroup(group);
    boolean isSharedGroup = contactGroup != null && contactGroup.isSharedGroup();
    if (isSharedGroup) {
        errorMessage = Res.getString("message.cannot.add.contact.to.shared.group");
    } else if (!ModelUtil.hasLength(contact)) {
        errorMessage = Res.getString("message.specify.contact.jid");
    } else if (!XmppStringUtils.parseBareJid(contact).contains("@")) {
        errorMessage = Res.getString("message.invalid.jid.error");
    } else if (!ModelUtil.hasLength(group)) {
        errorMessage = Res.getString("message.specify.group");
    } else if (ModelUtil.hasLength(contact) && ModelUtil.hasLength(group) && !isSharedGroup) {
        addEntry();
        dialog.setVisible(false);
    } else {
        JOptionPane.showMessageDialog(dialog, errorMessage, Res.getString("title.error"), JOptionPane.ERROR_MESSAGE);
    }
}
Also used : SmackException(org.jivesoftware.smack.SmackException) Transport(org.jivesoftware.sparkimpl.plugin.gateways.transports.Transport) XMPPException(org.jivesoftware.smack.XMPPException) VCard(org.jivesoftware.smackx.vcardtemp.packet.VCard)

Example 3 with Transport

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

the class GatewayTabItem method createTransportMenu.

private void createTransportMenu() {
    _signInOut.addActionListener(e -> {
        if (signedIn) {
            final Presence offlinePresence = new Presence(Presence.Type.unavailable);
            offlinePresence.setTo(_transport.getServiceName());
            try {
                SparkManager.getConnection().sendStanza(offlinePresence);
                _statusIcon.setIcon(SparkRes.getImageIcon(SparkRes.YELLOW_BALL));
            } catch (SmackException.NotConnectedException e1) {
                Log.error("Unable to send presence.", e1);
            }
        } else {
            final Presence onlinePresence = new Presence(Presence.Type.available);
            onlinePresence.setTo(_transport.getServiceName());
            try {
                SparkManager.getConnection().sendStanza(onlinePresence);
                _statusIcon.setIcon(SparkRes.getImageIcon(SparkRes.YELLOW_BALL));
            } catch (SmackException.NotConnectedException e1) {
                Log.error("Unable to send presence.", e1);
            }
        }
    });
    // If transport is registered, we can check if the autojoin is enabled
    if (_transportRegistered) {
        _autoJoin.setSelected(TransportUtils.autoJoinService(_transport.getServiceName()));
        _registerButton.setText(Res.getString("menuitem.delete.login.information"));
        _signInOut.setEnabled(true);
    } else {
        setNotRegistered();
    }
    _autoJoinButton.addActionListener(e -> {
        _autoJoin.setSelected(!_autoJoin.isSelected());
        TransportUtils.setAutoJoin(_transport.getServiceName(), _autoJoin.isSelected());
    });
    _registerButton.addActionListener(e -> {
        // If transport is registered we should show the
        // "delete information" gui
        UIManager.put("OptionPane.yesButtonText", Res.getString("yes"));
        UIManager.put("OptionPane.noButtonText", Res.getString("no"));
        UIManager.put("OptionPane.cancelButtonText", Res.getString("cancel"));
        if (TransportUtils.isRegistered(SparkManager.getConnection(), _transport)) {
            int confirm = JOptionPane.showConfirmDialog(SparkManager.getMainWindow(), Res.getString("message.disable.transport", _transport.getName()), Res.getString("title.disable.transport"), JOptionPane.YES_NO_OPTION);
            if (confirm == JOptionPane.YES_OPTION) {
                try {
                    TransportUtils.unregister(SparkManager.getConnection(), _transport.getServiceName());
                    setNotRegistered();
                } catch (SmackException e1) {
                    Log.error(e1);
                }
            }
        } else {
            // If transport is not registered we should show the
            // register gui
            TransportRegistrationDialog registrationDialog = new TransportRegistrationDialog(_transport.getServiceName());
            registrationDialog.invoke();
            // Set user as offline while he fills in the login
            // information
            setOffline();
            ActionListener al = e1 -> {
                // If user canceled the register window, he is sill
                // not registrated
                setNotRegistered();
            };
            registrationDialog.addCancelActionListener(al);
        }
    });
    _autoJoinButton.setText(Res.getString("menuitem.sign.in.at.login"));
    _autoJoinButton.setHorizontalAlignment(SwingConstants.LEFT);
    // JPanel signPanel = new JPanel(new BorderLayout());
    // signPanel.setBackground(Color.lightGray);
    // signPanel.add(_statusIcon, BorderLayout.WEST);
    // signPanel.add(_signInOut, BorderLayout.CENTER);
    _registerButton.setHorizontalAlignment(SwingConstants.LEFT);
    _signInOut.setHorizontalAlignment(SwingConstants.LEFT);
    _listPanel.add(new JLabel(), new GridBagConstraints(0, 0, 1, 1, 0.1, 0.0, GridBagConstraints.PAGE_START, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
    _listPanel.add(_statusIcon, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 3, 0, 0), 0, 0));
    _listPanel.add(_signInOut, new GridBagConstraints(2, 0, 1, 1, 0.9, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 5, 0, 0), 0, 0));
    _listPanel.add(_autoJoin, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
    _listPanel.add(_autoJoinButton, new GridBagConstraints(2, 1, 1, 1, 0.9, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 5, 0, 0), 0, 0));
    _listPanel.add(_registerButton, new GridBagConstraints(2, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 5, 0, 0), 0, 0));
// _listPanel.add(_autoJoin);
// _listPanel.add(_registerButton);
}
Also used : Color(java.awt.Color) SmackException(org.jivesoftware.smack.SmackException) UIManager(javax.swing.UIManager) Insets(java.awt.Insets) ActionListener(java.awt.event.ActionListener) Res(org.jivesoftware.resource.Res) Transport(org.jivesoftware.sparkimpl.plugin.gateways.transports.Transport) Log(org.jivesoftware.spark.util.log.Log) SwingConstants(javax.swing.SwingConstants) CollapsiblePane(org.jivesoftware.spark.component.panes.CollapsiblePane) StatusBar(org.jivesoftware.spark.ui.status.StatusBar) PresenceManager(org.jivesoftware.spark.PresenceManager) RolloverButton(org.jivesoftware.spark.component.RolloverButton) EventQueue(java.awt.EventQueue) TaskEngine(org.jivesoftware.spark.util.TaskEngine) Presence(org.jivesoftware.smack.packet.Presence) JPanelRenderer(org.jivesoftware.spark.component.renderer.JPanelRenderer) Font(java.awt.Font) SparkRes(org.jivesoftware.resource.SparkRes) JList(javax.swing.JList) GridBagConstraints(java.awt.GridBagConstraints) JOptionPane(javax.swing.JOptionPane) TransportUtils(org.jivesoftware.sparkimpl.plugin.gateways.transports.TransportUtils) DefaultListModel(javax.swing.DefaultListModel) JLabel(javax.swing.JLabel) SparkManager(org.jivesoftware.spark.SparkManager) JCheckBox(javax.swing.JCheckBox) GridBagLayout(java.awt.GridBagLayout) JPanel(javax.swing.JPanel) GridBagConstraints(java.awt.GridBagConstraints) Insets(java.awt.Insets) ActionListener(java.awt.event.ActionListener) SmackException(org.jivesoftware.smack.SmackException) Presence(org.jivesoftware.smack.packet.Presence) JLabel(javax.swing.JLabel)

Example 4 with Transport

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

the class RosterDialog method getAccounts.

public List<AccountItem> getAccounts() {
    List<AccountItem> list = new ArrayList<>();
    for (Transport transport : TransportUtils.getTransports()) {
        if (TransportUtils.isRegistered(SparkManager.getConnection(), transport)) {
            AccountItem item = new AccountItem(transport.getIcon(), transport.getName(), transport);
            list.add(item);
        }
    }
    return list;
}
Also used : ArrayList(java.util.ArrayList) Transport(org.jivesoftware.sparkimpl.plugin.gateways.transports.Transport)

Example 5 with Transport

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

the class ContactInfoWindow method customizeUI.

public void customizeUI(ContactItem contactItem) {
    if (contactItem == null) {
        return;
    }
    nicknameLabel.setText(contactItem.getDisplayName());
    String status = contactItem.getStatus();
    if (!ModelUtil.hasLength(status)) {
        if (contactItem.getPresence() == null || contactItem.getPresence().getType() == Presence.Type.unavailable) {
            status = Res.getString("offline");
        } else {
            status = Res.getString("online");
        }
    }
    if (status.equals(Res.getString("offline")) || contactItem.getPresence().isAway()) {
        try {
            String client = "";
            if (!status.equals(Res.getString("offline"))) {
                // If user is away (not offline), last activity request is sent to client
                client = contactItem.getPresence().getFrom();
                if ((client != null) && (client.lastIndexOf("/") != -1)) {
                    client = client.substring(client.lastIndexOf("/"));
                } else
                    client = "/";
            }
            LastActivity activity = LastActivityManager.getInstanceFor(SparkManager.getConnection()).getLastActivity(contactItem.getJID() + client);
            long idleTime = (activity.getIdleTime() * 1000);
            if (idleTime > 0) {
                if (status.equals(Res.getString("offline"))) {
                    SimpleDateFormat format = new SimpleDateFormat("M/d/yy");
                    Date l = new Date();
                    String curDay = format.format(l);
                    l.setTime(l.getTime() - idleTime);
                    // If idleTime is within today show the time, otherwise, show the day, date, and time
                    if (curDay.equals(format.format(l))) {
                        format = new SimpleDateFormat("h:mm a");
                    } else {
                        format = new SimpleDateFormat("EEE M/d/yy h:mm a");
                    }
                    status += (" since " + format.format(l));
                } else if (contactItem.getPresence().isAway()) {
                    status += "\n";
                    String time = ModelUtil.getTimeFromLong(idleTime);
                    status += Res.getString("message.idle.for", time);
                }
            }
        } catch (Exception e1) {
            Log.warning("Unable to get Last Activity from: " + contactItem.toString(), e1);
        }
    }
    statusLabel.setText(status);
    Transport transport = TransportUtils.getTransport(XmppStringUtils.parseDomain(contactItem.getJID()));
    if (transport != null) {
        fullJIDLabel.setIcon(transport.getIcon());
        String name = XmppStringUtils.parseLocalpart(contactItem.getJID());
        name = XmppStringUtils.unescapeLocalpart(name);
        fullJIDLabel.setText(transport.getName() + " - " + name);
    } else {
        String name = XmppStringUtils.unescapeLocalpart(contactItem.getJID());
        fullJIDLabel.setText(name);
        fullJIDLabel.setIcon(null);
    }
    avatarLabel.setBorder(null);
    try {
        URL avatarURL = contactItem.getAvatarURL();
        ImageIcon icon = null;
        if (avatarURL != null) {
            icon = new ImageIcon(avatarURL);
        }
        if (icon != null && icon.getIconHeight() > 1) {
            icon = GraphicUtils.scaleImageIcon(icon, 96, 96);
            avatarLabel.setIcon(icon);
        } else {
            icon = SparkRes.getImageIcon(SparkRes.DEFAULT_AVATAR_64x64_IMAGE);
            avatarLabel.setIcon(icon);
        }
        avatarLabel.setBorder(BorderFactory.createBevelBorder(0, Color.white, Color.lightGray));
    } catch (MalformedURLException e) {
        Log.error(e);
    }
    // Get VCard from memory (if available)
    String title = "";
    String phone = "";
    VCard vcard = SparkManager.getVCardManager().getVCardFromMemory(XmppStringUtils.parseBareJid(contactItem.getJID()));
    if (vcard != null) {
        title = vcard.getField("TITLE");
        phone = vcard.getPhoneWork("VOICE");
        if (!ModelUtil.hasLength(title)) {
            title = "";
        }
        if (!ModelUtil.hasLength(phone)) {
            phone = "";
        }
    }
    titleLabel.setText(title);
    phoneLabel.setText(phone);
}
Also used : ImageIcon(javax.swing.ImageIcon) MalformedURLException(java.net.MalformedURLException) Transport(org.jivesoftware.sparkimpl.plugin.gateways.transports.Transport) SimpleDateFormat(java.text.SimpleDateFormat) VCard(org.jivesoftware.smackx.vcardtemp.packet.VCard) LastActivity(org.jivesoftware.smackx.iqlast.packet.LastActivity) Date(java.util.Date) MalformedURLException(java.net.MalformedURLException) URL(java.net.URL)

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