Procházet zdrojové kódy

Initial work on a profile manager.

pull/134/head
Greg Holmes před 9 roky
rodič
revize
b174437f40

+ 2
- 0
src/com/dmdirc/ClientModule.java Zobrazit soubor

@@ -33,6 +33,7 @@ import com.dmdirc.commandparser.aliases.AliasesModule;
33 33
 import com.dmdirc.commandparser.auto.AutoCommandModule;
34 34
 import com.dmdirc.commandparser.commands.CommandModule;
35 35
 import com.dmdirc.config.ConfigModule;
36
+import com.dmdirc.config.profiles.ProfilesModule;
36 37
 import com.dmdirc.interfaces.ActionController;
37 38
 import com.dmdirc.interfaces.CommandController;
38 39
 import com.dmdirc.interfaces.ConnectionFactory;
@@ -73,6 +74,7 @@ import dagger.Provides;
73 74
                 ConfigModule.class,
74 75
                 MessagesModule.class,
75 76
                 PluginModule.class,
77
+                ProfilesModule.class,
76 78
                 UpdaterModule.class
77 79
         },
78 80
         library = true)

+ 4
- 0
src/com/dmdirc/config/profiles/Profile.java Zobrazit soubor

@@ -210,6 +210,10 @@ public class Profile {
210 210
         return hash;
211 211
     }
212 212
 
213
+    public boolean equalsConfigProvider(final ConfigProvider configProvider) {
214
+        return this.configProvider != null && configProvider.equals(configProvider);
215
+    }
216
+
213 217
     @Override
214 218
     public boolean equals(final Object obj) {
215 219
         if (obj == null) {

+ 113
- 0
src/com/dmdirc/config/profiles/ProfileManager.java Zobrazit soubor

@@ -0,0 +1,113 @@
1
+/*
2
+ * Copyright (c) 2006-2014 DMDirc Developers
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+
23
+package com.dmdirc.config.profiles;
24
+
25
+import com.dmdirc.interfaces.config.ConfigProvider;
26
+import com.dmdirc.interfaces.config.ConfigProviderListener;
27
+import com.dmdirc.interfaces.config.IdentityController;
28
+import com.dmdirc.interfaces.config.IdentityFactory;
29
+
30
+import java.util.ArrayList;
31
+import java.util.Collection;
32
+import java.util.Collections;
33
+import java.util.List;
34
+import java.util.stream.Collectors;
35
+
36
+import javax.inject.Inject;
37
+
38
+/**
39
+ * Manager for {@link Profile}s.
40
+ */
41
+public class ProfileManager implements ConfigProviderListener {
42
+
43
+    private final IdentityFactory identityFactory;
44
+    private final IdentityController identityController;
45
+    private final Collection<Profile> profiles;
46
+
47
+    @Inject
48
+    public ProfileManager(final IdentityController identityController,
49
+            final IdentityFactory identityFactory) {
50
+        this.identityFactory = identityFactory;
51
+        this.identityController = identityController;
52
+        profiles = new ArrayList<>();
53
+        final List<ConfigProvider> identities = identityController.getProvidersByType("profile");
54
+        profiles.addAll(identities.stream()
55
+                .map(identity -> new Profile(identityFactory, identity))
56
+                .collect(Collectors.toList()));
57
+    }
58
+
59
+    /**
60
+     * Starts this manager, registering listeners.
61
+     */
62
+    public void start() {
63
+        identityController.registerIdentityListener(this);
64
+    }
65
+
66
+    /**
67
+     * Stops this manager, unregistering listeners.
68
+     */
69
+    public void stop() {
70
+        identityController.unregisterIdentityListener(this);
71
+    }
72
+
73
+    /**
74
+     * Adds a profile to the manager.
75
+     *
76
+     * @param profile New profile to add
77
+     */
78
+    public void addProfile(final Profile profile) {
79
+        profiles.add(profile);
80
+    }
81
+
82
+    /**
83
+     * Removes a profile from the manager.
84
+     *
85
+     * @param profile Profile to be removed
86
+     */
87
+    public void deleteProfile(final Profile profile) {
88
+        profiles.remove(profile);
89
+    }
90
+
91
+    /**
92
+     * Returns the available profiles in the client.
93
+     *
94
+     * @return Unmodifiable collection of profiles
95
+     */
96
+    public Collection<Profile> getProfiles() {
97
+        return Collections.unmodifiableCollection(profiles);
98
+    }
99
+
100
+    @Override
101
+    public void configProviderAdded(final ConfigProvider configProvider) {
102
+        if (configProvider.isProfile()) {
103
+            profiles.removeIf(p-> p.equalsConfigProvider(configProvider));
104
+        }
105
+    }
106
+
107
+    @Override
108
+    public void configProviderRemoved(final ConfigProvider configProvider) {
109
+        if (configProvider.isProfile()) {
110
+            profiles.add(new Profile(identityFactory, configProvider));
111
+        }
112
+    }
113
+}

+ 52
- 0
src/com/dmdirc/config/profiles/ProfileManagerLifeCycleManager.java Zobrazit soubor

@@ -0,0 +1,52 @@
1
+/*
2
+ * Copyright (c) 2006-2014 DMDirc Developers
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+
23
+package com.dmdirc.config.profiles;
24
+
25
+import com.dmdirc.interfaces.SystemLifecycleComponent;
26
+
27
+import javax.inject.Inject;
28
+import javax.inject.Singleton;
29
+
30
+/**
31
+ * Life cycle manager for the profile manager.
32
+ */
33
+@Singleton
34
+public class ProfileManagerLifeCycleManager implements SystemLifecycleComponent {
35
+
36
+    private final ProfileManager profileManager;
37
+
38
+    @Inject
39
+    public ProfileManagerLifeCycleManager(final ProfileManager profileManager) {
40
+        this.profileManager = profileManager;
41
+    }
42
+
43
+    @Override
44
+    public void startUp() {
45
+        profileManager.start();
46
+    }
47
+
48
+    @Override
49
+    public void shutDown() {
50
+        profileManager.stop();
51
+    }
52
+}

+ 41
- 0
src/com/dmdirc/config/profiles/ProfilesModule.java Zobrazit soubor

@@ -0,0 +1,41 @@
1
+/*
2
+ * Copyright (c) 2006-2014 DMDirc Developers
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+
23
+package com.dmdirc.config.profiles;
24
+
25
+import com.dmdirc.interfaces.SystemLifecycleComponent;
26
+
27
+import dagger.Module;
28
+import dagger.Provides;
29
+
30
+/**
31
+ * Dagger module for profiles.
32
+ */
33
+@Module(library = true, complete = false)
34
+public class ProfilesModule {
35
+
36
+    @Provides(type = Provides.Type.SET)
37
+    public SystemLifecycleComponent getLifecycleComponent(
38
+            final ProfileManagerLifeCycleManager manager) {
39
+        return manager;
40
+    }
41
+}

Načítá se…
Zrušit
Uložit