Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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