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 4.2KB

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