Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

ConfigManager.java 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 com.dmdirc.identities;
  23. import java.awt.Color;
  24. import java.util.Collections;
  25. import java.util.List;
  26. import com.dmdirc.logger.ErrorLevel;
  27. import com.dmdirc.logger.Logger;
  28. import com.dmdirc.ui.messages.ColourManager;
  29. /**
  30. * The config manager manages the various config sources for each entity.
  31. * @author chris
  32. */
  33. public final class ConfigManager {
  34. /** A list of sources for this config manager. */
  35. private List<ConfigSource> sources;
  36. /** The ircd this manager is for. */
  37. private final String ircd;
  38. /** The network this manager is for. */
  39. private final String network;
  40. /** The server this manager is for. */
  41. private final String server;
  42. /** The channel this manager is for. */
  43. private final String channel;
  44. /**
  45. * Creates a new instance of ConfigManager.
  46. * @param ircd The name of the ircd for this manager
  47. * @param network The name of the network for this manager
  48. * @param server The name of the server for this manager
  49. */
  50. public ConfigManager(final String ircd, final String network,
  51. final String server) {
  52. this(ircd, network, server, "<Unknown>");
  53. }
  54. /**
  55. * Creates a new instance of ConfigManager.
  56. * @param ircd The name of the ircd for this manager
  57. * @param network The name of the network for this manager
  58. * @param server The name of the server for this manager
  59. * @param channel The name of the channel for this manager
  60. */
  61. public ConfigManager(final String ircd, final String network,
  62. final String server, final String channel) {
  63. final String chanName = channel + "@" + network;
  64. sources = IdentityManager.getSources(ircd, network, server, chanName);
  65. this.ircd = ircd;
  66. this.network = network;
  67. this.server = server;
  68. this.channel = chanName;
  69. IdentityManager.addConfigManager(this);
  70. }
  71. /**
  72. * Retrieves the specified option.
  73. * @param domain The domain of the option
  74. * @param option The name of the option
  75. * @return The value of the option
  76. */
  77. public String getOption(final String domain, final String option) {
  78. for (ConfigSource source : sources) {
  79. if (source.hasOption(domain, option)) {
  80. return source.getOption(domain, option);
  81. }
  82. }
  83. throw new IndexOutOfBoundsException("Config option not found: " + domain + "." + option);
  84. }
  85. /**
  86. * Retrieves a colour representation of the specified option.
  87. * @param domain The domain of the option
  88. * @param option The name of the option
  89. * @param fallback The colour that should be used in case of error
  90. * @return The colour representation of the option
  91. */
  92. public Color getOptionColour(final String domain, final String option,
  93. final Color fallback) {
  94. if (!hasOption(domain, option)) {
  95. return fallback;
  96. }
  97. return ColourManager.parseColour(getOption(domain, option), fallback);
  98. }
  99. /**
  100. * Retrieves a boolean representation of the specified option.
  101. * @param domain The domain of the option
  102. * @param option The name of the option
  103. * @return The boolean representation of the option
  104. */
  105. public boolean getOptionBool(final String domain, final String option) {
  106. if (!hasOption(domain, option)) {
  107. return false;
  108. }
  109. return Boolean.parseBoolean(getOption(domain, option));
  110. }
  111. /**
  112. * Retrieves an integral representation of the specified option.
  113. * @param domain The domain of the option
  114. * @param option The name of the option
  115. * @param fallback The value to use if the config isn't valud
  116. * @return The integer representation of the option
  117. */
  118. public int getOptionInt(final String domain, final String option,
  119. final int fallback) {
  120. if (!hasOption(domain, option)) {
  121. return fallback;
  122. }
  123. int res;
  124. try {
  125. res = Integer.parseInt(getOption(domain, option));
  126. } catch (NumberFormatException ex) {
  127. Logger.error(ErrorLevel.WARNING, "Invalid number format for " + domain + "." + option, ex);
  128. res = fallback;
  129. }
  130. return res;
  131. }
  132. /**
  133. * Returns the scope of the specified option.
  134. * @param domain The domain of the option
  135. * @param option The name of the option
  136. * @return The scope of the option
  137. */
  138. public ConfigTarget getOptionScope(final String domain, final String option) {
  139. for (ConfigSource source : sources) {
  140. if (source.hasOption(domain, option)) {
  141. return source.getTarget();
  142. }
  143. }
  144. throw new IndexOutOfBoundsException("Config option not found: " + domain + "." + option);
  145. }
  146. /**
  147. * Determines if this manager has the specified option.
  148. * @param domain The domain of the option
  149. * @param option The name of the option
  150. * @return True iff the option exists, false otherwise.
  151. */
  152. public boolean hasOption(final String domain, final String option) {
  153. for (ConfigSource source : sources) {
  154. if (source.hasOption(domain, option)) {
  155. return true;
  156. }
  157. }
  158. return false;
  159. }
  160. /**
  161. * Called whenever there is a new identity available. Checks if the
  162. * identity is relevant for this manager, and adds it if it is.
  163. * @param identity The identity to be checked
  164. */
  165. public void checkIdentity(final Identity identity) {
  166. String comp = "";
  167. switch (identity.getTarget().getType()) {
  168. case ConfigTarget.TYPE_IRCD:
  169. comp = ircd;
  170. break;
  171. case ConfigTarget.TYPE_NETWORK:
  172. comp = network;
  173. break;
  174. case ConfigTarget.TYPE_SERVER:
  175. comp = server;
  176. break;
  177. case ConfigTarget.TYPE_CHANNEL:
  178. comp = channel;
  179. break;
  180. default:
  181. comp = "<Unknown>";
  182. break;
  183. }
  184. if (comp != null && comp.equalsIgnoreCase(identity.getTarget().getData())) {
  185. sources.add(identity);
  186. Collections.sort(sources);
  187. }
  188. }
  189. /**
  190. * Retrieves a list of sources for this config manager.
  191. * @return This config manager's sources.
  192. */
  193. public List<ConfigSource> getSources() {
  194. return sources;
  195. }
  196. }