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

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