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

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