66import com .codedead .opal .interfaces .TrayIconListener ;
77import com .codedead .opal .utils .FxUtils ;
88import com .codedead .opal .utils .HelpUtils ;
9+ import com .codedead .opal .utils .PasswordCipher ;
910import com .codedead .opal .utils .RunnableFileOpener ;
1011import com .codedead .opal .utils .SharedVariables ;
1112import javafx .application .Platform ;
3435import java .io .File ;
3536import java .io .IOException ;
3637import java .io .InputStreamReader ;
37- import java .net .URISyntaxException ;
3838import java .nio .file .Files ;
3939import java .nio .file .Path ;
40+ import java .security .GeneralSecurityException ;
4041import java .util .*;
4142
4243import static com .codedead .opal .utils .SharedVariables .DEFAULT_LOCALE ;
@@ -197,6 +198,7 @@ public final class MainWindowController implements IAudioTimer, TrayIconListener
197198 private TimerTask countDownTask ;
198199 private final String platformName ;
199200 private final HelpUtils helpUtils ;
201+ private final PasswordCipher passwordCipher ;
200202 private final JsonMapper jsonMapper ;
201203 private final Timer timer ;
202204 private final Timer countDownTimer ;
@@ -215,6 +217,7 @@ public MainWindowController() {
215217
216218 platformName = OsCheck .getOperatingSystemType ().name ();
217219 helpUtils = new HelpUtils ();
220+ passwordCipher = new PasswordCipher ();
218221
219222 this .timer = new Timer ();
220223 this .countDownTimer = new Timer ();
@@ -255,7 +258,7 @@ public void setControllers(final SettingsController settingsController, final Up
255258
256259 logger .info ("Attempting to load the ResourceBundle for locale {}" , languageTag );
257260
258- final Locale locale = Locale . forLanguageTag (languageTag );
261+ final Locale locale = LanguageController . getLocaleFromLanguageTag (languageTag );
259262 resourceFactory .setResources (ResourceBundle .getBundle ("translations.OpalApplication" , locale ));
260263
261264 final boolean mediaButtons = Boolean .parseBoolean (properties .getProperty ("mediaButtons" , "true" ));
@@ -364,6 +367,37 @@ public void loadMediaButtonVisibility(final boolean visible) {
364367 getSoundPanes (grpCustom ).forEach (s -> s .setMediaButton (visible ));
365368 }
366369
370+ /**
371+ * Configure the proxy that should be used when checking for updates
372+ */
373+ private void configureUpdateProxy () {
374+ final Properties properties = settingsController .getProperties ();
375+ final boolean proxyEnabled = Boolean .parseBoolean (properties .getProperty ("updateProxyEnabled" , "false" ));
376+ final boolean authenticationEnabled = proxyEnabled && Boolean .parseBoolean (properties .getProperty ("updateProxyAuthEnabled" , "false" ));
377+ final String proxyPort = properties .getProperty ("updateProxyPort" , "8080" ).trim ();
378+ final int port = proxyPort .isBlank () ? 8080 : Integer .parseInt (proxyPort );
379+
380+ updateController .setProxy (proxyEnabled , properties .getProperty ("updateProxyHost" , "" ), port );
381+ updateController .setProxyAuthentication (
382+ authenticationEnabled ,
383+ properties .getProperty ("updateProxyUsername" , "" ),
384+ authenticationEnabled ? decryptProxyPassword (properties .getProperty ("updateProxyPassword" , "" )) : "" );
385+ }
386+
387+ /**
388+ * Decrypt the stored proxy password
389+ *
390+ * @param storedPassword The stored password
391+ * @return The decrypted password
392+ */
393+ private String decryptProxyPassword (final String storedPassword ) {
394+ try {
395+ return passwordCipher .decrypt (storedPassword );
396+ } catch (final IOException | GeneralSecurityException ex ) {
397+ throw new IllegalArgumentException ("Unable to decrypt the proxy password" , ex );
398+ }
399+ }
400+
367401 /**
368402 * Check for application updates
369403 *
@@ -374,6 +408,8 @@ private void checkForUpdates(final boolean showNoUpdates, final boolean showErro
374408 logger .info ("Attempting to check for updates" );
375409
376410 try {
411+ configureUpdateProxy ();
412+
377413 final Optional <PlatformUpdate > platformUpdate = updateController .checkForUpdates (platformName , SharedVariables .PORTABLE );
378414 if (platformUpdate .isPresent ()) {
379415 final PlatformUpdate update = platformUpdate .get ();
@@ -413,7 +449,7 @@ private void checkForUpdates(final boolean showNoUpdates, final boolean showErro
413449 FxUtils .showInformationAlert (resourceFactory .getResources ().getString ("NoUpdateAvailable" ), getClass ().getResourceAsStream (SharedVariables .ICON_URL ));
414450 }
415451 }
416- } catch (final InterruptedException | IOException | InvalidHttpResponseCodeException | URISyntaxException ex ) {
452+ } catch (final InterruptedException | IOException | InvalidHttpResponseCodeException | IllegalArgumentException ex ) {
417453 if (ex instanceof InterruptedException )
418454 Thread .currentThread ().interrupt ();
419455
@@ -461,6 +497,33 @@ private List<SoundPane> getSoundPanes(final GridPane parent) {
461497 return elements ;
462498 }
463499
500+ /**
501+ * Filter the {@link SoundPane} objects in a {@link GridPane} based on a search query
502+ *
503+ * @param gridPane The {@link GridPane} containing the {@link SoundPane} objects
504+ * @param query The trimmed, lower-case query to filter on
505+ */
506+ private void filterSoundPanes (final GridPane gridPane , final String query ) {
507+ getSoundPanes (gridPane ).forEach (e -> {
508+ e .setVisible (e .getName ().toLowerCase ().contains (query ));
509+ e .setManaged (e .isVisible ());
510+ });
511+ }
512+
513+ /**
514+ * Update the visibility of a {@link TitledPane} based on whether any of its {@link SoundPane} children are visible
515+ *
516+ * @param titledPane The {@link TitledPane} to update
517+ * @param gridPane The {@link GridPane} containing the {@link SoundPane} objects
518+ */
519+ private void updateTitledPaneVisibility (final TitledPane titledPane , final GridPane gridPane ) {
520+ final boolean hasVisibleSoundPane = getSoundPanes (gridPane ).stream ().anyMatch (Node ::isVisible );
521+
522+ titledPane .setExpanded (hasVisibleSoundPane );
523+ titledPane .setVisible (hasVisibleSoundPane );
524+ titledPane .setManaged (hasVisibleSoundPane );
525+ }
526+
464527 /**
465528 * Open a file on the local filesystem
466529 *
@@ -559,86 +622,25 @@ private void initialize() {
559622 }
560623
561624 Platform .runLater (() -> {
562- getAllSoundPanes ()
563- .forEach (e -> {
564- e .setVisible (e .getName ().toLowerCase ().contains (newValue .trim ().toLowerCase ()));
565- e .setManaged (e .isVisible ());
566- });
625+ final String query = newValue .trim ().toLowerCase ();
567626
568- getSoundPanes (grpCustom )
569- .forEach (e -> {
570- e .setVisible (e .getName ().toLowerCase ().contains (newValue .trim ().toLowerCase ()));
571- e .setManaged (e .isVisible ());
572- });
627+ filterSoundPanes (grpNature , query );
628+ filterSoundPanes (grpOffice , query );
629+ filterSoundPanes (grpAudiences , query );
630+ filterSoundPanes (grpRadioFrequencyStatic , query );
631+ filterSoundPanes (grpOther , query );
632+ filterSoundPanes (grpCustom , query );
573633
574- // Check if there are still active sound panes on pneCustom
634+ // Hide the TitledPanes that no longer contain any visible SoundPane
575635 final List <SoundPane > customSoundPanes = getSoundPanes (grpCustom );
576636 if (!customSoundPanes .isEmpty ()) {
577- customSoundPanes .stream ().filter (Node ::isVisible ).findFirst ().ifPresentOrElse (_ -> {
578- pneCustom .setExpanded (true );
579- pneCustom .setVisible (true );
580- pneCustom .setManaged (true );
581- }, () -> {
582- pneCustom .setExpanded (false );
583- pneCustom .setVisible (false );
584- pneCustom .setManaged (false );
585- });
637+ updateTitledPaneVisibility (pneCustom , grpCustom );
586638 }
587-
588- // Check if there are still active sound panes on pneNature
589- getSoundPanes (grpNature ).stream ().filter (Node ::isVisible ).findFirst ().ifPresentOrElse (_ -> {
590- pneNature .setExpanded (true );
591- pneNature .setVisible (true );
592- pneNature .setManaged (true );
593- }, () -> {
594- pneNature .setExpanded (false );
595- pneNature .setVisible (false );
596- pneNature .setManaged (false );
597- });
598-
599- // Check if there are still active sound panes on pneOffice
600- getSoundPanes (grpOffice ).stream ().filter (Node ::isVisible ).findFirst ().ifPresentOrElse (_ -> {
601- pneOffice .setExpanded (true );
602- pneOffice .setVisible (true );
603- pneOffice .setManaged (true );
604- }, () -> {
605- pneOffice .setExpanded (false );
606- pneOffice .setVisible (false );
607- pneOffice .setManaged (false );
608- });
609-
610- // Check if there are still active sound panes on pneAudiences
611- getSoundPanes (grpAudiences ).stream ().filter (Node ::isVisible ).findFirst ().ifPresentOrElse (_ -> {
612- pneAudiences .setExpanded (true );
613- pneAudiences .setVisible (true );
614- pneAudiences .setManaged (true );
615- }, () -> {
616- pneAudiences .setExpanded (false );
617- pneAudiences .setVisible (false );
618- pneAudiences .setManaged (false );
619- });
620-
621- // Check if there are still active sound panes on pneRadioFrequencyStatic
622- getSoundPanes (grpRadioFrequencyStatic ).stream ().filter (Node ::isVisible ).findFirst ().ifPresentOrElse (_ -> {
623- pneRadioFrequencyStatic .setExpanded (true );
624- pneRadioFrequencyStatic .setVisible (true );
625- pneRadioFrequencyStatic .setManaged (true );
626- }, () -> {
627- pneRadioFrequencyStatic .setExpanded (false );
628- pneRadioFrequencyStatic .setVisible (false );
629- pneRadioFrequencyStatic .setManaged (false );
630- });
631-
632- // Check if there are still active sound panes on pneOther
633- getSoundPanes (grpOther ).stream ().filter (Node ::isVisible ).findFirst ().ifPresentOrElse (_ -> {
634- pneOther .setExpanded (true );
635- pneOther .setVisible (true );
636- pneOther .setManaged (true );
637- }, () -> {
638- pneOther .setExpanded (false );
639- pneOther .setVisible (false );
640- pneOther .setManaged (false );
641- });
639+ updateTitledPaneVisibility (pneNature , grpNature );
640+ updateTitledPaneVisibility (pneOffice , grpOffice );
641+ updateTitledPaneVisibility (pneAudiences , grpAudiences );
642+ updateTitledPaneVisibility (pneRadioFrequencyStatic , grpRadioFrequencyStatic );
643+ updateTitledPaneVisibility (pneOther , grpOther );
642644
643645 btnClearSearch .setVisible (true );
644646 btnClearSearch .setManaged (true );
@@ -1019,16 +1021,17 @@ public void fired() {
10191021 try {
10201022 final ProcessBuilder p = new ProcessBuilder (command .split (" " ));
10211023
1022- try (final BufferedReader reader = new BufferedReader (new InputStreamReader (p .start ().getInputStream ()))) {
1023- final StringBuilder builder = new StringBuilder ();
1024- String line ;
1025- while ((line = reader .readLine ()) != null ) {
1026- builder .append (line );
1027- builder .append (System .lineSeparator ());
1024+ try (final Process process = p .start ()) {
1025+ try (final BufferedReader reader = new BufferedReader (new InputStreamReader (process .getInputStream ()))) {
1026+ final StringBuilder output = new StringBuilder ();
1027+ String line ;
1028+ while ((line = reader .readLine ()) != null ) {
1029+ output .append (line ).append (System .lineSeparator ());
1030+ }
1031+ logger .info ("Shutdown command result: {}" , output .toString ());
10281032 }
1029- final String result = builder .toString ();
1030- logger .info ("Shutdown command result: {}" , result );
10311033 }
1034+
10321035 exitAction ();
10331036 } catch (final IOException ex ) {
10341037 logger .error ("Unable to execute shutdown command" , ex );
0 commit comments