瀏覽代碼

Add serverlist plugin

Change-Id: I759dc8f56920362567778af366a34288fbf2a75d
Reviewed-on: http://gerrit.dmdirc.com/1444
Reviewed-by: Gregory Holmes <greg@dmdirc.com>
Automatic-Compile: DMDirc Local Commits <dmdirc@googlemail.com>
Automatic-Compile: Gregory Holmes <greg@dmdirc.com>
tags/0.6.4
Chris Smith 14 年之前
父節點
當前提交
14e847872d
共有 22 個文件被更改,包括 1345 次插入15 次删除
  1. 104
    0
      src/com/dmdirc/addons/serverlists/ServerEntry.java
  2. 221
    0
      src/com/dmdirc/addons/serverlists/ServerGroup.java
  3. 110
    0
      src/com/dmdirc/addons/serverlists/ServerGroupItem.java
  4. 122
    0
      src/com/dmdirc/addons/serverlists/ServerGroupItemBase.java
  5. 155
    0
      src/com/dmdirc/addons/serverlists/ServerList.java
  6. 47
    0
      src/com/dmdirc/addons/serverlists/ServerListPlugin.java
  7. 77
    0
      src/com/dmdirc/addons/serverlists/io/ServerEntryReader.java
  8. 146
    0
      src/com/dmdirc/addons/serverlists/io/ServerGroupReader.java
  9. 168
    0
      src/com/dmdirc/addons/serverlists/io/ServerGroupWriter.java
  10. 28
    0
      src/com/dmdirc/addons/serverlists/plugin.config
  11. 152
    0
      src/com/dmdirc/addons/serverlists/service/ServerListServiceProvider.java
  12. 1
    1
      src/com/dmdirc/addons/ui_swing/components/renderers/ServerGroupTreeRenderer.java
  13. 1
    1
      src/com/dmdirc/addons/ui_swing/dialogs/serverlist/AddEntryInputDialog.java
  14. 1
    1
      src/com/dmdirc/addons/ui_swing/dialogs/serverlist/AddGroupInputDialog.java
  15. 1
    1
      src/com/dmdirc/addons/ui_swing/dialogs/serverlist/Info.java
  16. 1
    1
      src/com/dmdirc/addons/ui_swing/dialogs/serverlist/Perform.java
  17. 1
    1
      src/com/dmdirc/addons/ui_swing/dialogs/serverlist/Profiles.java
  18. 1
    1
      src/com/dmdirc/addons/ui_swing/dialogs/serverlist/ServerListDialog.java
  19. 1
    1
      src/com/dmdirc/addons/ui_swing/dialogs/serverlist/ServerListListener.java
  20. 4
    4
      src/com/dmdirc/addons/ui_swing/dialogs/serverlist/ServerListModel.java
  21. 2
    2
      src/com/dmdirc/addons/ui_swing/dialogs/serverlist/Settings.java
  22. 1
    1
      src/com/dmdirc/addons/ui_swing/dialogs/serverlist/Tree.java

+ 104
- 0
src/com/dmdirc/addons/serverlists/ServerEntry.java 查看文件

@@ -0,0 +1,104 @@
1
+/*
2
+ * Copyright (c) 2006-2010 Chris Smith, Shane Mc Cormack, Gregory Holmes
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.addons.serverlists;
24
+
25
+import com.dmdirc.Server;
26
+
27
+import java.net.URI;
28
+
29
+/**
30
+ * Describes an entry for a server within a {@link ServerGroup}.
31
+ *
32
+ * @since 0.6.4
33
+ * @author chris
34
+ */
35
+public class ServerEntry extends ServerGroupItemBase {
36
+
37
+    /** The address of the server in question. */
38
+    private URI address;
39
+
40
+    /** The group that owns this entry. */
41
+    private final ServerGroup group;
42
+
43
+    /**
44
+     * Creates a new server entry.
45
+     *
46
+     * @param group The group that owns this entry
47
+     * @param name The name of this server
48
+     * @param address The address of this server
49
+     * @param profile The name of the profile to be used by this server
50
+     */
51
+    public ServerEntry(final ServerGroup group, final String name,
52
+            final URI address, final String profile) {
53
+        setName(name);
54
+        setProfile(profile);
55
+        this.address = address;
56
+        this.group = group;
57
+    }
58
+
59
+    /** {@inheritDoc} */
60
+    @Override
61
+    public ServerGroup getGroup() {
62
+        return group;
63
+    }
64
+
65
+    /** {@inheritDoc} */
66
+    @Override
67
+    protected ServerGroup getParent() {
68
+        return getGroup();
69
+    }
70
+
71
+    /**
72
+     * Retrieves the address used by this server.
73
+     *
74
+     * @return This server's address
75
+     */
76
+    @Override
77
+    public URI getAddress() {
78
+        return address;
79
+    }
80
+
81
+    /**
82
+     * Sets the address for this server entry.
83
+     *
84
+     * @param address The new address for this entry
85
+     */
86
+    public void setAddress(final URI address) {
87
+        setModified(true);
88
+        this.address = address;
89
+    }
90
+
91
+    /** {@inheritDoc} */
92
+    @Override
93
+    public void connect() {
94
+        final Server server = new Server(address, getProfileIdentity());
95
+        server.connect();
96
+    }
97
+
98
+    /** {@inheritDoc} */
99
+    @Override
100
+    public String toString() {
101
+        return "[" + getName() + ": address: " + getAddress() + "]";
102
+    }
103
+
104
+}

+ 221
- 0
src/com/dmdirc/addons/serverlists/ServerGroup.java 查看文件

@@ -0,0 +1,221 @@
1
+/*
2
+ * Copyright (c) 2006-2010 Chris Smith, Shane Mc Cormack, Gregory Holmes
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.addons.serverlists;
24
+
25
+import java.net.URI;
26
+import java.util.ArrayList;
27
+import java.util.Collections;
28
+import java.util.HashMap;
29
+import java.util.List;
30
+import java.util.Map;
31
+
32
+/**
33
+ * A server group contains an ordered collection of server entries or other
34
+ * server groups.
35
+ *
36
+ * @since 0.6.4
37
+ * @author chris
38
+ */
39
+public class ServerGroup extends ServerGroupItemBase {
40
+
41
+    /** The name of the network for the group. */
42
+    private String network;
43
+
44
+    /** A description of this group. */
45
+    private String description = "";
46
+
47
+    /** This group's parent. */
48
+    private final ServerGroup parent;
49
+
50
+    /** A set of links relevant to this group (e.g. homepages). */
51
+    private final Map<String, URI> links = new HashMap<String, URI>();
52
+    
53
+    /** The items contained within the group. */
54
+    private final List<ServerGroupItem> entries = new ArrayList<ServerGroupItem>();
55
+
56
+    /**
57
+     * Creates a new server group with the specified name and no parent.
58
+     *
59
+     * @param name The name to be used for this group
60
+     */
61
+    public ServerGroup(final String name) {
62
+        this(null, name);
63
+    }
64
+
65
+    /**
66
+     * Creates a new server group with the specified name.
67
+     *
68
+     * @param parent The parent of this group, or <code>null</code> for roots.
69
+     * @param name The name to be used for this group
70
+     */
71
+    public ServerGroup(final ServerGroup parent, final String name) {
72
+        this.parent = parent;
73
+        setName(name);
74
+    }
75
+
76
+    /** {@inheritDoc} */
77
+    @Override
78
+    public ServerGroup getGroup() {
79
+        return this;
80
+    }
81
+
82
+    /** {@inheritDoc} */
83
+    @Override
84
+    protected ServerGroup getParent() {
85
+        return parent;
86
+    }
87
+
88
+    /**
89
+     * Sets the network name of this group.
90
+     *
91
+     * @param network The new network name for the group
92
+     */
93
+    public void setNetwork(final String network) {
94
+        setModified(true);
95
+        this.network = network;
96
+    }
97
+
98
+    /**
99
+     * Adds a new item to this server group.
100
+     *
101
+     * @param item The item to be added
102
+     */
103
+    public void addItem(final ServerGroupItem item) {
104
+        setModified(true);
105
+        item.setModified(true);
106
+        entries.add(item);
107
+    }
108
+
109
+    /**
110
+     * Retrieves a list of items belonging to this group.
111
+     *
112
+     * @return An immutable list of items contained within this group
113
+     */
114
+    public List<ServerGroupItem> getItems() {
115
+        return Collections.unmodifiableList(entries);
116
+    }
117
+
118
+    /**
119
+     * Retrieves a ServerGroupItem with the specified name, if one exists. This
120
+     * method ignores the case of item's name when comparing.
121
+     *
122
+     * @param name The name of the item to be retrieved
123
+     * @return A correspondingly named item, or null if none exists
124
+     */
125
+    public ServerGroupItem getItemByName(final String name) {
126
+        for (ServerGroupItem item : getItems()) {
127
+            if (item.getName().equalsIgnoreCase(name)) {
128
+                return item;
129
+            }
130
+        }
131
+
132
+        return null;
133
+    }
134
+
135
+    /**
136
+     * Retrieves the description of this group.
137
+     * 
138
+     * @return This group's description
139
+     */
140
+    public String getDescription() {
141
+        return description;
142
+    }
143
+
144
+    /**
145
+     * Retrieves the network that this group represents, if any.
146
+     *
147
+     * @return This group's network name, or null if the group does not
148
+     * correspond to a known network.
149
+     */
150
+    public String getNetwork() {
151
+        return network;
152
+    }
153
+
154
+    /**
155
+     * Sets the description of this group.
156
+     *
157
+     * @param description The new description for this group.
158
+     */
159
+    public void setDescription(final String description) {
160
+        setModified(true);
161
+        this.description = description;
162
+    }
163
+
164
+    /**
165
+     * Retrieves a map of link titles to {@link URI}s which are associated
166
+     * with this server group. Links will typically include network homepages,
167
+     * forums, or support channels.
168
+     * 
169
+     * @return An immutable map of links
170
+     */
171
+    public Map<String, URI> getLinks() {
172
+        return Collections.unmodifiableMap(links);
173
+    }
174
+
175
+    /**
176
+     * Adds a new link with the specified title and address. If a link with the
177
+     * same title existed previously, it will be replaced.
178
+     *
179
+     * @param title The title of the new link
180
+     * @param address The address of the link
181
+     */
182
+    public void addLink(final String title, final URI address) {
183
+        setModified(true);
184
+        links.put(title, address);
185
+    }
186
+
187
+    /**
188
+     * {@inheritDoc}
189
+     *
190
+     * Current implementation just selects the first item in this group and
191
+     * asks for its URI.
192
+     */
193
+    @Override
194
+    public URI getAddress() {
195
+        if (!entries.isEmpty()) {
196
+            return entries.get(0).getAddress();
197
+        }
198
+
199
+        return null;
200
+    }
201
+
202
+    /**
203
+     * {@inheritDoc}
204
+     *
205
+     * Current implementation just selects the first item in this group and
206
+     * asks it to connect.
207
+     */
208
+    @Override
209
+    public void connect() {
210
+        if (!entries.isEmpty()) {
211
+            entries.get(0).connect();
212
+        }
213
+    }
214
+
215
+    /** {@inheritDoc} */
216
+    @Override
217
+    public String toString() {
218
+        return "[" + getName() + ": links: " + getLinks() + "; desc: "
219
+                + getDescription() + "; content: " + getItems() + "]";
220
+    }
221
+}

+ 110
- 0
src/com/dmdirc/addons/serverlists/ServerGroupItem.java 查看文件

@@ -0,0 +1,110 @@
1
+/*
2
+ * Copyright (c) 2006-2010 Chris Smith, Shane Mc Cormack, Gregory Holmes
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.addons.serverlists;
24
+
25
+import java.net.URI;
26
+
27
+/**
28
+ * An item which is included in a server group.
29
+ *
30
+ * @author chris
31
+ * @since 0.6.4
32
+ */
33
+public interface ServerGroupItem {
34
+
35
+    /**
36
+     * Retrieves a group that is either the same as this item, or contains this
37
+     * item.
38
+     *
39
+     * @return A group containing or equal to this item
40
+     */
41
+    ServerGroup getGroup();
42
+
43
+    /**
44
+     * Retrieves the name of this item.
45
+     *
46
+     * @return A string containing this item's name
47
+     */
48
+    String getName();
49
+
50
+    /**
51
+     * Sets the name of this group.
52
+     *
53
+     * @param name The new name for the group
54
+     */
55
+    void setName(final String name);
56
+
57
+    /**
58
+     * Retrieves the path and name of this item in textual format.
59
+     *
60
+     * @return A string containing the items path and name
61
+     */
62
+    String getPath();
63
+
64
+    /**
65
+     * Initiates a connection attempt for this item.
66
+     */
67
+    void connect();
68
+
69
+    /**
70
+     * Retrieves a URI for this item.
71
+     *
72
+     * @return A URI that represents this item or one of its children, or null
73
+     * if the item has no URIs associated with it.
74
+     */
75
+    URI getAddress();
76
+
77
+    /**
78
+     * Determines whether this item has been modified since the last time
79
+     * {@link #setModified(boolean)} was called with a <code>false</code>
80
+     * argument (or since this item was created).
81
+     *
82
+     * @return True if the item has been modified, false otherwise
83
+     */
84
+    boolean isModified();
85
+
86
+    /**
87
+     * Sets the modified status of this item.
88
+     *
89
+     * @param isModified The new status of the 'modified' property of this item
90
+     */
91
+    void setModified(final boolean isModified);
92
+
93
+    /**
94
+     * Retrieves the name of the profile which should be used when connecting
95
+     * to this item.
96
+     *
97
+     * @return The profile name used by this entry, or <code>null</code> if the
98
+     * default or parent group's profile should be used
99
+     */
100
+    String getProfile();
101
+
102
+    /**
103
+     * Sets the profile to be used for this server entry.
104
+     *
105
+     * @param profile The new profile name for this entry, or <code>null</code>
106
+     * if the default or parent group's profile should be used
107
+     */
108
+    void setProfile(final String profile);
109
+
110
+}

+ 122
- 0
src/com/dmdirc/addons/serverlists/ServerGroupItemBase.java 查看文件

@@ -0,0 +1,122 @@
1
+/*
2
+ * Copyright (c) 2006-2010 Chris Smith, Shane Mc Cormack, Gregory Holmes
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.addons.serverlists;
24
+
25
+import com.dmdirc.config.Identity;
26
+import com.dmdirc.config.IdentityManager;
27
+
28
+/**
29
+ * Abstract base class for {@link ServerGroupItem}s.
30
+ *
31
+ * @author chris
32
+ * @since 0.6.4
33
+ */
34
+public abstract class ServerGroupItemBase implements ServerGroupItem {
35
+
36
+    /** Whether or not this item has been modified. */
37
+    private boolean modified;
38
+
39
+    /** The name of the item. */
40
+    private String name;
41
+
42
+    /** The name of the profile to use. */
43
+    private String profile;
44
+
45
+    /** {@inheritDoc} */
46
+    @Override
47
+    public boolean isModified() {
48
+        return modified;
49
+    }
50
+
51
+    /** {@inheritDoc} */
52
+    @Override
53
+    public void setModified(final boolean isModified) {
54
+        this.modified = isModified;
55
+    }
56
+
57
+    /** {@inheritDoc} */
58
+    @Override
59
+    public String getName() {
60
+        return name;
61
+    }
62
+
63
+    /** {@inheritDoc} */
64
+    @Override
65
+    public void setName(final String name) {
66
+        setModified(true);
67
+        this.name = name;
68
+    }
69
+
70
+    /** {@inheritDoc} */
71
+    @Override
72
+    public String getPath() {
73
+        if (getParent() != null) {
74
+            return getParent().getPath() + " → " + getName();
75
+        }
76
+        return getName();
77
+    }
78
+
79
+    /** {@inheritDoc} */
80
+    @Override
81
+    public String getProfile() {
82
+        return profile;
83
+    }
84
+
85
+    /** {@inheritDoc} */
86
+    @Override
87
+    public void setProfile(final String profile) {
88
+        setModified(true);
89
+        this.profile = profile;
90
+    }
91
+
92
+    /**
93
+     * Returns the parent group of this item, or <code>null</code> if the
94
+     * item is a root group.
95
+     *
96
+     * @return This item's parent group
97
+     */
98
+    protected abstract ServerGroup getParent();
99
+
100
+    /**
101
+     * Returns the {@link Identity} which corresponds to this server's desired
102
+     * profile.
103
+     *
104
+     * @return This server's profile identity
105
+     */
106
+    protected Identity getProfileIdentity() {
107
+        if (profile != null) {
108
+            for (Identity identity : IdentityManager.getCustomIdentities("profile")) {
109
+                if (profile.equals(identity.getName())) {
110
+                    return identity;
111
+                }
112
+            }
113
+        }
114
+
115
+        if (getParent() == null) {
116
+            return IdentityManager.getCustomIdentities("profile").get(0);
117
+        } else {
118
+            return getParent().getProfileIdentity();
119
+        }
120
+    }
121
+
122
+}

+ 155
- 0
src/com/dmdirc/addons/serverlists/ServerList.java 查看文件

@@ -0,0 +1,155 @@
1
+/*
2
+ * Copyright (c) 2006-2010 Chris Smith, Shane Mc Cormack, Gregory Holmes
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.addons.serverlists;
24
+
25
+import com.dmdirc.Precondition;
26
+import com.dmdirc.config.Identity;
27
+import com.dmdirc.config.IdentityListener;
28
+import com.dmdirc.config.IdentityManager;
29
+import com.dmdirc.addons.serverlists.io.ServerGroupReader;
30
+import com.dmdirc.addons.serverlists.io.ServerGroupWriter;
31
+import com.dmdirc.addons.serverlists.service.ServerListServiceProvider;
32
+
33
+import java.io.IOException;
34
+import java.util.Collection;
35
+import java.util.Collections;
36
+import java.util.HashMap;
37
+import java.util.Map;
38
+
39
+/**
40
+ * Maintains a list of top level {@link ServerGroup}s and handles reading and
41
+ * writing of the lists to disk.
42
+ *
43
+ * @since 0.6.4
44
+ * @author chris
45
+ */
46
+public class ServerList implements IdentityListener {
47
+
48
+    /** A list of all known groups. */
49
+    private final Map<ServerGroup, ServerGroupWriter> groups
50
+            = new HashMap<ServerGroup, ServerGroupWriter>();
51
+
52
+    /**
53
+     * Creates a new ServerList and loads groups and servers.
54
+     */
55
+    public ServerList() {
56
+        IdentityManager.addIdentityListener("servergroup", this);
57
+
58
+        for (Identity identity : IdentityManager.getCustomIdentities("servergroup")) {
59
+            identityAdded(identity);
60
+        }
61
+
62
+        new ServerListServiceProvider(this).register();
63
+    }
64
+
65
+    /**
66
+     * Adds a server group to the master server list.
67
+     *
68
+     * @param group The group to be added
69
+     * @param writer The writer to use to write the group to disk
70
+     */
71
+    public void addServerGroup(final ServerGroup group, final ServerGroupWriter writer) {
72
+        groups.put(group, writer);
73
+    }
74
+
75
+    /**
76
+     * Adds a server group to the master server list, and creates a new
77
+     * writer which will write the group to an identity.
78
+     *
79
+     * @param group The group to be added
80
+     * @throws IOException if the new identity cannot be written
81
+     */
82
+    public void addServerGroup(final ServerGroup group) throws IOException {
83
+        final ServerGroupWriter writer = new ServerGroupWriter(
84
+                Identity.buildIdentity(group.getName(), "servergroup"));
85
+        group.setModified(true);
86
+        addServerGroup(group, writer);
87
+    }
88
+
89
+    /**
90
+     * Saves all entries in this list.
91
+     */
92
+    public void save() {
93
+        for (Map.Entry<ServerGroup, ServerGroupWriter> pair : groups.entrySet()) {
94
+            if (pair.getKey().isModified()) {
95
+                pair.getValue().write(pair.getKey());
96
+            }
97
+        }
98
+    }
99
+
100
+    /**
101
+     * Saves the specified group.
102
+     *
103
+     * @param group The group to be saved
104
+     */
105
+    @Precondition("Specified group is a known top-level group in this list")
106
+    public void save(final ServerGroup group) {
107
+        groups.get(group).write(group);
108
+    }
109
+
110
+    /**
111
+     * Retrieves a list of all known server groups.
112
+     *
113
+     * @return An immutable list of server groups.
114
+     */
115
+    public Collection<ServerGroup> getServerGroups() {
116
+        return Collections.unmodifiableCollection(groups.keySet());
117
+    }
118
+
119
+    /**
120
+     * Retrieves a ServerGroup with the specified name, if one exists. This
121
+     * method ignores the case of group's name when comparing.
122
+     *
123
+     * @param name The name of the group to be retrieved
124
+     * @return A correspondingly named server group, or null if none exists
125
+     */
126
+    public ServerGroup getGroupByName(final String name) {
127
+        for (ServerGroup group : getServerGroups()) {
128
+            if (group.getName().equalsIgnoreCase(name)) {
129
+                return group;
130
+            }
131
+        }
132
+
133
+        return null;
134
+    }
135
+
136
+    /** {@inheritDoc} */
137
+    @Override
138
+    public void identityAdded(final Identity identity) {
139
+        try {
140
+            final ServerGroupReader reader = new ServerGroupReader(identity);
141
+            addServerGroup(reader.read(), reader.getWriter());
142
+        } catch (IllegalArgumentException ex) {
143
+            // Silently ignore
144
+            // TODO: Raise error if the identity isn't a server group being
145
+            //       currently added by addServerGroup()
146
+        }
147
+    }
148
+
149
+    /** {@inheritDoc} */
150
+    @Override
151
+    public void identityRemoved(final Identity identity) {
152
+        // TODO: Remove server group
153
+    }
154
+
155
+}

+ 47
- 0
src/com/dmdirc/addons/serverlists/ServerListPlugin.java 查看文件

@@ -0,0 +1,47 @@
1
+/*
2
+ * Copyright (c) 2006-2010 Chris Smith, Shane Mc Cormack, Gregory Holmes
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.addons.serverlists;
24
+
25
+import com.dmdirc.plugins.Plugin;
26
+
27
+
28
+/**
29
+ * Dummy plugin for server lists.
30
+ *
31
+ * @author chris
32
+ */
33
+public class ServerListPlugin extends Plugin {
34
+
35
+    /** {@inheritDoc} */
36
+    @Override
37
+    public void onLoad() {
38
+        // Do nothing
39
+    }
40
+
41
+    /** {@inheritDoc} */
42
+    @Override
43
+    public void onUnload() {
44
+        // Do nothing
45
+    }
46
+
47
+}

+ 77
- 0
src/com/dmdirc/addons/serverlists/io/ServerEntryReader.java 查看文件

@@ -0,0 +1,77 @@
1
+/*
2
+ * Copyright (c) 2006-2010 Chris Smith, Shane Mc Cormack, Gregory Holmes
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.addons.serverlists.io;
24
+
25
+import com.dmdirc.config.Identity;
26
+import com.dmdirc.addons.serverlists.ServerEntry;
27
+import com.dmdirc.addons.serverlists.ServerGroup;
28
+
29
+import java.net.URI;
30
+import java.net.URISyntaxException;
31
+
32
+/**
33
+ * Facilitates loading of a {@link ServerEntry} from a DMDirc {@link Identity}.
34
+ *
35
+ * @since 0.6.4
36
+ * @author chris
37
+ */
38
+public class ServerEntryReader {
39
+
40
+    /** The identity to read entries from. */
41
+    private final Identity identity;
42
+
43
+    /**
44
+     * Creates a new Server Entry Reader which will read from the specified
45
+     * identity.
46
+     *
47
+     * @param identity The identity which defines our server entries
48
+     */
49
+    public ServerEntryReader(final Identity identity) {
50
+        this.identity = identity;
51
+    }
52
+
53
+    /**
54
+     * Attempts to read the details of the specified server from this reader's
55
+     * identity.
56
+     *
57
+     * @param group The group that owns this server
58
+     * @param name The name of the server to be read
59
+     * @return A corresponding ServerEntry
60
+     * @throws URISyntaxException If the server doesn't specify a valid URI
61
+     * @throws IllegalArgumentException If the server doesn't define a name or address
62
+     */
63
+    public ServerEntry read(final ServerGroup group, final String name) throws URISyntaxException,
64
+            IllegalArgumentException {
65
+        if (!identity.hasOptionString(name, "name")
66
+                || !identity.hasOptionString(name, "address")) {
67
+            throw new IllegalArgumentException("Server does not specify name or address: "
68
+                    + name);
69
+        }
70
+
71
+        final String serverName = identity.getOption(name, "name");
72
+        final URI serverURI = new URI(identity.getOption(name, "address"));
73
+
74
+        return new ServerEntry(group, serverName, serverURI, null);
75
+    }
76
+
77
+}

+ 146
- 0
src/com/dmdirc/addons/serverlists/io/ServerGroupReader.java 查看文件

@@ -0,0 +1,146 @@
1
+/*
2
+ * Copyright (c) 2006-2010 Chris Smith, Shane Mc Cormack, Gregory Holmes
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.addons.serverlists.io;
24
+
25
+import com.dmdirc.config.Identity;
26
+import com.dmdirc.addons.serverlists.ServerGroup;
27
+
28
+import java.net.URI;
29
+import java.net.URISyntaxException;
30
+import java.util.Map;
31
+
32
+/**
33
+ * Facilitates loading of a {@link ServerGroup} from a DMDirc {@link Identity}.
34
+ *
35
+ * @since 0.6.4
36
+ * @author chris
37
+ */
38
+public class ServerGroupReader {
39
+
40
+    /** The identity this reader should read from. */
41
+    private final Identity identity;
42
+
43
+    /** The reader we'll use for individual servers. */
44
+    private final ServerEntryReader entryReader;
45
+
46
+    /**
47
+     * Creates a new ServerGroupReader that will read from the specified
48
+     * identity.
49
+     *
50
+     * @param identity The identity describing the server group
51
+     */
52
+    public ServerGroupReader(final Identity identity) {
53
+        this.identity = identity;
54
+        this.entryReader = new ServerEntryReader(identity);
55
+    }
56
+
57
+    /**
58
+     * Reads the default server group from this reader's identity.
59
+     *
60
+     * @see #read(java.lang.String)
61
+     * @return A ServerGroup corresponding to the identity's default group
62
+     * @throws IllegalArgumentException If the identity doesn't define a group
63
+     */
64
+    public ServerGroup read() {
65
+        if (identity.hasOptionString("identity", "name")) {
66
+            return read(null, identity.getOption("identity", "name"));
67
+        }
68
+
69
+        throw new IllegalArgumentException("Identity has no name");
70
+    }
71
+
72
+    /**
73
+     * Reads a named server group from this reader's identity.
74
+     *
75
+     * @param parent The parent of the group being read
76
+     * @param name The name of the server group to read
77
+     * @return A corresponding ServerGroup
78
+     * @throws IllegalArgumentException If the server group doesn't define a name
79
+     */
80
+    public ServerGroup read(final ServerGroup parent, final String name)
81
+            throws IllegalArgumentException {
82
+        if (!identity.hasOptionString(name, "name")) {
83
+            throw new IllegalArgumentException("ServerGroup '" + name + "' not defined");
84
+        }
85
+
86
+        final ServerGroup group = new ServerGroup(parent,
87
+                identity.getOption(name, "name"));
88
+
89
+        if (identity.hasOptionString(name, "description")) {
90
+            group.setDescription(identity.getOption(name, "description"));
91
+        }
92
+
93
+        if (identity.hasOptionString(name, "links")) {
94
+            readLinks(group, identity.getOption(name, "links"));
95
+        }
96
+
97
+        for (String item : identity.getOptionList(name, "contents", true)) {
98
+            if (item.endsWith(" servergroup")) {
99
+                try {
100
+                    group.addItem(read(group, item));
101
+                } catch (IllegalArgumentException ex) {
102
+                    // TODO: Raise an error about malformed group
103
+                }
104
+            } else if (item.endsWith(" server")) {
105
+                try {
106
+                    group.addItem(entryReader.read(group, item));
107
+                } catch (URISyntaxException ex) {
108
+                    // TODO: Raise an error about malformed server
109
+                } catch (IllegalArgumentException ex) {
110
+                    // TODO: Raise an error about malformed server
111
+                }
112
+            }
113
+
114
+            // TODO: Raise an error about unknown content?
115
+        }
116
+
117
+        return group;
118
+    }
119
+
120
+    /**
121
+     * Reads a set of links from the named domain and adds them to the specified
122
+     * group.
123
+     *
124
+     * @param group The group to add links to
125
+     * @param domain The domain in the identity containing links
126
+     */
127
+    private void readLinks(final ServerGroup group, final String domain) {
128
+        for (Map.Entry<String, String> entry : identity.getOptions(domain).entrySet()) {
129
+            try {
130
+                group.addLink(entry.getKey(), new URI(entry.getValue()));
131
+            } catch (URISyntaxException ex) {
132
+                // TODO: Raise an error about illegal URI?
133
+            }
134
+        }
135
+    }
136
+
137
+    /**
138
+     * Returns a writer which may be used to write updated data.
139
+     *
140
+     * @return An appropriately configured {@link ServerGroupWriter}
141
+     */
142
+    public ServerGroupWriter getWriter() {
143
+        return new ServerGroupWriter(identity);
144
+    }
145
+
146
+}

+ 168
- 0
src/com/dmdirc/addons/serverlists/io/ServerGroupWriter.java 查看文件

@@ -0,0 +1,168 @@
1
+/*
2
+ * Copyright (c) 2006-2010 Chris Smith, Shane Mc Cormack, Gregory Holmes
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.addons.serverlists.io;
24
+
25
+import com.dmdirc.config.Identity;
26
+import com.dmdirc.addons.serverlists.ServerEntry;
27
+import com.dmdirc.addons.serverlists.ServerGroup;
28
+import com.dmdirc.addons.serverlists.ServerGroupItem;
29
+
30
+import java.net.URI;
31
+import java.util.ArrayList;
32
+import java.util.List;
33
+import java.util.Map;
34
+import java.util.Set;
35
+
36
+/**
37
+ * Provides methods to instantiate a writer for a server group or entry.
38
+ *
39
+ * @since 0.6.4
40
+ * @author chris
41
+ */
42
+public class ServerGroupWriter {
43
+
44
+    /** The identity which will be written to. */
45
+    private final Identity identity;
46
+
47
+    /**
48
+     * Creates a new writer which will write to the specified identity.
49
+     *
50
+     * @param identity The identity to write the server group to
51
+     */
52
+    public ServerGroupWriter(final Identity identity) {
53
+        this.identity = identity;
54
+    }
55
+
56
+    /**
57
+     * Writes the specified server group and all of its children to this
58
+     * writer's identity.
59
+     *
60
+     * @param group The group to be written
61
+     */
62
+    public void write(final ServerGroup group) {
63
+        final Set<String> sections = identity.getDomains();
64
+
65
+        identity.setOption("identity", "name", group.getName() + " servergroup");
66
+
67
+        writeGroup(group, sections);
68
+
69
+        for (String missing : sections) {
70
+            for (String setting : identity.getOptions(missing).keySet()) {
71
+                identity.unsetOption(missing, setting);
72
+            }
73
+        }
74
+    }
75
+
76
+    /**
77
+     * Appends the specified group to this writer's identity. Any keysections
78
+     * that are used will be removed from the sections set, so callers can
79
+     * track which sections in the identity have not been used.
80
+     *
81
+     * @param group The group to be written
82
+     * @param sections A set of sections from which any new keysections
83
+     * should be removed
84
+     */
85
+    protected void writeGroup(final ServerGroup group, final Set<String> sections) {
86
+        final String domain = group.getName() + " servergroup";
87
+
88
+        final List<String> children = new ArrayList<String>();
89
+
90
+        for (ServerGroupItem item : group.getItems()) {
91
+            if (item instanceof ServerGroup) {
92
+                writeGroup((ServerGroup) item, sections);
93
+                children.add(item.getName() + " servergroup");
94
+            } else if (item instanceof ServerEntry) {
95
+                writeEntry((ServerEntry) item, sections);
96
+                children.add(item.getName() + " server");
97
+            }
98
+        }
99
+
100
+        final boolean needsUpdating = group.isModified();
101
+        sections.remove(domain);
102
+        group.setModified(false);
103
+
104
+        if (!needsUpdating) {
105
+            return;
106
+        }
107
+
108
+        if (!group.getLinks().isEmpty()) {
109
+            writeLinks(group.getName(), group.getLinks());
110
+            sections.remove(group + " links");
111
+        }
112
+
113
+        if (group.getNetwork() == null) {
114
+            identity.unsetOption(domain, "network");
115
+        } else {
116
+            identity.setOption(domain, "network", group.getNetwork());
117
+        }
118
+
119
+        identity.setOption(domain, "description", group.getDescription());
120
+        identity.setOption(domain, "contents", children);
121
+        identity.setOption(domain, "name", group.getName());
122
+    }
123
+
124
+    /**
125
+     * Writes a map of links for the specified entry.
126
+     *
127
+     * @param name The name of the entry for which to write the links
128
+     * @param links The links to be written
129
+     */
130
+    protected void writeLinks(final String name, final Map<String, URI> links) {
131
+        final String domain = name + " links";
132
+        final Set<String> existing = identity.getOptions(domain).keySet();
133
+        existing.removeAll(links.keySet());
134
+
135
+        for (String deleted : existing) {
136
+            identity.unsetOption(domain, deleted);
137
+        }
138
+
139
+        for (Map.Entry<String, URI> link : links.entrySet()) {
140
+            identity.setOption(domain, link.getKey(), link.getValue().toString());
141
+        }
142
+    }
143
+
144
+    /**
145
+     * Writes the specified entry to this writer's identity. Any keysections
146
+     * that are used will be removed from the sections set, so callers can
147
+     * track which sections in the identity have not been used.
148
+     *
149
+     * @param entry The entry to be written
150
+     * @param sections A set of sections from which any new keysections
151
+     * should be removed
152
+     */
153
+    protected void writeEntry(final ServerEntry entry, final Set<String> sections) {
154
+        final String domain = entry.getName() + " server";
155
+        final boolean needsUpdating = entry.isModified();
156
+
157
+        sections.remove(domain);
158
+        entry.setModified(false);
159
+
160
+        if (!needsUpdating) {
161
+            return;
162
+        }
163
+
164
+        identity.setOption(domain, "name", entry.getName());
165
+        identity.setOption(domain, "address", entry.getAddress().toString());
166
+    }
167
+
168
+}

+ 28
- 0
src/com/dmdirc/addons/serverlists/plugin.config 查看文件

@@ -0,0 +1,28 @@
1
+# This is a DMDirc configuration file.
2
+
3
+# This section indicates which sections below take key/value
4
+# pairs, rather than a simple list. It should be placed above
5
+# any sections that take key/values.
6
+keysections:
7
+  metadata
8
+  updates
9
+  version
10
+
11
+metadata:
12
+  author=Chris <chris@dmdirc.com>
13
+  mainclass=com.dmdirc.addons.serverlists.ServerListPlugin
14
+  description=Support for server lists
15
+  name=serverlists
16
+  nicename=Server List Support
17
+
18
+updates:
19
+  id=59
20
+
21
+version:
22
+  friendly=0.5
23
+
24
+provides:
25
+  serverlist feature
26
+
27
+persistent:
28
+  *

+ 152
- 0
src/com/dmdirc/addons/serverlists/service/ServerListServiceProvider.java 查看文件

@@ -0,0 +1,152 @@
1
+/*
2
+ * Copyright (c) 2006-2010 Chris Smith, Shane Mc Cormack, Gregory Holmes
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.addons.serverlists.service;
24
+
25
+import com.dmdirc.ParserFactory;
26
+import com.dmdirc.parser.common.MyInfo;
27
+import com.dmdirc.parser.interfaces.Parser;
28
+import com.dmdirc.plugins.ExportedService;
29
+import com.dmdirc.plugins.PluginManager;
30
+import com.dmdirc.plugins.Service;
31
+import com.dmdirc.plugins.ServiceProvider;
32
+import com.dmdirc.addons.serverlists.ServerGroup;
33
+import com.dmdirc.addons.serverlists.ServerGroupItem;
34
+import com.dmdirc.addons.serverlists.ServerList;
35
+
36
+import java.net.URI;
37
+import java.util.Arrays;
38
+import java.util.List;
39
+
40
+/**
41
+ * Provides a fake parser service which handles <code>serverlist://</code> URIs,
42
+ * and returns a suitable parser for the corresponding server list.
43
+ *
44
+ * @author chris
45
+ * @since 0.6.4
46
+ */
47
+public class ServerListServiceProvider implements ServiceProvider {
48
+
49
+    /** The server list that we're providing for. */
50
+    private final ServerList serverList;
51
+
52
+    /** The services that this provider providers. */
53
+    private final List<Service> services;
54
+
55
+    /**
56
+     * Creates a new server list service provider.
57
+     *
58
+     * @param serverList The {@link ServerList} to retrieve items from
59
+     */
60
+    public ServerListServiceProvider(final ServerList serverList) {
61
+        this.serverList = serverList;
62
+        this.services = Arrays.asList(new Service[] {
63
+           PluginManager.getPluginManager().getService("parser", "serverlist", true)
64
+        });
65
+    }
66
+
67
+    /**
68
+     * Registers this service provider.
69
+     */
70
+    public void register() {
71
+        for (Service service : services) {
72
+            service.addProvider(this);
73
+        }
74
+    }
75
+
76
+    /** {@inheritDoc} */
77
+    @Override
78
+    public boolean isActive() {
79
+        return true;
80
+    }
81
+
82
+    /** {@inheritDoc} */
83
+    @Override
84
+    public void activateServices() {
85
+        // Do nothing
86
+    }
87
+
88
+    /** {@inheritDoc} */
89
+    @Override
90
+    public List<Service> getServices() {
91
+        return services;
92
+    }
93
+
94
+    /** {@inheritDoc} */
95
+    @Override
96
+    public String getProviderName() {
97
+        return "Serverlist service provider";
98
+    }
99
+
100
+    /** {@inheritDoc} */
101
+    @Override
102
+    public ExportedService getExportedService(final String name) {
103
+        if ("getParser".equals(name)) {
104
+            return new ExportedService(ServerListServiceProvider.class, "getParser", this);
105
+        } else {
106
+            return new ExportedService(null, null);
107
+        }
108
+    }
109
+
110
+    /**
111
+     * Retrieves a parser for the specified details.
112
+     *
113
+     * @param myInfo The user-supplied client identification information
114
+     * @param address The address to connect to (a serverlist:// URI)
115
+     * @return A corresponding Parser instance, or null if none applicable
116
+     */
117
+    public Parser getParser(final MyInfo myInfo, final URI address) {
118
+        ServerGroup group = serverList.getGroupByName(address.getHost());
119
+
120
+        if (address.getPath() != null && !address.getPath().isEmpty()) {
121
+            for (String part : address.getPath().split("/")) {
122
+                if (part.isEmpty()) {
123
+                    continue;
124
+                }
125
+
126
+                final ServerGroupItem item = group.getItemByName(part);
127
+
128
+                if (item == null) {
129
+                    return null;
130
+                } else if (item instanceof ServerGroup) {
131
+                    group = (ServerGroup) item;
132
+                } else {
133
+                    return getParserForItem(myInfo, item);
134
+                }
135
+            }
136
+        }
137
+
138
+        return getParserForItem(myInfo, group);
139
+    }
140
+
141
+    /**
142
+     * Retrieves a parser for the specified item.
143
+     *
144
+     * @param myInfo The user-supplied client identification information
145
+     * @param item The item to retrieve a URI from
146
+     * @return A corresponding parser instance
147
+     */
148
+    protected Parser getParserForItem(final MyInfo myInfo, final ServerGroupItem item) {
149
+        return new ParserFactory().getParser(myInfo, item.getAddress());
150
+    }
151
+
152
+}

+ 1
- 1
src/com/dmdirc/addons/ui_swing/components/renderers/ServerGroupTreeRenderer.java 查看文件

@@ -22,7 +22,7 @@
22 22
 
23 23
 package com.dmdirc.addons.ui_swing.components.renderers;
24 24
 
25
-import com.dmdirc.serverlists.ServerGroupItem;
25
+import com.dmdirc.addons.serverlists.ServerGroupItem;
26 26
 
27 27
 import java.awt.Color;
28 28
 import java.awt.Component;

+ 1
- 1
src/com/dmdirc/addons/ui_swing/dialogs/serverlist/AddEntryInputDialog.java 查看文件

@@ -29,7 +29,7 @@ import com.dmdirc.addons.ui_swing.dialogs.StandardInputDialog;
29 29
 import com.dmdirc.config.prefs.validator.NotEmptyValidator;
30 30
 import com.dmdirc.config.prefs.validator.URIValidator;
31 31
 import com.dmdirc.config.prefs.validator.Validator;
32
-import com.dmdirc.serverlists.ServerGroup;
32
+import com.dmdirc.addons.serverlists.ServerGroup;
33 33
 
34 34
 import java.awt.Window;
35 35
 import java.awt.Dialog.ModalityType;

+ 1
- 1
src/com/dmdirc/addons/ui_swing/dialogs/serverlist/AddGroupInputDialog.java 查看文件

@@ -28,7 +28,7 @@ import com.dmdirc.addons.ui_swing.dialogs.StandardDialog;
28 28
 import com.dmdirc.addons.ui_swing.dialogs.StandardInputDialog;
29 29
 import com.dmdirc.config.prefs.validator.NotEmptyValidator;
30 30
 import com.dmdirc.config.prefs.validator.Validator;
31
-import com.dmdirc.serverlists.ServerGroup;
31
+import com.dmdirc.addons.serverlists.ServerGroup;
32 32
 
33 33
 import java.awt.Window;
34 34
 import java.awt.Dialog.ModalityType;

+ 1
- 1
src/com/dmdirc/addons/ui_swing/dialogs/serverlist/Info.java 查看文件

@@ -24,7 +24,7 @@ package com.dmdirc.addons.ui_swing.dialogs.serverlist;
24 24
 
25 25
 import com.dmdirc.addons.ui_swing.UIUtilities;
26 26
 import com.dmdirc.addons.ui_swing.components.text.HTMLLabel;
27
-import com.dmdirc.serverlists.ServerGroupItem;
27
+import com.dmdirc.addons.serverlists.ServerGroupItem;
28 28
 import com.dmdirc.ui.core.util.URLHandler;
29 29
 
30 30
 import java.net.URI;

+ 1
- 1
src/com/dmdirc/addons/ui_swing/dialogs/serverlist/Perform.java 查看文件

@@ -23,7 +23,7 @@
23 23
 package com.dmdirc.addons.ui_swing.dialogs.serverlist;
24 24
 
25 25
 import com.dmdirc.addons.ui_swing.components.performpanel.PerformPanel;
26
-import com.dmdirc.serverlists.ServerGroupItem;
26
+import com.dmdirc.addons.serverlists.ServerGroupItem;
27 27
 
28 28
 import javax.swing.BorderFactory;
29 29
 import javax.swing.JPanel;

+ 1
- 1
src/com/dmdirc/addons/ui_swing/dialogs/serverlist/Profiles.java 查看文件

@@ -25,7 +25,7 @@ package com.dmdirc.addons.ui_swing.dialogs.serverlist;
25 25
 import com.dmdirc.addons.ui_swing.components.vetoable.VetoableComboBoxModel;
26 26
 import com.dmdirc.config.Identity;
27 27
 import com.dmdirc.config.IdentityManager;
28
-import com.dmdirc.serverlists.ServerGroupItem;
28
+import com.dmdirc.addons.serverlists.ServerGroupItem;
29 29
 
30 30
 import java.util.HashMap;
31 31
 import java.util.List;

+ 1
- 1
src/com/dmdirc/addons/ui_swing/dialogs/serverlist/ServerListDialog.java 查看文件

@@ -24,7 +24,7 @@ package com.dmdirc.addons.ui_swing.dialogs.serverlist;
24 24
 
25 25
 import com.dmdirc.addons.ui_swing.components.LockedLayer;
26 26
 import com.dmdirc.addons.ui_swing.dialogs.StandardDialog;
27
-import com.dmdirc.serverlists.ServerGroupItem;
27
+import com.dmdirc.addons.serverlists.ServerGroupItem;
28 28
 
29 29
 import java.awt.Window;
30 30
 import java.awt.color.ColorSpace;

+ 1
- 1
src/com/dmdirc/addons/ui_swing/dialogs/serverlist/ServerListListener.java 查看文件

@@ -22,7 +22,7 @@
22 22
 
23 23
 package com.dmdirc.addons.ui_swing.dialogs.serverlist;
24 24
 
25
-import com.dmdirc.serverlists.ServerGroupItem;
25
+import com.dmdirc.addons.serverlists.ServerGroupItem;
26 26
 
27 27
 /**
28 28
  * Server list listener.

+ 4
- 4
src/com/dmdirc/addons/ui_swing/dialogs/serverlist/ServerListModel.java 查看文件

@@ -26,10 +26,10 @@ import com.dmdirc.actions.wrappers.PerformWrapper.PerformDescription;
26 26
 import com.dmdirc.actions.wrappers.PerformType;
27 27
 import com.dmdirc.logger.ErrorLevel;
28 28
 import com.dmdirc.logger.Logger;
29
-import com.dmdirc.serverlists.ServerEntry;
30
-import com.dmdirc.serverlists.ServerGroup;
31
-import com.dmdirc.serverlists.ServerGroupItem;
32
-import com.dmdirc.serverlists.ServerList;
29
+import com.dmdirc.addons.serverlists.ServerEntry;
30
+import com.dmdirc.addons.serverlists.ServerGroup;
31
+import com.dmdirc.addons.serverlists.ServerGroupItem;
32
+import com.dmdirc.addons.serverlists.ServerList;
33 33
 import com.dmdirc.util.ListenerList;
34 34
 
35 35
 import java.io.IOException;

+ 2
- 2
src/com/dmdirc/addons/ui_swing/dialogs/serverlist/Settings.java 查看文件

@@ -25,8 +25,8 @@ package com.dmdirc.addons.ui_swing.dialogs.serverlist;
25 25
 import com.dmdirc.addons.ui_swing.components.expandingsettings.SettingsPanel;
26 26
 import com.dmdirc.addons.ui_swing.components.expandingsettings.SettingsPanel.OptionType;
27 27
 import com.dmdirc.config.IdentityManager;
28
-import com.dmdirc.serverlists.ServerGroup;
29
-import com.dmdirc.serverlists.ServerGroupItem;
28
+import com.dmdirc.addons.serverlists.ServerGroup;
29
+import com.dmdirc.addons.serverlists.ServerGroupItem;
30 30
 
31 31
 import java.util.HashMap;
32 32
 import java.util.Map;

+ 1
- 1
src/com/dmdirc/addons/ui_swing/dialogs/serverlist/Tree.java 查看文件

@@ -24,7 +24,7 @@ package com.dmdirc.addons.ui_swing.dialogs.serverlist;
24 24
 
25 25
 import com.dmdirc.addons.ui_swing.components.TreeScroller;
26 26
 import com.dmdirc.addons.ui_swing.components.renderers.ServerGroupTreeRenderer;
27
-import com.dmdirc.serverlists.ServerGroupItem;
27
+import com.dmdirc.addons.serverlists.ServerGroupItem;
28 28
 
29 29
 import java.awt.Rectangle;
30 30
 import java.awt.Window;

Loading…
取消
儲存