Переглянути джерело

Split config stuff into a separate dagger module

Change-Id: Icfe7aacc8a1a0e13cbf492ba49e95030b4d53918
Reviewed-on: http://gerrit.dmdirc.com/3876
Reviewed-by: Greg Holmes <greg@dmdirc.com>
Automatic-Compile: DMDirc Build Manager
pull/1/head
Chris Smith 9 роки тому
джерело
коміт
59182e752f
2 змінених файлів з 170 додано та 119 видалено
  1. 10
    119
      src/com/dmdirc/ClientModule.java
  2. 160
    0
      src/com/dmdirc/config/ConfigModule.java

+ 10
- 119
src/com/dmdirc/ClientModule.java Переглянути файл

@@ -34,20 +34,14 @@ import com.dmdirc.commandparser.CommandManager;
34 34
 import com.dmdirc.commandparser.aliases.AliasesModule;
35 35
 import com.dmdirc.commandparser.auto.AutoCommandModule;
36 36
 import com.dmdirc.commandparser.commands.CommandModule;
37
-import com.dmdirc.config.IdentityManager;
38
-import com.dmdirc.config.InvalidIdentityFileException;
37
+import com.dmdirc.config.ConfigModule;
39 38
 import com.dmdirc.interfaces.ActionController;
40 39
 import com.dmdirc.interfaces.CommandController;
40
+import com.dmdirc.interfaces.ConnectionFactory;
41 41
 import com.dmdirc.interfaces.ConnectionManager;
42 42
 import com.dmdirc.interfaces.LifecycleController;
43
-import com.dmdirc.interfaces.ConnectionFactory;
44 43
 import com.dmdirc.interfaces.config.AggregateConfigProvider;
45
-import com.dmdirc.interfaces.config.ConfigProvider;
46 44
 import com.dmdirc.interfaces.config.IdentityController;
47
-import com.dmdirc.interfaces.config.IdentityFactory;
48
-import com.dmdirc.logger.ErrorLevel;
49
-import com.dmdirc.logger.ErrorManager;
50
-import com.dmdirc.logger.Logger;
51 45
 import com.dmdirc.messages.MessagesModule;
52 46
 import com.dmdirc.plugins.CorePluginExtractor;
53 47
 import com.dmdirc.plugins.CorePluginHelper;
@@ -57,7 +51,6 @@ import com.dmdirc.plugins.PluginManager;
57 51
 import com.dmdirc.plugins.ServiceLocator;
58 52
 import com.dmdirc.plugins.ServiceManager;
59 53
 import com.dmdirc.ui.IconManager;
60
-import com.dmdirc.ui.WarningDialog;
61 54
 import com.dmdirc.ui.messages.ColourManager;
62 55
 import com.dmdirc.ui.themes.ThemeManager;
63 56
 import com.dmdirc.updater.UpdaterModule;
@@ -65,10 +58,6 @@ import com.dmdirc.updater.manager.UpdateManager;
65 58
 import com.dmdirc.util.URLBuilder;
66 59
 import com.dmdirc.util.io.Downloader;
67 60
 
68
-import java.awt.GraphicsEnvironment;
69
-import java.io.File;
70
-import java.text.SimpleDateFormat;
71
-import java.util.Date;
72 61
 import java.util.Set;
73 62
 
74 63
 import javax.inject.Provider;
@@ -86,12 +75,13 @@ import net.engio.mbassy.bus.config.BusConfiguration;
86 75
 @Module(
87 76
         injects = {Main.class, CommandLineParser.class},
88 77
         includes = {
89
-            AliasesModule.class,
90
-            AutoCommandModule.class,
91
-            CommandLineOptionsModule.class,
92
-            CommandModule.class,
93
-            MessagesModule.class,
94
-            UpdaterModule.class
78
+                AliasesModule.class,
79
+                AutoCommandModule.class,
80
+                CommandLineOptionsModule.class,
81
+                CommandModule.class,
82
+                ConfigModule.class,
83
+                MessagesModule.class,
84
+                UpdaterModule.class
95 85
         },
96 86
         library = true)
97 87
 public class ClientModule {
@@ -115,6 +105,7 @@ public class ClientModule {
115 105
     private ObjectGraph objectGraph;
116 106
 
117 107
     @Provides
108
+    @SuppressWarnings("TypeMayBeWeakened")
118 109
     public ConnectionManager getConnectionManager(final ServerManager serverManager) {
119 110
         return serverManager;
120 111
     }
@@ -125,44 +116,6 @@ public class ClientModule {
125 116
         return new DMDircMBassador(BusConfiguration.Default());
126 117
     }
127 118
 
128
-    @Provides
129
-    @Singleton
130
-    public IdentityManager getIdentityManager(
131
-            @Directory(DirectoryType.BASE) final String baseDirectory,
132
-            @Directory(DirectoryType.IDENTITIES) final String identitiesDirectory,
133
-            @Directory(DirectoryType.ERRORS) final String errorsDirectory,
134
-            final CommandLineParser commandLineParser,
135
-            final DMDircMBassador eventBus) {
136
-        final IdentityManager identityManager = new IdentityManager(baseDirectory,
137
-                identitiesDirectory, eventBus);
138
-        ErrorManager.getErrorManager()
139
-                .initialise(identityManager.getGlobalConfiguration(), errorsDirectory, eventBus);
140
-        identityManager.loadVersionIdentity();
141
-
142
-        try {
143
-            identityManager.initialise();
144
-        } catch (InvalidIdentityFileException ex) {
145
-            handleInvalidConfigFile(identityManager, baseDirectory);
146
-        }
147
-
148
-        if (commandLineParser.getDisableReporting()) {
149
-            identityManager.getUserSettings().setOption("temp", "noerrorreporting", true);
150
-        }
151
-
152
-        return identityManager;
153
-    }
154
-
155
-    @Provides
156
-    public IdentityController getIdentityController(final IdentityManager manager) {
157
-        return manager;
158
-    }
159
-
160
-    @Provides
161
-    @GlobalConfig
162
-    public AggregateConfigProvider getGlobalConfig(final IdentityController controller) {
163
-        return controller.getGlobalConfiguration();
164
-    }
165
-
166 119
     @Provides
167 120
     @GlobalConfig
168 121
     @Singleton
@@ -172,18 +125,6 @@ public class ClientModule {
172 125
         return new IconManager(globalConfig, urlBuilder);
173 126
     }
174 127
 
175
-    @Provides
176
-    @UserConfig
177
-    public ConfigProvider getUserConfig(final IdentityController controller) {
178
-        return controller.getUserSettings();
179
-    }
180
-
181
-    @Provides
182
-    @AddonConfig
183
-    public ConfigProvider getAddonConfig(final IdentityController controller) {
184
-        return controller.getAddonSettings();
185
-    }
186
-
187 128
     @Provides
188 129
     @Singleton
189 130
     public ActionManager getActionManager(
@@ -285,11 +226,6 @@ public class ClientModule {
285 226
         return connectionManager;
286 227
     }
287 228
 
288
-    @Provides
289
-    public IdentityFactory getIdentityFactory(final IdentityManager identityManager) {
290
-        return identityManager;
291
-    }
292
-
293 229
     @Provides
294 230
     public ServiceLocator getServiceLocator(final LegacyServiceLocator locator) {
295 231
         return locator;
@@ -315,49 +251,4 @@ public class ClientModule {
315 251
         return objectGraph;
316 252
     }
317 253
 
318
-    /**
319
-     * Called when the global config cannot be loaded due to an error. This method informs the user
320
-     * of the problem and installs a new default config file, backing up the old one.
321
-     *
322
-     * @param identityManager The identity manager to re-initialise after installing defaults.
323
-     * @param configdir       The directory to extract default settings into.
324
-     */
325
-    private void handleInvalidConfigFile(final IdentityManager identityManager,
326
-            final String configdir) {
327
-        final String date = new SimpleDateFormat("yyyyMMddkkmmss").format(new Date());
328
-
329
-        final String message = "DMDirc has detected that your config file "
330
-                + "has become corrupted.<br><br>DMDirc will now backup "
331
-                + "your current config and try restarting with a default "
332
-                + "config.<br><br>Your old config will be saved as:<br>"
333
-                + "dmdirc.config." + date;
334
-
335
-        if (!GraphicsEnvironment.isHeadless()) {
336
-            new WarningDialog("Invalid Config File", message).displayBlocking();
337
-        }
338
-
339
-        // Let command-line users know what is happening.
340
-        System.out.println(message.replace("<br>", "\n"));
341
-
342
-        final File configFile = new File(configdir + "dmdirc.config");
343
-        final File newConfigFile = new File(configdir + "dmdirc.config." + date);
344
-
345
-        if (configFile.renameTo(newConfigFile)) {
346
-            try {
347
-                identityManager.initialise();
348
-            } catch (InvalidIdentityFileException iife) {
349
-                // This shouldn't happen!
350
-                Logger.appError(ErrorLevel.FATAL, "Unable to load global config", iife);
351
-            }
352
-        } else {
353
-            final String newMessage = "DMDirc was unable to rename the "
354
-                    + "global config file and is unable to fix this issue.";
355
-            if (!GraphicsEnvironment.isHeadless()) {
356
-                new WarningDialog("Invalid Config File", newMessage).displayBlocking();
357
-            }
358
-            System.out.println(newMessage.replace("<br>", "\n"));
359
-            System.exit(1);
360
-        }
361
-    }
362
-
363 254
 }

+ 160
- 0
src/com/dmdirc/config/ConfigModule.java Переглянути файл

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

Завантаження…
Відмінити
Зберегти