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

Last change on this file since 1170 was 1034, checked in by alloc, 9 years ago

2.26: Support mails have a date now

File size: 8.4 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 msg.setSentDate(new Date ());
127
128 // Build multipart
129 Multipart multipart = new MimeMultipart();
130
131 // aei_output.log (live from string)
132 MimeBodyPart mimeBody = new MimeBodyPart();
133 DataSource source = new StringDataSource(LogPrintStream
134 .getInstance().getLog());
135 mimeBody.setDataHandler(new DataHandler(source));
136 mimeBody.setFileName("aei_output.log");
137 multipart.addBodyPart(mimeBody);
138
139 // updater_output.log
140 File f = new File(Paths.getInstallerPath(), "updater_output.log");
141 if (f.exists()) {
142 mimeBody = new MimeBodyPart();
143 source = new FileDataSource(f);
144 mimeBody.setDataHandler(new DataHandler(source));
145 mimeBody.setFileName("updater_output.log");
146 multipart.addBodyPart(mimeBody);
147 } else {
148 msgText.append("updater_output.log does not exist!\n");
149 }
150
151 // Initialization.log
152 f = new File(Paths.getInstallerPath(), "Initialization.log");
153 if (f.exists()) {
154 mimeBody = new MimeBodyPart();
155 source = new FileDataSource(f);
156 mimeBody.setDataHandler(new DataHandler(source));
157 mimeBody.setFileName("Initialization.log");
158 multipart.addBodyPart(mimeBody);
159 } else {
160 msgText.append("Initialization.log does not exist!\n");
161 }
162
163 // Installation.log
164 f = new File(Paths.getInstallerPath(), "Installation.log");
165 if (f.exists()) {
166 mimeBody = new MimeBodyPart();
167 source = new FileDataSource(f);
168 mimeBody.setDataHandler(new DataHandler(source));
169 mimeBody.setFileName("Installation.log");
170 multipart.addBodyPart(mimeBody);
171 } else {
172 msgText.append("Installation.log does not exist!\n");
173 }
174
175 // daodan.ini
176 f = new File(Paths.getEditionBasePath(), "daodan.ini");
177 if (f.exists()) {
178 mimeBody = new MimeBodyPart();
179 source = new FileDataSource(f);
180 mimeBody.setDataHandler(new DataHandler(source));
181 mimeBody.setFileName("daodan.ini");
182 multipart.addBodyPart(mimeBody);
183 } else {
184 msgText.append("daodan.ini does not exist!\n");
185 }
186
187 // startup.txt
188 f = new File(Paths.getEditionBasePath(), "startup.txt");
189 if (f.exists()) {
190 mimeBody = new MimeBodyPart();
191 source = new FileDataSource(f);
192 mimeBody.setDataHandler(new DataHandler(source));
193 mimeBody.setFileName("startup.txt");
194 multipart.addBodyPart(mimeBody);
195 } else {
196 msgText.append("startup.txt does not exist!\n");
197 }
198
199 // debugger.txt
200 f = new File(Paths.getEditionBasePath(), "debugger.txt");
201 if (f.exists()) {
202 mimeBody = new MimeBodyPart();
203 source = new FileDataSource(f);
204 mimeBody.setDataHandler(new DataHandler(source));
205 mimeBody.setFileName("debugger.txt");
206 multipart.addBodyPart(mimeBody);
207 } else {
208 msgText.append("debugger.txt does not exist!\n");
209 }
210
211 // File list (live from string)
212 StringBuffer fileList = new StringBuffer();
213 int baseLength = Paths.getEditionBasePath().getAbsolutePath()
214 .length();
215 IOFileFilter svnDirFilter = new NameFileFilter(
216 new String[] { ".svn" });
217 IOFileFilter notFilter = new NotFileFilter(svnDirFilter);
218 Pattern packagePattern = Pattern.compile(
219 ".*/packages/[0-9]{5}[^/]*/.+", Pattern.CASE_INSENSITIVE);
220 Pattern jrePattern = Pattern.compile(
221 ".*/JRE/.+", Pattern.CASE_INSENSITIVE);
222 for (File flF : FileUtils.listFilesAndDirs(
223 Paths.getEditionBasePath(), TrueFileFilter.INSTANCE,
224 notFilter)) {
225 String name = flF.getAbsolutePath().substring(baseLength);
226 name = name.replace('\\', '/');
227 if (!packagePattern.matcher(name).matches() && !jrePattern.matcher(name).matches()) {
228 if (flF.isFile())
229 fileList.append(name + "\t" + flF.length() + "\n");
230 else
231 fileList.append(name + "\n");
232 }
233 }
234
235 mimeBody = new MimeBodyPart();
236 source = new StringDataSource(fileList.toString());
237 mimeBody.setDataHandler(new DataHandler(source));
238 mimeBody.setFileName("filelist.txt");
239 multipart.addBodyPart(mimeBody);
240
241 // Build text part
242 msgText.append("\n\nMessage:\n" + txtMessage.getText());
243 mimeBody = new MimeBodyPart();
244 mimeBody.setText(msgText.toString());
245 multipart.addBodyPart(mimeBody, 0);
246
247 msg.setContent(multipart);
248
249 Transport.send(msg);
250
251 return true;
252 } catch (AuthenticationFailedException e) {
253 e.printStackTrace();
254 } catch (MessagingException e) {
255 e.printStackTrace();
256 }
257 return false;
258 }
259}
Note: See TracBrowser for help on using the repository browser.