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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. @GlobalConfig
  77. public AggregateConfigProvider getGlobalConfig(final IdentityController controller) {
  78. return controller.getGlobalConfiguration();
  79. }
  80. @Provides
  81. @UserConfig
  82. public ConfigProvider getUserConfig(final IdentityController controller) {
  83. return controller.getUserSettings();
  84. }
  85. @Provides
  86. @com.dmdirc.ClientModule.AddonConfig
  87. public ConfigProvider getOldAddonConfig(final IdentityController controller) {
  88. return controller.getAddonSettings();
  89. }
  90. @Provides
  91. @AddonConfig
  92. public ConfigProvider getAddonConfig(final IdentityController controller) {
  93. return controller.getAddonSettings();
  94. }
  95. @Provides
  96. public IdentityFactory getIdentityFactory(final IdentityManager identityManager) {
  97. return identityManager;
  98. }
  99. /**
  100. * Called when the global config cannot be loaded due to an error. This method informs the user
  101. * of the problem and installs a new default config file, backing up the old one.
  102. * @param identityManager The identity manager to re-initialise after installing defaults.
  103. * @param configdir The directory to extract default settings into.
  104. */
  105. private void handleInvalidConfigFile(final IdentityManager identityManager,
  106. final Path configdir) {
  107. final String date = new SimpleDateFormat("yyyyMMddkkmmss").format(new Date());
  108. final String message = "DMDirc has detected that your config file "
  109. + "has become corrupted.<br><br>DMDirc will now backup "
  110. + "your current config and try restarting with a default "
  111. + "config.<br><br>Your old config will be saved as:<br>"
  112. + "dmdirc.config." + date;
  113. if (!GraphicsEnvironment.isHeadless()) {
  114. new WarningDialog("Invalid Config File", message).displayBlocking();
  115. }
  116. // Let command-line users know what is happening.
  117. System.out.println(message.replace("<br>", "\n"));
  118. final Path configFile = configdir.resolve("dmdirc.config");
  119. final Path newConfigFile = configdir.resolve("dmdirc.config." + date);
  120. try {
  121. Files.move(configFile, newConfigFile);
  122. try {
  123. identityManager.initialise();
  124. } catch (InvalidIdentityFileException iife) {
  125. // This shouldn't happen!
  126. System.err.println("Unable to load global config");
  127. iife.printStackTrace();
  128. }
  129. } catch (IOException ex) {
  130. final String newMessage = "DMDirc was unable to rename the "
  131. + "global config file and is unable to fix this issue.";
  132. if (!GraphicsEnvironment.isHeadless()) {
  133. new WarningDialog("Invalid Config File", newMessage).displayBlocking();
  134. }
  135. System.out.println(newMessage.replace("<br>", "\n"));
  136. System.exit(1);
  137. }
  138. }
  139. }