You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SendWorker.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Copyright (c) 2006-2013 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. package com.dmdirc.addons.ui_swing.components;
  23. import com.dmdirc.addons.ui_swing.dialogs.FeedbackDialog;
  24. import com.dmdirc.util.io.Downloader;
  25. import java.io.IOException;
  26. import java.net.MalformedURLException;
  27. import java.util.HashMap;
  28. import java.util.List;
  29. import java.util.Map;
  30. /**
  31. * Sends feedback worker thread.
  32. */
  33. public class SendWorker extends LoggingSwingWorker<Object, Void> {
  34. /** Parent feedback dialog. */
  35. private final FeedbackDialog dialog;
  36. /** Name. */
  37. private final String name;
  38. /** Email. */
  39. private final String email;
  40. /** Feedback. */
  41. private final String feedback;
  42. /** Server name. */
  43. private final String serverInfo;
  44. /** DMDirc Info. */
  45. private final String dmdircInfo;
  46. /** Error/Success message. */
  47. private final StringBuilder error;
  48. /**
  49. * Creates a new send worker to send feedback.
  50. *
  51. * @param dialog Parent feedback dialog
  52. * @param name Name
  53. * @param email Email
  54. * @param feedback Feedback
  55. */
  56. public SendWorker(final FeedbackDialog dialog, final String name,
  57. final String email, final String feedback) {
  58. this(dialog, name, email, feedback, "", "");
  59. }
  60. /**
  61. * Creates a new send worker to send feedback.
  62. *
  63. * @param dialog Parent feedback dialog
  64. * @param name Name
  65. * @param email Email
  66. * @param feedback Feedback
  67. * @param serverInfo serverInfo
  68. * @param dmdircInfo DMDirc info
  69. */
  70. public SendWorker(final FeedbackDialog dialog, final String name,
  71. final String email, final String feedback,
  72. final String serverInfo, final String dmdircInfo) {
  73. super();
  74. this.dialog = dialog;
  75. this.name = name;
  76. this.email = email;
  77. this.feedback = feedback;
  78. this.serverInfo = serverInfo;
  79. this.dmdircInfo = dmdircInfo;
  80. error = new StringBuilder();
  81. }
  82. /** {@inheritDoc} */
  83. @Override
  84. protected Object doInBackground() {
  85. final Map<String, String> postData =
  86. new HashMap<String, String>();
  87. if (!name.isEmpty()) {
  88. postData.put("name", name);
  89. }
  90. if (!email.isEmpty()) {
  91. postData.put("email", email);
  92. }
  93. if (!feedback.isEmpty()) {
  94. postData.put("feedback", feedback);
  95. }
  96. postData.put("version", dialog.getController().getGlobalConfig()
  97. .getOption("version", "version"));
  98. if (!serverInfo.isEmpty()) {
  99. postData.put("serverInfo", serverInfo);
  100. }
  101. if (!dmdircInfo.isEmpty()) {
  102. postData.put("dmdircInfo", dmdircInfo);
  103. }
  104. sendData(postData);
  105. return error;
  106. }
  107. /**
  108. * Sends the error data to the server appending returned information to the
  109. * global error variable.
  110. *
  111. * @param postData Feedback data to send
  112. */
  113. private void sendData(final Map<String, String> postData) {
  114. try {
  115. final List<String> response =
  116. Downloader.getPage("http://www.dmdirc.com/feedback.php",
  117. postData);
  118. if (response.size() >= 1) {
  119. for (final String responseLine : response) {
  120. error.append(responseLine).append("\n");
  121. }
  122. } else {
  123. error.append("Failure: Unknown response from the server.");
  124. }
  125. } catch (final MalformedURLException ex) {
  126. error.append("Malformed feedback URL.");
  127. } catch (final IOException ex) {
  128. error.append("Failure: ").append(ex.getMessage());
  129. }
  130. }
  131. /** {@inheritDoc} */
  132. @Override
  133. protected void done() {
  134. super.done();
  135. dialog.layoutComponents2(error);
  136. }
  137. }