source: java/installer2/src/net/oni2/aeinstaller/gui/reporter/ReporterDialog.java@ 1016

Last change on this file since 1016 was 1011, checked in by alloc, 10 years ago

Fixes #91

File size: 8.3 KB
Line 
1package net.oni2.aeinstaller.gui.reporter;
2
3import java.awt.event.ActionEvent;
4import java.awt.event.KeyEvent;
5import java.io.File;
6import java.text.SimpleDateFormat;
7import java.util.Date;
8import java.util.Properties;
9import java.util.ResourceBundle;
10import java.util.regex.Pattern;
11
12import javax.activation.DataHandler;
13import javax.activation.DataSource;
14import javax.activation.FileDataSource;
15import javax.mail.AuthenticationFailedException;
16import javax.mail.Message;
17import javax.mail.Message.RecipientType;
18import javax.mail.MessagingException;
19import javax.mail.Multipart;
20import javax.mail.Session;
21import javax.mail.Transport;
22import javax.mail.internet.InternetAddress;
23import javax.mail.internet.MimeBodyPart;
24import javax.mail.internet.MimeMessage;
25import javax.mail.internet.MimeMultipart;
26import javax.swing.AbstractAction;
27import javax.swing.JCheckBox;
28import javax.swing.JComponent;
29import javax.swing.JDialog;
30import javax.swing.JTextArea;
31import javax.swing.JTextField;
32import javax.swing.KeyStroke;
33
34import net.oni2.aeinstaller.backend.LogPrintStream;
35import net.oni2.aeinstaller.backend.Paths;
36import net.oni2.aeinstaller.backend.StringDataSource;
37import net.oni2.resourcebundle.UTF8ResourceBundleLoader;
38import net.oni2.swingcomponents.HTMLLinkLabel;
39
40import org.apache.commons.io.FileUtils;
41import org.apache.commons.io.filefilter.IOFileFilter;
42import org.apache.commons.io.filefilter.NameFileFilter;
43import org.apache.commons.io.filefilter.NotFileFilter;
44import org.apache.commons.io.filefilter.TrueFileFilter;
45import org.javabuilders.BuildResult;
46import org.javabuilders.annotations.DoInBackground;
47import org.javabuilders.event.BackgroundEvent;
48import org.javabuilders.swing.SwingJavaBuilder;
49
50/**
51 * @author Christian Illy
52 */
53public class ReporterDialog extends JDialog {
54 private static final long serialVersionUID = -5719515325671846620L;
55
56 private ResourceBundle bundle = UTF8ResourceBundleLoader
57 .getBundle("net.oni2.aeinstaller.localization."
58 + getClass().getSimpleName());
59 @SuppressWarnings("unused")
60 private BuildResult result = SwingJavaBuilder.build(this, bundle);
61
62 private HTMLLinkLabel lblInfo;
63 private HTMLLinkLabel lblFiles;
64 private JTextField txtMail;
65 private JTextArea txtMessage;
66 private JCheckBox chkGetCopy;
67
68 private final String SMTP_HOST_NAME = "mail.illy.bz";
69 private final String EMAIL_SUBJECT = "AE support request";
70 private final String EMAIL_TO = "ae-support@oni2.net";
71
72 /**
73 * Open the settings
74 */
75 public ReporterDialog() {
76 lblInfo.setText(bundle.getString("lblInfo"));
77 lblFiles.setText(bundle.getString("lblFiles"));
78
79 AbstractAction closeAction = new AbstractAction() {
80
81 private static final long serialVersionUID = 1L;
82
83 public void actionPerformed(ActionEvent arg0) {
84 dispose();
85 }
86 };
87 KeyStroke ksCtrlW = KeyStroke
88 .getKeyStroke('W', KeyEvent.CTRL_DOWN_MASK);
89 getRootPane()
90 .getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
91 .put(ksCtrlW, "close");
92 getRootPane().getActionMap().put("close", closeAction);
93
94 setLocationRelativeTo(null);
95
96 initFields();
97 }
98
99 private void initFields() {
100 }
101
102 @DoInBackground(progressMessage = "send.title", cancelable = false, indeterminateProgress = true)
103 private boolean send(final BackgroundEvent evt) {
104 try {
105 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
106
107 StringBuffer msgText = new StringBuffer();
108 msgText.append("Support request\nTime: " + sdf.format(new Date())
109 + "\n\n");
110
111 // Set the host smtp address
112 Properties props = new Properties();
113 props.put("mail.smtp.host", SMTP_HOST_NAME);
114 Session session = Session.getDefaultInstance(props);
115
116 session.setDebug(false);
117
118 // create a message
119 Message msg = new MimeMessage(session);
120 msg.setFrom(new InternetAddress(txtMail.getText()));
121 msg.setRecipient(RecipientType.TO, new InternetAddress(EMAIL_TO));
122 if (chkGetCopy.isSelected())
123 msg.addRecipient(RecipientType.CC,
124 new InternetAddress(txtMail.getText()));
125 msg.setSubject(EMAIL_SUBJECT);
126
127 // Build multipart
128 Multipart multipart = new MimeMultipart();
129
130 // aei_output.log (live from string)
131 MimeBodyPart mimeBody = new MimeBodyPart();
132 DataSource source = new StringDataSource(LogPrintStream
133 .getInstance().getLog());
134 mimeBody.setDataHandler(new DataHandler(source));
135 mimeBody.setFileName("aei_output.log");
136 multipart.addBodyPart(mimeBody);
137
138 // updater_output.log
139 File f = new File(Paths.getInstallerPath(), "updater_output.log");
140 if (f.exists()) {
141 mimeBody = new MimeBodyPart();
142 source = new FileDataSource(f);
143 mimeBody.setDataHandler(new DataHandler(source));
144 mimeBody.setFileName("updater_output.log");
145 multipart.addBodyPart(mimeBody);
146 } else {
147 msgText.append("updater_output.log does not exist!\n");
148 }
149
150 // Initialization.log
151 f = new File(Paths.getInstallerPath(), "Initialization.log");
152 if (f.exists()) {
153 mimeBody = new MimeBodyPart();
154 source = new FileDataSource(f);
155 mimeBody.setDataHandler(new DataHandler(source));
156 mimeBody.setFileName("Initialization.log");
157 multipart.addBodyPart(mimeBody);
158 } else {
159 msgText.append("Initialization.log does not exist!\n");
160 }
161
162 // Installation.log
163 f = new File(Paths.getInstallerPath(), "Installation.log");
164 if (f.exists()) {
165 mimeBody = new MimeBodyPart();
166 source = new FileDataSource(f);
167 mimeBody.setDataHandler(new DataHandler(source));
168 mimeBody.setFileName("Installation.log");
169 multipart.addBodyPart(mimeBody);
170 } else {
171 msgText.append("Installation.log does not exist!\n");
172 }
173
174 // daodan.ini
175 f = new File(Paths.getEditionBasePath(), "daodan.ini");
176 if (f.exists()) {
177 mimeBody = new MimeBodyPart();
178 source = new FileDataSource(f);
179 mimeBody.setDataHandler(new DataHandler(source));
180 mimeBody.setFileName("daodan.ini");
181 multipart.addBodyPart(mimeBody);
182 } else {
183 msgText.append("daodan.ini does not exist!\n");
184 }
185
186 // startup.txt
187 f = new File(Paths.getEditionBasePath(), "startup.txt");
188 if (f.exists()) {
189 mimeBody = new MimeBodyPart();
190 source = new FileDataSource(f);
191 mimeBody.setDataHandler(new DataHandler(source));
192 mimeBody.setFileName("startup.txt");
193 multipart.addBodyPart(mimeBody);
194 } else {
195 msgText.append("startup.txt does not exist!\n");
196 }
197
198 // debugger.txt
199 f = new File(Paths.getEditionBasePath(), "debugger.txt");
200 if (f.exists()) {
201 mimeBody = new MimeBodyPart();
202 source = new FileDataSource(f);
203 mimeBody.setDataHandler(new DataHandler(source));
204 mimeBody.setFileName("debugger.txt");
205 multipart.addBodyPart(mimeBody);
206 } else {
207 msgText.append("debugger.txt does not exist!\n");
208 }
209
210 // File list (live from string)
211 StringBuffer fileList = new StringBuffer();
212 int baseLength = Paths.getEditionBasePath().getAbsolutePath()
213 .length();
214 IOFileFilter svnDirFilter = new NameFileFilter(
215 new String[] { ".svn" });
216 IOFileFilter notFilter = new NotFileFilter(svnDirFilter);
217 Pattern packagePattern = Pattern.compile(
218 ".*/packages/[0-9]{5}[^/]*/.+", Pattern.CASE_INSENSITIVE);
219 for (File flF : FileUtils.listFilesAndDirs(
220 Paths.getEditionBasePath(), TrueFileFilter.INSTANCE,
221 notFilter)) {
222 String name = flF.getAbsolutePath().substring(baseLength);
223 name = name.replace('\\', '/');
224 if (!packagePattern.matcher(name).matches()) {
225 if (flF.isFile())
226 fileList.append(name + "\t" + flF.length() + "\n");
227 else
228 fileList.append(name + "\n");
229 }
230 }
231
232 mimeBody = new MimeBodyPart();
233 source = new StringDataSource(fileList.toString());
234 mimeBody.setDataHandler(new DataHandler(source));
235 mimeBody.setFileName("filelist.txt");
236 multipart.addBodyPart(mimeBody);
237
238 // Build text part
239 msgText.append("\n\nMessage:\n" + txtMessage.getText());
240 mimeBody = new MimeBodyPart();
241 mimeBody.setText(msgText.toString());
242 multipart.addBodyPart(mimeBody, 0);
243
244 msg.setContent(multipart);
245
246 Transport.send(msg);
247
248 return true;
249 } catch (AuthenticationFailedException e) {
250 e.printStackTrace();
251 } catch (MessagingException e) {
252 e.printStackTrace();
253 }
254 return false;
255 }
256}
Note: See TracBrowser for help on using the repository browser.