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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * Copyright (c) 2006-2014 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.time;
  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.Map.Entry;
  33. import java.util.Set;
  34. import java.util.stream.Collectors;
  35. import javax.annotation.Nonnull;
  36. import javax.inject.Inject;
  37. /**
  38. * The timer command allows users to schedule commands to occur after a certain interval, or to
  39. * repeatedly occur with a specified delay.
  40. */
  41. public class TimerCommand extends Command implements IntelligentCommand {
  42. /** A command info object for this command. */
  43. public static final BaseCommandInfo INFO = new BaseCommandInfo("timer",
  44. "timer [--list|--cancel <timer id> | <repetitions> <interval> "
  45. + "<command>] - lists all active timers / cancels an active timer "
  46. + "of given ID / schedules a command to be executed after a certain "
  47. + "time",
  48. CommandType.TYPE_GLOBAL);
  49. /** The TimerManager for this TimerCommand. */
  50. private final TimerManager manager;
  51. /**
  52. * Creates a new instance of TimerCommand.
  53. *
  54. * @param manager The instance of TimerManager associated with this command
  55. * @param commandController The controller to use for command information.
  56. */
  57. @Inject
  58. public TimerCommand(final TimerManager manager, final CommandController commandController) {
  59. super(commandController);
  60. this.manager = manager;
  61. }
  62. @Override
  63. public void execute(@Nonnull final FrameContainer origin,
  64. final CommandArguments args, final CommandContext context) {
  65. if (args.getArguments().length > 0) {
  66. switch (args.getArguments()[0]) {
  67. case "--cancel":
  68. doCancel(origin, args.isSilent(), args.getArgumentsAsString(1));
  69. break;
  70. case "--list":
  71. doList(origin, args.isSilent());
  72. break;
  73. default:
  74. if (args.getArguments().length < 3) {
  75. doUsage(origin, args.isSilent());
  76. } else {
  77. doCommand(origin, args);
  78. }
  79. break;
  80. }
  81. } else {
  82. doUsage(origin, args.isSilent());
  83. }
  84. }
  85. private void doCommand(final FrameContainer origin, final CommandArguments args) {
  86. final int repetitions;
  87. final int interval;
  88. final String command = args.getArgumentsAsString(2);
  89. try {
  90. repetitions = Integer.parseInt(args.getArguments()[0]);
  91. interval = Integer.parseInt(args.getArguments()[1]);
  92. } catch (NumberFormatException ex) {
  93. doUsage(origin, args.isSilent());
  94. return;
  95. }
  96. if (interval < 1) {
  97. sendLine(origin, args.isSilent(), FORMAT_ERROR, "Cannot use intervals below 1");
  98. return;
  99. }
  100. manager.addTimer(repetitions, interval, command, origin);
  101. sendLine(origin, args.isSilent(), FORMAT_OUTPUT, "Command scheduled.");
  102. }
  103. private void doCancel(final FrameContainer origin, final boolean isSilent, final String arg) {
  104. final int timerKey;
  105. try {
  106. timerKey = Integer.parseInt(arg);
  107. } catch (NumberFormatException ex) {
  108. doUsage(origin, isSilent);
  109. return;
  110. }
  111. if (manager.hasTimerWithID(timerKey)) {
  112. manager.getTimerByID(timerKey).cancelTimer();
  113. sendLine(origin, isSilent, FORMAT_OUTPUT, "Timer cancelled");
  114. } else {
  115. sendLine(origin, isSilent, FORMAT_ERROR, "There is currently no timer with that ID");
  116. }
  117. }
  118. private void doList(final FrameContainer origin, final boolean isSilent) {
  119. final Set<Entry<Integer, TimedCommand>> timerList = manager.listTimers();
  120. if (timerList.isEmpty()) {
  121. sendLine(origin, isSilent, FORMAT_ERROR, "There are " + "currently no active timers");
  122. } else {
  123. for (Entry<Integer, TimedCommand> entry : timerList) {
  124. sendLine(origin, isSilent, FORMAT_OUTPUT,
  125. "Timer ID: " + entry.getKey() + " - " + entry.getValue().getCommand());
  126. }
  127. }
  128. }
  129. /**
  130. * Displays usage information for this command.
  131. *
  132. * @param origin The window that the command was entered in
  133. * @param isSilent Whether this command is being silenced or not
  134. */
  135. private void doUsage(final FrameContainer origin, final boolean isSilent) {
  136. showUsage(origin, isSilent, "timer", "[--list|--cancel <timer id> | "
  137. + "<repetitions> <interval> <command>]");
  138. }
  139. @Override
  140. public AdditionalTabTargets getSuggestions(final int arg,
  141. final IntelligentCommandContext context) {
  142. final AdditionalTabTargets targets = new AdditionalTabTargets();
  143. targets.excludeAll();
  144. if (arg == 0) {
  145. targets.add("--list");
  146. targets.add("--cancel");
  147. } else if (arg == 1 && "--cancel".equals(context.getPreviousArgs().get(0))) {
  148. targets.addAll(manager.getTimerIDs().stream()
  149. .map(Object::toString).collect(Collectors.toList()));
  150. }
  151. return targets;
  152. }
  153. }