Переглянути джерело

Add CoreProfilesDialogModel.

The profiles wrapper needs to be replaced (probably), the model is
flexible enough to cope with this. It's still a little bit hacky
with the wrapped model and if we stick with them we should probably
change this model.

EditSelectedProfileNameValidator and EditSelectedNicknameValidator
are both a work in progress and quite like prevent the editing of
profiles.

This still needs to be unit tested.

Change-Id: I9dd73f62ae3641b8d11da9881ad13a11a5b4453b
Reviewed-on: http://gerrit.dmdirc.com/3617
Automatic-Compile: DMDirc Build Manager
Reviewed-by: Chris Smith <chris@dmdirc.com>
pull/1/head
Greg Holmes 10 роки тому
джерело
коміт
26478d87bb

+ 305
- 0
src/com/dmdirc/interfaces/ui/ProfilesDialogModel.java Переглянути файл

@@ -0,0 +1,305 @@
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.interfaces.ui;
24
+
25
+import com.dmdirc.actions.wrappers.Profile;
26
+import com.dmdirc.util.validators.Validator;
27
+
28
+import com.google.common.base.Optional;
29
+
30
+import java.util.Collection;
31
+import java.util.List;
32
+
33
+/**
34
+ * Dialog to edit the user profiles.
35
+ */
36
+public interface ProfilesDialogModel {
37
+
38
+    /**
39
+     * Adds a listener to the model.
40
+     *
41
+     * @param listener Listener to add
42
+     */
43
+    void addListener(ProfilesDialogModelListener listener);
44
+
45
+    /**
46
+     * Adds a profile to the model.
47
+     *
48
+     * @param name      Name of the profile to add
49
+     * @param realname  Realname of the profile
50
+     * @param ident     Optional ident of the profile
51
+     * @param nicknames Nicknames to add
52
+     */
53
+    void addProfile(String name, String realname, String ident, List<String> nicknames);
54
+
55
+    /**
56
+     * Edits a profile in the model.
57
+     *
58
+     * @param name      Name of the profile to edit
59
+     * @param realname  New realname
60
+     * @param ident     New ident
61
+     * @param nicknames New nicknames
62
+     */
63
+    void editProfile(String name, String realname, String ident, List<String> nicknames);
64
+
65
+    /**
66
+     * Retrieves a profile from the model.
67
+     *
68
+     * @param name Name of the profile to get
69
+     *
70
+     * @return Optional profile from the model
71
+     */
72
+    Optional<Profile> getProfile(String name);
73
+
74
+    /**
75
+     * Gets the list of profiles from the model.
76
+     *
77
+     * @return Collection of profiles
78
+     */
79
+    Collection<Profile> getProfileList();
80
+
81
+    /**
82
+     * Gets the profile list validator.
83
+     *
84
+     * @return Profile list validator
85
+     */
86
+    Validator<List<Profile>> getProfileListValidator();
87
+
88
+    /**
89
+     * Gets the selected profile in the model.
90
+     *
91
+     * @return Optional selected profile
92
+     */
93
+    Optional<Profile> getSelectedProfile();
94
+
95
+    /**
96
+     * Gets the selected profile's ident
97
+     *
98
+     * @return Optional profile's ident
99
+     */
100
+    Optional<String> getSelectedProfileIdent();
101
+
102
+    /**
103
+     * Gets the selected profile's ident validator.
104
+     *
105
+     * @return Selected profile ident validator
106
+     */
107
+    Validator<String> getSelectedProfileIdentValidator();
108
+
109
+    /**
110
+     * Gets the selected profile's name.
111
+     *
112
+     * @return Optional selected profile name
113
+     */
114
+    Optional<String> getSelectedProfileName();
115
+
116
+    /**
117
+     * Gets the selected profile's name validator.
118
+     *
119
+     * @return Selected profile name validator
120
+     */
121
+    Validator<String> getSelectedProfileNameValidator();
122
+
123
+    /**
124
+     * Gets the new profile name validator.
125
+     *
126
+     * @return New profile name validator
127
+     */
128
+    Validator<String> getNewProfileNameValidator();
129
+
130
+    /**
131
+     * Gets the selected profile's list of nicknames.
132
+     *
133
+     * @return Optional selected profile's list of nicknames
134
+     */
135
+    Optional<List<String>> getSelectedProfileNicknames();
136
+
137
+    /**
138
+     * Gets the selected profile's list of nicknames validator.
139
+     *
140
+     * @return
141
+     */
142
+    Validator<List<String>> getSelectedProfileNicknamesValidator();
143
+
144
+    /**
145
+     * Gets the selected profile's realname.
146
+     *
147
+     * @return Optional selected profile's realname
148
+     */
149
+    Optional<String> getSelectedProfileRealname();
150
+
151
+    /**
152
+     * Gets the selected profile's realname validator.
153
+     *
154
+     * @return Selected profile's realname validator
155
+     */
156
+    Validator<String> getSelectedProfileRealnameValidator();
157
+
158
+    /**
159
+     * Gets the selected profile's selected nickname.
160
+     *
161
+     * @return Optional selected profile's selected nickname
162
+     */
163
+    Optional<String> getSelectedProfileSelectedNickname();
164
+
165
+    /**
166
+     * Is the profile list valid?
167
+     *
168
+     * @return true or false
169
+     */
170
+    boolean isProfileListValid();
171
+
172
+    /**
173
+     * Is the selected profile's ident valid?
174
+     *
175
+     * @return true or false
176
+     */
177
+    boolean isSelectedProfileIdentValid();
178
+
179
+    /**
180
+     * Is the selected profile's name valid?
181
+     *
182
+     * @return true or false
183
+     */
184
+    boolean isSelectedProfileNameValid();
185
+
186
+    /**
187
+     * Is the selected profile's list of nicknames valid?
188
+     *
189
+     * @return true of false
190
+     */
191
+    boolean isSelectedProfileNicknamesValid();
192
+
193
+    /**
194
+     * Is the selected profile's realname valid.
195
+     *
196
+     * @return true or false
197
+     */
198
+    boolean isSelectedProfileRealnameValid();
199
+
200
+    /**
201
+     * Removes a listener from this model.
202
+     *
203
+     * @param listener Listener to remove
204
+     */
205
+    void removeListener(ProfilesDialogModelListener listener);
206
+
207
+    /**
208
+     * Removes a profile from this model.
209
+     *
210
+     * @param name Name of the profile to remove
211
+     */
212
+    void removeProfile(String name);
213
+
214
+    /**
215
+     * Renames a profile in this model.
216
+     *
217
+     * @param oldName Name of the profile to rename
218
+     * @param newName New name for the profile
219
+     */
220
+    void renameProfile(String oldName, String newName);
221
+
222
+    /**
223
+     * Saves the profiles in the model, deleting as appropriate.
224
+     */
225
+    void save();
226
+
227
+    /**
228
+     * Sets the selected profile in the model.
229
+     *
230
+     * @param profile New selected profile
231
+     */
232
+    void setSelectedProfile(Optional<Profile> profile);
233
+
234
+    /**
235
+     * Sets the selected profile's ident.
236
+     *
237
+     * @param ident New selected profile's ident
238
+     */
239
+    void setSelectedProfileIdent(Optional<String> ident);
240
+
241
+    /**
242
+     * Sets the selected profile's name.
243
+     *
244
+     * @param name New selected profile's name
245
+     */
246
+    void setSelectedProfileName(Optional<String> name);
247
+
248
+    /**
249
+     * Sets the selected profile's list of nicknames.
250
+     *
251
+     * @param nicknames New selected profile's list of nicknames
252
+     */
253
+    void setSelectedProfileNicknames(Optional<List<String>> nicknames);
254
+
255
+    /**
256
+     * Sets the selected profile's realname.
257
+     *
258
+     * @param realname New selected profile's realname
259
+     */
260
+    void setSelectedProfileRealname(Optional<String> realname);
261
+
262
+    /**
263
+     * Sets the selected profile's selected nickname.
264
+     *
265
+     * @param selectedNickname New selected profile's selected nickname
266
+     */
267
+    void setSelectedProfileSelectedNickname(Optional<String> selectedNickname);
268
+
269
+    /**
270
+     * Adds a nickname to the selected profile.
271
+     *
272
+     * @param nickname Nickname to add
273
+     */
274
+    void addSelectedProfileNickname(final String nickname);
275
+
276
+    /**
277
+     * Removes a nickname from the selected profile.
278
+     *
279
+     * @param nickname Nickname to remove
280
+     */
281
+    void removeSelectedProfileNickname(final String nickname);
282
+
283
+    /**
284
+     * Gets the selected profile's add nickname validator.
285
+     *
286
+     * @return Selected profile's add nickname validator
287
+     */
288
+    Validator<String> getSelectedProfileAddNicknameValidator();
289
+
290
+    /**
291
+     * Gets the selected profile's edit nickname validator.
292
+     *
293
+     * @param oldName Old nickname
294
+     * @param newName New nickname
295
+     */
296
+    void editSelectedProfileNickname(final String oldName, final String newName);
297
+
298
+    /**
299
+     * Gets the selected profile's edit nickname validator.
300
+     *
301
+     * @return Selected profile's edit nickname validator
302
+     */
303
+    Validator<String> getSelectedProfileEditNicknameValidator();
304
+
305
+}

+ 112
- 0
src/com/dmdirc/interfaces/ui/ProfilesDialogModelListener.java Переглянути файл

@@ -0,0 +1,112 @@
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.interfaces.ui;
24
+
25
+import com.dmdirc.actions.wrappers.Profile;
26
+
27
+import com.google.common.base.Optional;
28
+
29
+import java.util.List;
30
+
31
+/**
32
+ * Listener for various events in a profiles dialog.
33
+ */
34
+public interface ProfilesDialogModelListener {
35
+
36
+    /**
37
+     * Called when a profile is added to the model.
38
+     *
39
+     * @param profile New profile
40
+     */
41
+    void profileAdded(Profile profile);
42
+
43
+    /**
44
+     * Called when a profile is removed from the model.
45
+     *
46
+     * @param profile Old profile
47
+     */
48
+    void profileRemoved(Profile profile);
49
+
50
+    /**
51
+     * Called when a profile in the model is edited.
52
+     *
53
+     * @param oldProfile Old profile
54
+     * @param newProfile New profile
55
+     */
56
+    void profileEdited(Profile oldProfile, Profile newProfile);
57
+
58
+    /**
59
+     * Called when a profile in the model is renamed.
60
+     *
61
+     * @param oldProfile Old profile
62
+     * @param newProfile New profile
63
+     */
64
+    void profileRenamed(Profile oldProfile, Profile newProfile);
65
+
66
+    /**
67
+     * Called when the selected profile in the model changes.
68
+     *
69
+     * @param profile New selected profile
70
+     */
71
+    void profileSelectionChanged(Optional<Profile> profile);
72
+
73
+    /**
74
+     * Called when the selected profile's selected nickname is changed
75
+     *
76
+     * @param nickname Optional selected profile's selected nickname
77
+     */
78
+    void selectedNicknameChanged(Optional<String> nickname);
79
+
80
+    /**
81
+     * Called when the selected profile in the model is edited.
82
+     *
83
+     * @param name      Name of the profile being edited
84
+     * @param realname  New realname
85
+     * @param ident     New ident
86
+     * @param nicknames New nicknames
87
+     */
88
+    void selectedProfileEdited(String name, String realname, String ident, List<String> nicknames);
89
+
90
+    /**
91
+     * Called when a nickname in the selected profile is changed.
92
+     *
93
+     * @param oldNickname Old nickname
94
+     * @param newNickname New nickname
95
+     */
96
+    void selectedProfileNicknameEdited(String oldNickname, String newNickname);
97
+
98
+    /**
99
+     * Called when a nickname is added to the selected profile.
100
+     *
101
+     * @param nickname New nickname
102
+     */
103
+    void selectedProfileNicknameAdded(String nickname);
104
+
105
+    /**
106
+     * Called when a nickname is removed from the selected profile.
107
+     *
108
+     * @param nickname Old nickname
109
+     */
110
+    void selectedProfileNicknameRemoved(String nickname);
111
+
112
+}

+ 53
- 0
src/com/dmdirc/ui/core/profiles/AddNicknameValidator.java Переглянути файл

@@ -0,0 +1,53 @@
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.ui.core.profiles;
24
+
25
+import com.dmdirc.interfaces.ui.ProfilesDialogModel;
26
+import com.dmdirc.util.validators.ValidationResponse;
27
+import com.dmdirc.util.validators.Validator;
28
+
29
+/**
30
+ * Validates a nickname that is being added to the model.
31
+ */
32
+public class AddNicknameValidator implements Validator<String> {
33
+
34
+    private final ProfilesDialogModel model;
35
+
36
+    public AddNicknameValidator(final ProfilesDialogModel model) {
37
+        this.model = model;
38
+    }
39
+
40
+    @Override
41
+    public ValidationResponse validate(final String object) {
42
+        if (model.getSelectedProfile().isPresent()
43
+                && model.getSelectedProfileNicknames().isPresent()) {
44
+            for (String nickname : model.getSelectedProfileNicknames().get()) {
45
+                if (nickname.equals(object)) {
46
+                    return new ValidationResponse("This valid already exists.");
47
+                }
48
+            }
49
+        }
50
+        return new ValidationResponse();
51
+    }
52
+
53
+}

+ 432
- 0
src/com/dmdirc/ui/core/profiles/CoreProfilesDialogModel.java Переглянути файл

@@ -0,0 +1,432 @@
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.ui.core.profiles;
24
+
25
+import com.dmdirc.actions.wrappers.Profile;
26
+import com.dmdirc.interfaces.config.ConfigProvider;
27
+import com.dmdirc.interfaces.config.IdentityController;
28
+import com.dmdirc.interfaces.config.IdentityFactory;
29
+import com.dmdirc.interfaces.ui.ProfilesDialogModel;
30
+import com.dmdirc.interfaces.ui.ProfilesDialogModelListener;
31
+import com.dmdirc.util.collections.ListenerList;
32
+import com.dmdirc.util.validators.ListNotEmptyValidator;
33
+import com.dmdirc.util.validators.NotEmptyValidator;
34
+import com.dmdirc.util.validators.PermissiveValidator;
35
+import com.dmdirc.util.validators.Validator;
36
+
37
+import com.google.common.base.Optional;
38
+import com.google.common.collect.ImmutableList;
39
+import com.google.common.collect.Lists;
40
+
41
+import java.io.IOException;
42
+import java.util.HashMap;
43
+import java.util.List;
44
+
45
+import javax.inject.Inject;
46
+
47
+import static com.google.common.base.Preconditions.checkArgument;
48
+import static com.google.common.base.Preconditions.checkNotNull;
49
+import static com.google.common.base.Preconditions.checkState;
50
+
51
+public class CoreProfilesDialogModel implements ProfilesDialogModel {
52
+
53
+    private final ListenerList listeners;
54
+    private final IdentityFactory identityFactory;
55
+    private final IdentityController identityController;
56
+    private HashMap<String, Profile> profiles;
57
+    private Optional<Profile> selectedProfile = Optional.absent();
58
+    private Optional<String> name = Optional.absent();
59
+    private Optional<List<String>> nicknames = Optional.absent();
60
+    private Optional<String> selectedNickname = Optional.absent();
61
+    private Optional<String> realname = Optional.absent();
62
+    private Optional<String> ident = Optional.absent();
63
+
64
+    @Inject
65
+    public CoreProfilesDialogModel(final IdentityController identityController,
66
+            final IdentityFactory identityFactory) {
67
+        this.identityFactory = identityFactory;
68
+        this.identityController = identityController;
69
+        listeners = new ListenerList();
70
+        final List<ConfigProvider> identities = identityController.getProvidersByType("profile");
71
+        for (ConfigProvider identity : identities) {
72
+            profiles.put(identity.getName(), getProfile(identity));
73
+        }
74
+    }
75
+
76
+    private Profile getProfile(final ConfigProvider configProvider) {
77
+        final Profile newProfile = new Profile(configProvider.getName(), identityFactory);
78
+        this.name = Optional.fromNullable(configProvider.getOption("identity", "name"));
79
+        this.nicknames = Optional.fromNullable(configProvider.getOptionList("profile", "nicknames"));
80
+        this.realname = Optional.fromNullable(configProvider.getOption("profile", "realname"));
81
+        this.ident = Optional.fromNullable(configProvider.getOption("profile", "ident"));
82
+        return newProfile;
83
+    }
84
+
85
+    @Override
86
+    public List<Profile> getProfileList() {
87
+        return ImmutableList.copyOf(profiles.values());
88
+    }
89
+
90
+    @Override
91
+    public Optional<Profile> getProfile(final String name) {
92
+        checkNotNull(name, "Name cannot be null");
93
+        return Optional.fromNullable(profiles.get(name));
94
+    }
95
+
96
+    @Override
97
+    public boolean isProfileListValid() {
98
+        return !getProfileListValidator().validate(getProfileList()).isFailure();
99
+    }
100
+
101
+    @Override
102
+    public Validator<List<Profile>> getProfileListValidator() {
103
+        return new ListNotEmptyValidator<Profile>();
104
+    }
105
+
106
+    @Override
107
+    public void addProfile(final String name, final String realname, final String ident,
108
+            final List<String> nicknames) {
109
+        checkNotNull(name, "Name cannot be null");
110
+        checkArgument(!profiles.containsKey(name), "Name cannot already exist");
111
+        final Profile profile = new Profile(name, identityFactory);
112
+        profile.setRealname(realname);
113
+        profile.setIdent(ident);
114
+        profile.setNicknames(Lists.newArrayList(nicknames));
115
+        profiles.put(name, profile);
116
+        listeners.getCallable(ProfilesDialogModelListener.class).profileAdded(profile);
117
+    }
118
+
119
+    @Override
120
+    public void editProfile(final String name, final String realname, final String ident,
121
+            final List<String> nicknames) {
122
+        checkNotNull(name, "Name cannot be null");
123
+        checkArgument(profiles.containsKey(name), "Name must already exist");
124
+        final Profile profile = profiles.get(name);
125
+        profile.setRealname(realname);
126
+        profile.setIdent(ident);
127
+        profile.setNicknames(Lists.newArrayList(nicknames));
128
+        listeners.getCallable(ProfilesDialogModelListener.class).profileEdited(profile, profile);
129
+    }
130
+
131
+    @Override
132
+    public void renameProfile(final String oldName, final String newName) {
133
+        renameProfile(oldName, newName, false);
134
+    }
135
+
136
+    public void renameProfile(final String oldName, final String newName, final boolean selection) {
137
+        checkNotNull(oldName, "Oldname cannot be null");
138
+        checkNotNull(newName, "Newname cannot be null");
139
+        checkArgument(profiles.containsKey(oldName), "Old name must exist");
140
+        checkArgument(!profiles.containsKey(newName), "New name must not exist");
141
+        final Profile profile = profiles.get(oldName);
142
+        final Profile newProfile = new Profile(newName, identityFactory);
143
+        profile.setRealname(profile.getRealname());
144
+        profile.setIdent(profile.getIdent());
145
+        profile.setNicknames(Lists.newArrayList(profile.getNicknames()));
146
+        final Profile oldProfile = profiles.remove(oldName);
147
+        profiles.put(newName, newProfile);
148
+        listeners.getCallable(ProfilesDialogModelListener.class).profileRenamed(oldProfile,
149
+                newProfile);
150
+    }
151
+
152
+    @Override
153
+    public void removeProfile(final String name) {
154
+        checkNotNull(name, "Name cannot be null");
155
+        checkArgument(profiles.containsKey(name), "profile must exist in list");
156
+        final Profile profile = profiles.remove(name);
157
+        if (getSelectedProfile().isPresent() && getSelectedProfile().get().equals(profile)) {
158
+            setSelectedProfile(Optional.<Profile>absent());
159
+        }
160
+        listeners.getCallable(ProfilesDialogModelListener.class).profileRemoved(profile);
161
+    }
162
+
163
+    @Override
164
+    public void save() {
165
+        setSelectedProfile(Optional.<Profile>absent());
166
+        final List<ConfigProvider> identities = identityController.getProvidersByType("profile");
167
+        for (ConfigProvider identity : identities) {
168
+            try {
169
+                identity.delete();
170
+            } catch (IOException ex) {
171
+                //Can't handle and will be dealt with when profiles are redone.
172
+            }
173
+        }
174
+        for (Profile profile : profiles.values()) {
175
+            profile.save();
176
+        }
177
+    }
178
+
179
+    @Override
180
+    public void setSelectedProfile(final Optional<Profile> profile) {
181
+        checkNotNull(profile, "profile cannot be null");
182
+        if (profile.isPresent()) {
183
+            checkArgument(profiles.containsValue(profile.get()), "Profile must exist in list");
184
+        }
185
+        if (selectedProfile.isPresent()) {
186
+            if (!Optional.fromNullable(selectedProfile.get().getRealname()).equals(realname)
187
+                    || !Optional.fromNullable(selectedProfile.get()
188
+                            .getIdent()).equals(ident)
189
+                    || !Optional.fromNullable(selectedProfile.get()
190
+                            .getNicknames()).equals(nicknames)) {
191
+                editProfile(selectedProfile.get().getName(), realname.get(),
192
+                        ident.get(), nicknames.get());
193
+            }
194
+            if (!Optional.fromNullable(selectedProfile.get().getName()).equals(name)) {
195
+                renameProfile(selectedProfile.get().getName(), name.get(), true);
196
+            }
197
+        }
198
+        selectedProfile = profile;
199
+        if (selectedProfile.isPresent()) {
200
+            name = Optional.fromNullable(selectedProfile.get().getName());
201
+            realname = Optional.fromNullable(selectedProfile.get().getRealname());
202
+            ident = Optional.fromNullable(selectedProfile.get().getIdent());
203
+            nicknames = Optional.fromNullable(selectedProfile.get().getNicknames());
204
+        } else {
205
+            name = Optional.absent();
206
+            realname = Optional.absent();
207
+            ident = Optional.absent();
208
+            nicknames = Optional.absent();
209
+        }
210
+        listeners.getCallable(ProfilesDialogModelListener.class).profileSelectionChanged(profile);
211
+    }
212
+
213
+    @Override
214
+    public Optional<Profile> getSelectedProfile() {
215
+        return selectedProfile;
216
+    }
217
+
218
+    @Override
219
+    public Optional<String> getSelectedProfileName() {
220
+        if (selectedProfile.isPresent()) {
221
+            return name;
222
+        }
223
+        return Optional.absent();
224
+    }
225
+
226
+    @Override
227
+    public void setSelectedProfileName(final Optional<String> name) {
228
+        checkNotNull(name, "Name cannot be null");
229
+        checkState(selectedProfile.isPresent(), "There must be a profile selected");
230
+        this.name = name;
231
+        listeners.getCallable(ProfilesDialogModelListener.class)
232
+                .selectedProfileEdited(name.get(), realname.get(), ident.get(), nicknames.get());
233
+    }
234
+
235
+    @Override
236
+    public Validator<String> getSelectedProfileNameValidator() {
237
+        return new EditSelectedProfileNameValidator(this);
238
+    }
239
+
240
+    @Override
241
+    public Validator<String> getNewProfileNameValidator() {
242
+        return new NewProfileNameValidator(this);
243
+    }
244
+
245
+    @Override
246
+    public boolean isSelectedProfileNameValid() {
247
+        if (getSelectedProfileName().isPresent()) {
248
+            return !getSelectedProfileNameValidator()
249
+                    .validate(getSelectedProfileName().get()).isFailure();
250
+        }
251
+        return true;
252
+    }
253
+
254
+    @Override
255
+    public Optional<String> getSelectedProfileRealname() {
256
+        if (selectedProfile.isPresent()) {
257
+            return realname;
258
+        }
259
+        return Optional.absent();
260
+    }
261
+
262
+    @Override
263
+    public void setSelectedProfileRealname(final Optional<String> realname) {
264
+        checkNotNull(realname, "Realname cannot be null");
265
+        checkState(selectedProfile.isPresent(), "There must be a profile selected");
266
+        this.realname = realname;
267
+        listeners.getCallable(ProfilesDialogModelListener.class)
268
+                .selectedProfileEdited(name.get(), realname.get(), ident.get(), nicknames.get());
269
+    }
270
+
271
+    @Override
272
+    public Validator<String> getSelectedProfileRealnameValidator() {
273
+        return new NotEmptyValidator();
274
+    }
275
+
276
+    @Override
277
+    public boolean isSelectedProfileRealnameValid() {
278
+        if (getSelectedProfileRealname().isPresent()) {
279
+            return !getSelectedProfileRealnameValidator()
280
+                    .validate(getSelectedProfileRealname().get()).isFailure();
281
+        }
282
+        return true;
283
+    }
284
+
285
+    @Override
286
+    public Optional<String> getSelectedProfileIdent() {
287
+        if (selectedProfile.isPresent()) {
288
+            return ident;
289
+        }
290
+        return Optional.absent();
291
+    }
292
+
293
+    @Override
294
+    public void setSelectedProfileIdent(final Optional<String> ident) {
295
+        checkNotNull(ident, "Ident cannot be null");
296
+        checkState(selectedProfile.isPresent(), "There must be a profile selected");
297
+        this.ident = ident;
298
+        listeners.getCallable(ProfilesDialogModelListener.class)
299
+                .selectedProfileEdited(name.get(), realname.get(), ident.get(), nicknames.get());
300
+    }
301
+
302
+    @Override
303
+    public Validator<String> getSelectedProfileIdentValidator() {
304
+        return new PermissiveValidator<>();
305
+    }
306
+
307
+    @Override
308
+    public boolean isSelectedProfileIdentValid() {
309
+        if (getSelectedProfileIdent().isPresent()) {
310
+            return !getSelectedProfileIdentValidator()
311
+                    .validate(getSelectedProfileIdent().get()).isFailure();
312
+        }
313
+        return true;
314
+    }
315
+
316
+    @Override
317
+    public Optional<List<String>> getSelectedProfileNicknames() {
318
+        if (selectedProfile.isPresent()) {
319
+            return nicknames;
320
+        }
321
+        return Optional.absent();
322
+    }
323
+
324
+    @Override
325
+    public void setSelectedProfileNicknames(final Optional<List<String>> nicknames) {
326
+        checkNotNull(nicknames, "nicknames cannot be null");
327
+        checkState(selectedProfile.isPresent(), "There must be a profile selected");
328
+        if (nicknames.isPresent()) {
329
+            this.nicknames = nicknames;
330
+        } else {
331
+            this.nicknames = Optional.fromNullable((List<String>) Lists.
332
+                    newArrayList(nicknames.get()));
333
+        }
334
+        listeners.getCallable(ProfilesDialogModelListener.class)
335
+                .selectedProfileEdited(name.get(), realname.get(), ident.get(), nicknames.get());
336
+    }
337
+
338
+    @Override
339
+    public Validator<List<String>> getSelectedProfileNicknamesValidator() {
340
+        return new ListNotEmptyValidator<String>();
341
+    }
342
+
343
+    @Override
344
+    public boolean isSelectedProfileNicknamesValid() {
345
+        if (getSelectedProfileNicknames().isPresent()) {
346
+            return !getSelectedProfileNicknamesValidator()
347
+                    .validate(getSelectedProfileNicknames().get()).isFailure();
348
+        }
349
+        return true;
350
+    }
351
+
352
+    @Override
353
+    public void addSelectedProfileNickname(final String nickname) {
354
+        checkNotNull(nickname, "Nickname cannot be null");
355
+        checkState(selectedProfile.isPresent(), "There must be a profile selected");
356
+        checkState(nicknames.isPresent(), "There must be nicknames present");
357
+        checkArgument(!nicknames.get().contains(nickname), "New nickname must not exist");
358
+        nicknames.get().add(nickname);
359
+        listeners.getCallable(ProfilesDialogModelListener.class).selectedProfileNicknameAdded(
360
+                nickname);
361
+    }
362
+
363
+    @Override
364
+    public void removeSelectedProfileNickname(final String nickname) {
365
+        checkNotNull(nickname, "Nickname cannot be null");
366
+        checkState(selectedProfile.isPresent(), "There must be a profile selected");
367
+        checkState(nicknames.isPresent(), "There must be nicknames present");
368
+        checkArgument(nicknames.get().contains(nickname), "Nickname must exist");
369
+        nicknames.get().add(nickname);
370
+        listeners.getCallable(ProfilesDialogModelListener.class).selectedProfileNicknameRemoved(
371
+                nickname);
372
+    }
373
+
374
+    @Override
375
+    public Validator<String> getSelectedProfileAddNicknameValidator() {
376
+        return new AddNicknameValidator(this);
377
+    }
378
+
379
+    @Override
380
+    public void editSelectedProfileNickname(final String oldName, final String newName) {
381
+        checkNotNull(oldName, "Nickname cannot be null");
382
+        checkNotNull(newName, "Nickname cannot be null");
383
+        checkState(selectedProfile.isPresent(), "There must be a profile selected");
384
+        checkState(nicknames.isPresent(), "There must be nicknames present");
385
+        checkArgument(nicknames.get().contains(oldName), "Old nickname must exist");
386
+        checkArgument(!nicknames.get().contains(newName), "New nickname must not exist");
387
+        final int index = nicknames.get().indexOf(oldName);
388
+        nicknames.get().set(index, newName);
389
+        listeners.getCallable(ProfilesDialogModelListener.class).selectedProfileNicknameEdited(
390
+                oldName, newName);
391
+    }
392
+
393
+    @Override
394
+    public Validator<String> getSelectedProfileEditNicknameValidator() {
395
+        return new EditSelectedNicknameValidator(this);
396
+    }
397
+
398
+    @Override
399
+    public Optional<String> getSelectedProfileSelectedNickname() {
400
+        if (selectedProfile.isPresent()) {
401
+            return selectedNickname;
402
+        }
403
+        return Optional.absent();
404
+    }
405
+
406
+    @Override
407
+    public void setSelectedProfileSelectedNickname(final Optional<String> selectedNickname) {
408
+        checkNotNull(selectedNickname, "Nickname cannot be null");
409
+        checkState(selectedProfile.isPresent(), "There must be a profile selected");
410
+        checkState(nicknames.isPresent(), "There must be nicknames present");
411
+        if (selectedNickname.isPresent()) {
412
+            checkArgument(nicknames.get().contains(selectedNickname.get()),
413
+                    "Nickname must exist in nicknames list");
414
+        }
415
+        this.selectedNickname = selectedNickname;
416
+        listeners.getCallable(ProfilesDialogModelListener.class)
417
+                .selectedNicknameChanged(selectedNickname);
418
+    }
419
+
420
+    @Override
421
+    public void addListener(final ProfilesDialogModelListener listener) {
422
+        checkNotNull(listener, "Listener must not be null");
423
+        listeners.add(ProfilesDialogModelListener.class, listener);
424
+    }
425
+
426
+    @Override
427
+    public void removeListener(final ProfilesDialogModelListener listener) {
428
+        checkNotNull(listener, "Listener must not be null");
429
+        listeners.remove(ProfilesDialogModelListener.class, listener);
430
+    }
431
+
432
+}

+ 53
- 0
src/com/dmdirc/ui/core/profiles/EditSelectedNicknameValidator.java Переглянути файл

@@ -0,0 +1,53 @@
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.ui.core.profiles;
24
+
25
+import com.dmdirc.interfaces.ui.ProfilesDialogModel;
26
+import com.dmdirc.util.validators.ValidationResponse;
27
+import com.dmdirc.util.validators.Validator;
28
+
29
+/**
30
+ * Validates a nickname that is being edited in the model.
31
+ */
32
+public class EditSelectedNicknameValidator implements Validator<String> {
33
+
34
+    private final ProfilesDialogModel model;
35
+
36
+    public EditSelectedNicknameValidator(final ProfilesDialogModel model) {
37
+        this.model = model;
38
+    }
39
+
40
+    @Override
41
+    public ValidationResponse validate(final String object) {
42
+        if (model.getSelectedProfile().isPresent()
43
+                && model.getSelectedProfileNicknames().isPresent()) {
44
+            for (String nickname : model.getSelectedProfileNicknames().get()) {
45
+                if (nickname.equals(object)) {
46
+                    return new ValidationResponse("This valid already exists.");
47
+                }
48
+            }
49
+        }
50
+        return new ValidationResponse();
51
+    }
52
+
53
+}

+ 51
- 0
src/com/dmdirc/ui/core/profiles/EditSelectedProfileNameValidator.java Переглянути файл

@@ -0,0 +1,51 @@
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.ui.core.profiles;
24
+
25
+import com.dmdirc.actions.wrappers.Profile;
26
+import com.dmdirc.interfaces.ui.ProfilesDialogModel;
27
+import com.dmdirc.util.validators.ValidationResponse;
28
+import com.dmdirc.util.validators.Validator;
29
+
30
+/**
31
+ * Validates an existing profile name.
32
+ */
33
+public class EditSelectedProfileNameValidator implements Validator<String> {
34
+
35
+    private final ProfilesDialogModel model;
36
+
37
+    public EditSelectedProfileNameValidator(final ProfilesDialogModel model) {
38
+        this.model = model;
39
+    }
40
+
41
+    @Override
42
+    public ValidationResponse validate(final String object) {
43
+        for (Profile nickname : model.getProfileList()) {
44
+            if (nickname.getName().equals(object)) {
45
+                return new ValidationResponse("This valid already exists.");
46
+            }
47
+        }
48
+        return new ValidationResponse();
49
+    }
50
+
51
+}

+ 51
- 0
src/com/dmdirc/ui/core/profiles/NewProfileNameValidator.java Переглянути файл

@@ -0,0 +1,51 @@
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.ui.core.profiles;
24
+
25
+import com.dmdirc.actions.wrappers.Profile;
26
+import com.dmdirc.interfaces.ui.ProfilesDialogModel;
27
+import com.dmdirc.util.validators.ValidationResponse;
28
+import com.dmdirc.util.validators.Validator;
29
+
30
+/**
31
+ * Validates a new profile name.
32
+ */
33
+public class NewProfileNameValidator implements Validator<String> {
34
+
35
+    private final ProfilesDialogModel model;
36
+
37
+    public NewProfileNameValidator(final ProfilesDialogModel model) {
38
+        this.model = model;
39
+    }
40
+
41
+    @Override
42
+    public ValidationResponse validate(final String object) {
43
+        for (Profile nickname : model.getProfileList()) {
44
+            if (nickname.getName().equals(object)) {
45
+                return new ValidationResponse("This valid already exists.");
46
+            }
47
+        }
48
+        return new ValidationResponse();
49
+    }
50
+
51
+}

+ 76
- 0
src/com/dmdirc/ui/core/profiles/ProfilesDialogModelAdapter.java Переглянути файл

@@ -0,0 +1,76 @@
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.ui.core.profiles;
24
+
25
+import com.dmdirc.actions.wrappers.Profile;
26
+import com.dmdirc.interfaces.ui.ProfilesDialogModelListener;
27
+
28
+import com.google.common.base.Optional;
29
+
30
+import java.util.List;
31
+
32
+
33
+public class ProfilesDialogModelAdapter implements ProfilesDialogModelListener {
34
+
35
+    @Override
36
+    public void profileAdded(final Profile profile) {
37
+    }
38
+
39
+    @Override
40
+    public void profileRemoved(final Profile profile) {
41
+    }
42
+
43
+    @Override
44
+    public void profileEdited(final Profile oldProfile, final Profile newProfile) {
45
+    }
46
+
47
+    @Override
48
+    public void profileRenamed(final Profile oldProfile, final Profile newProfile) {
49
+    }
50
+
51
+    @Override
52
+    public void profileSelectionChanged(final Optional<Profile> profile) {
53
+    }
54
+
55
+    @Override
56
+    public void selectedProfileEdited(final String name, final String realname, final String ident,
57
+            final List<String> nicknames) {
58
+    }
59
+
60
+    @Override
61
+    public void selectedNicknameChanged(final Optional<String> nickname) {
62
+    }
63
+
64
+    @Override
65
+    public void selectedProfileNicknameEdited(final String oldNickname, final String newNickname) {
66
+    }
67
+
68
+    @Override
69
+    public void selectedProfileNicknameAdded(final String nickname) {
70
+    }
71
+
72
+    @Override
73
+    public void selectedProfileNicknameRemoved(final String nickname) {
74
+    }
75
+
76
+}

Завантаження…
Відмінити
Зберегти