File Source: ActionManager.java
/*
P/P * Method: com.dmdirc.actions.ActionManager$1__static_init
*/
1 /*
2 * Copyright (c) 2006-2009 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
23 package com.dmdirc.actions;
24
25 import com.dmdirc.Main;
26 import com.dmdirc.Precondition;
27 import com.dmdirc.actions.interfaces.ActionComparison;
28 import com.dmdirc.actions.interfaces.ActionComponent;
29 import com.dmdirc.actions.interfaces.ActionType;
30 import com.dmdirc.actions.wrappers.AliasWrapper;
31 import com.dmdirc.actions.wrappers.PerformWrapper;
32 import com.dmdirc.config.IdentityManager;
33 import com.dmdirc.interfaces.ActionListener;
34 import com.dmdirc.interfaces.ConfigChangeListener;
35 import com.dmdirc.logger.ErrorLevel;
36 import com.dmdirc.logger.Logger;
37 import com.dmdirc.updater.components.ActionGroupComponent;
38 import com.dmdirc.util.MapList;
39 import com.dmdirc.util.resourcemanager.ZipResourceManager;
40
41 import java.io.File;
42 import java.io.IOException;
43 import java.util.ArrayList;
44 import java.util.HashMap;
45 import java.util.List;
46 import java.util.Map;
47
48 /**
49 * Manages all actions for the client.
50 *
51 * @author chris
52 */
/*
P/P * Method: bool access$002(bool)
*
* Postconditions:
* killSwitch == x0
* init'ed(killSwitch)
* return_value == killSwitch
*/
53 public final class ActionManager {
54
55 /** A list of registered action types. */
/*
P/P * Method: com.dmdirc.actions.ActionManager__static_init
*
* Presumptions:
* com.dmdirc.config.IdentityManager:getGlobalConfig(...)@84 != null
*
* Postconditions:
* actionComparisons == &new ArrayList(ActionManager__static_init#3)
* actionComponents == &new ArrayList(ActionManager__static_init#2)
* actionTypeGroups == &new MapList(ActionManager__static_init#6)
* actionTypes == &new ArrayList(ActionManager__static_init#1)
* actions == &new MapList(ActionManager__static_init#4)
* groups == &new HashMap(ActionManager__static_init#5)
* init'ed(killSwitch)
* listeners == &new MapList(ActionManager__static_init#7)
* new ArrayList(ActionManager__static_init#1) num objects == 1
* new ArrayList(ActionManager__static_init#2) num objects == 1
* ...
*/
56 private final static List<ActionType> actionTypes
57 = new ArrayList<ActionType>();
58
59 /** A list of registered action components. */
60 private final static List<ActionComponent> actionComponents
61 = new ArrayList<ActionComponent>();
62
63 /** A list of registered action comparisons. */
64 private final static List<ActionComparison> actionComparisons
65 = new ArrayList<ActionComparison>();
66
67 /** A map linking types and a list of actions that're registered for them. */
68 private final static MapList<ActionType, Action> actions
69 = new MapList<ActionType, Action>();
70
71 /** A map linking groups and a list of actions that're in them. */
72 private final static Map<String, ActionGroup> groups
73 = new HashMap<String, ActionGroup>();
74
75 /** A map of the action type groups to the action types within. */
76 private final static MapList<String, ActionType> actionTypeGroups
77 = new MapList<String, ActionType>();
78
79 /** The listeners that we have registered. */
80 private final static MapList<ActionType, ActionListener> listeners
81 = new MapList<ActionType, ActionListener>();
82
83 /** Indicates whether or not user actions should be killed (not processed). */
84 private static boolean killSwitch
85 = IdentityManager.getGlobalConfig().getOptionBool("actions", "killswitch");
86
87 /** Creates a new instance of ActionManager. */
/*
P/P * Method: void com.dmdirc.actions.ActionManager()
*/
88 private ActionManager() {
89 // Shouldn't be instansiated
90 }
91
92 /**
93 * Initialises the action manager.
94 */
95 public static void init() {
/*
P/P * Method: void init()
*
* Preconditions:
* com/dmdirc/actions/wrappers/PerformWrapper.me != null
* init'ed(com/dmdirc/actions/wrappers/AliasWrapper.me)
* (soft) init'ed(com.dmdirc.actions.CoreActionComparison__static_init.new CoreActionComparison[](CoreActionComparison__static_init#13)[...])
* (soft) init'ed(com.dmdirc.actions.CoreActionComponent__static_init.new CoreActionComponent[](CoreActionComponent__static_init#28)[...])
* (soft) com.dmdirc.actions.CoreActionType__static_init.new CoreActionType[](CoreActionType__static_init#74)[...] != null
* (soft) com.dmdirc.actions.CoreActionType__static_init.new CoreActionType[](CoreActionType__static_init#74)[...].type != null
*
* Presumptions:
* com.dmdirc.config.IdentityManager:getGlobalConfig(...)@114 != null
*
* Postconditions:
* com/dmdirc/actions/wrappers/AliasWrapper.me == One-of{old com/dmdirc/actions/wrappers/AliasWrapper.me, &new AliasWrapper(getAliasWrapper#1)}
* com/dmdirc/actions/wrappers/AliasWrapper.me != null
* new AliasWrapper(getAliasWrapper#1) num objects <= 1
* new ArrayList(ActionGroup#1) num objects == new AliasWrapper(getAliasWrapper#1) num objects
* new ArrayList(AliasWrapper#1) num objects == new AliasWrapper(getAliasWrapper#1) num objects
* new HashMap(ActionGroup#2) num objects == new AliasWrapper(getAliasWrapper#1) num objects
* new AliasWrapper(getAliasWrapper#1).actions == &new ArrayList(ActionGroup#1)
* new AliasWrapper(getAliasWrapper#1).aliases == &new ArrayList(AliasWrapper#1)
* new AliasWrapper(getAliasWrapper#1).author == null
* new AliasWrapper(getAliasWrapper#1).description == null
* ...
*/
96 registerActionTypes(CoreActionType.values());
97 registerActionComparisons(CoreActionComparison.values());
98 registerActionComponents(CoreActionComponent.values());
99
100 registerGroup(AliasWrapper.getAliasWrapper());
101 registerGroup(PerformWrapper.getPerformWrapper());
102
103 // Register a listener for the closing event, so we can save actions
/*
P/P * Method: void com.dmdirc.actions.ActionManager$1()
*/
104 addListener(new ActionListener() {
105 /** {@inheritDoc} */
106 @Override
107 public void processEvent(final ActionType type, final StringBuffer format,
108 final Object... arguments) {
/*
P/P * Method: void processEvent(ActionType, StringBuffer, Object[])
*/
109 saveActions();
110 }
111 }, CoreActionType.CLIENT_CLOSED);
112
113 // Make sure we listen for the killswitch
114 IdentityManager.getGlobalConfig().addChangeListener("actions", "killswitch",
/*
P/P * Method: void com.dmdirc.actions.ActionManager$2()
*/
115 new ConfigChangeListener() {
116 /** {@inheritDoc} */
117 @Override
118 public void configChanged(final String domain, final String key) {
/*
P/P * Method: void configChanged(String, String)
*
* Presumptions:
* com.dmdirc.config.IdentityManager:getGlobalConfig(...)@119 != null
*
* Postconditions:
* init'ed(com/dmdirc/actions/ActionManager.killSwitch)
*/
119 killSwitch = IdentityManager.getGlobalConfig().getOptionBool(
120 "actions", "killswitch");
121 }
122 });
123 }
124
125 /**
126 * Saves all actions.
127 */
128 public static void saveActions() {
/*
P/P * Method: void saveActions()
*
* Presumptions:
* group.actions@129 != null
* java.util.Iterator:next(...)@129 != null
* java.util.Iterator:next(...)@130 != null
* java.util.Map:values(...)@129 != null
*
* Test Vectors:
* java.util.Iterator:hasNext(...)@129: {0}, {1}
* java.util.Iterator:hasNext(...)@130: {0}, {1}
*/
129 for (ActionGroup group : groups.values()) {
130 for (Action action : group) {
131 action.save();
132 }
133 }
134 }
135
136 /**
137 * Registers the specified default setting for actions.
138 *
139 * @param name The name of the setting to be registered
140 * @param value The default value for the setting
141 */
142 public static void registerDefault(final String name, final String value) {
/*
P/P * Method: void registerDefault(String, String)
*
* Presumptions:
* com.dmdirc.config.IdentityManager:getAddonIdentity(...)@143 != null
*/
143 IdentityManager.getAddonIdentity().setOption("actions", name, value);
144 }
145
146 /**
147 * Registers the specified group of actions with the manager.
148 *
149 * @param group The group of actions to be registered
150 */
151 public static void registerGroup(final ActionGroup group) {
/*
P/P * Method: void registerGroup(ActionGroup)
*
* Preconditions:
* group != null
*/
152 groups.put(group.getName(), group);
153 }
154
155 /**
156 * Registers a set of actiontypes with the manager.
157 *
158 * @param types An array of ActionTypes to be registered
159 */
160 @Precondition("None of the specified ActionTypes are null")
161 public static void registerActionTypes(final ActionType[] types) {
/*
P/P * Method: void registerActionTypes(ActionType[])
*
* Preconditions:
* types != null
* types.length <= 232-1
* (soft) types[...] != null
* (soft) types[...].type != null
*
* Presumptions:
* getType(...)@167 != getType(...)
*
* Test Vectors:
* java.util.List:contains(...)@165: {1}, {0}
*/
162 for (ActionType type : types) {
163 Logger.assertTrue(type != null);
164
165 if(!actionTypes.contains(type)) {
166 actionTypes.add(type);
167 actionTypeGroups.add(type.getType().getGroup(), type);
168 }
169 }
170 }
171
172 /**
173 * Registers a set of action components with the manager.
174 *
175 * @param comps An array of ActionComponents to be registered
176 */
177 @Precondition("None of the specified ActionComponents are null")
178 public static void registerActionComponents(final ActionComponent[] comps) {
/*
P/P * Method: void registerActionComponents(ActionComponent[])
*
* Preconditions:
* comps != null
* comps.length <= 232-1
* (soft) init'ed(comps[...])
*/
179 for (ActionComponent comp : comps) {
180 Logger.assertTrue(comp != null);
181
182 actionComponents.add(comp);
183 }
184 }
185
186 /**
187 * Registers a set of action comparisons with the manager.
188 *
189 * @param comps An array of ActionComparisons to be registered
190 */
191 @Precondition("None of the specified ActionComparisons are null")
192 public static void registerActionComparisons(final ActionComparison[] comps) {
/*
P/P * Method: void registerActionComparisons(ActionComparison[])
*
* Preconditions:
* comps != null
* comps.length <= 232-1
* (soft) init'ed(comps[...])
*/
193 for (ActionComparison comp : comps) {
194 Logger.assertTrue(comp != null);
195
196 actionComparisons.add(comp);
197 }
198 }
199
200 /**
201 * Returns a map of groups to action lists.
202 *
203 * @return a map of groups to action lists
204 */
205 public static Map<String, ActionGroup> getGroups() {
/*
P/P * Method: Map getGroups()
*
* Postconditions:
* return_value == &new HashMap(ActionManager__static_init#5)
*/
206 return groups;
207 }
208
209 /**
210 * Returns a map of type groups to types.
211 *
212 * @return A map of type groups to types
213 */
214 public static MapList<String, ActionType> getTypeGroups() {
/*
P/P * Method: MapList getTypeGroups()
*
* Postconditions:
* return_value == &new MapList(ActionManager__static_init#6)
*/
215 return actionTypeGroups;
216 }
217
218 /**
219 * Loads actions from the user's directory.
220 */
221 public static void loadActions() {
/*
P/P * Method: void loadActions()
*
* Preconditions:
* init'ed(com/dmdirc/Main.configdir)
*
* Presumptions:
* arr$.length@243 <= 232-1
* arr$[i$]@243 != null
* init'ed(com.dmdirc.logger.ErrorLevel.HIGH)
* init'ed(com.dmdirc.logger.ErrorLevel.MEDIUM)
* group.actions@224 != null
* ...
*
* Postconditions:
* possibly_updated(com/dmdirc/Main.configdir)
* possibly_updated(com/dmdirc/ServerManager.me)
* java.lang.StringBuilder:toString(...)._tainted == 0
* init'ed(new ArrayList(ServerManager#1) num objects)
* init'ed(new ServerManager(getServerManager#1) num objects)
* init'ed(new ServerManager(getServerManager#1).servers)
*
* Test Vectors:
* java.io.File:exists(...)@230: {1}, {0}
* java.io.File:isDirectory(...)@244: {0}, {1}
* java.io.File:listFiles(...)@240: Inverse{null}, Addr_Set{null}
* java.util.Iterator:hasNext(...)@224: {0}, {1}
*/
222 actions.clear();
223
224 for (ActionGroup group : groups.values()) {
225 group.clear();
226 }
227
228 final File dir = new File(getDirectory());
229
230 if (!dir.exists()) {
231 try {
232 dir.mkdirs();
233 dir.createNewFile();
234 } catch (IOException ex) {
235 Logger.userError(ErrorLevel.HIGH, "I/O error when creating actions directory: "
236 + ex.getMessage());
237 }
238 }
239
240 if (dir.listFiles() == null) {
241 Logger.userError(ErrorLevel.MEDIUM, "Unable to load user action files");
242 } else {
243 for (File file : dir.listFiles()) {
244 if (file.isDirectory()) {
245 loadActions(file);
246 }
247 }
248 }
249
250 registerComponents();
251 }
252
253 /**
254 * Creates new ActionGroupComponents for each action group.
255 */
256 private static void registerComponents() {
/*
P/P * Method: void registerComponents()
*
* Presumptions:
* java.util.Map:values(...)@257 != null
*
* Test Vectors:
* java.util.Iterator:hasNext(...)@257: {0}, {1}
*/
257 for (ActionGroup group : groups.values()) {
258 new ActionGroupComponent(group);
259 }
260 }
261
262 /**
263 * Loads action files from a specified group directory.
264 *
265 * @param dir The directory to scan.
266 */
267 @Precondition("The specified File is not null and represents a directory")
268 private static void loadActions(final File dir) {
/*
P/P * Method: void loadActions(File)
*
* Preconditions:
* dir != null
* (soft) init'ed(com/dmdirc/Main.configdir)
*
* Presumptions:
* arr$.length@276 <= 232-1
* arr$[i$]@276 != null
* java.io.File:listFiles(...)@276 != null
*
* Postconditions:
* init'ed(com/dmdirc/Main.configdir)
* java.lang.StringBuilder:toString(...)._tainted == 0
*
* Test Vectors:
* java.util.Map:containsKey(...)@272: {1}, {0}
*/
269 Logger.assertTrue(dir != null);
270 Logger.assertTrue(dir.isDirectory());
271
272 if (!groups.containsKey(dir.getName())) {
273 groups.put(dir.getName(), new ActionGroup(dir.getName()));
274 }
275
276 for (File file : dir.listFiles()) {
277 new Action(dir.getName(), file.getName());
278 }
279 }
280
281 /**
282 * Registers an action with the manager.
283 *
284 * @param action The action to be registered
285 */
286 @Precondition("The specified action is not null")
287 public static void registerAction(final Action action) {
/*
P/P * Method: void registerAction(Action)
*
* Preconditions:
* action != null
* init'ed(action.group)
* action.triggers != null
* action.triggers.length <= 232-1
* (soft) init'ed(action.triggers[0])
* (soft) init'ed(action.triggers[...])
* (soft) init'ed(com/dmdirc/ServerManager.me)
*
* Presumptions:
* getGroup(...).actions != null
* java.util.Map:get(...)@309 != null
*
* Postconditions:
* init'ed(com/dmdirc/ServerManager.me)
* new ArrayList(ServerManager#1) num objects <= 1
* new ServerManager(getServerManager#1) num objects <= 1
* init'ed(new ServerManager(getServerManager#1).servers)
*/
288 Logger.assertTrue(action != null);
289
290 for (ActionType trigger : action.getTriggers()) {
291 actions.add(trigger, action);
292 }
293
294 getGroup(action.getGroup()).add(action);
295 }
296
297 /**
298 * Retrieves the action group with the specified name. A new group is
299 * created if it doesn't already exist.
300 *
301 * @param name The name of the group to retrieve
302 * @return The corresponding ActionGroup
303 */
304 public static ActionGroup getGroup(final String name) {
/*
P/P * Method: ActionGroup getGroup(String)
*
* Postconditions:
* init'ed(return_value)
*
* Test Vectors:
* java.util.Map:containsKey(...)@305: {1}, {0}
*/
305 if (!groups.containsKey(name)) {
306 groups.put(name, new ActionGroup(name));
307 }
308
309 return groups.get(name);
310 }
311
312 /**
313 * Unregisters an action with the manager.
314 *
315 * @param action The action to be unregistered
316 */
317 @Precondition("The specified action is not null")
318 public static void unregisterAction(final Action action) {
/*
P/P * Method: void unregisterAction(Action)
*
* Preconditions:
* action != null
* init'ed(action.group)
* (soft) init'ed(com/dmdirc/ServerManager.me)
*
* Presumptions:
* getGroup(...).actions != null
* java.util.Map:get(...)@309 != null
*
* Postconditions:
* init'ed(com/dmdirc/ServerManager.me)
* new ArrayList(ServerManager#1) num objects <= 1
* new ServerManager(getServerManager#1) num objects <= 1
* init'ed(new ServerManager(getServerManager#1).servers)
*/
319 Logger.assertTrue(action != null);
320
321 actions.removeFromAll(action);
322 getGroup(action.getGroup()).remove(action);
323 }
324
325 /**
326 * Reregisters the specified action. Should be used when the action's
327 * triggers change.
328 *
329 * @param action The action to be reregistered
330 */
331 public static void reregisterAction(final Action action) {
/*
P/P * Method: void reregisterAction(Action)
*
* Preconditions:
* action != null
* init'ed(action.group)
* action.triggers != null
* action.triggers.length <= 232-1
* (soft) init'ed(action.triggers[0])
* (soft) init'ed(action.triggers[...])
* (soft) init'ed(com/dmdirc/ServerManager.me)
*
* Postconditions:
* init'ed(com/dmdirc/ServerManager.me)
* new ArrayList(ServerManager#1) num objects <= 1
* new ServerManager(getServerManager#1) num objects <= 1
* init'ed(new ServerManager(getServerManager#1).servers)
*/
332 unregisterAction(action);
333 registerAction(action);
334 }
335
336 /**
337 * Deletes the specified action.
338 *
339 * @param action The action to be deleted
340 */
341 @Precondition("The specified Action is not null")
342 public static void deleteAction(final Action action) {
/*
P/P * Method: void deleteAction(Action)
*
* Preconditions:
* action != null
* init'ed(action.group)
* init'ed(action.location)
* init'ed(action.name)
* init'ed(killSwitch)
* (soft) com.dmdirc.actions.CoreActionType__static_init.new CoreActionType(CoreActionType__static_init#73).type != null
* (soft) init'ed(com/dmdirc/ServerManager.me)
*
* Postconditions:
* init'ed(com/dmdirc/ServerManager.me)
* new ArrayList(ServerManager#1) num objects <= 1
* new ArrayList(ServerManager#1) num objects == undefined
* new ArrayList(ServerManager#1) num objects == 0, if init'ed
* new ServerManager(getServerManager#1) num objects == new ArrayList(ServerManager#1) num objects
* new ServerManager(getServerManager#1) num objects <= 1
* init'ed(new ServerManager(getServerManager#1).servers)
* new ServerManager(getServerManager#1).servers == undefined
* new ServerManager(getServerManager#1).servers == null
*/
343 Logger.assertTrue(action != null);
344
345 unregisterAction(action);
346
347 action.delete();
348 }
349
350 /**
351 * Processes an event of the specified type.
352 *
353 * @param type The type of the event to process
354 * @param format The format of the message that's going to be displayed for
355 * the event. Actions may change this format.
356 * @param arguments The arguments for the event
357 */
358 @Precondition({
359 "The specified ActionType is not null",
360 "The specified ActionType has a valid ActionMetaType",
361 "The length of the arguments array equals the arity of the ActionType's ActionMetaType"
362 })
363 public static void processEvent(final ActionType type,
364 final StringBuffer format, final Object ... arguments) {
/*
P/P * Method: void processEvent(ActionType, StringBuffer, Object[])
*
* Preconditions:
* arguments != null
* init'ed(killSwitch)
* type != null
* (soft) init'ed(com/dmdirc/ServerManager.me)
* (soft) type.type != null
*
* Presumptions:
* java.util.ArrayList:iterator(...)@371 != null
* java.util.Iterator:next(...)@371 != null
*
* Postconditions:
* com/dmdirc/ServerManager.me == old com/dmdirc/ServerManager.me
* format._tainted == old format._tainted
* init'ed(format._tainted)
* new ArrayList(ServerManager#1) num objects == undefined
* new ArrayList(ServerManager#1) num objects == 0, if init'ed
* new ServerManager(getServerManager#1) num objects == new ArrayList(ServerManager#1) num objects
* new ServerManager(getServerManager#1).servers == undefined
* new ServerManager(getServerManager#1).servers == null
*
* Test Vectors:
* killSwitch: {1}, {0}
* com.dmdirc.util.MapList:containsKey(...)@369: {0}, {1}
* java.util.Iterator:hasNext(...)@371: {0}, {1}
*/
365 Logger.assertTrue(type != null);
366 Logger.assertTrue(type.getType() != null);
367 Logger.assertTrue(type.getType().getArity() == arguments.length);
368
369 if (listeners.containsKey(type)) {
370 for (ActionListener listener :
371 new ArrayList<ActionListener>(listeners.get(type))) {
372 listener.processEvent(type, format, arguments);
373 }
374 }
375
376 if (!killSwitch) {
377 triggerActions(type, format, arguments);
378 }
379 }
380
381 /**
382 * Triggers actions that respond to the specified type.
383 *
384 * @param type The type of the event to process
385 * @param format The format of the message that's going to be displayed for
386 * the event. Actions may change this format.*
387 * @param arguments The arguments for the event
388 */
389 @Precondition("The specified ActionType is not null")
390 private static void triggerActions(final ActionType type,
391 final StringBuffer format, final Object ... arguments) {
/*
P/P * Method: void triggerActions(ActionType, StringBuffer, Object[])
*
* Preconditions:
* (soft) arguments != null
* (soft) init'ed(arguments[0])
* (soft) init'ed(arguments[1])
* (soft) arguments[1].length in {1..232-1}
* (soft) init'ed(arguments[2])
* (soft) arguments[2].length in {1..232-1}
* (soft) init'ed(arguments[...])
* (soft) init'ed(com.dmdirc.actions.ConditionTree$1__static_init.new int[](ConditionTree$1__static_init#1)[...])
* (soft) com/dmdirc/Main.controller != null
* (soft) init'ed(com/dmdirc/ServerManager.me)
*
* Presumptions:
* action.conditions@395 != null
* action.response.length@395 <= 232-1
* action.response@395 != null
* action.triggers.length@395 >= 1
* action.triggers@395 != null
* ...
*
* Postconditions:
* init'ed(com/dmdirc/ServerManager.me)
* init'ed(format._tainted)
* new ArrayList(ServerManager#1) num objects == 0
* new ServerManager(getServerManager#1) num objects == 0
* init'ed(new ServerManager(getServerManager#1).servers)
*
* Test Vectors:
* com.dmdirc.util.MapList:containsKey(...)@394: {0}, {1}
* java.util.Iterator:hasNext(...)@395: {0}, {1}
*/
392 Logger.assertTrue(type != null);
393
394 if (actions.containsKey(type)) {
395 for (Action action : new ArrayList<Action>(actions.get(type))) {
396 action.trigger(format, arguments);
397 }
398 }
399 }
400
401 /**
402 * Returns the directory that should be used to store actions.
403 *
404 * @return The directory that should be used to store actions
405 */
406 public static String getDirectory() {
/*
P/P * Method: String getDirectory()
*
* Preconditions:
* init'ed(com/dmdirc/Main.configdir)
*
* Postconditions:
* com/dmdirc/Main.configdir == One-of{old com/dmdirc/Main.configdir, &java.lang.StringBuilder:toString(...)}
* com/dmdirc/Main.configdir != null
* init'ed(java.lang.StringBuilder:toString(...)._tainted)
* java.lang.StringBuilder:toString(...)._tainted == 0
* return_value == &java.lang.StringBuilder:toString(...)
*/
407 return Main.getConfigDir() + "actions" + System.getProperty("file.separator");
408 }
409
410 /**
411 * Creates a new group with the specified name.
412 *
413 * @param group The group to be created
414 *
415 * @return The newly created group
416 */
417 @Precondition({
418 "The specified group is non-null and not empty",
419 "The specified group is not an existing group"
420 })
421 public static ActionGroup makeGroup(final String group) {
/*
P/P * Method: ActionGroup makeGroup(String)
*
* Preconditions:
* group != null
* init'ed(com/dmdirc/Main.configdir)
*
* Presumptions:
* java.io.File:mkdir(...)@427 == 1
*
* Postconditions:
* com/dmdirc/Main.configdir == One-of{old com/dmdirc/Main.configdir, &java.lang.StringBuilder:toString(...)}
* com/dmdirc/Main.configdir != null
* java.lang.StringBuilder:toString(...)._tainted == 0
* return_value == &new ActionGroup(makeGroup#3)
* new ActionGroup(makeGroup#3) num objects == 1
* new ActionGroup(makeGroup#3).actions == &new ArrayList(ActionGroup#1)
* init'ed(new ActionGroup(makeGroup#3).author)
* init'ed(new ActionGroup(makeGroup#3).component)
* init'ed(new ActionGroup(makeGroup#3).description)
* new ActionGroup(makeGroup#3).name == group
* ...
*
* Test Vectors:
* java.io.File:isDirectory(...)@427: {1}, {0}
*/
422 Logger.assertTrue(group != null);
423 Logger.assertTrue(!group.isEmpty());
424 Logger.assertTrue(!groups.containsKey(group));
425
426 final File file = new File(getDirectory() + group);
427 if (file.isDirectory() || file.mkdir()) {
428 final ActionGroup actionGroup = new ActionGroup(group);
429 groups.put(group, actionGroup);
430 return actionGroup;
431 } else {
432 throw new IllegalArgumentException("Unable to create action group directory"
433 + "\n\nDir: " + getDirectory() + group);
434 }
435 }
436
437 /**
438 * Removes the group with the specified name.
439 *
440 * @param group The group to be removed
441 */
442 @Precondition({
443 "The specified group is non-null and not empty",
444 "The specified group is an existing group"
445 })
446 public static void removeGroup(final String group) {
/*
P/P * Method: void removeGroup(String)
*
* Preconditions:
* group != null
* init'ed(com/dmdirc/Main.configdir)
* (soft) init'ed(com/dmdirc/ServerManager.me)
*
* Presumptions:
* arr$.length@458 <= 232-1
* arr$[i$]@458 != null
* init'ed(com.dmdirc.logger.ErrorLevel.MEDIUM)
* java.io.File:listFiles(...)@458 != null
* java.util.Iterator:next(...)@451 != null
* ...
*
* Postconditions:
* com/dmdirc/Main.configdir == One-of{old com/dmdirc/Main.configdir, &java.lang.StringBuilder:toString(...)}
* com/dmdirc/Main.configdir != null
* init'ed(com/dmdirc/ServerManager.me)
* java.lang.StringBuilder:toString(...)._tainted == 0
* init'ed(new ArrayList(ServerManager#1) num objects)
* init'ed(new ServerManager(getServerManager#1) num objects)
* init'ed(new ServerManager(getServerManager#1).servers)
*
* Test Vectors:
* java.io.File:delete(...)@459: {1}, {0}
* java.io.File:delete(...)@467: {1}, {0}
* java.io.File:isDirectory(...)@457: {0}, {1}
* java.util.Iterator:hasNext(...)@451: {0}, {1}
*/
447 Logger.assertTrue(group != null);
448 Logger.assertTrue(!group.isEmpty());
449 Logger.assertTrue(groups.containsKey(group));
450
451 for (Action action : groups.get(group).getActions()) {
452 unregisterAction(action);
453 }
454
455 final File dir = new File(getDirectory() + group);
456
457 if (dir.isDirectory()) {
458 for (File file : dir.listFiles()) {
459 if (!file.delete()) {
460 Logger.userError(ErrorLevel.MEDIUM, "Unable to remove file: "
461 + file.getAbsolutePath());
462 return;
463 }
464 }
465 }
466
467 if (!dir.delete()) {
468 Logger.userError(ErrorLevel.MEDIUM, "Unable to remove directory: "
469 + dir.getAbsolutePath());
470 return;
471 }
472
473 groups.remove(group);
474 }
475
476 /**
477 * Renames the specified group.
478 *
479 * @param oldName The old name of the group
480 * @param newName The new name of the group
481 */
482 @Precondition({
483 "The old name is non-null and not empty",
484 "The old name is an existing group",
485 "The new name is non-null and not empty",
486 "The new name is not an existing group",
487 "The old name does not equal the new name"
488 })
489 public static void renameGroup(final String oldName, final String newName) {
/*
P/P * Method: void renameGroup(String, String)
*
* Preconditions:
* newName != null
* init'ed(com/dmdirc/Main.configdir)
* oldName != null
* (soft) init'ed(com/dmdirc/ServerManager.me)
*
* Presumptions:
* getGroup(...).actions != null
* java.util.Iterator:next(...)@500 != null
* java.util.Map:get(...)@309 != null
* java.util.Map:get(...)@500 != null
*
* Postconditions:
* com/dmdirc/Main.configdir != null
* init'ed(com/dmdirc/ServerManager.me)
* java.lang.StringBuilder:toString(...)._tainted == 0
* init'ed(java.lang.StringBuilder:toString(...)._tainted)
* init'ed(new ArrayList(ServerManager#1) num objects)
* init'ed(new ServerManager(getServerManager#1) num objects)
* init'ed(new ServerManager(getServerManager#1).servers)
*
* Test Vectors:
* java.util.Iterator:hasNext(...)@500: {0}, {1}
*/
490 Logger.assertTrue(oldName != null);
491 Logger.assertTrue(!oldName.isEmpty());
492 Logger.assertTrue(newName != null);
493 Logger.assertTrue(!newName.isEmpty());
494 Logger.assertTrue(groups.containsKey(oldName));
495 Logger.assertTrue(!groups.containsKey(newName));
496 Logger.assertTrue(!newName.equals(oldName));
497
498 makeGroup(newName);
499
500 for (Action action : groups.get(oldName).getActions()) {
501 action.setGroup(newName);
502 getGroup(oldName).remove(action);
503 getGroup(newName).add(action);
504 }
505
506 removeGroup(oldName);
507 }
508
509 /**
510 * Returns the action comparison specified by the given string, or null if it
511 * doesn't match a valid registered action comparison.
512 *
513 * @param type The name of the action comparison to try and find
514 * @return The actioncomparison with the specified name, or null on failure
515 */
516 public static ActionType getActionType(final String type) {
/*
P/P * Method: ActionType getActionType(String)
*
* Presumptions:
* java.lang.Enum:name(...)@522 != null
* java.util.Iterator:next(...)@521 != null
*
* Postconditions:
* init'ed(return_value)
*
* Test Vectors:
* type: Addr_Set{null}, Inverse{null}
* java.lang.String:equals(...)@522: {0}, {1}
* java.lang.String:isEmpty(...)@517: {0}, {1}
* java.util.Iterator:hasNext(...)@521: {0}, {1}
*/
517 if (type == null || type.isEmpty()) {
518 return null;
519 }
520
521 for (ActionType target : actionTypes) {
522 if (((Enum) target).name().equals(type)) {
523 return target;
524 }
525 }
526
527 return null;
528 }
529
530 /**
531 * Returns a list of action types that are compatible with the one
532 * specified.
533 *
534 * @param type The type to be checked against
535 * @return A list of compatible action types
536 */
537 @Precondition("The specified type is not null")
538 public static List<ActionType> getCompatibleTypes(final ActionType type) {
/*
P/P * Method: List getCompatibleTypes(ActionType)
*
* Preconditions:
* (soft) type != null
*
* Presumptions:
* java.util.Iterator:next(...)@542 != null
* target.type@542 != null
*
* Postconditions:
* return_value == &new ArrayList(getCompatibleTypes#1)
* new ArrayList(getCompatibleTypes#1) num objects == 1
*
* Test Vectors:
* java.lang.Object:equals(...)@543: {1}, {0}
* java.lang.Object:equals(...)@543: {0}, {1}
* java.util.Iterator:hasNext(...)@542: {0}, {1}
*/
539 Logger.assertTrue(type != null);
540
541 final List<ActionType> res = new ArrayList<ActionType>();
542 for (ActionType target : actionTypes) {
543 if (!target.equals(type) && target.getType().equals(type.getType())) {
544 res.add(target);
545 }
546 }
547
548 return res;
549 }
550
551 /**
552 * Returns a list of action components that are compatible with the
553 * specified class.
554 *
555 * @param target The class to be tested
556 * @return A list of compatible action components
557 */
558 @Precondition("The specified target is not null")
559 public static List<ActionComponent> getCompatibleComponents(final Class target) {
/*
P/P * Method: List getCompatibleComponents(Class)
*
* Presumptions:
* appliesTo(...)@564 != null
* java.util.Iterator:next(...)@563 != null
*
* Postconditions:
* return_value == &new ArrayList(getCompatibleComponents#1)
* new ArrayList(getCompatibleComponents#1) num objects == 1
*
* Test Vectors:
* java.lang.Object:equals(...)@564: {0}, {1}
* java.util.Iterator:hasNext(...)@563: {0}, {1}
*/
560 Logger.assertTrue(target != null);
561
562 final List<ActionComponent> res = new ArrayList<ActionComponent>();
563 for (ActionComponent subject : actionComponents) {
564 if (subject.appliesTo().equals(target)) {
565 res.add(subject);
566 }
567 }
568
569 return res;
570 }
571
572 /**
573 * Returns a list of action comparisons that are compatible with the
574 * specified class.
575 *
576 * @param target The class to be tested
577 * @return A list of compatible action comparisons
578 */
579 @Precondition("The specified target is not null")
580 public static List<ActionComparison> getCompatibleComparisons(final Class target) {
/*
P/P * Method: List getCompatibleComparisons(Class)
*
* Presumptions:
* appliesTo(...)@585 != null
* java.util.Iterator:next(...)@584 != null
*
* Postconditions:
* return_value == &new ArrayList(getCompatibleComparisons#1)
* new ArrayList(getCompatibleComparisons#1) num objects == 1
*
* Test Vectors:
* java.lang.Object:equals(...)@585: {0}, {1}
* java.util.Iterator:hasNext(...)@584: {0}, {1}
*/
581 Logger.assertTrue(target != null);
582
583 final List<ActionComparison> res = new ArrayList<ActionComparison>();
584 for (ActionComparison subject : actionComparisons) {
585 if (subject.appliesTo().equals(target)) {
586 res.add(subject);
587 }
588 }
589
590 return res;
591 }
592
593 /**
594 * Returns a list of all the action types registered by this manager.
595 *
596 * @return A list of registered action types
597 */
598 public static List<ActionType> getTypes() {
/*
P/P * Method: List getTypes()
*
* Postconditions:
* return_value == &new ArrayList(ActionManager__static_init#1)
*/
599 return actionTypes;
600 }
601
602 /**
603 * Returns a list of all the action types registered by this manager.
604 *
605 * @return A list of registered action comparisons
606 */
607 public static List<ActionComparison> getComparisons() {
/*
P/P * Method: List getComparisons()
*
* Postconditions:
* return_value == &new ArrayList(ActionManager__static_init#3)
*/
608 return actionComparisons;
609 }
610
611 /**
612 * Returns the action component specified by the given string, or null if it
613 * doesn't match a valid registered action component.
614 *
615 * @param type The name of the action component to try and find
616 * @return The actioncomponent with the specified name, or null on failure
617 */
618 @Precondition("The specified type is non-null and not empty")
619 public static ActionComponent getActionComponent(final String type) {
/*
P/P * Method: ActionComponent getActionComponent(String)
*
* Preconditions:
* type != null
*
* Presumptions:
* java.lang.Enum:name(...)@624 != null
* java.util.Iterator:next(...)@623 != null
*
* Postconditions:
* init'ed(return_value)
*
* Test Vectors:
* java.lang.String:equals(...)@624: {0}, {1}
* java.util.Iterator:hasNext(...)@623: {0}, {1}
*/
620 Logger.assertTrue(type != null);
621 Logger.assertTrue(!type.isEmpty());
622
623 for (ActionComponent target : actionComponents) {
624 if (((Enum) target).name().equals(type)) {
625 return target;
626 }
627 }
628
629 return null;
630 }
631
632 /**
633 * Returns the action type specified by the given string, or null if it
634 * doesn't match a valid registered action type.
635 *
636 * @param type The name of the action type to try and find
637 * @return The actiontype with the specified name, or null on failure
638 */
639 @Precondition("The specified type is non-null and not empty")
640 public static ActionComparison getActionComparison(final String type) {
/*
P/P * Method: ActionComparison getActionComparison(String)
*
* Preconditions:
* type != null
*
* Presumptions:
* java.lang.Enum:name(...)@645 != null
* java.util.Iterator:next(...)@644 != null
*
* Postconditions:
* init'ed(return_value)
*
* Test Vectors:
* java.lang.String:equals(...)@645: {0}, {1}
* java.util.Iterator:hasNext(...)@644: {0}, {1}
*/
641 Logger.assertTrue(type != null);
642 Logger.assertTrue(!type.isEmpty());
643
644 for (ActionComparison target : actionComparisons) {
645 if (((Enum) target).name().equals(type)) {
646 return target;
647 }
648 }
649
650 return null;
651 }
652
653 /**
654 * Installs an action pack located at the specified path.
655 *
656 * @param path The full path of the action pack .zip.
657 * @throws IOException If the zip cannot be extracted
658 */
659 public static void installActionPack(final String path) throws IOException {
/*
P/P * Method: void installActionPack(String)
*
* Preconditions:
* init'ed(com/dmdirc/Main.configdir)
*
* Presumptions:
* com.dmdirc.util.resourcemanager.ZipResourceManager:getInstance(...)@660 != null
*
* Postconditions:
* init'ed(com/dmdirc/Main.configdir)
* possibly_updated(com/dmdirc/ServerManager.me)
* java.lang.StringBuilder:toString(...)._tainted == 0
* init'ed(new ArrayList(ServerManager#1) num objects)
* init'ed(new ServerManager(getServerManager#1) num objects)
* init'ed(new ServerManager(getServerManager#1).servers)
*/
660 final ZipResourceManager ziprm = ZipResourceManager.getInstance(path);
661
662 ziprm.extractResources("", getDirectory());
663
664 loadActions();
665
666 new File(path).delete();
667 }
668
669 /**
670 * Adds a new listener for the specified action type.
671 *
672 * @param types The action types that are to be listened for
673 * @param listener The listener to be added
674 */
675 public static void addListener(final ActionListener listener, final ActionType ... types) {
/*
P/P * Method: void addListener(ActionListener, ActionType[])
*
* Preconditions:
* types != null
* types.length <= 232-1
* (soft) init'ed(types[...])
*/
676 for (ActionType type : types) {
677 listeners.add(type, listener);
678 }
679 }
680
681 /**
682 * Removes a listener for the specified action type.
683 *
684 * @param types The action types that were being listened for
685 * @param listener The listener to be removed
686 */
687 public static void removeListener(final ActionListener listener, final ActionType ... types) {
/*
P/P * Method: void removeListener(ActionListener, ActionType[])
*
* Preconditions:
* types != null
* types.length <= 232-1
* (soft) init'ed(types[...])
*/
688 for (ActionType type : types) {
689 listeners.remove(type, listener);
690 }
691 }
692
693 /**
694 * Removes a listener for all action types.
695 *
696 * @param listener The listener to be removed
697 */
698 public static void removeListener(final ActionListener listener) {
/*
P/P * Method: void removeListener(ActionListener)
*/
699 listeners.removeFromAll(listener);
700 }
701 }
SofCheck Inspector Build Version : 2.17854
| ActionManager.java |
2009-Jun-25 01:54:24 |
| ActionManager.class |
2009-Sep-02 17:04:13 |
| ActionManager$1.class |
2009-Sep-02 17:04:13 |
| ActionManager$2.class |
2009-Sep-02 17:04:13 |