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.

NickColourYamlStore.java 3.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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.addons.nickcolours;
  18. import com.dmdirc.config.GlobalConfig;
  19. import com.dmdirc.ui.messages.ColourManager;
  20. import com.dmdirc.util.io.yaml.BaseYamlStore;
  21. import java.awt.Color;
  22. import java.nio.file.Path;
  23. import java.util.ArrayList;
  24. import java.util.Collection;
  25. import java.util.HashMap;
  26. import java.util.Map;
  27. import java.util.Optional;
  28. import javax.inject.Inject;
  29. import javax.inject.Singleton;
  30. import org.slf4j.Logger;
  31. import org.slf4j.LoggerFactory;
  32. import static com.dmdirc.util.io.yaml.YamlReaderUtils.asMap;
  33. import static com.dmdirc.util.io.yaml.YamlReaderUtils.requiredString;
  34. @Singleton
  35. public class NickColourYamlStore extends BaseYamlStore<NickColourEntry> {
  36. private static final Logger LOG = LoggerFactory.getLogger(NickColourYamlStore.class);
  37. private final ColourManager colourManager;
  38. @Inject
  39. public NickColourYamlStore(@GlobalConfig final ColourManager colourManager) {
  40. this.colourManager = colourManager;
  41. }
  42. public Map<String, Color> readNickColourEntries(final Path path) {
  43. final Map<String, Color> nickColours = new HashMap<>();
  44. final Collection<NickColourEntry> nickColourEntries = read(path);
  45. nickColourEntries.forEach(e -> nickColours.put(e.getNetwork() + ':' + e.getUser(), e.getColor()));
  46. return nickColours;
  47. }
  48. public void writeNickColourEntries(final Path path, final Map<String, Color> nickColours) {
  49. final Collection<NickColourEntry> nickColourEntries = new ArrayList<>();
  50. nickColours.forEach((description, colour) -> nickColourEntries.add(NickColourEntry
  51. .create(description.split(":")[0], description.split(":")[1], colour)));
  52. write(path, nickColourEntries);
  53. }
  54. @Override
  55. protected Optional<NickColourEntry> convertFromYaml(final Object object) {
  56. try {
  57. final Map<Object, Object> map = asMap(object);
  58. final String network = requiredString(map, "network");
  59. final String user = requiredString(map, "user");
  60. final String colour = requiredString(map, "colour");
  61. return Optional.of(NickColourEntry.create(network, user,
  62. NickColourUtils.getColorFromString(colourManager, colour)));
  63. } catch (IllegalArgumentException ex) {
  64. LOG.info("Unable to read profile", ex);
  65. return Optional.empty();
  66. }
  67. }
  68. @Override
  69. protected Object convertToYaml(final NickColourEntry object) {
  70. final Map<Object, Object> map = new HashMap<>();
  71. map.put("network", object.getNetwork());
  72. map.put("user", object.getUser());
  73. map.put("colour", NickColourUtils.getStringFromColor(object.getColor()));
  74. return map;
  75. }
  76. }