11package io .beanvortex .bitkip .controllers ;
22
33import io .beanvortex .bitkip .config .AppConfigs ;
4+ import io .beanvortex .bitkip .models .Credentials ;
45import io .beanvortex .bitkip .utils .FxUtils ;
56import io .beanvortex .bitkip .config .observers .QueueObserver ;
67import io .beanvortex .bitkip .config .observers .QueueSubject ;
3435public class BatchDownload implements QueueObserver {
3536
3637 @ FXML
37- private Label errorLabel ;
38+ private PasswordField passwordField ;
3839 @ FXML
39- private Button questionBtnUri , checkBtn , cancelBtn , questionBtnChunks , openLocation , newQueue ;
40+ private Label errorLabel ;
4041 @ FXML
41- private TextField startField , locationField , endField ;
42+ private Button questionBtnUri , refreshBtn , checkBtn , cancelBtn , questionBtnChunks , openLocation , newQueue ;
4243 @ FXML
4344 private ComboBox <QueueModel > queueCombo ;
4445 @ FXML
45- private TextField chunksField , urlField ;
46+ private TextField chunksField , urlField , usernameField , startField , locationField , endField ;
4647 @ FXML
47- private CheckBox lastLocationCheck ;
48+ private CheckBox lastLocationCheck , authorizedCheck ;
4849
4950 private LinkModel tempLink ;
5051 private Stage stage ;
@@ -77,6 +78,12 @@ public void initialize(URL location, ResourceBundle resources) {
7778 queueCombo .setValue (queues .get (0 ));
7879 errorLabel .setVisible (false );
7980 checkBtn .setDisable (true );
81+ usernameField .getParent ().setManaged (false );
82+ usernameField .getParent ().setVisible (false );
83+ passwordField .getParent ().setManaged (false );
84+ passwordField .getParent ().setVisible (false );
85+ refreshBtn .setGraphic (new FontIcon ());
86+ refreshBtn .setOnAction (e -> autoFillLocation ());
8087 if (AppConfigs .lastSavedDir == null )
8188 lastLocationCheck .setDisable (true );
8289 Validations .prepareLinkFromClipboard (urlField );
@@ -96,20 +103,20 @@ public void initialize(URL location, ResourceBundle resources) {
96103 endField .textProperty ().addListener (o -> autoFillLocation ());
97104 urlField .textProperty ().addListener ((o , ol , n ) -> {
98105 if (n .isBlank ())
99- DownloadUtils .disableControlsAndShowError ("URL is blank" , errorLabel , checkBtn , null , null );
106+ DownloadUtils .disableControlsAndShowError ("URL is blank" , errorLabel , checkBtn , null );
100107 else autoFillLocation ();
101108 });
102109 locationField .textProperty ().addListener ((o , ol , n ) -> {
103110 if (n .isBlank ())
104- DownloadUtils .disableControlsAndShowError ("Location is blank" , errorLabel , checkBtn , null , null );
111+ DownloadUtils .disableControlsAndShowError ("Location is blank" , errorLabel , checkBtn , null );
105112 else onOfflineFieldsChanged ();
106113 });
107114 }
108115
109116 private void onOfflineFieldsChanged () {
110117 if (tempLink != null )
111118 handleError (() -> DownloadUtils .onOfflineFieldsChanged (locationField , tempLink .getName (),
112- null , queueCombo , null , checkBtn , openLocation , null , lastLocationCheck ), errorLabel );
119+ null , queueCombo , null , checkBtn , openLocation , lastLocationCheck ), errorLabel );
113120
114121 }
115122
@@ -122,26 +129,27 @@ private void autoFillLocation() {
122129 var links = generateLinks (url , start , end , Integer .parseInt (chunksField .getText ()), true );
123130 var link = links .get (0 );
124131 tempLink = link ;
125- var connection = DownloadUtils .connect (link .getUri ());
132+ var credential = new Credentials (usernameField .getText (), passwordField .getText ());
133+ var connection = DownloadUtils .connect (link .getUri (), credential );
126134 var fileNameLocationFuture = CompletableFuture .supplyAsync (() -> DownloadUtils .extractFileName (link .getUri (), connection ))
127135 .thenAccept (this ::setLocation );
128136 fileNameLocationFuture
129137 .whenComplete ((unused , throwable ) ->
130138 handleError (() -> DownloadUtils .checkIfFileIsOKToSave (locationField .getText (),
131- tempLink .getName (), null , checkBtn , null , lastLocationCheck ), errorLabel ))
139+ tempLink .getName (), null , checkBtn , lastLocationCheck ), errorLabel ))
132140 .exceptionally (throwable -> {
133141 var errorMsg = throwable .getCause ().getLocalizedMessage ();
134142 Platform .runLater (() ->
135143 DownloadUtils .disableControlsAndShowError (errorMsg , errorLabel ,
136- checkBtn , null , null ));
144+ checkBtn , null ));
137145 return null ;
138146 });
139147 } catch (NumberFormatException ignore ) {
140148 } catch (Exception e ) {
141149 var errorMsg = e .getLocalizedMessage ();
142150 if (e instanceof IndexOutOfBoundsException )
143151 errorMsg = "No URLs found" ;
144- DownloadUtils .disableControlsAndShowError (errorMsg , errorLabel , checkBtn , null , null );
152+ DownloadUtils .disableControlsAndShowError (errorMsg , errorLabel , checkBtn , null );
145153 }
146154 }
147155
@@ -231,7 +239,7 @@ private void onSelectLocation(ActionEvent e) {
231239 locationField .setText (path );
232240 if (tempLink != null )
233241 handleError (() -> DownloadUtils .checkIfFileIsOKToSave (locationField .getText (),
234- tempLink .getName (), null , checkBtn , null , lastLocationCheck ), errorLabel );
242+ tempLink .getName (), null , checkBtn , lastLocationCheck ), errorLabel );
235243 }
236244
237245 @ FXML
@@ -244,12 +252,12 @@ private void onCheck() {
244252 var finalPath = path ;
245253 if (url .isBlank ()) {
246254 log .warn ("URL is blank" );
247- DownloadUtils .disableControlsAndShowError ("URL is blank" , errorLabel , checkBtn , null , null );
255+ DownloadUtils .disableControlsAndShowError ("URL is blank" , errorLabel , checkBtn , null );
248256 return ;
249257 }
250258 if (path .isBlank ()) {
251259 log .warn ("Location is blank" );
252- DownloadUtils .disableControlsAndShowError ("Location is blank" , errorLabel , checkBtn , null , null );
260+ DownloadUtils .disableControlsAndShowError ("Location is blank" , errorLabel , checkBtn , null );
253261 return ;
254262 }
255263 var start = Integer .parseInt (startField .getText ());
@@ -270,7 +278,7 @@ private void onCheck() {
270278 var msg = "At least one URL exists for this location. Change location or change start, end.\n "
271279 + found .get ().getUri ();
272280 log .warn (msg );
273- DownloadUtils .disableControlsAndShowError (msg , errorLabel , checkBtn , null , null );
281+ DownloadUtils .disableControlsAndShowError (msg , errorLabel , checkBtn , null );
274282 return ;
275283 }
276284 }
@@ -286,14 +294,17 @@ private void onCheck() {
286294 lm .setPath (finalPath );
287295 lm .setSelectedPath (finalPath );
288296 });
289- FxUtils .newBatchListStage (links );
297+ Credentials credentials = null ;
298+ if (!usernameField .getText ().isBlank () && !passwordField .getText ().isBlank ())
299+ credentials = new Credentials (usernameField .getText (), passwordField .getText ());
300+ FxUtils .newBatchListStage (links , credentials );
290301 getQueueSubject ().removeObserver (this );
291302 stage .close ();
292303 } catch (IllegalArgumentException e ) {
293304 if (e instanceof NumberFormatException )
294305 return ;
295306 log .error (e .getLocalizedMessage ());
296- DownloadUtils .disableControlsAndShowError (e .getLocalizedMessage (), errorLabel , checkBtn , null , null );
307+ DownloadUtils .disableControlsAndShowError (e .getLocalizedMessage (), errorLabel , checkBtn , null );
297308 }
298309 }
299310
@@ -351,6 +362,16 @@ private void onLastLocationCheck() {
351362 else
352363 setLocation (tempLink .getName ());
353364 }
365+
366+ @ FXML
367+ private void onAuthorizedCheck () {
368+ usernameField .getParent ().setManaged (authorizedCheck .isSelected ());
369+ usernameField .getParent ().setVisible (authorizedCheck .isSelected ());
370+ passwordField .getParent ().setManaged (authorizedCheck .isSelected ());
371+ passwordField .getParent ().setVisible (authorizedCheck .isSelected ());
372+ usernameField .setText ("" );
373+ passwordField .setText ("" );
374+ }
354375}
355376
356377
0 commit comments