Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

SendWorker.java 5.0KB

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