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

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