Skip to content

Commit

Permalink
Add keyboard dialog, closes #110
Browse files Browse the repository at this point in the history
  • Loading branch information
RetGal committed Feb 28, 2024
1 parent 3bb8e8b commit 71b1bd3
Show file tree
Hide file tree
Showing 17 changed files with 93 additions and 39 deletions.
4 changes: 2 additions & 2 deletions src/main/java/mpo/dayon/assistant/gui/Assistant.java
Original file line number Diff line number Diff line change
Expand Up @@ -837,11 +837,11 @@ public boolean onAccepted(Socket connection) {
* Should not block as called from the network receiving thread (!)
*/
@Override
public void onConnected(Socket connection, char osId) {
public void onConnected(Socket connection, char osId, String inputLocale) {
sendCaptureConfiguration(captureEngineConfiguration);
sendCompressorConfiguration(compressorEngineConfiguration);
frame.resetCanvas();
frame.onSessionStarted(osId);
frame.onSessionStarted(osId, inputLocale);
}

@Override
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/mpo/dayon/assistant/gui/AssistantFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.im.InputContext;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.Socket;
Expand Down Expand Up @@ -474,7 +475,7 @@ void onClipboardReceived() {
enableTransferControls();
}

void onSessionStarted(char osId) {
void onSessionStarted(char osId, String inputLocale) {
this.osId = osId;
if (osId == 'm') {
windowsKeyToggleButton.setIcon(getOrCreateIcon(ImageNames.CMD));
Expand All @@ -483,6 +484,10 @@ void onSessionStarted(char osId) {
windowsKeyToggleButton.setIcon(getOrCreateIcon(ImageNames.WIN));
windowsKeyToggleButton.setToolTipText(translate("send.winKey"));
}
if (!inputLocale.isEmpty() && !inputLocale.equals(InputContext.getInstance().getLocale().toString())) {
String infoMessage = format("%s\n%s\n%s", translate("keyboardlayout.msg1", inputLocale), translate("keyboardlayout.msg2"), translate("keyboardlayout.msg3"));
JOptionPane.showMessageDialog(this, infoMessage, "", JOptionPane.INFORMATION_MESSAGE);
}
long sessionStartTime = Instant.now().getEpochSecond();
sessionTimer = new Timer(1000, e -> {
final long seconds = Instant.now().getEpochSecond() - sessionStartTime;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ private boolean processIntroduced(NetworkMessageType type, ObjectInputStream in)
}
}

private boolean processUnIntroduced(NetworkMessageType type, ObjectInputStream in) throws IOException {
private boolean processUnIntroduced(NetworkMessageType type, ObjectInputStream in) throws IOException, ClassNotFoundException {
switch (type) {
case HELLO:
fireOnConnected(connection, introduce(in));
Expand All @@ -275,14 +275,14 @@ private boolean processUnIntroduced(NetworkMessageType type, ObjectInputStream i
}
}

private char introduce(ObjectInputStream in) throws IOException {
private NetworkHelloMessage introduce(ObjectInputStream in) throws IOException, ClassNotFoundException {
final NetworkHelloMessage hello = NetworkHelloMessage.unmarshall(in);
fireOnByteReceived(1 + hello.getWireSize()); // +1 : magic number (byte)
if (!isCompatibleVersion(hello.getMajor(), hello.getMinor(), Version.get())) {
Log.error(format("Incompatible assisted version: %d.%d", hello.getMajor(), hello.getMinor()));
throw new IOException("version.wrong");
}
return hello.getOsId();
return hello;
}

/**
Expand Down Expand Up @@ -351,8 +351,8 @@ private boolean fireOnAccepted(Socket connection) {
return listeners.getListeners().stream().allMatch(listener -> listener.onAccepted(connection));
}

private void fireOnConnected(Socket connection, char osId) {
listeners.getListeners().forEach(listener -> listener.onConnected(connection, osId));
private void fireOnConnected(Socket connection, NetworkHelloMessage hello) {
listeners.getListeners().forEach(listener -> listener.onConnected(connection, hello.getOsId(), hello.getInputLocale()));
}

private void fireOnByteReceived(int count) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public interface NetworkAssistantEngineListener extends Listener {
/**
* Should not block as called from the network receiving thread (!)
*/
void onConnected(Socket connection, char osId);
void onConnected(Socket connection, char osId, String inputLocale);

/**
* Should not block as called from the network receiving thread (!)
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/mpo/dayon/common/network/NetworkSender.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package mpo.dayon.common.network;

import java.awt.Point;
import java.awt.im.InputContext;
import java.io.File;
import java.io.IOException;
import java.io.ObjectOutputStream;
Expand Down Expand Up @@ -61,7 +62,8 @@ public void cancel() {
*/
public void sendHello(char osId) {
final Version version = Version.get();
send(true, new NetworkHelloMessage(version.getMajor(), version.getMinor(), osId));
final String inputLocale = InputContext.getInstance().getLocale().toString();
send(true, new NetworkHelloMessage(version.getMajor(), version.getMinor(), osId, inputLocale));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@ public class NetworkHelloMessage extends NetworkMessage {

private final char osId;

public NetworkHelloMessage(int major, int minor, char osId) {
private final String inputLocale;

public NetworkHelloMessage(int major, int minor, char osId, String inputLocale) {
this.major = major;
this.minor = minor;
this.osId = osId;
}
this.inputLocale = inputLocale;
}

@Override
public NetworkMessageType getType() {
Expand All @@ -32,13 +35,17 @@ public char getOsId() {
return osId;
}

public String getInputLocale() {
return inputLocale;
}

/**
* Take into account some extra-info sent over the network with the actual
* payload ...
*/
@Override
public int getWireSize() {
return 11; // type (byte) + major (int) + minor (int) + osId (char)
return 11 + inputLocale.length(); // (type (byte) + major (int) + minor (int) + osId (char)) + localeId (String
}

@Override
Expand All @@ -47,16 +54,21 @@ public void marshall(ObjectOutputStream out) throws IOException {
out.writeInt(major);
out.writeInt(minor);
out.writeChar(osId);
out.writeUTF(inputLocale);
}

public static NetworkHelloMessage unmarshall(ObjectInputStream in) throws IOException {
public static NetworkHelloMessage unmarshall(ObjectInputStream in) throws IOException, ClassNotFoundException {
final int major = in.readInt();
final int minor = in.readInt();
char osId = major > 13 || (major == 13 && minor > 0) || major == 0 ? in.readChar() : 'x';
return new NetworkHelloMessage(major, minor, osId);
if (major > 13 || (major == 13 && minor > 0) || major == 0 ) {
char osId = in.readChar();
String localeId = in.readUTF();
return new NetworkHelloMessage(major, minor, osId, localeId);
}
return new NetworkHelloMessage(major, minor, 'x', "");
}

public String toString() {
return String.format("[major:%d] [minor:%s] [osId:%c]", major, minor, osId);
return String.format("[major:%d] [minor:%d] [osId:%c] [inputLocale:%s]", major, minor, osId, inputLocale);
}
}
4 changes: 4 additions & 0 deletions src/main/resources/Babylon.properties
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,7 @@ compatibility.mode.info = Please consider updating Assisted to the latest versio

changeLanguage = Change language
startChat = Start a chat

keyboardlayout.msg1 = The assisted currently uses a different keyboard layout: %s
keyboardlayout.msg2 = This can lead to problems, especially when entering special characters.
keyboardlayout.msg3 = If you experience problems, please ensure you are using the same on both computers.
6 changes: 5 additions & 1 deletion src/main/resources/Babylon_de.properties
Original file line number Diff line number Diff line change
Expand Up @@ -189,4 +189,8 @@ compatibility.mode.active = Kompatibilit\u00E4tsmodus aktiv
compatibility.mode.info = Um Sicherheitsrisiken zu minimieren, aktualisieren Sie doch bitte den Assistierten auf die neuste Version.

changeLanguage = Sprache \u00E4ndern
startChat = Chat starten
startChat = Chat starten

keyboardlayout.msg1 = Der Assistent verwendet derzeit eine andere Tastaturbelegung: %s
keyboardlayout.msg2 = Dies kann insbesondere bei der Eingabe von Sonderzeichen zu Problemen f\u00FChren.
keyboardlayout.msg3 = Sollten Probleme auftreten, stellen Sie bitte sicher,\ndass Sie auf beiden Computern die gleiche Tastaturbelegung verwenden.
6 changes: 5 additions & 1 deletion src/main/resources/Babylon_es.properties
Original file line number Diff line number Diff line change
Expand Up @@ -189,4 +189,8 @@ compatibility.mode.active = Modo de compatibilidad activo
compatibility.mode.info = Por favor, considere actualizar Assisted a la \u00FAltima versi\u00F3n para minimizar los riesgos.

changeLanguage = Cambiar el idioma
startChat = Iniciar un chat
startChat = Iniciar un chat

keyboardlayout.msg1 = Actualmente, el asistido utiliza una distribuci\u00F3n de teclado diferente: %s
keyboardlayout.msg2 = Esto puede ocasionar problemas, especialmente al introducir caracteres especiales.
keyboardlayout.msg3 = Si se producen problemas, aseg\u00FArese de que utiliza la misma distribuci\u00F3n de teclado en ambos ordenadores.
6 changes: 5 additions & 1 deletion src/main/resources/Babylon_fr.properties
Original file line number Diff line number Diff line change
Expand Up @@ -189,4 +189,8 @@ compatibility.mode.active = Mode de compatibilit\u00E9 actif
compatibility.mode.info = Veuillez consid\u00E9rer la mise \u00E0 jour de l'assist\u00E9 vers la derni\u00E8re version afin de minimiser les risques.

changeLanguage = Changer la langue
startChat = Commencer un chat
startChat = Commencer un chat

keyboardlayout.msg1 = L'assist\u00E9 utilise actuellement une autre disposition de clavier : %s
keyboardlayout.msg2 = Cela peut entra\u00EEner des probl\u00E8mes, notamment lors de la saisie de caract\u00E8res sp\u00E9ciaux.
keyboardlayout.msg3 = En cas de probl\u00E8me, assurez-vous que vous utilisez la m\u00EAme disposition de clavier sur les deux ordinateurs.
6 changes: 5 additions & 1 deletion src/main/resources/Babylon_it.properties
Original file line number Diff line number Diff line change
Expand Up @@ -189,4 +189,8 @@ compatibility.mode.active = Modalit\u00E0 di compatibilit\u00E0 attiva
compatibility.mode.info = Si consiglia di aggiornare assistito alla versione pi\u00F9 recente per ridurre al minimo i rischi.

changeLanguage = Cambiare la lingua
startChat = Iniziare una chat
startChat = Iniziare una chat

keyboardlayout.msg1 = L'assistito utilizza attualmente un layout di tastiera diverso: %s
keyboardlayout.msg2 = Questo pu\u00F2 causare problemi, soprattutto nell'inserimento di caratteri speciali.
keyboardlayout.msg3 = In caso di problemi, accertarsi di utilizzare lo stesso layout di tastiera su entrambi i computer.
6 changes: 5 additions & 1 deletion src/main/resources/Babylon_ru.properties
Original file line number Diff line number Diff line change
Expand Up @@ -189,4 +189,8 @@ compatibility.mode.active = \u0420\u0435\u0436\u0438\u043C \u0441\u043E\u0432\u0
compatibility.mode.info = \u041F\u043E\u0436\u0430\u043B\u0443\u0439\u0441\u0442\u0430, \u043E\u0431\u043D\u043E\u0432\u0438\u0442\u0435 \u041F\u043E\u043C\u043E\u0433\u0430\u043B \u0434\u043E \u043F\u043E\u0441\u043B\u0435\u0434\u043D\u0435\u0439 \u0432\u0435\u0440\u0441\u0438\u0438, \u0447\u0442\u043E\u0431\u044B \u043C\u0438\u043D\u0438\u043C\u0438\u0437\u0438\u0440\u043E\u0432\u0430\u0442\u044C \u0440\u0438\u0441\u043A\u0438.

changeLanguage = \u0438\u0437\u043C\u0435\u043D\u0438\u0442\u044C \u044F\u0437\u044B\u043A
startChat = \u043D\u0430\u0447\u0430\u0442\u044C \u0447\u0430\u0442
startChat = \u043D\u0430\u0447\u0430\u0442\u044C \u0447\u0430\u0442

keyboardlayout.msg1 = \u0412 \u043D\u0430\u0441\u0442\u043E\u044F\u0449\u0435\u0435 \u0432\u0440\u0435\u043C\u044F \u041F\u043E\u043C\u043E\u0433\u0430\u043B \u0438\u0441\u043F\u043E\u043B\u044C\u0437\u0443\u0435\u0442 \u0434\u0440\u0443\u0433\u0443\u044E \u0440\u0430\u0441\u043A\u043B\u0430\u0434\u043A\u0443 \u043A\u043B\u0430\u0432\u0438\u0430\u0442\u0443\u0440\u044B: %s
keyboardlayout.msg2 = \u042D\u0442\u043E \u043C\u043E\u0436\u0435\u0442 \u043F\u0440\u0438\u0432\u0435\u0441\u0442\u0438 \u043A \u043F\u0440\u043E\u0431\u043B\u0435\u043C\u0430\u043C, \u043E\u0441\u043E\u0431\u0435\u043D\u043D\u043E \u043F\u0440\u0438 \u0432\u0432\u043E\u0434\u0435 \u0441\u043F\u0435\u0446\u0438\u0430\u043B\u044C\u043D\u044B\u0445 \u0441\u0438\u043C\u0432\u043E\u043B\u043E\u0432.
keyboardlayout.msg3 = \u0415\u0441\u043B\u0438 \u0432\u043E\u0437\u043D\u0438\u043A\u043B\u0438 \u043F\u0440\u043E\u0431\u043B\u0435\u043C\u044B, \u0443\u0431\u0435\u0434\u0438\u0442\u0435\u0441\u044C, \u0447\u0442\u043E \u043D\u0430 \u043E\u0431\u043E\u0438\u0445 \u043A\u043E\u043C\u043F\u044C\u044E\u0442\u0435\u0440\u0430\u0445 \u0438\u0441\u043F\u043E\u043B\u044C\u0437\u0443\u0435\u0442\u0441\u044F \u043E\u0434\u0438\u043D\u0430\u043A\u043E\u0432\u0430\u044F \u0440\u0430\u0441\u043A\u043B\u0430\u0434\u043A\u0430 \u043A\u043B\u0430\u0432\u0438\u0430\u0442\u0443\u0440\u044B.
6 changes: 5 additions & 1 deletion src/main/resources/Babylon_sv.properties
Original file line number Diff line number Diff line change
Expand Up @@ -189,4 +189,8 @@ compatibility.mode.active = Kompatibiltetsl\u00E4ge aktiverat
compatibility.mode.info = V\u00E4nligen uppdatera Hj\u00E4lptagare till den senaste versionen f\u00F6r att minimera risker.

changeLanguage = V\u00E4xla spr\u00E5kl\u00E4ge
startChat = Starta en chatt
startChat = Starta en chatt

keyboardlayout.msg1 = Hj\u00E4lptagare anv\u00E4nder f\u00F6r n\u00E4rvarande en annan tangentbordslayout: %s
keyboardlayout.msg2 = Detta kan leda till problem, s\u00E4rskilt vid inmatning av specialtecken.
keyboardlayout.msg3 = Om du upplever problem, kontrollera att du anv\u00E4nder samma tangentbord p\u00E5 b\u00E5da datorerna.
6 changes: 5 additions & 1 deletion src/main/resources/Babylon_tr.properties
Original file line number Diff line number Diff line change
Expand Up @@ -189,4 +189,8 @@ compatibility.mode.active = Uyumluluk modu etkin
compatibility.mode.info = Riskleri en aza indirmek i\u00E7in l\u00FCtfen yard\u0131ml\u0131 en son s\u00FCr\u00FCme g\u00FCncellemeyi d\u00FC\u015F\u00FCn\u00FCn.

changeLanguage = Dili de\u011Fi\u015Ftir
startChat = Sohbet ba\u015Flat
startChat = Sohbet ba\u015Flat

keyboardlayout.msg1 = Yard\u0131ml\u0131 \u015Fu anda farkl\u0131 bir klavye d\u00FCzeni kullan\u0131yor: %s
keyboardlayout.msg2 = Bu, \u00F6zellikle \u00F6zel karakterleri girerken sorunlara neden olabilir.
keyboardlayout.msg3 = Sorun ya\u015Farsan\u0131z, l\u00FCtfen her iki bilgisayarda da ayn\u0131 klavyeyi kulland\u0131\u011F\u0131n\u0131zdan emin olun.
6 changes: 5 additions & 1 deletion src/main/resources/Babylon_zh.properties
Original file line number Diff line number Diff line change
Expand Up @@ -293,4 +293,8 @@ compatibility.mode.active = \u517C\u5BB9\u6A21\u5F0F\u5DF2\u6FC0\u6D3B
compatibility.mode.info = \u8BF7\u8003\u8651\u5C06\u60A8\u7684\u540C\u884C\u7CFB\u7EDF\u5347\u7EA7\u5230\u6700\u65B0\u7248\u672C\uFF0C\u4EE5\u6700\u5927\u9650\u5EA6\u5730\u964D\u4F4E\u98CE\u9669\u3002

changeLanguage = \u6539\u53D8\u8BED\u8A00
startChat = \u5F00\u59CB\u804A\u5929
startChat = \u5F00\u59CB\u804A\u5929

Keyboardlayout.msg1 = Assisted - \u7528\u6237\u7AEF\u5F53\u524D\u4F7F\u7528\u4E0D\u540C\u7684\u952E\u76D8\u5E03\u5C40\uFF1A%s
Keyboardlayout.msg2 = \u8FD9\u53EF\u80FD\u4F1A\u5BFC\u81F4\u95EE\u9898\uFF0C\u5C24\u5176\u662F\u5728\u8F93\u5165\u7279\u6B8A\u5B57\u7B26\u65F6\u3002
Keyboardlayout.msg3 = \u5982\u679C\u60A8\u9047\u5230\u95EE\u9898\uFF0C\u8BF7\u786E\u4FDD\u60A8\u5728\u4E24\u53F0\u8BA1\u7B97\u673A\u4E0A\u4F7F\u7528\u76F8\u540C\u7684\u8BBE\u7F6E\u3002
16 changes: 5 additions & 11 deletions src/test/java/mpo/dayon/common/network/NetworkSenderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.mockito.Mockito;

import java.awt.*;
import java.awt.im.InputContext;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.List;
Expand Down Expand Up @@ -44,23 +45,16 @@ void init() {
@Test
void sendHello() throws IOException {
// given
final int major = 0;
final int minor = 0;
final char osId = 'l';
final String inputLocale = InputContext.getInstance().getLocale().toString();
// when
sender.sendHello(osId);
// then
verify(outMock, timeout(50)).writeByte(MAGIC_NUMBER);
verify(outMock).write(NetworkMessageType.HELLO.ordinal());
verify(outMock, times(2)).writeInt(valueCaptor.capture());
verify(outMock).writeChar(valueCaptor.capture());
final List<Integer> capturedValues = valueCaptor.getAllValues();
int first = capturedValues.get(0);
int second = capturedValues.get(1);
int last = capturedValues.get(capturedValues.size() - 1);
assertEquals(major, first);
assertEquals(minor, second);
assertEquals(osId, last);
verify(outMock, times(2)).writeInt(anyInt());
verify(outMock).writeChar(osId);
verify(outMock).writeUTF(inputLocale);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,21 @@ void testHelloMessage() {
final int major = 0;
final int minor = 0;
final char osId = 'l';
final String inputLocale = "de_CH";

// when
final NetworkHelloMessage message = new NetworkHelloMessage(major, minor, osId);
final NetworkHelloMessage message = new NetworkHelloMessage(major, minor, osId, inputLocale);

// then
assertEquals(major, message.getMajor());
assertEquals(minor, message.getMinor());
assertEquals(osId, message.getOsId());
assertEquals(inputLocale, message.getInputLocale());
}

@ParameterizedTest
@CsvSource({ "13, 0", "12, 0", "11, 0" })
void unmarshallHelloMessageFromLegacyVersion(int major, int minor) throws IOException {
void unmarshallHelloMessageFromLegacyVersion(int major, int minor) throws IOException, ClassNotFoundException {
// given
String fileName = "tmp";
ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream(fileName));
Expand All @@ -45,19 +47,21 @@ void unmarshallHelloMessageFromLegacyVersion(int major, int minor) throws IOExce
assertEquals(major, message.getMajor());
assertEquals(minor, message.getMinor());
assertEquals('x', message.getOsId(), "Should use default value without trying to read osId from the stream");
assertEquals("", message.getInputLocale(), "Should use default value without trying to read inputLocale from the stream");
}



@ParameterizedTest
@CsvSource({ "13, 1, l", "0, 0, w" })
void unmarshallHelloMessageFromSupportedVersion(int major, int minor, char osId) throws IOException {
void unmarshallHelloMessageFromSupportedVersion(int major, int minor, char osId) throws IOException, ClassNotFoundException {
// given
String fileName = "tmp";
ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream(fileName));
output.writeInt(major);
output.writeInt(minor);
output.writeChar(osId);
output.writeUTF("de_CH");
output.close();
ObjectInputStream objStream = new ObjectInputStream(new FileInputStream(fileName));

Expand All @@ -69,5 +73,6 @@ void unmarshallHelloMessageFromSupportedVersion(int major, int minor, char osId)
assertEquals(major, message.getMajor());
assertEquals(minor, message.getMinor());
assertEquals(osId, message.getOsId());
assertEquals("de_CH", message.getInputLocale());
}
}

0 comments on commit 71b1bd3

Please sign in to comment.