You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PluginEventFormatManager.java 3.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.plugins;
  18. import com.dmdirc.config.GlobalConfig;
  19. import com.dmdirc.events.PluginLoadedEvent;
  20. import com.dmdirc.events.PluginUnloadedEvent;
  21. import com.dmdirc.events.eventbus.EventBus;
  22. import com.dmdirc.ui.messages.ColourManager;
  23. import com.dmdirc.ui.messages.EventFormatProvider;
  24. import com.dmdirc.ui.messages.MultiEventFormatProvider;
  25. import com.dmdirc.ui.messages.YamlEventFormatProvider;
  26. import com.dmdirc.util.system.SystemLifecycleComponent;
  27. import net.engio.mbassy.listener.Handler;
  28. import javax.inject.Inject;
  29. import javax.inject.Singleton;
  30. import java.nio.file.Files;
  31. import java.nio.file.Path;
  32. import java.util.HashMap;
  33. import java.util.Map;
  34. /**
  35. * Loads default event formats from plugins.
  36. *
  37. * <p>Plugins can bundle a format file in {@code /META-INF/format.yml}, and it will be
  38. * automatically added to the client when the plugin is loaded.
  39. */
  40. @Singleton
  41. public class PluginEventFormatManager implements SystemLifecycleComponent {
  42. private final EventBus eventbus;
  43. private final MultiEventFormatProvider multiEventFormatProvider;
  44. private final Map<PluginInfo, EventFormatProvider> providers = new HashMap<>();
  45. private final ColourManager colourManager;
  46. private final DisplayLocationManager displayLocationManager;
  47. @Inject
  48. public PluginEventFormatManager(
  49. final EventBus eventbus,
  50. final MultiEventFormatProvider multiEventFormatProvider,
  51. @GlobalConfig final ColourManager colourManager,
  52. final DisplayLocationManager displayLocationManager) {
  53. this.eventbus = eventbus;
  54. this.multiEventFormatProvider = multiEventFormatProvider;
  55. this.colourManager = colourManager;
  56. this.displayLocationManager = displayLocationManager;
  57. }
  58. @Override
  59. public void startUp() {
  60. eventbus.subscribe(this);
  61. }
  62. @Override
  63. public void shutDown() {
  64. eventbus.unsubscribe(this);
  65. }
  66. @Handler
  67. public void handlePluginLoaded(final PluginLoadedEvent event) {
  68. final Path path = event.getPlugin().getPath("/META-INF/format.yml");
  69. if (Files.exists(path)) {
  70. final YamlEventFormatProvider provider =
  71. new YamlEventFormatProvider(path, colourManager, displayLocationManager);
  72. provider.load();
  73. providers.put(event.getPlugin(), provider);
  74. multiEventFormatProvider.addProvider(provider);
  75. }
  76. }
  77. @Handler
  78. public void handlePluginUnloaded(final PluginUnloadedEvent event) {
  79. if (providers.containsKey(event.getPlugin())) {
  80. multiEventFormatProvider.removeProvider(providers.remove(event.getPlugin()));
  81. }
  82. }
  83. }