diff options
author | Samuel Mannehed (ThinLinc team) <samuel@cendio.se> | 2024-05-07 15:00:43 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-05-07 15:00:43 +0200 |
commit | 3db859ef6c40539ac20cd5e33e0f31e00dc1a4a3 (patch) | |
tree | 9d911240e3456f0a9ca664bc242169e56efea6b9 /java/com | |
parent | 6d19e961e243a6a5a517afd2dc3602ce013e4433 (diff) | |
parent | 337c136ce018fc8d9d12b9e188634925238d11cc (diff) | |
download | tigervnc-3db859ef6c40539ac20cd5e33e0f31e00dc1a4a3.tar.gz tigervnc-3db859ef6c40539ac20cd5e33e0f31e00dc1a4a3.zip |
Merge pull request #1737 from 62832/fix-1195
Allow for alternative user config locations, deprecate `~/.vnc` in favour of XDG Base Directory Specification paths
Diffstat (limited to 'java/com')
-rw-r--r-- | java/com/tigervnc/rfb/CSecurityTLS.java | 9 | ||||
-rw-r--r-- | java/com/tigervnc/vncviewer/FileUtils.java | 48 | ||||
-rw-r--r-- | java/com/tigervnc/vncviewer/Parameters.java | 18 | ||||
-rw-r--r-- | java/com/tigervnc/vncviewer/ServerDialog.java | 6 |
4 files changed, 46 insertions, 35 deletions
diff --git a/java/com/tigervnc/rfb/CSecurityTLS.java b/java/com/tigervnc/rfb/CSecurityTLS.java index 14a5eb66..ab221427 100644 --- a/java/com/tigervnc/rfb/CSecurityTLS.java +++ b/java/com/tigervnc/rfb/CSecurityTLS.java @@ -89,13 +89,13 @@ public class CSecurityTLS extends CSecurity { public static String getDefaultCA() { if (UserPreferences.get("viewer", "x509ca") != null) return UserPreferences.get("viewer", "x509ca"); - return FileUtils.getVncHomeDir()+"x509_ca.pem"; + return FileUtils.getVncConfigDir()+"x509_ca.pem"; } public static String getDefaultCRL() { if (UserPreferences.get("viewer", "x509crl") != null) return UserPreferences.get("viewer", "x509crl"); - return FileUtils.getVncHomeDir()+"x509_crl.pem"; + return FileUtils.getVncConfigDir()+"x509_crl.pem"; } public static void setDefaults() @@ -277,12 +277,12 @@ public class CSecurityTLS extends CSecurity { "do you want to continue?")) throw new AuthFailureException("server certificate has expired"); } - File vncDir = new File(FileUtils.getVncHomeDir()); + File vncDir = new File(FileUtils.getVncDataDir()); if (!vncDir.exists()) { try { vncDir.mkdir(); } catch(SecurityException e) { - throw new AuthFailureException("Could not obtain VNC home directory "+ + throw new AuthFailureException("Could not obtain VNC data directory "+ "path for known hosts storage"); } } @@ -356,7 +356,6 @@ public class CSecurityTLS extends CSecurity { private void store_pubkey(File dbPath, String serverName, String pk) { ArrayList<String> lines = new ArrayList<String>(); - File vncDir = new File(FileUtils.getVncHomeDir()); try { if (dbPath.exists()) { FileReader db = new FileReader(dbPath); diff --git a/java/com/tigervnc/vncviewer/FileUtils.java b/java/com/tigervnc/vncviewer/FileUtils.java index af5da3c2..95fec3e6 100644 --- a/java/com/tigervnc/vncviewer/FileUtils.java +++ b/java/com/tigervnc/vncviewer/FileUtils.java @@ -22,9 +22,11 @@ import javax.swing.filechooser.FileSystemView; import com.tigervnc.rfb.LogWriter; +import java.io.File; + public class FileUtils { - public static final String getHomeDir() { + public static String getHomeDir() { String homeDir = null; try { String os = System.getProperty("os.name"); @@ -56,21 +58,47 @@ public class FileUtils { vlog.error("Cannot access os.name system property:"+e.getMessage()); } - String separator = null; - try { - separator = Character.toString(java.io.File.separatorChar); - } catch(java.security.AccessControlException e) { - vlog.error("Cannot access file.separator system property:"+e.getMessage()); + return homeDir + getFileSeparator(); + } + + public static String getVncDir(String xdgEnv, String xdgDefault) { + File legacyDir = new File(getHomeDir() + ".vnc" + getFileSeparator()); + String os = System.getProperty("os.name"); + + if (os.startsWith("Windows")) { + File newDir = new File(System.getenv("APPDATA") + getFileSeparator() + "TigerVNC" + getFileSeparator()); + if (!newDir.exists()) { + newDir.mkdirs(); + } + File[] existingFiles = legacyDir.listFiles(); + if (existingFiles != null) { + for (File file : existingFiles) { + file.renameTo(new File(newDir.getPath() + file.getName())); + } + legacyDir.delete(); + } + return newDir.getPath(); + } else { + if (legacyDir.exists()) { + vlog.info("WARNING: ~/.vnc is deprecated, please consult 'man vncviewer' for paths to migrate to."); + return legacyDir.getPath(); + } + String xdgBaseDir = System.getenv(xdgEnv); + return (xdgBaseDir != null && xdgBaseDir.startsWith("/")) + ? xdgBaseDir + getFileSeparator() + "tigervnc" + getFileSeparator() + : getHomeDir() + xdgDefault + getFileSeparator() + "tigervnc" + getFileSeparator(); } + } - return homeDir + getFileSeparator(); + public static String getVncConfigDir() { + return getVncDir("XDG_CONFIG_HOME", ".config"); } - public static final String getVncHomeDir() { - return getHomeDir()+".vnc"+getFileSeparator(); + public static String getVncDataDir() { + return getVncDir("XDG_DATA_HOME", ".local" + getFileSeparator() + "share"); } - public static final String getFileSeparator() { + public static String getFileSeparator() { String separator = null; try { separator = Character.toString(java.io.File.separatorChar); diff --git a/java/com/tigervnc/vncviewer/Parameters.java b/java/com/tigervnc/vncviewer/Parameters.java index f08202c0..8c8465cb 100644 --- a/java/com/tigervnc/vncviewer/Parameters.java +++ b/java/com/tigervnc/vncviewer/Parameters.java @@ -325,13 +325,6 @@ public class Parameters { if (filename == null || filename.isEmpty()) { saveToReg(servername); return; - /* - String homeDir = FileUtils.getVncHomeDir(); - if (homeDir == null) - throw new Exception("Failed to read configuration file, "+ - "can't obtain home directory path."); - filepath = homeDir.concat("default.tigervnc"); - */ } else { filepath = filename; } @@ -385,16 +378,7 @@ public class Parameters { String filepath; if (filename == null) { - - return loadFromReg(); - - /* - String homeDir = FileUtils.getVncHomeDir(); - if (homeDir == null) - throw new Exception("Failed to read configuration file, "+ - "can't obtain home directory path."); - filepath = homeDir.concat("default.tigervnc"); - */ + return loadFromReg(); } else { filepath = filename; } diff --git a/java/com/tigervnc/vncviewer/ServerDialog.java b/java/com/tigervnc/vncviewer/ServerDialog.java index aeee0b21..5f75fd3d 100644 --- a/java/com/tigervnc/vncviewer/ServerDialog.java +++ b/java/com/tigervnc/vncviewer/ServerDialog.java @@ -235,7 +235,7 @@ class ServerDialog extends Dialog implements Runnable { private void handleLoad() { String title = "Select a TigerVNC configuration file"; - File dflt = new File(FileUtils.getVncHomeDir().concat("default.tigervnc")); + File dflt = new File(FileUtils.getVncConfigDir().concat("default.tigervnc")); FileNameExtensionFilter filter = new FileNameExtensionFilter("TigerVNC configuration (*.tigervnc)", "tigervnc"); File f = showChooser(title, dflt, filter); @@ -245,9 +245,9 @@ class ServerDialog extends Dialog implements Runnable { private void handleSaveAs() { String title = "Save the TigerVNC configuration to file"; - File dflt = new File(FileUtils.getVncHomeDir().concat("default.tigervnc")); + File dflt = new File(FileUtils.getVncConfigDir().concat("default.tigervnc")); if (!dflt.exists() || !dflt.isFile()) - dflt = new File(FileUtils.getVncHomeDir()); + dflt = new File(FileUtils.getVncConfigDir()); FileNameExtensionFilter filter = new FileNameExtensionFilter("TigerVNC configuration (*.tigervnc)", "tigervnc"); File f = showChooser(title, dflt, filter); |