Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

PluginEventFormatManager.java 3.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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.*;
  23. import com.dmdirc.util.system.SystemLifecycleComponent;
  24. import net.engio.mbassy.listener.Handler;
  25. import javax.inject.Inject;
  26. import javax.inject.Singleton;
  27. import java.nio.file.Files;
  28. import java.nio.file.Path;
  29. import java.util.HashMap;
  30. import java.util.Map;
  31. /**
  32. * Loads default event formats from plugins.
  33. *
  34. * <p>Plugins can bundle a format file in {@code /META-INF/format.yml}, and it will be
  35. * automatically added to the client when the plugin is loaded.
  36. */
  37. @Singleton
  38. public class PluginEventFormatManager implements SystemLifecycleComponent {
  39. private final EventBus eventbus;
  40. private final MultiEventFormatProvider multiEventFormatProvider;
  41. private final Map<PluginInfo, EventFormatProvider> providers = new HashMap<>();
  42. private final ColourManager colourManager;
  43. private final DisplayLocationManager displayLocationManager;
  44. @Inject
  45. public PluginEventFormatManager(
  46. final EventBus eventbus,
  47. final MultiEventFormatProvider multiEventFormatProvider,
  48. @GlobalConfig final ColourManager colourManager,
  49. final DisplayLocationManager displayLocationManager) {
  50. this.eventbus = eventbus;
  51. this.multiEventFormatProvider = multiEventFormatProvider;
  52. this.colourManager = colourManager;
  53. this.displayLocationManager = displayLocationManager;
  54. }
  55. @Override
  56. public void startUp() {
  57. eventbus.subscribe(this);
  58. }
  59. @Override
  60. public void shutDown() {
  61. eventbus.unsubscribe(this);
  62. }
  63. @Handler
  64. public void handlePluginLoaded(final PluginLoadedEvent event) {
  65. final Path path = event.getPlugin().getPath("/META-INF/format.yml");
  66. if (Files.exists(path)) {
  67. final YamlEventFormatProvider provider =
  68. new YamlEventFormatProvider(path, colourManager, displayLocationManager);
  69. provider.load();
  70. providers.put(event.getPlugin(), provider);
  71. multiEventFormatProvider.addProvider(provider);
  72. }
  73. }
  74. @Handler
  75. public void handlePluginUnloaded(final PluginUnloadedEvent event) {
  76. if (providers.containsKey(event.getPlugin())) {
  77. multiEventFormatProvider.removeProvider(providers.remove(event.getPlugin()));
  78. }
  79. }
  80. }