Compare commits

..

No commits in common. "475808acd80e56423f2a710a3df24fd7240646e9" and "eb07e42ab0e83e41b592df4166f9b47e7c9adda2" have entirely different histories.

6 changed files with 22 additions and 180 deletions

View File

@ -176,7 +176,7 @@ public class Main extends Application {
Path nextMessagePath = stationDirPath.resolve("next-message.txt");
String messageText;
if (!Files.exists(nextMessagePath)) {
messageText = loadedStation.generateMessage(settings.getMessageGenerationAttempts());
messageText = loadedStation.generateMessage();
} else {
messageText = new String(Files.readAllBytes(nextMessagePath));
}
@ -202,9 +202,9 @@ public class Main extends Application {
System.exit(1);
}
String newMessageText = loadedStation.generateMessage(settings.getMessageGenerationAttempts());
String newMessageText = loadedStation.generateMessage();
Files.write(nextMessagePath, newMessageText.getBytes(StandardCharsets.UTF_8));
} catch (IOException | StationSettings.MessageGenerationException e) {
} catch (IOException e) {
logger.log(Level.SEVERE, "Exception while posting message to station " + stationName, e);
// TODO: Notification
System.exit(1);

View File

@ -8,7 +8,6 @@ import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.Random;
@ -50,6 +49,9 @@ public class MainController implements Initializable {
@FXML
private void handleStationSettingsButtonPress() {
System.out.println("Button pressed!");
lastRetrievedLabel.setText("Button pressed!");
if (settingsStage == null || !settingsStage.isShowing()) {
try {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("StationSettingsView.fxml"));
@ -65,7 +67,6 @@ public class MainController implements Initializable {
return;
}
controller.setStationSettings(selectedStation);
controller.setStationNameList(settings.getStations().stream().map(StationSettings::toString).toList());
settingsStage = new Stage();
settingsStage.initModality(Modality.APPLICATION_MODAL);
@ -168,12 +169,8 @@ public class MainController implements Initializable {
.findFirst()
.orElse(null);
// TODO: if there are no stations, then create a default station and select that
if (selectedStation == null) {
logger.log(Level.SEVERE, "Selected station '" + selectedStationName.get() + "' not found");
selectedStation = settings.getStations().get(0);
selectedStationName.set(selectedStation.getName());
// TODO: create a new station and save it?
}
messageTextArea.addEventFilter(KeyEvent.ANY, event -> {

View File

@ -18,8 +18,8 @@ public class MainSettings {
private static final Logger logger = Logger.getLogger(Main.class.getName());
private int digitsPerGroup;
private int messageGenerationAttempts;
private String username;
private int refreshInterval;
private String selectedStationName;
private ArrayList<StationSettings> stations;
@ -27,7 +27,6 @@ public class MainSettings {
public MainSettings() {
stations = new ArrayList<>();
stations.add(new StationSettings("Station 1"));
messageGenerationAttempts = 5;
}
public static Path getSettingsFilePath() {
@ -48,14 +47,6 @@ public class MainSettings {
return stations;
}
public int getMessageGenerationAttempts() {
return messageGenerationAttempts;
}
public void setMessageGenerationAttempts(int messageGenerationAttempts) {
this.messageGenerationAttempts = messageGenerationAttempts;
}
public String getUsername() {
return username;
}
@ -64,6 +55,14 @@ public class MainSettings {
this.username = username;
}
public int getRefreshInterval() {
return refreshInterval;
}
public void setRefreshInterval(int refreshInterval) {
this.refreshInterval = refreshInterval;
}
public String getSelectedStationName() {
return selectedStationName;
}

View File

@ -1,15 +1,10 @@
package name.nathanmcrae.numbersstation;
import java.io.IOException;
import java.util.logging.Logger;
import java.util.logging.Level;
import java.util.Optional;
import javafx.collections.ListChangeListener;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.control.TextInputDialog;
@ -19,7 +14,6 @@ import javafx.scene.Node;
import javafx.stage.Stage;
public class StationSelectionController {
private static final Logger logger = Logger.getLogger(Main.class.getName());
private MainSettings settings;
@ -64,26 +58,6 @@ public class StationSelectionController {
Optional<String> result = dialog.showAndWait();
result.ifPresent(stationName -> {
if (stationName == null || stationName.equals("")) {
Alert alert = new Alert(Alert.AlertType.WARNING);
alert.setTitle("Invalid station name");
alert.setHeaderText(null);
alert.setContentText("Station name must not be empty");
alert.showAndWait();
return;
}
for (StationSettings station : stationList) {
if (stationName.toLowerCase().equals(station.getName().toLowerCase())) {
Alert alert = new Alert(Alert.AlertType.WARNING);
alert.setTitle("Invalid station name");
alert.setHeaderText(null);
alert.setContentText("Station name '" + stationName + "' is already used.");
alert.showAndWait();
return;
}
}
StationSettings newStation = new StationSettings(stationName);
stationList.add(newStation);
});
@ -99,16 +73,7 @@ public class StationSelectionController {
@FXML
private void handleRemoveButtonPress(ActionEvent event) {
int selectedIndex = stationListView.getSelectionModel().getSelectedIndex();
StationSettings station = stationList.get(selectedIndex);
String stationName = station.toString();
String message = "Delete station '" + stationName + "'? All data will be erased.";
Alert alert = new Alert(Alert.AlertType.CONFIRMATION, message, ButtonType.YES, ButtonType.NO);
alert.setTitle("Delete station?");
alert.setHeaderText(null);
alert.showAndWait();
if (selectedIndex >= 0 && alert.getResult() == ButtonType.YES) {
if (selectedIndex >= 0) {
stationList.remove(selectedIndex);
}
}
@ -136,18 +101,6 @@ public class StationSelectionController {
if (change.wasRemoved()) {
for (StationSettings removedStation : change.getRemoved()) {
System.out.println("Removed: " + removedStation.getName());
try {
removedStation.deleteDir();
} catch (IOException e) {
logger.log(Level.SEVERE, "Exception when removing station directory", e);
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle("Error removing station");
alert.setHeaderText(null);
alert.setContentText("Exception when removing station directory: " + e.getMessage());
alert.showAndWait();
return;
}
}
}
}

View File

@ -1,23 +1,15 @@
package name.nathanmcrae.numbersstation;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.Files;
import java.nio.file.FileVisitResult;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.security.SecureRandom;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.logging.Logger;
import java.util.Random;
public class StationSettings {
private static final Logger logger = Logger.getLogger(Main.class.getName());
private String address;
private int digitsPerGroup;
private String externalProgramCommand;
@ -65,22 +57,6 @@ public class StationSettings {
scheduleStartTime = LocalTime.now();
}
public void deleteDir() throws IOException {
Files.walkFileTree(stationPath(), new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
Files.delete(file);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
Files.delete(dir);
return FileVisitResult.CONTINUE;
}
});
}
public Path stationPath() {
String configHome = System.getenv("XDG_CONFIG_HOME");
Path directoryPath;
@ -100,51 +76,18 @@ public class StationSettings {
return directoryPath.resolve(stationDirName);
}
public String generateMessage(int generationAttempts) throws MessageGenerationException {
public String generateMessage() {
SecureRandom random = new SecureRandom();
StringBuilder messageBuilder = new StringBuilder();
int attemptsLeft = generationAttempts;
boolean noPrefixCollisions = false;
while (!noPrefixCollisions) {
messageBuilder = new StringBuilder();
for (int i = 0; i < messageLength; i++) {
messageBuilder.append(random.nextInt(10));
}
String message = messageBuilder.toString();
boolean prefixCollision = false;
for (String prefix : prefixes) {
if (message.startsWith(prefix)) {
prefixCollision = true;
}
}
if (!prefixCollision) {
noPrefixCollisions = true;
}
attemptsLeft--;
if (prefixCollision && attemptsLeft <= 0) {
throw new MessageGenerationException("Failed " + generationAttempts + " attempts to generate message without matching prefixes. Ensure prefixes are valid.");
}
}
String messageNoSpaces = messageBuilder.toString();
StringBuilder messageFormattedBuilder = new StringBuilder();
for (int i = 0; i < messageNoSpaces.length(); i++) {
for (int i = 0; i < messageLength; i++) {
if (i > 0 && i % digitsPerGroup == 0) {
messageFormattedBuilder.append(" ");
messageBuilder.append(" ");
}
messageFormattedBuilder.append(messageNoSpaces.charAt(i));
messageBuilder.append(random.nextInt(10));
}
return messageFormattedBuilder.toString();
return messageBuilder.toString();
}
public String getAddress() {
@ -254,10 +197,4 @@ public class StationSettings {
public String toString() {
return name;
}
public class MessageGenerationException extends Exception {
public MessageGenerationException(String message) {
super(message);
}
}
}

View File

@ -10,13 +10,9 @@ import com.tearsofaunicorn.wordpress.api.model.Post;
import com.tearsofaunicorn.wordpress.api.WordpressClient;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.format.DateTimeFormatter;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.Optional;
@ -34,7 +30,6 @@ import javafx.collections.ObservableList;
import javafx.event.Event;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.DatePicker;
@ -79,8 +74,6 @@ public class StationSettingsController {
private StringProperty scheduleStartTime = new SimpleStringProperty();
private StringProperty username = new SimpleStringProperty();
private ArrayList<String> stationNames = new ArrayList<String>();
@FXML
private Label connectionTestStatusLabel;
@ -397,29 +390,6 @@ public class StationSettingsController {
@FXML
private void handleSaveButtonPress(Event e) {
if (stationName.get() == null || stationName.get().equals("")) {
Alert alert = new Alert(Alert.AlertType.WARNING);
alert.setTitle("Invalid station name");
alert.setHeaderText(null);
alert.setContentText("Station name must not be empty");
alert.showAndWait();
return;
}
if (!stationName.get().toLowerCase().equals(settings.getName().toLowerCase())) {
for (String otherStationName : stationNames) {
if (stationName.get().toLowerCase().equals(otherStationName.toLowerCase())) {
Alert alert = new Alert(Alert.AlertType.WARNING);
alert.setTitle("Invalid station name");
alert.setHeaderText(null);
alert.setContentText("Station name '" + stationName.get() + "' is already used.");
alert.showAndWait();
return;
}
}
}
Path oldDirectory = settings.stationPath();
settings.setAddress(stationAddress.get());
settings.setDigitsPerGroup(digitsPerGroup.get());
settings.setExternalProgramCommand(externalProgramCommand.get());
@ -434,14 +404,6 @@ public class StationSettingsController {
settings.setScheduleStartDate(scheduleStartDatePicker.getValue());
settings.setUsername(username.get());
try {
if (!oldDirectory.toString().equals(settings.stationPath().toString())) {
Files.move(oldDirectory, settings.stationPath());
}
} catch (IOException ex) {
logger.log(Level.SEVERE, "Failed to move directory", ex);
}
try {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
LocalTime startTime = LocalTime.parse(scheduleStartTimeField.getText(), formatter);
@ -457,7 +419,6 @@ public class StationSettingsController {
logger.log(Level.SEVERE, "Unsupported OS " + osName);
} else {
logger.log(Level.SEVERE, "Unsupported OS " + osName);
// TODO: Alert
}
Node node = (Node) e.getSource();
@ -522,11 +483,6 @@ public class StationSettingsController {
return stationAddress;
}
public void setStationNameList(List<String> newStationNames) {
stationNames.clear();
stationNames.addAll(newStationNames);
}
public void setStationSettings(StationSettings newSettings) {
settings = newSettings;