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.

LoadPlugin.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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.commandparser.BaseCommandInfo;
  19. import com.dmdirc.commandparser.CommandArguments;
  20. import com.dmdirc.commandparser.CommandInfo;
  21. import com.dmdirc.commandparser.CommandType;
  22. import com.dmdirc.commandparser.commands.BaseCommand;
  23. import com.dmdirc.commandparser.commands.IntelligentCommand;
  24. import com.dmdirc.commandparser.commands.context.CommandContext;
  25. import com.dmdirc.interfaces.CommandController;
  26. import com.dmdirc.interfaces.WindowModel;
  27. import com.dmdirc.plugins.PluginInfo;
  28. import com.dmdirc.plugins.PluginManager;
  29. import com.dmdirc.plugins.PluginMetaData;
  30. import com.dmdirc.ui.input.AdditionalTabTargets;
  31. import javax.annotation.Nonnull;
  32. import javax.inject.Inject;
  33. import java.util.stream.Collectors;
  34. /**
  35. * Allows the user to load a plugin.
  36. */
  37. public class LoadPlugin extends BaseCommand implements IntelligentCommand {
  38. /** A command info object for this command. */
  39. public static final CommandInfo INFO = new BaseCommandInfo("loadplugin",
  40. "loadplugin <plugin> - loads the specified class as a plugin",
  41. CommandType.TYPE_GLOBAL);
  42. /** The plugin manager to use to load plugins. */
  43. private final PluginManager pluginManager;
  44. /**
  45. * Creates a new instance of the {@link LoadPlugin} command.
  46. *
  47. * @param controller The controller to use for command information.
  48. * @param pluginManager The plugin manager to load plugins with.
  49. */
  50. @Inject
  51. public LoadPlugin(final CommandController controller, final PluginManager pluginManager) {
  52. super(controller);
  53. this.pluginManager = pluginManager;
  54. }
  55. @Override
  56. public void execute(@Nonnull final WindowModel origin,
  57. final CommandArguments args, final CommandContext context) {
  58. if (args.getArguments().length == 0) {
  59. showUsage(origin, args.isSilent(), "loadplugin", "<plugin>");
  60. return;
  61. }
  62. // Add previously unknown plugin to plugin manager
  63. pluginManager.addPlugin(args.getArgumentsAsString());
  64. final PluginInfo plugin = pluginManager.getPluginInfo(args.getArgumentsAsString());
  65. if (plugin == null) {
  66. showError(origin, args.isSilent(), "Plugin loading failed");
  67. } else if (plugin.isLoaded()) {
  68. showError(origin, args.isSilent(), "Plugin already loaded.");
  69. } else {
  70. plugin.loadPlugin();
  71. if (plugin.isLoaded()) {
  72. showOutput(origin, args.isSilent(), "Plugin loaded.");
  73. pluginManager.updateAutoLoad(plugin);
  74. } else {
  75. showOutput(origin, args.isSilent(),
  76. "Loading plugin failed. (" + plugin.getLastError() + ')');
  77. }
  78. }
  79. }
  80. @Override
  81. public AdditionalTabTargets getSuggestions(final int arg,
  82. final IntelligentCommandContext context) {
  83. final AdditionalTabTargets res = new AdditionalTabTargets();
  84. res.excludeAll();
  85. if (arg == 0) {
  86. res.addAll(
  87. pluginManager.getAllPlugins().stream().map(PluginMetaData::getRelativeFilename)
  88. .collect(Collectors.toList()));
  89. }
  90. return res;
  91. }
  92. }