Browse Source

Server list core merge

Issue 41

Change-Id: Ia3a38a768d0d53974dcca2d44e650e74b553b8dc
Reviewed-on: http://gerrit.dmdirc.com/1234
Automatic-Compile: DMDirc Local Commits <dmdirc@googlemail.com>
Reviewed-by: Gregory Holmes <greg@dmdirc.com>
tags/0.6.4rc1
Chris Smith 14 years ago
parent
commit
a193a7985b

+ 17
- 3
src/com/dmdirc/config/ConfigManager.java View File

@@ -40,7 +40,8 @@ import java.util.logging.Level;
40 40
  *
41 41
  * @author chris
42 42
  */
43
-public class ConfigManager extends ConfigSource implements ConfigChangeListener {
43
+public class ConfigManager extends ConfigSource implements ConfigChangeListener,
44
+        IdentityListener {
44 45
 
45 46
     /** Temporary map for lookup stats. */
46 47
     private static final Map<String, Integer> stats = new TreeMap<String, Integer>();
@@ -244,8 +245,8 @@ public class ConfigManager extends ConfigSource implements ConfigChangeListener
244 245
         case CHANNEL:
245 246
             comp = channel;
246 247
             break;
247
-        case PROFILE:
248
-            // We don't want profiles
248
+        case CUSTOM:
249
+            // We don't want custom identities
249 250
             comp = null;
250 251
             break;
251 252
         default:
@@ -482,4 +483,17 @@ public class ConfigManager extends ConfigSource implements ConfigChangeListener
482 483
             listener.configChanged(domain, key);
483 484
         }
484 485
     }
486
+
487
+    /** {@inheritDoc} */
488
+    @Override
489
+    public void identityAdded(final Identity identity) {
490
+        checkIdentity(identity);
491
+    }
492
+
493
+    /** {@inheritDoc} */
494
+    @Override
495
+    public void identityRemoved(final Identity identity) {
496
+        removeIdentity(identity);
497
+    }
498
+    
485 499
 }

+ 24
- 8
src/com/dmdirc/config/ConfigTarget.java View File

@@ -41,8 +41,6 @@ public class ConfigTarget implements Comparable<ConfigTarget>, Serializable {
41 41
         GLOBAL,
42 42
         /** Settings for a theme. */
43 43
         THEME,
44
-        /** Settings for a profile. */
45
-        PROFILE,
46 44
         /** Settings for an IRCd. */
47 45
         IRCD,
48 46
         /** Settings for a network. */
@@ -53,6 +51,8 @@ public class ConfigTarget implements Comparable<ConfigTarget>, Serializable {
53 51
         CHANNEL,
54 52
         /** Settings for a protocol (parser). */
55 53
         PROTOCOL,
54
+        /** A custom identity, which doesn't contain settings to be loaded. */
55
+        CUSTOM,
56 56
     }
57 57
 
58 58
     /**
@@ -112,10 +112,26 @@ public class ConfigTarget implements Comparable<ConfigTarget>, Serializable {
112 112
         data = "";
113 113
     }
114 114
 
115
-    /** Sets this target to be a profile source. */
116
-    public void setProfile() {
117
-        type = TYPE.PROFILE;
118
-        data = "";
115
+    /**
116
+     * Sets this target to be a custom identity.
117
+     * 
118
+     * @param customType The type of custom identity
119
+     * @since 0.6.4
120
+     */
121
+    public void setCustom(final String customType) {
122
+        type = TYPE.CUSTOM;
123
+        data = customType;
124
+    }
125
+
126
+    /**
127
+     * Determines if this target is the specified custom type.
128
+     *
129
+     * @param customType The type of custom identity
130
+     * @return True if this target is a CUSTOM type with the specified type.
131
+     * @since 0.6.4
132
+     */
133
+    public boolean isCustom(final String customType) {
134
+        return type == TYPE.CUSTOM && customType.equals(data);
119 135
     }
120 136
 
121 137
     /**
@@ -238,8 +254,8 @@ public class ConfigTarget implements Comparable<ConfigTarget>, Serializable {
238 254
             return "Global defaults";
239 255
         case THEME:
240 256
             return "Theme";
241
-        case PROFILE:
242
-            return "Profile";
257
+        case CUSTOM:
258
+            return "Custom: " + data;
243 259
         case IRCD:
244 260
             return "Ircd specific: " + data;
245 261
         case NETWORK:

+ 17
- 4
src/com/dmdirc/config/Identity.java View File

@@ -100,7 +100,7 @@ public class Identity extends ConfigSource implements Comparable<Identity> {
100 100
         initFile(forceDefault);
101 101
         myTarget = getTarget(forceDefault);
102 102
 
103
-        if (myTarget.getType() == ConfigTarget.TYPE.PROFILE) {
103
+        if (myTarget.isCustom(PROFILE_DOMAIN)) {
104 104
             migrateProfile();
105 105
         }
106 106
     }
@@ -123,7 +123,7 @@ public class Identity extends ConfigSource implements Comparable<Identity> {
123 123
         initFile(forceDefault);
124 124
         myTarget = getTarget(forceDefault);
125 125
 
126
-        if (myTarget.getType() == ConfigTarget.TYPE.PROFILE) {
126
+        if (myTarget.isCustom(PROFILE_DOMAIN)) {
127 127
             migrateProfile();
128 128
         }
129 129
     }
@@ -141,7 +141,7 @@ public class Identity extends ConfigSource implements Comparable<Identity> {
141 141
         this.file.setAutomake(true);
142 142
         this.myTarget = target;
143 143
 
144
-        if (myTarget.getType() == ConfigTarget.TYPE.PROFILE) {
144
+        if (myTarget.isCustom(PROFILE_DOMAIN)) {
145 145
             migrateProfile();
146 146
         }
147 147
     }
@@ -172,7 +172,9 @@ public class Identity extends ConfigSource implements Comparable<Identity> {
172 172
         } else if (hasOption(DOMAIN, "global") || (forceDefault && !isProfile())) {
173 173
             target.setGlobal();
174 174
         } else if (isProfile()) {
175
-            target.setProfile();
175
+            target.setCustom(PROFILE_DOMAIN);
176
+        } else if (hasOption(DOMAIN, "type")) {
177
+            target.setCustom(getOption(DOMAIN, "type"));
176 178
         } else {
177 179
             throw new InvalidIdentityFileException("No target and no profile");
178 180
         }
@@ -566,6 +568,17 @@ public class Identity extends ConfigSource implements Comparable<Identity> {
566 568
         return file;
567 569
     }
568 570
 
571
+    /**
572
+     * Determines if this identity is loaded from the specified File.
573
+     *
574
+     * @param target The file to be checked
575
+     * @return True if this identity comes from the target file, false otherwise
576
+     * @since 0.6.4
577
+     */
578
+    public boolean isFile(final File target) {
579
+        return file != null && file.getFile() != null && file.getFile().equals(target);
580
+    }
581
+
569 582
     /**
570 583
      * Adds a new config change listener for this identity.
571 584
      *

+ 50
- 0
src/com/dmdirc/config/IdentityListener.java View File

@@ -0,0 +1,50 @@
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.config;
24
+
25
+/**
26
+ * An interface of objects which are interested in identities being added
27
+ * and removed from the {@link IdentityManager}.
28
+ *
29
+ * @since 0.6.4
30
+ * @author chris
31
+ */
32
+public interface IdentityListener {
33
+
34
+    /**
35
+     * Called whenever a relevant new identity is added to the
36
+     * {@link IdentityManager}.
37
+     *
38
+     * @param identity The identity which has been added
39
+     */
40
+    void identityAdded(final Identity identity);
41
+
42
+    /**
43
+     * Called whenever a relevant identity is removed from the
44
+     * {@link IdentityManager}.
45
+     *
46
+     * @param identity The identity which has been removed
47
+     */
48
+    void identityRemoved(final Identity identity);
49
+
50
+}

+ 125
- 48
src/com/dmdirc/config/IdentityManager.java View File

@@ -29,7 +29,8 @@ import com.dmdirc.logger.Logger;
29 29
 import com.dmdirc.updater.Version;
30 30
 import com.dmdirc.util.ConfigFile;
31 31
 import com.dmdirc.util.InvalidConfigFileException;
32
-import com.dmdirc.util.WeakList;
32
+import com.dmdirc.util.MapList;
33
+import com.dmdirc.util.WeakMapList;
33 34
 import com.dmdirc.util.resourcemanager.ResourceManager;
34 35
 
35 36
 import java.io.File;
@@ -37,8 +38,10 @@ import java.io.IOException;
37 38
 import java.util.ArrayList;
38 39
 import java.util.Collections;
39 40
 import java.util.HashMap;
41
+import java.util.LinkedHashSet;
40 42
 import java.util.List;
41 43
 import java.util.Map;
44
+import java.util.Set;
42 45
     
43 46
 /**
44 47
  * The identity manager manages all known identities, providing easy methods
@@ -48,11 +51,23 @@ import java.util.Map;
48 51
  */
49 52
 public final class IdentityManager {
50 53
     
51
-    /** The identities that have been loaded into this manager. */
52
-    private final static List<Identity> identities = new ArrayList<Identity>();
54
+    /**
55
+     * The identities that have been loaded into this manager.
56
+     * 
57
+     * Standard identities are inserted with a <code>null</code> key, custom
58
+     * identities use their custom type as the key.
59
+     */
60
+    private static final MapList<String, Identity> identities
61
+            = new MapList<String, Identity>();
53 62
     
54
-    /** The config managers that have registered with this manager. */
55
-    private final static List<ConfigManager> managers = new WeakList<ConfigManager>();
63
+    /**
64
+     * The {@link IdentityListener}s that have registered with this manager.
65
+     * 
66
+     * Listeners for standard identities are inserted with a <code>null</code>
67
+     * key, listeners for a specific custom type use their type as the key.
68
+     */
69
+    private static final MapList<String, IdentityListener> listeners
70
+            = new WeakMapList<String, IdentityListener>();
56 71
 
57 72
     /** A logger for this class. */
58 73
     private static final java.util.logging.Logger LOGGER = java.util.logging
@@ -82,12 +97,7 @@ public final class IdentityManager {
82 97
      */
83 98
     public static void load() throws InvalidIdentityFileException {
84 99
         identities.clear();
85
-        managers.clear();
86
-        
87
-        if (globalconfig != null) {
88
-            // May have been created earlier
89
-            managers.add(globalconfig);
90
-        }
100
+        identities.clear();
91 101
 
92 102
         loadVersion();
93 103
         loadDefaults();
@@ -258,11 +268,10 @@ public final class IdentityManager {
258 268
      * 
259 269
      * @param file The file to load the identity from.
260 270
      */
261
-    @SuppressWarnings("deprecation")
262 271
     private static void loadIdentity(final File file) {
263 272
         synchronized (identities) {
264
-            for (Identity identity : identities) {
265
-                if (file.equals(identity.getFile().getFile())) {
273
+            for (Identity identity : getAllIdentities()) {
274
+                if (identity.isFile(file)) {
266 275
                     try {
267 276
                         identity.reload();
268 277
                     } catch (IOException ex) {
@@ -291,6 +300,36 @@ public final class IdentityManager {
291 300
         }
292 301
     }
293 302
 
303
+    /**
304
+     * Retrieves all known identities.
305
+     *
306
+     * @return A set of all known identities
307
+     * @since 0.6.4
308
+     */
309
+    private static Set<Identity> getAllIdentities() {
310
+        final Set<Identity> res = new LinkedHashSet<Identity>();
311
+
312
+        for (Map.Entry<String, List<Identity>> entry : identities.entrySet()) {
313
+            res.addAll(entry.getValue());
314
+        }
315
+
316
+        return res;
317
+    }
318
+
319
+    /**
320
+     * Returns the "group" to which the specified identity belongs. For custom
321
+     * identities this is the custom identity type, otherwise this is
322
+     * <code>null</code>.
323
+     *
324
+     * @param identity The identity whose group is being retrieved
325
+     * @return The group of the specified identity
326
+     * @since 0.6.4
327
+     */
328
+    private static String getGroup(final Identity identity) {
329
+        return identity.getTarget().getType() == ConfigTarget.TYPE.CUSTOM
330
+                ? identity.getTarget().getData() : null;
331
+    }
332
+
294 333
     /** Loads the version information. */
295 334
     public static void loadVersion() {
296 335
         try {
@@ -307,7 +346,7 @@ public final class IdentityManager {
307 346
      * Loads the config identity.
308 347
      * 
309 348
      * @throws InvalidIdentityFileException if there is a problem with the
310
-     *                                      config file.
349
+     * config file.
311 350
      */
312 351
     private static void loadConfig() throws InvalidIdentityFileException {
313 352
         try {
@@ -360,7 +399,7 @@ public final class IdentityManager {
360 399
      */
361 400
     public static void save() {
362 401
         synchronized (identities) {
363
-            for (Identity identity : identities) {
402
+            for (Identity identity : getAllIdentities()) {
364 403
                 identity.save();
365 404
             }
366 405
         }
@@ -373,20 +412,22 @@ public final class IdentityManager {
373 412
     @Precondition("The specified Identity is not null")
374 413
     public static void addIdentity(final Identity identity) {
375 414
         Logger.assertTrue(identity != null);
376
-        
377
-        if (identities.contains(identity)) {
415
+
416
+        final String target = getGroup(identity);
417
+
418
+        if (identities.containsValue(target, identity)) {
378 419
             removeIdentity(identity);
379 420
         }
380
-        
421
+
381 422
         synchronized (identities) {
382
-            identities.add(identity);
423
+            identities.add(target, identity);
383 424
         }
384 425
 
385
-        LOGGER.finer("Adding identity: " + identity);
386
-        
387
-        synchronized (managers) {
388
-            for (ConfigManager manager : managers) {
389
-                manager.checkIdentity(identity);
426
+        LOGGER.finer("Adding identity: " + identity + " (group: " + target + ")");
427
+
428
+        synchronized (listeners) {
429
+            for (IdentityListener listener : listeners.safeGet(target)) {
430
+                listener.identityAdded(identity);
390 431
             }
391 432
         }
392 433
     }
@@ -401,48 +442,84 @@ public final class IdentityManager {
401 442
     })
402 443
     public static void removeIdentity(final Identity identity) {
403 444
         Logger.assertTrue(identity != null);
404
-        Logger.assertTrue(identities.contains(identity));
445
+
446
+        final String group = getGroup(identity);
447
+
448
+        Logger.assertTrue(identities.containsValue(group, identity));
405 449
         
406 450
         synchronized (identities) {
407
-            identities.remove(identity);
451
+            identities.remove(group, identity);
408 452
         }
409 453
         
410
-        synchronized (managers) {
411
-            for (ConfigManager manager : managers) {
412
-                manager.removeIdentity(identity);
454
+        synchronized (listeners) {
455
+            for (IdentityListener listener : listeners.safeGet(group)) {
456
+                listener.identityRemoved(identity);
413 457
             }
414 458
         }
415 459
     }
416 460
     
417 461
     /**
418 462
      * Adds a config manager to this manager.
463
+     *
419 464
      * @param manager The ConfigManager to add
465
+     * @deprecated Use {@link #addIdentityListener(com.dmdirc.config.IdentityListener)}
420 466
      */
467
+    @Deprecated
421 468
     @Precondition("The specified ConfigManager is not null")
422 469
     public static void addConfigManager(final ConfigManager manager) {
423
-        Logger.assertTrue(manager != null);
470
+        addIdentityListener(manager);
471
+    }
472
+
473
+    /**
474
+     * Adds a new identity listener which will be informed of all settings
475
+     * identities which are added to this manager.
476
+     *
477
+     * @param listener The listener to be added
478
+     * @since 0.6.4
479
+     */
480
+    @Precondition("The specified listener is not null")
481
+    public static void addIdentityListener(final IdentityListener listener) {
482
+        addIdentityListener(null, listener);
483
+    }
484
+
485
+    /**
486
+     * Adds a new identity listener which will be informed of all identities
487
+     * of the specified custom type which are added to this manager.
488
+     *
489
+     * @param type The type of identities to listen for
490
+     * @param listener The listener to be added
491
+     * @since 0.6.4
492
+     */
493
+    @Precondition("The specified listener is not null")
494
+    public static void addIdentityListener(final String type, final IdentityListener listener) {
495
+        Logger.assertTrue(listener != null);
424 496
         
425
-        synchronized (managers) {
426
-            managers.add(manager);
497
+        synchronized (listeners) {
498
+            listeners.add(type, listener);
427 499
         }
428 500
     }
429 501
     
430 502
     /**
431 503
      * Retrieves a list of identities that serve as profiles.
504
+     *
432 505
      * @return A list of profiles
506
+     * @deprecated Use {@link #getCustomIdentities(java.lang.String)} with
507
+     * an argument of <code>profile</code> to retrieve profiles.
433 508
      */
509
+    @Deprecated
434 510
     public static List<Identity> getProfiles() {
435
-        final List<Identity> profiles = new ArrayList<Identity>();
436
-        
437
-        synchronized (identities) {
438
-            for (Identity identity : identities) {
439
-                if (identity.isProfile()) {
440
-                    profiles.add(identity);
441
-                }
442
-            }
443
-        }
444
-        
445
-        return profiles;
511
+        return getCustomIdentities("profile");
512
+    }
513
+
514
+    /**
515
+     * Retrieves a list of identities that belong to the specified custom type.
516
+     *
517
+     * @param type The type of identity to search for
518
+     * @return A list of matching identities
519
+     * @since 0.6.4
520
+     */
521
+    public static List<Identity> getCustomIdentities(final String type) {
522
+        return Collections.unmodifiableList(identities.safeGet(type));
446 523
     }
447 524
     
448 525
     /**
@@ -457,7 +534,7 @@ public final class IdentityManager {
457 534
         final List<Identity> sources = new ArrayList<Identity>();
458 535
         
459 536
         synchronized (identities) {
460
-            for (Identity identity : identities) {
537
+            for (Identity identity : identities.safeGet(null)) {
461 538
                 if (manager.identityApplies(identity)) {
462 539
                     sources.add(identity);
463 540
                 }
@@ -508,7 +585,7 @@ public final class IdentityManager {
508 585
         final String myTarget = (channel + "@" + network).toLowerCase();
509 586
         
510 587
         synchronized (identities) {
511
-            for (Identity identity : identities) {
588
+            for (Identity identity : identities.safeGet(null)) {
512 589
                 if (identity.getTarget().getType() == ConfigTarget.TYPE.CHANNEL
513 590
                         && identity.getTarget().getData().equalsIgnoreCase(myTarget)) {
514 591
                     return identity;
@@ -545,7 +622,7 @@ public final class IdentityManager {
545 622
         final String myTarget = network.toLowerCase();
546 623
         
547 624
         synchronized (identities) {
548
-            for (Identity identity : identities) {
625
+            for (Identity identity : identities.safeGet(null)) {
549 626
                 if (identity.getTarget().getType() == ConfigTarget.TYPE.NETWORK
550 627
                         && identity.getTarget().getData().equalsIgnoreCase(myTarget)) {
551 628
                     return identity;
@@ -582,7 +659,7 @@ public final class IdentityManager {
582 659
         final String myTarget = server.toLowerCase();
583 660
         
584 661
         synchronized (identities) {
585
-            for (Identity identity : identities) {
662
+            for (Identity identity : identities.safeGet(null)) {
586 663
                 if (identity.getTarget().getType() == ConfigTarget.TYPE.SERVER
587 664
                         && identity.getTarget().getData().equalsIgnoreCase(myTarget)) {
588 665
                     return identity;

+ 113
- 0
src/com/dmdirc/serverlists/ServerEntry.java View File

@@ -0,0 +1,113 @@
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.serverlists;
24
+
25
+import com.dmdirc.Server;
26
+import com.dmdirc.config.Identity;
27
+import com.dmdirc.config.IdentityManager;
28
+import java.net.URI;
29
+
30
+/**
31
+ * Describes an entry for a server within a {@link ServerGroup}.
32
+ *
33
+ * @since 0.6.4
34
+ * @author chris
35
+ */
36
+public class ServerEntry implements ServerGroupItem {
37
+
38
+    /** The address of the server in question. */
39
+    private URI address;
40
+    /** The user-friendly name of the server. */
41
+    private String name;
42
+    /** The name of the profile to use. */
43
+    private String profile;
44
+
45
+    /**
46
+     * Creates a new server entry.
47
+     *
48
+     * @param name The name of this server
49
+     * @param address The address of this server
50
+     * @param profile The name of the profile to be used by this server
51
+     */
52
+    public ServerEntry(final String name, final URI address, final String profile) {
53
+        this.name = name;
54
+        this.address = address;
55
+        this.profile = profile;
56
+    }
57
+
58
+    /** {@inheritDoc} */
59
+    @Override
60
+    public String getName() {
61
+        return name;
62
+    }
63
+
64
+    /**
65
+     * Retrieves the address used by this server.
66
+     *
67
+     * @return This server's address
68
+     */
69
+    @Override
70
+    public URI getAddress() {
71
+        return address;
72
+    }
73
+
74
+    /**
75
+     * Retrieves the name of the profile which should be used when connecting
76
+     * to this server.
77
+     *
78
+     * @return The profile name used by this entry
79
+     */
80
+    public String getProfile() {
81
+        return profile;
82
+    }
83
+
84
+    /** {@inheritDoc} */
85
+    @Override
86
+    public void connect() {
87
+        final Server server = new Server(address, getProfileIdentity());
88
+        server.connect();
89
+    }
90
+
91
+    /**
92
+     * Returns the {@link Identity} which corresponds to this server's desired
93
+     * profile.
94
+     *
95
+     * @return This server's profile identity
96
+     */
97
+    protected Identity getProfileIdentity() {
98
+        for (Identity identity : IdentityManager.getProfiles()) {
99
+            if (profile.equals(identity.getName())) {
100
+                return identity;
101
+            }
102
+        }
103
+
104
+        return IdentityManager.getProfiles().get(0);
105
+    }
106
+
107
+    /** {@inheritDoc} */
108
+    @Override
109
+    public String toString() {
110
+        return "[" + getName() + ": address: " + getAddress() + "]";
111
+    }
112
+
113
+}

+ 186
- 0
src/com/dmdirc/serverlists/ServerGroup.java View File

@@ -0,0 +1,186 @@
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.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 implements ServerGroupItem {
40
+
41
+    /** The name of the group. */
42
+    private String name;
43
+
44
+    /** A description of this group. */
45
+    private String description = "";
46
+
47
+    /** A set of links relevant to this group (e.g. homepages). */
48
+    private Map<String, URI> links = new HashMap<String, URI>();
49
+    
50
+    /** The items contained within the group. */
51
+    private final List<ServerGroupItem> entries = new ArrayList<ServerGroupItem>();
52
+
53
+    /**
54
+     * Creates a new server group with the specified name.
55
+     *
56
+     * @param name The name to be used for this group
57
+     */
58
+    public ServerGroup(final String name) {
59
+        this.name = name;
60
+    }
61
+
62
+    /** {@inheritDoc} */
63
+    @Override
64
+    public String getName() {
65
+        return name;
66
+    }
67
+
68
+    /**
69
+     * Sets the name of this group.
70
+     *
71
+     * @param name The new name for the group
72
+     */
73
+    public void setName(final String name) {
74
+        this.name = name;
75
+    }
76
+
77
+    /**
78
+     * Adds a new item to this server group.
79
+     *
80
+     * @param item The item to be added
81
+     */
82
+    public void addItem(final ServerGroupItem item) {
83
+        entries.add(item);
84
+    }
85
+
86
+    /**
87
+     * Retrieves a list of items belonging to this group.
88
+     *
89
+     * @return An immutable list of items contained within this group
90
+     */
91
+    public List<ServerGroupItem> getItems() {
92
+        return Collections.unmodifiableList(entries);
93
+    }
94
+
95
+    /**
96
+     * Retrieves a ServerGroupItem with the specified name, if one exists. This
97
+     * method ignores the case of item's name when comparing.
98
+     *
99
+     * @param name The name of the item to be retrieved
100
+     * @return A correspondingly named item, or null if none exists
101
+     */
102
+    public ServerGroupItem getItemByName(final String name) {
103
+        for (ServerGroupItem item : getItems()) {
104
+            if (item.getName().equalsIgnoreCase(name)) {
105
+                return item;
106
+            }
107
+        }
108
+
109
+        return null;
110
+    }
111
+
112
+    /**
113
+     * Retrieves the description of this group.
114
+     * 
115
+     * @return This group's description
116
+     */
117
+    public String getDescription() {
118
+        return description;
119
+    }
120
+
121
+    /**
122
+     * Sets the description of this group.
123
+     *
124
+     * @param description The new description for this group.
125
+     */
126
+    public void setDescription(final String description) {
127
+        this.description = description;
128
+    }
129
+
130
+    /**
131
+     * Retrieves a map of link titles to {@link URI}s which are associated
132
+     * with this server group. Links will typically include network homepages,
133
+     * forums, or support channels.
134
+     * 
135
+     * @return An immutable map of links
136
+     */
137
+    public Map<String, URI> getLinks() {
138
+        return Collections.unmodifiableMap(links);
139
+    }
140
+
141
+    /**
142
+     * Adds a new link with the specified title and address. If a link with the
143
+     * same title existed previously, it will be replaced.
144
+     *
145
+     * @param title The title of the new link
146
+     * @param address The address of the link
147
+     */
148
+    public void addLink(final String title, final URI address) {
149
+        links.put(title, address);
150
+    }
151
+
152
+    /**
153
+     * {@inheritDoc}
154
+     *
155
+     * Current implementation just selects the first item in this group and
156
+     * asks for its URI.
157
+     */
158
+    @Override
159
+    public URI getAddress() {
160
+        if (!entries.isEmpty()) {
161
+            return entries.get(0).getAddress();
162
+        }
163
+
164
+        return null;
165
+    }
166
+
167
+    /**
168
+     * {@inheritDoc}
169
+     * 
170
+     * Current implementation just selects the first item in this group and
171
+     * asks it to connect.
172
+     */
173
+    @Override
174
+    public void connect() {
175
+        if (!entries.isEmpty()) {
176
+            entries.get(0).connect();
177
+        }
178
+    }
179
+
180
+    /** {@inheritDoc} */
181
+    @Override
182
+    public String toString() {
183
+        return "[" + getName() + ": links: " + getLinks() + "; desc: "
184
+                + getDescription() + "; content: " + getItems() + "]";
185
+    }
186
+}

+ 55
- 0
src/com/dmdirc/serverlists/ServerGroupItem.java View File

@@ -0,0 +1,55 @@
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.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 the name of this item.
37
+     *
38
+     * @return A string containing this item's name
39
+     */
40
+    String getName();
41
+
42
+    /**
43
+     * Initiates a connection attempt for this item.
44
+     */
45
+    void connect();
46
+
47
+    /**
48
+     * Retrieves a URI for this item.
49
+     *
50
+     * @return A URI that represents this item or one of its children, or null
51
+     * if the item has no URIs associated with it.
52
+     */
53
+    URI getAddress();
54
+
55
+}

+ 107
- 0
src/com/dmdirc/serverlists/ServerList.java View File

@@ -0,0 +1,107 @@
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.serverlists;
24
+
25
+import com.dmdirc.serverlists.io.ServerGroupReader;
26
+import com.dmdirc.config.Identity;
27
+import com.dmdirc.config.IdentityListener;
28
+import com.dmdirc.config.IdentityManager;
29
+import com.dmdirc.serverlists.service.ServerListServiceProvider;
30
+
31
+import java.util.ArrayList;
32
+import java.util.Collections;
33
+import java.util.List;
34
+
35
+/**
36
+ * Maintains a list of top level {@link ServerGroup}s and handles reading and
37
+ * writing of the lists to disk.
38
+ * 
39
+ * @since 0.6.4
40
+ * @author chris
41
+ */
42
+public class ServerList implements IdentityListener {
43
+
44
+    /** A list of all known groups. */
45
+    private final List<ServerGroup> groups = new ArrayList<ServerGroup>();
46
+
47
+    /**
48
+     * Creates a new ServerList and loads groups and servers.
49
+     */
50
+    public ServerList() {
51
+        IdentityManager.addIdentityListener("servergroup", this);
52
+
53
+        for (Identity identity : IdentityManager.getCustomIdentities("servergroup")) {
54
+            identityAdded(identity);
55
+        }
56
+
57
+        new ServerListServiceProvider(this).register();
58
+    }
59
+
60
+    /**
61
+     * Adds a server group to the master server list.
62
+     *
63
+     * @param group The group to be added
64
+     */
65
+    public void addServerGroup(final ServerGroup group) {
66
+        groups.add(group);
67
+    }
68
+
69
+    /**
70
+     * Retrieves a list of all known server groups.
71
+     *
72
+     * @return An immutable list of server groups.
73
+     */
74
+    public List<ServerGroup> getServerGroups() {
75
+        return Collections.unmodifiableList(groups);
76
+    }
77
+
78
+    /**
79
+     * Retrieves a ServerGroup with the specified name, if one exists. This
80
+     * method ignores the case of group's name when comparing.
81
+     *
82
+     * @param name The name of the group to be retrieved
83
+     * @return A correspondingly named server group, or null if none exists
84
+     */
85
+    public ServerGroup getGroupByName(final String name) {
86
+        for (ServerGroup group : getServerGroups()) {
87
+            if (group.getName().equalsIgnoreCase(name)) {
88
+                return group;
89
+            }
90
+        }
91
+
92
+        return null;
93
+    }
94
+
95
+    /** {@inheritDoc} */
96
+    @Override
97
+    public void identityAdded(final Identity identity) {
98
+        addServerGroup(new ServerGroupReader(identity).read());
99
+    }
100
+
101
+    /** {@inheritDoc} */
102
+    @Override
103
+    public void identityRemoved(final Identity identity) {
104
+        // TODO: Remove server group
105
+    }
106
+
107
+}

+ 75
- 0
src/com/dmdirc/serverlists/io/ServerEntryReader.java View File

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

+ 134
- 0
src/com/dmdirc/serverlists/io/ServerGroupReader.java View File

@@ -0,0 +1,134 @@
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.serverlists.io;
24
+
25
+import com.dmdirc.config.Identity;
26
+import com.dmdirc.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(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 name The name of the server group to read
76
+     * @return A corresponding ServerGroup
77
+     * @throws IllegalArgumentException If the server group doesn't define a name
78
+     */
79
+    public ServerGroup read(final String name) throws IllegalArgumentException {
80
+        if (!identity.hasOptionString(name, "name")) {
81
+            throw new IllegalArgumentException("ServerGroup '" + name + "' not defined");
82
+        }
83
+
84
+        final ServerGroup group = new ServerGroup(identity.getOption(name, "name"));
85
+
86
+        if (identity.hasOptionString(name, "description")) {
87
+            group.setDescription(identity.getOption(name, "description"));
88
+        }
89
+
90
+        if (identity.hasOptionString(name, "links")) {
91
+            readLinks(group, identity.getOption(name, "links"));
92
+        }
93
+
94
+        for (String item : identity.getOptionList(name, "contents", true)) {
95
+            if (item.endsWith(" servergroup")) {
96
+                try {
97
+                    group.addItem(read(item));
98
+                } catch (IllegalArgumentException ex) {
99
+                    // TODO: Raise an error about malformed group
100
+                }
101
+            } else if (item.endsWith(" server")) {
102
+                try {
103
+                    group.addItem(entryReader.read(item));
104
+                } catch (URISyntaxException ex) {
105
+                    // TODO: Raise an error about malformed server
106
+                } catch (IllegalArgumentException ex) {
107
+                    // TODO: Raise an error about malformed server
108
+                }
109
+            }
110
+
111
+            // TODO: Raise an error about unknown content?
112
+        }
113
+
114
+        return group;
115
+    }
116
+
117
+    /**
118
+     * Reads a set of links from the named domain and adds them to the specified
119
+     * group.
120
+     *
121
+     * @param group The group to add links to
122
+     * @param domain The domain in the identity containing links
123
+     */
124
+    private void readLinks(final ServerGroup group, final String domain) {
125
+        for (Map.Entry<String, String> entry : identity.getOptions(domain).entrySet()) {
126
+            try {
127
+                group.addLink(entry.getKey(), new URI(entry.getValue()));
128
+            } catch (URISyntaxException ex) {
129
+                // TODO: Raise an error about illegal URI?
130
+            }
131
+        }
132
+    }
133
+
134
+}

+ 152
- 0
src/com/dmdirc/serverlists/service/ServerListServiceProvider.java View File

@@ -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.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
+
33
+import com.dmdirc.serverlists.ServerGroup;
34
+import com.dmdirc.serverlists.ServerGroupItem;
35
+import com.dmdirc.serverlists.ServerList;
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
+}

Loading…
Cancel
Save