Преглед изворни кода

Load defaults from the jar.

Closes #156
pull/157/head
Chris Smith пре 9 година
родитељ
комит
be966bbf16
2 измењених фајлова са 32 додато и 142 уклоњено
  1. 32
    84
      src/com/dmdirc/config/IdentityManager.java
  2. 0
    58
      test/com/dmdirc/config/IdentityManagerTest.java

+ 32
- 84
src/com/dmdirc/config/IdentityManager.java Прегледај датотеку

@@ -24,6 +24,7 @@ package com.dmdirc.config;
24 24
 
25 25
 import com.dmdirc.DMDircMBassador;
26 26
 import com.dmdirc.Precondition;
27
+import com.dmdirc.events.AppErrorEvent;
27 28
 import com.dmdirc.events.UserErrorEvent;
28 29
 import com.dmdirc.interfaces.config.AggregateConfigProvider;
29 30
 import com.dmdirc.interfaces.config.ConfigProvider;
@@ -32,7 +33,6 @@ import com.dmdirc.interfaces.config.ConfigProviderMigrator;
32 33
 import com.dmdirc.interfaces.config.IdentityController;
33 34
 import com.dmdirc.interfaces.config.IdentityFactory;
34 35
 import com.dmdirc.logger.ErrorLevel;
35
-import com.dmdirc.updater.Version;
36 36
 import com.dmdirc.util.collections.MapList;
37 37
 import com.dmdirc.util.collections.WeakMapList;
38 38
 import com.dmdirc.util.io.ConfigFile;
@@ -40,6 +40,7 @@ import com.dmdirc.util.io.FileUtils;
40 40
 import com.dmdirc.util.io.InvalidConfigFileException;
41 41
 
42 42
 import java.io.IOException;
43
+import java.net.URISyntaxException;
43 44
 import java.nio.file.DirectoryStream;
44 45
 import java.nio.file.Files;
45 46
 import java.nio.file.Path;
@@ -138,98 +139,45 @@ public class IdentityManager implements IdentityFactory, IdentityController {
138 139
 
139 140
         addonConfig = new ConfigFileBackedConfigProvider(this, addonConfigFile, target);
140 141
         addConfigProvider(addonConfig);
141
-
142
-        if (!getGlobalConfiguration().hasOptionString("identity", "defaultsversion")) {
143
-            eventBus.publishAsync(new UserErrorEvent(ErrorLevel.FATAL, null,
144
-                    "Default settings could not be loaded", ""));
145
-        }
146 142
     }
147 143
 
148 144
     /** Loads the default (built in) identities. */
149 145
     private void loadDefaults() {
150
-        final String[] targets = {"default", "modealiases"};
151
-
152
-        for (String target : targets) {
153
-            final Path file = identitiesDirectory.resolve(target);
154
-
155
-            if (Files.exists(file) && !Files.isDirectory(file)) {
156
-                boolean success = false;
157
-                for (int i = 0; i < 10 && !success; i++) {
158
-                    try {
159
-                        final String suffix = ".old" + (i > 0 ? "-" + i : "");
160
-                        Files.move(file, identitiesDirectory.resolve(target + suffix));
161
-                        success = true;
162
-                    } catch (IOException ex) {
163
-                        success = false;
164
-                    }
165
-                }
166
-
167
-                if (!success) {
168
-                    eventBus.publishAsync(new UserErrorEvent(ErrorLevel.HIGH, null,
169
-                            "Unable to create directory for default settings folder ("
170
-                            + target + ')',
171
-                            "A file with that name already exists, and couldn't be renamed."
172
-                            + " Rename or delete " + file));
173
-                    continue;
174
-                }
175
-            }
146
+        try {
147
+            loadIdentity(FileUtils.getPathForResource(getClass().getResource(
148
+                    "defaults/default/defaults")));
149
+            loadIdentity(FileUtils.getPathForResource(getClass().getResource(
150
+                    "defaults/default/formatter")));
151
+        } catch (URISyntaxException ex) {
152
+            eventBus.publishAsync(new AppErrorEvent(ErrorLevel.FATAL, ex,
153
+                    "Unable to load settings", ""));
154
+        }
176 155
 
177
-            if (!Files.exists(file)) {
178
-                try {
179
-                    Files.createDirectories(file);
180
-                } catch (IOException ex) {
181
-                    eventBus.publishAsync(new UserErrorEvent(ErrorLevel.FATAL, null,
182
-                            "Unable to create required directory '" + file + "'. Please check " +
183
-                                    "file permissions or specify a different configuration " +
184
-                                    "directory.", ""));
185
-                    return;
186
-                }
187
-            }
156
+        final Path file = identitiesDirectory.resolve("modealiases");
188 157
 
189
-            try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(file)) {
190
-                if (!directoryStream.iterator().hasNext()) {
191
-                    extractIdentities(target);
192
-                }
158
+        if (!Files.exists(file)) {
159
+            try {
160
+                Files.createDirectories(file);
193 161
             } catch (IOException ex) {
194
-                eventBus.publishAsync(new UserErrorEvent(ErrorLevel.FATAL, null,
195
-                        "Unable to iterate required directory '" + file + "'. Please check " +
196
-                                "file permissions or specify a different configuration " +
197
-                                "directory.", ""));
198
-                return;
162
+                eventBus.publishAsync(new UserErrorEvent(ErrorLevel.LOW, ex,
163
+                        "Unable to create modealiases directory", "Please check file permissions " +
164
+                        "for " + file));
199 165
             }
200
-
201
-            loadUser(file);
202 166
         }
203 167
 
204
-        extractFormatters();
205
-
206
-        // If the bundled defaults are newer than the ones the user is
207
-        // currently using, extract them.
208
-        if (getGlobalConfiguration().hasOptionString("identity", "defaultsversion")
209
-                && getGlobalConfiguration().hasOptionString("updater", "bundleddefaultsversion")) {
210
-            final Version installedVersion = new Version(getGlobalConfiguration()
211
-                    .getOption("identity", "defaultsversion"));
212
-            final Version bundledVersion = new Version(getGlobalConfiguration()
213
-                    .getOption("updater", "bundleddefaultsversion"));
214
-
215
-            if (bundledVersion.compareTo(installedVersion) > 0) {
216
-                extractIdentities("default");
217
-                loadUser(identitiesDirectory.resolve("default"));
168
+        try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(file)) {
169
+            if (!directoryStream.iterator().hasNext()) {
170
+                extractIdentities("modealiases");
218 171
             }
219
-        }
220
-    }
221
-
222
-    /**
223
-     * Extracts the bundled formatters to the user's identity folder.
224
-     */
225
-    private void extractFormatters() {
226
-        try {
227
-            FileUtils.copyResources(getClass().getResource("defaults/default/formatter"),
228
-                    identitiesDirectory.resolve("default"));
229 172
         } catch (IOException ex) {
230
-            eventBus.publishAsync(new UserErrorEvent(ErrorLevel.MEDIUM, null,
231
-                    "Unable to extract default formatters: " + ex.getMessage(), ""));
173
+            eventBus.publishAsync(new UserErrorEvent(ErrorLevel.FATAL, ex,
174
+                    "Unable to iterate required directory '" + file + "'. Please check " +
175
+                            "file permissions or specify a different configuration " +
176
+                            "directory.", ""));
177
+            return;
232 178
         }
179
+
180
+        loadUser(file);
233 181
     }
234 182
 
235 183
     /**
@@ -606,7 +554,7 @@ public class IdentityManager implements IdentityFactory, IdentityController {
606 554
     @Override
607 555
     public ConfigProvider createCustomConfig(final String name, final String type) {
608 556
         final Map<String, Map<String, String>> settings = new HashMap<>();
609
-        settings.put(IDENTITY_DOMAIN, new HashMap<String, String>(2));
557
+        settings.put(IDENTITY_DOMAIN, new HashMap<>(2));
610 558
 
611 559
         settings.get(IDENTITY_DOMAIN).put("name", name);
612 560
         settings.get(IDENTITY_DOMAIN).put("type", type);
@@ -623,8 +571,8 @@ public class IdentityManager implements IdentityFactory, IdentityController {
623 571
     @Override
624 572
     public ConfigProvider createProfileConfig(final String name) {
625 573
         final Map<String, Map<String, String>> settings = new HashMap<>();
626
-        settings.put(IDENTITY_DOMAIN, new HashMap<String, String>(1));
627
-        settings.put(PROFILE_DOMAIN, new HashMap<String, String>(2));
574
+        settings.put(IDENTITY_DOMAIN, new HashMap<>(1));
575
+        settings.put(PROFILE_DOMAIN, new HashMap<>(2));
628 576
 
629 577
         final String nick = System.getProperty("user.name").replace(' ', '_');
630 578
 
@@ -644,7 +592,7 @@ public class IdentityManager implements IdentityFactory, IdentityController {
644 592
     @Override
645 593
     public ConfigProvider createConfig(final ConfigTarget target) {
646 594
         final Map<String, Map<String, String>> settings = new HashMap<>();
647
-        settings.put(IDENTITY_DOMAIN, new HashMap<String, String>(2));
595
+        settings.put(IDENTITY_DOMAIN, new HashMap<>(2));
648 596
         settings.get(IDENTITY_DOMAIN).put("name", target.getData());
649 597
         settings.get(IDENTITY_DOMAIN).put(target.getTypeName(), target.getData());
650 598
 

+ 0
- 58
test/com/dmdirc/config/IdentityManagerTest.java Прегледај датотеку

@@ -42,7 +42,6 @@ import org.mockito.runners.MockitoJUnitRunner;
42 42
 
43 43
 import static org.junit.Assert.assertEquals;
44 44
 import static org.junit.Assert.assertNotNull;
45
-import static org.junit.Assert.assertTrue;
46 45
 
47 46
 @RunWith(MockitoJUnitRunner.class)
48 47
 public class IdentityManagerTest {
@@ -72,63 +71,6 @@ public class IdentityManagerTest {
72 71
         assertEquals("DMDirc version information", versionSettings.getName());
73 72
     }
74 73
 
75
-    @Test
76
-    public void testRenamesExistingDefaultsFile() throws IOException, InvalidIdentityFileException {
77
-        Files.createDirectories(identitiesDirectory);
78
-        Files.createFile(identitiesDirectory.resolve("default"));
79
-
80
-        final IdentityManager identityManager = new IdentityManager(
81
-                baseDirectory, identitiesDirectory, eventBus);
82
-        identityManager.initialise();
83
-
84
-        assertTrue(Files.exists(identitiesDirectory.resolve("default.old")));
85
-        assertTrue(Files.isDirectory(identitiesDirectory.resolve("default")));
86
-    }
87
-
88
-    @Test
89
-    public void testRenamesExistingDefaultsFileWithSuffix() throws IOException,
90
-            InvalidIdentityFileException {
91
-        Files.createDirectories(identitiesDirectory);
92
-        Files.createFile(identitiesDirectory.resolve("default"));
93
-        Files.createFile(identitiesDirectory.resolve("default.old"));
94
-        Files.createFile(identitiesDirectory.resolve("default.old-1"));
95
-
96
-        final IdentityManager identityManager = new IdentityManager(
97
-                baseDirectory, identitiesDirectory, eventBus);
98
-        identityManager.initialise();
99
-
100
-        assertTrue(Files.exists(identitiesDirectory.resolve("default.old")));
101
-        assertTrue(Files.exists(identitiesDirectory.resolve("default.old-1")));
102
-        assertTrue(Files.exists(identitiesDirectory.resolve("default.old-2")));
103
-        assertTrue(Files.isDirectory(identitiesDirectory.resolve("default")));
104
-    }
105
-
106
-    @Test
107
-    public void testExtractsDefaults() throws InvalidIdentityFileException {
108
-        final IdentityManager identityManager = new IdentityManager(
109
-                baseDirectory, identitiesDirectory, eventBus);
110
-        identityManager.initialise();
111
-
112
-        assertTrue(Files.isDirectory(identitiesDirectory.resolve("default")));
113
-        assertTrue(Files.exists(identitiesDirectory.resolve("default").resolve("defaults")));
114
-        assertTrue(Files.exists(identitiesDirectory.resolve("default").resolve("formatter")));
115
-    }
116
-
117
-    @Test
118
-    public void testAlwaysUpdatesFormatter() throws InvalidIdentityFileException, IOException {
119
-        final Path formatterPath = identitiesDirectory.resolve("default").resolve("formatter");
120
-
121
-        Files.createDirectories(identitiesDirectory.resolve("default"));
122
-        Files.createFile(formatterPath);
123
-
124
-        final IdentityManager identityManager = new IdentityManager(
125
-                baseDirectory, identitiesDirectory, eventBus);
126
-        identityManager.initialise();
127
-
128
-        assertTrue(Files.exists(formatterPath));
129
-        assertTrue(Files.size(formatterPath) > 0);
130
-    }
131
-
132 74
     @Test
133 75
     public void testUsesSystemUsernameForProfileNickname() throws InvalidIdentityFileException {
134 76
         final IdentityManager identityManager = new IdentityManager(

Loading…
Откажи
Сачувај