Browse Source

Merge pull request #628 from csmith/tidying

Allow plugins to specify event formats.
pull/629/head
Greg Holmes 8 years ago
parent
commit
8c213dd424

+ 98
- 0
src/com/dmdirc/plugins/PluginEventFormatManager.java View File

@@ -0,0 +1,98 @@
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.plugins;
24
+
25
+import com.dmdirc.ClientModule.GlobalConfig;
26
+import com.dmdirc.DMDircMBassador;
27
+import com.dmdirc.events.PluginLoadedEvent;
28
+import com.dmdirc.events.PluginUnloadedEvent;
29
+import com.dmdirc.interfaces.SystemLifecycleComponent;
30
+import com.dmdirc.ui.messages.ColourManager;
31
+import com.dmdirc.ui.messages.EventFormatProvider;
32
+import com.dmdirc.ui.messages.MultiEventFormatProvider;
33
+import com.dmdirc.ui.messages.YamlEventFormatProvider;
34
+
35
+import com.google.common.eventbus.Subscribe;
36
+
37
+import java.nio.file.Files;
38
+import java.nio.file.Path;
39
+import java.util.HashMap;
40
+import java.util.Map;
41
+
42
+import javax.inject.Inject;
43
+import javax.inject.Singleton;
44
+
45
+/**
46
+ * Loads default event formats from plugins.
47
+ *
48
+ * <p>Plugins can bundle a format file in {@code /META-INF/format.yml}, and it will be
49
+ * automatically added to the client when the plugin is loaded.
50
+ */
51
+@Singleton
52
+public class PluginEventFormatManager implements SystemLifecycleComponent {
53
+
54
+    private final DMDircMBassador eventbus;
55
+    private final MultiEventFormatProvider multiEventFormatProvider;
56
+    private final Map<PluginInfo, EventFormatProvider> providers = new HashMap<>();
57
+    private final ColourManager colourManager;
58
+
59
+    @Inject
60
+    public PluginEventFormatManager(
61
+            final DMDircMBassador eventbus,
62
+            final MultiEventFormatProvider multiEventFormatProvider,
63
+            @GlobalConfig final ColourManager colourManager) {
64
+        this.eventbus = eventbus;
65
+        this.multiEventFormatProvider = multiEventFormatProvider;
66
+        this.colourManager = colourManager;
67
+    }
68
+
69
+    @Override
70
+    public void startUp() {
71
+        eventbus.subscribe(this);
72
+    }
73
+
74
+    @Override
75
+    public void shutDown() {
76
+        eventbus.unsubscribe(this);
77
+    }
78
+
79
+    @Subscribe
80
+    public void handlePluginLoaded(final PluginLoadedEvent event) {
81
+        final Path path = event.getPlugin().getPath("/META-INF/format.yml");
82
+        if (Files.exists(path)) {
83
+            final YamlEventFormatProvider provider =
84
+                    new YamlEventFormatProvider(path, colourManager);
85
+            provider.load();
86
+            providers.put(event.getPlugin(), provider);
87
+            multiEventFormatProvider.addProvider(provider);
88
+        }
89
+    }
90
+
91
+    @Subscribe
92
+    public void handlePluginUnloaded(final PluginUnloadedEvent event) {
93
+        if (providers.containsKey(event.getPlugin())) {
94
+            multiEventFormatProvider.removeProvider(providers.remove(event.getPlugin()));
95
+        }
96
+    }
97
+
98
+}

+ 7
- 1
src/com/dmdirc/plugins/PluginModule.java View File

@@ -23,6 +23,7 @@
23 23
 package com.dmdirc.plugins;
24 24
 
25 25
 import com.dmdirc.DMDircMBassador;
26
+import com.dmdirc.interfaces.SystemLifecycleComponent;
26 27
 import com.dmdirc.interfaces.config.IdentityController;
27 28
 import com.dmdirc.updater.manager.UpdateManager;
28 29
 
@@ -42,7 +43,6 @@ import static com.dmdirc.commandline.CommandLineOptionsModule.DirectoryType;
42 43
 @Module(library = true, complete = false)
43 44
 public class PluginModule {
44 45
 
45
-
46 46
     @Provides
47 47
     @Singleton
48 48
     public PluginManager getPluginManager(
@@ -84,4 +84,10 @@ public class PluginModule {
84 84
         return locator;
85 85
     }
86 86
 
87
+    @Provides(type = Provides.Type.SET)
88
+    @Singleton
89
+    public SystemLifecycleComponent getEventFormatManager(final PluginEventFormatManager manager) {
90
+        return manager;
91
+    }
92
+
87 93
 }

Loading…
Cancel
Save