1 | package net.oni2.aeinstaller.gui.reporter;
|
---|
2 |
|
---|
3 | import java.awt.event.ActionEvent;
|
---|
4 | import java.awt.event.KeyEvent;
|
---|
5 | import java.io.File;
|
---|
6 | import java.text.SimpleDateFormat;
|
---|
7 | import java.util.Date;
|
---|
8 | import java.util.Properties;
|
---|
9 | import java.util.ResourceBundle;
|
---|
10 | import java.util.regex.Pattern;
|
---|
11 |
|
---|
12 | import javax.activation.DataHandler;
|
---|
13 | import javax.activation.DataSource;
|
---|
14 | import javax.activation.FileDataSource;
|
---|
15 | import javax.mail.AuthenticationFailedException;
|
---|
16 | import javax.mail.Message;
|
---|
17 | import javax.mail.Message.RecipientType;
|
---|
18 | import javax.mail.MessagingException;
|
---|
19 | import javax.mail.Multipart;
|
---|
20 | import javax.mail.Session;
|
---|
21 | import javax.mail.Transport;
|
---|
22 | import javax.mail.internet.InternetAddress;
|
---|
23 | import javax.mail.internet.MimeBodyPart;
|
---|
24 | import javax.mail.internet.MimeMessage;
|
---|
25 | import javax.mail.internet.MimeMultipart;
|
---|
26 | import javax.swing.AbstractAction;
|
---|
27 | import javax.swing.JCheckBox;
|
---|
28 | import javax.swing.JComponent;
|
---|
29 | import javax.swing.JDialog;
|
---|
30 | import javax.swing.JTextArea;
|
---|
31 | import javax.swing.JTextField;
|
---|
32 | import javax.swing.KeyStroke;
|
---|
33 |
|
---|
34 | import net.oni2.aeinstaller.backend.LogPrintStream;
|
---|
35 | import net.oni2.aeinstaller.backend.Paths;
|
---|
36 | import net.oni2.aeinstaller.backend.StringDataSource;
|
---|
37 | import net.oni2.resourcebundle.UTF8ResourceBundleLoader;
|
---|
38 | import net.oni2.swingcomponents.HTMLLinkLabel;
|
---|
39 |
|
---|
40 | import org.apache.commons.io.FileUtils;
|
---|
41 | import org.apache.commons.io.filefilter.IOFileFilter;
|
---|
42 | import org.apache.commons.io.filefilter.NameFileFilter;
|
---|
43 | import org.apache.commons.io.filefilter.NotFileFilter;
|
---|
44 | import org.apache.commons.io.filefilter.TrueFileFilter;
|
---|
45 | import org.javabuilders.BuildResult;
|
---|
46 | import org.javabuilders.annotations.DoInBackground;
|
---|
47 | import org.javabuilders.event.BackgroundEvent;
|
---|
48 | import org.javabuilders.swing.SwingJavaBuilder;
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * @author Christian Illy
|
---|
52 | */
|
---|
53 | public 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 | fileList.append(name + "\n");
|
---|
226 | }
|
---|
227 |
|
---|
228 | mimeBody = new MimeBodyPart();
|
---|
229 | source = new StringDataSource(fileList.toString());
|
---|
230 | mimeBody.setDataHandler(new DataHandler(source));
|
---|
231 | mimeBody.setFileName("filelist.txt");
|
---|
232 | multipart.addBodyPart(mimeBody);
|
---|
233 |
|
---|
234 | // Build text part
|
---|
235 | msgText.append("\n\nMessage:\n" + txtMessage.getText());
|
---|
236 | mimeBody = new MimeBodyPart();
|
---|
237 | mimeBody.setText(msgText.toString());
|
---|
238 | multipart.addBodyPart(mimeBody, 0);
|
---|
239 |
|
---|
240 | msg.setContent(multipart);
|
---|
241 |
|
---|
242 | Transport.send(msg);
|
---|
243 |
|
---|
244 | return true;
|
---|
245 | } catch (AuthenticationFailedException e) {
|
---|
246 | e.printStackTrace();
|
---|
247 | } catch (MessagingException e) {
|
---|
248 | e.printStackTrace();
|
---|
249 | }
|
---|
250 | return false;
|
---|
251 | }
|
---|
252 | }
|
---|