Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

NotificationCommand.java 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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.FrameContainer;
  24. import com.dmdirc.commandparser.BaseCommandInfo;
  25. import com.dmdirc.commandparser.CommandArguments;
  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.ui.input.AdditionalTabTargets;
  32. import java.util.Collection;
  33. import javax.annotation.Nonnull;
  34. /**
  35. * Notification command, delegating notification to one of the registered notification commands as
  36. * preferred by the end user.
  37. */
  38. public class NotificationCommand extends Command implements
  39. IntelligentCommand {
  40. /** A command info object for this command. */
  41. public static final BaseCommandInfo 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 FrameContainer 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. sendLine(origin, args.isSilent(), FORMAT_ERROR,
  72. "Method not found.");
  73. } else {
  74. handler.showNotification("DMDirc", args.getArgumentsAsString(2));
  75. }
  76. } else {
  77. sendLine(origin, args.isSilent(), FORMAT_ERROR,
  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. sendLine(origin, args.isSilent(), FORMAT_ERROR,
  84. "No active notification methods available.");
  85. }
  86. }
  87. /**
  88. * Outputs a list of methods for the notifcation command.
  89. *
  90. * @param origin The input window where the command was entered
  91. * @param isSilent Whether this command is being silenced
  92. */
  93. private void doMethodList(final FrameContainer origin,
  94. final boolean isSilent) {
  95. final Collection<String> handlers = manager.getHandlerNames();
  96. if (handlers.isEmpty()) {
  97. sendLine(origin, isSilent, FORMAT_ERROR, "No notification "
  98. + "methods available.");
  99. } else {
  100. final String[] headers = {"Method"};
  101. final String[][] data = new String[handlers.size()][1];
  102. int i = 0;
  103. for (String handler : handlers) {
  104. data[i][0] = handler;
  105. i++;
  106. }
  107. sendLine(origin, isSilent, FORMAT_OUTPUT, doTable(headers, data));
  108. }
  109. }
  110. @Override
  111. public AdditionalTabTargets getSuggestions(final int arg,
  112. final IntelligentCommandContext context) {
  113. final AdditionalTabTargets res = new AdditionalTabTargets();
  114. res.excludeAll();
  115. if (arg == 0) {
  116. res.add("--methods");
  117. res.add("--method");
  118. return res;
  119. } else if (arg == 1 && "--method".equalsIgnoreCase(context.getPreviousArgs().get(0))) {
  120. res.addAll(manager.getHandlerNames());
  121. return res;
  122. }
  123. return res;
  124. }
  125. }