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.

Set.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. * Copyright (c) 2006-2010 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.commandparser.commands.global;
  23. import com.dmdirc.FrameContainer;
  24. import com.dmdirc.commandparser.CommandArguments;
  25. import com.dmdirc.commandparser.CommandInfo;
  26. import com.dmdirc.commandparser.CommandManager;
  27. import com.dmdirc.commandparser.CommandType;
  28. import com.dmdirc.commandparser.commands.Command;
  29. import com.dmdirc.commandparser.commands.IntelligentCommand;
  30. import com.dmdirc.commandparser.commands.context.CommandContext;
  31. import com.dmdirc.config.ConfigManager;
  32. import com.dmdirc.config.Identity;
  33. import com.dmdirc.config.IdentityManager;
  34. import com.dmdirc.ui.input.AdditionalTabTargets;
  35. import java.util.List;
  36. /**
  37. * The set command allows the user to inspect and change global config settings.
  38. *
  39. * @author chris
  40. */
  41. public final class Set extends Command implements IntelligentCommand, CommandInfo {
  42. /**
  43. * Creates a new instance of Set.
  44. */
  45. public Set() {
  46. super();
  47. CommandManager.registerCommand(this);
  48. }
  49. /** {@inheritDoc} */
  50. @Override
  51. public void execute(final FrameContainer<?> origin,
  52. final CommandArguments args, final CommandContext context) {
  53. int i = 0;
  54. Identity identity = IdentityManager.getConfigIdentity();
  55. ConfigManager manager = IdentityManager.getGlobalConfig();
  56. if (args.getArguments().length > 0
  57. && "--server".equalsIgnoreCase(args.getArguments()[0]) && origin != null
  58. && origin.getServer() != null) {
  59. i = 1;
  60. identity = origin.getServer().getServerIdentity();
  61. manager = origin.getServer().getConfigManager();
  62. }
  63. switch (args.getArguments().length - i) {
  64. case 0:
  65. doDomainList(origin, args.isSilent(), manager);
  66. break;
  67. case 1:
  68. doOptionsList(origin, args.isSilent(), manager, args.getArguments()[i]);
  69. break;
  70. case 2:
  71. doShowOption(origin, args.isSilent(), manager, args.getArguments()[i],
  72. args.getArguments()[1 + i]);
  73. break;
  74. default:
  75. if (args.getArguments()[i].equalsIgnoreCase("--unset")) {
  76. doUnsetOption(origin, args.isSilent(), identity, args.getArguments()[1 + i],
  77. args.getArguments()[2 + i]);
  78. } else if (args.getArguments()[i].equalsIgnoreCase("--append")
  79. && args.getArguments().length > 3 + i) {
  80. doAppendOption(origin, args.isSilent(), identity, manager,
  81. args.getArguments()[1 + i], args.getArguments()[2 + i],
  82. args.getArgumentsAsString(3 + i));
  83. } else {
  84. doSetOption(origin, args.isSilent(), identity, args.getArguments()[i],
  85. args.getArguments()[1 + i], args.getArgumentsAsString(2 + i));
  86. }
  87. break;
  88. }
  89. }
  90. /**
  91. * Shows the user a list of valid domains.
  92. *
  93. * @param origin The window the command was issued from
  94. * @param isSilent Whether or not the command is being silenced or not
  95. * @param manager The config manager to use to retrieve data
  96. */
  97. private void doDomainList(final FrameContainer<?> origin, final boolean isSilent,
  98. final ConfigManager manager) {
  99. final StringBuffer output = new StringBuffer(67);
  100. output.append("Valid domains (use ");
  101. output.append(CommandManager.getCommandChar());
  102. output.append("set <domain> to see options within a domain): ");
  103. for (String domain : manager.getDomains()) {
  104. output.append(domain);
  105. output.append(", ");
  106. }
  107. sendLine(origin, isSilent, FORMAT_OUTPUT, output.substring(0, output.length() - 2));
  108. }
  109. /**
  110. * Shows the user a list of valid options within a domain.
  111. *
  112. * @param origin The window the command was issued from
  113. * @param isSilent Whether or not the command is being silenced or not
  114. * @param manager The config manager to use to retrieve data
  115. * @param domain The domain to be inspected
  116. */
  117. private void doOptionsList(final FrameContainer<?> origin,
  118. final boolean isSilent, final ConfigManager manager, final String domain) {
  119. final StringBuffer output = new StringBuffer(24);
  120. output.append("Options in domain '");
  121. output.append(domain);
  122. output.append("': ");
  123. boolean found = false;
  124. for (String option : manager.getOptions(domain).keySet()) {
  125. output.append(option);
  126. output.append(", ");
  127. found = true;
  128. }
  129. if (found) {
  130. sendLine(origin, isSilent, FORMAT_OUTPUT, output.substring(0, output.length() - 2));
  131. } else {
  132. sendLine(origin, isSilent, FORMAT_ERROR,
  133. "There are no options in the domain '" + domain + "'.");
  134. }
  135. }
  136. /**
  137. * Shows the user the current value of one option.
  138. *
  139. * @param origin The window the command was issued from
  140. * @param isSilent Whether or not the command is being silenced or not
  141. * @param manager The config manager to use to retrieve data
  142. * @param domain The domain of the option
  143. * @param option The name of the option
  144. */
  145. private void doShowOption(final FrameContainer<?> origin,
  146. final boolean isSilent, final ConfigManager manager,
  147. final String domain, final String option) {
  148. if (manager.hasOptionString(domain, option)) {
  149. sendLine(origin, isSilent, FORMAT_OUTPUT, "The current value of "
  150. + domain + "." + option + " is: " + manager.getOption(domain, option));
  151. } else {
  152. sendLine(origin, isSilent, FORMAT_ERROR, "Option not found: " + domain + "." + option);
  153. }
  154. }
  155. /**
  156. * Sets the value of the specified option.
  157. *
  158. * @param origin The window the command was issued from
  159. * @param isSilent Whether or not the command is being silenced or not
  160. * @param identity The identity to use to set data
  161. * @param domain The domain of the option
  162. * @param option The name of the option
  163. * @param newvalue The value the option should be set to
  164. */
  165. private void doSetOption(final FrameContainer<?> origin,
  166. final boolean isSilent, final Identity identity,
  167. final String domain, final String option, final String newvalue) {
  168. identity.setOption(domain, option, newvalue);
  169. sendLine(origin, isSilent, FORMAT_OUTPUT, domain + "." + option +
  170. " has been set to: " + newvalue);
  171. }
  172. /**
  173. * Appends data to the specified option.
  174. *
  175. * @param origin The window the command was issued from
  176. * @param isSilent Whether or not the command is being silenced or not
  177. * @param identity The identity to use to set data
  178. * @param manager The config manager to use to retrieve data
  179. * @param domain The domain of the option
  180. * @param option The name of the option
  181. * @param data The data to be appended
  182. */
  183. private void doAppendOption(final FrameContainer<?> origin,
  184. final boolean isSilent, final Identity identity, final ConfigManager manager,
  185. final String domain,final String option, final String data) {
  186. doSetOption(origin, isSilent, identity, domain, option,
  187. (manager.hasOptionString(domain, option) ?
  188. manager.getOption(domain, option) : "") + data);
  189. }
  190. /**
  191. * Unsets the specified option.
  192. *
  193. * @param origin The window the command was issued from
  194. * @param isSilent Whether or not the command is being silenced or not
  195. * @param identity The identity to use to set data
  196. * @param domain The domain of the option
  197. * @param option The name of the option
  198. */
  199. private void doUnsetOption(final FrameContainer<?> origin,
  200. final boolean isSilent, final Identity identity, final String domain,
  201. final String option) {
  202. identity.unsetOption(domain, option);
  203. sendLine(origin, isSilent, FORMAT_OUTPUT, domain + "." + option + " has been unset.");
  204. }
  205. /** {@inheritDoc} */
  206. @Override
  207. public String getName() {
  208. return "set";
  209. }
  210. /** {@inheritDoc} */
  211. @Override
  212. public boolean showInHelp() {
  213. return true;
  214. }
  215. /** {@inheritDoc} */
  216. @Override
  217. public CommandType getType() {
  218. return CommandType.TYPE_GLOBAL;
  219. }
  220. /** {@inheritDoc} */
  221. @Override
  222. public String getHelp() {
  223. return "set [--server] [domain [option [newvalue]]] - inspect or change configuration settings"
  224. + "\nset [--server] --append <domain> <option> <data> - appends data to the specified option"
  225. + "\nset [--server] --unset <domain> <option> - unsets the specified option";
  226. }
  227. /** {@inheritDoc} */
  228. @Override
  229. public AdditionalTabTargets getSuggestions(final int arg,
  230. final IntelligentCommandContext context) {
  231. final List<String> previousArgs = context.getPreviousArgs();
  232. final AdditionalTabTargets res = new AdditionalTabTargets();
  233. if (arg == 0) {
  234. res.addAll(context.getWindow().getContainer().getConfigManager()
  235. .getDomains());
  236. res.add("--unset");
  237. res.add("--append");
  238. res.add("--server");
  239. res.excludeAll();
  240. } else if (arg == 1 && previousArgs.size() >= 1) {
  241. if (previousArgs.get(0).equalsIgnoreCase("--unset")
  242. || previousArgs.get(0).equalsIgnoreCase("--append")
  243. || previousArgs.get(0).equalsIgnoreCase("--server")) {
  244. res.addAll(context.getWindow().getContainer().getConfigManager()
  245. .getDomains());
  246. } else {
  247. res.addAll(context.getWindow().getContainer().getConfigManager()
  248. .getOptions(previousArgs.get(0)).keySet());
  249. }
  250. res.excludeAll();
  251. } else if (arg == 2 && (previousArgs.get(0).equalsIgnoreCase("--unset")
  252. || previousArgs.get(0).equalsIgnoreCase("--append")
  253. || previousArgs.get(0).equalsIgnoreCase("--server"))) {
  254. res.addAll(context.getWindow().getContainer().getConfigManager()
  255. .getOptions(previousArgs.get(1)).keySet());
  256. res.excludeAll();
  257. }
  258. return res;
  259. }
  260. }