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.5KB

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