Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default storage location functionality #1

Merged
merged 2 commits into from
Jan 12, 2024
Merged
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
99 changes: 70 additions & 29 deletions src/main/java/LibGenSearchApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.event.MouseInputAdapter;

import java.util.prefs.Preferences;
public class LibGenSearchApp {

private static JFrame frame;
Expand Down Expand Up @@ -137,6 +137,41 @@ private static void createAndShowGUI() {
JMenu optionsMenu = new JMenu("Options");
JMenuItem setLanguageItem = new JMenuItem("Set Language Code");
setLanguageItem.addActionListener(e -> setLanguageCode());
JMenuItem defaultStorageLocation = new JMenuItem("Set Default Storage Location");

defaultStorageLocation.addActionListener(e -> {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setDialogTitle("Select Download Directory");
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

// Create a custom dialog
JDialog dialog = new JDialog();
dialog.setTitle("Select Download Directory");
try {
// Set custom icon for the dialog
dialog.setIconImage(
ImageIO.read(Thread.currentThread().getContextClassLoader().getResourceAsStream("icon.png")));
} catch (IOException ex) {
ex.printStackTrace();
}

dialog.add(fileChooser);
dialog.pack();
dialog.setLocationRelativeTo(null); // Center the dialog on screen

// Show the dialog and get the user's selection
int userSelection = fileChooser.showOpenDialog(dialog);

if (userSelection == JFileChooser.APPROVE_OPTION) {
downloadDirectory = fileChooser.getSelectedFile().toPath();
System.out.println("Download location set to: " + downloadDirectory);
Preferences prefs = Preferences.userRoot().node("org/serverboi/lgd");
prefs.put("downloadDirectory", downloadDirectory.toString());
} else {
System.out.println("Download directory selection canceled.");
}
});
optionsMenu.add(defaultStorageLocation);

JMenu filterMenu = new JMenu("Filter");
addFilterCheckBox(filterMenu, "Libgen", "l");
Expand Down Expand Up @@ -656,35 +691,41 @@ private void showDownloadStartedAlert(String bookTitle) {
}

private boolean selectDownloadDirectory() {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setDialogTitle("Select Download Directory");
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

// Create a custom dialog
JDialog dialog = new JDialog();
dialog.setTitle("Select Download Directory");
try {
// Set custom icon for the dialog
dialog.setIconImage(
ImageIO.read(Thread.currentThread().getContextClassLoader().getResourceAsStream("icon.png")));
} catch (IOException e) {
e.printStackTrace();
}

dialog.add(fileChooser);
dialog.pack();
dialog.setLocationRelativeTo(null); // Center the dialog on screen

// Show the dialog and get the user's selection
int userSelection = fileChooser.showOpenDialog(dialog);
if (downloadDirectory == null) {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setDialogTitle("Select Download Directory");
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

// Create a custom dialog
JDialog dialog = new JDialog();
dialog.setTitle("Select Download Directory");
try {
// Set custom icon for the dialog
dialog.setIconImage(ImageIO.read(
Thread.currentThread().getContextClassLoader().getResourceAsStream("icon.png")));
} catch (IOException ex) {
ex.printStackTrace();
}

if (userSelection == JFileChooser.APPROVE_OPTION) {
downloadDirectory = fileChooser.getSelectedFile().toPath();
System.out.println("Download location set to: " + downloadDirectory);
return true;
dialog.add(fileChooser);
dialog.pack();
dialog.setLocationRelativeTo(null); // Center the dialog on screen

// Show the dialog and get the user's selection
int userSelection = fileChooser.showOpenDialog(dialog);

if (userSelection == JFileChooser.APPROVE_OPTION) {
downloadDirectory = fileChooser.getSelectedFile().toPath();
System.out.println("Download location set to: " + downloadDirectory);
Preferences prefs = Preferences.userRoot().node("org/serverboi/lgd");
prefs.put("downloadDirectory", downloadDirectory.toString());
return true;
} else {
System.out.println("Download directory selection canceled.");
return false;
}
} else {
System.out.println("Download directory selection canceled.");
return false;
return true;
}
}
}
Expand All @@ -710,7 +751,7 @@ public ImageDetails(String imageUrl, String title, String author, String publish
this.size = size;
this.mirrors = mirrors;
}

//
public String getImageUrl() {
return imageUrl;
}
Expand Down