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 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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 uk.org.ownage.dmdirc;
  23. import java.awt.Color;
  24. import java.io.File;
  25. import java.io.FileInputStream;
  26. import java.io.FileNotFoundException;
  27. import java.io.FileOutputStream;
  28. import java.io.IOException;
  29. import java.util.ArrayList;
  30. import java.util.Enumeration;
  31. import java.util.InvalidPropertiesFormatException;
  32. import java.util.List;
  33. import java.util.Properties;
  34. import javax.swing.UIManager;
  35. import uk.org.ownage.dmdirc.identities.IdentityManager;
  36. import uk.org.ownage.dmdirc.logger.ErrorLevel;
  37. import uk.org.ownage.dmdirc.logger.Logger;
  38. import uk.org.ownage.dmdirc.ui.messages.ColourManager;
  39. /**
  40. * Reads/writes the application's config file.
  41. * @author chris
  42. */
  43. public final class Config {
  44. /**
  45. * The application's current configuration.
  46. */
  47. private static Properties properties;
  48. /** Disallow creation of a new instance of Config. */
  49. private Config() {
  50. }
  51. /**
  52. * Returns the singleton instance of Config properties.
  53. * @return Instance of Config properties
  54. */
  55. public static Properties getConfig() {
  56. if (properties == null) {
  57. initialise();
  58. }
  59. return properties;
  60. }
  61. /**
  62. * Returns the full path to the application's config file.
  63. * @return config file
  64. */
  65. private static String getConfigFile() {
  66. return getConfigDir() + "dmdirc.xml";
  67. }
  68. /**
  69. * Returns the application's config directory.
  70. * @return configuration directory
  71. */
  72. public static String getConfigDir() {
  73. final String fs = System.getProperty("file.separator");
  74. final String osName = System.getProperty("os.name");
  75. String configDir = null;
  76. if (osName.startsWith("Mac OS")) {
  77. configDir = System.getProperty("user.home") + "/Library/Preferences/DMDirc/";
  78. } else if (osName.startsWith("Windows")) {
  79. if (System.getenv("APPDATA") == null) {
  80. configDir = System.getProperty("user.home") + fs + "DMDirc" + fs;
  81. } else {
  82. configDir = System.getenv("APPDATA") + fs + "DMDirc" + fs;
  83. }
  84. } else {
  85. configDir = System.getProperty("user.home") + fs + ".DMDirc" + fs;
  86. }
  87. return configDir;
  88. }
  89. /**
  90. * Returns the default settings for DMDirc.
  91. * @return default settings
  92. */
  93. private static Properties getDefaults() {
  94. final Properties defaults = new Properties();
  95. defaults.setProperty("general.commandchar", "/");
  96. defaults.setProperty("general.reconnectmessage", "Reconnecting");
  97. defaults.setProperty("general.closemessage", "DMDirc exiting");
  98. defaults.setProperty("general.quitmessage", "Using DMDirc");
  99. defaults.setProperty("general.partmessage", "Using DMDirc");
  100. defaults.setProperty("general.cyclemessage", "Cycling");
  101. defaults.setProperty("general.kickmessage", "Bye!");
  102. defaults.setProperty("general.hidequeries", "false");
  103. defaults.setProperty("general.closechannelsonquit", "false");
  104. defaults.setProperty("general.closequeriesonquit", "false");
  105. defaults.setProperty("general.closechannelsondisconnect", "false");
  106. defaults.setProperty("general.closequeriesondisconnect", "false");
  107. defaults.setProperty("general.reconnectonconnectfailure", "true");
  108. defaults.setProperty("general.reconnectondisconnect", "true");
  109. defaults.setProperty("general.reconnectdelay", "5");
  110. // These are temporary until we get server list support
  111. defaults.setProperty("general.server", "blueyonder.uk.quakenet.org");
  112. defaults.setProperty("general.port", "7000");
  113. defaults.setProperty("general.password", "");
  114. // These control where notifications will go. Expected values are
  115. // "active", "all", or "server".
  116. // TODO: Some kind of validation in the config class itself, rather
  117. // than elsewhere?
  118. defaults.setProperty("notifications.connectError", "server");
  119. defaults.setProperty("notifications.connectRetry", "server");
  120. defaults.setProperty("notifications.socketClosed", "all");
  121. defaults.setProperty("notifications.stonedServer", "all");
  122. defaults.setProperty("notifications.privateNotice", "all");
  123. defaults.setProperty("notifications.privateCTCP", "server");
  124. defaults.setProperty("notifications.privateCTCPreply", "server");
  125. // Send whois info to active window by default
  126. defaults.setProperty("notifications.numeric_301", "active");
  127. defaults.setProperty("notifications.numeric_311", "active");
  128. defaults.setProperty("notifications.numeric_312", "active");
  129. defaults.setProperty("notifications.numeric_313", "active");
  130. defaults.setProperty("notifications.numeric_318", "active");
  131. defaults.setProperty("notifications.numeric_319", "active");
  132. defaults.setProperty("notifications.numeric_330", "active");
  133. defaults.setProperty("notifications.numeric_343", "active");
  134. defaults.setProperty("ui.backgroundcolour", "0");
  135. defaults.setProperty("ui.foregroundcolour", "1");
  136. defaults.setProperty("ui.maximisewindows", "false");
  137. defaults.setProperty("ui.sortByMode", "true");
  138. defaults.setProperty("ui.sortByCase", "false");
  139. defaults.setProperty("ui.inputbuffersize", "50");
  140. defaults.setProperty("ui.showversion", "true");
  141. defaults.setProperty("ui.lookandfeel", UIManager.getCrossPlatformLookAndFeelClassName());
  142. defaults.setProperty("ui.quickCopy", "false");
  143. defaults.setProperty("ui.pasteProtectionLimit", "1");
  144. // TODO: These should probably be renamed to treeview.* or so?
  145. defaults.setProperty("ui.treeviewRolloverEnabled", "true");
  146. defaults.setProperty("ui.treeviewRolloverColour", "f0f0f0");
  147. defaults.setProperty("treeview.sortwindows", "true");
  148. defaults.setProperty("treeview.sortservers", "true");
  149. defaults.setProperty("channel.splitusermodes", "false");
  150. defaults.setProperty("channel.sendwho", "false");
  151. defaults.setProperty("channel.showmodeprefix", "true");
  152. defaults.setProperty("general.whotime", "60000");
  153. defaults.setProperty("tabcompletion.casesensitive", "false");
  154. defaults.setProperty("logging.dateFormat", "EEE, d MMM yyyy HH:mm:ss Z");
  155. defaults.setProperty("logging.programLogging", "true");
  156. defaults.setProperty("logging.debugLogging", "true");
  157. defaults.setProperty("logging.debugLoggingSysOut", "true");
  158. defaults.setProperty("server.friendlymodes", "true");
  159. defaults.setProperty("server.pingtimeout", "60000");
  160. // Some defaults for use in actions
  161. defaults.setProperty("actions.textcolour", "12");
  162. defaults.setProperty("actions.eventcolour", "3");
  163. defaults.setProperty("actions.highlightcolour", "4");
  164. return defaults;
  165. }
  166. /**
  167. * Determines if the specified option exists.
  168. * @return true iff the option exists, false otherwise
  169. * @param domain the domain of the option
  170. * @param option the name of the option
  171. */
  172. public static boolean hasOption(final String domain, final String option) {
  173. if (properties == null) {
  174. initialise();
  175. }
  176. return properties.getProperty(domain + "." + option) != null;
  177. }
  178. /**
  179. * Returns the application's command character.
  180. * @return The command character (general.commandchar)
  181. */
  182. public static String getCommandChar() {
  183. return getOption("general", "commandchar", "/");
  184. }
  185. /**
  186. * Returns the specified option.
  187. * @return the value of the specified option
  188. * @param domain the domain of the option
  189. * @param option the name of the option
  190. */
  191. public static String getOption(final String domain, final String option) {
  192. if (properties == null) {
  193. initialise();
  194. }
  195. return properties.getProperty(domain + "." + option);
  196. }
  197. /**
  198. * Returns the specified option.
  199. * @return the value of the specified option
  200. * @param domain the domain of the option
  201. * @param option the name of the option
  202. * @param fallback the balue to be returned if the option is not found
  203. */
  204. public static String getOption(final String domain, final String option, final String fallback) {
  205. if (properties == null) {
  206. initialise();
  207. }
  208. if (hasOption(domain, option)) {
  209. return getOption(domain, option);
  210. } else {
  211. return fallback;
  212. }
  213. }
  214. /**
  215. * Returns the specified option parsed as a boolean.
  216. * @return the boolean value of the specified option
  217. * @param domain the domain of the option
  218. * @param option the name of the option
  219. */
  220. public static boolean getOptionBool(final String domain, final String option) {
  221. if (properties == null) {
  222. initialise();
  223. }
  224. if (hasOption(domain, option)) {
  225. return Boolean.parseBoolean(getOption(domain, option));
  226. } else {
  227. return false;
  228. }
  229. }
  230. /**
  231. * Returns the specified option parsed as an integer.
  232. * @return the integer value of the specified option
  233. * @param domain the domain of the option
  234. * @param option the name of the option
  235. * @param fallback The value to use if the option can't be parsed
  236. */
  237. public static int getOptionInt(final String domain, final String option, final int fallback) {
  238. if (properties == null) {
  239. initialise();
  240. }
  241. if (!hasOption(domain, option)) {
  242. return fallback;
  243. }
  244. int res;
  245. try {
  246. res = Integer.parseInt(getOption(domain, option));
  247. } catch (NumberFormatException ex) {
  248. Logger.error(ErrorLevel.WARNING, "Invalid number format for " + domain + "." + option, ex);
  249. res = fallback;
  250. }
  251. return res;
  252. }
  253. /**
  254. * Returns the specified option parsed as a colour.
  255. * @return the colour object representing the specified option
  256. * @param domain the domain of the option
  257. * @param option the name of the option
  258. * @param fallback The value to use if the colour can't be parsed
  259. */
  260. public static Color getOptionColor(final String domain, final String option, final Color fallback) {
  261. if (properties == null) {
  262. initialise();
  263. }
  264. if (!hasOption(domain, option)) {
  265. return fallback;
  266. }
  267. return ColourManager.parseColour(getOption(domain, option), fallback);
  268. }
  269. /**
  270. * Returns the name of all the options in the specified domain.
  271. * @param domain The domain to search
  272. * @return A list of options in the specified domain
  273. */
  274. public static List<String> getOptions(final String domain) {
  275. if (properties == null) {
  276. initialise();
  277. }
  278. final ArrayList<String> res = new ArrayList<String>();
  279. for (Object key : properties.keySet()) {
  280. if (((String) key).startsWith(domain + ".")) {
  281. res.add(((String) key).substring(domain.length() + 1));
  282. }
  283. }
  284. return res;
  285. }
  286. /**
  287. * Returns the name of all domains in the global config.
  288. * @return A list of domains in this config
  289. */
  290. public static List<String> getDomains() {
  291. if (properties == null) {
  292. initialise();
  293. }
  294. final ArrayList<String> res = new ArrayList<String>();
  295. String domain;
  296. for (Object key : properties.keySet()) {
  297. domain = ((String) key).substring(0, ((String) key).indexOf('.'));
  298. if (!res.contains(domain)) {
  299. res.add(domain);
  300. }
  301. }
  302. return res;
  303. }
  304. /**
  305. * Sets a specified option.
  306. * @param domain domain of the option
  307. * @param option name of the option
  308. * @param value value of the option
  309. */
  310. public static void setOption(final String domain, final String option,
  311. final String value) {
  312. if (properties == null) {
  313. initialise();
  314. }
  315. properties.setProperty(domain + "." + option, value);
  316. }
  317. /**
  318. * Unsets a specified option.
  319. * @param domain domain of the option
  320. * @param option name of the option
  321. */
  322. public static void unsetOption(final String domain, final String option) {
  323. if (properties == null) {
  324. initialise();
  325. }
  326. properties.remove(domain + "." + option);
  327. }
  328. /**
  329. * Loads the config file from disc, if it exists else initialises defaults
  330. * and creates file.
  331. */
  332. private static void initialise() {
  333. properties = getDefaults();
  334. final File file = new File(getConfigFile());
  335. if (file.exists()) {
  336. try {
  337. properties.loadFromXML(new FileInputStream(file));
  338. } catch (InvalidPropertiesFormatException ex) {
  339. Logger.error(ErrorLevel.TRIVIAL, "Invalid properties file", ex);
  340. } catch (FileNotFoundException ex) {
  341. Logger.error(ErrorLevel.TRIVIAL, "No config file, using defaults");
  342. } catch (IOException ex) {
  343. Logger.error(ErrorLevel.WARNING, "Unable to load config file", ex);
  344. }
  345. } else {
  346. try {
  347. (new File(getConfigDir())).mkdirs();
  348. file.createNewFile();
  349. Config.save();
  350. } catch (IOException ex) {
  351. Logger.error(ErrorLevel.WARNING, "Unable to load config file", ex);
  352. }
  353. }
  354. }
  355. /**
  356. * Saves the config file to disc.
  357. */
  358. public static void save() {
  359. IdentityManager.save();
  360. if (properties == null) {
  361. return;
  362. }
  363. final Properties defaults = getDefaults();
  364. final Properties output = new Properties();
  365. final Enumeration<Object> keys = properties.keys();
  366. while (keys.hasMoreElements()) {
  367. final String key = (String) keys.nextElement();
  368. if (!defaults.containsKey(key) || !defaults.getProperty(key).equals(properties.getProperty(key))) {
  369. output.setProperty(key, properties.getProperty(key));
  370. }
  371. }
  372. try {
  373. output.storeToXML(new FileOutputStream(
  374. new File(getConfigFile())), null);
  375. } catch (FileNotFoundException ex) {
  376. Logger.error(ErrorLevel.TRIVIAL, "Unable to save config file", ex);
  377. } catch (IOException ex) {
  378. Logger.error(ErrorLevel.WARNING, "Unable to save config file", ex);
  379. }
  380. }
  381. }