123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291 |
- /*
- * Copyright (c) 2006-2010 Chris Smith, Shane Mc Cormack, Gregory Holmes
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
- package com.dmdirc.commandparser.commands.global;
-
- import com.dmdirc.FrameContainer;
- import com.dmdirc.commandparser.CommandArguments;
- import com.dmdirc.commandparser.CommandInfo;
- import com.dmdirc.commandparser.CommandManager;
- import com.dmdirc.commandparser.CommandType;
- import com.dmdirc.commandparser.commands.Command;
- import com.dmdirc.commandparser.commands.IntelligentCommand;
- import com.dmdirc.commandparser.commands.context.CommandContext;
- import com.dmdirc.config.ConfigManager;
- import com.dmdirc.config.Identity;
- import com.dmdirc.config.IdentityManager;
- import com.dmdirc.ui.input.AdditionalTabTargets;
-
- import java.util.List;
-
- /**
- * The set command allows the user to inspect and change global config settings.
- *
- * @author chris
- */
- public final class Set extends Command implements IntelligentCommand, CommandInfo {
-
- /**
- * Creates a new instance of Set.
- */
- public Set() {
- super();
- }
-
- /** {@inheritDoc} */
- @Override
- public void execute(final FrameContainer<?> origin,
- final CommandArguments args, final CommandContext context) {
- int i = 0;
-
- Identity identity = IdentityManager.getConfigIdentity();
- ConfigManager manager = IdentityManager.getGlobalConfig();
-
- if (args.getArguments().length > 0
- && "--server".equalsIgnoreCase(args.getArguments()[0]) && origin != null
- && origin.getServer() != null) {
- i = 1;
- identity = origin.getServer().getServerIdentity();
- manager = origin.getServer().getConfigManager();
- }
-
- switch (args.getArguments().length - i) {
- case 0:
- doDomainList(origin, args.isSilent(), manager);
- break;
- case 1:
- doOptionsList(origin, args.isSilent(), manager, args.getArguments()[i]);
- break;
- case 2:
- doShowOption(origin, args.isSilent(), manager, args.getArguments()[i],
- args.getArguments()[1 + i]);
- break;
- default:
- if (args.getArguments()[i].equalsIgnoreCase("--unset")) {
- doUnsetOption(origin, args.isSilent(), identity, args.getArguments()[1 + i],
- args.getArguments()[2 + i]);
- } else if (args.getArguments()[i].equalsIgnoreCase("--append")
- && args.getArguments().length > 3 + i) {
- doAppendOption(origin, args.isSilent(), identity, manager,
- args.getArguments()[1 + i], args.getArguments()[2 + i],
- args.getArgumentsAsString(3 + i));
- } else {
- doSetOption(origin, args.isSilent(), identity, args.getArguments()[i],
- args.getArguments()[1 + i], args.getArgumentsAsString(2 + i));
- }
- break;
- }
- }
-
- /**
- * Shows the user a list of valid domains.
- *
- * @param origin The window the command was issued from
- * @param isSilent Whether or not the command is being silenced or not
- * @param manager The config manager to use to retrieve data
- */
- private void doDomainList(final FrameContainer<?> origin, final boolean isSilent,
- final ConfigManager manager) {
- final StringBuffer output = new StringBuffer(67);
-
- output.append("Valid domains (use ");
- output.append(CommandManager.getCommandChar());
- output.append("set <domain> to see options within a domain): ");
-
- for (String domain : manager.getDomains()) {
- output.append(domain);
- output.append(", ");
- }
-
- sendLine(origin, isSilent, FORMAT_OUTPUT, output.substring(0, output.length() - 2));
- }
-
- /**
- * Shows the user a list of valid options within a domain.
- *
- * @param origin The window the command was issued from
- * @param isSilent Whether or not the command is being silenced or not
- * @param manager The config manager to use to retrieve data
- * @param domain The domain to be inspected
- */
- private void doOptionsList(final FrameContainer<?> origin,
- final boolean isSilent, final ConfigManager manager, final String domain) {
- final StringBuffer output = new StringBuffer(24);
-
- output.append("Options in domain '");
- output.append(domain);
- output.append("': ");
-
- boolean found = false;
-
- for (String option : manager.getOptions(domain).keySet()) {
- output.append(option);
- output.append(", ");
- found = true;
- }
-
- if (found) {
- sendLine(origin, isSilent, FORMAT_OUTPUT, output.substring(0, output.length() - 2));
- } else {
- sendLine(origin, isSilent, FORMAT_ERROR,
- "There are no options in the domain '" + domain + "'.");
- }
- }
-
- /**
- * Shows the user the current value of one option.
- *
- * @param origin The window the command was issued from
- * @param isSilent Whether or not the command is being silenced or not
- * @param manager The config manager to use to retrieve data
- * @param domain The domain of the option
- * @param option The name of the option
- */
- private void doShowOption(final FrameContainer<?> origin,
- final boolean isSilent, final ConfigManager manager,
- final String domain, final String option) {
- if (manager.hasOptionString(domain, option)) {
- sendLine(origin, isSilent, FORMAT_OUTPUT, "The current value of "
- + domain + "." + option + " is: " + manager.getOption(domain, option));
- } else {
- sendLine(origin, isSilent, FORMAT_ERROR, "Option not found: " + domain + "." + option);
- }
- }
-
- /**
- * Sets the value of the specified option.
- *
- * @param origin The window the command was issued from
- * @param isSilent Whether or not the command is being silenced or not
- * @param identity The identity to use to set data
- * @param domain The domain of the option
- * @param option The name of the option
- * @param newvalue The value the option should be set to
- */
- private void doSetOption(final FrameContainer<?> origin,
- final boolean isSilent, final Identity identity,
- final String domain, final String option, final String newvalue) {
- identity.setOption(domain, option, newvalue);
-
- sendLine(origin, isSilent, FORMAT_OUTPUT, domain + "." + option
- + " has been set to: " + newvalue);
- }
-
- /**
- * Appends data to the specified option.
- *
- * @param origin The window the command was issued from
- * @param isSilent Whether or not the command is being silenced or not
- * @param identity The identity to use to set data
- * @param manager The config manager to use to retrieve data
- * @param domain The domain of the option
- * @param option The name of the option
- * @param data The data to be appended
- */
- private void doAppendOption(final FrameContainer<?> origin,
- final boolean isSilent, final Identity identity, final ConfigManager manager,
- final String domain, final String option, final String data) {
- doSetOption(origin, isSilent, identity, domain, option,
- (manager.hasOptionString(domain, option)
- ? manager.getOption(domain, option) : "") + data);
- }
-
- /**
- * Unsets the specified option.
- *
- * @param origin The window the command was issued from
- * @param isSilent Whether or not the command is being silenced or not
- * @param identity The identity to use to set data
- * @param domain The domain of the option
- * @param option The name of the option
- */
- private void doUnsetOption(final FrameContainer<?> origin,
- final boolean isSilent, final Identity identity, final String domain,
- final String option) {
- identity.unsetOption(domain, option);
-
- sendLine(origin, isSilent, FORMAT_OUTPUT, domain + "." + option + " has been unset.");
- }
-
- /** {@inheritDoc} */
- @Override
- public String getName() {
- return "set";
- }
-
- /** {@inheritDoc} */
- @Override
- public boolean showInHelp() {
- return true;
- }
-
- /** {@inheritDoc} */
- @Override
- public CommandType getType() {
- return CommandType.TYPE_GLOBAL;
- }
-
- /** {@inheritDoc} */
- @Override
- public String getHelp() {
- return "set [--server] [domain [option [newvalue]]] - inspect or change configuration settings"
- + "\nset [--server] --append <domain> <option> <data> - appends data to the specified option"
- + "\nset [--server] --unset <domain> <option> - unsets the specified option";
- }
-
- /** {@inheritDoc} */
- @Override
- public AdditionalTabTargets getSuggestions(final int arg,
- final IntelligentCommandContext context) {
- final List<String> previousArgs = context.getPreviousArgs();
- final AdditionalTabTargets res = new AdditionalTabTargets();
-
- if (arg == 0) {
- res.addAll(context.getWindow().getContainer().getConfigManager()
- .getDomains());
- res.add("--unset");
- res.add("--append");
- res.add("--server");
- res.excludeAll();
- } else if (arg == 1 && previousArgs.size() >= 1) {
- if (previousArgs.get(0).equalsIgnoreCase("--unset")
- || previousArgs.get(0).equalsIgnoreCase("--append")
- || previousArgs.get(0).equalsIgnoreCase("--server")) {
- res.addAll(context.getWindow().getContainer().getConfigManager()
- .getDomains());
- } else {
- res.addAll(context.getWindow().getContainer().getConfigManager()
- .getOptions(previousArgs.get(0)).keySet());
- }
- res.excludeAll();
- } else if (arg == 2 && (previousArgs.get(0).equalsIgnoreCase("--unset")
- || previousArgs.get(0).equalsIgnoreCase("--append")
- || previousArgs.get(0).equalsIgnoreCase("--server"))) {
- res.addAll(context.getWindow().getContainer().getConfigManager()
- .getOptions(previousArgs.get(1)).keySet());
- res.excludeAll();
- }
-
- return res;
- }
-
- }
|