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

Add CoreFeedbackDialogModel.

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

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

@@ -0,0 +1,172 @@
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.util.validators.Validator;
26
+
27
+import com.google.common.base.Optional;
28
+
29
+/**
30
+ * Dialog to connect to send feedback to the developers.
31
+ */
32
+public interface FeedbackDialogModel {
33
+
34
+    /**
35
+     * Gets the name in the model.
36
+     *
37
+     * @return Optional string containing the name
38
+     */
39
+    Optional<String> getName();
40
+
41
+    /**
42
+     * Sets the name in the model.
43
+     *
44
+     * @param name Optional string containing the name
45
+     */
46
+    void setName(Optional<String> name);
47
+
48
+    /**
49
+     * Is the name in the model valid?
50
+     *
51
+     * @return true or false
52
+     */
53
+    boolean isNameValid();
54
+
55
+    /**
56
+     * Gets the name validator in the model.
57
+     *
58
+     * @return Name validator
59
+     */
60
+    Validator<String> getNameValidator();
61
+
62
+    /**
63
+     * Gets the email in the model.
64
+     *
65
+     * @return Optional string containing the email
66
+     */
67
+    Optional<String> getEmail();
68
+
69
+    /**
70
+     * Sets the email in the model.
71
+     *
72
+     * @param email Optional string containing the email
73
+     */
74
+    void setEmail(Optional<String> email);
75
+
76
+    /**
77
+     * Is the email in the model valid?
78
+     *
79
+     * @return true or false
80
+     */
81
+    boolean isEmailValid();
82
+
83
+    /**
84
+     * Gets the validator for the email address
85
+     *
86
+     * @return Email validator
87
+     */
88
+    Validator<String> getEmailValidator();
89
+
90
+    /**
91
+     * Gets the feedback in the model.
92
+     *
93
+     * @return Optional string containing the feedback
94
+     */
95
+    Optional<String> getFeedback();
96
+
97
+    /**
98
+     * Sets the feedback in the model.
99
+     *
100
+     * @param feedback Optional string containing the feedback
101
+     */
102
+    void setFeedback(Optional<String> feedback);
103
+
104
+    /**
105
+     * Is the feedback in the model valid?
106
+     *
107
+     * @return true or false
108
+     */
109
+    boolean isFeedbackValid();
110
+
111
+    /**
112
+     * Gets the validator for the feedback.
113
+     *
114
+     * @return Feedback validator
115
+     */
116
+    Validator<String> getFeedbackValidator();
117
+
118
+    /**
119
+     * Should we include the server info with the feedback?
120
+     *
121
+     * @return true or false
122
+     */
123
+    boolean getIncludeServerInfo();
124
+
125
+    /**
126
+     * Sets whether we should include the server info with the feedback.
127
+     *
128
+     * @param includeServerInfo true or false
129
+     */
130
+    void setIncludeServerInfo(boolean includeServerInfo);
131
+
132
+    /**
133
+     * Should we include information about DMDirc with the feedback?
134
+     *
135
+     * @return true or false
136
+     */
137
+    boolean getIncludeDMDircInfo();
138
+
139
+    /**
140
+     * Sets whether we should include information about DMDirc with the feedback.
141
+     *
142
+     * @param includeDMDircInfo true or false
143
+     */
144
+    void setIncludeDMDircInfo(boolean includeDMDircInfo);
145
+
146
+    /**
147
+     * Are we allowed to save the dialog?
148
+     *
149
+     * @return true or false
150
+     */
151
+    boolean isSaveAllowed();
152
+
153
+    /**
154
+     * Saves the state of the dialog, sending specified feedback.
155
+     */
156
+    void save();
157
+
158
+    /**
159
+     * Adds a listener to this model.
160
+     *
161
+     * @param listener Listener to add
162
+     */
163
+    void addListener(FeedbackDialogModelListener listener);
164
+
165
+    /**
166
+     * Removes a listener from this model.
167
+     *
168
+     * @param listener Listener to remove
169
+     */
170
+    void removeListener(FeedbackDialogModelListener listener);
171
+
172
+}

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

@@ -0,0 +1,67 @@
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.google.common.base.Optional;
26
+
27
+/**
28
+ * Listener for various events in a feedback dialog.
29
+ */
30
+public interface FeedbackDialogModelListener {
31
+
32
+    /**
33
+     * Called when the name is changed.
34
+     *
35
+     * @param name New name
36
+     */
37
+    void nameChanged(Optional<String> name);
38
+
39
+    /**
40
+     * Called when the email is changed.
41
+     *
42
+     * @param email New email
43
+     */
44
+    void emailChanged(Optional<String> email);
45
+
46
+    /**
47
+     * Called when the feedback is changed.
48
+     *
49
+     * @param feedback New feedback
50
+     */
51
+    void feedbackChanged(Optional<String> feedback);
52
+
53
+    /**
54
+     * Called when the server info checkbox is changed.
55
+     *
56
+     * @param includeServerInfo New state
57
+     */
58
+    void includeServerInfoChanged(boolean includeServerInfo);
59
+
60
+    /**
61
+     * Called when the DMDirc info checkbox is changed.
62
+     *
63
+     * @param includeDMDircInfo New state
64
+     */
65
+    void includeDMDircInfoChanged(boolean includeDMDircInfo);
66
+
67
+}

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

@@ -0,0 +1,222 @@
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.feedback;
24
+
25
+import com.dmdirc.ClientModule;
26
+import com.dmdirc.ServerManager;
27
+import com.dmdirc.commandline.CommandLineOptionsModule.Directory;
28
+import com.dmdirc.commandline.CommandLineOptionsModule.DirectoryType;
29
+import com.dmdirc.interfaces.Connection;
30
+import com.dmdirc.interfaces.config.AggregateConfigProvider;
31
+import com.dmdirc.interfaces.ui.FeedbackDialogModel;
32
+import com.dmdirc.interfaces.ui.FeedbackDialogModelListener;
33
+import com.dmdirc.util.ClientInfo;
34
+import com.dmdirc.util.collections.ListenerList;
35
+import com.dmdirc.util.validators.EmailAddressValidator;
36
+import com.dmdirc.util.validators.NotEmptyValidator;
37
+import com.dmdirc.util.validators.PermissiveValidator;
38
+import com.dmdirc.util.validators.Validator;
39
+
40
+import com.google.common.base.Optional;
41
+
42
+import javax.inject.Inject;
43
+
44
+/**
45
+ * Default implementation of a feedback dialog module.
46
+ */
47
+public class CoreFeedbackDialogModel implements FeedbackDialogModel {
48
+
49
+    private final FeedbackSenderFactory feedbackSenderFactory;
50
+    private final ListenerList listeners;
51
+    private final AggregateConfigProvider config;
52
+    private final ServerManager serverManager;
53
+    private final String configDirectory;
54
+    private Optional<String> name;
55
+    private Optional<String> email;
56
+    private Optional<String> feedback;
57
+    private boolean includeServerInfo;
58
+    private boolean includeDMDircInfo;
59
+
60
+    @Inject
61
+    public CoreFeedbackDialogModel(@ClientModule.GlobalConfig final AggregateConfigProvider config,
62
+            final ServerManager serverManager, final FeedbackSenderFactory feedbackSenderFactory,
63
+            @Directory(DirectoryType.BASE) final String configDirectory) {
64
+        this.serverManager = serverManager;
65
+        this.configDirectory = configDirectory;
66
+        this.feedbackSenderFactory = feedbackSenderFactory;
67
+        this.config = config;
68
+        this.listeners = new ListenerList();
69
+        name = Optional.absent();
70
+        email = Optional.absent();
71
+        feedback = Optional.absent();
72
+        includeServerInfo = false;
73
+        includeDMDircInfo = false;
74
+    }
75
+
76
+    @Override
77
+    public Optional<String> getName() {
78
+        return name;
79
+    }
80
+
81
+    @Override
82
+    public void setName(final Optional<String> name) {
83
+        this.name = name;
84
+        listeners.getCallable(FeedbackDialogModelListener.class).nameChanged(name);
85
+    }
86
+
87
+    @Override
88
+    public boolean isNameValid() {
89
+        return !getNameValidator().validate(name.or("")).isFailure();
90
+    }
91
+
92
+    @Override
93
+    public Validator<String> getNameValidator() {
94
+        return new PermissiveValidator<>();
95
+    }
96
+
97
+    @Override
98
+    public Optional<String> getEmail() {
99
+        return email;
100
+    }
101
+
102
+    @Override
103
+    public void setEmail(final Optional<String> email) {
104
+        this.email = email;
105
+        listeners.getCallable(FeedbackDialogModelListener.class).emailChanged(email);
106
+    }
107
+
108
+    @Override
109
+    public boolean isEmailValid() {
110
+        return !getEmailValidator().validate(email.or("")).isFailure();
111
+    }
112
+
113
+    @Override
114
+    public Validator<String> getEmailValidator() {
115
+        if (email.isPresent()) {
116
+            return new EmailAddressValidator();
117
+        }
118
+        return new PermissiveValidator<>();
119
+    }
120
+
121
+    @Override
122
+    public Optional<String> getFeedback() {
123
+        return feedback;
124
+    }
125
+
126
+    @Override
127
+    public void setFeedback(final Optional<String> feedback) {
128
+        this.feedback = feedback;
129
+        listeners.getCallable(FeedbackDialogModelListener.class).feedbackChanged(feedback);
130
+    }
131
+
132
+    @Override
133
+    public boolean isFeedbackValid() {
134
+        return !getFeedbackValidator().validate(feedback.or("")).isFailure();
135
+    }
136
+
137
+    @Override
138
+    public Validator<String> getFeedbackValidator() {
139
+        return new NotEmptyValidator();
140
+    }
141
+
142
+    @Override
143
+    public boolean getIncludeServerInfo() {
144
+        return includeServerInfo;
145
+    }
146
+
147
+    @Override
148
+    public void setIncludeServerInfo(final boolean includeServerInfo) {
149
+        this.includeServerInfo = includeServerInfo;
150
+        listeners.getCallable(FeedbackDialogModelListener.class).includeServerInfoChanged(
151
+                includeServerInfo);
152
+    }
153
+
154
+    @Override
155
+    public boolean getIncludeDMDircInfo() {
156
+        return includeDMDircInfo;
157
+    }
158
+
159
+    @Override
160
+    public void setIncludeDMDircInfo(final boolean includeDMDircInfo) {
161
+        this.includeDMDircInfo = includeDMDircInfo;
162
+        listeners.getCallable(FeedbackDialogModelListener.class).includeDMDircInfoChanged(
163
+                includeDMDircInfo);
164
+    }
165
+
166
+    @Override
167
+    public boolean isSaveAllowed() {
168
+        return isNameValid() && isEmailValid() && isFeedbackValid();
169
+    }
170
+
171
+    @Override
172
+    public void save() {
173
+        final StringBuilder serverInfo = new StringBuilder(255);
174
+        final StringBuilder dmdircInfo = new StringBuilder(255);
175
+        if (getIncludeServerInfo()) {
176
+            for (Connection connection : serverManager.getServers()) {
177
+                if (connection.getState().isDisconnected()) {
178
+                    continue;
179
+                }
180
+                serverInfo.append("Actual name: ").append(connection.getParser()
181
+                        .getServerName()).append("\n");
182
+                serverInfo.append("Network: ").append(connection.getNetwork())
183
+                        .append("\n");
184
+                serverInfo.append("IRCd: ").append(connection.getParser()
185
+                        .getServerSoftware()).append(" - ");
186
+                serverInfo.append(connection.getParser().getServerSoftwareType())
187
+                        .append("\n");
188
+                serverInfo.append("Modes: ").append(connection.getParser()
189
+                        .getBooleanChannelModes()).append(" ");
190
+                serverInfo.append(connection.getParser().getListChannelModes())
191
+                        .append(" ");
192
+                serverInfo.append(connection.getParser().getParameterChannelModes())
193
+                        .append(" ");
194
+                serverInfo.append(connection.getParser().
195
+                        getDoubleParameterChannelModes());
196
+            }
197
+        }
198
+        if (getIncludeDMDircInfo()) {
199
+            dmdircInfo.append("DMDirc version: ").append(ClientInfo.getVersionInformation())
200
+                    .append("\n");
201
+            dmdircInfo.append("Profile directory: ").append(configDirectory).append("\n");
202
+            dmdircInfo.append("Java version: ").append(ClientInfo.getJavaInformation())
203
+                    .append("\n");
204
+            dmdircInfo.append("OS Version: ").append(ClientInfo.getOperatingSystemInformation());
205
+        }
206
+        final FeedbackSender sender = feedbackSenderFactory.getFeedbackSender(
207
+                name.or(""), email.or(""), feedback.or(""), config.getOption("version", "version"),
208
+                serverInfo.toString(), dmdircInfo.toString());
209
+        new Thread(sender, "Feedback Sender").start();
210
+    }
211
+
212
+    @Override
213
+    public void addListener(final FeedbackDialogModelListener listener) {
214
+        listeners.add(FeedbackDialogModelListener.class, listener);
215
+    }
216
+
217
+    @Override
218
+    public void removeListener(final FeedbackDialogModelListener listener) {
219
+        listeners.remove(FeedbackDialogModelListener.class, listener);
220
+    }
221
+
222
+}

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

@@ -0,0 +1,55 @@
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.feedback;
24
+
25
+import com.dmdirc.interfaces.ui.FeedbackDialogModelListener;
26
+
27
+import com.google.common.base.Optional;
28
+
29
+/**
30
+ * An abstract adapter class for receiving feedback dialog model events. The methods in this class
31
+ * are empty. This class exists as convenience for creating listener objects.
32
+ */
33
+public class FeedbackDialogModelAdapter implements FeedbackDialogModelListener {
34
+
35
+    @Override
36
+    public void nameChanged(final Optional<String> name) {
37
+    }
38
+
39
+    @Override
40
+    public void emailChanged(final Optional<String> email) {
41
+    }
42
+
43
+    @Override
44
+    public void feedbackChanged(final Optional<String> feedback) {
45
+    }
46
+
47
+    @Override
48
+    public void includeServerInfoChanged(final boolean includeServerInfo) {
49
+    }
50
+
51
+    @Override
52
+    public void includeDMDircInfoChanged(final boolean includeDMDircInfo) {
53
+    }
54
+
55
+}

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

@@ -0,0 +1,109 @@
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.feedback;
24
+
25
+import com.dmdirc.ClientModule.GlobalConfig;
26
+import com.dmdirc.interfaces.config.AggregateConfigProvider;
27
+import com.dmdirc.interfaces.ui.StatusBar;
28
+import com.dmdirc.ui.StatusMessage;
29
+import com.dmdirc.util.annotations.factory.Factory;
30
+import com.dmdirc.util.annotations.factory.Unbound;
31
+import com.dmdirc.util.io.Downloader;
32
+
33
+import java.io.IOException;
34
+import java.util.HashMap;
35
+import java.util.List;
36
+import java.util.Map;
37
+
38
+/**
39
+ * Sends feedback back to the developers.
40
+ */
41
+@Factory(inject = true)
42
+public class FeedbackSender implements Runnable {
43
+
44
+    private final Map<String, String> postData;
45
+    private final StatusBar statusBar;
46
+    private final AggregateConfigProvider config;
47
+    private final Downloader downloader;
48
+
49
+    public FeedbackSender(
50
+            @SuppressWarnings("qualifiers") @GlobalConfig final AggregateConfigProvider config,
51
+            final Downloader downloader,
52
+            final StatusBar statusBar,
53
+            @Unbound final String name,
54
+            @Unbound final String email,
55
+            @Unbound final String feedback,
56
+            @Unbound final String version,
57
+            @Unbound final String serverInfo,
58
+            @Unbound final String dmdircInfo) {
59
+        this.downloader = downloader;
60
+        this.config = config;
61
+        this.statusBar = statusBar;
62
+        this.postData = new HashMap<>(6);
63
+
64
+        if (!name.isEmpty()) {
65
+            postData.put("name", name);
66
+        }
67
+        if (!email.isEmpty()) {
68
+            postData.put("email", email);
69
+        }
70
+        if (!feedback.isEmpty()) {
71
+            postData.put("feedback", feedback);
72
+        }
73
+        if (!version.isEmpty()) {
74
+            postData.put("version", version);
75
+        }
76
+        if (!serverInfo.isEmpty()) {
77
+            postData.put("serverInfo", serverInfo);
78
+        }
79
+        if (!dmdircInfo.isEmpty()) {
80
+            postData.put("dmdircInfo", dmdircInfo);
81
+        }
82
+    }
83
+
84
+    /**
85
+     * Sends the error data to the server appending returned information to the global error
86
+     * variable.
87
+     *
88
+     * @param postData Feedback data to send
89
+     */
90
+    private String sendData(final Map<String, String> postData) {
91
+        try {
92
+            final List<String> response = downloader.getPage("http://www.dmdirc.com/feedback.php",
93
+                    postData);
94
+            if (response.size() >= 1) {
95
+                return "Feedback sent successfully";
96
+            } else {
97
+                return "Feedback failure: Unknown response from the server.";
98
+            }
99
+        } catch (IOException ex) {
100
+            return "Feedback failure: " + ex.getMessage();
101
+        }
102
+    }
103
+
104
+    @Override
105
+    public void run() {
106
+        statusBar.setMessage(new StatusMessage(sendData(postData), config));
107
+    }
108
+
109
+}

+ 145
- 0
test/com/dmdirc/ui/core/feedback/CoreFeedbackDialogModelTest.java Переглянути файл

@@ -0,0 +1,145 @@
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.feedback;
24
+
25
+import com.dmdirc.ServerManager;
26
+import com.dmdirc.interfaces.config.AggregateConfigProvider;
27
+import com.dmdirc.interfaces.ui.FeedbackDialogModelListener;
28
+
29
+import com.google.common.base.Optional;
30
+
31
+import org.junit.Test;
32
+import org.junit.runner.RunWith;
33
+import org.mockito.Mock;
34
+import org.mockito.runners.MockitoJUnitRunner;
35
+
36
+import static org.junit.Assert.assertEquals;
37
+import static org.mockito.Mockito.verify;
38
+
39
+@RunWith(MockitoJUnitRunner.class)
40
+public class CoreFeedbackDialogModelTest {
41
+
42
+    @Mock private AggregateConfigProvider config;
43
+    @Mock private ServerManager serverManager;
44
+    @Mock private FeedbackSenderFactory feedbackSenderFactory;
45
+    @Mock private FeedbackDialogModelListener listener;
46
+    private final String name = "Bob Dole";
47
+    private final String email = "bob@dole.com";
48
+    private final String feedback = "DMDirc Rocks.";
49
+
50
+    @Test
51
+    public void testName() {
52
+        final CoreFeedbackDialogModel instance = new CoreFeedbackDialogModel(config, serverManager,
53
+                feedbackSenderFactory, "configDirectory");
54
+        assertEquals("testName", Optional.absent(), instance.getName());
55
+        instance.setName(Optional.fromNullable(name));
56
+        assertEquals("testName", Optional.fromNullable(name), instance.getName());
57
+    }
58
+
59
+    @Test
60
+    public void testEmail() {
61
+        final CoreFeedbackDialogModel instance = new CoreFeedbackDialogModel(config, serverManager,
62
+                feedbackSenderFactory, "configDirectory");
63
+        assertEquals("testEmail", Optional.absent(), instance.getEmail());
64
+        instance.setEmail(Optional.fromNullable(email));
65
+        assertEquals("testEmail", Optional.fromNullable(email), instance.getEmail());
66
+    }
67
+
68
+    @Test
69
+    public void testFeedback() {
70
+        final CoreFeedbackDialogModel instance = new CoreFeedbackDialogModel(config, serverManager,
71
+                feedbackSenderFactory, "configDirectory");
72
+        assertEquals("testFeedback", Optional.absent(), instance.getFeedback());
73
+        instance.setFeedback(Optional.fromNullable(feedback));
74
+        assertEquals("testFeedback", Optional.fromNullable(feedback), instance.getFeedback());
75
+    }
76
+
77
+    @Test
78
+    public void testServerInfo() {
79
+        final CoreFeedbackDialogModel instance = new CoreFeedbackDialogModel(config, serverManager,
80
+                feedbackSenderFactory, "configDirectory");
81
+        assertEquals("testServerInfo", false, instance.getIncludeServerInfo());
82
+        instance.setIncludeServerInfo(true);
83
+        assertEquals("testServerInfo", true, instance.getIncludeServerInfo());
84
+    }
85
+
86
+    @Test
87
+    public void testDMDircInfo() {
88
+        final CoreFeedbackDialogModel instance = new CoreFeedbackDialogModel(config, serverManager,
89
+                feedbackSenderFactory, "configDirectory");
90
+        assertEquals("testDMDircInfo", false, instance.getIncludeDMDircInfo());
91
+        instance.setIncludeDMDircInfo(true);
92
+        assertEquals("testDMDircInfo", true, instance.getIncludeDMDircInfo());
93
+    }
94
+
95
+    @Test
96
+    public void testSave() {
97
+        // TODO: fix ClientInfo being static somehow and test.
98
+    }
99
+
100
+    @Test
101
+    public void testNameListener() {
102
+        final CoreFeedbackDialogModel instance = new CoreFeedbackDialogModel(config, serverManager,
103
+                feedbackSenderFactory, "configDirectory");
104
+        instance.addListener(listener);
105
+        instance.setName(Optional.fromNullable("Bob Dole"));
106
+        verify(listener).nameChanged(Optional.fromNullable("Bob Dole"));
107
+    }
108
+
109
+    @Test
110
+    public void testEmailListener() {
111
+        final CoreFeedbackDialogModel instance = new CoreFeedbackDialogModel(config, serverManager,
112
+                feedbackSenderFactory, "configDirectory");
113
+        instance.addListener(listener);
114
+        instance.setEmail(Optional.fromNullable("bob@dole.com"));
115
+        verify(listener).emailChanged(Optional.fromNullable("bob@dole.com"));
116
+    }
117
+
118
+    @Test
119
+    public void testFeedbackListener() {
120
+        final CoreFeedbackDialogModel instance = new CoreFeedbackDialogModel(config, serverManager,
121
+                feedbackSenderFactory, "configDirectory");
122
+        instance.addListener(listener);
123
+        instance.setFeedback(Optional.fromNullable("DMDirc Rocks."));
124
+        verify(listener).feedbackChanged(Optional.fromNullable("DMDirc Rocks."));
125
+    }
126
+
127
+    @Test
128
+    public void testServerInfoListener() {
129
+        final CoreFeedbackDialogModel instance = new CoreFeedbackDialogModel(config, serverManager,
130
+                feedbackSenderFactory, "configDirectory");
131
+        instance.addListener(listener);
132
+        instance.setIncludeServerInfo(true);
133
+        verify(listener).includeServerInfoChanged(true);
134
+    }
135
+
136
+    @Test
137
+    public void testDMDircInfoListener() {
138
+        final CoreFeedbackDialogModel instance = new CoreFeedbackDialogModel(config, serverManager,
139
+                feedbackSenderFactory, "configDirectory");
140
+        instance.addListener(listener);
141
+        instance.setIncludeDMDircInfo(true);
142
+        verify(listener).includeDMDircInfoChanged(true);
143
+    }
144
+
145
+}

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