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.

ConfigModule.java 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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.config;
  23. import com.dmdirc.commandline.CommandLineParser;
  24. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  25. import com.dmdirc.interfaces.config.ConfigProvider;
  26. import com.dmdirc.interfaces.config.IdentityController;
  27. import com.dmdirc.interfaces.config.IdentityFactory;
  28. import com.dmdirc.logger.ErrorManager;
  29. import com.dmdirc.ui.WarningDialog;
  30. import com.dmdirc.util.ClientInfo;
  31. import java.awt.GraphicsEnvironment;
  32. import java.io.IOException;
  33. import java.nio.file.Files;
  34. import java.nio.file.Path;
  35. import java.text.SimpleDateFormat;
  36. import java.util.Date;
  37. import javax.inject.Singleton;
  38. import dagger.Module;
  39. import dagger.Provides;
  40. import static com.dmdirc.commandline.CommandLineOptionsModule.Directory;
  41. import static com.dmdirc.commandline.CommandLineOptionsModule.DirectoryType;
  42. /**
  43. * Dagger module for the configuration system.
  44. */
  45. @SuppressWarnings("TypeMayBeWeakened")
  46. @Module(library = true, complete = false)
  47. public class ConfigModule {
  48. @Provides
  49. @Singleton
  50. public IdentityManager getIdentityManager(
  51. @Directory(DirectoryType.BASE) final Path baseDirectory,
  52. @Directory(DirectoryType.IDENTITIES) final Path identitiesDirectory,
  53. @Directory(DirectoryType.ERRORS) final Path errorsDirectory,
  54. final CommandLineParser commandLineParser,
  55. final ClientInfo clientInfo,
  56. final ErrorManager errorManager) {
  57. final IdentityManager identityManager = new IdentityManager(baseDirectory,
  58. identitiesDirectory, clientInfo);
  59. errorManager.initialise(identityManager.getGlobalConfiguration());
  60. identityManager.loadVersionIdentity();
  61. try {
  62. identityManager.initialise();
  63. } catch (InvalidIdentityFileException ex) {
  64. handleInvalidConfigFile(identityManager, baseDirectory);
  65. }
  66. if (commandLineParser.getDisableReporting()) {
  67. identityManager.getUserSettings().setOption("temp", "noerrorreporting", true);
  68. }
  69. return identityManager;
  70. }
  71. @Provides
  72. public IdentityController getIdentityController(final IdentityManager manager) {
  73. return manager;
  74. }
  75. @Provides
  76. @com.dmdirc.ClientModule.GlobalConfig
  77. public AggregateConfigProvider getOldGlobalConfig(final IdentityController controller) {
  78. return controller.getGlobalConfiguration();
  79. }
  80. @Provides
  81. @GlobalConfig
  82. public AggregateConfigProvider getGlobalConfig(final IdentityController controller) {
  83. return controller.getGlobalConfiguration();
  84. }
  85. @Provides
  86. @com.dmdirc.ClientModule.UserConfig
  87. public ConfigProvider getOldUserConfig(final IdentityController controller) {
  88. return controller.getUserSettings();
  89. }
  90. @Provides
  91. @UserConfig
  92. public ConfigProvider getUserConfig(final IdentityController controller) {
  93. return controller.getUserSettings();
  94. }
  95. @Provides
  96. @com.dmdirc.ClientModule.AddonConfig
  97. public ConfigProvider getOldAddonConfig(final IdentityController controller) {
  98. return controller.getAddonSettings();
  99. }
  100. @Provides
  101. @AddonConfig
  102. public ConfigProvider getAddonConfig(final IdentityController controller) {
  103. return controller.getAddonSettings();
  104. }
  105. @Provides
  106. public IdentityFactory getIdentityFactory(final IdentityManager identityManager) {
  107. return identityManager;
  108. }
  109. /**
  110. * Called when the global config cannot be loaded due to an error. This method informs the user
  111. * of the problem and installs a new default config file, backing up the old one.
  112. * @param identityManager The identity manager to re-initialise after installing defaults.
  113. * @param configdir The directory to extract default settings into.
  114. */
  115. private void handleInvalidConfigFile(final IdentityManager identityManager,
  116. final Path configdir) {
  117. final String date = new SimpleDateFormat("yyyyMMddkkmmss").format(new Date());
  118. final String message = "DMDirc has detected that your config file "
  119. + "has become corrupted.<br><br>DMDirc will now backup "
  120. + "your current config and try restarting with a default "
  121. + "config.<br><br>Your old config will be saved as:<br>"
  122. + "dmdirc.config." + date;
  123. if (!GraphicsEnvironment.isHeadless()) {
  124. new WarningDialog("Invalid Config File", message).displayBlocking();
  125. }
  126. // Let command-line users know what is happening.
  127. System.out.println(message.replace("<br>", "\n"));
  128. final Path configFile = configdir.resolve("dmdirc.config");
  129. final Path newConfigFile = configdir.resolve("dmdirc.config." + date);
  130. try {
  131. Files.move(configFile, newConfigFile);
  132. try {
  133. identityManager.initialise();
  134. } catch (InvalidIdentityFileException iife) {
  135. // This shouldn't happen!
  136. System.err.println("Unable to load global config");
  137. iife.printStackTrace();
  138. }
  139. } catch (IOException ex) {
  140. final String newMessage = "DMDirc was unable to rename the "
  141. + "global config file and is unable to fix this issue.";
  142. if (!GraphicsEnvironment.isHeadless()) {
  143. new WarningDialog("Invalid Config File", newMessage).displayBlocking();
  144. }
  145. System.out.println(newMessage.replace("<br>", "\n"));
  146. System.exit(1);
  147. }
  148. }
  149. }