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.

Set.java 11KB

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