Open ts3server:// links on both apps
A link connects with its nickname, passwords, channel (path or cid) and privilege key, the key used once on; addbookmark saves it as a bookmark. A link to a server we are on already goes to its channel instead. Android takes the links from browsers and other apps through an intent filter, and from chat. The desktop opens them from chat and descriptions, and its launcher entry claims the scheme when run from the jar; a second launch hands its link to the running instance over a Unix socket in the user's runtime directory. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,14 +1,17 @@
|
||||
package com.ts3client;
|
||||
|
||||
import com.ts3client.config.Settings;
|
||||
import com.ts3client.session.ServerLink;
|
||||
import com.ts3client.ui.DesktopEntry;
|
||||
import com.ts3client.ui.DisplayScale;
|
||||
import com.ts3client.ui.IconTheme;
|
||||
import com.ts3client.ui.InstanceChannel;
|
||||
import com.ts3client.ui.LookAndFeelManager;
|
||||
import com.ts3client.ui.MainFrame;
|
||||
import com.ts3client.ui.WindowClass;
|
||||
|
||||
import javax.swing.SwingUtilities;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* Application entry point. Loads settings, installs the look-and-feel and shows the
|
||||
@@ -24,6 +27,10 @@ public final class Main {
|
||||
} catch (Throwable ignored) {
|
||||
}
|
||||
|
||||
// A link a browser opened goes to the instance already running, if there is one.
|
||||
final String link = Arrays.stream(args).filter(ServerLink::isLink).findFirst().orElse(null);
|
||||
if (link != null && InstanceChannel.send(link)) return;
|
||||
|
||||
final Settings settings = Settings.load();
|
||||
DisplayScale.configure(settings.uiScale);
|
||||
WindowClass.apply();
|
||||
@@ -32,7 +39,10 @@ public final class Main {
|
||||
LookAndFeelManager.install(settings.appearance);
|
||||
IconTheme.get().reload(settings);
|
||||
DesktopEntry.install();
|
||||
new MainFrame(settings).setVisible(true);
|
||||
MainFrame frame = new MainFrame(settings);
|
||||
frame.setVisible(true);
|
||||
InstanceChannel.listen(href -> SwingUtilities.invokeLater(() -> frame.openLink(href)));
|
||||
if (link != null) frame.openLink(link);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.ts3client.ui;
|
||||
|
||||
import com.ts3client.Main;
|
||||
import com.ts3client.session.ServerLink;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.image.BufferedImage;
|
||||
@@ -22,6 +23,8 @@ import java.util.Locale;
|
||||
* claims the class, Plasma falls back to the process's command line — {@code java} —
|
||||
* and files the window under whatever launcher runs Java, with its name and icon.
|
||||
*
|
||||
* <p>A launcher run from a jar also takes {@code ts3server://} links.
|
||||
*
|
||||
* <p>The files are only rewritten when their content changes, e.g. after the jar moved.
|
||||
* {@code -Dts3j.desktopEntry=false} turns this off.
|
||||
*/
|
||||
@@ -42,8 +45,11 @@ public final class DesktopEntry {
|
||||
Path data = dataHome();
|
||||
Path icon = icon();
|
||||
writeIfChanged(icon, iconPng());
|
||||
writeIfChanged(data.resolve("applications").resolve(WindowClass.NAME + ".desktop"),
|
||||
entry(icon).getBytes(StandardCharsets.UTF_8));
|
||||
Path applications = data.resolve("applications");
|
||||
if (writeIfChanged(applications.resolve(WindowClass.NAME + ".desktop"),
|
||||
entry(icon).getBytes(StandardCharsets.UTF_8))) {
|
||||
updateSchemeHandlers(applications);
|
||||
}
|
||||
} catch (IOException | RuntimeException e) {
|
||||
System.err.println("Cannot install the desktop entry: " + e);
|
||||
}
|
||||
@@ -69,13 +75,14 @@ public final class DesktopEntry {
|
||||
.append("GenericName=TeamSpeak 3 Client\n")
|
||||
.append("Comment=Voice chat on TeamSpeak 3 servers\n")
|
||||
.append("Icon=").append(icon).append('\n')
|
||||
.append("Exec=").append(exec == null ? "java" : exec).append('\n')
|
||||
.append("Exec=").append(exec == null ? "java" : exec + " %u").append('\n')
|
||||
.append("StartupWMClass=").append(WindowClass.NAME).append('\n')
|
||||
.append("Categories=Network;InstantMessaging;AudioVideo;\n")
|
||||
.append("Terminal=false\n");
|
||||
// Run from classes (an IDE, mvn exec:java) there is nothing sensible to launch;
|
||||
// the entry is still needed to name the window, but stays out of the menu.
|
||||
if (exec == null) s.append("NoDisplay=true\n");
|
||||
else s.append("MimeType=x-scheme-handler/").append(ServerLink.SCHEME).append(";\n");
|
||||
return s.toString();
|
||||
}
|
||||
|
||||
@@ -108,12 +115,26 @@ public final class DesktopEntry {
|
||||
return out.toByteArray();
|
||||
}
|
||||
|
||||
private static void writeIfChanged(Path file, byte[] content) throws IOException {
|
||||
if (Files.exists(file) && Arrays.equals(Files.readAllBytes(file), content)) return;
|
||||
/**
|
||||
* Browsers find the handler of a {@code ts3server://} link in the folder's MIME cache, which
|
||||
* only this tool rebuilds; without it the entry still names the window.
|
||||
*/
|
||||
private static void updateSchemeHandlers(Path applications) {
|
||||
try {
|
||||
new ProcessBuilder("update-desktop-database", applications.toString())
|
||||
.redirectErrorStream(true).redirectOutput(ProcessBuilder.Redirect.DISCARD).start();
|
||||
} catch (IOException ignored) {
|
||||
}
|
||||
}
|
||||
|
||||
/** @return whether the file changed */
|
||||
private static boolean writeIfChanged(Path file, byte[] content) throws IOException {
|
||||
if (Files.exists(file) && Arrays.equals(Files.readAllBytes(file), content)) return false;
|
||||
Files.createDirectories(file.getParent());
|
||||
Path tmp = file.resolveSibling(file.getFileName() + ".tmp");
|
||||
Files.write(tmp, content);
|
||||
Files.move(tmp, file, StandardCopyOption.REPLACE_EXISTING,
|
||||
StandardCopyOption.ATOMIC_MOVE);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
package com.ts3client.ui;
|
||||
|
||||
import com.ts3client.config.AppDirs;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.StandardProtocolFamily;
|
||||
import java.net.UnixDomainSocketAddress;
|
||||
import java.nio.channels.Channels;
|
||||
import java.nio.channels.ServerSocketChannel;
|
||||
import java.nio.channels.SocketChannel;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
* Lets a second launch of the client hand what it was started with (a {@code ts3server://}
|
||||
* link a browser opened) to the instance already running on the same profile, over a Unix
|
||||
* domain socket.
|
||||
*/
|
||||
public final class InstanceChannel {
|
||||
|
||||
private static final int MAX_MESSAGE = 8192;
|
||||
|
||||
private InstanceChannel() {
|
||||
}
|
||||
|
||||
/**
|
||||
* In the user's private runtime directory where there is one, as a socket path may not be
|
||||
* much over 100 bytes and a profile's can be longer; named after the profile, so each
|
||||
* profile has an instance of its own.
|
||||
*/
|
||||
private static Path socketPath() {
|
||||
String runtime = System.getenv("XDG_RUNTIME_DIR");
|
||||
if (runtime == null || runtime.isBlank()) return AppDirs.file("instance.sock").toPath();
|
||||
String profile = AppDirs.profile().getAbsolutePath();
|
||||
return Path.of(runtime, "ts3j-" + Integer.toHexString(profile.hashCode()) + ".sock");
|
||||
}
|
||||
|
||||
/** Sends {@code message} to the running instance; false when there is none to take it. */
|
||||
public static boolean send(String message) {
|
||||
try (SocketChannel channel = SocketChannel.open(UnixDomainSocketAddress.of(socketPath()));
|
||||
OutputStream out = Channels.newOutputStream(channel)) {
|
||||
out.write(message.getBytes(StandardCharsets.UTF_8));
|
||||
return true;
|
||||
} catch (IOException | UnsupportedOperationException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/** Takes the messages later launches send, each on a thread of its own; quietly does nothing where it cannot. */
|
||||
public static void listen(Consumer<String> onMessage) {
|
||||
Path path = socketPath();
|
||||
ServerSocketChannel server;
|
||||
try {
|
||||
AppDirs.createProfile();
|
||||
// Left behind by an instance that did not exit cleanly; a live one answered send() already.
|
||||
Files.deleteIfExists(path);
|
||||
server = ServerSocketChannel.open(StandardProtocolFamily.UNIX);
|
||||
server.bind(UnixDomainSocketAddress.of(path));
|
||||
path.toFile().deleteOnExit();
|
||||
} catch (IOException | UnsupportedOperationException e) {
|
||||
System.err.println("Cannot take links from other launches: " + e);
|
||||
return;
|
||||
}
|
||||
Thread accept = new Thread(() -> {
|
||||
while (server.isOpen()) {
|
||||
try (SocketChannel client = server.accept(); InputStream in = Channels.newInputStream(client)) {
|
||||
String message = new String(in.readNBytes(MAX_MESSAGE), StandardCharsets.UTF_8).trim();
|
||||
if (!message.isEmpty()) onMessage.accept(message);
|
||||
} catch (IOException e) {
|
||||
if (!server.isOpen()) return;
|
||||
}
|
||||
}
|
||||
}, "instance-channel");
|
||||
accept.setDaemon(true);
|
||||
accept.start();
|
||||
}
|
||||
}
|
||||
@@ -1,17 +1,36 @@
|
||||
package com.ts3client.ui;
|
||||
|
||||
import com.ts3client.session.ServerLink;
|
||||
|
||||
import javax.swing.JOptionPane;
|
||||
import java.awt.Component;
|
||||
import java.awt.Desktop;
|
||||
import java.net.URI;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/** Opens http(s) links from chat messages and descriptions in the system browser. */
|
||||
/**
|
||||
* Opens http(s) links from chat messages and descriptions in the system browser, and
|
||||
* {@code ts3server://} links in the client itself.
|
||||
*/
|
||||
final class Links {
|
||||
|
||||
private static volatile Consumer<ServerLink> serverLinks;
|
||||
|
||||
private Links() {
|
||||
}
|
||||
|
||||
/** Where {@code ts3server://} links go. */
|
||||
static void setServerLinkHandler(Consumer<ServerLink> handler) {
|
||||
serverLinks = handler;
|
||||
}
|
||||
|
||||
static void open(String href, Component parent) {
|
||||
ServerLink link = ServerLink.parse(href);
|
||||
Consumer<ServerLink> handler = serverLinks;
|
||||
if (link != null && handler != null) {
|
||||
handler.accept(link);
|
||||
return;
|
||||
}
|
||||
try {
|
||||
URI uri = new URI(href);
|
||||
String scheme = uri.getScheme() == null ? "" : uri.getScheme().toLowerCase();
|
||||
|
||||
@@ -8,6 +8,7 @@ import com.ts3client.config.AwayMessages;
|
||||
import com.ts3client.config.BanReasonPresets;
|
||||
import com.ts3client.config.Bookmark;
|
||||
import com.ts3client.config.Bookmarks;
|
||||
import com.ts3client.session.ServerLink;
|
||||
import com.ts3client.config.IdentityStore;
|
||||
import com.ts3client.config.Settings;
|
||||
import com.ts3client.myts.ChatAttachments;
|
||||
@@ -186,6 +187,8 @@ public final class MainFrame extends JFrame implements ServerTabPane.Listener {
|
||||
first.chat().appendSystem("Welcome to the TS3J Swing client.");
|
||||
first.chat().appendSystem("Use Connections → Connect to join a server.");
|
||||
|
||||
Links.setServerLinkHandler(this::openServerLink);
|
||||
|
||||
statusTimer = new javax.swing.Timer(3000, e -> updateConnectionStatus());
|
||||
statusTimer.start();
|
||||
setSize(UIScale.scale(new Dimension(880, 560)));
|
||||
@@ -815,6 +818,33 @@ public final class MainFrame extends JFrame implements ServerTabPane.Listener {
|
||||
tab.session().switchTo(address, port, nickname, password, identityId, channel, channelPassword);
|
||||
}
|
||||
|
||||
/** Opens a {@code ts3server://} link handed over by the system or a later launch. */
|
||||
public void openLink(String href) {
|
||||
ServerLink link = ServerLink.parse(href);
|
||||
if (link == null) return;
|
||||
bringToFront();
|
||||
openServerLink(link);
|
||||
}
|
||||
|
||||
/** Goes where a link leads: on a server we are on already, to its channel; else connects there. */
|
||||
private void openServerLink(ServerLink link) {
|
||||
if (!link.bookmarkLabel.isEmpty()) {
|
||||
bookmarks.add(link.toBookmark());
|
||||
bookmarks.save();
|
||||
menuBar.rebuildBookmarks();
|
||||
}
|
||||
for (ServerTab tab : tabs) {
|
||||
if (tab.session().isOn(link)) {
|
||||
selectTab(tab);
|
||||
tab.session().follow(link);
|
||||
return;
|
||||
}
|
||||
}
|
||||
ServerTab tab = tabForNewConnection();
|
||||
if (sessions.microphoneOwner() == null) sessions.giveMicrophoneTo(tab.session());
|
||||
tab.session().open(link, "");
|
||||
}
|
||||
|
||||
/** Connects on the current tab, leaving whatever server it is on. */
|
||||
private void connectToBookmark(Bookmark b) {
|
||||
startConnection(b.address, b.port, b.nickname == null ? "" : b.nickname, b.password, b.identityId, b.channel, b.channelPassword, true);
|
||||
|
||||
Reference in New Issue
Block a user