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.

NotificationCommand.java 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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.addons.notifications;
  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.ui.input.AdditionalTabTargets;
  28. import javax.annotation.Nonnull;
  29. import java.util.Collection;
  30. /**
  31. * Notification command, delegating notification to one of the registered notification commands as
  32. * preferred by the end user.
  33. */
  34. public class NotificationCommand extends BaseCommand implements IntelligentCommand {
  35. /** A command info object for this command. */
  36. public static final CommandInfo INFO = new BaseCommandInfo(
  37. "notification",
  38. "notification [--methods|--method <method>] text - "
  39. + "Notifies you of the text",
  40. CommandType.TYPE_GLOBAL);
  41. /** The notifications manager to get notification plugins from. */
  42. private final NotificationsManager manager;
  43. /**
  44. * Creates a new instance of this notification command.
  45. *
  46. * @param controller The controller to use for command information.
  47. * @param manager The notifications manager to get notification plugins from
  48. */
  49. public NotificationCommand(final CommandController controller,
  50. final NotificationsManager manager) {
  51. super(controller);
  52. this.manager = manager;
  53. }
  54. @Override
  55. public void execute(@Nonnull final WindowModel origin,
  56. final CommandArguments args, final CommandContext context) {
  57. if (args.getArguments().length > 0 &&
  58. "--methods".equalsIgnoreCase(args.getArguments()[0])) {
  59. doMethodList(origin, args.isSilent());
  60. } else if (args.getArguments().length > 0 &&
  61. "--method".equalsIgnoreCase(args.getArguments()[0])) {
  62. if (args.getArguments().length > 1) {
  63. final String sourceName = args.getArguments()[1];
  64. final NotificationHandler handler = manager.getHandler(sourceName);
  65. if (handler == null) {
  66. showError(origin, args.isSilent(), "Method not found.");
  67. } else {
  68. handler.showNotification("DMDirc", args.getArgumentsAsString(2));
  69. }
  70. } else {
  71. showError(origin, args.isSilent(),
  72. "You must specify a method when using --method.");
  73. }
  74. } else if (manager.hasActiveHandler()) {
  75. manager.getPreferredHandler().showNotification("DMDirc", args.getArgumentsAsString(0));
  76. } else {
  77. showError(origin, args.isSilent(), "No active notification methods available.");
  78. }
  79. }
  80. /**
  81. * Outputs a list of methods for the notifcation command.
  82. *
  83. * @param origin The input window where the command was entered
  84. * @param isSilent Whether this command is being silenced
  85. */
  86. private void doMethodList(final WindowModel origin,
  87. final boolean isSilent) {
  88. final Collection<String> handlers = manager.getHandlerNames();
  89. if (handlers.isEmpty()) {
  90. showError(origin, isSilent, "No notification methods available.");
  91. } else {
  92. final String[] headers = {"Method"};
  93. final String[][] data = new String[handlers.size()][1];
  94. int i = 0;
  95. for (String handler : handlers) {
  96. data[i][0] = handler;
  97. i++;
  98. }
  99. showOutput(origin, isSilent, doTable(headers, data));
  100. }
  101. }
  102. @Override
  103. public AdditionalTabTargets getSuggestions(final int arg,
  104. final IntelligentCommandContext context) {
  105. final AdditionalTabTargets res = new AdditionalTabTargets();
  106. res.excludeAll();
  107. if (arg == 0) {
  108. res.add("--methods");
  109. res.add("--method");
  110. return res;
  111. } else if (arg == 1 && "--method".equalsIgnoreCase(context.getPreviousArgs().get(0))) {
  112. res.addAll(manager.getHandlerNames());
  113. return res;
  114. }
  115. return res;
  116. }
  117. }