|
@@ -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
|
+}
|