Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

TimerCommand.java 6.6KB

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