瀏覽代碼

Start making the NotificationManager more sane.

Add an interface for handlers, rework the manager to always use
the interface instead of doing dodgy exported stuff.
pull/299/head
Chris Smith 9 年之前
父節點
當前提交
39e67d2dca

+ 47
- 0
notifications/src/com/dmdirc/addons/notifications/LegacyNotificationHandler.java 查看文件

@@ -0,0 +1,47 @@
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
+
23
+package com.dmdirc.addons.notifications;
24
+
25
+import com.dmdirc.plugins.ExportedService;
26
+import com.dmdirc.plugins.PluginInfo;
27
+
28
+/**
29
+ * A notification handler that works using exported services.
30
+ */
31
+public class LegacyNotificationHandler implements NotificationHandler {
32
+
33
+    private final PluginInfo pluginInfo;
34
+
35
+    public LegacyNotificationHandler(final PluginInfo pluginInfo) {
36
+        this.pluginInfo = pluginInfo;
37
+    }
38
+
39
+    @Override
40
+    public void showNotification(final String title, final String message) {
41
+        final ExportedService source = pluginInfo.getExportedService("showNotification");
42
+        if (source != null) {
43
+            source.execute(title, message);
44
+        }
45
+    }
46
+
47
+}

+ 12
- 19
notifications/src/com/dmdirc/addons/notifications/NotificationCommand.java 查看文件

@@ -30,12 +30,9 @@ import com.dmdirc.commandparser.commands.Command;
30 30
 import com.dmdirc.commandparser.commands.IntelligentCommand;
31 31
 import com.dmdirc.commandparser.commands.context.CommandContext;
32 32
 import com.dmdirc.interfaces.CommandController;
33
-import com.dmdirc.plugins.ExportedService;
34
-import com.dmdirc.plugins.PluginInfo;
35 33
 import com.dmdirc.ui.input.AdditionalTabTargets;
36 34
 
37
-import java.util.List;
38
-import java.util.stream.Collectors;
35
+import java.util.Collection;
39 36
 
40 37
 import javax.annotation.Nonnull;
41 38
 
@@ -78,22 +75,20 @@ public class NotificationCommand extends Command implements
78 75
                 "--method".equalsIgnoreCase(args.getArguments()[0])) {
79 76
             if (args.getArguments().length > 1) {
80 77
                 final String sourceName = args.getArguments()[1];
81
-                final ExportedService source = manager.getMethod(sourceName)
82
-                        .getExportedService("showNotification");
78
+                final NotificationHandler handler = manager.getHandler(sourceName);
83 79
 
84
-                if (source == null) {
80
+                if (handler == null) {
85 81
                     sendLine(origin, args.isSilent(), FORMAT_ERROR,
86 82
                             "Method not found.");
87 83
                 } else {
88
-                    source.execute("DMDirc", args.getArgumentsAsString(2));
84
+                    handler.showNotification("DMDirc", args.getArgumentsAsString(2));
89 85
                 }
90 86
             } else {
91 87
                 sendLine(origin, args.isSilent(), FORMAT_ERROR,
92 88
                         "You must specify a method when using --method.");
93 89
             }
94
-        } else if (manager.hasActiveMethod()) {
95
-            manager.getPreferredMethod().getExportedService("showNotification")
96
-                    .execute("DMDirc", args.getArgumentsAsString(0));
90
+        } else if (manager.hasActiveHandler()) {
91
+            manager.getPreferredHandler().showNotification("DMDirc", args.getArgumentsAsString(0));
97 92
         } else {
98 93
             sendLine(origin, args.isSilent(), FORMAT_ERROR,
99 94
                     "No active notification methods available.");
@@ -108,17 +103,17 @@ public class NotificationCommand extends Command implements
108 103
      */
109 104
     private void doMethodList(final FrameContainer origin,
110 105
             final boolean isSilent) {
111
-        final List<PluginInfo> methods = manager.getMethods();
106
+        final Collection<String> handlers = manager.getHandlerNames();
112 107
 
113
-        if (methods.isEmpty()) {
108
+        if (handlers.isEmpty()) {
114 109
             sendLine(origin, isSilent, FORMAT_ERROR, "No notification "
115 110
                     + "methods available.");
116 111
         } else {
117 112
             final String[] headers = {"Method"};
118
-            final String[][] data = new String[methods.size()][1];
113
+            final String[][] data = new String[handlers.size()][1];
119 114
             int i = 0;
120
-            for (PluginInfo method : methods) {
121
-                data[i][0] = method.getMetaData().getName();
115
+            for (String handler : handlers) {
116
+                data[i][0] = handler;
122 117
                 i++;
123 118
             }
124 119
 
@@ -136,9 +131,7 @@ public class NotificationCommand extends Command implements
136 131
             res.add("--method");
137 132
             return res;
138 133
         } else if (arg == 1 && "--method".equalsIgnoreCase(context.getPreviousArgs().get(0))) {
139
-            res.addAll(manager.getMethods().stream()
140
-                    .map(source -> source.getMetaData().getName())
141
-                    .collect(Collectors.toList()));
134
+            res.addAll(manager.getHandlerNames());
142 135
             return res;
143 136
         }
144 137
         return res;

+ 38
- 0
notifications/src/com/dmdirc/addons/notifications/NotificationHandler.java 查看文件

@@ -0,0 +1,38 @@
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
+
23
+package com.dmdirc.addons.notifications;
24
+
25
+/**
26
+ * Handler of notifications.
27
+ */
28
+public interface NotificationHandler {
29
+
30
+    /**
31
+     * Displays a notification to the user.
32
+     *
33
+     * @param title The title of the notification.
34
+     * @param message The notification content.
35
+     */
36
+    void showNotification(String title, String message);
37
+
38
+}

+ 28
- 30
notifications/src/com/dmdirc/addons/notifications/NotificationsManager.java 查看文件

@@ -38,7 +38,9 @@ import com.dmdirc.plugins.PluginManager;
38 38
 
39 39
 import java.util.ArrayList;
40 40
 import java.util.Collection;
41
+import java.util.HashMap;
41 42
 import java.util.List;
43
+import java.util.Map;
42 44
 
43 45
 import javax.inject.Inject;
44 46
 
@@ -46,8 +48,8 @@ import net.engio.mbassy.listener.Handler;
46 48
 
47 49
 public class NotificationsManager {
48 50
 
49
-    /** The notification methods that we know of. */
50
-    private final Collection<String> methods = new ArrayList<>();
51
+    /** The notification handlers that we know of. */
52
+    private final Map<String, NotificationHandler> handlers = new HashMap<>();
51 53
     /** The user's preferred order for method usage. */
52 54
     private List<String> order;
53 55
     /** This plugin's settings domain. */
@@ -73,7 +75,7 @@ public class NotificationsManager {
73 75
     }
74 76
 
75 77
     public void onLoad() {
76
-        methods.clear();
78
+        handlers.clear();
77 79
         loadSettings();
78 80
         eventBus.subscribe(this);
79 81
         pluginManager.getPluginInfos().stream()
@@ -82,7 +84,7 @@ public class NotificationsManager {
82 84
     }
83 85
 
84 86
     public void onUnload() {
85
-        methods.clear();
87
+        handlers.clear();
86 88
         eventBus.unsubscribe(this);
87 89
     }
88 90
 
@@ -113,8 +115,8 @@ public class NotificationsManager {
113 115
      */
114 116
     private void addPlugin(final PluginInfo target) {
115 117
         if (target.hasExportedService("showNotification")) {
116
-            methods.add(target.getMetaData().getName());
117
-            addMethodToOrder(target);
118
+            handlers.put(target.getMetaData().getName(), new LegacyNotificationHandler(pluginInfo));
119
+            addHandlerToOrder(target);
118 120
         }
119 121
     }
120 122
 
@@ -124,7 +126,7 @@ public class NotificationsManager {
124 126
      *
125 127
      * @param source The notification method to be tested
126 128
      */
127
-    private void addMethodToOrder(final PluginInfo source) {
129
+    private void addHandlerToOrder(final PluginInfo source) {
128 130
         if (!order.contains(source.getMetaData().getName())) {
129 131
             order.add(source.getMetaData().getName());
130 132
         }
@@ -137,54 +139,50 @@ public class NotificationsManager {
137 139
      * @param target The plugin to be tested
138 140
      */
139 141
     private void removePlugin(final PluginInfo target) {
140
-        methods.remove(target.getMetaData().getName());
142
+        handlers.remove(target.getMetaData().getName());
141 143
     }
142 144
 
143 145
     /**
144
-     * Retrieves a method based on its name.
146
+     * Retrieves a handler based on its name.
145 147
      *
146 148
      * @param name The name to search for
147 149
      *
148
-     * @return The method with the specified name or null if none were found.
150
+     * @return The handler with the specified name or null if none were found.
149 151
      */
150
-    public PluginInfo getMethod(final String name) {
151
-        return pluginManager.getPluginInfoByName(name);
152
+    public NotificationHandler getHandler(final String name) {
153
+        return handlers.get(name);
152 154
     }
153 155
 
154 156
     /**
155
-     * Retrieves all the methods registered with this plugin.
157
+     * Retrieves the names of all the handlers registered with this plugin.
156 158
      *
157
-     * @return All known notification sources
159
+     * @return All known notification handler names
158 160
      */
159
-    public List<PluginInfo> getMethods() {
160
-        final List<PluginInfo> plugins = new ArrayList<>();
161
-        for (String method : methods) {
162
-            plugins.add(pluginManager.getPluginInfoByName(method));
163
-        }
164
-        return plugins;
161
+    public Collection<String> getHandlerNames() {
162
+        return handlers.keySet();
165 163
     }
166 164
 
167 165
     /**
168
-     * Does this plugin have any active notification methods?
166
+     * Does this plugin have any active notification handler?
169 167
      *
170
-     * @return true iif active notification methods are registered
168
+     * @return true iif active notification handlers are registered
171 169
      */
172
-    public boolean hasActiveMethod() {
173
-        return !methods.isEmpty();
170
+    public boolean hasActiveHandler() {
171
+        return !handlers.isEmpty();
174 172
     }
175 173
 
176 174
     /**
177
-     * Returns the user's preferred method if loaded, or null if none loaded.
175
+     * Returns the user's preferred handler if loaded, or null if none loaded.
178 176
      *
179
-     * @return Preferred notification method
177
+     * @return Preferred notification handler
180 178
      */
181
-    public PluginInfo getPreferredMethod() {
182
-        if (methods.isEmpty()) {
179
+    public NotificationHandler getPreferredHandler() {
180
+        if (handlers.isEmpty()) {
183 181
             return null;
184 182
         }
185 183
         for (String method : order) {
186
-            if (methods.contains(method)) {
187
-                return pluginManager.getPluginInfoByName(method);
184
+            if (handlers.containsKey(method)) {
185
+                return handlers.get(method);
188 186
             }
189 187
         }
190 188
         return null;

Loading…
取消
儲存