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.

ThemeManager.java 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Copyright (c) 2006-2011 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.ui.themes;
  23. import com.dmdirc.Main;
  24. import com.dmdirc.config.IdentityManager;
  25. import com.dmdirc.interfaces.ConfigChangeListener;
  26. import com.dmdirc.logger.ErrorLevel;
  27. import com.dmdirc.logger.Logger;
  28. import java.io.File;
  29. import java.util.HashMap;
  30. import java.util.List;
  31. import java.util.Map;
  32. /**
  33. * Manages available themes.
  34. */
  35. public final class ThemeManager {
  36. /** The directory to look for themes in. */
  37. private static final String THEME_DIR = Main.getConfigDir() + "themes/";
  38. /** Available themes. */
  39. private static final Map<String, Theme> THEMES = new HashMap<String, Theme>();
  40. static {
  41. IdentityManager.getIdentityManager().getGlobalConfiguration()
  42. .addChangeListener("themes", "enabled",
  43. new ConfigChangeListener() {
  44. /** {@inheritDoc} */
  45. @Override
  46. public void configChanged(final String domain, final String key) {
  47. loadThemes();
  48. }
  49. });
  50. }
  51. /**
  52. * Creates a new instance of theme manager.
  53. */
  54. private ThemeManager() {
  55. // Do nothing
  56. }
  57. /**
  58. * Scans for available themes and loads any themes that the user has enabled.
  59. */
  60. public static void loadThemes() {
  61. final File dir = new File(THEME_DIR);
  62. if (!dir.exists() && !dir.mkdirs()) {
  63. Logger.userError(ErrorLevel.HIGH, "Could not create themes directory");
  64. }
  65. if (dir.listFiles() == null) {
  66. Logger.userError(ErrorLevel.MEDIUM, "Unable to load themes");
  67. return;
  68. }
  69. final List<String> enabled = IdentityManager.getIdentityManager().getGlobalConfiguration()
  70. .getOptionList("themes", "enabled");
  71. synchronized (THEMES) {
  72. for (File file : dir.listFiles()) {
  73. if (file.isDirectory()) {
  74. continue;
  75. }
  76. loadTheme(file, enabled.contains(file.getName()));
  77. }
  78. }
  79. }
  80. /**
  81. * Attempts to load the theme from the specified file. If the enabled
  82. * argument is true, the theme will be applied automatically. If it
  83. * has been previously applied and is no longer enabled, it will be
  84. * unapplied.
  85. *
  86. * @param file The file pointing to the theme to be loaded
  87. * @param enabled Whether this theme is enabled or not
  88. */
  89. private static void loadTheme(final File file, final boolean enabled) {
  90. Theme theme;
  91. if (THEMES.containsKey(file.getName())) {
  92. theme = THEMES.get(file.getName());
  93. } else {
  94. theme = new Theme(file);
  95. if (theme.isValidTheme()) {
  96. THEMES.put(file.getName(), theme);
  97. } else {
  98. return;
  99. }
  100. }
  101. if (enabled && !theme.isEnabled()) {
  102. theme.applyTheme();
  103. } else if (theme.isEnabled() && !enabled) {
  104. theme.removeTheme();
  105. }
  106. }
  107. /**
  108. * Retrieves a list of available themes.
  109. *
  110. * @return A list of available themes
  111. */
  112. public static Map<String, Theme> getAvailableThemes() {
  113. loadThemes();
  114. synchronized (THEMES) {
  115. return new HashMap<String, Theme>(THEMES);
  116. }
  117. }
  118. /**
  119. * Retrieves the directory used for storing themes.
  120. *
  121. * @return The directory used for storing themes
  122. */
  123. public static String getThemeDirectory() {
  124. return THEME_DIR;
  125. }
  126. /**
  127. * Updates the theme auto load list with the state of the specified theme.
  128. *
  129. * @param theme Theme to update auto load
  130. */
  131. public static void updateAutoLoad(final Theme theme) {
  132. final List<String> enabled = IdentityManager.getConfigIdentity()
  133. .getOptionList("themes", "enabled", true);
  134. if (theme.isEnabled()) {
  135. enabled.add(theme.getFileName());
  136. } else {
  137. enabled.remove(theme.getFileName());
  138. }
  139. IdentityManager.getConfigIdentity().setOption("themes", "enabled",
  140. enabled);
  141. }
  142. }