Set and delete our avatar from the Self menu and the avatar's menu

As in TeamSpeak 3, Self offers "Set avatar…", which picks an image and
uploads it, and "Delete avatar" while we have one; the old avatar
dialog is gone. Setting (and stealing) is offered only while the server
grants i_client_max_avatar_filesize. Right-clicking our own avatar in
the info panel offers the same two beside saving it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-01 16:01:16 +00:00
co-authored by Claude Opus 5.5
parent e7d338ce28
commit b1890cbf79
8 changed files with 182 additions and 241 deletions
@@ -0,0 +1,69 @@
package com.ts3client.ui;
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));
}
/** 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));
}
}