One byte-size format and one chat scope

text.ByteSize writes sizes and rates as TeamSpeak does and replaces the
formatters in ChatHtml, FileBrowserDialog and ConnectionStatsFormat.
chat.ChatScope replaces ConnectionListener.ChatScope, ChatLogScope and
ChatPanel.Target, and the mappings between them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-12 23:01:42 +00:00
co-authored by Claude Opus 5.5
parent 203082d040
commit 53585994a8
24 changed files with 188 additions and 174 deletions
@@ -0,0 +1,8 @@
package eu.lixko.ts3j.client.chat;
/** Which chat a message is in: the server's, the channel's, or a private one with a client. */
public enum ChatScope {
SERVER,
CHANNEL,
PRIVATE
}
@@ -1,7 +0,0 @@
package eu.lixko.ts3j.client.chatlog;
public enum ChatLogScope {
SERVER,
CHANNEL,
PRIVATE
}
@@ -1,5 +1,6 @@
package eu.lixko.ts3j.client.chatlog;
import eu.lixko.ts3j.client.chat.ChatScope;
import eu.lixko.ts3j.client.config.AppDirs;
import eu.lixko.ts3j.client.text.BBCode;
import eu.lixko.ts3j.client.text.HtmlText;
@@ -59,21 +60,21 @@ public final class ChatLogStore {
public synchronized void begin() throws IOException {
LocalDateTime now = LocalDateTime.now();
appendRaw(log(ChatLogScope.SERVER), "*** Log begins " + BEGIN.format(now), beginHtml("Log", now));
appendRaw(log(ChatLogScope.CHANNEL), "*** Chat begins " + BEGIN.format(now), beginHtml("Chat", now));
appendRaw(log(ChatScope.SERVER), "*** Log begins " + BEGIN.format(now), beginHtml("Log", now));
appendRaw(log(ChatScope.CHANNEL), "*** Chat begins " + BEGIN.format(now), beginHtml("Chat", now));
}
public List<String> serverHtmlHistory() throws IOException {
return last(readHtml(log(ChatLogScope.SERVER), HISTORY_TAIL_BYTES), HISTORY_LIMIT);
return last(readHtml(log(ChatScope.SERVER), HISTORY_TAIL_BYTES), HISTORY_LIMIT);
}
public List<String> channelHtmlHistory() throws IOException {
return last(readTextMessages(log(ChatLogScope.CHANNEL), ChatLogScope.CHANNEL, HISTORY_TAIL_BYTES),
return last(readTextMessages(log(ChatScope.CHANNEL), ChatScope.CHANNEL, HISTORY_TAIL_BYTES),
HISTORY_LIMIT);
}
public List<String> privateHtmlHistory(String clientUniqueId) throws IOException {
return last(readTextMessages(privateLog(clientUniqueId), ChatLogScope.PRIVATE, HISTORY_TAIL_BYTES),
return last(readTextMessages(privateLog(clientUniqueId), ChatScope.PRIVATE, HISTORY_TAIL_BYTES),
HISTORY_LIMIT);
}
@@ -87,14 +88,14 @@ public final class ChatLogStore {
/**
* Searches a whole log, not only the part {@link #HISTORY_LIMIT} loads into a chat.
*
* @param clientUniqueId the peer, for {@link ChatLogScope#PRIVATE}; ignored otherwise
* @param clientUniqueId the peer, for {@link ChatScope#PRIVATE}; ignored otherwise
* @param limit how many of the newest matches to return
*/
public SearchResult search(ChatLogScope scope, String clientUniqueId, TextQuery query, int limit)
public SearchResult search(ChatScope scope, String clientUniqueId, TextQuery query, int limit)
throws IOException {
List<String> all = scope == ChatLogScope.SERVER
List<String> all = scope == ChatScope.SERVER
? readHtml(log(scope), Long.MAX_VALUE)
: readTextMessages(scope == ChatLogScope.PRIVATE ? privateLog(clientUniqueId) : log(scope), scope,
: readTextMessages(scope == ChatScope.PRIVATE ? privateLog(clientUniqueId) : log(scope), scope,
Long.MAX_VALUE);
List<String> hits = new ArrayList<>();
for (String entry : all) {
@@ -104,9 +105,9 @@ public final class ChatLogStore {
}
/** Logs a message; a private one goes to the log of the client it came from. */
public synchronized String appendMessage(ChatLogScope scope, boolean outgoing, int clientId, String uniqueId,
public synchronized String appendMessage(ChatScope scope, boolean outgoing, int clientId, String uniqueId,
String nickname, String message) throws IOException {
LogFiles files = scope == ChatLogScope.PRIVATE ? privateLog(uniqueId) : log(scope);
LogFiles files = scope == ChatScope.PRIVATE ? privateLog(uniqueId) : log(scope);
return append(files, scope, outgoing, clientId, uniqueId, nickname, message);
}
@@ -114,11 +115,11 @@ public final class ChatLogStore {
public synchronized String appendPrivateMessage(String peerUniqueId, boolean outgoing, int clientId,
String uniqueId, String nickname, String message)
throws IOException {
return append(privateLog(peerUniqueId), ChatLogScope.PRIVATE, outgoing, clientId, uniqueId, nickname,
return append(privateLog(peerUniqueId), ChatScope.PRIVATE, outgoing, clientId, uniqueId, nickname,
message);
}
private String append(LogFiles files, ChatLogScope scope, boolean outgoing, int clientId, String uniqueId,
private String append(LogFiles files, ChatScope scope, boolean outgoing, int clientId, String uniqueId,
String nickname, String message) throws IOException {
LocalDateTime now = LocalDateTime.now();
String text = "<" + TIME.format(now) + "> " + safe(nickname) + ": " + safe(message);
@@ -131,7 +132,7 @@ public final class ChatLogStore {
LocalDateTime now = LocalDateTime.now();
String text = "<" + TIME.format(now) + "> " + safe(message);
String html = eventHtml(now, BBCode.linksToHtml(message));
appendRaw(log(ChatLogScope.SERVER), text, html);
appendRaw(log(ChatScope.SERVER), text, html);
return html;
}
@@ -143,7 +144,7 @@ public final class ChatLogStore {
return new String(Base64.getDecoder().decode(value), StandardCharsets.UTF_8);
}
private LogFiles log(ChatLogScope scope) {
private LogFiles log(ChatScope scope) {
return switch (scope) {
case SERVER -> new LogFiles(serverDir.resolve("server.txt"), serverDir.resolve("server.html"));
case CHANNEL -> new LogFiles(serverDir.resolve("channel.txt"), serverDir.resolve("channel.html"));
@@ -197,7 +198,7 @@ public final class ChatLogStore {
* A message is written as it was sent, so one with line breaks spans several lines of the
* log: a line that starts no entry of its own continues the message before it.
*/
private static List<String> readTextMessages(LogFiles files, ChatLogScope scope, long tailBytes)
private static List<String> readTextMessages(LogFiles files, ChatScope scope, long tailBytes)
throws IOException {
if (!Files.isRegularFile(files.txt)) return Collections.emptyList();
List<String> entries = new ArrayList<>();
@@ -250,7 +251,7 @@ public final class ChatLogStore {
+ BBCode.escape(line) + "</span></div>";
}
private static String messageLineHtml(Matcher m, CharSequence text, ChatLogScope scope) {
private static String messageLineHtml(Matcher m, CharSequence text, ChatScope scope) {
String target = switch (scope) {
case PRIVATE -> "Private";
case CHANNEL -> "Channel";
@@ -268,7 +269,7 @@ public final class ChatLogStore {
+ "*** " + kind + " begins " + BEGIN.format(timestamp) + "</span></p>";
}
private static String messageHtml(ChatLogScope scope, boolean outgoing, LocalDateTime timestamp,
private static String messageHtml(ChatScope scope, boolean outgoing, LocalDateTime timestamp,
int clientId, String uniqueId, String nickname, String message) {
String target = switch (scope) {
case PRIVATE -> "Private";
@@ -1,6 +1,6 @@
package eu.lixko.ts3j.client.net;
import eu.lixko.ts3j.client.chatlog.ChatLogScope;
import eu.lixko.ts3j.client.chat.ChatScope;
import eu.lixko.ts3j.client.chatlog.ChatLogStore;
import eu.lixko.ts3j.client.config.Settings;
@@ -44,8 +44,8 @@ final class ChatLog {
logs.begin();
List<String> serverHistory = logs.serverHtmlHistory();
List<String> channelHistory = logs.channelHtmlHistory();
ui.onChatHistory(ConnectionListener.ChatScope.SERVER, serverHistory);
ui.onChatHistory(ConnectionListener.ChatScope.CHANNEL, channelHistory);
ui.onChatHistory(ChatScope.SERVER, serverHistory);
ui.onChatHistory(ChatScope.CHANNEL, channelHistory);
ui.onStatus("Loaded chat logs from " + logs.serverDir()
+ " (" + serverHistory.size() + " server, " + channelHistory.size() + " channel)");
} catch (Exception e) {
@@ -69,28 +69,28 @@ final class ChatLog {
}
}
void received(ConnectionListener.ChatScope scope, int fromClientId, String fromUniqueId, String fromName,
void received(ChatScope scope, int fromClientId, String fromUniqueId, String fromName,
String message) {
ChatLogStore logs = store;
if (logs == null) return;
try {
logs.appendMessage(scope(scope), false, fromClientId, fromUniqueId, fromName, message);
logs.appendMessage(scope, false, fromClientId, fromUniqueId, fromName, message);
} catch (IOException e) {
ui.onStatus("Could not write chat log: " + RequestException.rootMessage(e));
}
}
/** What we said; a private message is kept with {@code peerUniqueId}'s log, and dropped without one. */
void sent(ConnectionListener.ChatScope scope, int selfClientId, String selfUniqueId, String selfName,
void sent(ChatScope scope, int selfClientId, String selfUniqueId, String selfName,
String peerUniqueId, String message) {
ChatLogStore logs = store;
if (logs == null) return;
try {
if (scope == ConnectionListener.ChatScope.PRIVATE) {
if (scope == ChatScope.PRIVATE) {
if (peerUniqueId == null || peerUniqueId.isBlank()) return;
logs.appendPrivateMessage(peerUniqueId, true, selfClientId, selfUniqueId, selfName, message);
} else {
logs.appendMessage(scope(scope), true, selfClientId, selfUniqueId, selfName, message);
logs.appendMessage(scope, true, selfClientId, selfUniqueId, selfName, message);
}
} catch (IOException e) {
ui.onStatus("Could not write chat log: " + RequestException.rootMessage(e));
@@ -111,12 +111,4 @@ final class ChatLog {
ui.onStatus("Could not write server log: " + RequestException.rootMessage(e));
}
}
private static ChatLogScope scope(ConnectionListener.ChatScope scope) {
return switch (scope) {
case SERVER -> ChatLogScope.SERVER;
case CHANNEL -> ChatLogScope.CHANNEL;
case PRIVATE -> ChatLogScope.PRIVATE;
};
}
}
@@ -3,6 +3,7 @@ package eu.lixko.ts3j.client.net;
import com.github.manevolent.ts3j.event.*;
import com.github.manevolent.ts3j.api.TextMessageTargetMode;
import eu.lixko.ts3j.client.audio.VoiceOutput;
import eu.lixko.ts3j.client.chat.ChatScope;
import eu.lixko.ts3j.client.contacts.Contact;
import eu.lixko.ts3j.client.sound.SoundEvent;
import eu.lixko.ts3j.client.sound.SoundNotifier;
@@ -651,19 +652,19 @@ final class ConnectionEventHandler implements TS3Listener {
boolean isPrivate = e.getTargetMode() == TextMessageTargetMode.CLIENT;
if (isPrivate ? contact.ignorePrivateMessages : contact.ignorePublicMessages) return;
}
ConnectionListener.ChatScope scope;
ChatScope scope;
SoundEvent notification;
switch (e.getTargetMode()) {
case CLIENT:
scope = ConnectionListener.ChatScope.PRIVATE;
scope = ChatScope.PRIVATE;
notification = SoundEvent.CHAT_RECEIVED_MESSAGE_CLIENT;
break;
case CHANNEL:
scope = ConnectionListener.ChatScope.CHANNEL;
scope = ChatScope.CHANNEL;
notification = SoundEvent.CHAT_RECEIVED_MESSAGE_CHANNEL;
break;
default:
scope = ConnectionListener.ChatScope.SERVER;
scope = ChatScope.SERVER;
notification = SoundEvent.CHAT_RECEIVED_MESSAGE_SERVER;
break;
}
@@ -1,5 +1,7 @@
package eu.lixko.ts3j.client.net;
import eu.lixko.ts3j.client.chat.ChatScope;
import java.util.List;
/**
@@ -8,9 +10,6 @@ import java.util.List;
*/
public interface ConnectionListener {
/** Message scope for chat display. */
enum ChatScope {SERVER, CHANNEL, PRIVATE}
void onStatus(String status);
void onConnected();
@@ -1,6 +1,7 @@
package eu.lixko.ts3j.client.net;
import eu.lixko.ts3j.client.audio.VoiceReceiveStats;
import eu.lixko.ts3j.client.text.ByteSize;
import java.text.DecimalFormat;
import java.util.ArrayList;
@@ -13,7 +14,7 @@ import java.util.List;
*/
public final class ConnectionStatsFormat {
public static final String UNKNOWN = "—";
public static final String UNKNOWN = ByteSize.UNKNOWN;
public static final String HIDDEN = "Hidden";
public static final String UNLIMITED = "Unlimited";
public static final String EXCEEDED = "Exceeded";
@@ -73,15 +74,15 @@ public final class ConnectionStatsFormat {
List<TrafficRow> rows = new ArrayList<>(List.of(
new TrafficRow("Packet loss", loss(t.lossTotal()), loss(t.lossIn), loss(t.lossOut)),
new TrafficRow("Packets transferred", "", count(t.packetsIn), count(t.packetsOut)),
new TrafficRow("Bytes transferred", "", bytes(t.bytesIn), bytes(t.bytesOut)),
new TrafficRow("Bytes transferred", "", ByteSize.format(t.bytesIn), ByteSize.format(t.bytesOut)),
new TrafficRow("Bandwidth last second", "",
rate(t.bandwidthInLastSecond), rate(t.bandwidthOutLastSecond)),
ByteSize.rate(t.bandwidthInLastSecond), ByteSize.rate(t.bandwidthOutLastSecond)),
new TrafficRow("Bandwidth last minute", "",
rate(t.bandwidthInLastMinute), rate(t.bandwidthOutLastMinute))));
ByteSize.rate(t.bandwidthInLastMinute), ByteSize.rate(t.bandwidthOutLastMinute))));
if (kind == null) {
long in = s == null ? -1 : s.filetransferBandwidthReceived;
long out = s == null ? -1 : s.filetransferBandwidthSent;
rows.add(new TrafficRow("File transfer bandwidth", "", rate(in), rate(out)));
rows.add(new TrafficRow("File transfer bandwidth", "", ByteSize.rate(in), ByteSize.rate(out)));
}
return rows;
}
@@ -98,14 +99,14 @@ public final class ConnectionStatsFormat {
}
private static List<Row> quota(ConnectionStats s, String direction, String label, long used, long quotaMb) {
Row month = new Row(label, bytes(used));
Row month = new Row(label, ByteSize.format(used));
if (!s.self) return List.of(month);
String available = quotaMb < 0 ? UNLIMITED : bytes(quotaMb * 1024 * 1024);
String available = quotaMb < 0 ? UNLIMITED : ByteSize.format(quotaMb * 1024 * 1024);
String remaining;
if (quotaMb < 0) remaining = UNLIMITED;
else if (used < 0) remaining = UNKNOWN;
else if (used > quotaMb * 1024 * 1024) remaining = EXCEEDED;
else remaining = bytes(quotaMb * 1024 * 1024 - used);
else remaining = ByteSize.format(quotaMb * 1024 * 1024 - used);
return List.of(month,
new Row("Available " + direction + " quota:", available),
new Row("Remaining " + direction + " this month:", remaining));
@@ -157,21 +158,6 @@ public final class ConnectionStatsFormat {
return count(n) + " (" + new DecimalFormat("0.0").format(100.0 * n / of) + " %)";
}
public static String bytes(long bytes) {
if (bytes < 0) return UNKNOWN;
if (bytes < 1024) return bytes + " Bytes";
DecimalFormat f = new DecimalFormat("0.00");
double kib = bytes / 1024.0;
if (kib < 1024) return f.format(kib) + " KiB";
double mib = kib / 1024.0;
if (mib < 1024) return f.format(mib) + " MiB";
return f.format(mib / 1024.0) + " GiB";
}
public static String rate(long bytesPerSecond) {
return bytesPerSecond < 0 ? UNKNOWN : bytes(bytesPerSecond) + "/s";
}
/** {@code 04:34}, or {@code 15:35:17} from an hour on. */
public static String duration(long ms) {
if (ms < 0) return UNKNOWN;
@@ -14,6 +14,7 @@ import com.github.manevolent.ts3j.protocol.socket.client.LocalTeamspeakClientSoc
import com.github.manevolent.ts3j.util.Ts3Crypt;
import eu.lixko.ts3j.client.audio.AudioBackend;
import eu.lixko.ts3j.client.audio.VoiceOutput;
import eu.lixko.ts3j.client.chat.ChatScope;
import eu.lixko.ts3j.client.config.Settings;
import eu.lixko.ts3j.client.contacts.ContactStore;
import eu.lixko.ts3j.client.myts.MyTeamSpeakIdentity;
@@ -561,7 +562,7 @@ public final class TeamspeakConnection implements TS3Listener {
ClientEntry self = own.entry();
int cid = self != null ? self.channelId : 0;
sock.sendChannelMessage(cid, text);
keepSentChat(ConnectionListener.ChatScope.CHANNEL, "", text);
keepSentChat(ChatScope.CHANNEL, "", text);
announce.sound(SoundEvent.CHAT_SENT_MESSAGE_CHANNEL, announce.channelVars(cid, null));
});
}
@@ -569,7 +570,7 @@ public final class TeamspeakConnection implements TS3Listener {
public void sendServerMessage(String text) {
act("Message failed: ", sock -> {
sock.sendServerMessage(text);
keepSentChat(ConnectionListener.ChatScope.SERVER, "", text);
keepSentChat(ChatScope.SERVER, "", text);
announce.sound(SoundEvent.CHAT_SENT_MESSAGE_SERVER, announce.serverVars());
});
}
@@ -586,7 +587,7 @@ public final class TeamspeakConnection implements TS3Listener {
} catch (Exception ignored) {
}
}
keepSentChat(ConnectionListener.ChatScope.PRIVATE, peerUid, text);
keepSentChat(ChatScope.PRIVATE, peerUid, text);
announce.sound(SoundEvent.CHAT_SENT_MESSAGE_CLIENT, announce.clientVars(clientId, null));
});
}
@@ -798,7 +799,7 @@ public final class TeamspeakConnection implements TS3Listener {
chatLog.loadPrivateHistory(clientId, c != null ? c.nickname : "Client " + clientId, uniqueId);
}
private void keepSentChat(ConnectionListener.ChatScope scope, String peerUniqueId, String message) {
private void keepSentChat(ChatScope scope, String peerUniqueId, String message) {
ClientEntry self = own.entry();
String uid = identity != null ? identity.getUid().toBase64() : "";
String nickname = self != null ? self.nickname : settings.nickname.get();
@@ -1,6 +1,7 @@
package eu.lixko.ts3j.client.session;
import eu.lixko.ts3j.client.audio.AudioBackend;
import eu.lixko.ts3j.client.chat.ChatScope;
import eu.lixko.ts3j.client.config.Bookmark;
import eu.lixko.ts3j.client.config.IdentityEntry;
import eu.lixko.ts3j.client.config.IdentityStore;
@@ -648,7 +649,7 @@ public final class ServerSession {
*
* @param clientId the recipient of a private message; ignored otherwise
*/
public void sendChat(ConnectionListener.ChatScope scope, int clientId, String text) {
public void sendChat(ChatScope scope, int clientId, String text) {
if (!conn.isConnected()) {
listener.onSystemMessage("Not connected.");
return;
@@ -676,7 +677,7 @@ public final class ServerSession {
*
* @param data read and closed in the background
*/
public void sendAttachment(ConnectionListener.ChatScope scope, int clientId, ChatAttachments attachments,
public void sendAttachment(ChatScope scope, int clientId, ChatAttachments attachments,
InputStream data, long size, String fileName) {
String room = conn.serverUniqueId();
if (!conn.isConnected() || room == null) {
@@ -0,0 +1,28 @@
package eu.lixko.ts3j.client.text;
import java.text.DecimalFormat;
/** Byte counts the way the TeamSpeak client writes them: {@code 1023 Bytes}, {@code 353.98 KiB}. */
public final class ByteSize {
/** What an unknown (negative) size shows as. */
public static final String UNKNOWN = "—";
private ByteSize() {
}
public static String format(long bytes) {
if (bytes < 0) return UNKNOWN;
if (bytes < 1024) return bytes + " Bytes";
DecimalFormat f = new DecimalFormat("0.00");
double kib = bytes / 1024.0;
if (kib < 1024) return f.format(kib) + " KiB";
double mib = kib / 1024.0;
if (mib < 1024) return f.format(mib) + " MiB";
return f.format(mib / 1024.0) + " GiB";
}
public static String rate(long bytesPerSecond) {
return bytesPerSecond < 0 ? UNKNOWN : format(bytesPerSecond) + "/s";
}
}
@@ -5,7 +5,6 @@ import eu.lixko.ts3j.client.myts.ChatAttachments;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -73,7 +72,7 @@ public final class ChatHtml {
if (attachment.isImage() && attachment.fileSize() <= ChatAttachments.MAX_PREVIEW_BYTES) {
return "<br>" + open + "<img src=\"" + href + "\"></a>" + caption;
}
String size = attachment.fileSize() >= 0 ? " " + span(MUTED_CLASS, "(" + size(attachment.fileSize()) + ")") : "";
String size = attachment.fileSize() >= 0 ? " " + span(MUTED_CLASS, "(" + ByteSize.format(attachment.fileSize()) + ")") : "";
return open + name + "</a>" + size + caption;
}
@@ -131,13 +130,6 @@ public final class ChatHtml {
return html.substring(0, m.start(2)) + attachment(attachment, room) + html.substring(m.end(2));
}
/** A file size as people read it. */
static String size(long bytes) {
if (bytes < 1024) return bytes + " B";
if (bytes < 1024 * 1024) return String.format(Locale.ROOT, "%.0f KB", bytes / 1024.0);
return String.format(Locale.ROOT, "%.1f MB", bytes / (1024.0 * 1024));
}
private static String stamp() {
return "[" + TIME.format(LocalTime.now()) + "] ";
}