You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FileUtils.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /* Copyright (C) 2012 Brian P. Hinz
  2. *
  3. * This is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This software is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this software; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  16. * USA.
  17. */
  18. package com.tigervnc.vncviewer;
  19. import javax.swing.filechooser.FileSystemView;
  20. import com.tigervnc.rfb.LogWriter;
  21. import java.io.File;
  22. public class FileUtils {
  23. public static String getHomeDir() {
  24. String homeDir = null;
  25. try {
  26. String os = System.getProperty("os.name");
  27. try {
  28. if (os.startsWith("Windows")) {
  29. // JRE prior to 1.5 cannot reliably determine USERPROFILE
  30. // return user.home and hope it's right...
  31. if (Integer.parseInt(System.getProperty("java.version").split("\\.")[1]) < 5) {
  32. try {
  33. homeDir = System.getProperty("user.home");
  34. } catch(java.security.AccessControlException e) {
  35. vlog.error("Cannot access user.home system property:"+e.getMessage());
  36. }
  37. } else {
  38. homeDir = System.getenv("USERPROFILE");
  39. }
  40. } else {
  41. try {
  42. homeDir = FileSystemView.getFileSystemView().
  43. getDefaultDirectory().getCanonicalPath();
  44. } catch(java.security.AccessControlException e) {
  45. vlog.error("Cannot access system property:"+e.getMessage());
  46. }
  47. }
  48. } catch (java.lang.Exception e) {
  49. e.printStackTrace();
  50. }
  51. } catch(java.security.AccessControlException e) {
  52. vlog.error("Cannot access os.name system property:"+e.getMessage());
  53. }
  54. return homeDir + getFileSeparator();
  55. }
  56. public static String getVncDir(String xdgEnv, String xdgDefault) {
  57. File legacyDir = new File(getHomeDir() + ".vnc" + getFileSeparator());
  58. String os = System.getProperty("os.name");
  59. if (os.startsWith("Windows")) {
  60. File newDir = new File(System.getenv("APPDATA") + getFileSeparator() + "TigerVNC" + getFileSeparator());
  61. if (!newDir.exists()) {
  62. newDir.mkdirs();
  63. }
  64. File[] existingFiles = legacyDir.listFiles();
  65. if (existingFiles != null) {
  66. for (File file : existingFiles) {
  67. file.renameTo(new File(newDir.getPath() + file.getName()));
  68. }
  69. legacyDir.delete();
  70. }
  71. return newDir.getPath();
  72. } else {
  73. if (legacyDir.exists()) {
  74. vlog.info("WARNING: ~/.vnc is deprecated, please consult 'man vncviewer' for paths to migrate to.");
  75. return legacyDir.getPath();
  76. }
  77. String xdgBaseDir = System.getenv(xdgEnv);
  78. return (xdgBaseDir != null && xdgBaseDir.startsWith("/"))
  79. ? xdgBaseDir + getFileSeparator() + "tigervnc" + getFileSeparator()
  80. : getHomeDir() + xdgDefault + getFileSeparator() + "tigervnc" + getFileSeparator();
  81. }
  82. }
  83. public static String getVncConfigDir() {
  84. return getVncDir("XDG_CONFIG_HOME", ".config");
  85. }
  86. public static String getVncDataDir() {
  87. return getVncDir("XDG_DATA_HOME", ".local" + getFileSeparator() + "share");
  88. }
  89. public static String getFileSeparator() {
  90. String separator = null;
  91. try {
  92. separator = Character.toString(java.io.File.separatorChar);
  93. } catch(java.security.AccessControlException e) {
  94. vlog.error("Cannot access file.separator system property:"+e.getMessage());
  95. }
  96. return separator;
  97. }
  98. static LogWriter vlog = new LogWriter("FileUtils");
  99. }