Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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.ui.themes;
  18. import com.dmdirc.interfaces.config.IdentityController;
  19. import java.io.File;
  20. import java.util.HashMap;
  21. import java.util.List;
  22. import java.util.Map;
  23. import org.slf4j.Logger;
  24. import org.slf4j.LoggerFactory;
  25. import static com.dmdirc.util.LogUtils.USER_ERROR;
  26. /**
  27. * Manages available themes.
  28. */
  29. public class ThemeManager {
  30. private static final Logger LOG = LoggerFactory.getLogger(ThemeManager.class);
  31. /** The identity controller to read settings from. */
  32. private final IdentityController identityController;
  33. /** The directory to look for themes in. */
  34. private final String themeDirectory;
  35. /** Available themes. */
  36. private final Map<String, Theme> themes = new HashMap<>();
  37. /**
  38. * Creates a new instance of the {@link ThemeManager}.
  39. *
  40. * @param identityController The identity controller to read settings from.
  41. * @param themesDirectory The directory to load themes from.
  42. */
  43. public ThemeManager(
  44. final IdentityController identityController,
  45. final String themesDirectory) {
  46. this.identityController = identityController;
  47. this.themeDirectory = themesDirectory;
  48. identityController.getGlobalConfiguration().addChangeListener("themes", "enabled",
  49. (domain, key) -> refreshAndLoadThemes());
  50. }
  51. /**
  52. * Scans for available themes and loads any themes that the user has enabled.
  53. */
  54. public void refreshAndLoadThemes() {
  55. final File dir = new File(themeDirectory);
  56. if (!dir.exists() && !dir.mkdirs()) {
  57. LOG.error(USER_ERROR, "Could not create themes directory");
  58. }
  59. if (dir.listFiles() == null) {
  60. LOG.error(USER_ERROR, "Unable to load themes");
  61. return;
  62. }
  63. final List<String> enabled = identityController.getGlobalConfiguration()
  64. .getOptionList("themes", "enabled");
  65. synchronized (themes) {
  66. for (File file : dir.listFiles()) {
  67. if (file.isDirectory()) {
  68. continue;
  69. }
  70. loadTheme(file, enabled.contains(file.getName()));
  71. }
  72. }
  73. }
  74. /**
  75. * Attempts to load the theme from the specified file. If the enabled argument is true, the
  76. * theme will be applied automatically. If it has been previously applied and is no longer
  77. * enabled, it will be unapplied.
  78. *
  79. * @param file The file pointing to the theme to be loaded
  80. * @param enabled Whether this theme is enabled or not
  81. */
  82. private void loadTheme(final File file, final boolean enabled) {
  83. final Theme theme;
  84. if (themes.containsKey(file.getName())) {
  85. theme = themes.get(file.getName());
  86. } else {
  87. theme = new Theme(identityController, file);
  88. if (theme.isValidTheme()) {
  89. themes.put(file.getName(), theme);
  90. } else {
  91. return;
  92. }
  93. }
  94. if (enabled && !theme.isEnabled()) {
  95. theme.applyTheme();
  96. } else if (theme.isEnabled() && !enabled) {
  97. theme.removeTheme();
  98. }
  99. }
  100. /**
  101. * Retrieves a list of available themes.
  102. *
  103. * @return A list of available themes
  104. */
  105. public Map<String, Theme> getAllThemes() {
  106. refreshAndLoadThemes();
  107. synchronized (themes) {
  108. return new HashMap<>(themes);
  109. }
  110. }
  111. /**
  112. * Retrieves the directory used for storing themes.
  113. *
  114. * @return The directory used for storing themes
  115. */
  116. public String getDirectory() {
  117. return themeDirectory;
  118. }
  119. /**
  120. * Updates the theme auto load list with the state of the specified theme.
  121. *
  122. * @param theme Theme to update auto load
  123. */
  124. public void synchroniseAutoLoad(final Theme theme) {
  125. final List<String> enabled = identityController.getGlobalConfiguration()
  126. .getOptionList("themes", "enabled", true);
  127. if (theme.isEnabled()) {
  128. enabled.add(theme.getFileName());
  129. } else {
  130. enabled.remove(theme.getFileName());
  131. }
  132. identityController.getUserSettings()
  133. .setOption("themes", "enabled", enabled);
  134. }
  135. }