package envoy.client.util; import java.awt.Toolkit; import java.awt.datatransfer.StringSelection; import java.io.*; import java.util.logging.*; import javafx.stage.FileChooser; import dev.kske.eventbus.core.EventBus; import envoy.data.Message; import envoy.util.EnvoyLog; import envoy.client.data.*; import envoy.client.event.MessageDeletion; import envoy.client.ui.controller.ChatScene; /** * Contains methods that are commonly used for {@link Message}s. * * @author Leon Hofmeister * @since Envoy Client v0.3-beta */ public class MessageUtil { private MessageUtil() {} private static Logger logger = EnvoyLog.getLogger(MessageUtil.class); /** * Copies the text of the given message to the System Clipboard. * * @param message the message whose text to copy * @since Envoy Client v0.3-beta */ public static void copyMessageText(Message message) { logger.log(Level.FINEST, "A copy of message text \"" + message.getText() + "\" was requested"); Toolkit.getDefaultToolkit().getSystemClipboard() .setContents(new StringSelection(message.getText()), null); } /** * Deletes the given message. * * @param message the message to delete * @since Envoy Client v0.3-beta */ public static void deleteMessage(Message message) { final var messageDeletionEvent = new MessageDeletion(message.getID()); final var controller = Context.getInstance().getSceneContext().getController(); if (controller instanceof ChatScene) ((ChatScene) controller).clearMessageSelection(); // Removing the message locally EventBus.getInstance().dispatch(messageDeletionEvent); logger.log(Level.FINEST, "message deletion was requested for " + message); } /** * Forwards the given message. Currently not implemented. * * @param message the message to forward * @since Envoy Client v0.3-beta */ public static void forwardMessage(Message message) { logger.log(Level.FINEST, "Message forwarding was requested for " + message); } /** * Quotes the given message. Currently not implemented. * * @param message the message to quote * @since Envoy Client v0.3-beta */ public static void quoteMessage(Message message) { logger.log(Level.FINEST, "Message quotation was requested for " + message); } /** * Saves the attachment of a message, if present. * * @param message the message whose attachment to save * @throws IllegalStateException if no attachment is present in the message * @since Envoy Client v0.3-beta */ public static void saveAttachment(Message message) { if (!message.hasAttachment()) throw new IllegalArgumentException("Cannot save a non-existing attachment"); File file; final var fileName = message.getAttachment().getName(); final var downloadLocation = Settings.getInstance().getDownloadLocation(); // Show save file dialog, if the user did not opt-out if (!Settings.getInstance().isDownloadSavedWithoutAsking()) { final var fileChooser = new FileChooser(); fileChooser.setInitialFileName(fileName); fileChooser.setInitialDirectory(downloadLocation); file = fileChooser.showSaveDialog(Context.getInstance().getSceneContext().getStage()); } else file = new File(downloadLocation, fileName); // A file was selected if (file != null) try (var fos = new FileOutputStream(file)) { fos.write(message.getAttachment().getData()); logger.log(Level.FINE, "Attachment of message was saved at " + file.getAbsolutePath()); } catch (final IOException e) { logger.log(Level.WARNING, "Could not save attachment of " + message + ": ", e); } } }