With b_client_avatar_delete_other, the client's context menu and the menu on their avatar offer "Delete Avatar", as in TeamSpeak 3. The server takes the deletion as ftdeletefile on /avatar_<unique id> (not the stored file's name, which it refuses with "convert error") and clears the client's avatar flag for everyone. Our own avatar's file is now deleted the same way, which only such clients are allowed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
77 lines
2.7 KiB
Java
77 lines
2.7 KiB
Java
package com.ts3client.ui;
|
|
|
|
import com.ts3client.net.ClientEntry;
|
|
import com.ts3client.net.TeamspeakConnection;
|
|
import com.ts3client.net.avatar.Avatar;
|
|
|
|
import javax.swing.JFileChooser;
|
|
import javax.swing.JOptionPane;
|
|
import javax.swing.SwingUtilities;
|
|
import javax.swing.filechooser.FileNameExtensionFilter;
|
|
import java.awt.Component;
|
|
import java.io.File;
|
|
import java.io.IOException;
|
|
import java.nio.file.Files;
|
|
|
|
/** Sets, takes and deletes our avatar on one server, reporting failures in a dialog. */
|
|
final class AvatarActions {
|
|
|
|
/** Where the last avatar was picked from, for every server alike. */
|
|
private static File lastDirectory;
|
|
|
|
private final Component owner;
|
|
private final TeamspeakConnection conn;
|
|
|
|
AvatarActions(Component owner, TeamspeakConnection conn) {
|
|
this.owner = owner;
|
|
this.conn = conn;
|
|
}
|
|
|
|
/** Asks for an image and uploads it as our avatar. */
|
|
void choose() {
|
|
if (!conn.isConnected()) return;
|
|
JFileChooser chooser = new JFileChooser(lastDirectory);
|
|
chooser.setDialogTitle("Select your avatar");
|
|
chooser.setFileFilter(new FileNameExtensionFilter("Images", "png", "jpg", "jpeg", "gif", "bmp"));
|
|
if (chooser.showOpenDialog(owner) != JFileChooser.APPROVE_OPTION) return;
|
|
File file = chooser.getSelectedFile();
|
|
lastDirectory = file.getParentFile();
|
|
conn.setAvatar(file, error -> fail("Error uploading avatar: ", error));
|
|
}
|
|
|
|
/** Uploads someone else's avatar as ours. */
|
|
void steal(Avatar avatar) {
|
|
if (!conn.isConnected()) return;
|
|
File file;
|
|
try {
|
|
file = Files.createTempFile("avatar", "." + avatar.format().extension).toFile();
|
|
Files.write(file.toPath(), avatar.data());
|
|
} catch (IOException e) {
|
|
fail("Error uploading avatar: ", e.getMessage());
|
|
return;
|
|
}
|
|
conn.setAvatar(file, error -> {
|
|
file.delete();
|
|
fail("Error uploading avatar: ", error);
|
|
});
|
|
}
|
|
|
|
void delete() {
|
|
if (!conn.isConnected()) return;
|
|
conn.removeAvatar(error -> fail("Error deleting avatar: ", error));
|
|
}
|
|
|
|
/** Deletes another client's avatar for everyone. */
|
|
void deleteOf(ClientEntry client) {
|
|
if (!conn.isConnected()) return;
|
|
conn.removeAvatarOf(client, error -> fail("Error deleting avatar: ", error));
|
|
}
|
|
|
|
/** Shows {@code error}, if there is one; callable from any thread. */
|
|
private void fail(String what, String error) {
|
|
if (error == null) return;
|
|
SwingUtilities.invokeLater(() ->
|
|
JOptionPane.showMessageDialog(owner, what + error, "Error", JOptionPane.ERROR_MESSAGE));
|
|
}
|
|
}
|