use of org.jivesoftware.spark.util.ByteFormat in project Spark by igniterealtime.
the class ReceiveFileTransfer method acceptFileTransfer.
public void acceptFileTransfer(final FileTransferRequest request) {
String fileName = request.getFileName();
long fileSize = request.getFileSize();
String requestor = request.getRequestor();
String bareJID = XmppStringUtils.parseBareJid(requestor);
// SPARK-1869
FileTransferNegotiator.getInstanceFor(SparkManager.getConnection());
FileTransferNegotiator.IBB_ONLY = SettingsManager.getLocalPreferences().isFileTransferIbbOnly();
ByteFormat format = new ByteFormat();
String text = format.format(fileSize);
fileLabel.setText(fileName + " (" + text + ")");
ContactList contactList = SparkManager.getWorkspace().getContactList();
ContactItem contactItem = contactList.getContactItemByJID(bareJID);
titleLabel.setText(Res.getString("message.user.is.sending.you.a.file", contactItem.getDisplayName()));
File tempFile = new File(Spark.getSparkUserHome(), "/tmp");
try {
tempFile.mkdirs();
File file = new File(tempFile, fileName);
file.delete();
BufferedWriter out = new BufferedWriter(new FileWriter(file));
out.write("a");
out.close();
imageLabel.setIcon(GraphicUtils.getIcon(file));
// Delete temp file when program exits.
file.delete();
} catch (IOException e) {
imageLabel.setIcon(SparkRes.getImageIcon(SparkRes.DOCUMENT_INFO_32x32));
Log.error(e);
}
acceptButton.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
try {
Downloads.checkDownloadDirectory();
acceptRequest(request);
} catch (Exception ex) {
// this means there is a problem with the download directory
try {
request.reject();
} catch (SmackException.NotConnectedException e1) {
Log.warning("Unable to reject the request.", ex);
}
setBackground(new Color(239, 245, 250));
acceptButton.setVisible(false);
declineButton.setVisible(false);
if (Downloads.getDownloadDirectory() == null) {
fileLabel.setText("");
} else {
ResourceUtils.resLabel(fileLabel, null, Res.getString("label.transfer.download.directory") + " " + Downloads.getDownloadDirectory().getAbsolutePath());
}
// option to set a new path for the file-download
pathButton.setVisible(true);
pathButton.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
Preference p = SparkManager.getPreferenceManager().getPreference(new FileTransferPreference().getNamespace());
// retrieve the filetransfer preferences and show the preference menu
// to the user
SparkManager.getPreferenceManager().showPreferences(p);
}
});
titleLabel.setText(ex.getMessage());
titleLabel.setForeground(new Color(65, 139, 179));
invalidate();
validate();
repaint();
}
}
});
declineButton.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
rejectRequest(request);
}
});
}
use of org.jivesoftware.spark.util.ByteFormat in project Spark by igniterealtime.
the class CheckUpdates method downloadUpdate.
public void downloadUpdate(final File downloadedFile, final SparkVersion version) {
final java.util.Timer timer = new java.util.Timer();
// Prepare HTTP post
final GetMethod post = new GetMethod(version.getDownloadURL());
// Get HTTP client
Protocol.registerProtocol("https", new Protocol("https", new EasySSLProtocolSocketFactory(), 443));
final HttpClient httpclient = new HttpClient();
String proxyHost = System.getProperty("http.proxyHost");
String proxyPort = System.getProperty("http.proxyPort");
if (ModelUtil.hasLength(proxyHost) && ModelUtil.hasLength(proxyPort)) {
try {
httpclient.getHostConfiguration().setProxy(proxyHost, Integer.parseInt(proxyPort));
} catch (NumberFormatException e) {
Log.error(e);
}
}
try {
int result = httpclient.executeMethod(post);
if (result != 200) {
return;
}
long length = post.getResponseContentLength();
int contentLength = (int) length;
bar = new JProgressBar(0, contentLength);
} catch (IOException e) {
Log.error(e);
}
final JFrame frame = new JFrame(Res.getString("title.downloading.im.client"));
frame.setIconImage(SparkRes.getImageIcon(SparkRes.SMALL_MESSAGE_IMAGE).getImage());
titlePanel = new TitlePanel(Res.getString("title.upgrading.client"), Res.getString("message.version", version.getVersion()), SparkRes.getImageIcon(SparkRes.SEND_FILE_24x24), true);
final Thread thread = new Thread(() -> {
try {
InputStream stream = post.getResponseBodyAsStream();
long size = post.getResponseContentLength();
ByteFormat formater = new ByteFormat();
sizeText = formater.format(size);
titlePanel.setDescription(Res.getString("message.version", version.getVersion()) + " \n" + Res.getString("message.file.size", sizeText));
downloadedFile.getParentFile().mkdirs();
FileOutputStream out = new FileOutputStream(downloadedFile);
copy(stream, out);
out.close();
if (!cancel) {
downloadComplete = true;
promptForInstallation(downloadedFile, Res.getString("title.download.complete"), Res.getString("message.restart.spark"));
} else {
out.close();
downloadedFile.delete();
}
UPDATING = false;
frame.dispose();
} catch (Exception ex) {
// Nothing to do
} finally {
timer.cancel();
// Release current connection to the connection pool once you are done
post.releaseConnection();
}
});
frame.getContentPane().setLayout(new GridBagLayout());
frame.getContentPane().add(titlePanel, new GridBagConstraints(0, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
frame.getContentPane().add(bar, new GridBagConstraints(0, 1, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
JEditorPane pane = new JEditorPane();
boolean displayContentPane = version.getChangeLogURL() != null || version.getDisplayMessage() != null;
try {
pane.setEditable(false);
if (version.getChangeLogURL() != null) {
pane.setEditorKit(new HTMLEditorKit());
pane.setPage(version.getChangeLogURL());
} else if (version.getDisplayMessage() != null) {
pane.setText(version.getDisplayMessage());
}
if (displayContentPane) {
frame.getContentPane().add(new JScrollPane(pane), new GridBagConstraints(0, 2, 1, 1, 1.0, 1.0, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0));
}
} catch (IOException e) {
Log.error(e);
}
frame.getContentPane().setBackground(Color.WHITE);
frame.pack();
if (displayContentPane) {
frame.setSize(600, 400);
} else {
frame.setSize(400, 100);
}
frame.setLocationRelativeTo(SparkManager.getMainWindow());
GraphicUtils.centerWindowOnScreen(frame);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent) {
thread.interrupt();
cancel = true;
UPDATING = false;
if (!downloadComplete) {
UIManager.put("OptionPane.okButtonText", Res.getString("ok"));
JOptionPane.showMessageDialog(SparkManager.getMainWindow(), Res.getString("message.updating.cancelled"), Res.getString("title.cancelled"), JOptionPane.ERROR_MESSAGE);
}
}
});
frame.setVisible(true);
thread.start();
timer.scheduleAtFixedRate(new TimerTask() {
int seconds = 1;
public void run() {
ByteFormat formatter = new ByteFormat();
long value = bar.getValue();
long average = value / seconds;
String text = formatter.format(average) + "/Sec";
String total = formatter.format(value);
titlePanel.setDescription(Res.getString("message.version", version.getVersion()) + " \n" + Res.getString("message.file.size", sizeText) + "\n" + Res.getString("message.transfer.rate") + ": " + text + "\n" + Res.getString("message.total.downloaded") + ": " + total);
seconds++;
}
}, 1000, 1000);
}
use of org.jivesoftware.spark.util.ByteFormat in project Spark by igniterealtime.
the class ReceiveFileTransfer method acceptRequest.
private void acceptRequest(final FileTransferRequest request) {
String requestor = request.getRequestor();
String bareJID = XmppStringUtils.parseBareJid(requestor);
ContactList contactList = SparkManager.getWorkspace().getContactList();
final ContactItem contactItem = contactList.getContactItemByJID(bareJID);
setBackground(new Color(239, 245, 250));
acceptButton.setVisible(false);
declineButton.setVisible(false);
titleLabel.setText(Res.getString("message.negotiate.file.transfer"));
titleLabel.setForeground(new Color(65, 139, 179));
add(progressBar, new GridBagConstraints(1, 2, 2, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 5, 0, 5), 150, 0));
add(progressLabel, new GridBagConstraints(1, 3, 2, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 5, 0, 5), 150, 0));
add(cancelButton, new GridBagConstraints(1, 4, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 5, 5, 5), 0, 0));
cancelButton.setVisible(true);
transfer = request.accept();
final File downloadedFile = new File(Downloads.getDownloadDirectory(), request.getFileName());
try {
_starttime = System.currentTimeMillis();
transfer.recieveFile(downloadedFile);
} catch (SmackException | IOException e) {
Log.error(e);
}
// setting it to percent
progressBar.setMaximum(100);
progressBar.setStringPainted(true);
final Timer timer = new Timer();
TimerTask updateProgessBar = new TimerTask() {
@Override
public void run() {
if (transfer.getAmountWritten() >= request.getFileSize() || transfer.getStatus() == FileTransfer.Status.error || transfer.getStatus() == FileTransfer.Status.refused || transfer.getStatus() == FileTransfer.Status.cancelled || transfer.getStatus() == FileTransfer.Status.complete) {
this.cancel();
timer.cancel();
_endtime = System.currentTimeMillis();
updateonFinished(request, downloadedFile);
} else {
// 100 % = Filesize
// x % = Currentsize
long p = (transfer.getAmountWritten() * 100 / transfer.getFileSize());
progressBar.setValue(Math.round(p));
}
}
};
final Timer timer2 = new Timer();
TimerTask updatePrograssBarText = new TimerTask() {
long timenow;
long timeearlier;
long bytesnow;
long bytesearlier;
@Override
public void run() {
if (transfer.getAmountWritten() >= request.getFileSize() || transfer.getStatus() == FileTransfer.Status.error || transfer.getStatus() == FileTransfer.Status.refused || transfer.getStatus() == FileTransfer.Status.cancelled || transfer.getStatus() == FileTransfer.Status.complete) {
this.cancel();
timer2.cancel();
} else {
timenow = System.currentTimeMillis();
bytesnow = transfer.getAmountWritten();
bytesRead = transfer.getAmountWritten();
if (bytesRead == -1) {
bytesRead = 0;
}
ByteFormat format = new ByteFormat();
String text = format.format(bytesRead);
FileTransfer.Status status = transfer.getStatus();
if (status == FileTransfer.Status.in_progress) {
titleLabel.setText(Res.getString("message.receiving.file", contactItem.getDisplayName()));
String speed = TransferUtils.calculateSpeed(bytesnow - bytesearlier, timenow - timeearlier);
String est = TransferUtils.calculateEstimate(bytesnow, transfer.getFileSize(), _starttime, System.currentTimeMillis());
progressLabel.setText(Res.getString("message.transfer.progressbar.text.received", text, speed, est));
} else if (status == FileTransfer.Status.negotiating_stream) {
titleLabel.setText(Res.getString("message.negotiate.stream"));
}
bytesearlier = bytesnow;
timeearlier = timenow;
}
}
};
timer.scheduleAtFixedRate(updateProgessBar, 10, 10);
timer2.scheduleAtFixedRate(updatePrograssBarText, 10, 500);
}
use of org.jivesoftware.spark.util.ByteFormat in project Spark by igniterealtime.
the class SendFileTransfer method updateBar.
private void updateBar(final OutgoingFileTransfer transfer, String nickname, String kBperSecond) {
FileTransfer.Status status = transfer.getStatus();
if (status == Status.negotiating_stream) {
titleLabel.setText(Res.getString("message.negotiation.file.transfer", nickname));
} else if (status == Status.error) {
if (transfer.getException() != null) {
Log.error("Error occured during file transfer.", transfer.getException());
}
progressBar.setVisible(false);
progressLabel.setVisible(false);
titleLabel.setText(Res.getString("message.unable.to.send.file", nickname));
cancelButton.setVisible(false);
retryButton.setVisible(true);
showAlert(true);
} else if (status == Status.in_progress) {
titleLabel.setText(Res.getString("message.sending.file.to", nickname));
showAlert(false);
if (!progressBar.isVisible()) {
progressBar.setVisible(true);
progressLabel.setVisible(true);
}
try {
SwingUtilities.invokeAndWait(() -> {
// 100 % = Filesize
// x % = Currentsize
long p = (transfer.getBytesSent() * 100 / transfer.getFileSize());
progressBar.setValue(Math.round(p));
});
} catch (Exception e) {
Log.error(e);
}
ByteFormat format = new ByteFormat();
String bytesSent = format.format(transfer.getBytesSent());
String est = TransferUtils.calculateEstimate(transfer.getBytesSent(), transfer.getFileSize(), _starttime, System.currentTimeMillis());
progressLabel.setText(Res.getString("message.transfer.progressbar.text.sent", bytesSent, kBperSecond, est));
} else if (status == Status.complete) {
progressBar.setVisible(false);
String fin = TransferUtils.convertSecondstoHHMMSS(Math.round(System.currentTimeMillis() - _starttime) / 1000);
progressLabel.setText(Res.getString("label.time", fin));
titleLabel.setText(Res.getString("message.you.have.sent", nickname));
cancelButton.setVisible(false);
showAlert(true);
} else if (status == Status.cancelled) {
progressBar.setVisible(false);
progressLabel.setVisible(false);
titleLabel.setText(Res.getString("message.file.transfer.canceled"));
cancelButton.setVisible(false);
retryButton.setVisible(true);
showAlert(true);
} else if (status == Status.refused) {
progressBar.setVisible(false);
progressLabel.setVisible(false);
titleLabel.setText(Res.getString("message.file.transfer.rejected", nickname));
cancelButton.setVisible(false);
retryButton.setVisible(true);
showAlert(true);
}
}
use of org.jivesoftware.spark.util.ByteFormat in project Spark by igniterealtime.
the class SendFileTransfer method sendFile.
public void sendFile(final OutgoingFileTransfer transfer, FileTransferManager transferManager, final String jid, final String nickname) {
this.transferManager = transferManager;
// SPARK-1869
FileTransferNegotiator.getInstanceFor(SparkManager.getConnection());
FileTransferNegotiator.IBB_ONLY = SettingsManager.getLocalPreferences().isFileTransferIbbOnly();
cancelButton.setVisible(true);
retryButton.setVisible(false);
this.fullJID = jid;
this.nickname = nickname;
this.transfer = transfer;
String fileName = transfer.getFileName();
long fileSize = transfer.getFileSize();
ByteFormat format = new ByteFormat();
String text = format.format(fileSize);
fileToSend = new File(transfer.getFilePath());
imageLabel.setFile(fileToSend);
fileLabel.setText(fileName + " (" + text + ")");
ContactList contactList = SparkManager.getWorkspace().getContactList();
ContactItem contactItem = contactList.getContactItemByJID(jid);
titleLabel.setText(Res.getString("message.transfer.waiting.on.user", contactItem.getDisplayName()));
if (isImage(fileName)) {
try {
URL imageURL = new File(transfer.getFilePath()).toURI().toURL();
ImageIcon image = new ImageIcon(imageURL);
image = GraphicUtils.scaleImageIcon(image, 64, 64);
imageLabel.setIcon(image);
} catch (MalformedURLException e) {
Log.error("Could not locate image.", e);
imageLabel.setIcon(SparkRes.getImageIcon(SparkRes.DOCUMENT_INFO_32x32));
}
} else {
File file = new File(transfer.getFilePath());
Icon icon = GraphicUtils.getIcon(file);
imageLabel.setIcon(icon);
}
cancelButton.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent mouseEvent) {
transfer.cancel();
}
});
cancelButton.addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent e) {
cancelButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
}
public void mouseExited(MouseEvent e) {
cancelButton.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
}
});
progressBar.setMaximum(100);
progressBar.setVisible(false);
progressBar.setStringPainted(true);
add(progressBar, new GridBagConstraints(1, 2, 2, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 5, 0, 5), 150, 0));
add(progressLabel, new GridBagConstraints(1, 3, 2, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 5, 0, 5), 150, 0));
SwingWorker worker = new SwingWorker() {
public Object construct() {
while (true) {
try {
if (transfer.getBytesSent() > 0 && _starttime == 0) {
_starttime = System.currentTimeMillis();
}
long starttime = System.currentTimeMillis();
long startbyte = transfer.getBytesSent();
Thread.sleep(500);
FileTransfer.Status status = transfer.getStatus();
if (status == Status.error || status == Status.complete || status == Status.cancelled || status == Status.refused) {
break;
}
long endtime = System.currentTimeMillis();
long endbyte = transfer.getBytesSent();
long timediff = endtime - starttime;
long bytediff = endbyte - startbyte;
updateBar(transfer, nickname, TransferUtils.calculateSpeed(bytediff, timediff));
} catch (InterruptedException e) {
Log.error("Unable to sleep thread.", e);
}
}
return "";
}
public void finished() {
updateBar(transfer, nickname, "??MB/s");
}
};
worker.start();
makeClickable(imageLabel);
makeClickable(titleLabel);
}
Aggregations