Browse Source

Redo the Identity/ConfigManager get options so they use maps where appropriate, rather than stupid string building

Remove all deprecated Identity/ConfigManager methods
Fixes issue 1335

git-svn-id: http://svn.dmdirc.com/trunk@4200 00569f92-eb28-0410-84fd-f71c24880f
tags/0.6
Chris Smith 16 years ago
parent
commit
335e9c3629

+ 6
- 6
src/com/dmdirc/actions/ActionSubstitutor.java View File

30
 import com.dmdirc.config.IdentityManager;
30
 import com.dmdirc.config.IdentityManager;
31
 
31
 
32
 import java.util.HashMap;
32
 import java.util.HashMap;
33
-import java.util.List;
34
 import java.util.Map;
33
 import java.util.Map;
34
+import java.util.Set;
35
 
35
 
36
 /**
36
 /**
37
  * Handles the substitution of variables into action targets and responses.
37
  * Handles the substitution of variables into action targets and responses.
58
      *
58
      *
59
      * @return A list of global variable names that will be substituted
59
      * @return A list of global variable names that will be substituted
60
      */
60
      */
61
-    public List<String> getConfigSubstitutions() {
62
-        return IdentityManager.getGlobalConfig().getOptions("actions");
61
+    public Set<String> getConfigSubstitutions() {
62
+        return IdentityManager.getGlobalConfig().getOptions("actions").keySet();
63
     }
63
     }
64
     
64
     
65
     /**
65
     /**
68
      * @param target The StringBuilder to modify
68
      * @param target The StringBuilder to modify
69
      */
69
      */
70
     private void doConfigSubstitutions(final StringBuilder target) {
70
     private void doConfigSubstitutions(final StringBuilder target) {
71
-        for (String option : IdentityManager.getGlobalConfig().getOptions("actions")) {
72
-            doReplacement(target, "$" + option,
73
-                    IdentityManager.getGlobalConfig().getOption("actions", option));
71
+        for (Map.Entry<String, String> option 
72
+                : IdentityManager.getGlobalConfig().getOptions("actions").entrySet()) {
73
+            doReplacement(target, "$" + option.getKey(), option.getValue());
74
         }
74
         }
75
     }
75
     }
76
     
76
     

+ 5
- 3
src/com/dmdirc/addons/nickcolours/NickColourPlugin.java View File

139
      */
139
      */
140
     @SuppressWarnings("unchecked")
140
     @SuppressWarnings("unchecked")
141
     private void putColour(final Map map, final Color textColour, final Color nickColour) {
141
     private void putColour(final Map map, final Color textColour, final Color nickColour) {
142
-        if (IdentityManager.getGlobalConfig().getOptionBool(DOMAIN, "settext", false) && textColour != null) {
142
+        if (IdentityManager.getGlobalConfig().getOptionBool(DOMAIN, "settext", false) 
143
+                && textColour != null) {
143
             map.put(ChannelClientProperty.TEXT_FOREGROUND, textColour);
144
             map.put(ChannelClientProperty.TEXT_FOREGROUND, textColour);
144
         }
145
         }
145
         
146
         
146
-        if (IdentityManager.getGlobalConfig().getOptionBool(DOMAIN, "setnicklist", false) && nickColour != null) {
147
+        if (IdentityManager.getGlobalConfig().getOptionBool(DOMAIN, "setnicklist", false) 
148
+                && nickColour != null) {
147
             map.put(ChannelClientProperty.NICKLIST_FOREGROUND, nickColour);
149
             map.put(ChannelClientProperty.NICKLIST_FOREGROUND, nickColour);
148
         }
150
         }
149
     }
151
     }
174
     public Object[][] getData() {
176
     public Object[][] getData() {
175
         final List<Object[]> data = new ArrayList<Object[]>();
177
         final List<Object[]> data = new ArrayList<Object[]>();
176
         
178
         
177
-        for (String key : IdentityManager.getGlobalConfig().getOptions(DOMAIN)) {
179
+        for (String key : IdentityManager.getGlobalConfig().getOptions(DOMAIN).keySet()) {
178
             if (key.startsWith("color:")) {
180
             if (key.startsWith("color:")) {
179
                 final String network = key.substring(6, key.indexOf(':', 6));
181
                 final String network = key.substring(6, key.indexOf(':', 6));
180
                 final String user = key.substring(1 + key.indexOf(':', 6));
182
                 final String user = key.substring(1 + key.indexOf(':', 6));

+ 8
- 2
src/com/dmdirc/addons/userlevel/UserLevelPlugin.java View File

72
     }
72
     }
73
 
73
 
74
     /** {@inheritDoc} */
74
     /** {@inheritDoc} */
75
+    @Override
75
     public void processEvent(final ActionType type, final StringBuffer format,
76
     public void processEvent(final ActionType type, final StringBuffer format,
76
                              final Object... arguments) {
77
                              final Object... arguments) {
77
         switch ((CoreActionType) type) {
78
         switch ((CoreActionType) type) {
127
     private void loadLevels() {
128
     private void loadLevels() {
128
         LEVELS.clear();
129
         LEVELS.clear();
129
         
130
         
130
-        for (String key : IdentityManager.getGlobalConfig().getOptions(DOMAIN)) {
131
-            LEVELS.put(key, IdentityManager.getGlobalConfig().getOptionInt(DOMAIN, key, 0));
131
+        for (Map.Entry<String, String> item 
132
+                : IdentityManager.getGlobalConfig().getOptions(DOMAIN).entrySet()) {
133
+            try {
134
+                LEVELS.put(item.getKey(), Integer.parseInt(item.getValue()));
135
+            } catch (NumberFormatException ex) {
136
+                LEVELS.put(item.getKey(), 0);
137
+            }
132
         }
138
         }
133
     }
139
     }
134
 
140
 

+ 3
- 3
src/com/dmdirc/commandparser/commands/global/Set.java View File

129
         
129
         
130
         boolean found = false;
130
         boolean found = false;
131
         
131
         
132
-        for (String option : manager.getOptions(domain)) {
132
+        for (String option : manager.getOptions(domain).keySet()) {
133
             output.append(option);
133
             output.append(option);
134
             output.append(", ");
134
             output.append(", ");
135
             found = true;
135
             found = true;
254
                     || previousArgs.get(0).equalsIgnoreCase("--server")) {
254
                     || previousArgs.get(0).equalsIgnoreCase("--server")) {
255
                 res.addAll(IdentityManager.getGlobalConfig().getDomains());
255
                 res.addAll(IdentityManager.getGlobalConfig().getDomains());
256
             } else {
256
             } else {
257
-                res.addAll(IdentityManager.getGlobalConfig().getOptions(previousArgs.get(0)));
257
+                res.addAll(IdentityManager.getGlobalConfig().getOptions(previousArgs.get(0)).keySet());
258
             }
258
             }
259
             res.excludeAll();
259
             res.excludeAll();
260
         } else if (arg == 2 && (previousArgs.get(0).equalsIgnoreCase("--unset")
260
         } else if (arg == 2 && (previousArgs.get(0).equalsIgnoreCase("--unset")
261
                 || previousArgs.get(0).equalsIgnoreCase("--append")
261
                 || previousArgs.get(0).equalsIgnoreCase("--append")
262
                 || previousArgs.get(0).equalsIgnoreCase("--server"))) {
262
                 || previousArgs.get(0).equalsIgnoreCase("--server"))) {
263
-            res.addAll(IdentityManager.getGlobalConfig().getOptions(previousArgs.get(1)));
263
+            res.addAll(IdentityManager.getGlobalConfig().getOptions(previousArgs.get(1)).keySet());
264
             res.excludeAll();
264
             res.excludeAll();
265
         }
265
         }
266
         
266
         

+ 13
- 33
src/com/dmdirc/config/ConfigManager.java View File

28
 import java.io.Serializable;
28
 import java.io.Serializable;
29
 import java.util.ArrayList;
29
 import java.util.ArrayList;
30
 import java.util.Collections;
30
 import java.util.Collections;
31
+import java.util.HashMap;
31
 import java.util.HashSet;
32
 import java.util.HashSet;
32
 import java.util.List;
33
 import java.util.List;
33
 import java.util.Map;
34
 import java.util.Map;
137
         return false;
138
         return false;
138
     }
139
     }
139
 
140
 
140
-    /**
141
-     * Returns the name of all known options.
142
-     *
143
-     * @return A list of options
144
-     */
145
-    public Set<String> getOptions() {
146
-        final HashSet<String> res = new HashSet<String>();
147
-
148
-        synchronized (sources) {
149
-            for (Identity source : sources) {
150
-                for (String key : source.getOptions()) {
151
-                    res.add(key);
152
-                }
153
-            }
154
-        }
155
-
156
-        return res;
157
-    }
158
-
159
     /**
141
     /**
160
      * Returns the name of all the options in the specified domain. If the
142
      * Returns the name of all the options in the specified domain. If the
161
      * domain doesn't exist, an empty list is returned.
143
      * domain doesn't exist, an empty list is returned.
163
      * @param domain The domain to search
145
      * @param domain The domain to search
164
      * @return A list of options in the specified domain
146
      * @return A list of options in the specified domain
165
      */
147
      */
166
-    public List<String> getOptions(final String domain) {
167
-        final ArrayList<String> res = new ArrayList<String>();
148
+    public Map<String, String> getOptions(final String domain) {
149
+        final Map<String, String> res = new HashMap<String, String>();
168
 
150
 
169
-        for (String key : getOptions()) {
170
-            if (key.startsWith(domain + ".")) {
171
-                res.add(key.substring(domain.length() + 1));
151
+        synchronized (sources) {
152
+            for (Identity source : sources) {
153
+                res.putAll(source.getOptions(domain));
172
             }
154
             }
173
         }
155
         }
174
 
156
 
297
      *
279
      *
298
      * @return A list of domains known to this manager
280
      * @return A list of domains known to this manager
299
      */
281
      */
300
-    public List<String> getDomains() {
301
-        final ArrayList<String> res = new ArrayList<String>();
302
-        String domain;
303
-
304
-        for (String key : getOptions()) {
305
-            domain = key.substring(0, key.indexOf('.'));
306
-            if (!res.contains(domain)) {
307
-                res.add(domain);
282
+    public Set<String> getDomains() {
283
+        final Set<String> res = new HashSet<String>();
284
+
285
+        synchronized (sources) {
286
+            for (Identity source : sources) {
287
+                res.addAll(source.getDomains());
308
             }
288
             }
309
         }
289
         }
310
-
290
+        
311
         return res;
291
         return res;
312
     }
292
     }
313
 
293
 

+ 24
- 37
src/com/dmdirc/config/Identity.java View File

35
 import java.io.FileWriter;
35
 import java.io.FileWriter;
36
 import java.io.IOException;
36
 import java.io.IOException;
37
 import java.io.InputStream;
37
 import java.io.InputStream;
38
+import java.io.Reader;
38
 import java.io.Serializable;
39
 import java.io.Serializable;
39
 import java.net.MalformedURLException;
40
 import java.net.MalformedURLException;
40
 import java.util.ArrayList;
41
 import java.util.ArrayList;
41
 import java.util.List;
42
 import java.util.List;
42
 import java.util.Map;
43
 import java.util.Map;
43
 import java.util.Properties;
44
 import java.util.Properties;
45
+import java.util.Set;
44
 
46
 
45
 /**
47
 /**
46
  * An identity is a group of settings that are applied to a connection, server,
48
  * An identity is a group of settings that are applied to a connection, server,
211
             this.file.read();
213
             this.file.read();
212
         } catch (InvalidConfigFileException ex) {            
214
         } catch (InvalidConfigFileException ex) {            
213
             final Properties properties = new Properties();
215
             final Properties properties = new Properties();
214
-            properties.load(new LineReader(this.file.getLines()));            
216
+            final Reader reader = new LineReader(this.file.getLines());
217
+            properties.load(reader);
218
+            reader.close();
215
             migrateProperties(properties);
219
             migrateProperties(properties);
216
         }
220
         }
217
 
221
 
276
         }
280
         }
277
     }
281
     }
278
 
282
 
279
-    /**
280
-     * Returns the properties object belonging to this identity.
281
-     *
282
-     * @return This identity's property object
283
-     * @deprecated Get a map
284
-     */
285
-    @Deprecated
286
-    public Properties getProperties() {
287
-        final Properties properties = new Properties();
288
-
289
-        for (Map.Entry<String, Map<String, String>> entry : file.getKeyDomains().entrySet()) {
290
-            for (Map.Entry<String, String> subentry : entry.getValue().entrySet()) {
291
-                properties.setProperty(entry.getKey() + "." + subentry.getKey(),
292
-                        subentry.getValue());
293
-            }
294
-        }
295
-
296
-        return properties;
297
-    }
298
-
299
     /**
283
     /**
300
      * Returns the name of this identity.
284
      * Returns the name of this identity.
301
      *
285
      *
431
             listener.configChanged(domain, option);
415
             listener.configChanged(domain, option);
432
         }
416
         }
433
     }
417
     }
434
-
418
+    
435
     /**
419
     /**
436
-     * Returns a list of options avaiable in this identity.
437
-     *
438
-     * @return Option list
439
-     * @deprecated Get a map
420
+     * Returns the set of domains available in this identity.
421
+     * 
422
+     * @since 0.6
423
+     * @return The set of domains used by this identity
440
      */
424
      */
441
-    @Deprecated
442
-    public List<String> getOptions() {
443
-        final List<String> res = new ArrayList<String>();
444
-
445
-        for (Map.Entry<String, Map<String, String>> entry : file.getKeyDomains().entrySet()) {
446
-            for (String key : entry.getValue().keySet()) {
447
-                res.add(entry.getKey() + "." + key);
448
-            }
449
-        }
450
-
451
-        return res;
425
+    public Set<String> getDomains() {
426
+        return file.getKeyDomains().keySet();
427
+    }
428
+    
429
+    /**
430
+     * Retrieves a map of all options within the specified domain in this
431
+     * identity.
432
+     * 
433
+     * @param domain The domain to retrieve
434
+     * @since 0.6
435
+     * @return A map of option names to values
436
+     */
437
+    public Map<String, String> getOptions(final String domain) {
438
+        return file.getKeyDomain(domain);
452
     }
439
     }
453
 
440
 
454
     /**
441
     /**

+ 3
- 2
src/com/dmdirc/ui/swing/dialogs/actioneditor/ActionResponsePanel.java View File

24
 
24
 
25
 import com.dmdirc.config.IdentityManager;
25
 import com.dmdirc.config.IdentityManager;
26
 
26
 
27
-import java.util.List;
27
+import java.util.Set;
28
 
28
 
29
 import javax.swing.BorderFactory;
29
 import javax.swing.BorderFactory;
30
 import javax.swing.DefaultComboBoxModel;
30
 import javax.swing.DefaultComboBoxModel;
69
         ((DefaultComboBoxModel) formatter.getModel()).addElement("No change");
69
         ((DefaultComboBoxModel) formatter.getModel()).addElement("No change");
70
         ((DefaultComboBoxModel) formatter.getModel()).addElement("No response");
70
         ((DefaultComboBoxModel) formatter.getModel()).addElement("No response");
71
         
71
         
72
-        final List<String> formatters = IdentityManager.getGlobalConfig().getOptions("formatter");
72
+        final Set<String> formatters
73
+                = IdentityManager.getGlobalConfig().getOptions("formatter").keySet();
73
         
74
         
74
         for (String format : formatters) {
75
         for (String format : formatters) {
75
             ((DefaultComboBoxModel) formatter.getModel()).addElement(format);
76
             ((DefaultComboBoxModel) formatter.getModel()).addElement(format);

+ 3
- 2
src/com/dmdirc/ui/swing/dialogs/actionseditor/ResponseTabPanel.java View File

32
 import java.awt.GridBagConstraints;
32
 import java.awt.GridBagConstraints;
33
 import java.awt.GridBagLayout;
33
 import java.awt.GridBagLayout;
34
 import java.awt.Insets;
34
 import java.awt.Insets;
35
-import java.util.List;
35
+import java.util.Set;
36
 
36
 
37
 import javax.swing.BorderFactory;
37
 import javax.swing.BorderFactory;
38
 import javax.swing.DefaultComboBoxModel;
38
 import javax.swing.DefaultComboBoxModel;
103
         ((DefaultComboBoxModel) formatter.getModel()).addElement("No change");
103
         ((DefaultComboBoxModel) formatter.getModel()).addElement("No change");
104
         ((DefaultComboBoxModel) formatter.getModel()).addElement("No response");
104
         ((DefaultComboBoxModel) formatter.getModel()).addElement("No response");
105
         
105
         
106
-        final List<String> formatters = IdentityManager.getGlobalConfig().getOptions("formatter");
106
+        final Set<String> formatters
107
+                = IdentityManager.getGlobalConfig().getOptions("formatter").keySet();
107
         
108
         
108
         for (String format : formatters) {
109
         for (String format : formatters) {
109
             ((DefaultComboBoxModel) formatter.getModel()).addElement(format);
110
             ((DefaultComboBoxModel) formatter.getModel()).addElement(format);

+ 7
- 7
src/com/dmdirc/ui/swing/dialogs/prefs/URLConfigPanel.java View File

36
 import java.awt.event.ActionEvent;
36
 import java.awt.event.ActionEvent;
37
 import java.awt.event.ActionListener;
37
 import java.awt.event.ActionListener;
38
 import java.net.URI;
38
 import java.net.URI;
39
-
40
 import java.net.URISyntaxException;
39
 import java.net.URISyntaxException;
41
 import java.util.HashMap;
40
 import java.util.HashMap;
42
-import java.util.List;
43
 import java.util.Map;
41
 import java.util.Map;
44
 import java.util.Map.Entry;
42
 import java.util.Map.Entry;
43
+import java.util.Set;
44
+
45
 import javax.swing.JButton;
45
 import javax.swing.JButton;
46
 import javax.swing.JPanel;
46
 import javax.swing.JPanel;
47
 import javax.swing.JScrollPane;
47
 import javax.swing.JScrollPane;
48
 import javax.swing.ListSelectionModel;
48
 import javax.swing.ListSelectionModel;
49
 import javax.swing.event.ListSelectionEvent;
49
 import javax.swing.event.ListSelectionEvent;
50
 import javax.swing.event.ListSelectionListener;
50
 import javax.swing.event.ListSelectionListener;
51
-
52
 import javax.swing.table.TableCellRenderer;
51
 import javax.swing.table.TableCellRenderer;
52
+
53
 import net.miginfocom.swing.MigLayout;
53
 import net.miginfocom.swing.MigLayout;
54
 
54
 
55
 /**
55
 /**
139
 
139
 
140
         tableScrollPane.setViewportView(table);
140
         tableScrollPane.setViewportView(table);
141
 
141
 
142
-        final List<String> options = IdentityManager.getGlobalConfig().
143
-                getOptions("protocol");
142
+        final Set<String> options = IdentityManager.getGlobalConfig().
143
+                getOptions("protocol").keySet();
144
 
144
 
145
         for (String option : options) {
145
         for (String option : options) {
146
             try {
146
             try {
180
     public void save() {
180
     public void save() {
181
         valueChanged(null);
181
         valueChanged(null);
182
         final Map<URI, String> handlers = model.getURLHandlers();
182
         final Map<URI, String> handlers = model.getURLHandlers();
183
-        final List<String> protocols = IdentityManager.getGlobalConfig().
184
-                getOptions("protocol");
183
+        final Set<String> protocols = IdentityManager.getGlobalConfig().
184
+                getOptions("protocol").keySet();
185
         for (String protocol : protocols) {
185
         for (String protocol : protocols) {
186
             URI uri;
186
             URI uri;
187
             try {
187
             try {

+ 12
- 20
test/com/dmdirc/config/IdentityTest.java View File

23
 package com.dmdirc.config;
23
 package com.dmdirc.config;
24
 
24
 
25
 import com.dmdirc.harness.TestConfigListener;
25
 import com.dmdirc.harness.TestConfigListener;
26
-import com.dmdirc.interfaces.ConfigChangeListener;
27
 
26
 
28
 import java.io.IOException;
27
 import java.io.IOException;
29
-import java.util.Properties;
28
+import java.util.Map;
30
 
29
 
31
 import org.junit.After;
30
 import org.junit.After;
32
 import org.junit.Before;
31
 import org.junit.Before;
51
         myIdent = null;
50
         myIdent = null;
52
     }    
51
     }    
53
     
52
     
54
-    @Test
55
-    public void testGetProperties() {
56
-        myIdent.setOption("domain", "option", "value");
57
-        final Properties props = myIdent.getProperties();
58
-        
59
-        assertEquals(props.getProperty("domain.option"), "value");
60
-        
61
-        myIdent.unsetOption("domain", "option");
62
-    }
63
-
64
     @Test
53
     @Test
65
     public void testGetName() {
54
     public void testGetName() {
66
-        final Properties props = myIdent.getProperties();
55
+        final Map<String, String> props = myIdent.getOptions("identity");
67
         
56
         
68
-        assertEquals(props.getProperty("identity.name"), myIdent.getName());
57
+        assertEquals(props.get("name"), myIdent.getName());
69
     }
58
     }
70
     
59
     
71
     @Test
60
     @Test
100
     @Test
89
     @Test
101
     public void testGetOption() {
90
     public void testGetOption() {
102
         myIdent.setOption("domain", "option", "value");
91
         myIdent.setOption("domain", "option", "value");
103
-        final Properties props = myIdent.getProperties();
92
+        final Map<String, String> props = myIdent.getOptions("domain");
104
         
93
         
105
-        assertEquals(props.getProperty("domain.option"), myIdent.getOption("domain", "option"));
94
+        assertEquals(props.get("option"), myIdent.getOption("domain", "option"));
106
         
95
         
107
         myIdent.unsetOption("domain", "option");
96
         myIdent.unsetOption("domain", "option");
108
     }
97
     }
109
 
98
 
110
     @Test
99
     @Test
111
     public void testSetOption() {
100
     public void testSetOption() {
112
-        final int count = myIdent.getProperties().size();
101
+        final int count = myIdent.getOptions("foo").size();
113
         
102
         
114
         myIdent.setOption("foo", "bar", "baz");
103
         myIdent.setOption("foo", "bar", "baz");
115
         
104
         
116
-        assertEquals(count + 1, myIdent.getProperties().size());
105
+        assertEquals(count + 1, myIdent.getOptions("foo").size());
117
         
106
         
118
         myIdent.unsetOption("foo", "bar");
107
         myIdent.unsetOption("foo", "bar");
119
     }
108
     }
134
 
123
 
135
     @Test
124
     @Test
136
     public void testRemoveOption() {
125
     public void testRemoveOption() {
137
-        final Properties props = myIdent.getProperties();
126
+        final Map<String, String> props = myIdent.getOptions("foo");
138
         final int count = props.size();
127
         final int count = props.size();
139
         
128
         
140
         myIdent.setOption("foo", "bar", "baz");
129
         myIdent.setOption("foo", "bar", "baz");
130
+        
131
+        assertEquals(count + 1, myIdent.getOptions("foo").size());
132
+        
141
         myIdent.unsetOption("foo", "bar");
133
         myIdent.unsetOption("foo", "bar");
142
         
134
         
143
-        assertEquals(count, props.size());
135
+        assertEquals(count, myIdent.getOptions("foo").size());
144
     }
136
     }
145
 
137
 
146
     @Test
138
     @Test

Loading…
Cancel
Save