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