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

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