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.

PerformWrapper.java 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * Copyright (c) 2006-2010 Chris Smith, Shane Mc Cormack, Gregory Holmes
  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.actions.wrappers;
  23. import com.dmdirc.actions.Action;
  24. import com.dmdirc.actions.ActionComponentChain;
  25. import com.dmdirc.actions.interfaces.ActionComponent;
  26. import com.dmdirc.actions.ActionCondition;
  27. import com.dmdirc.actions.ActionGroup;
  28. import com.dmdirc.actions.interfaces.ActionType;
  29. import com.dmdirc.actions.CoreActionComparison;
  30. import com.dmdirc.actions.CoreActionComponent;
  31. import com.dmdirc.actions.CoreActionType;
  32. import com.dmdirc.logger.ErrorLevel;
  33. import com.dmdirc.logger.Logger;
  34. import java.util.ArrayList;
  35. import java.util.List;
  36. /**
  37. * An action wrapper for performs.
  38. *
  39. * @author Chris
  40. */
  41. public class PerformWrapper extends ActionGroup {
  42. /** A singleton instance of the Perform Wrapper. */
  43. private static PerformWrapper me = new PerformWrapper();
  44. /**
  45. * Creates a new instance of PerformWrapper.
  46. */
  47. private PerformWrapper() {
  48. super("performs");
  49. }
  50. /**
  51. * Retrieves a singleton instance of this perform wrapper.
  52. *
  53. * @return A singleton instance of PerformWrapper
  54. */
  55. public static PerformWrapper getPerformWrapper() {
  56. return me;
  57. }
  58. /** {@inheritDoc} */
  59. @Override
  60. public void add(final Action action) {
  61. if (action.getTriggers().length == 1
  62. && action.getTriggers()[0] == CoreActionType.SERVER_CONNECTED
  63. && checkConditions(action.getConditions())) {
  64. super.add(action);
  65. } else {
  66. Logger.userError(ErrorLevel.MEDIUM, "Invalid perform action: " + action.getName());
  67. }
  68. }
  69. /**
  70. * Checks that the specified conditions are valid for a perform action.
  71. *
  72. * @param conditions The conditions to be checked
  73. * @since 0.6.3m2
  74. * @return True if the conditions are valid, false otherwise
  75. */
  76. protected boolean checkConditions(final List<ActionCondition> conditions) {
  77. boolean target = false, profile = false;
  78. for (ActionCondition condition : conditions) {
  79. if ((condition.getComponent() == CoreActionComponent.SERVER_NETWORK
  80. || condition.getComponent() == CoreActionComponent.SERVER_NAME)
  81. && !target) {
  82. target = true;
  83. } else if (condition.getComponent() instanceof ActionComponentChain
  84. && "SERVER_IDENTITY.IDENTITY_NAME".equals(condition.getComponent().toString())
  85. && !profile) {
  86. profile = true;
  87. } else {
  88. return false;
  89. }
  90. }
  91. return target || profile;
  92. }
  93. /**
  94. * Retrieve the action that handles the perform for the specified server,
  95. * or null if no such action exists.
  96. *
  97. * @param server The server to look for
  98. * @return The action that handles the server's perform, or null
  99. */
  100. public Action getActionForServer(final String server) {
  101. return getAction(CoreActionComponent.SERVER_NAME, server);
  102. }
  103. /**
  104. * Retrieve the action that handles the perform for the specified network,
  105. * or null if no such action exists.
  106. *
  107. * @param network The network to look for
  108. * @return The action that handles the network's perform, or null
  109. */
  110. public Action getActionForNetwork(final String network) {
  111. return getAction(CoreActionComponent.SERVER_NETWORK, network);
  112. }
  113. /**
  114. * Creates a new, empty, perform wrapper for the specified server.
  115. *
  116. * @param server The server to create the action for
  117. * @return The new perform wrapper action
  118. */
  119. public Action createActionForServer(final String server) {
  120. return createAction(server, "");
  121. }
  122. /**
  123. * Creates a new, empty, perform wrapper for the specified network.
  124. *
  125. * @param network The network to create the action for
  126. * @return The new perform wrapper action
  127. */
  128. public Action createActionForNetwork(final String network) {
  129. return createAction("", network);
  130. }
  131. /**
  132. * Creates a new, empty, perform wrapper for the specified server or
  133. * network. Note that both server and network must be specified, and
  134. * exactly one of them must be empty.
  135. *
  136. * @param server The server to create the action for
  137. * @param network The network to create the action for
  138. * @return The new perform wrapper action
  139. */
  140. private Action createAction(final String server, final String network) {
  141. final List<ActionCondition> conditions = new ArrayList<ActionCondition>();
  142. final CoreActionComponent component =
  143. server.isEmpty() ? CoreActionComponent.SERVER_NETWORK
  144. : CoreActionComponent.SERVER_NAME;
  145. conditions.add(new ActionCondition(0, component,
  146. CoreActionComparison.STRING_EQUALS, server + network));
  147. return new Action(getName(), server + network,
  148. new ActionType[]{CoreActionType.SERVER_CONNECTED},
  149. new String[0], conditions, null);
  150. }
  151. /**
  152. * Retrieve an action with a condition that checks the specified component,
  153. * and matches it against the specified target.
  154. *
  155. * @param component The action component to look for
  156. * @param target The string the component is matched against
  157. * @return The matching action if one exists, or null
  158. */
  159. private Action getAction(final ActionComponent component, final String target) {
  160. for (Action action : this) {
  161. if (action.getConditions().get(0).getComponent() == component
  162. && action.getConditions().get(0).getTarget().equalsIgnoreCase(target)) {
  163. return action;
  164. }
  165. }
  166. return null;
  167. }
  168. /** {@inheritDoc} */
  169. @Override
  170. public boolean isDelible() {
  171. return false;
  172. }
  173. /** {@inheritDoc} */
  174. @Override
  175. public String getDescription() {
  176. return "Performs allow you to automatically execute commands when"
  177. + " you connect to a specific server or network. You can edit"
  178. + " the perform for the current server or network in the "
  179. + "\"Server Settings\" dialog, which can be accessed through "
  180. + "the Settings menu.";
  181. }
  182. }