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.3KB

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