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

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