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