From 733f259342409ac228354bd058a352fd342cc2e0 Mon Sep 17 00:00:00 2001 From: "Brian P. Hinz" Date: Sun, 19 Feb 2017 23:57:25 -0500 Subject: [PATCH] Java client was not honoring "ServerName" option in config files Mentioned in issue #38 --- java/com/tigervnc/vncviewer/ServerDialog.java | 49 +++++++++------- java/com/tigervnc/vncviewer/VncViewer.java | 57 +++++++++++-------- 2 files changed, 63 insertions(+), 43 deletions(-) diff --git a/java/com/tigervnc/vncviewer/ServerDialog.java b/java/com/tigervnc/vncviewer/ServerDialog.java index 4b3f26bf..001e2317 100644 --- a/java/com/tigervnc/vncviewer/ServerDialog.java +++ b/java/com/tigervnc/vncviewer/ServerDialog.java @@ -22,6 +22,7 @@ package com.tigervnc.vncviewer; import java.awt.*; import java.awt.event.*; import java.io.File; +import java.nio.CharBuffer; import javax.swing.*; import javax.swing.border.*; import javax.swing.filechooser.*; @@ -41,8 +42,10 @@ import static com.tigervnc.vncviewer.Parameters.*; class ServerDialog extends Dialog implements Runnable { @SuppressWarnings({"unchecked","rawtypes"}) - public ServerDialog() { + public ServerDialog(String defaultServerName, + CharBuffer vncServerName) { super(true); + this.vncServerName = vncServerName; setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); setTitle("VNC Viewer: Connection Details"); setResizable(false); @@ -54,11 +57,19 @@ class ServerDialog extends Dialog implements Runnable { }); JLabel serverLabel = new JLabel("VNC server:", JLabel.RIGHT); - String valueStr = new String(""); - if (UserPreferences.get("ServerDialog", "history") != null) - valueStr = UserPreferences.get("ServerDialog", "history"); - server = new MyJComboBox(valueStr.split(",")); - server.addActionListener(new ActionListener() { + String valueStr = new String(defaultServerName); + ArrayList servernames = new ArrayList(); + if (!valueStr.isEmpty()) + servernames.add(valueStr); + String history = UserPreferences.get("ServerDialog", "history"); + if (history != null) { + for (String s : history.split(",")) { + if (servernames.indexOf(s) < 0) + servernames.add(s); + } + } + serverName = new MyJComboBox(servernames.toArray()); + serverName.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JComboBox s = (JComboBox)e.getSource(); if (e.getActionCommand().equals("comboBoxEdited")) { @@ -67,18 +78,18 @@ class ServerDialog extends Dialog implements Runnable { } } }); - if (valueStr.equals("")) - server.setPrototypeDisplayValue("255.255.255.255:5900"); + if (servernames.size() == 0) + serverName.setPrototypeDisplayValue("255.255.255.255:5900"); - server.setEditable(true); - editor = server.getEditor(); + serverName.setEditable(true); + editor = serverName.getEditor(); editor.getEditorComponent().addKeyListener(new KeyListener() { public void keyTyped(KeyEvent e) {} public void keyReleased(KeyEvent e) {} public void keyPressed(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_ENTER) { - server.insertItemAt(editor.getItem(), 0); - server.setSelectedIndex(0); + serverName.insertItemAt(editor.getItem(), 0); + serverName.setSelectedIndex(0); handleConnect(); } } @@ -139,7 +150,7 @@ class ServerDialog extends Dialog implements Runnable { LINE_START, NONE, new Insets(5, 10, 5, 5), NONE, NONE)); - contentPane.add(server, + contentPane.add(serverName, new GridBagConstraints(2, 0, REMAINDER, 1, HEAVY, LIGHT, @@ -254,7 +265,7 @@ class ServerDialog extends Dialog implements Runnable { f = showChooser(title, f, filter); } if (f != null && (!f.exists() || f.canWrite())) - saveViewerParameters(f.getAbsolutePath(), (String)server.getSelectedItem()); + saveViewerParameters(f.getAbsolutePath(), (String)serverName.getSelectedItem()); } private void handleAbout() { @@ -262,22 +273,22 @@ class ServerDialog extends Dialog implements Runnable { } private void handleCancel() { - vncServerName.setParam(""); endDialog(); } private void handleConnect() { - String serverName = (String)server.getSelectedItem(); - vncServerName.setParam(serverName); - saveViewerParameters(null, serverName); + String servername = (String)serverName.getSelectedItem(); + vncServerName.put(servername).flip(); + saveViewerParameters(null, servername); endDialog(); } @SuppressWarnings("rawtypes") - MyJComboBox server; + MyJComboBox serverName; ComboBoxEditor editor; JButton aboutButton, optionsButton, connectButton, cancelButton; OptionsDialog options; + CharBuffer vncServerName; static LogWriter vlog = new LogWriter("ServerDialog"); } diff --git a/java/com/tigervnc/vncviewer/VncViewer.java b/java/com/tigervnc/vncviewer/VncViewer.java index f5b31775..d690cc26 100644 --- a/java/com/tigervnc/vncviewer/VncViewer.java +++ b/java/com/tigervnc/vncviewer/VncViewer.java @@ -43,9 +43,10 @@ import java.io.File; import java.lang.Character; import java.lang.reflect.*; import java.net.URL; +import java.nio.CharBuffer; +import java.util.*; import java.util.jar.Attributes; import java.util.jar.Manifest; -import java.util.*; import javax.swing.*; import javax.swing.border.*; import javax.swing.plaf.FontUIResource; @@ -83,6 +84,10 @@ public class VncViewer extends javax.swing.JApplet System.getProperty("os.name").toLowerCase(); private static VncViewer applet; + private String defaultServerName; + int VNCSERVERNAMELEN = 64; + CharBuffer vncServerName = CharBuffer.allocate(VNCSERVERNAMELEN); + public static void setLookAndFeel() { try { if (os.startsWith("mac os x")) { @@ -160,7 +165,6 @@ public class VncViewer extends javax.swing.JApplet Configuration.enableViewerParams(); /* Load the default parameter settings */ - String defaultServerName; try { defaultServerName = loadViewerParameters(null); } catch (com.tigervnc.rfb.Exception e) { @@ -200,9 +204,7 @@ public class VncViewer extends javax.swing.JApplet usage(); } - if (!vncServerName.getValue().isEmpty()) - usage(); - vncServerName.setParam(argv[i]); + vncServerName.put(argv[i].toCharArray()).flip(); } } @@ -367,9 +369,7 @@ public class VncViewer extends javax.swing.JApplet setLookAndFeel(); setBackground(Color.white); applet = this; - String servername = loadAppletParameters(applet); - vncServerName.setParam(servername); - alwaysShowServerDialog.setParam(false); + vncServerName.put(loadAppletParameters(applet).toCharArray()).flip(); if (embed.getValue()) { fullScreen.setParam(false); remoteResize.setParam(false); @@ -486,13 +486,24 @@ public class VncViewer extends javax.swing.JApplet public void run() { cc = null; + Socket sock = null; + + /* Specifying -via and -listen together is nonsense */ + if (listenMode.getValue() && !via.getValueStr().isEmpty()) { + vlog.error("Parameters -listen and -via are incompatible"); + String msg = + new String("Parameters -listen and -via are incompatible"); + JOptionPane.showMessageDialog(null, msg, "ERROR", + JOptionPane.ERROR_MESSAGE); + exit(1); + } if (listenMode.getValue()) { int port = 5500; - if (!vncServerName.getValue().isEmpty() && - Character.isDigit(vncServerName.getValue().charAt(0))) - port = Integer.parseInt(vncServerName.getValue()); + if (vncServerName.charAt(0) != 0 && + Character.isDigit(vncServerName.charAt(0))) + port = Integer.parseInt(vncServerName.toString()); TcpListener listener = null; try { @@ -507,23 +518,22 @@ public class VncViewer extends javax.swing.JApplet while (sock == null) sock = listener.accept(); } else { - if (alwaysShowServerDialog.getValue() || sock == null) { - if (vncServerName.getValue().isEmpty()) { - try { - SwingUtilities.invokeAndWait(new ServerDialog()); - } catch (InvocationTargetException e) { - reportException(e); - } catch (InterruptedException e) { - reportException(e); - } - if (vncServerName.getValue().isEmpty()) - exit(0); + if (vncServerName.charAt(0) == 0) { + try { + SwingUtilities.invokeAndWait( + new ServerDialog(defaultServerName, vncServerName)); + } catch (InvocationTargetException e) { + reportException(e); + } catch (InterruptedException e) { + reportException(e); } + if (vncServerName.charAt(0) == 0) + exit(0); } } try { - cc = new CConn(vncServerName.getValue(), sock); + cc = new CConn(vncServerName.toString(), sock); while (!cc.shuttingDown) cc.processMsg(); exit(0); @@ -546,6 +556,5 @@ public class VncViewer extends javax.swing.JApplet "Specifies a configuration file to load.", null); Thread thread; - Socket sock; static LogWriter vlog = new LogWriter("VncViewer"); } -- 2.39.5