11package notepack ;
22
3+ import javafx .scene .input .Clipboard ;
4+ import javafx .scene .input .ClipboardContent ;
35import notepack .NotepadCreateCallback ;
46import java .io .File ;
57import java .io .IOException ;
68import java .net .URL ;
9+ import java .util .Arrays ;
10+ import java .util .Collections ;
11+ import java .util .List ;
712import java .util .ResourceBundle ;
813import java .util .logging .Level ;
914import java .util .logging .Logger ;
@@ -44,14 +49,12 @@ public class NotepadCreateController implements Initializable {
4449
4550 @ FXML
4651 private TextField notepadName ;
47- private TextField directoryPath ;
4852
4953 private NotepadCreateCallback clbk ;
5054 @ FXML
5155 private Button btnCancel ;
5256
5357 private Notepad notepad ;
54- private boolean notepadEdition = false ;
5558 @ FXML
5659 private ToggleButton btnUserColor6 ;
5760 @ FXML
@@ -77,20 +80,17 @@ public class NotepadCreateController implements Initializable {
7780 private AnchorPane engineForm ;
7881
7982 private EngineController currentFormController ;
83+
8084 @ FXML
81- private CheckBox gpgCheckbox ;
82- @ FXML
83- private TextField gpgPublicKeyPath ;
84- @ FXML
85- private Button gpgSelectPublicKey ;
85+ private CheckBox encryptionCheckbox ;
8686 @ FXML
87- private TextField gpgPrivateKeyPath ;
87+ private PasswordField encryptionPassword ;
8888 @ FXML
89- private Button gpgSelectPrivateKey ;
89+ private Button generatePassword ;
9090 @ FXML
91- private Label gpgPublicLabel ;
91+ private Button copyPasswordBtn ;
9292 @ FXML
93- private Label gpgPrivateLabel ;
93+ private Label passwordLabel ;
9494
9595 /**
9696 * Initializes the controller class.
@@ -119,21 +119,15 @@ public void initialize(URL url, ResourceBundle rb) {
119119 }
120120 });
121121
122- gpgCheckbox .selectedProperty ().addListener ((o ) -> {
123- if (gpgCheckbox .isSelected ()) {
124- gpgPrivateLabel .setDisable (false );
125- gpgPublicLabel .setDisable (false );
126- gpgPrivateKeyPath .setDisable (false );
127- gpgPublicKeyPath .setDisable (false );
128- gpgSelectPrivateKey .setDisable (false );
129- gpgSelectPublicKey .setDisable (false );
122+ encryptionCheckbox .selectedProperty ().addListener ((o ) -> {
123+ if (encryptionCheckbox .isSelected ()) {
124+ encryptionPassword .setDisable (false );
125+ generatePassword .setDisable (false );
126+ passwordLabel .setDisable (false );
130127 } else {
131- gpgPrivateLabel .setDisable (true );
132- gpgPublicLabel .setDisable (true );
133- gpgPrivateKeyPath .setDisable (true );
134- gpgPublicKeyPath .setDisable (true );
135- gpgSelectPrivateKey .setDisable (true );
136- gpgSelectPublicKey .setDisable (true );
128+ encryptionPassword .setDisable (true );
129+ generatePassword .setDisable (true );
130+ passwordLabel .setDisable (true );
137131 }
138132 });
139133
@@ -151,8 +145,7 @@ public void initialize(URL url, ResourceBundle rb) {
151145
152146 try {
153147 Parent root = fxmlLoader .load ();
154- // new Theme(new PreferencesSettings()).setCurrent(root.getScene());
155-
148+
156149 currentFormController = (EngineController ) fxmlLoader .getController ();
157150
158151 engineForm .getChildren ().add (root );
@@ -171,7 +164,7 @@ public void setNotepadCreateCallback(NotepadCreateCallback clbk) {
171164
172165 public void setNotepadToEdit (Notepad notepad ) {
173166 this .notepad = notepad ;
174- notepadEdition = true ;
167+ // notepadEdition = true;
175168
176169 notepadName .setText (notepad .getName ());
177170
@@ -188,17 +181,16 @@ public void setNotepadToEdit(Notepad notepad) {
188181 engineSelection .getSelectionModel ().select (0 );
189182 engineSelection .setDisable (true );
190183
191- if (notepad .getParam ("gpg -enabled" ).equals ("1" )) {
192- gpgPrivateKeyPath .setText (notepad .getParam ("gpg-private-key " ));
193- gpgPublicKeyPath . setText ( notepad . getParam ( "gpg-public-key" ) );
194- gpgCheckbox . setSelected ( true );
184+ if (notepad .getParam ("encryption -enabled" ).equals ("1" )) {
185+ encryptionPassword .setText (notepad .getParam ("encryption-password " ));
186+ encryptionCheckbox . setSelected ( true );
187+ copyPasswordBtn . setDisable ( false );
195188 }
196189
197- gpgCheckbox .setDisable (true );
198- gpgPrivateKeyPath .setDisable (true );
199- gpgPublicKeyPath .setDisable (true );
200- gpgSelectPrivateKey .setDisable (true );
201- gpgSelectPublicKey .setDisable (true );
190+ encryptionCheckbox .setDisable (true );
191+ encryptionPassword .setDisable (true );
192+ generatePassword .setDisable (true );
193+ passwordLabel .setDisable (true );
202194
203195 currentFormController .setStorage (notepad .getStorage ());
204196
@@ -248,28 +240,21 @@ private void onAdd(ActionEvent event) {
248240 return ;
249241 }
250242
251- if (gpgCheckbox .isSelected ()) {
252- notepad .setParam ("gpg -enabled" , "1" );
243+ if (encryptionCheckbox .isSelected ()) {
244+ notepad .setParam ("encryption -enabled" , "1" );
253245
254- File privateFile = new File (gpgPrivateKeyPath .getText ());
255- File publicFile = new File (gpgPublicKeyPath .getText ());
246+ String pwd = encryptionPassword .getText ();
256247
257- if (!privateFile .exists ()) {
258- Alert a = new Alert (Alert .AlertType .ERROR , "GPG private key not exists" );
259- a .show ();
260- return ;
261- }
262- if (!publicFile .exists ()) {
263- Alert a = new Alert (Alert .AlertType .ERROR , "GPG public key not exists" );
248+ if (pwd .length () < 5 ) {
249+ Alert a = new Alert (Alert .AlertType .ERROR , "Password must have at least 5 characters" );
264250 a .show ();
265251 return ;
266252 }
267253
268- notepad .setParam ("gpg-private-key" , privateFile .getAbsolutePath ());
269- notepad .setParam ("gpg-public-key" , publicFile .getAbsolutePath ());
254+ notepad .setParam ("encryption-password" , pwd );
270255
271256 } else {
272- notepad .setParam ("gpg -enabled" , "0" );
257+ notepad .setParam ("encryption -enabled" , "0" );
273258 }
274259
275260 String color = (String ) tg .getSelectedToggle ().getUserData ();
@@ -289,57 +274,81 @@ private void onCancel(ActionEvent event) {
289274 stage .close ();
290275 }
291276
292- private void webDavTestConnection (ActionEvent event ) {
293-
294- String url = webDavUrl .getText ();
295- String username = webDavUsername .getText ();
296- String password = webDavPassword .getText ();
297-
298- NoteStorageConfiguration nsc = new NoteStorageConfiguration ();
299- nsc .set ("url" , url );
300- nsc .set ("username" , username );
301- nsc .set ("password" , password );
302-
303- Webdav wd = new Webdav (nsc );
304-
305- try {
306- NoteStorageItem noteItem = wd .getItemsInStorage ();
307- wd .refreshItemsInStorage ();
308- } catch (MessageError ex ) {
309- Alert a = new Alert (Alert .AlertType .ERROR , "WebDAV problem: " + ex .getMessage ());
310- a .showAndWait ();
311- return ;
312- }
313-
314- Alert a = new Alert (Alert .AlertType .INFORMATION , "WebDAV configuration looks good " );
315- a .showAndWait ();
316-
317- }
318-
319277 @ FXML
320- private void onSelectPublicKey (ActionEvent event ) {
321- Stage stage = (Stage ) gpgPublicKeyPath .getScene ().getWindow ();
278+ public void generateRandomPassword (ActionEvent actionEvent ) {
279+ String input = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()-_=+,./<>?;':[]{}" ;
280+ String pwd = "" ;
281+ for (int i = 0 ; i < 20 ; i ++) {
282+ List <String > result = Arrays .asList (input .split ("" ));
283+ Collections .shuffle (result );
284+ pwd = pwd + result .get (0 );
285+ }
286+ encryptionPassword .setText (pwd );
287+ copyPasswordBtn .setDisable (false );
322288
323- FileChooser chooser = new FileChooser ();
324- chooser .setTitle ("Select GPG public key" );
289+ Alert alert = new Alert (Alert .AlertType .INFORMATION );
290+ alert .setTitle ("Information Dialog" );
291+ alert .setHeaderText (null );
292+ alert .setContentText ("Copy and save generated password, this is possible only now!" );
325293
326- File selectedFile = chooser .showOpenDialog (stage );
327- if (selectedFile != null ) {
328- gpgPublicKeyPath .setText (selectedFile .getAbsolutePath ());
329- }
294+ alert .showAndWait ();
330295 }
331296
332297 @ FXML
333- private void onSelectPrivateKey (ActionEvent event ) {
334- Stage stage = (Stage ) gpgPrivateKeyPath .getScene ().getWindow ();
335-
336- FileChooser chooser = new FileChooser ();
337- chooser .setTitle ("Select GPG private key" );
338-
339- File selectedFile = chooser .showOpenDialog (stage );
340- if (selectedFile != null ) {
341- gpgPrivateKeyPath .setText (selectedFile .getAbsolutePath ());
342- }
298+ public void copyPassword (ActionEvent actionEvent ) {
299+ Clipboard clipboard = Clipboard .getSystemClipboard ();
300+ ClipboardContent content = new ClipboardContent ();
301+ content .putString (encryptionPassword .getText ());
302+ clipboard .setContent (content );
343303 }
344304
305+ // private void webDavTestConnection(ActionEvent event) {
306+ //
307+ // String url = webDavUrl.getText();
308+ // String username = webDavUsername.getText();
309+ // String password = webDavPassword.getText();
310+ //
311+ // NoteStorageConfiguration nsc = new NoteStorageConfiguration();
312+ // nsc.set("url", url);
313+ // nsc.set("username", username);
314+ // nsc.set("password", password);
315+ //
316+ // Webdav wd = new Webdav(nsc);
317+ //
318+ // try {
319+ // NoteStorageItem noteItem = wd.getItemsInStorage();
320+ // wd.refreshItemsInStorage();
321+ // } catch (MessageError ex) {
322+ // Alert a = new Alert(Alert.AlertType.ERROR, "WebDAV problem: " + ex.getMessage());
323+ // a.showAndWait();
324+ // return;
325+ // }
326+ //
327+ // Alert a = new Alert(Alert.AlertType.INFORMATION, "WebDAV configuration looks good ");
328+ // a.showAndWait();
329+ //
330+ // }
331+
332+ // private void onSelectPublicKey(ActionEvent event) {
333+ // Stage stage = (Stage) gpgPublicKeyPath.getScene().getWindow();
334+ //
335+ // FileChooser chooser = new FileChooser();
336+ // chooser.setTitle("Select GPG public key");
337+ //
338+ // File selectedFile = chooser.showOpenDialog(stage);
339+ // if (selectedFile != null) {
340+ // gpgPublicKeyPath.setText(selectedFile.getAbsolutePath());
341+ // }
342+ // }
343+ // private void onSelectPrivateKey(ActionEvent event) {
344+ // Stage stage = (Stage) gpgPrivateKeyPath.getScene().getWindow();
345+ //
346+ // FileChooser chooser = new FileChooser();
347+ // chooser.setTitle("Select GPG private key");
348+ //
349+ // File selectedFile = chooser.showOpenDialog(stage);
350+ // if (selectedFile != null) {
351+ // gpgPrivateKeyPath.setText(selectedFile.getAbsolutePath());
352+ // }
353+ // }
345354}
0 commit comments