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.

TimerCommand.java 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.addons.time;
  18. import com.dmdirc.commandparser.BaseCommandInfo;
  19. import com.dmdirc.commandparser.CommandArguments;
  20. import com.dmdirc.commandparser.CommandInfo;
  21. import com.dmdirc.commandparser.CommandType;
  22. import com.dmdirc.commandparser.commands.BaseCommand;
  23. import com.dmdirc.commandparser.commands.IntelligentCommand;
  24. import com.dmdirc.commandparser.commands.context.CommandContext;
  25. import com.dmdirc.interfaces.CommandController;
  26. import com.dmdirc.interfaces.WindowModel;
  27. import com.dmdirc.ui.input.AdditionalTabTargets;
  28. import javax.annotation.Nonnull;
  29. import javax.inject.Inject;
  30. import java.util.Map.Entry;
  31. import java.util.Set;
  32. import java.util.stream.Collectors;
  33. /**
  34. * The timer command allows users to schedule commands to occur after a certain interval, or to
  35. * repeatedly occur with a specified delay.
  36. */
  37. public class TimerCommand extends BaseCommand implements IntelligentCommand {
  38. /** A command info object for this command. */
  39. public static final CommandInfo INFO = new BaseCommandInfo("timer",
  40. "timer [--list|--cancel <timer id> | <repetitions> <interval> "
  41. + "<command>] - lists all active timers / cancels an active timer "
  42. + "of given ID / schedules a command to be executed after a certain "
  43. + "time",
  44. CommandType.TYPE_GLOBAL);
  45. /** The TimerManager for this TimerCommand. */
  46. private final TimerManager manager;
  47. /**
  48. * Creates a new instance of TimerCommand.
  49. *
  50. * @param manager The instance of TimerManager associated with this command
  51. * @param commandController The controller to use for command information.
  52. */
  53. @Inject
  54. public TimerCommand(final TimerManager manager, final CommandController commandController) {
  55. super(commandController);
  56. this.manager = manager;
  57. }
  58. @Override
  59. public void execute(@Nonnull final WindowModel origin,
  60. final CommandArguments args, final CommandContext context) {
  61. if (args.getArguments().length > 0) {
  62. switch (args.getArguments()[0]) {
  63. case "--cancel":
  64. doCancel(origin, args.isSilent(), args.getArgumentsAsString(1));
  65. break;
  66. case "--list":
  67. doList(origin, args.isSilent());
  68. break;
  69. default:
  70. if (args.getArguments().length < 3) {
  71. doUsage(origin, args.isSilent());
  72. } else {
  73. doCommand(origin, args);
  74. }
  75. break;
  76. }
  77. } else {
  78. doUsage(origin, args.isSilent());
  79. }
  80. }
  81. private void doCommand(final WindowModel origin, final CommandArguments args) {
  82. final int repetitions;
  83. final int interval;
  84. final String command = args.getArgumentsAsString(2);
  85. try {
  86. repetitions = Integer.parseInt(args.getArguments()[0]);
  87. interval = Integer.parseInt(args.getArguments()[1]);
  88. } catch (NumberFormatException ex) {
  89. doUsage(origin, args.isSilent());
  90. return;
  91. }
  92. if (interval < 1) {
  93. showError(origin, args.isSilent(), "Cannot use intervals below 1");
  94. return;
  95. }
  96. manager.addTimer(repetitions, interval, command, origin);
  97. showOutput(origin, args.isSilent(), "Command scheduled.");
  98. }
  99. private void doCancel(final WindowModel origin, final boolean isSilent, final String arg) {
  100. final int timerKey;
  101. try {
  102. timerKey = Integer.parseInt(arg);
  103. } catch (NumberFormatException ex) {
  104. doUsage(origin, isSilent);
  105. return;
  106. }
  107. if (manager.hasTimerWithID(timerKey)) {
  108. manager.getTimerByID(timerKey).cancelTimer();
  109. showOutput(origin, isSilent, "Timer cancelled");
  110. } else {
  111. showError(origin, isSilent, "There is currently no timer with that ID");
  112. }
  113. }
  114. private void doList(final WindowModel origin, final boolean isSilent) {
  115. final Set<Entry<Integer, TimedCommand>> timerList = manager.listTimers();
  116. if (timerList.isEmpty()) {
  117. showError(origin, isSilent, "There are currently no active timers");
  118. } else {
  119. for (Entry<Integer, TimedCommand> entry : timerList) {
  120. showOutput(origin, isSilent,
  121. "Timer ID: " + entry.getKey() + " - " + entry.getValue().getCommand());
  122. }
  123. }
  124. }
  125. /**
  126. * Displays usage information for this command.
  127. *
  128. * @param origin The window that the command was entered in
  129. * @param isSilent Whether this command is being silenced or not
  130. */
  131. private void doUsage(final WindowModel origin, final boolean isSilent) {
  132. showUsage(origin, isSilent, INFO.getName(), INFO.getHelp());
  133. }
  134. @Override
  135. public AdditionalTabTargets getSuggestions(final int arg,
  136. final IntelligentCommandContext context) {
  137. final AdditionalTabTargets targets = new AdditionalTabTargets();
  138. targets.excludeAll();
  139. if (arg == 0) {
  140. targets.add("--list");
  141. targets.add("--cancel");
  142. } else if (arg == 1 && "--cancel".equals(context.getPreviousArgs().get(0))) {
  143. targets.addAll(manager.getTimerIDs().stream()
  144. .map(Object::toString).collect(Collectors.toList()));
  145. }
  146. return targets;
  147. }
  148. }