|
@@ -24,11 +24,22 @@ package com.dmdirc;
|
24
|
24
|
|
25
|
25
|
import com.dmdirc.actions.ActionManager;
|
26
|
26
|
import com.dmdirc.commandline.CommandLineOptionsModule;
|
|
27
|
+import com.dmdirc.commandline.CommandLineOptionsModule.Directory;
|
|
28
|
+import com.dmdirc.commandline.CommandLineOptionsModule.DirectoryType;
|
27
|
29
|
import com.dmdirc.config.IdentityManager;
|
|
30
|
+import com.dmdirc.config.InvalidIdentityFileException;
|
28
|
31
|
import com.dmdirc.interfaces.ActionController;
|
29
|
32
|
import com.dmdirc.interfaces.IdentityController;
|
30
|
33
|
import com.dmdirc.interfaces.LifecycleController;
|
|
34
|
+import com.dmdirc.logger.ErrorLevel;
|
|
35
|
+import com.dmdirc.logger.Logger;
|
|
36
|
+import com.dmdirc.plugins.PluginManager;
|
|
37
|
+import com.dmdirc.ui.WarningDialog;
|
31
|
38
|
|
|
39
|
+import java.awt.GraphicsEnvironment;
|
|
40
|
+import java.io.File;
|
|
41
|
+import java.text.SimpleDateFormat;
|
|
42
|
+import java.util.Date;
|
32
|
43
|
import javax.inject.Singleton;
|
33
|
44
|
|
34
|
45
|
import dagger.Module;
|
|
@@ -43,14 +54,23 @@ public class ClientModule {
|
43
|
54
|
/**
|
44
|
55
|
* Provides an identity manager for the client.
|
45
|
56
|
*
|
46
|
|
- * @return An unitialised {@link IdentityManager}.
|
|
57
|
+ * @param directory The directory to load settings from.
|
|
58
|
+ * @return An initialised {@link IdentityManager}.
|
47
|
59
|
*/
|
48
|
60
|
@Provides
|
49
|
61
|
@Singleton
|
50
|
|
- public IdentityManager getIdentityManager() {
|
51
|
|
- final IdentityManager identityManager = new IdentityManager();
|
|
62
|
+ public IdentityManager getIdentityManager(
|
|
63
|
+ @Directory(DirectoryType.BASE) final String directory) {
|
|
64
|
+ final IdentityManager identityManager = new IdentityManager(directory);
|
52
|
65
|
IdentityManager.setIdentityManager(identityManager);
|
53
|
66
|
identityManager.loadVersionIdentity();
|
|
67
|
+
|
|
68
|
+ try {
|
|
69
|
+ identityManager.initialise();
|
|
70
|
+ } catch (InvalidIdentityFileException ex) {
|
|
71
|
+ handleInvalidConfigFile(identityManager, directory);
|
|
72
|
+ }
|
|
73
|
+
|
54
|
74
|
return identityManager;
|
55
|
75
|
}
|
56
|
76
|
|
|
@@ -68,11 +88,12 @@ public class ClientModule {
|
68
|
88
|
/**
|
69
|
89
|
* Provides a parser factory.
|
70
|
90
|
*
|
|
91
|
+ * @param pluginManager The plugin manager to use to find parsers.
|
71
|
92
|
* @return A parser factory for use in the client.
|
72
|
93
|
*/
|
73
|
94
|
@Provides
|
74
|
|
- public ParserFactory getParserFactory() {
|
75
|
|
- return new ParserFactory(Main.mainInstance.getPluginManager());
|
|
95
|
+ public ParserFactory getParserFactory(final PluginManager pluginManager) {
|
|
96
|
+ return new ParserFactory(pluginManager);
|
76
|
97
|
}
|
77
|
98
|
|
78
|
99
|
/**
|
|
@@ -112,4 +133,49 @@ public class ClientModule {
|
112
|
133
|
return controller;
|
113
|
134
|
}
|
114
|
135
|
|
|
136
|
+ /**
|
|
137
|
+ * Called when the global config cannot be loaded due to an error. This
|
|
138
|
+ * method informs the user of the problem and installs a new default config
|
|
139
|
+ * file, backing up the old one.
|
|
140
|
+ *
|
|
141
|
+ * @param identityManager The identity manager to re-initialise after installing defaults.
|
|
142
|
+ * @param configdir The directory to extract default settings into.
|
|
143
|
+ */
|
|
144
|
+ private void handleInvalidConfigFile(final IdentityManager identityManager, final String configdir) {
|
|
145
|
+ final String date = new SimpleDateFormat("yyyyMMddkkmmss").format(new Date());
|
|
146
|
+
|
|
147
|
+ final String message = "DMDirc has detected that your config file "
|
|
148
|
+ + "has become corrupted.<br><br>DMDirc will now backup "
|
|
149
|
+ + "your current config and try restarting with a default "
|
|
150
|
+ + "config.<br><br>Your old config will be saved as:<br>"
|
|
151
|
+ + "dmdirc.config." + date;
|
|
152
|
+
|
|
153
|
+ if (!GraphicsEnvironment.isHeadless()) {
|
|
154
|
+ new WarningDialog("Invalid Config File", message).displayBlocking();
|
|
155
|
+ }
|
|
156
|
+
|
|
157
|
+ // Let command-line users know what is happening.
|
|
158
|
+ System.out.println(message.replace("<br>", "\n"));
|
|
159
|
+
|
|
160
|
+ final File configFile = new File(configdir + "dmdirc.config");
|
|
161
|
+ final File newConfigFile = new File(configdir + "dmdirc.config." + date);
|
|
162
|
+
|
|
163
|
+ if (configFile.renameTo(newConfigFile)) {
|
|
164
|
+ try {
|
|
165
|
+ identityManager.initialise();
|
|
166
|
+ } catch (InvalidIdentityFileException iife) {
|
|
167
|
+ // This shouldn't happen!
|
|
168
|
+ Logger.appError(ErrorLevel.FATAL, "Unable to load global config", iife);
|
|
169
|
+ }
|
|
170
|
+ } else {
|
|
171
|
+ final String newMessage = "DMDirc was unable to rename the "
|
|
172
|
+ + "global config file and is unable to fix this issue.";
|
|
173
|
+ if (!GraphicsEnvironment.isHeadless()) {
|
|
174
|
+ new WarningDialog("Invalid Config File", newMessage).displayBlocking();
|
|
175
|
+ }
|
|
176
|
+ System.out.println(newMessage.replace("<br>", "\n"));
|
|
177
|
+ System.exit(1);
|
|
178
|
+ }
|
|
179
|
+ }
|
|
180
|
+
|
115
|
181
|
}
|