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 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.config;
  18. import com.dmdirc.commandline.CommandLineParser;
  19. import com.dmdirc.config.provider.AggregateConfigProvider;
  20. import com.dmdirc.config.provider.ConfigProvider;
  21. import com.dmdirc.interfaces.config.IdentityController;
  22. import com.dmdirc.interfaces.config.IdentityFactory;
  23. import com.dmdirc.logger.ErrorManager;
  24. import com.dmdirc.ui.WarningDialog;
  25. import com.dmdirc.util.ClientInfo;
  26. import java.awt.GraphicsEnvironment;
  27. import java.io.IOException;
  28. import java.nio.file.Files;
  29. import java.nio.file.Path;
  30. import java.text.SimpleDateFormat;
  31. import java.util.Date;
  32. import javax.inject.Singleton;
  33. import dagger.Module;
  34. import dagger.Provides;
  35. import static com.dmdirc.commandline.CommandLineOptionsModule.Directory;
  36. import static com.dmdirc.commandline.CommandLineOptionsModule.DirectoryType;
  37. /**
  38. * Dagger module for the configuration system.
  39. */
  40. @SuppressWarnings("TypeMayBeWeakened")
  41. @Module(library = true, complete = false)
  42. public class ConfigModule {
  43. @Provides
  44. @Singleton
  45. public IdentityManager getIdentityManager(
  46. @Directory(DirectoryType.BASE) final Path baseDirectory,
  47. @Directory(DirectoryType.IDENTITIES) final Path identitiesDirectory,
  48. @Directory(DirectoryType.ERRORS) final Path errorsDirectory,
  49. final CommandLineParser commandLineParser,
  50. final ClientInfo clientInfo,
  51. final ErrorManager errorManager) {
  52. final IdentityManager identityManager = new IdentityManager(baseDirectory,
  53. identitiesDirectory, clientInfo);
  54. errorManager.initialise(identityManager.getGlobalConfiguration());
  55. identityManager.loadVersionIdentity();
  56. try {
  57. identityManager.initialise();
  58. } catch (InvalidIdentityFileException ex) {
  59. handleInvalidConfigFile(identityManager, baseDirectory);
  60. }
  61. if (commandLineParser.getDisableReporting()) {
  62. identityManager.getUserSettings().setOption("temp", "noerrorreporting", true);
  63. }
  64. return identityManager;
  65. }
  66. @Provides
  67. public IdentityController getIdentityController(final IdentityManager manager) {
  68. return manager;
  69. }
  70. @Provides
  71. @GlobalConfig
  72. public AggregateConfigProvider getGlobalConfig(final IdentityController controller) {
  73. return controller.getGlobalConfiguration();
  74. }
  75. @Provides
  76. @UserConfig
  77. public ConfigProvider getUserConfig(final IdentityController controller) {
  78. return controller.getUserSettings();
  79. }
  80. @Provides
  81. @AddonConfig
  82. public ConfigProvider getAddonConfig(final IdentityController controller) {
  83. return controller.getAddonSettings();
  84. }
  85. @Provides
  86. public IdentityFactory getIdentityFactory(final IdentityManager identityManager) {
  87. return identityManager;
  88. }
  89. /**
  90. * Called when the global config cannot be loaded due to an error. This method informs the user
  91. * of the problem and installs a new default config file, backing up the old one.
  92. * @param identityManager The identity manager to re-initialise after installing defaults.
  93. * @param configdir The directory to extract default settings into.
  94. */
  95. private void handleInvalidConfigFile(final IdentityManager identityManager,
  96. final Path configdir) {
  97. final String date = new SimpleDateFormat("yyyyMMddkkmmss").format(new Date());
  98. final String message = "DMDirc has detected that your config file "
  99. + "has become corrupted.<br><br>DMDirc will now backup "
  100. + "your current config and try restarting with a default "
  101. + "config.<br><br>Your old config will be saved as:<br>"
  102. + "dmdirc.config." + date;
  103. if (!GraphicsEnvironment.isHeadless()) {
  104. new WarningDialog("Invalid Config File", message).displayBlocking();
  105. }
  106. // Let command-line users know what is happening.
  107. System.out.println(message.replace("<br>", "\n"));
  108. final Path configFile = configdir.resolve("dmdirc.config");
  109. final Path newConfigFile = configdir.resolve("dmdirc.config." + date);
  110. try {
  111. Files.move(configFile, newConfigFile);
  112. try {
  113. identityManager.initialise();
  114. } catch (InvalidIdentityFileException iife) {
  115. // This shouldn't happen!
  116. System.err.println("Unable to load global config");
  117. iife.printStackTrace();
  118. }
  119. } catch (IOException ex) {
  120. final String newMessage = "DMDirc was unable to rename the "
  121. + "global config file and is unable to fix this issue.";
  122. if (!GraphicsEnvironment.isHeadless()) {
  123. new WarningDialog("Invalid Config File", newMessage).displayBlocking();
  124. }
  125. System.out.println(newMessage.replace("<br>", "\n"));
  126. System.exit(1);
  127. }
  128. }
  129. }