|
@@ -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
|
+}
|