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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Copyright (c) 2006-2013 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.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 {
  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 plugin that's using this command. */
  47. private final NotificationsPlugin parent;
  48. /**
  49. * Creates a new instance of this notification command.
  50. *
  51. * @param parent The plugin that's instantiating this command
  52. */
  53. public NotificationCommand(final NotificationsPlugin parent) {
  54. super();
  55. this.parent = parent;
  56. }
  57. /** {@inheritDoc} */
  58. @Override
  59. public void execute(final FrameContainer origin,
  60. final CommandArguments args, final CommandContext context) {
  61. if (args.getArguments().length > 0 && args.getArguments()[0]
  62. .equalsIgnoreCase("--methods")) {
  63. doMethodList(origin, args.isSilent());
  64. } else if (args.getArguments().length > 0 && args.getArguments()[0]
  65. .equalsIgnoreCase("--method")) {
  66. if (args.getArguments().length > 1) {
  67. final String sourceName = args.getArguments()[1];
  68. final ExportedService source = parent.getMethod(sourceName)
  69. .getExportedService("showNotification");
  70. if (source == null) {
  71. sendLine(origin, args.isSilent(), FORMAT_ERROR,
  72. "Method not found.");
  73. } else {
  74. source.execute("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 (parent.hasActiveMethod()) {
  81. parent.getPreferredMethod().getExportedService("showNotification")
  82. .execute("DMDirc", args.getArgumentsAsString(0));
  83. } else {
  84. sendLine(origin, args.isSilent(), FORMAT_ERROR,
  85. "No active notification methods available.");
  86. }
  87. }
  88. /**
  89. * Outputs a list of methods for the notifcation command.
  90. *
  91. * @param origin The input window where the command was entered
  92. * @param isSilent Whether this command is being silenced
  93. */
  94. private void doMethodList(final FrameContainer origin,
  95. final boolean isSilent) {
  96. final List<PluginInfo> methods = parent.getMethods();
  97. if (methods.isEmpty()) {
  98. sendLine(origin, isSilent, FORMAT_ERROR, "No notification "
  99. + "methods available.");
  100. } else {
  101. final String[] headers = {"Method"};
  102. final String[][] data = new String[methods.size()][1];
  103. int i = 0;
  104. for (PluginInfo method : methods) {
  105. data[i][0] = method.getMetaData().getName();
  106. i++;
  107. }
  108. sendLine(origin, isSilent, FORMAT_OUTPUT, doTable(headers, data));
  109. }
  110. }
  111. /** {@inheritDoc} */
  112. @Override
  113. public AdditionalTabTargets getSuggestions(final int arg,
  114. final IntelligentCommandContext context) {
  115. final AdditionalTabTargets res = new AdditionalTabTargets();
  116. res.excludeAll();
  117. if (arg == 0) {
  118. res.add("--methods");
  119. res.add("--method");
  120. return res;
  121. } else if (arg == 1 && context.getPreviousArgs().get(0)
  122. .equalsIgnoreCase("--method")) {
  123. for (PluginInfo source : parent.getMethods()) {
  124. res.add(source.getMetaData().getName());
  125. }
  126. return res;
  127. }
  128. return res;
  129. }
  130. }