Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 98 additions & 0 deletions src/main/java/com/CDPrintable/ConfigManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* CDPrintable: A program that prints labels with track listings for your CD cases.
* Copyright (C) 2025 Alexander McLean
*
* This source code is licensed under the GNU General Public License v3.0
* found in the LICENSE file in the root directory of this source tree.
*
* This class reads and writes to the config file for the program.
*/

package com.CDPrintable;

import com.google.gson.*;

import javax.swing.*;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

public class ConfigManager {
private final static String configFilePath;
private final static File file;
private static String json;

static {
configFilePath = System.getProperty("user.home") + "/CDPrintable/config.json";
file = new File(configFilePath);
File parentDir = file.getParentFile();
if (!parentDir.exists()) {
if (!parentDir.mkdirs()) {
JOptionPane.showMessageDialog(null, "Couldn't create the parent directory", "Error", JOptionPane.ERROR_MESSAGE);
}
}
try {
if (file.createNewFile()) {
JOptionPane.showMessageDialog(null, "Config file created. Welcome to CDPrintable!", "Welcome", JOptionPane.INFORMATION_MESSAGE);
Files.writeString(file.toPath(), "{\"userAgentWebAddress\": \"https://github.com/EatSleepProgramRepeat/CDPrintable\"}");
}
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "oopsie poopsies", "Error", JOptionPane.ERROR_MESSAGE);
}
}

/**
* Reads the config file and returns the JSON string.
* @return JSON string from the config file.
*/
public static String getProperty(String key) {
readConfigFile();
try {
JsonElement jsonElement = JsonParser.parseString(json);
JsonObject jsonObject = jsonElement.getAsJsonObject();
return jsonObject.has(key) ? jsonObject.get(key).getAsString() : null;
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Error reading property from config file!", "Error", JOptionPane.ERROR_MESSAGE);
return null;
}
}

/**
* Sets a property in the config file.
* @param key The key to set.
* @param value The value to set.
*/
public static void setProperty(String key, String value) {
readConfigFile();
JsonElement jsonElement = JsonParser.parseString(json);
JsonObject jsonObject = jsonElement.getAsJsonObject();
jsonObject.addProperty(key, value);
writeConfigFile(jsonObject);
}

/**
* Helper method to read a JSON file.
*/
private static void readConfigFile() {
try {
json = Files.readString(file.toPath());
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "Could not read config file!", "Error", JOptionPane.ERROR_MESSAGE);
json = "{}";
}
}

/**
* Helper method to write a JSON file.
*/
public static void writeConfigFile(JsonObject jsonObject) {
Path filePath = Path.of(configFilePath);

try {
Files.writeString(filePath, jsonObject.toString());
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "Could not write to JSON config file!", "Error", JOptionPane.ERROR_MESSAGE);
}
}
}
2 changes: 0 additions & 2 deletions src/main/java/com/CDPrintable/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ public class Constants {
VERSION = tempVersion;
}

public static final boolean USER_AGENT_EMAIL_CHANGED = false;

public static final int MAX_THREADS = 4;
public static final ThreadManager THREAD_MANAGER = new ThreadManager();
}
54 changes: 17 additions & 37 deletions src/main/java/com/CDPrintable/ProgramWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
import com.CDPrintable.MusicBrainzResources.*;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
Expand All @@ -37,7 +35,11 @@ public class ProgramWindow {
* Creates a new ProgramWindow and sets up the GUI.
*/
public ProgramWindow() {
userAgent = new UserAgent("CDPrintable/" + Constants.VERSION, "example@example.com");
String userAgentWebAddress = ConfigManager.getProperty("userAgentWebAddress");
if (userAgentWebAddress == null) {
userAgentWebAddress = "https://github.com/EatSleepProgramRepeat/CDPrintable";
}
userAgent = new UserAgent("CDPrintable/" + Constants.VERSION, userAgentWebAddress);

JFrame frame = new JFrame("CD Printable v"+Constants.VERSION);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Expand Down Expand Up @@ -114,7 +116,7 @@ public void mouseClicked(MouseEvent e) {
// Make the input field
JTextField searchField = new JTextField(15);

// Search type combo box set up
// Search type combo box setup
JComboBox<String> searchTypeComboBox = new JComboBox<>(new String[] {"CDStub", "Artist", "Release"});

// Search button and event listener setup
Expand Down Expand Up @@ -270,41 +272,18 @@ private JPanel settingsPanel() {
userAgentPanel.setBorder(BorderFactory.createTitledBorder("User Agent"));

// Setup user agent text fields, labels, and document listeners
JLabel userAgentLabel = new JLabel("User Agent:");
JLabel userAgentLabel = new JLabel("User Agent (this dosen't save):");
JTextField userAgentField = new JTextField(15);
userAgentField.setText(userAgent.getUserAgent());
userAgentField.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void insertUpdate(DocumentEvent e) {
userAgent.setUserAgent(userAgentField.getText(), fullUserAgentLabel);
}

@Override
public void removeUpdate(DocumentEvent e) {
userAgent.setUserAgent(userAgentField.getText(), fullUserAgentLabel);
}

@Override
public void changedUpdate(DocumentEvent e) {} // Not used
});
userAgentField.addActionListener(_ -> userAgent.setUserAgent(userAgentField.getText(), fullUserAgentLabel));

// Set up the user agent field with labels and document listener.
JLabel userAgentEmailLabel = new JLabel("User Agent Email:");
JTextField userAgentEmailField = new JTextField(15);
userAgentEmailField.setText(userAgent.getUserAgentEmail());
userAgentEmailField.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void insertUpdate(DocumentEvent e) {
userAgent.setUserAgentEmail(userAgentEmailField.getText(), fullUserAgentLabel);
}

@Override
public void removeUpdate(DocumentEvent e) {
userAgent.setUserAgentEmail(userAgentEmailField.getText(), fullUserAgentLabel);
}

@Override
public void changedUpdate(DocumentEvent e) {} // Not used
JLabel userAgentWebAddressLabel = new JLabel("User Agent Web Address:");
JTextField userAgentWebAddressField = new JTextField(15);
userAgentWebAddressField.setText(userAgent.getUserAgentWebAddress());
userAgentWebAddressField.addActionListener(_ -> {
userAgent.setUserAgentWebAddress(userAgentWebAddressField.getText(), fullUserAgentLabel);
ConfigManager.setProperty("userAgentWebAddress", userAgentWebAddressField.getText());
});

fullUserAgentLabel = new JLabel(userAgent.toString());
Expand Down Expand Up @@ -334,11 +313,11 @@ public void changedUpdate(DocumentEvent e) {} // Not used

gbc.gridx = 0;
gbc.gridy = 1;
userAgentInputPanel.add(userAgentEmailLabel, gbc);
userAgentInputPanel.add(userAgentWebAddressLabel, gbc);
fontPanel.add(fontSizeLabel, gbc);

gbc.gridx = 1;
userAgentInputPanel.add(userAgentEmailField, gbc);
userAgentInputPanel.add(userAgentWebAddressField, gbc);
fontPanel.add(fontSizeField, gbc);

userAgentInputPanel.add(fullUserAgentLabel, gbc);
Expand All @@ -347,6 +326,7 @@ public void changedUpdate(DocumentEvent e) {} // Not used
// Add panels to the UA main panel
userAgentPanel.add(fullAgentPanel, BorderLayout.NORTH);
userAgentPanel.add(userAgentInputPanel, BorderLayout.CENTER);
userAgentPanel.add(new JLabel("Press enter to save settings anywhere."), BorderLayout.SOUTH);

// Add subpanels to the main panel
panel.add(userAgentPanel);
Expand Down
31 changes: 11 additions & 20 deletions src/main/java/com/CDPrintable/UserAgent.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

public class UserAgent {
private String userAgent;
private String userAgentEmail;
private String userAgentWebAddress;

/**
* Constructor for UserAgent.
Expand All @@ -23,7 +23,7 @@ public class UserAgent {
*/
public UserAgent(String userAgent, String userAgentEmail) {
this.userAgent = userAgent;
this.userAgentEmail = userAgentEmail;
this.userAgentWebAddress = userAgentEmail;
}

/**
Expand All @@ -32,7 +32,7 @@ public UserAgent(String userAgent, String userAgentEmail) {
*/
@Override
public String toString() {
return userAgent + " (" + userAgentEmail + ")";
return userAgent + " (" + userAgentWebAddress + ")";
}

/**
Expand All @@ -44,11 +44,11 @@ public String getUserAgent() {
}

/**
* Gets the user agent email.
* @return The user agent email.
* Gets the user agent web link
* @return The user agent web link.
*/
public String getUserAgentEmail() {
return userAgentEmail;
public String getUserAgentWebAddress() {
return userAgentWebAddress;
}

/**
Expand All @@ -70,20 +70,11 @@ public void setUserAgent(String userAgent, JLabel fullUserAgentLabel) {
}

/**
* Sets the user agent email.
* @param userAgentEmail The user agent email to set.
* Sets the user agent web address and does some tomfoolery IDK man.
* @param address The user agent web address to set.
*/
public void setUserAgentEmail(String userAgentEmail) {
this.userAgentEmail = userAgentEmail;
}

/**
* Sets the user agent email and updates the full user agent label.
* @param userAgentEmail The user agent email to set.
* @param fullUserAgentLabel The label to update.
*/
public void setUserAgentEmail(String userAgentEmail, JLabel fullUserAgentLabel) {
this.userAgentEmail = userAgentEmail;
public void setUserAgentWebAddress(String address, JLabel fullUserAgentLabel) {
this.userAgentWebAddress = address;
fullUserAgentLabel.setText(toString());
}
}