forked from firm1/zest-writer
/
MenuController.java
1010 lines (894 loc) · 40.5 KB
/
MenuController.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package com.zestedesavoir.zestwriter.view;
import java.io.File;
import java.io.IOException;
import java.net.CookieHandler;
import java.net.CookieManager;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.Scanner;
import org.apache.commons.lang.StringEscapeUtils;
import org.codehaus.jackson.map.ObjectMapper;
import org.python.core.PyString;
import org.python.util.PythonInterpreter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.zeroturnaround.zip.ZipUtil;
import com.zestedesavoir.zestwriter.MainApp;
import com.zestedesavoir.zestwriter.model.ExtractFile;
import com.zestedesavoir.zestwriter.model.MetadataContent;
import com.zestedesavoir.zestwriter.utils.Corrector;
import com.zestedesavoir.zestwriter.utils.ZdsHttp;
import com.zestedesavoir.zestwriter.utils.readability.Readability;
import com.zestedesavoir.zestwriter.view.dialogs.FindDialog;
import java.util.logging.Level;
import org.fxmisc.richtext.CodeArea;
import javafx.application.Platform;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.concurrent.Service;
import javafx.concurrent.Task;
import javafx.concurrent.Worker;
import javafx.concurrent.Worker.State;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonBar.ButtonData;
import javafx.scene.control.ButtonType;
import javafx.scene.control.ChoiceDialog;
import javafx.scene.control.Dialog;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.MenuItem;
import javafx.scene.control.PasswordField;
import javafx.scene.control.ProgressBar;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.SplitPane;
import javafx.scene.control.Tab;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.control.TreeItem;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.text.Text;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.DirectoryChooser;
import javafx.stage.Stage;
import javafx.util.Pair;
public class MenuController {
private MainApp mainApp;
TextArea textArea;
@FXML
MenuItem menuDownload;
@FXML
MenuItem menuUpload;
@FXML
MenuItem menuLogin;
@FXML
MenuItem menuLogout;
@FXML
MenuItem menuReport;
@FXML
MenuItem menuLisibility;
@FXML
MenuItem menuGoogle;
@FXML
HBox hBottomBox;
final ProgressBar pb = new ProgressBar(0);
final Text labelField = new Text("");
private final Logger logger;
public MenuController() {
super();
logger = LoggerFactory.getLogger(MenuController.class);
}
public void setMainApp(MainApp mainApp) {
this.mainApp = mainApp;
}
@FXML
private void HandleQuitButtonAction(ActionEvent event) {
System.exit(0);
}
private List<ExtractFile> getExtractFilesFromTree(TreeItem<ExtractFile> node) {
List<ExtractFile> extractFiles = new ArrayList<>();
for (TreeItem<ExtractFile> child : node.getChildren()) {
if (child.getChildren().isEmpty()) {
extractFiles.add(child.getValue());
}
else {
extractFiles.addAll(getExtractFilesFromTree(child));
}
}
return extractFiles;
}
private void correctChildren(TreeItem<ExtractFile> root, boolean typo) throws IOException {
List<ExtractFile> myExtracts = getExtractFilesFromTree(root);
for(ExtractFile extract:myExtracts) {
String markdown = "";
// load mdText
Path path = Paths.get(extract.getFilePath());
Scanner scanner;
StringBuilder bfString = new StringBuilder();
try {
scanner = new Scanner(path, StandardCharsets.UTF_8.name());
while (scanner.hasNextLine()) {
bfString.append(scanner.nextLine());
bfString.append("\n");
}
markdown = bfString.toString();
} catch (IOException e) {
logger.error("", e);
}
Corrector cr = new Corrector();
if (!typo) {
cr.ignoreRule("FRENCH_WHITESPACE");
}
String htmlText = StringEscapeUtils.unescapeHtml(markdownToHtml(mainApp.getIndex(), markdown));
textArea.appendText(cr.checkHtmlContentToText(htmlText, extract.getTitle().getValue()));
}
}
public Map<ExtractFile, Double> getGunning(TreeItem<ExtractFile> root) {
Map<ExtractFile, Double> map = new HashMap<>();
for (TreeItem<ExtractFile> child : root.getChildren()) {
String markdown = "";
if (child.getChildren().isEmpty()) {
// load mdText
Path path = Paths.get(child.getValue().getFilePath());
Scanner scanner;
StringBuilder bfString = new StringBuilder();
try {
scanner = new Scanner(path, StandardCharsets.UTF_8.name());
while (scanner.hasNextLine()) {
bfString.append(scanner.nextLine());
bfString.append("\n");
}
markdown = bfString.toString();
} catch (IOException e) {
// TODO Auto-generated catch block
logger.error("", e);
}
String htmlText = StringEscapeUtils.unescapeHtml(markdownToHtml(mainApp.getIndex(), markdown));
String plainText = Corrector.HtmlToTextWithoutCode(htmlText);
Readability rd = new Readability(plainText);
map.put(child.getValue(), rd.getGunningFog());
} else {
map.putAll(getGunning(child));
}
}
return map;
}
public Map<ExtractFile, Double> getFlesch(TreeItem<ExtractFile> root) {
Map<ExtractFile, Double> map = new HashMap<>();
for (TreeItem<ExtractFile> child : root.getChildren()) {
String markdown = "";
if (child.getChildren().isEmpty()) {
// load mdText
Path path = Paths.get(child.getValue().getFilePath());
Scanner scanner;
StringBuilder bfString = new StringBuilder();
try {
scanner = new Scanner(path, StandardCharsets.UTF_8.name());
while (scanner.hasNextLine()) {
bfString.append(scanner.nextLine());
bfString.append("\n");
}
markdown = bfString.toString();
} catch (IOException e) {
// TODO Auto-generated catch block
logger.error("", e);
}
String htmlText = StringEscapeUtils.unescapeHtml(markdownToHtml(mainApp.getIndex(), markdown));
String plainText = Corrector.HtmlToTextWithoutCode(htmlText);
Readability rd = new Readability(plainText);
map.put(child.getValue(), rd.getFleschReadingEase());
} else {
map.putAll(getGunning(child));
}
}
return map;
}
public String markdownToHtml(MdTextController index, String chaine) {
PythonInterpreter console = index.getPyconsole();
console.set("text", chaine);
console.exec("render = Markdown(extensions=(ZdsExtension({'inline': False, 'emoticons': smileys}),),safe_mode = 'escape', enable_attributes = False, tab_length = 4, output_format = 'html5', smart_emphasis = True, lazy_ol = True).convert(text)");
PyString render = console.get("render", PyString.class);
return render.toString();
}
@FXML
private void HandleFleshButtonAction(ActionEvent event) {
Map<ExtractFile, Double> results = getFlesch(mainApp.getIndex().getSummary().getRoot());
ObservableList<String> rows = FXCollections.observableArrayList();
for (Entry<ExtractFile, Double> entry : results.entrySet()) {
String easy;
if (entry.getValue() < 30) {
easy = "très difficile";
} else if (entry.getValue() < 50) {
easy = "difficile";
} else if (entry.getValue() < 60) {
easy = "assez difficile";
} else if (entry.getValue() < 70) {
easy = "standard";
} else if (entry.getValue() < 80) {
easy = "assez facile";
} else {
easy = "très facile";
}
String v1 = entry.getKey().getTitle().getValue();
String v2 = entry.getValue().toString() + " (" + easy + ")";
rows.add(v1 + " => " + v2);
}
// Create the custom dialog.
Dialog<Pair<String, String>> dialog = new Dialog<>();
dialog.setTitle("Indice de Flesch");
dialog.setHeaderText("Indice de Rudolph Flesch");
// Set the button types.
dialog.getDialogPane().getButtonTypes().addAll(ButtonType.OK, ButtonType.CANCEL);
ListView<String> list = new ListView<>();
list.setPrefSize(800, 500);
list.setItems(rows);
dialog.getDialogPane().setContent(list);
dialog.setResultConverter(dialogButton -> {
if (dialogButton == ButtonType.OK) {
return null;
}
return null;
});
Optional<Pair<String, String>> result = dialog.showAndWait();
}
@FXML
private void HandleGunningButtonAction(ActionEvent event) {
Map<ExtractFile, Double> results = getGunning(mainApp.getIndex().getSummary().getRoot());
ObservableList<String> rows = FXCollections.observableArrayList();
for (Entry<ExtractFile, Double> entry : results.entrySet()) {
String easy;
if (entry.getValue() >= 15) {
easy = "très difficile";
} else if (entry.getValue() >= 12) {
easy = "difficile";
} else if (entry.getValue() >= 10) {
easy = "assez difficile";
} else if (entry.getValue() >= 8) {
easy = "standard";
} else if (entry.getValue() >= 6) {
easy = "assez facile";
} else {
easy = "très facile";
}
String v1 = entry.getKey().getTitle().getValue();
String v2 = entry.getValue().toString() + " (" + easy + ")";
rows.add(v1 + " => " + v2);
}
// Create the custom dialog.
Dialog<Pair<String, String>> dialog = new Dialog<>();
dialog.setTitle("Indice de Gunning");
dialog.setHeaderText("Indice de Gunning");
// Set the button types.
dialog.getDialogPane().getButtonTypes().addAll(ButtonType.OK, ButtonType.CANCEL);
ListView<String> list = new ListView<>();
list.setPrefSize(800, 500);
list.setItems(rows);
dialog.getDialogPane().setContent(list);
dialog.setResultConverter(dialogButton -> {
if (dialogButton == ButtonType.OK) {
return null;
}
return null;
});
Optional<Pair<String, String>> result = dialog.showAndWait();
}
@FXML
private void HandleReportWithoutTypoButtonAction(ActionEvent event) {
textArea = new TextArea();
textArea.setEditable(true);
textArea.setWrapText(true);
textArea.setMaxWidth(Double.MAX_VALUE);
textArea.setMaxHeight(Double.MAX_VALUE);
GridPane.setVgrow(textArea, Priority.ALWAYS);
GridPane.setHgrow(textArea, Priority.ALWAYS);
GridPane expContent = new GridPane();
expContent.setMaxWidth(Double.MAX_VALUE);
expContent.add(new Label("Rapport de correction"), 0, 0);
expContent.add(textArea, 0, 1);
hBottomBox.getChildren().addAll(labelField);
List<ExtractFile> myExtracts = getExtractFilesFromTree(mainApp.getIndex().getSummary().getRoot());
MdTextController mdText = mainApp.getIndex();
Corrector corrector = new Corrector();
corrector.ignoreRule("FRENCH_WHITESPACE");
Service<String> correctTask = new Service<String>() {
@Override
protected Task<String> createTask() {
return new Task<String>() {
@Override
protected String call(){
updateMessage("Préparation du rapport de validation ...");
StringBuilder resultCorrect = new StringBuilder();
for(ExtractFile extract:myExtracts) {
updateMessage("Préparation du rapport de validation de "+extract.getTitle().getValue());
String markdown = "";
// load mdText
Path path = Paths.get(extract.getFilePath());
Scanner scanner;
StringBuilder bfString = new StringBuilder();
try {
scanner = new Scanner(path, StandardCharsets.UTF_8.name());
while (scanner.hasNextLine()) {
bfString.append(scanner.nextLine());
bfString.append("\n");
}
markdown = bfString.toString();
} catch (IOException e) {
logger.error("", e);
}
String htmlText = StringEscapeUtils.unescapeHtml(markdownToHtml(mdText, markdown));
String note = corrector.checkHtmlContentToText(htmlText, extract.getTitle().getValue());
resultCorrect.append(note);
}
return resultCorrect.toString();
}
};
}
};
labelField.textProperty().bind(correctTask.messageProperty());
textArea.textProperty().bind(correctTask.valueProperty());
correctTask.stateProperty().addListener((ObservableValue<? extends Worker.State> observableValue, Worker.State oldValue, Worker.State newValue) -> {
Alert alert;
switch (newValue) {
case FAILED:
alert = new Alert(AlertType.ERROR);
alert.setTitle("Validation du contenu");
alert.setHeaderText("Erreur de validation");
alert.setContentText("Désolé une erreur nous empêche de trouver les erreurs dans votre contenu");
alert.showAndWait();
break;
case CANCELLED:
case SUCCEEDED:
alert = new Alert(AlertType.ERROR);
alert.setTitle("Validation du contenu");
alert.setHeaderText("Rapport de validation du contenu prêt à être copié sur ZdS");
// Set expandable Exception into the dialog pane.
alert.getDialogPane().setExpandableContent(expContent);
alert.showAndWait();
break;
}
hBottomBox.getChildren().clear();
});
correctTask.start();
}
@FXML
private void HandleNewButtonAction(ActionEvent event) {
File defaultDirectory;
if(mainApp.getConfig().getWorkspaceFactory() == null) {
defaultDirectory = new File(System.getProperty("user.home"));
} else {
defaultDirectory = new File(mainApp.getZdsutils().getOfflineContentPathDir());
}
Map<String,Object> paramContent= mainApp.getIndex().createContentDialog(null);
if(paramContent != null) {
// find inexistant directory
String localPath = defaultDirectory.getAbsolutePath()+File.separator+ZdsHttp.toSlug((String)paramContent.get("title"));
String realLocalPath = localPath;
File folder = new File(realLocalPath);
int i=1;
while(folder.exists()) {
realLocalPath = localPath+"-"+i;
folder = new File(realLocalPath);
i++;
}
// create directory
folder.mkdir();
// create manifest.json
File manifest = new File(realLocalPath+File.separator+"manifest.json");
ObjectMapper mapper = new ObjectMapper();
paramContent.put("slug", ZdsHttp.toSlug((String)paramContent.get("title")));
paramContent.put("version", 2);
paramContent.put("object", "container");
paramContent.put("introduction", "introduction.md");
paramContent.put("conclusion", "conclusion.md");
paramContent.put("children", new ArrayList<>());
try {
mapper.writeValue(manifest, paramContent);
// create introduction and conclusion
File intro = new File(realLocalPath+File.separator+"introduction.md");
File conclu = new File(realLocalPath+File.separator+"conclusion.md");
intro.createNewFile();
conclu.createNewFile();
mainApp.getIndex().openContent(folder.getAbsolutePath());
mainApp.getContents().put("dir", folder.getAbsolutePath());
} catch (IOException e) {
// TODO Auto-generated catch block
logger.error("", e);
}
menuUpload.setDisable(false);
menuLisibility.setDisable(false);
menuReport.setDisable(false);
}
}
@FXML
private void HandleOpenButtonAction(ActionEvent event) {
DirectoryChooser chooser = new DirectoryChooser();
chooser.setTitle("Contenus Zestueux");
File defaultDirectory;
if(mainApp.getConfig().getWorkspaceFactory() == null) {
defaultDirectory = new File(System.getProperty("user.home"));
} else {
defaultDirectory = new File(mainApp.getZdsutils().getOfflineContentPathDir());
}
chooser.setInitialDirectory(defaultDirectory);
File selectedDirectory = chooser.showDialog(mainApp.getPrimaryStage());
if (selectedDirectory != null) {
mainApp.getContents().put("dir", selectedDirectory.getAbsolutePath());
menuUpload.setDisable(false);
menuLisibility.setDisable(false);
menuReport.setDisable(false);
}
}
@FXML
private void HandleLogoutButtonAction(ActionEvent event) {
if (mainApp.getZdsutils().isAuthenticated()) {
mainApp.getZdsutils().logout();
menuDownload.setDisable(true);
menuLogin.setDisable(false);
menuLogout.setDisable(true);
}
}
@FXML
private Service<Void> HandleLoginButtonAction(ActionEvent event) {
// Create the custom dialog.
Dialog<Pair<String, String>> dialog = new Dialog<>();
dialog.setTitle("Connexion");
dialog.setHeaderText("Connectez vous au site Zeste de Savoir");
// Set the icon (must be included in the project).
dialog.setGraphic(new ImageView(this.getClass().getResource("static/icons/login.png").toString()));
// Set the button types.
ButtonType loginButtonType = new ButtonType("Se connecter", ButtonData.OK_DONE);
dialog.getDialogPane().getButtonTypes().addAll(loginButtonType, ButtonType.CANCEL);
// Button for google
Button googleAuth = new Button("Connexion via Google", MdTextController.createGoogleIcon());
googleAuth.setOnAction(t-> {
HandleGoogleAction(dialog);
});
// Create the username and password labels and fields.
GridPane grid = new GridPane();
grid.setHgap(20);
grid.setVgap(20);
grid.setPadding(new Insets(20, 10, 10, 10));
TextField username = new TextField();
username.setPromptText("username");
PasswordField password = new PasswordField();
password.setPromptText("password");
grid.add(googleAuth, 0, 0, 1, 2);
grid.add(new Label("Nom d'utilisateur:"), 1, 0);
grid.add(username, 2, 0);
grid.add(new Label("Mot de passe:"), 1, 1);
grid.add(password, 2, 1);
// Enable/Disable login button depending on whether a username was entered.
Node loginButton = dialog.getDialogPane().lookupButton(loginButtonType);
loginButton.setDisable(true);
username.textProperty().addListener((observable, oldValue, newValue) -> {
loginButton.setDisable(newValue.trim().isEmpty());
});
dialog.getDialogPane().setContent(grid);
// Request focus on the username field by default.
Platform.runLater(username::requestFocus);
// Convert the result to a username-password-pair when the login button is clicked.
dialog.setResultConverter(dialogButton -> {
if (dialogButton == loginButtonType) {
return new Pair<>(username.getText(), password.getText());
}
return null;
});
Optional<Pair<String, String>> result = dialog.showAndWait();
hBottomBox.getChildren().addAll(labelField);
Service<Void> loginTask = new Service<Void>() {
@Override
protected Task<Void> createTask() {
return new Task<Void>() {
@Override
protected Void call() {
result.ifPresent(usernamePassword -> {
try {
updateMessage("Connexion au site en cours ...");
if(mainApp.getZdsutils().login(usernamePassword.getKey(), usernamePassword.getValue())) {
updateMessage("Recherche des contenus ...");
mainApp.getZdsutils().initInfoOnlineContent("tutorial");
mainApp.getZdsutils().initInfoOnlineContent("article");
}
else {
cancel();
}
} catch (Exception e) {
cancel();
}
});
if(!result.isPresent()) {
if(mainApp.getZdsutils().isAuthenticated()) {
updateMessage("Recherche des contenus ...");
try {
mainApp.getZdsutils().initInfoOnlineContent("tutorial");
mainApp.getZdsutils().initInfoOnlineContent("article");
} catch (IOException e) {
logger.error("", e);
}
} else {
cancel();
}
}
return null;
}
};
}
};
labelField.textProperty().bind(loginTask.messageProperty());
return loginTask;
}
private void downloadContents() {
prerequisitesForData();
hBottomBox.getChildren().clear();
hBottomBox.getChildren().addAll(pb, labelField);
Service<Void> downloadContentTask = new Service<Void>() {
@Override
protected Task<Void> createTask() {
return new Task<Void>() {
@Override
protected Void call() throws Exception {
int max = mainApp.getZdsutils().getContentListOnline().size();
int iterations = 0;
if (mainApp.getZdsutils().isAuthenticated()) {
for (MetadataContent meta : mainApp.getZdsutils().getContentListOnline()) {
updateMessage("Téléchargement : " + meta.getSlug());
updateProgress(iterations, max);
mainApp.getZdsutils().downloaDraft(meta.getId(), meta.getType());
iterations++;
}
iterations = 0;
for (MetadataContent meta : mainApp.getZdsutils().getContentListOnline()) {
updateMessage("Décompression : " + meta.getSlug());
updateProgress(iterations, max);
mainApp.getZdsutils().unzipOnlineContent(mainApp.getZdsutils().getOnlineContentPathDir() + File.separator + meta.getSlug() + ".zip");
iterations++;
}
updateMessage("Terminé");
updateProgress(iterations, max);
}
return null;
}
};
}
};
labelField.textProperty().bind(downloadContentTask.messageProperty());
pb.progressProperty().bind(downloadContentTask.progressProperty());
downloadContentTask.stateProperty().addListener((ObservableValue<? extends Worker.State> observableValue, Worker.State oldValue, Worker.State newValue) -> {
switch (newValue) {
case FAILED:
case CANCELLED:
case SUCCEEDED:
Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle("Téléchargement des contenus");
alert.setHeaderText("Confirmation du téléchargement");
alert.setContentText("Vos contenus (tutoriels et articles) de ZdS ont été téléchargés en local. \n" +
"Vous pouvez maintenant travailler en mode Hors ligne !");
alert.showAndWait();
hBottomBox.getChildren().clear();
break;
}
});
downloadContentTask.start();
}
@FXML
private void HandleDownloadButtonAction(ActionEvent event) {
if (!mainApp.getZdsutils().isAuthenticated()) {
Service<Void> loginTask = HandleLoginButtonAction(event);
loginTask.stateProperty().addListener((ObservableValue<? extends Worker.State> observableValue, Worker.State oldValue, Worker.State newValue) -> {
Alert alert;
switch (newValue) {
case FAILED:
case CANCELLED:
hBottomBox.getChildren().clear();
alert = new Alert(AlertType.ERROR);
alert.setTitle("Connexion");
alert.setHeaderText("Erreur de connexion");
alert.setContentText("Désolé mais vous n'avez pas été authentifié sur le serveur de Zeste de Savoir.");
alert.showAndWait();
break;
case SUCCEEDED:
if (mainApp.getContents().containsKey("dir")) {
menuUpload.setDisable(false);
}
downloadContents();
break;
}
});
loginTask.start();
} else {
downloadContents();
}
}
private void prerequisitesForData() {
if(mainApp.getConfig().getWorkspaceFactory() == null) {
try {
mainApp.getConfig().loadWorkspace();
} catch (IOException e) {
// TODO Auto-generated catch block
logger.error("", e);
}
}
}
private void uploadContents() {
prerequisitesForData();
hBottomBox.getChildren().clear();
hBottomBox.getChildren().addAll(labelField);
List<MetadataContent> contents = new ArrayList<>();
contents.add(new MetadataContent(null, "*** Nouveau Contenu ***", null));
contents.addAll(mainApp.getZdsutils().getContentListOnline());
ChoiceDialog<MetadataContent> dialog = new ChoiceDialog<>(null, contents);
dialog.setTitle("Choix du tutoriel");
dialog.setHeaderText("Choisissez le tutoriel vers lequel importer");
dialog.setContentText("Tutoriel: ");
Optional<MetadataContent> result = dialog.showAndWait();
Service<Void> uploadContentTask = new Service<Void>() {
@Override
protected Task<Void> createTask() {
return new Task<Void>() {
@Override
protected Void call() throws Exception {
if (mainApp.getZdsutils().isAuthenticated()) {
String targetId = result.get().getId();
String localSlug = mainApp.getZdsutils().getLocalSlug();
String targetSlug = result.get().getSlug();
try {
String pathDir = mainApp.getZdsutils().getOfflineContentPathDir() + File.separator + localSlug;
updateMessage("Compression : "+targetSlug+" en cours ...");
ZipUtil.pack(new File(pathDir), new File(pathDir + ".zip"));
updateMessage("Import : "+targetSlug+" en cours ...");
if(targetId == null) {
if(!mainApp.getZdsutils().importNewContent(pathDir+ ".zip")) {
failed();
};
} else {
if(!mainApp.getZdsutils().importContent(pathDir + ".zip", targetId, targetSlug)) {
failed();
};
}
} catch (IOException e) {
logger.error("", e);
}
}
return null;
}
};
}
};
labelField.textProperty().bind(uploadContentTask.messageProperty());
uploadContentTask.stateProperty().addListener((ObservableValue<? extends Worker.State> observableValue, Worker.State oldValue, Worker.State newValue) -> {
Alert alert;
switch (newValue) {
case FAILED:
alert = new Alert(AlertType.ERROR);
alert.setTitle("Import de contenu");
alert.setHeaderText("Erreur d'import");
alert.setContentText("Désolé mais un problème vous empêche d'importer votre contenu sur ZdS");
alert.showAndWait();
break;
case CANCELLED:
break;
case SUCCEEDED:
alert = new Alert(AlertType.INFORMATION);
alert.setTitle("Import de contenu");
alert.setHeaderText("Confirmation de l'import");
alert.setContentText("Votre contenu à été importé sur ZdS avec succès !");
alert.showAndWait();
hBottomBox.getChildren().clear();
break;
}
});
if (result.isPresent()) {
uploadContentTask.start();
}
}
@FXML
private void HandleUploadButtonAction(ActionEvent event) {
if (!mainApp.getZdsutils().isAuthenticated()) {
Service<Void> loginTask = HandleLoginButtonAction(event);
loginTask.stateProperty().addListener((ObservableValue<? extends Worker.State> observableValue, Worker.State oldValue, Worker.State newValue) -> {
Alert alert;
switch (newValue) {
case FAILED:
break;
case CANCELLED:
hBottomBox.getChildren().clear();
alert = new Alert(AlertType.ERROR);
alert.setTitle("Connexion");
alert.setHeaderText("Erreur de connexion");
alert.setContentText("Désolé mais vous n'avez pas été authentifié sur le serveur de Zeste de Savoir.");
alert.showAndWait();
break;
case SUCCEEDED:
if (mainApp.getContents().containsKey("dir")) {
menuUpload.setDisable(false);
}
uploadContents();
break;
}
});
loginTask.start();
} else {
uploadContents();
}
}
@FXML
private void HandleSwitchWorkspaceAction(ActionEvent event) throws IOException {
DirectoryChooser fileChooser = new DirectoryChooser();
fileChooser.setTitle("Sélectionnez un dossier");
File selectedDirectory = fileChooser.showDialog(mainApp.getPrimaryStage());
mainApp.getConfig().setWorkspacePath(selectedDirectory.getAbsolutePath());
mainApp.getConfig().loadWorkspace();
Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle("Dossier de travail");
alert.setHeaderText("Changement de dossier de travail");
alert.setContentText("Votre dossier de travail est maintenant dans "+mainApp.getConfig().getWorkspacePath());
alert.setResizable(true);
alert.showAndWait();
}
@FXML
private void HandleSimpleFind(ActionEvent event){
// TODO: create a method somewhere more apropriate
String currentFileText = "";
Tab currentTab = null;
for(Entry<ExtractFile, Tab> entry: mainApp.getExtracts().entrySet()){
if(entry.getValue().isSelected()){
currentFileText = entry.getKey().getMdText().get();
currentTab = entry.getValue();
}
}
if(currentTab == null){
return;
}
FindDialog dialog = new FindDialog(currentFileText);
dialog.show();
SplitPane editorPane = (SplitPane)currentTab.getContent();
BorderPane editorNode = (BorderPane) editorPane.getItems().get(0);
CodeArea editorAera = (CodeArea) editorNode.getCenter();
for(Integer coords : dialog.getResult()){
}
}
@FXML
private void HandleGlobalFind(ActionEvent event){
}
@FXML
private void HandleSimpleReplace(ActionEvent event){
}
@FXML
private void HandleGlobalReplace(ActionEvent event){
}
private void HandleGoogleAction(Dialog parent) {
Dialog<Pair<String, String>> dialog = new Dialog<>();
dialog.setTitle("Authentification via Google");
final WebView browser = new WebView();
final WebEngine webEngine = browser.getEngine();
ScrollPane scrollPane = new ScrollPane();
scrollPane.setContent(browser);
CookieManager manager = new CookieManager();
CookieHandler.setDefault(manager);
webEngine.setJavaScriptEnabled(false);
dialog.getDialogPane().setContent(scrollPane);
dialog.getDialogPane().getButtonTypes().addAll(ButtonType.CANCEL);
webEngine.getLoadWorker().stateProperty().addListener(new ChangeListener<State>() {
@Override public void changed(ObservableValue ov, State oldState, State newState) {
if(newState == Worker.State.RUNNING) {
if(webEngine.getLocation().contains("accounts.google.com/ServiceLoginAuth")) {
scrollPane.setVisible(false);
}
}
if(newState == Worker.State.SUCCEEDED) {
if(webEngine.getLocation().equals("https://zestedesavoir.com/")) {
Element elementary = webEngine.getDocument().getDocumentElement();
Element logbox = getLogBox(elementary);
String pseudo = getPseudo(logbox);
String id = getId(logbox);
mainApp.getZdsutils().authToGoogle(manager.getCookieStore().getCookies(), pseudo, id);
dialog.close();
parent.close();
} else {
if(webEngine.getLocation().contains("accounts.google.com/ServiceLoginAuth")) {
scrollPane.setVisible(true);
}
}
}
}
});
webEngine.load("https://zestedesavoir.com/login/google-oauth2/");
dialog.show();
}
private Element getLogBox(Element el) {
NodeList childNodes = el.getChildNodes();
if(el.getNodeName().equals("DIV")){
String attr = el.getAttribute("class");
if(attr != null) {
if(attr.contains("my-account-dropdown")) {
return el;
}
}
}
for(int i=0; i<childNodes.getLength(); i++){
org.w3c.dom.Node item = childNodes.item(i);
if(item instanceof Element){
Element res = getLogBox((Element)item);
if(res != null) return res;
}
}
return null;
}
private String getPseudo(Element logbox){
NodeList childNodes = logbox.getChildNodes();
for(int i=0; i<childNodes.getLength(); i++){
org.w3c.dom.Node item = childNodes.item(i);
if(item instanceof Element){
Element find = ((Element)item);
if(find.getNodeName().equals("SPAN")) {
return find.getTextContent();
};
}
}
return null;
}
private String getId(Element logbox){
NodeList childNodes = logbox.getChildNodes();
for(int i=0; i<childNodes.getLength(); i++){
org.w3c.dom.Node item = childNodes.item(i);
if(item instanceof Element){
Element ulItem = ((Element)item);
if(ulItem.getNodeName().equals("UL")) {
NodeList childUlNodes = ulItem.getChildNodes();
for(int j=0; j<childUlNodes.getLength(); j++){
org.w3c.dom.Node jtem = childUlNodes.item(j);
if(jtem instanceof Element){
Element liItem = ((Element)jtem);
if(liItem.getNodeName().equals("LI")) {
NodeList childIlNodes = liItem.getChildNodes();
for(int k=0; k<childIlNodes.getLength(); k++){
org.w3c.dom.Node ktem = childIlNodes.item(k);
if(ktem instanceof Element){
Element aItem = ((Element)ktem);
//System.out.println("BALISE : "+aItem.getNodeName());
if(aItem.getNodeName().equals("A")) {
String ref = aItem.getAttribute("href").toString();
if(ref.startsWith("/contenus/tutoriels")) {
String[] splt = ref.split("/");
if(splt.length >= 4) {
return splt[3];
}
else {
return null;
}
}
}
}
}
}