Search in sources :

Example 1 with UserManager

use of org.jivesoftware.spark.UserManager in project Spark by igniterealtime.

the class UriManager method handleMessage.

/**
 * handles the ?message URI
 *
 * @param uri
 *            the decoded uri
 */
public void handleMessage(URI uri) {
    String query = uri.getQuery();
    int bodyIndex = query.indexOf("body=");
    String jid = retrieveJID(uri);
    String body = null;
    // Find body
    if (bodyIndex != -1) {
        body = query.substring(bodyIndex + 5);
    }
    body = org.jivesoftware.spark.util.StringUtils.unescapeFromXML(body);
    UserManager userManager = SparkManager.getUserManager();
    String nickname = userManager.getUserNicknameFromJID(jid);
    if (nickname == null) {
        nickname = jid;
    }
    ChatManager chatManager = SparkManager.getChatManager();
    ChatRoom chatRoom = chatManager.createChatRoom(jid, nickname, nickname);
    if (body != null) {
        Message message = new Message();
        message.setBody(body);
        chatRoom.sendMessage(message);
    }
    chatManager.getChatContainer().activateChatRoom(chatRoom);
}
Also used : Message(org.jivesoftware.smack.packet.Message) ChatRoom(org.jivesoftware.spark.ui.ChatRoom) UserManager(org.jivesoftware.spark.UserManager) ChatManager(org.jivesoftware.spark.ChatManager)

Example 2 with UserManager

use of org.jivesoftware.spark.UserManager in project Spark by igniterealtime.

the class WorkgroupInvitationDialog method hasSelectedAgent.

/**
 * Returns the agent selected.
 *
 * @param chatRoom the room this roster will be used with.
 * @param transfer true if this will be a transfer.
 * @return true if a valid agent has been selected.
 */
public boolean hasSelectedAgent(ChatRoom chatRoom, boolean transfer) {
    final JiveTreeNode workgroupsNode = new JiveTreeNode("Workgroups", true);
    final JiveTreeNode queueNode = new JiveTreeNode("Queues", true);
    final String workgroupService = "workgroup." + SparkManager.getSessionManager().getServerAddress();
    final String jid = SparkManager.getSessionManager().getJID();
    String room = chatRoom.getRoomname();
    Collection agents = null;
    try {
        agents = getAvailableAgents(FastpathPlugin.getAgentSession().getAgentRoster(), room);
    } catch (SmackException.NotConnectedException e) {
        Log.warning("Unable to get agent roster.", e);
    }
    // Clear jid field
    jidField.setText("");
    String title = FpRes.getString("invite.agent");
    String acceptButton = FpRes.getString("invite");
    if (transfer) {
        title = FpRes.getString("transfer.to.agent");
        acceptButton = FpRes.getString("transfer");
    }
    String stockMessage = FpRes.getString("message.transfering.to.user");
    if (!transfer) {
        stockMessage = FpRes.getString("message.join.me.in.chat");
    }
    rootNode = new JiveTreeNode("Ask For Assistance", true);
    tree = new Tree(rootNode);
    tree.setCellRenderer(new JiveTreeCellRenderer());
    // This will remove lines from the tree and setup initial l&f
    tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
    tree.setRowHeight(16);
    tree.setExpandsSelectedPaths(true);
    tree.addTreeSelectionListener(new TreeSelectionListener() {

        public void valueChanged(TreeSelectionEvent e) {
            TreePath path = e.getNewLeadSelectionPath();
            Object o = path.getLastPathComponent();
            if (o instanceof JiveTreeNode) {
                JiveTreeNode node = (JiveTreeNode) o;
                JiveTreeNode parentNode = (JiveTreeNode) node.getParent();
                if (parentNode == workgroupsNode) {
                    jidField.setText(node.getUserObject().toString() + "@" + workgroupService);
                } else if (parentNode == queueNode) {
                    jidField.setText(FastpathPlugin.getWorkgroup().getWorkgroupJID() + "/" + node.getUserObject().toString());
                } else {
                    String agent = getAgent();
                    jidField.setText(agent);
                }
            }
        }
    });
    // Create message field
    messageField = new JTextArea();
    messageField.setWrapStyleWord(true);
    inviteLabel = new JLabel();
    if (transfer) {
        inviteLabel.setText(FpRes.getString("transfer.to") + ":");
    } else {
        inviteLabel.setText(FpRes.getString("invite") + ":");
    }
    if (transfer) {
        // Create the title panel for this dialog
        titlePanel = new TitlePanel(FpRes.getString("title.transfer"), FpRes.getString("message.transfer.to.another.agent"), FastpathRes.getImageIcon(FastpathRes.CHATTING_AGENT_IMAGE), true);
    } else {
        // Create the title panel for this dialog
        titlePanel = new TitlePanel(FpRes.getString("title.invitation"), FpRes.getString("message.invite.another.agent"), FastpathRes.getImageIcon(FastpathRes.CHATTING_AGENT_IMAGE), true);
    }
    // Build Tree
    String joinedWorkgroupName = XmppStringUtils.parseLocalpart(FastpathPlugin.getWorkgroup().getWorkgroupJID());
    final JiveTreeNode workgroupNode = new JiveTreeNode(joinedWorkgroupName, true);
    final Iterator agentIter = agents.iterator();
    while (agentIter.hasNext()) {
        while (agentIter.hasNext()) {
            String agentName = UserManager.unescapeJID((String) agentIter.next());
            final JiveTreeNode agentNode = new JiveTreeNode(agentName, false, FastpathRes.getImageIcon(FastpathRes.GREEN_BALL));
            workgroupNode.add(agentNode);
        }
        if (workgroupNode.getChildCount() > 0) {
            rootNode.add(workgroupNode);
        }
    }
    Collection workgroupAgents;
    try {
        workgroupAgents = Agent.getWorkgroups(workgroupService, jid, SparkManager.getConnection());
    } catch (XMPPException | SmackException e) {
        Log.error(e);
        workgroupAgents = Collections.EMPTY_LIST;
    }
    if (workgroupAgents.size() > 0) {
        // Add workgroups to combobox
        Iterator<String> workgroups = workgroupAgents.iterator();
        while (workgroups.hasNext()) {
            String workgroup = workgroups.next();
            String workgroupName = XmppStringUtils.parseLocalpart(workgroup);
            final JiveTreeNode wgNode = new JiveTreeNode(workgroupName, false, FastpathRes.getImageIcon(FastpathRes.FASTPATH_IMAGE_16x16));
            workgroupsNode.add(wgNode);
        }
        rootNode.add(workgroupsNode);
    }
    Iterator iter = FastpathPlugin.getAgentSession().getQueues();
    while (iter.hasNext()) {
        final WorkgroupQueue queue = (WorkgroupQueue) iter.next();
        if (queue.getStatus() == WorkgroupQueue.Status.OPEN) {
            final JiveTreeNode qNode = new JiveTreeNode(queue.getName(), false, FastpathRes.getImageIcon(FastpathRes.FASTPATH_IMAGE_16x16));
            queueNode.add(qNode);
        }
    }
    rootNode.add(queueNode);
    // New Roster tree. Do not show agents at all.
    final UserManager userManager = SparkManager.getUserManager();
    Collection jids = userManager.getUserJidsInRoom(room, false);
    roster = new WorkgroupRosterTree(jids, false, workgroups);
    try {
        rosterTree = roster.getRosterTree();
        JiveTreeNode node = (JiveTreeNode) rosterTree.getModel().getRoot();
        rootNode.add(node);
    } catch (Exception e) {
        Log.error("Error checking for selected agent", e);
    }
    // Construct main panel w/ layout.
    final JPanel mainPanel = new JPanel();
    mainPanel.setLayout(new BorderLayout());
    mainPanel.add(titlePanel, BorderLayout.NORTH);
    // The user should only be able to close this dialog.
    Object[] options = { acceptButton, FpRes.getString("cancel") };
    // Create objects to add to mainPanel.
    final JPanel parentPanel = new JPanel();
    final JLabel messageLabel = new JLabel();
    final JLabel agentLabel = new JLabel();
    parentPanel.setLayout(new GridBagLayout());
    // Update Labels using ResourceUtils
    ResourceUtils.resLabel(messageLabel, messageField, FpRes.getString("label.message.to.agent"));
    ResourceUtils.resLabel(agentLabel, tree, FpRes.getString("label.select.agent"));
    // Add Components to Parent Panel
    parentPanel.add(agentLabel, new GridBagConstraints(0, 0, 3, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 0, 5), 0, 0));
    parentPanel.add(new JScrollPane(tree), new GridBagConstraints(0, 1, 3, 1, 1.0, 1.0, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 100));
    parentPanel.add(messageLabel, new GridBagConstraints(0, 3, 3, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 0, 5), 0, 0));
    parentPanel.add(new JScrollPane(messageField), new GridBagConstraints(0, 4, 3, 1, 1.0, 1.0, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 25));
    parentPanel.add(inviteLabel, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
    parentPanel.add(jidField, new GridBagConstraints(1, 2, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
    // Use line wrap.
    messageField.setLineWrap(true);
    // Update MessageField with stock message
    messageField.setText(stockMessage);
    // Add Parent Object to master OptionPane
    pane = new JOptionPane(parentPanel, JOptionPane.PLAIN_MESSAGE, JOptionPane.OK_CANCEL_OPTION, null, options, options[0]);
    mainPanel.add(pane, BorderLayout.CENTER);
    dlg = new JDialog(SparkManager.getChatManager().getChatContainer().getChatFrame(), title, true);
    dlg.pack();
    dlg.setSize(500, 500);
    dlg.setContentPane(mainPanel);
    dlg.setLocationRelativeTo(chatRoom);
    pane.addPropertyChangeListener(this);
    // expand the tree for displaying
    for (int i = 0; i <= tree.getRowCount(); i++) {
        tree.expandPath(tree.getPathForRow(i));
    }
    dlg.setVisible(true);
    dlg.toFront();
    dlg.requestFocus();
    if (!isValid) {
        return false;
    }
    return true;
}
Also used : JPanel(javax.swing.JPanel) GridBagConstraints(java.awt.GridBagConstraints) JTextArea(javax.swing.JTextArea) Insets(java.awt.Insets) GridBagLayout(java.awt.GridBagLayout) TreeSelectionListener(javax.swing.event.TreeSelectionListener) WorkgroupQueue(org.jivesoftware.smackx.workgroup.agent.WorkgroupQueue) JiveTreeCellRenderer(org.jivesoftware.spark.component.JiveTreeCellRenderer) BorderLayout(java.awt.BorderLayout) JiveTreeNode(org.jivesoftware.spark.component.JiveTreeNode) Iterator(java.util.Iterator) Tree(org.jivesoftware.spark.component.Tree) JScrollPane(javax.swing.JScrollPane) SmackException(org.jivesoftware.smack.SmackException) JLabel(javax.swing.JLabel) JOptionPane(javax.swing.JOptionPane) TitlePanel(org.jivesoftware.spark.component.TitlePanel) SmackException(org.jivesoftware.smack.SmackException) XMPPException(org.jivesoftware.smack.XMPPException) TreePath(javax.swing.tree.TreePath) UserManager(org.jivesoftware.spark.UserManager) Collection(java.util.Collection) TreeSelectionEvent(javax.swing.event.TreeSelectionEvent) XMPPException(org.jivesoftware.smack.XMPPException) JDialog(javax.swing.JDialog)

Example 3 with UserManager

use of org.jivesoftware.spark.UserManager in project Spark by igniterealtime.

the class GroupChatParticipantList method addInvitee.

public void addInvitee(String jid, String message) {
    // So the problem with this is that I have no idea what the users actual
    // jid is in most cases.
    final UserManager userManager = SparkManager.getUserManager();
    String displayName = userManager.getUserNicknameFromJID(jid);
    groupChatRoom.getTranscriptWindow().insertNotificationMessage(displayName + " has been invited to join this room.", ChatManager.NOTIFICATION_COLOR);
    if (roomInformation != null && !roomInformation.containsFeature("muc_nonanonymous")) {
        return;
    }
    final ImageIcon inviteIcon = SparkRes.getImageIcon(SparkRes.USER1_BACK_16x16);
    addUser(inviteIcon, displayName);
    invitees.put(displayName, message);
}
Also used : ImageIcon(javax.swing.ImageIcon) UserManager(org.jivesoftware.spark.UserManager)

Example 4 with UserManager

use of org.jivesoftware.spark.UserManager 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

UserManager (org.jivesoftware.spark.UserManager)4 GridBagConstraints (java.awt.GridBagConstraints)2 GridBagLayout (java.awt.GridBagLayout)2 Insets (java.awt.Insets)2 JLabel (javax.swing.JLabel)2 JOptionPane (javax.swing.JOptionPane)2 JPanel (javax.swing.JPanel)2 SmackException (org.jivesoftware.smack.SmackException)2 XMPPException (org.jivesoftware.smack.XMPPException)2 TitlePanel (org.jivesoftware.spark.component.TitlePanel)2 BorderLayout (java.awt.BorderLayout)1 Dimension (java.awt.Dimension)1 Frame (java.awt.Frame)1 ActionListener (java.awt.event.ActionListener)1 KeyEvent (java.awt.event.KeyEvent)1 WindowAdapter (java.awt.event.WindowAdapter)1 WindowEvent (java.awt.event.WindowEvent)1 Collection (java.util.Collection)1 Iterator (java.util.Iterator)1 BorderFactory (javax.swing.BorderFactory)1