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.

Ifplugin.java 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.commandparser.commands.global;
  18. import com.dmdirc.GlobalWindow;
  19. import com.dmdirc.commandparser.BaseCommandInfo;
  20. import com.dmdirc.commandparser.CommandArguments;
  21. import com.dmdirc.commandparser.CommandInfo;
  22. import com.dmdirc.commandparser.CommandType;
  23. import com.dmdirc.commandparser.commands.BaseCommand;
  24. import com.dmdirc.commandparser.commands.IntelligentCommand;
  25. import com.dmdirc.commandparser.commands.context.CommandContext;
  26. import com.dmdirc.commandparser.parsers.CommandParser;
  27. import com.dmdirc.commandparser.parsers.GlobalCommandParser;
  28. import com.dmdirc.interfaces.CommandController;
  29. import com.dmdirc.interfaces.InputModel;
  30. import com.dmdirc.interfaces.WindowModel;
  31. import com.dmdirc.plugins.PluginInfo;
  32. import com.dmdirc.plugins.PluginManager;
  33. import com.dmdirc.ui.input.AdditionalTabTargets;
  34. import com.dmdirc.ui.input.TabCompleterUtils;
  35. import javax.annotation.Nonnull;
  36. import javax.inject.Inject;
  37. import javax.inject.Provider;
  38. import java.util.Optional;
  39. /**
  40. * The if plugin command allows the user to execute commands based on whether or not a plugin is
  41. * loaded.
  42. */
  43. public class Ifplugin extends BaseCommand implements IntelligentCommand {
  44. /** A command info object for this command. */
  45. public static final CommandInfo INFO = new BaseCommandInfo("ifplugin",
  46. "ifplugin <[!]plugin> <command> - executes a command if the "
  47. + "specified plugin is/isn't loaded",
  48. CommandType.TYPE_GLOBAL);
  49. /** The plugin manager to use to query plugins. */
  50. private final PluginManager pluginManager;
  51. /** Provider of global command parsers. */
  52. private final Provider<GlobalCommandParser> globalCommandParserProvider;
  53. /** Provider of global windows. */
  54. private final Provider<GlobalWindow> globalWindowProvider;
  55. /** Tab-completer utilities. */
  56. private final TabCompleterUtils tabCompleterUtils;
  57. /**
  58. * Creates a new instance of the {@link Ifplugin} command.
  59. *
  60. * @param controller The controller to use for command information.
  61. * @param pluginManager The plugin manager to use to query plugins.
  62. * @param globalCommandParserProvider Provider to use to retrieve a global command parser.
  63. * @param globalWindowProvider Provider to use to retrieve a global window.
  64. */
  65. @Inject
  66. public Ifplugin(
  67. final CommandController controller,
  68. final PluginManager pluginManager,
  69. final Provider<GlobalCommandParser> globalCommandParserProvider,
  70. final Provider<GlobalWindow> globalWindowProvider,
  71. final TabCompleterUtils tabCompleterUtils) {
  72. super(controller);
  73. this.pluginManager = pluginManager;
  74. this.globalCommandParserProvider = globalCommandParserProvider;
  75. this.globalWindowProvider = globalWindowProvider;
  76. this.tabCompleterUtils = tabCompleterUtils;
  77. }
  78. @Override
  79. public void execute(@Nonnull final WindowModel origin,
  80. final CommandArguments args, final CommandContext context) {
  81. if (args.getArguments().length <= 1) {
  82. showUsage(origin, args.isSilent(), "ifplugin", "<[!]plugin> <command>");
  83. return;
  84. }
  85. final boolean negative = args.getArguments()[0].charAt(0) == '!';
  86. final String pname = args.getArguments()[0].substring(negative ? 1 : 0);
  87. final PluginInfo pluginInfo = pluginManager.getPluginInfoByName(pname);
  88. boolean result = true;
  89. if (pluginInfo == null || !pluginInfo.isLoaded()) {
  90. result = false;
  91. }
  92. if (result != negative) {
  93. final Optional<CommandParser> parser = origin.getInputModel()
  94. .map(InputModel::getCommandParser);
  95. if (parser.isPresent()) {
  96. parser.get().parseCommand(origin, args.getArgumentsAsString(1));
  97. } else {
  98. globalCommandParserProvider.get()
  99. .parseCommand(globalWindowProvider.get(), args.getArgumentsAsString(1));
  100. }
  101. }
  102. }
  103. @Override
  104. public AdditionalTabTargets getSuggestions(final int arg,
  105. final IntelligentCommandContext context) {
  106. final AdditionalTabTargets res;
  107. if (arg == 0) {
  108. res = new AdditionalTabTargets().excludeAll();
  109. for (PluginInfo possPlugin : pluginManager.getPluginInfos()) {
  110. res.add(possPlugin.getMetaData().getName());
  111. res.add('!' + possPlugin.getMetaData().getName());
  112. }
  113. } else {
  114. res = tabCompleterUtils.getIntelligentResults(arg, context, 1);
  115. }
  116. return res;
  117. }
  118. }