Skip to content

Commit 6a6ecd0

Browse files
committed
Updated the way the output dir is handled internally
No longer a global variable, wow! This change means that the "open output directory" button will always open the appropriate directory based on your current settings and simplifies output dir validation.
1 parent ed90dd5 commit 6a6ecd0

File tree

9 files changed

+878
-870
lines changed

9 files changed

+878
-870
lines changed

NUSGet.py

Lines changed: 56 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ def load_title_data(self, selected_title: TitleData):
357357
self.ui.archive_file_entry.setText(archive_name)
358358
danger_text = selected_title.danger
359359
# Add warning text to the log if the selected title has no ticket.
360-
if selected_title.ticket is False:
360+
if not selected_title.ticket:
361361
danger_text = danger_text + ("Note: This Title does not have a Ticket available, so it cannot be decrypted"
362362
" or packed into a WAD/TAD.")
363363
# Print log info about the selected title and version.
@@ -422,24 +422,10 @@ def download_btn_pressed(self):
422422
"like the download to be saved."))
423423
msg_box.exec()
424424
return
425+
out_path = self.get_output_dir()
426+
if out_path is None:
427+
return
425428
self.lock_ui()
426-
# Check for a custom output directory, and ensure that it's valid. If it is, then use that.
427-
if self.ui.custom_out_dir_checkbox.isChecked() and self.ui.custom_out_dir_entry.text() != "":
428-
out_path = pathlib.Path(self.ui.custom_out_dir_entry.text())
429-
if not out_path.exists() or not out_path.is_dir():
430-
msg_box = QMessageBox()
431-
msg_box.setIcon(QMessageBox.Icon.Critical)
432-
msg_box.setStandardButtons(QMessageBox.StandardButton.Ok)
433-
msg_box.setDefaultButton(QMessageBox.StandardButton.Ok)
434-
msg_box.setWindowTitle(app.translate("MainWindow", "Invalid Download Directory"))
435-
msg_box.setText(app.translate("MainWindow", "The specified download directory does not exist!"))
436-
msg_box.setInformativeText(app.translate("MainWindow",
437-
"Please make sure the specified download directory exists,"
438-
" and that you have permission to access it."))
439-
msg_box.exec()
440-
return
441-
else:
442-
out_path = out_folder
443429
# Create a new worker object to handle the download in a new thread.
444430
if self.ui.console_select_dropdown.currentText() == "DSi":
445431
worker = Worker(run_nus_download_dsi, out_path, self.ui.tid_entry.text(),
@@ -615,8 +601,11 @@ def script_btn_pressed(self):
615601
archive_name = ""
616602
break
617603
titles.append(BatchTitleData(tid, title_version, console, archive_name))
604+
out_path = self.get_output_dir()
605+
if out_path is None:
606+
return
618607
self.lock_ui()
619-
worker = Worker(run_nus_download_batch, out_folder, titles, self.ui.pack_archive_checkbox.isChecked(),
608+
worker = Worker(run_nus_download_batch, out_path, titles, self.ui.pack_archive_checkbox.isChecked(),
620609
self.ui.keep_enc_checkbox.isChecked(), self.ui.create_dec_checkbox.isChecked(),
621610
self.ui.use_wiiu_nus_checkbox.isChecked(), self.ui.use_local_checkbox.isChecked(),
622611
self.ui.pack_vwii_mode_checkbox.isChecked(), self.ui.patch_ios_checkbox.isChecked())
@@ -626,13 +615,16 @@ def script_btn_pressed(self):
626615

627616
def open_output_dir(self):
628617
# Like all good things in life, this is a platform-dependent procedure. Did I say good? I meant annoying.
618+
out_path = self.get_output_dir()
619+
if out_path is None:
620+
return
629621
system = platform.system()
630622
if system == "Windows":
631-
subprocess.run(["explorer.exe", out_folder])
623+
subprocess.run(["explorer.exe", out_path])
632624
elif system == "Darwin":
633-
subprocess.run(["open", out_folder])
625+
subprocess.run(["open", out_path])
634626
else:
635-
subprocess.run(["xdg-open", out_folder])
627+
subprocess.run(["xdg-open", out_path])
636628

637629
def choose_output_dir(self):
638630
# Use this handy convenience method to prompt the user to select a directory. Then we just need to validate
@@ -641,20 +633,11 @@ def choose_output_dir(self):
641633
"", QFileDialog.ShowDirsOnly | QFileDialog.DontResolveSymlinks)
642634
if selected_dir == "":
643635
return
644-
out_path = pathlib.Path(selected_dir)
645-
if not out_path.exists() or not out_path.is_dir():
646-
msg_box = QMessageBox()
647-
msg_box.setIcon(QMessageBox.Icon.Critical)
648-
msg_box.setStandardButtons(QMessageBox.StandardButton.Ok)
649-
msg_box.setDefaultButton(QMessageBox.StandardButton.Ok)
650-
msg_box.setWindowTitle(app.translate("MainWindow", "Invalid Download Directory"))
651-
msg_box.setText(app.translate("MainWindow", "<b>The specified download directory does not exist!</b>"))
652-
msg_box.setInformativeText(app.translate("MainWindow",
653-
"Please make sure the download directory you want to use exists, and "
654-
"that you have permission to access it."))
655-
msg_box.exec()
636+
# Write it to the box and use existing validation code. Efficiency!
637+
self.ui.custom_out_dir_entry.setText(selected_dir)
638+
out_path = self.get_output_dir()
639+
if out_path is None:
656640
return
657-
self.ui.custom_out_dir_entry.setText(str(out_path))
658641
config_data["out_path"] = str(out_path.absolute())
659642
save_config(config_data)
660643

@@ -670,6 +653,44 @@ def custom_output_dir_changed(self):
670653
config_data["out_path"] = str(out_path.absolute())
671654
save_config(config_data)
672655

656+
def get_output_dir(self) -> pathlib.Path | None:
657+
# Whether a custom download directory is set.
658+
if self.ui.custom_out_dir_checkbox.isChecked() and self.ui.custom_out_dir_entry.text() != "":
659+
# Check for a custom output directory, and ensure that it's valid. If it is, then use that. Otherwise,
660+
# return None and let the calling code determine how to continue.
661+
out_path = pathlib.Path(self.ui.custom_out_dir_entry.text())
662+
if not out_path.exists() or not out_path.is_dir():
663+
msg_box = QMessageBox()
664+
msg_box.setIcon(QMessageBox.Icon.Critical)
665+
msg_box.setStandardButtons(QMessageBox.StandardButton.Ok)
666+
msg_box.setDefaultButton(QMessageBox.StandardButton.Ok)
667+
msg_box.setWindowTitle(app.translate("MainWindow", "Invalid Download Directory"))
668+
msg_box.setText(app.translate("MainWindow", "<b>The specified download directory does not exist!</b>"))
669+
msg_box.setInformativeText(app.translate("MainWindow",
670+
"Please make sure the specified download directory exists,"
671+
" and that you have permission to access it."))
672+
msg_box.exec()
673+
return None
674+
return out_path
675+
else:
676+
# Default path if there's no custom download directory configured. Yay for platform differences!
677+
if os.name == 'nt':
678+
# This code is required because on Windows, the name of the downloads directory is localized based on your
679+
# system's language. This means that literally "Downloads" isn't always going to exist, so we want to ask
680+
# the registry what it's named on this particular machine.
681+
import winreg
682+
sub_key = r'SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders'
683+
downloads_guid = '{374DE290-123F-4565-9164-39C4925E467B}'
684+
with winreg.OpenKey(winreg.HKEY_CURRENT_USER, sub_key) as key:
685+
location = pathlib.Path(winreg.QueryValueEx(key, downloads_guid)[0])
686+
else:
687+
location = pathlib.Path(os.path.expanduser('~')).joinpath('Downloads')
688+
# Build the path by combining the path to the Downloads photo with "NUSGet".
689+
out_folder = location.joinpath("NUSGet Downloads")
690+
# Create the "NUSGet Downloads" directory if it doesn't exist.
691+
out_folder.mkdir(exist_ok=True, parents=True)
692+
return out_folder
693+
673694
def about_nusget(self):
674695
about_box = AboutNUSGet([nusget_version, version("libWiiPy"), version("libTWLPy")])
675696
about_box.exec()
@@ -704,23 +725,6 @@ def change_theme(self, new_theme):
704725
vwii_database = json.load(database_file)
705726
database_file = open(os.path.join(os.path.dirname(__file__), "data", "dsi-database.json"), encoding="utf-8")
706727
dsi_database = json.load(database_file)
707-
# Load the user's Downloads directory, which of course requires different steps on Windows vs macOS/Linux.
708-
if os.name == 'nt':
709-
import winreg
710-
sub_key = r'SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders'
711-
downloads_guid = '{374DE290-123F-4565-9164-39C4925E467B}'
712-
with winreg.OpenKey(winreg.HKEY_CURRENT_USER, sub_key) as key:
713-
location = pathlib.Path(winreg.QueryValueEx(key, downloads_guid)[0])
714-
else:
715-
# Silence a false linter warning about redeclaration, since this is actually only ever assigned once.
716-
# noinspection PyRedeclaration
717-
location = pathlib.Path(os.path.expanduser('~')).joinpath('Downloads')
718-
# Build the path by combining the path to the Downloads photo with "NUSGet".
719-
out_folder = location.joinpath("NUSGet Downloads")
720-
# Create the "NUSGet Downloads" directory if it doesn't exist. In the future, this will be user-customizable, but
721-
# this works for now, and avoids using a directory next to the binary (mostly an issue on macOS/Linux).
722-
if not out_folder.is_dir():
723-
out_folder.mkdir()
724728

725729
# Load the config path and then the configuration data, if it exists. If not, then we should initialize it and write
726730
# it out.

0 commit comments

Comments
 (0)