From 116986c6fc26510ed1c8ab6fbcbb3b0a2a121d6f Mon Sep 17 00:00:00 2001 From: ericek111 Date: Thu, 15 Oct 2026 12:37:50 +0000 Subject: [PATCH] Desktop: Synchronize with myTeamSpeak, with a conflicts window Options -> myTeamSpeak gains Synchronize. Items changed here and in the account since the last run are listed field by field, the differences in bold, each kept from one side; nothing changes until all are settled. Co-Authored-By: Claude Opus 5.5 --- .../eu/lixko/ts3j/client/ui/MainFrame.java | 2 + .../client/ui/options/MyTeamSpeakPanel.java | 76 +++++- .../ui/options/SyncConflictsDialog.java | 228 ++++++++++++++++++ 3 files changed, 302 insertions(+), 4 deletions(-) create mode 100644 ts3-client/swing/src/main/java/eu/lixko/ts3j/client/ui/options/SyncConflictsDialog.java diff --git a/ts3-client/swing/src/main/java/eu/lixko/ts3j/client/ui/MainFrame.java b/ts3-client/swing/src/main/java/eu/lixko/ts3j/client/ui/MainFrame.java index 4130753..de46c19 100644 --- a/ts3-client/swing/src/main/java/eu/lixko/ts3j/client/ui/MainFrame.java +++ b/ts3-client/swing/src/main/java/eu/lixko/ts3j/client/ui/MainFrame.java @@ -14,6 +14,7 @@ import eu.lixko.ts3j.client.config.Settings; import eu.lixko.ts3j.client.contacts.ContactStore; import eu.lixko.ts3j.client.myts.ChatAttachments; import eu.lixko.ts3j.client.myts.MyTeamSpeakLogin; +import eu.lixko.ts3j.client.myts.MyTeamSpeakSync; import eu.lixko.ts3j.client.net.HostButton; import eu.lixko.ts3j.client.net.PictureDownloads; import eu.lixko.ts3j.client.session.SelfState; @@ -639,6 +640,7 @@ public final class MainFrame extends JFrame implements ServerTabPane.Listener { selected == null ? null : selected.session().playback(), sounds, hotkeys, new MyTeamSpeakPanel(myTeamSpeak, new TeamSpeakImporter(identities, bookmarks, settings, contacts), + new MyTeamSpeakSync(myTeamSpeak, identities, bookmarks, settings), this::bookmarksChanged, this::myTeamSpeakChanged), this::contactsChanged); dlg.setVisible(true); diff --git a/ts3-client/swing/src/main/java/eu/lixko/ts3j/client/ui/options/MyTeamSpeakPanel.java b/ts3-client/swing/src/main/java/eu/lixko/ts3j/client/ui/options/MyTeamSpeakPanel.java index a864dbc..8adee84 100644 --- a/ts3-client/swing/src/main/java/eu/lixko/ts3j/client/ui/options/MyTeamSpeakPanel.java +++ b/ts3-client/swing/src/main/java/eu/lixko/ts3j/client/ui/options/MyTeamSpeakPanel.java @@ -6,6 +6,7 @@ import eu.lixko.ts3j.client.config.Bookmark; import eu.lixko.ts3j.client.myts.AccountInfo; import eu.lixko.ts3j.client.myts.MyTeamSpeak; import eu.lixko.ts3j.client.myts.MyTeamSpeakLogin; +import eu.lixko.ts3j.client.myts.MyTeamSpeakSync; import eu.lixko.ts3j.client.myts.MyTsException; import eu.lixko.ts3j.client.teamspeak.SyncItem; import eu.lixko.ts3j.client.teamspeak.TeamSpeakImporter; @@ -36,8 +37,8 @@ import java.util.List; import java.util.stream.Stream; /** - * Options page for the myTeamSpeak account: sign in and out, and see what the - * account synchronises and import it. Everything here takes effect at once rather + * Options page for the myTeamSpeak account: sign in and out, see what the account + * synchronises and import it, or synchronise both ways. Everything here takes effect at once rather * than on OK, as signing in is not a setting to be cancelled. */ public final class MyTeamSpeakPanel extends JPanel { @@ -47,6 +48,7 @@ public final class MyTeamSpeakPanel extends JPanel { private final MyTeamSpeakLogin login; private final TeamSpeakImporter importer; + private final MyTeamSpeakSync sync; private final Runnable onImported; /** Runs when the client signs in or out, the saved sign-in being turned away included. */ private final Runnable onSignInChanged; @@ -66,15 +68,17 @@ public final class MyTeamSpeakPanel extends JPanel { private final FormPanel sections = new FormPanel(false); private final JButton refresh = new JButton("Refresh"); private final JButton importButton = new JButton("Import selected"); + private final JButton syncButton = new JButton("Synchronize"); private final JLabel dataStatus = new JLabel(" "); private List accountItems = List.of(); private boolean fetched; - public MyTeamSpeakPanel(MyTeamSpeakLogin login, TeamSpeakImporter importer, Runnable onImported, - Runnable onSignInChanged) { + public MyTeamSpeakPanel(MyTeamSpeakLogin login, TeamSpeakImporter importer, MyTeamSpeakSync sync, + Runnable onImported, Runnable onSignInChanged) { this.login = login; this.importer = importer; + this.sync = sync; this.onImported = onImported; this.onSignInChanged = onSignInChanged; setLayout(cards); @@ -134,8 +138,11 @@ public final class MyTeamSpeakPanel extends JPanel { buttons.add(refresh); buttons.add(Box.createHorizontalStrut(UIScale.scale(4))); buttons.add(importButton); + buttons.add(Box.createHorizontalStrut(UIScale.scale(4))); + buttons.add(syncButton); refresh.addActionListener(e -> fetch()); importButton.addActionListener(e -> importSelected()); + syncButton.addActionListener(e -> synchronize()); JPanel data = new JPanel(new BorderLayout(0, UIScale.scale(4))); data.add(new JLabel("Synchronised by the account:"), BorderLayout.NORTH); @@ -274,6 +281,66 @@ public final class MyTeamSpeakPanel extends JPanel { updateImportButton(); } + /** Reads the account, settles conflicts with the user, sends, then brings the account's changes here. */ + private void synchronize() { + setDataBusy(true); + status(dataStatus, "Synchronizing…", false); + background(() -> { + MyTeamSpeakSync.Remote remote = sync.fetch(); + SwingUtilities.invokeLater(() -> resolveAndUpload(remote)); + }, this::syncFailed); + } + + private void resolveAndUpload(MyTeamSpeakSync.Remote remote) { + MyTeamSpeakSync.Plan plan; + try { + plan = sync.plan(remote); + } catch (IOException e) { + syncFailed("Could not read local items: " + e.getMessage()); + return; + } + if (!plan.conflicts().isEmpty() && !SyncConflictsDialog.resolve(this, plan.conflicts())) { + setDataBusy(false); + status(dataStatus, "Synchronization cancelled.", false); + return; + } + background(() -> { + sync.upload(plan); + SwingUtilities.invokeLater(() -> applySynced(plan)); + }, this::syncFailed); + } + + private void applySynced(MyTeamSpeakSync.Plan plan) { + setDataBusy(false); + try { + MyTeamSpeakSync.Result r = sync.apply(plan); + status(dataStatus, r.sent() == 0 && r.received() == 0 ? "In sync." + : "Synchronized: " + r.sent() + " sent, " + r.received() + " received.", false); + if (onImported != null) onImported.run(); + } catch (IOException e) { + status(dataStatus, "Could not save synchronized items: " + e.getMessage(), true); + } + refreshPresence(); + } + + private void syncFailed(String message) { + setDataBusy(false); + status(dataStatus, message, true); + showState(); + if (!login.isSignedIn()) { + status(signInStatus, message, true); + onSignInChanged.run(); + } + } + + /** Whether each listed item is here, after something changed the stores. */ + private void refreshPresence() { + List rows = new ArrayList<>(); + allRows().forEach(row -> rows.add(new Row(row.item(), importer.isPresent(row.item()), row.selected()))); + setRows(rows); + updateImportButton(); + } + private interface Work { void run() throws IOException, MyTsException; } @@ -300,6 +367,7 @@ public final class MyTeamSpeakPanel extends JPanel { private void setDataBusy(boolean busy) { refresh.setEnabled(!busy); importButton.setEnabled(!busy); + syncButton.setEnabled(!busy); if (!busy) updateImportButton(); } diff --git a/ts3-client/swing/src/main/java/eu/lixko/ts3j/client/ui/options/SyncConflictsDialog.java b/ts3-client/swing/src/main/java/eu/lixko/ts3j/client/ui/options/SyncConflictsDialog.java new file mode 100644 index 0000000..f5696d1 --- /dev/null +++ b/ts3-client/swing/src/main/java/eu/lixko/ts3j/client/ui/options/SyncConflictsDialog.java @@ -0,0 +1,228 @@ +package eu.lixko.ts3j.client.ui.options; + +import com.formdev.flatlaf.util.ScaledEmptyBorder; +import com.formdev.flatlaf.util.UIScale; +import eu.lixko.ts3j.client.myts.MyTeamSpeakSync.Conflict; +import eu.lixko.ts3j.client.ui.widget.Dialogs; + +import javax.swing.Box; +import javax.swing.BoxLayout; +import javax.swing.ButtonGroup; +import javax.swing.JButton; +import javax.swing.JDialog; +import javax.swing.JLabel; +import javax.swing.JPanel; +import javax.swing.JRadioButton; +import javax.swing.JScrollPane; +import javax.swing.JTable; +import javax.swing.UIManager; +import javax.swing.table.AbstractTableModel; +import javax.swing.table.DefaultTableCellRenderer; +import java.awt.BorderLayout; +import java.awt.Component; +import java.awt.Dimension; +import java.awt.Font; +import java.awt.Window; +import java.util.ArrayList; +import java.util.List; + +/** + * Asks which copy to keep of each item changed both here and in myTeamSpeak since the last + * synchronisation: the item's fields side by side, the ones that differ in bold. + */ +final class SyncConflictsDialog extends JDialog { + + private final List conflicts; + private final List groups = new ArrayList<>(); + private final JButton ok = new JButton("Synchronize"); + private boolean accepted; + + private SyncConflictsDialog(Window owner, List conflicts) { + super(owner, "Synchronization Conflicts", ModalityType.APPLICATION_MODAL); + this.conflicts = conflicts; + + JPanel list = new JPanel(); + list.setLayout(new BoxLayout(list, BoxLayout.Y_AXIS)); + list.setBorder(new ScaledEmptyBorder(8, 8, 8, 8)); + for (Conflict conflict : conflicts) { + list.add(section(conflict)); + list.add(Box.createVerticalStrut(UIScale.scale(12))); + } + JScrollPane scroll = new JScrollPane(list); + scroll.getVerticalScrollBar().setUnitIncrement(UIScale.scale(16)); + scroll.setPreferredSize(new Dimension(UIScale.scale(640), + Math.min(UIScale.scale(520), list.getPreferredSize().height + UIScale.scale(8)))); + + JLabel intro = new JLabel("Changed here and in myTeamSpeak since the last synchronization:"); + intro.setBorder(new ScaledEmptyBorder(10, 10, 4, 10)); + + getContentPane().setLayout(new BorderLayout()); + getContentPane().add(intro, BorderLayout.NORTH); + getContentPane().add(scroll, BorderLayout.CENTER); + getContentPane().add(buttons(), BorderLayout.SOUTH); + + updateOk(); + Dialogs.closeOnEscape(this); + setDefaultCloseOperation(DISPOSE_ON_CLOSE); + pack(); + setLocationRelativeTo(owner); + } + + /** + * Shows the conflicts and records the choices on them. + * + * @return whether the user went on with the synchronization + */ + static boolean resolve(Component parent, List conflicts) { + Window owner = parent == null ? null : javax.swing.SwingUtilities.getWindowAncestor(parent); + SyncConflictsDialog dialog = new SyncConflictsDialog(owner, conflicts); + dialog.setVisible(true); + return dialog.accepted; + } + + private JPanel section(Conflict conflict) { + JLabel title = new JLabel((conflict.isIdentity() ? "Identity: " : "Bookmark: ") + conflict.title()); + title.setFont(title.getFont().deriveFont(Font.BOLD)); + title.setAlignmentX(LEFT_ALIGNMENT); + + JTable table = new JTable(new DifferenceModel(conflict)); + table.setDefaultRenderer(Object.class, new DifferenceRenderer()); + table.setRowSelectionAllowed(false); + table.setFocusable(false); + table.getTableHeader().setReorderingAllowed(false); + JPanel tablePanel = new JPanel(new BorderLayout()); + tablePanel.add(table.getTableHeader(), BorderLayout.NORTH); + tablePanel.add(table, BorderLayout.CENTER); + tablePanel.setAlignmentX(LEFT_ALIGNMENT); + + JRadioButton here = new JRadioButton(conflict.deletedHere() ? "Delete" : "Keep this device's"); + JRadioButton cloud = new JRadioButton(conflict.deletedInCloud() ? "Delete" : "Keep myTeamSpeak's"); + ButtonGroup group = new ButtonGroup(); + group.add(here); + group.add(cloud); + groups.add(group); + here.addActionListener(e -> choose(conflict, Conflict.Choice.HERE)); + cloud.addActionListener(e -> choose(conflict, Conflict.Choice.CLOUD)); + if (conflict.choice() == Conflict.Choice.HERE) here.setSelected(true); + if (conflict.choice() == Conflict.Choice.CLOUD) cloud.setSelected(true); + + JPanel choices = new JPanel(); + choices.setLayout(new BoxLayout(choices, BoxLayout.X_AXIS)); + choices.add(here); + choices.add(Box.createHorizontalStrut(UIScale.scale(16))); + choices.add(cloud); + choices.add(Box.createHorizontalGlue()); + choices.setAlignmentX(LEFT_ALIGNMENT); + + JPanel section = new JPanel(); + section.setLayout(new BoxLayout(section, BoxLayout.Y_AXIS)); + section.add(title); + section.add(Box.createVerticalStrut(UIScale.scale(4))); + section.add(tablePanel); + section.add(Box.createVerticalStrut(UIScale.scale(4))); + section.add(choices); + section.setAlignmentX(LEFT_ALIGNMENT); + return section; + } + + private JPanel buttons() { + JButton allHere = new JButton("Keep all of this device's"); + JButton allCloud = new JButton("Keep all of myTeamSpeak's"); + JButton cancel = new JButton("Cancel"); + allHere.addActionListener(e -> chooseAll(Conflict.Choice.HERE)); + allCloud.addActionListener(e -> chooseAll(Conflict.Choice.CLOUD)); + ok.addActionListener(e -> { + accepted = true; + dispose(); + }); + cancel.addActionListener(e -> dispose()); + + JPanel row = new JPanel(); + row.setLayout(new BoxLayout(row, BoxLayout.X_AXIS)); + row.setBorder(new ScaledEmptyBorder(8, 10, 10, 10)); + row.add(allHere); + row.add(Box.createHorizontalStrut(UIScale.scale(4))); + row.add(allCloud); + row.add(Box.createHorizontalGlue()); + row.add(ok); + row.add(Box.createHorizontalStrut(UIScale.scale(4))); + row.add(cancel); + getRootPane().setDefaultButton(ok); + return row; + } + + private void choose(Conflict conflict, Conflict.Choice choice) { + conflict.choose(choice); + updateOk(); + } + + private void chooseAll(Conflict.Choice choice) { + for (int i = 0; i < conflicts.size(); i++) { + conflicts.get(i).choose(choice); + ButtonGroup group = groups.get(i); + var buttons = java.util.Collections.list(group.getElements()); + buttons.get(choice == Conflict.Choice.HERE ? 0 : 1).setSelected(true); + } + updateOk(); + } + + private void updateOk() { + ok.setEnabled(conflicts.stream().allMatch(c -> c.choice() != null)); + } + + /** One row per field: its name, then this device's and myTeamSpeak's value. */ + private static final class DifferenceModel extends AbstractTableModel { + private final Conflict conflict; + private final List rows; + + DifferenceModel(Conflict conflict) { + this.conflict = conflict; + this.rows = conflict.differences(); + } + + boolean differs(int row) { + return rows.get(row).differs(); + } + + @Override + public int getRowCount() { + return rows.size(); + } + + @Override + public int getColumnCount() { + return 3; + } + + @Override + public String getColumnName(int column) { + return switch (column) { + case 0 -> ""; + case 1 -> conflict.deletedHere() ? "This device (deleted)" : "This device"; + default -> conflict.deletedInCloud() ? "myTeamSpeak (deleted)" : "myTeamSpeak"; + }; + } + + @Override + public Object getValueAt(int row, int column) { + Conflict.Difference d = rows.get(row); + return switch (column) { + case 0 -> d.label(); + case 1 -> conflict.deletedHere() ? "—" : d.here(); + default -> conflict.deletedInCloud() ? "—" : d.cloud(); + }; + } + } + + private static final class DifferenceRenderer extends DefaultTableCellRenderer { + @Override + public Component getTableCellRendererComponent(JTable table, Object value, boolean selected, boolean focused, + int row, int column) { + super.getTableCellRendererComponent(table, value, false, false, row, column); + boolean differs = ((DifferenceModel) table.getModel()).differs(row); + setFont(table.getFont().deriveFont(differs && column > 0 ? Font.BOLD : Font.PLAIN)); + setForeground(UIManager.getColor(column == 0 ? "Label.disabledForeground" : "Table.foreground")); + return this; + } + } +}