|
@@ -0,0 +1,225 @@
|
|
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.addons.notifications;
|
|
23
|
+
|
|
24
|
+import com.dmdirc.actions.ActionManager;
|
|
25
|
+import com.dmdirc.actions.CoreActionType;
|
|
26
|
+import com.dmdirc.actions.interfaces.ActionType;
|
|
27
|
+import com.dmdirc.addons.ui_swing.UIUtilities;
|
|
28
|
+import com.dmdirc.commandparser.CommandManager;
|
|
29
|
+import com.dmdirc.config.IdentityManager;
|
|
30
|
+import com.dmdirc.config.prefs.PluginPreferencesCategory;
|
|
31
|
+import com.dmdirc.config.prefs.PreferencesCategory;
|
|
32
|
+import com.dmdirc.config.prefs.PreferencesManager;
|
|
33
|
+import com.dmdirc.interfaces.ActionListener;
|
|
34
|
+import com.dmdirc.plugins.Plugin;
|
|
35
|
+import com.dmdirc.plugins.PluginInfo;
|
|
36
|
+import com.dmdirc.plugins.PluginManager;
|
|
37
|
+import com.dmdirc.util.ReturnableThread;
|
|
38
|
+
|
|
39
|
+import java.util.ArrayList;
|
|
40
|
+import java.util.List;
|
|
41
|
+
|
|
42
|
+/**
|
|
43
|
+ * Notification Manager plugin, aggregates notification sources exposing them
|
|
44
|
+ * via a single command.
|
|
45
|
+ */
|
|
46
|
+public class NotificationsPlugin extends Plugin implements ActionListener {
|
|
47
|
+
|
|
48
|
+ /** The notification methods that we know of. */
|
|
49
|
+ private final List<String> methods = new ArrayList<String>();
|
|
50
|
+ /** The command we're registering. */
|
|
51
|
+ private NotificationCommand command;
|
|
52
|
+ /** The user's preferred order for method usage. */
|
|
53
|
+ private List<String> order;
|
|
54
|
+
|
|
55
|
+ /**
|
|
56
|
+ * Creates a new notifications plugin.
|
|
57
|
+ */
|
|
58
|
+ public NotificationsPlugin() {
|
|
59
|
+ super();
|
|
60
|
+ }
|
|
61
|
+
|
|
62
|
+ /** {@inheritDoc} */
|
|
63
|
+ @Override
|
|
64
|
+ public void onLoad() {
|
|
65
|
+ methods.clear();
|
|
66
|
+ loadSettings();
|
|
67
|
+ ActionManager.addListener(this, CoreActionType.PLUGIN_LOADED,
|
|
68
|
+ CoreActionType.PLUGIN_UNLOADED);
|
|
69
|
+ for (PluginInfo target : PluginManager.getPluginManager()
|
|
70
|
+ .getPluginInfos()) {
|
|
71
|
+ if (target.isLoaded()) {
|
|
72
|
+ addPlugin(target);
|
|
73
|
+ }
|
|
74
|
+ }
|
|
75
|
+ command = new NotificationCommand(this);
|
|
76
|
+ CommandManager.registerCommand(command);
|
|
77
|
+ }
|
|
78
|
+
|
|
79
|
+ /** {@inheritDoc} */
|
|
80
|
+ @Override
|
|
81
|
+ public void onUnload() {
|
|
82
|
+ methods.clear();
|
|
83
|
+ ActionManager.removeListener(this);
|
|
84
|
+ CommandManager.unregisterCommand(command);
|
|
85
|
+ }
|
|
86
|
+
|
|
87
|
+ /** {@inheritDoc} */
|
|
88
|
+ @Override
|
|
89
|
+ public void showConfig(final PreferencesManager manager) {
|
|
90
|
+ final NotificationConfig configPanel = UIUtilities.invokeAndWait(
|
|
91
|
+ new ReturnableThread<NotificationConfig>() {
|
|
92
|
+
|
|
93
|
+ /** {@inheritDoc} */
|
|
94
|
+ @Override
|
|
95
|
+ public void run() {
|
|
96
|
+ setObject(new NotificationConfig(NotificationsPlugin.this,
|
|
97
|
+ order));
|
|
98
|
+ }
|
|
99
|
+ });
|
|
100
|
+
|
|
101
|
+ final PreferencesCategory category = new PluginPreferencesCategory(
|
|
102
|
+ getPluginInfo(), "Notifications", "", "category-notifications",
|
|
103
|
+ configPanel);
|
|
104
|
+ manager.getCategory("Plugins").addSubCategory(category);
|
|
105
|
+ }
|
|
106
|
+
|
|
107
|
+ /** Loads the plugins settings. */
|
|
108
|
+ private void loadSettings() {
|
|
109
|
+ if (IdentityManager.getGlobalConfig().hasOptionString(getDomain(),
|
|
110
|
+ "methodOrder")) {
|
|
111
|
+ order = IdentityManager.getGlobalConfig().getOptionList(
|
|
112
|
+ getDomain(), "methodOrder");
|
|
113
|
+ } else {
|
|
114
|
+ order = new ArrayList<String>();
|
|
115
|
+ }
|
|
116
|
+ }
|
|
117
|
+
|
|
118
|
+ /** {@inheritDoc} */
|
|
119
|
+ @Override
|
|
120
|
+ public void processEvent(final ActionType type, final StringBuffer format,
|
|
121
|
+ final Object... arguments) {
|
|
122
|
+ if (type == CoreActionType.PLUGIN_LOADED) {
|
|
123
|
+ addPlugin((PluginInfo) arguments[0]);
|
|
124
|
+ } else if (type == CoreActionType.PLUGIN_UNLOADED) {
|
|
125
|
+ removePlugin((PluginInfo) arguments[0]);
|
|
126
|
+ }
|
|
127
|
+ }
|
|
128
|
+
|
|
129
|
+ /**
|
|
130
|
+ * Checks to see if a plugin implements the notification method interface
|
|
131
|
+ * and if it does, adds the method to our list.
|
|
132
|
+ *
|
|
133
|
+ * @param target The plugin to be tested
|
|
134
|
+ */
|
|
135
|
+ private void addPlugin(final PluginInfo target) {
|
|
136
|
+ if (target.hasExportedService("showNotification")) {
|
|
137
|
+ methods.add(target.getName());
|
|
138
|
+ addMethodToOrder(target);
|
|
139
|
+ }
|
|
140
|
+ }
|
|
141
|
+
|
|
142
|
+ /**
|
|
143
|
+ * Checks to see if the specified notification method needs to be added to
|
|
144
|
+ * our order list, and adds it if neccessary.
|
|
145
|
+ *
|
|
146
|
+ * @param source The notification method to be tested
|
|
147
|
+ */
|
|
148
|
+ private void addMethodToOrder(final PluginInfo source) {
|
|
149
|
+ if (!order.contains(source.getName())) {
|
|
150
|
+ order.add(source.getName());
|
|
151
|
+ }
|
|
152
|
+ }
|
|
153
|
+
|
|
154
|
+ /**
|
|
155
|
+ * Checks to see if a plugin implements the notification method interface
|
|
156
|
+ * and if it does, removes the method from our list.
|
|
157
|
+ *
|
|
158
|
+ * @param target The plugin to be tested
|
|
159
|
+ */
|
|
160
|
+ private void removePlugin(final PluginInfo target) {
|
|
161
|
+ methods.remove(target.getName());
|
|
162
|
+ }
|
|
163
|
+
|
|
164
|
+ /**
|
|
165
|
+ * Retrieves a method based on its name.
|
|
166
|
+ *
|
|
167
|
+ * @param name The name to search for
|
|
168
|
+ * @return The method with the specified name or null if none were found.
|
|
169
|
+ */
|
|
170
|
+ public PluginInfo getMethod(final String name) {
|
|
171
|
+ return PluginManager.getPluginManager().getPluginInfoByName(name);
|
|
172
|
+ }
|
|
173
|
+
|
|
174
|
+ /**
|
|
175
|
+ * Retrieves all the methods registered with this plugin.
|
|
176
|
+ *
|
|
177
|
+ * @return All known notification sources
|
|
178
|
+ */
|
|
179
|
+ public List<PluginInfo> getMethods() {
|
|
180
|
+ final List<PluginInfo> plugins = new ArrayList<PluginInfo>();
|
|
181
|
+ for (String method : methods) {
|
|
182
|
+ plugins.add(PluginManager.getPluginManager()
|
|
183
|
+ .getPluginInfoByName(method));
|
|
184
|
+ }
|
|
185
|
+ return plugins;
|
|
186
|
+ }
|
|
187
|
+
|
|
188
|
+ /**
|
|
189
|
+ * Does this plugin have any active notification methods?
|
|
190
|
+ *
|
|
191
|
+ * @return true iif active notification methods are registered
|
|
192
|
+ */
|
|
193
|
+ public boolean hasActiveMethod() {
|
|
194
|
+ return !methods.isEmpty();
|
|
195
|
+ }
|
|
196
|
+
|
|
197
|
+ /**
|
|
198
|
+ * Returns the user's preferred method if loaded, or null if none loaded.
|
|
199
|
+ *
|
|
200
|
+ * @return Preferred notification method
|
|
201
|
+ */
|
|
202
|
+ public PluginInfo getPreferredMethod() {
|
|
203
|
+ if (methods.isEmpty()) {
|
|
204
|
+ return null;
|
|
205
|
+ }
|
|
206
|
+ for (String method : order) {
|
|
207
|
+ if (methods.contains(method)) {
|
|
208
|
+ return PluginManager.getPluginManager().getPluginInfoByName(
|
|
209
|
+ method);
|
|
210
|
+ }
|
|
211
|
+ }
|
|
212
|
+ return null;
|
|
213
|
+ }
|
|
214
|
+
|
|
215
|
+ /**
|
|
216
|
+ * Saves the plugins settings.
|
|
217
|
+ *
|
|
218
|
+ * @param newOrder The new order for methods
|
|
219
|
+ */
|
|
220
|
+ protected void saveSettings(final List<String> newOrder) {
|
|
221
|
+ order = newOrder;
|
|
222
|
+ IdentityManager.getConfigIdentity().setOption(getDomain(),
|
|
223
|
+ "methodOrder", order);
|
|
224
|
+ }
|
|
225
|
+}
|