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.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Copyright (c) 2006-2011 Chris Smith, Shane Mc Cormack, Gregory Holmes
  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.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.plugins.ExportedService;
  31. import com.dmdirc.plugins.PluginInfo;
  32. import com.dmdirc.ui.input.AdditionalTabTargets;
  33. import java.util.List;
  34. /**
  35. * Notification command, delegating notification to one of the registered
  36. * notification commands as preferred by the end user.
  37. */
  38. public class NotificationCommand extends Command implements
  39. IntelligentCommand, CommandInfo {
  40. /** The plugin that's using this command. */
  41. private final NotificationsPlugin parent;
  42. /**
  43. * Creates a new instance of this notification command.
  44. *
  45. * @param parent The plugin that's instansiating this command
  46. */
  47. public NotificationCommand(final NotificationsPlugin parent) {
  48. super();
  49. this.parent = parent;
  50. }
  51. /** {@inheritDoc} */
  52. @Override
  53. public void execute(final FrameContainer<?> origin,
  54. final CommandArguments args, final CommandContext context) {
  55. if (args.getArguments().length > 0 && args.getArguments()[0]
  56. .equalsIgnoreCase("--methods")) {
  57. doMethodList(origin, args.isSilent());
  58. } else if (args.getArguments().length > 0 && args.getArguments()[0]
  59. .equalsIgnoreCase("--method")) {
  60. if (args.getArguments().length > 1) {
  61. final String sourceName = args.getArguments()[1];
  62. final ExportedService source = parent.getMethod(sourceName)
  63. .getExportedService("showNotification");
  64. if (source == null) {
  65. sendLine(origin, args.isSilent(), FORMAT_ERROR,
  66. "Method not found.");
  67. } else {
  68. source.execute("DMDirc", args.getArgumentsAsString(2));
  69. }
  70. } else {
  71. sendLine(origin, args.isSilent(), FORMAT_ERROR,
  72. "You must specify a method when using --method.");
  73. }
  74. } else if (parent.hasActiveMethod()) {
  75. parent.getPreferredMethod().getExportedService("showNotification")
  76. .execute("DMDirc", args.getArgumentsAsString(0));
  77. } else {
  78. sendLine(origin, args.isSilent(), FORMAT_ERROR,
  79. "No active notification methods available.");
  80. }
  81. }
  82. /**
  83. * Outputs a list of methods for the notifcation command.
  84. *
  85. * @param origin The input window where the command was entered
  86. * @param isSilent Whether this command is being silenced
  87. */
  88. private void doMethodList(final FrameContainer<?> origin,
  89. final boolean isSilent) {
  90. final List<PluginInfo> methods = parent.getMethods();
  91. if (methods.isEmpty()) {
  92. sendLine(origin, isSilent, FORMAT_ERROR, "No notification "
  93. + "methods available.");
  94. } else {
  95. final String[] headers = {"Method"};
  96. final String[][] data = new String[methods.size()][1];
  97. int i = 0;
  98. for (PluginInfo method : methods) {
  99. data[i][0] = method.getName();
  100. i++;
  101. }
  102. sendLine(origin, isSilent, FORMAT_OUTPUT, doTable(headers, data));
  103. }
  104. }
  105. /** {@inheritDoc} */
  106. @Override
  107. public AdditionalTabTargets getSuggestions(final int arg,
  108. final IntelligentCommandContext context) {
  109. final AdditionalTabTargets res = new AdditionalTabTargets();
  110. res.excludeAll();
  111. if (arg == 0) {
  112. res.add("--methods");
  113. res.add("--method");
  114. return res;
  115. } else if (arg == 1 && context.getPreviousArgs().get(0)
  116. .equalsIgnoreCase("--method")) {
  117. for (PluginInfo source : parent.getMethods()) {
  118. res.add(source.getName());
  119. }
  120. return res;
  121. }
  122. return res;
  123. }
  124. /** {@inheritDoc} */
  125. @Override
  126. public String getName() {
  127. return "notification";
  128. }
  129. /** {@inheritDoc} */
  130. @Override
  131. public boolean showInHelp() {
  132. return true;
  133. }
  134. /** {@inheritDoc} */
  135. @Override
  136. public String getHelp() {
  137. return "notification [--methods|--method <method>] text - "
  138. + "Notifies you of the text";
  139. }
  140. /** {@inheritDoc} */
  141. @Override
  142. public CommandType getType() {
  143. return CommandType.TYPE_GLOBAL;
  144. }
  145. }