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.

Config.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Copyright (c) 2006-2007 Chris Smith, Shane Mc Cormack, Gregory Holmes
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package org.ownage.dmdirc;
  23. import java.io.File;
  24. import java.io.FileInputStream;
  25. import java.io.FileNotFoundException;
  26. import java.io.FileOutputStream;
  27. import java.io.IOException;
  28. import java.util.InvalidPropertiesFormatException;
  29. import java.util.Properties;
  30. /**
  31. * Reads/writes the application's config file
  32. * @author chris
  33. */
  34. public class Config {
  35. /**
  36. * The application's current configuration
  37. */
  38. private static Properties properties;
  39. /** Disallow creation of a new instance of Config */
  40. private Config() {
  41. }
  42. /**
  43. * Returns the singleton instance of ServerManager
  44. * @return Instance of ServerManager
  45. */
  46. public static Properties getConfig() {
  47. if (properties == null) {
  48. initialise();
  49. }
  50. return properties;
  51. }
  52. /**
  53. * Returns the full path to the application's config file
  54. * @return config file
  55. */
  56. private static String getConfigFile() {
  57. return getConfigDir()+"dmdirc.xml";
  58. }
  59. /**
  60. * Returns the application's config directory
  61. * @return configuration directory
  62. */
  63. public static String getConfigDir() {
  64. String fs = System.getProperty("file.separator");
  65. return System.getProperty("user.home")+fs+".DMDirc"+fs;
  66. }
  67. /**
  68. * Returns the default settings for DMDirc
  69. * @return default settings
  70. */
  71. private static Properties getDefaults() {
  72. Properties defaults = new Properties();
  73. defaults.setProperty("general.commandchar","/");
  74. defaults.setProperty("ui.maximisewindows","true");
  75. return defaults;
  76. }
  77. /**
  78. * Determines if the specified option exists
  79. * @return true iff the option exists, false otherwise
  80. * @param domain the domain of the option
  81. * @param option the name of the option
  82. */
  83. public static boolean hasOption(String domain, String option) {
  84. if (properties == null) {
  85. initialise();
  86. }
  87. return (properties.getProperty(domain+"."+option) != null);
  88. }
  89. /**
  90. * Returns the specified option
  91. * @return the value of the specified option
  92. * @param domain the domain of the option
  93. * @param option the name of the option
  94. */
  95. public static String getOption(String domain, String option) {
  96. if (properties == null) {
  97. initialise();
  98. }
  99. return properties.getProperty(domain+"."+option);
  100. }
  101. /**
  102. * Sets a specified option
  103. * @param domain domain of the option
  104. * @param option name of the option
  105. * @param value value of the option
  106. */
  107. public static void setOption(String domain, String option, String value) {
  108. if (properties == null) {
  109. initialise();
  110. }
  111. properties.setProperty(domain+"."+option, value);
  112. }
  113. /**
  114. * Loads the config file from disc, if it exists else initialises defaults
  115. * and creates file
  116. */
  117. private static void initialise() {
  118. properties = getDefaults();
  119. File file = new File(getConfigFile());
  120. if (file.exists()) {
  121. try {
  122. properties.loadFromXML(new FileInputStream(file));
  123. } catch (InvalidPropertiesFormatException ex) {
  124. //Do nothing, defaults used
  125. } catch (FileNotFoundException ex) {
  126. //Do nothing, defaults used
  127. } catch (IOException ex) {
  128. //Do nothing, defaults used
  129. }
  130. } else {
  131. try {
  132. (new File(getConfigDir())).mkdirs();
  133. file.createNewFile();
  134. Config.save();
  135. } catch (IOException ex) {
  136. //TODO Alert user, use defaults, saving will fail
  137. ex.printStackTrace();
  138. }
  139. }
  140. }
  141. /**
  142. * Saves the config file to disc
  143. */
  144. public static void save() {
  145. if (properties == null) {
  146. initialise();
  147. }
  148. try {
  149. properties.storeToXML(new FileOutputStream(new File(getConfigFile())), null);
  150. } catch (FileNotFoundException ex) {
  151. //Do nothing, shouldnt be able to happen
  152. } catch (IOException ex) {
  153. //Do nothing
  154. }
  155. }
  156. }