-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatCommon.java
More file actions
805 lines (713 loc) · 31.4 KB
/
ChatCommon.java
File metadata and controls
805 lines (713 loc) · 31.4 KB
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
package atChat;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import javafx.application.Platform;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TreeItem;
import javafx.scene.image.Image;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Pane;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
public class ChatCommon {
public static final String REQ_PASSWORD = "REQPASSWORD";
public static final String NO_PASSWORD = "NOPASSWORD";
public static final String ACCEPTED = "ACCEPTED";
public static final String REFUSED = "REFUSED";
private static final String LINE_BREAK = "%%!%%!!";
public static final String BEGIN_SYSTEM = "%%BEGINSYSTEM%%";
public static final String END_SYSTEM = "%%ENDSYSTEM%%";
public static final String SEPARATOR = "|";
public static final String SPLIT_SEPARATOR = "\\|";
public static final String NEWCLIENT = "NEWCLIENT";
public static final String CONFIRM = "CONFIRMED";
public static final String DISCONNECT = "DISCONNECT";
public static final String TRANSFER_FILES = "TRANSFERFILES";
public static final String TRANSFER_DIRECTORY = "TRANSFERDIRECTORY";
public static final String ACCEPT_TRANSFER = "R_ACCEPTTRANSFER";
public static final String DOWNLOAD_R_CANCELLED = "R_CANCELLED";
public static final String DOWNLOAD_R_REFUSED = "R_REFUSED";
public static final String DOWNLOAD_R_TIMEDOUT = "R_TIMEDOUT";
public static final String DOWNLOAD_R_ERROR = "R_ERROR";
public static final String DOWNLOAD_S_CANCELLED = "S_CANCELLED";
public static final String DOWNLOAD_S_REFUSED = "S_REFUSED";
public static final String DOWNLOAD_COMPLETE = "COMPLETE";
public static final String DOWNLOAD_S_TIMEDOUT = "S_TIMEDOUT";
public static final String DOWNLOAD_S_ERROR = "S_ERROR";
public static final String DOWNLOAD_SETFILE = "SETFILE";
public static final String senderHandleStyle = "-fx-font-family: Arial; -fx-text-fill: rgb(32, 186, 255); -fx-font-size: 13px;";
public static final String localHandleStyle = "-fx-font-family: Arial; -fx-text-fill: rgb(255, 216, 23); -fx-font-size: 13px;";
public static final String msgStyle = "-fx-font-family: Arial; -fx-text-fill: rgb(93%, 93%, 93%); -fx-font-size: 12px;";
public static final String msgHoverStyle = "-fx-font-family: Arial; -fx-text-fill: rgb(186, 255, 32); -fx-font-size: 12px;";
public static final String systemSenderStyle = "-fx-font-family: Arial; -fx-text-fill: rgb(255, 175, 38); -fx-font-size: 13px;";
public static final String systemStyleInfo = "-fx-font-family: Arial; -fx-text-fill: rgb(93%, 93%, 93%); -fx-font-size: 12px;";
public static final String systemStyleWarning = "-fx-font-family: Arial; -fx-text-fill: rgb(93%, 93%, 93%); -fx-font-size: 12px;";
public static final String systemStyleSevere = "-fx-font-family: Arial; -fx-text-fill: rgb(255, 73, 11); -fx-font-size: 12px;";
private static Clip incommingClip;
private static Clip outgoingClip;
private static Clip systemClip;
private static ChatMain mMain;
protected static void setMain(ChatMain main) {
mMain = main;
}
public static String formatSystemMessage(String msg) {
msg = BEGIN_SYSTEM + msg + END_SYSTEM;
return msg;
}
public static String unFormatSystemMessage(String msg) {
msg = msg.substring(BEGIN_SYSTEM.length(), msg.length() - END_SYSTEM.length());
return msg;
}
public static boolean isSystemMessage(String msg) {
if (msg.startsWith(BEGIN_SYSTEM) && msg.endsWith(END_SYSTEM) && msg.length() > BEGIN_SYSTEM.length() + END_SYSTEM.length()) {
return true;
} else {
return false;
}
}
public static String formatMessage(String msg) {
msg = msg.replaceAll("\n", LINE_BREAK);
return msg;
}
public static String unFormatMessage(String msg) {
msg = msg.replaceAll(LINE_BREAK, Matcher.quoteReplacement("\n"));
return msg;
}
public static String formatFilePath(String path) {
String newpath = path.replace('/', File.separatorChar);
newpath = newpath.replace('\\', File.separatorChar);
if (newpath.indexOf(":") == 1 && newpath.indexOf(File.separatorChar) == 2) {
newpath = (newpath.length() > 2) ? newpath.substring(2) : "Root" + File.separator;
}
if (newpath.indexOf(File.separatorChar) == 0) {
if (newpath.length() > 2) {
newpath = newpath.substring(1);
} else if (newpath.length() > 1) {
if (newpath.charAt(1) == File.separatorChar) {
newpath = "Root" + File.separator;
} else {
newpath = newpath.substring(1);
}
} else {
newpath = "Root" + File.separator;
}
}
return newpath;
}
public static String formatFileSize(long size) {
double kb = size / 1024d;
double mb = kb / 1024d;
double gb = mb / 1024d;
double tb = gb / 1024d;
if (tb >= 1) {
return Double.toString(Math.round(tb * 100d) / 100d) + " TB";
} else if (gb >= 1) {
return Double.toString(Math.round(gb * 100d) / 100d) + " GB";
} else if (mb >= 1) {
return Double.toString(Math.round(mb * 100d) / 100d) + " MB";
} else if (kb >= 1) {
return Double.toString(Math.round(kb * 100d) / 100d) + " KB";
} else {
return Long.toString(Math.round(size)) + " Bytes";
}
}
protected static void centerWindow(Stage window, Pane root, Stage centerStage) {
GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
int width = gd.getDisplayMode().getWidth();
int height = gd.getDisplayMode().getHeight();
double x = ((centerStage.getWidth() / 2) + centerStage.getX()) - (root.getPrefWidth() / 2);
double y = ((centerStage.getHeight() / 2) + centerStage.getY()) - (root.getPrefHeight() / 2);
if (x < 0) {
x = 0;
} else if (x + root.getPrefWidth() > width) {
x = width - root.getPrefWidth();
}
if (y < 0) {
y = 0;
} else if (y + root.getPrefHeight() > height) {
y = height - root.getPrefHeight();
}
window.setX(x);
window.setY(y);
}
protected static void popMessage(String title, String msg, boolean runLater) {
if (!runLater) {
try {
final Stage dialogWin = new Stage(StageStyle.DECORATED);
FXMLLoader loader = new FXMLLoader(Main.class.getResource("/atChat/resources/fxml/atMessageWindow.fxml"));
AnchorPane msgRoot = (AnchorPane) loader.load();
MessageWindow diagCont = (MessageWindow) loader.getController();
diagCont.setMessage(msg, dialogWin);
dialogWin.getIcons().addAll(new Image("/atChat/resources/images/@Chat_Icon_16_32bit.png"), new Image("/atChat/resources/images/@Chat_Icon_24_32bit.png"), new Image("/atChat/resources/images/@Chat_Icon_32_32bit.png"), new Image("/atChat/resources/images/@Chat_Icon_48_32bit.png"), new Image("/atChat/resources/images/@Chat_Icon_64_32bit.png"));
//dialogWin.setResizable(false);
dialogWin.setTitle(title);
Scene scene = new Scene(msgRoot);
scene.getStylesheets().clear();
scene.getStylesheets().add(Main.class.getResource("/atChat/resources/fxml/atChatTheme.css").toExternalForm());
dialogWin.setScene(scene);
centerWindow(dialogWin, msgRoot, mMain.getStage());
dialogWin.initOwner(mMain.getStage());
dialogWin.initModality(Modality.APPLICATION_MODAL);
dialogWin.show();
double tmpW = scene.getWidth();
double tmpH = scene.getHeight();
double tmpX = dialogWin.getX();
double tmpY = dialogWin.getY();
dialogWin.setResizable(false);
if (tmpW != scene.getWidth()) {
dialogWin.setWidth(dialogWin.getWidth() - (scene.getWidth() - tmpW));
dialogWin.setX(tmpX);
}
if (tmpH != scene.getHeight()) {
dialogWin.setHeight(dialogWin.getHeight() - (scene.getHeight() - tmpH));
dialogWin.setY(tmpY);
}
} catch (IOException e) {
System.out.println("Unable to create message window:" + System.getProperty("line.separator") + e.getMessage());
Logger.getLogger(Main.class.getName()).log(Level.WARNING, "Unable to create message window:" + System.getProperty("line.separator") + e.getMessage(), e);
}
} else {
Platform.runLater(new ChatCommon().new PopMessage(title, msg));
}
}
protected static void popError(String error, boolean runLater) {
if (!runLater) {
try {
final Stage dialogWin = new Stage(StageStyle.DECORATED);
FXMLLoader loader = new FXMLLoader(Main.class.getResource("/atChat/resources/fxml/atMessageWindow.fxml"));
AnchorPane msgRoot = (AnchorPane) loader.load();
MessageWindow diagCont = (MessageWindow) loader.getController();
diagCont.setMessage(error, dialogWin);
dialogWin.getIcons().addAll(new Image("/atChat/resources/images/@Chat_Icon_16_32bit.png"), new Image("/atChat/resources/images/@Chat_Icon_24_32bit.png"), new Image("/atChat/resources/images/@Chat_Icon_32_32bit.png"), new Image("/atChat/resources/images/@Chat_Icon_48_32bit.png"), new Image("/atChat/resources/images/@Chat_Icon_64_32bit.png"));
//dialogWin.setResizable(false);
dialogWin.setTitle("Error");
Scene scene = new Scene(msgRoot);
scene.getStylesheets().clear();
scene.getStylesheets().add(Main.class.getResource("/atChat/resources/fxml/atChatTheme.css").toExternalForm());
dialogWin.setScene(scene);
centerWindow(dialogWin, msgRoot, mMain.getStage());
dialogWin.initOwner(mMain.getStage());
dialogWin.initModality(Modality.APPLICATION_MODAL);
dialogWin.show();
double tmpW = scene.getWidth();
double tmpH = scene.getHeight();
double tmpX = dialogWin.getX();
double tmpY = dialogWin.getY();
dialogWin.setResizable(false);
if (tmpW != scene.getWidth()) {
dialogWin.setWidth(dialogWin.getWidth() - (scene.getWidth() - tmpW));
dialogWin.setX(tmpX);
}
if (tmpH != scene.getHeight()) {
dialogWin.setHeight(dialogWin.getHeight() - (scene.getHeight() - tmpH));
dialogWin.setY(tmpY);
}
} catch (IOException e) {
System.out.println("Unable to create message window:" + System.getProperty("line.separator") + e.getMessage());
Logger.getLogger(Main.class.getName()).log(Level.WARNING, "Unable to create message window:" + System.getProperty("line.separator") + e.getMessage(), e);
}
} else {
Platform.runLater(new ChatCommon().new PopMessage("Error", error));
}
}
private class PopMessage implements Runnable {
private final String msg;
private final String title;
public PopMessage(String title, String msg) {
this.msg = msg;
this.title = title;
}
@Override
public void run() {
try {
final Stage dialogWin = new Stage(StageStyle.DECORATED);
FXMLLoader loader = new FXMLLoader(Main.class.getResource("/atChat/resources/fxml/atMessageWindow.fxml"));
AnchorPane msgRoot = (AnchorPane) loader.load();
MessageWindow diagCont = (MessageWindow) loader.getController();
diagCont.setMessage(msg, dialogWin);
dialogWin.getIcons().addAll(new Image("/atChat/resources/images/@Chat_Icon_16_32bit.png"), new Image("/atChat/resources/images/@Chat_Icon_24_32bit.png"), new Image("/atChat/resources/images/@Chat_Icon_32_32bit.png"), new Image("/atChat/resources/images/@Chat_Icon_48_32bit.png"), new Image("/atChat/resources/images/@Chat_Icon_64_32bit.png"));
//dialogWin.setResizable(false);
dialogWin.setTitle(title);
Scene scene = new Scene(msgRoot);
scene.getStylesheets().clear();
scene.getStylesheets().add(Main.class.getResource("/atChat/resources/fxml/atChatTheme.css").toExternalForm());
dialogWin.setScene(scene);
centerWindow(dialogWin, msgRoot, mMain.getStage());
dialogWin.initOwner(mMain.getStage());
dialogWin.initModality(Modality.APPLICATION_MODAL);
dialogWin.show();
double tmpW = scene.getWidth();
double tmpH = scene.getHeight();
double tmpX = dialogWin.getX();
double tmpY = dialogWin.getY();
dialogWin.setResizable(false);
if (tmpW != scene.getWidth()) {
dialogWin.setWidth(dialogWin.getWidth() - (scene.getWidth() - tmpW));
dialogWin.setX(tmpX);
}
if (tmpH != scene.getHeight()) {
dialogWin.setHeight(dialogWin.getHeight() - (scene.getHeight() - tmpH));
dialogWin.setY(tmpY);
}
} catch (IOException e) {
System.out.println("Unable to create error window:" + System.getProperty("line.separator") + e.getMessage());
Logger.getLogger(Main.class.getName()).log(Level.WARNING, "Unable to create error window:" + System.getProperty("line.separator") + e.getMessage(), e);
}
}
}
protected static void popAcceptWindow(HostConnection.Client client, CryptoRSA clientRSA, String handle, int connectingPort, String connectingIP) {
Platform.runLater(new ChatCommon().new PopAccept(client, clientRSA, handle + " on port: " + Integer.toString(connectingPort) + " from IP: " + connectingIP));
}
private class PopAccept implements Runnable {
private final HostConnection.Client client;
private final CryptoRSA clientRSA;
private final String connectingParty;
public PopAccept(HostConnection.Client client, CryptoRSA clientRSA, String connectingParty) {
this.client = client;
this.clientRSA = clientRSA;
this.connectingParty = connectingParty;
}
@Override
public void run() {
try {
final Stage dialogWin = new Stage(StageStyle.DECORATED);
FXMLLoader loader = new FXMLLoader(Main.class.getResource("/atChat/resources/fxml/atAcceptConnection.fxml"));
AnchorPane acceptRoot = (AnchorPane) loader.load();
AcceptConnectionWindow diagCont = (AcceptConnectionWindow) loader.getController();
diagCont.initWindow(dialogWin, client, clientRSA, connectingParty, mMain);
dialogWin.getIcons().addAll(new Image("/atChat/resources/images/@Chat_Icon_16_32bit.png"), new Image("/atChat/resources/images/@Chat_Icon_24_32bit.png"), new Image("/atChat/resources/images/@Chat_Icon_32_32bit.png"), new Image("/atChat/resources/images/@Chat_Icon_48_32bit.png"), new Image("/atChat/resources/images/@Chat_Icon_64_32bit.png"));
//dialogWin.setResizable(false);
dialogWin.setTitle("Accept New Client");
Scene scene = new Scene(acceptRoot);
scene.getStylesheets().clear();
scene.getStylesheets().add(Main.class.getResource("/atChat/resources/fxml/atChatTheme.css").toExternalForm());
dialogWin.setScene(scene);
centerWindow(dialogWin, acceptRoot, mMain.getStage());
dialogWin.initOwner(mMain.getStage());
dialogWin.show();
double tmpW = scene.getWidth();
double tmpH = scene.getHeight();
double tmpX = dialogWin.getX();
double tmpY = dialogWin.getY();
dialogWin.setResizable(false);
if (tmpW != scene.getWidth()) {
dialogWin.setWidth(dialogWin.getWidth() - (scene.getWidth() - tmpW));
dialogWin.setX(tmpX);
}
if (tmpH != scene.getHeight()) {
dialogWin.setHeight(dialogWin.getHeight() - (scene.getHeight() - tmpH));
dialogWin.setY(tmpY);
}
} catch (IOException e) {
System.out.println("Unable to create accept new client window:" + System.getProperty("line.separator") + e.getMessage());
Logger.getLogger(Main.class.getName()).log(Level.WARNING, "Unable to create accept new client window:" + System.getProperty("line.separator") + e.getMessage(), e);
}
}
}
protected static void popAcceptFiles(final String[] files, final long[] fileSizes, final FileSelector[] selectors, final String handle, final int key, final String clientKey, final int remotePort, final HostConnection.Client client) {
Platform.runLater(new Runnable() {
@Override
public void run() {
try {
final Stage dialogWin = new Stage(StageStyle.DECORATED);
FXMLLoader loader = new FXMLLoader(Main.class.getResource("/atChat/resources/fxml/atAcceptFiles.fxml"));
AnchorPane acceptRoot = (AnchorPane) loader.load();
AcceptFileTransfer diagCont = (AcceptFileTransfer) loader.getController();
diagCont.initWindow(files, fileSizes, selectors, dialogWin, handle, key, clientKey, remotePort, mMain, client);
dialogWin.getIcons().addAll(new Image("/atChat/resources/images/@Chat_Icon_16_32bit.png"), new Image("/atChat/resources/images/@Chat_Icon_24_32bit.png"), new Image("/atChat/resources/images/@Chat_Icon_32_32bit.png"), new Image("/atChat/resources/images/@Chat_Icon_48_32bit.png"), new Image("/atChat/resources/images/@Chat_Icon_64_32bit.png"));
//dialogWin.setResizable(false);
dialogWin.setTitle("Accept File Transfer");
Scene scene = new Scene(acceptRoot);
scene.getStylesheets().clear();
scene.getStylesheets().add(Main.class.getResource("/atChat/resources/fxml/atChatTheme.css").toExternalForm());
dialogWin.setScene(scene);
centerWindow(dialogWin, acceptRoot, mMain.getStage());
dialogWin.initOwner(mMain.getStage());
dialogWin.show();
double tmpW = scene.getWidth();
double tmpH = scene.getHeight();
double tmpX = dialogWin.getX();
double tmpY = dialogWin.getY();
dialogWin.setResizable(false);
if (tmpW != scene.getWidth()) {
dialogWin.setWidth(dialogWin.getWidth() - (scene.getWidth() - tmpW));
dialogWin.setX(tmpX);
}
if (tmpH != scene.getHeight()) {
dialogWin.setHeight(dialogWin.getHeight() - (scene.getHeight() - tmpH));
dialogWin.setY(tmpY);
}
} catch (IOException e) {
System.out.println("Unable to create accept file transfer window:" + System.getProperty("line.separator") + e.getMessage());
Logger.getLogger(Main.class.getName()).log(Level.WARNING, "Unable to create accept file transfer window:" + System.getProperty("line.separator") + e.getMessage(), e);
}
}
});
}
protected static void popAcceptDirectory(final TreeItem<String> files, final long[] fileSizes, final LinkedHashMap<String, FileSelector> fSelectors, final String handle, final int key, final String clientKey, final int remotePort, final HostConnection.Client client) {
Platform.runLater(new Runnable() {
@Override
public void run() {
try {
final Stage dialogWin = new Stage(StageStyle.DECORATED);
FXMLLoader loader = new FXMLLoader(Main.class.getResource("/atChat/resources/fxml/atAcceptDirectory.fxml"));
AnchorPane acceptRoot = (AnchorPane) loader.load();
AcceptDirectoryTransfer diagCont = (AcceptDirectoryTransfer) loader.getController();
diagCont.initWindow(files, fileSizes, fSelectors, dialogWin, handle, key, clientKey, remotePort, mMain, client);
dialogWin.getIcons().addAll(new Image("/atChat/resources/images/@Chat_Icon_16_32bit.png"), new Image("/atChat/resources/images/@Chat_Icon_24_32bit.png"), new Image("/atChat/resources/images/@Chat_Icon_32_32bit.png"), new Image("/atChat/resources/images/@Chat_Icon_48_32bit.png"), new Image("/atChat/resources/images/@Chat_Icon_64_32bit.png"));
//dialogWin.setResizable(false);
dialogWin.setTitle("Accept Directory Transfer");
Scene scene = new Scene(acceptRoot);
scene.getStylesheets().clear();
scene.getStylesheets().add(Main.class.getResource("/atChat/resources/fxml/atChatTheme.css").toExternalForm());
dialogWin.setScene(scene);
centerWindow(dialogWin, acceptRoot, mMain.getStage());
dialogWin.initOwner(mMain.getStage());
dialogWin.show();
double tmpW = scene.getWidth();
double tmpH = scene.getHeight();
double tmpX = dialogWin.getX();
double tmpY = dialogWin.getY();
dialogWin.setResizable(false);
if (tmpW != scene.getWidth()) {
dialogWin.setWidth(dialogWin.getWidth() - (scene.getWidth() - tmpW));
dialogWin.setX(tmpX);
}
if (tmpH != scene.getHeight()) {
dialogWin.setHeight(dialogWin.getHeight() - (scene.getHeight() - tmpH));
dialogWin.setY(tmpY);
}
} catch (IOException e) {
System.out.println("Unable to create accept directory transfer window:" + System.getProperty("line.separator") + e.getMessage());
Logger.getLogger(Main.class.getName()).log(Level.WARNING, "Unable to create accept directory transfer window:" + System.getProperty("line.separator") + e.getMessage(), e);
}
}
});
}
public class parseTransferRequest implements Runnable {
private final String[] sMsg;
private final String handle;
private final int key;
private final String clientKey;
private final int remotePort;
private final HostConnection.Client client;
private final boolean isDir;
private LinkedHashMap<String, FileSelector> fSelectors;
private int newLoc = 0;
public parseTransferRequest(String[] msg, String handle, int key, String clientKey, int remotePort, HostConnection.Client client, boolean isDir) {
sMsg = msg;
this.handle = handle;
this.key = key;
this.clientKey = clientKey;
this.remotePort = remotePort;
this.client = client;
this.isDir = isDir;
fSelectors = new LinkedHashMap<String, FileSelector>();
}
@Override
public void run() {
boolean cancelled = false;
String[] files;
if (client == null) {
files = Arrays.copyOfRange(sMsg, 4, sMsg.length);
} else {
files = Arrays.copyOfRange(sMsg, 2, sMsg.length);
}
if (files.length % 2 == 0) {
String[] fileNames = new String[files.length / 2];
long[] fileSizes = new long[fileNames.length];
FileSelector[] fileSelectors = null;
boolean success = true;
if (!isDir) {
fileSelectors = new FileSelector[fileNames.length];
for (int x = 0; x < files.length; x ++) {
if (mMain.isConnected()) {
fileNames[x / 2] = files[x];
fileSelectors[x / 2] = new FileSelector(files[x]);
try {
fileSizes[(x / 2)] = Long.parseLong(files[x + 1]);
} catch (NumberFormatException sizeE) {
success = false;
break;
}
x++;
} else {
cancelled = true;
break;
}
}
} else {
for (int x = 0; x < files.length; x ++) {
if (mMain.isConnected()) {
fileNames[x / 2] = formatFilePath(files[x]);
try {
fileSizes[(x / 2)] = Long.parseLong(files[x + 1]);
} catch (NumberFormatException sizeE) {
success = false;
break;
}
x++;
} else {
cancelled = true;
break;
}
}
}
if (success && !cancelled && mMain.isConnected()) {
if (client != null) {
if (client.isAccepted()) {
if (!isDir) {
popAcceptFiles(fileNames, fileSizes, fileSelectors, handle, key, clientKey, remotePort, client);
} else {
TreeItem<String> rootNode = createTree(fileNames, fileSizes);
popAcceptDirectory(rootNode, fileSizes, fSelectors, handle, key, clientKey, remotePort, client);
}
}
} else {
if (!isDir) {
popAcceptFiles(fileNames, fileSizes, fileSelectors, handle, key, clientKey, remotePort, null);
} else {
TreeItem<String> rootNode = createTree(fileNames, fileSizes);
popAcceptDirectory(rootNode, fileSizes, fSelectors, handle, key, clientKey, remotePort, null);
}
}
} else if (!cancelled && mMain.isConnected()) {
if (client == null) {
mMain.getClient().sendMessage(ChatCommon.formatSystemMessage(ChatCommon.DOWNLOAD_S_ERROR + ChatCommon.SEPARATOR + Integer.toString(key)), true);
} else if (client.isAccepted()) {
ArrayList<HostConnection.Client> sendTo = new ArrayList<HostConnection.Client>();
sendTo.add(client);
mMain.getHost().addMessageToQueue(ChatCommon.formatSystemMessage(ChatCommon.DOWNLOAD_S_ERROR + ChatCommon.SEPARATOR + Integer.toString(key)), sendTo, false, false, null);
}
}
} else if (mMain.isConnected()) {
if (client == null) {
mMain.getClient().sendMessage(ChatCommon.formatSystemMessage(ChatCommon.DOWNLOAD_S_ERROR + ChatCommon.SEPARATOR + Integer.toString(key)), true);
} else if (client.isAccepted()) {
ArrayList<HostConnection.Client> sendTo = new ArrayList<HostConnection.Client>();
sendTo.add(client);
mMain.getHost().addMessageToQueue(ChatCommon.formatSystemMessage(ChatCommon.DOWNLOAD_S_ERROR + ChatCommon.SEPARATOR + Integer.toString(key)), sendTo, false, false, null);
}
}
}
private TreeItem<String> createTree(String[] files, long[] sizes) {
FileSelector rootSel = new FileSelector(files[0], sizes[0]);
fSelectors.put(files[0], rootSel);
TreeItem<String> root = rootSel.getItem();
for (int t = 1; t < files.length; t ++) {
String file = files[t];
if (!file.endsWith(File.separator)) {
FileSelector nodeSel = new FileSelector(file, sizes[t]);
fSelectors.put(file, nodeSel);
root.getChildren().add(nodeSel.getItem());
} else {
root.getChildren().add(recursiveTree(files, t, sizes));
t = newLoc;
}
}
return root;
}
private TreeItem<String> recursiveTree(String[] files, int startLoc, long[] sizes) {
String parentDir = files[startLoc];
FileSelector parentSel = new FileSelector(parentDir, sizes[startLoc]);
TreeItem<String> subRoot = parentSel.getItem();
fSelectors.put(files[startLoc], parentSel);
if (files.length > startLoc + 1) {
for (int n = startLoc + 1; n < files.length; n ++) {
String file = files[n];
if (file.startsWith(parentDir)) {
if (!file.endsWith(File.separator)) {
FileSelector nodeSel = new FileSelector(file, sizes[n]);
subRoot.getChildren().add(nodeSel.getItem());
fSelectors.put(file, nodeSel);
} else {
subRoot.getChildren().add(recursiveTree(files, n, sizes));
n = newLoc;
}
if (n == files.length - 1) {
newLoc = n;
}
} else {
newLoc = n - 1;
break;
}
}
}
return subRoot;
}
}
public class DownloadCalculator {
private long lastCalc;
private long[] bytes = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
private long[] times = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
private int currentLoc = 9;
private long currentAmount = 0;
private final Label etaLabel;
private final Label speedLabel;
public DownloadCalculator(Label eta, Label speed) {
lastCalc = System.currentTimeMillis();
etaLabel = eta;
speedLabel = speed;
}
public boolean calc(long byteAmount, long remaining) {
currentAmount += byteAmount;
if (System.currentTimeMillis() - lastCalc >= 1000) {
currentLoc = (currentLoc == 9) ? 0 : currentLoc + 1;
bytes[currentLoc] = currentAmount;
times[currentLoc] = System.currentTimeMillis() - lastCalc;
currentAmount = 0;
long totalBytes = 0;
long totalTimes = 0;
for (int i = 0; i < bytes.length; i ++) {
totalBytes += bytes[i];
totalTimes += times[i];
}
if (totalTimes >= 0) {
double seconds = (double) totalTimes / (double) 1000;
double bytesPerSecond = totalBytes / seconds;
double secondsRemaining = remaining / bytesPerSecond;
setLabels("ETA: " + formatTimeRemaining(secondsRemaining), "Rate: " + formatSpeed(bytesPerSecond));
} else {
setLabels("ETA: ??", "Rate: ??");
}
lastCalc = System.currentTimeMillis();
return true;
} else {
return false;
}
}
private void setLabels(final String eta, final String speed) {
Platform.runLater(new Runnable() {
@Override
public void run() {
etaLabel.setText(eta);
speedLabel.setText(speed);
}
});
}
private String formatSpeed(double bytesPerSecond) {
double kb = bytesPerSecond / 1024;
double mb = kb / 1024;
double gb = mb / 1024;
if (gb >= 1) {
return Float.toString(Math.round(gb * 100) / 100f) + "GB/s";
} else if (mb >= 1) {
return Float.toString(Math.round(mb * 100) / 100f) + "MB/s";
} else if (kb >= 1) {
return Float.toString(Math.round(kb * 100) / 100f) + "KB/s";
} else {
return Float.toString(Math.round(bytesPerSecond * 100) / 100f) + "B/s";
}
}
private String formatTimeRemaining (double secondsRemaining) {
int seconds = (int) Math.floor(((secondsRemaining / (double) 60) - Math.floor(secondsRemaining / (double) 60)) * (double) 60);
int minutes = (int) Math.floor((((secondsRemaining / 60) / 60) - Math.floor((secondsRemaining / 60) / 60)) * 60);
int hours = (int) Math.floor((secondsRemaining / 60) / 60);
if (hours <= 99) {
String timeRemaining = Integer.toString(hours) + ":";
if (minutes >= 10) {
timeRemaining += Integer.toString(minutes) + ":";
} else {
timeRemaining += "0" + Integer.toString(minutes) + ":";
}
if (seconds >= 10) {
timeRemaining += Integer.toString(seconds);
} else {
timeRemaining += "0" + Integer.toString(seconds);
}
return timeRemaining;
} else {
return ">99 hours";
}
}
public void setLastTime(long time) {
lastCalc = time;
}
}
public static void playIncomming() {
incommingClip.setFramePosition(0);
incommingClip.start();
}
public static void playOutgoing() {
outgoingClip.setFramePosition(0);
outgoingClip.start();
}
public static void playSystem() {
systemClip.setFramePosition(0);
systemClip.start();
}
public static void loadAudioClips() {
AudioInputStream incomming = null;
AudioInputStream outgoing = null;
AudioInputStream systemSound = null;
try {
incomming = AudioSystem.getAudioInputStream(Main.class.getResource("/atChat/resources/sounds/incoming_message.wav"));
incommingClip = AudioSystem.getClip();
incommingClip.open(incomming);
outgoing = AudioSystem.getAudioInputStream(Main.class.getResource("/atChat/resources/sounds/outgoing_message.wav"));
outgoingClip = AudioSystem.getClip();
outgoingClip.open(outgoing);
systemSound = AudioSystem.getAudioInputStream(Main.class.getResource("/atChat/resources/sounds/system_message.wav"));
systemClip = AudioSystem.getClip();
systemClip.open(systemSound);
} catch (UnsupportedAudioFileException aE) {
if (aE.getMessage() != null) {
System.out.println("Unable to play sound clip:" + System.getProperty("line.separator") + aE.getMessage());
}
} catch (IOException ioE) {
if (ioE.getMessage() != null) {
System.out.println("Unable to play sound clip:" + System.getProperty("line.separator") + ioE.getMessage());
}
} catch (LineUnavailableException lineE) {
if (lineE.getMessage() != null) {
System.out.println("Unable to play sound clip:" + System.getProperty("line.separator") + lineE.getMessage());
}
} finally {
if (incomming != null) {
try {
incomming.close();
} catch (IOException e) {
}
}
if (outgoing != null) {
try {
outgoing.close();
} catch (IOException e) {
}
}
if (systemSound != null) {
try {
systemSound.close();
} catch (IOException e) {
}
}
}
}
public static void closeClips() {
if (outgoingClip != null) {
outgoingClip.close();
}
if (incommingClip != null) {
incommingClip.close();
}
if (systemClip != null) {
systemClip.close();
}
}
}