Browse Source

Refactor dCC slightly.

Add question dialog to DCC Chats.
Add question dialog to DCC Sends.
Remove unused method askQuestion method.

Change-Id: Iaf9f6ff679ea662df46b7da25d212d79097bd9bd
Reviewed-on: http://gerrit.dmdirc.com/3498
Automatic-Compile: DMDirc Build Manager
Reviewed-by: Chris Smith <chris@dmdirc.com>
changes/98/3498/5
Greg Holmes 10 years ago
parent
commit
229ea9bed6

+ 64
- 0
src/com/dmdirc/addons/dcc/ChatRequestDialog.java View File

@@ -0,0 +1,64 @@
1
+/*
2
+ * Copyright (c) 2006-2014 DMDirc Developers
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+
23
+package com.dmdirc.addons.dcc;
24
+
25
+import com.dmdirc.addons.ui_swing.dialogs.StandardQuestionDialog;
26
+import com.dmdirc.interfaces.Connection;
27
+import com.dmdirc.parser.interfaces.Parser;
28
+
29
+import java.awt.Window;
30
+
31
+/**
32
+ * Confirm DCC Chat dialog.
33
+ */
34
+public class ChatRequestDialog extends StandardQuestionDialog {
35
+
36
+    private final DCCManager manager;
37
+    private final Parser parser;
38
+    private final String nickname;
39
+    private final String[] ctcpData;
40
+
41
+    public ChatRequestDialog(final Window owner, final DCCManager manager,
42
+            final Connection connection, final String nickname, final String[] ctcpData) {
43
+        super(owner, ModalityType.APPLICATION_MODAL,
44
+                "DCC Chat Request", "User " + nickname + " on "
45
+                + connection.getAddress()
46
+                + " would like to start a DCC Chat with you.\n\n"
47
+                + "Do you want to continue?");
48
+        this.manager = manager;
49
+        this.parser = connection.getParser();
50
+        this.nickname = nickname;
51
+        this.ctcpData = ctcpData;
52
+    }
53
+
54
+    @Override
55
+    public boolean save() {
56
+        manager.handleDCCChat(parser, nickname, ctcpData);
57
+        return true;
58
+    }
59
+
60
+    @Override
61
+    public void cancelled() {
62
+    }
63
+
64
+}

+ 62
- 98
src/com/dmdirc/addons/dcc/DCCManager.java View File

@@ -168,33 +168,6 @@ public class DCCManager implements ActionListener {
168 168
         return domain;
169 169
     }
170 170
 
171
-    /**
172
-     * Ask a question, if the answer is the answer required, then recall handleProcessEvent.
173
-     *
174
-     * @param question      Question to ask
175
-     * @param title         Title of question dialog
176
-     * @param desiredAnswer Answer required
177
-     * @param type          Actiontype to pass back
178
-     * @param format        StringBuffer to pass back
179
-     * @param arguments     arguments to pass back
180
-     */
181
-    public void askQuestion(final String question, final String title,
182
-            final int desiredAnswer, final ActionType type,
183
-            final StringBuffer format, final Object... arguments) {
184
-        // New thread to ask the question in to stop us locking the UI
185
-        new Thread(new Runnable() {
186
-
187
-            @Override
188
-            public void run() {
189
-                final int result = JOptionPane.showConfirmDialog(null, question,
190
-                        title, JOptionPane.YES_NO_OPTION);
191
-                if (result == desiredAnswer) {
192
-                    handleProcessEvent(type, format, true, arguments);
193
-                }
194
-            }
195
-        }, "QuestionThread: " + title).start();
196
-    }
197
-
198 171
     /**
199 172
      * Ask the location to save a file, then start the download.
200 173
      *
@@ -402,10 +375,10 @@ public class DCCManager implements ActionListener {
402 375
             if ("DCC".equalsIgnoreCase((String) arguments[2])) {
403 376
                 if ("chat".equalsIgnoreCase(ctcpData[0])
404 377
                         && ctcpData.length > 3) {
405
-                    handleChat(type, format, dontAsk, ctcpData, arguments);
378
+                    handleChat(dontAsk, ctcpData, arguments);
406 379
                 } else if ("send".equalsIgnoreCase(ctcpData[0])
407 380
                         && ctcpData.length > 3) {
408
-                    handleSend(type, format, dontAsk, ctcpData, arguments);
381
+                    handleSend(dontAsk, ctcpData, arguments);
409 382
                 } else if (("resume".equalsIgnoreCase(ctcpData[0])
410 383
                         || "accept".equalsIgnoreCase(ctcpData[0]))
411 384
                         && ctcpData.length > 2) {
@@ -418,64 +391,59 @@ public class DCCManager implements ActionListener {
418 391
     /**
419 392
      * Handles a DCC chat request.
420 393
      *
421
-     * @param type      The type of the event to process
422
-     * @param format    Format of messages that are about to be sent. (May be null)
423 394
      * @param dontAsk   Don't ask any questions, assume yes.
424 395
      * @param ctcpData  CTCP data bits
425 396
      * @param arguments The arguments for the event
426 397
      */
427
-    private void handleChat(final ActionType type, final StringBuffer format,
428
-            final boolean dontAsk, final String[] ctcpData,
398
+    private void handleChat(final boolean dontAsk, final String[] ctcpData,
429 399
             final Object... arguments) {
430 400
         final String nickname = ((ClientInfo) arguments[1]).getNickname();
431 401
         if (dontAsk) {
432
-            final DCCChat chat = new DCCChat();
433
-            try {
434
-                chat.setAddress(Long.parseLong(ctcpData[2]),
435
-                        Integer.parseInt(ctcpData[3]));
436
-            } catch (NumberFormatException nfe) {
437
-                return;
438
-            }
439
-            final String myNickname = ((Connection) arguments[0]).getParser()
440
-                    .getLocalClient().getNickname();
441
-            final DCCFrameContainer f = new ChatContainer(
442
-                    getContainer(),
443
-                    chat,
444
-                    config,
445
-                    commandController,
446
-                    "Chat: " + nickname,
447
-                    myNickname,
448
-                    nickname,
449
-                    tabCompleterFactory,
450
-                    messageSinkManager,
451
-                    urlBuilder,
452
-                    eventBus);
453
-            windowManager.addWindow(getContainer(), f);
454
-            f.addLine("DCCChatStarting", nickname, chat.getHost(),
455
-                    chat.getPort());
456
-            chat.connect();
402
+            handleDCCChat(((Connection) arguments[0]).getParser(), nickname, ctcpData);
457 403
         } else {
458 404
             eventBus.post(new DccChatRequestEvent((Connection) arguments[0], nickname));
459
-            askQuestion("User " + nickname + " on "
460
-                    + ((Connection) arguments[0]).getAddress()
461
-                    + " would like to start a DCC Chat with you.\n\n"
462
-                    + "Do you want to continue?",
463
-                    "DCC Chat Request", JOptionPane.YES_OPTION,
464
-                    type, format, arguments);
405
+            new ChatRequestDialog(mainWindow, this, (Connection) arguments[0], nickname, ctcpData)
406
+                    .display();
465 407
         }
466 408
     }
467 409
 
410
+    void handleDCCChat(final Parser parser, final String nickname, final String[] ctcpData) {
411
+        long ipAddress;
412
+        int port;
413
+        try {
414
+            ipAddress = Long.parseLong(ctcpData[2]);
415
+            port = Integer.parseInt(ctcpData[3]);
416
+        } catch (NumberFormatException nfe) {
417
+            return;
418
+        }
419
+        final DCCChat chat = new DCCChat();
420
+        chat.setAddress(ipAddress, port);
421
+        final String myNickname = parser.getLocalClient().getNickname();
422
+        final DCCFrameContainer f = new ChatContainer(
423
+                getContainer(),
424
+                chat,
425
+                config,
426
+                commandController,
427
+                "Chat: " + nickname,
428
+                myNickname,
429
+                nickname,
430
+                tabCompleterFactory,
431
+                messageSinkManager,
432
+                urlBuilder,
433
+                eventBus);
434
+        windowManager.addWindow(getContainer(), f);
435
+        f.addLine("DCCChatStarting", nickname, chat.getHost(), chat.getPort());
436
+        chat.connect();
437
+    }
438
+
468 439
     /**
469 440
      * Handles a DCC send request.
470 441
      *
471
-     * @param type      The type of the event to process
472
-     * @param format    Format of messages that are about to be sent. (May be null)
473 442
      * @param dontAsk   Don't ask any questions, assume yes.
474 443
      * @param ctcpData  CTCP data bits
475 444
      * @param arguments The arguments for the event
476 445
      */
477
-    private void handleSend(final ActionType type, final StringBuffer format,
478
-            final boolean dontAsk, final String[] ctcpData,
446
+    private void handleSend(final boolean dontAsk, final String[] ctcpData,
479 447
             final Object... arguments) {
480 448
         final String nickname = ((ClientInfo) arguments[1]).getNickname();
481 449
         final String filename;
@@ -530,51 +498,47 @@ public class DCCManager implements ActionListener {
530 498
                 && !ctcpData[i + 1].equals("T")) ? ctcpData[++i] : "";
531 499
 
532 500
         // Ignore incorrect ports, or non-numeric IP/Port
501
+        long ipLong;
502
+        int portInt;
533 503
         try {
534
-            final int portInt = Integer.parseInt(port);
504
+            portInt = Integer.parseInt(port);
535 505
             if (portInt > 65535 || portInt < 0) {
536 506
                 return;
537 507
             }
538
-            Long.parseLong(ip);
508
+            ipLong = Long.parseLong(ip);
539 509
         } catch (NumberFormatException nfe) {
540 510
             return;
541 511
         }
542 512
 
543
-        DCCTransfer send = DCCTransfer.findByToken(token);
544
-
545
-        if (send == null && !dontAsk) {
513
+        if (DCCTransfer.findByToken(token) == null && !dontAsk) {
546 514
             if (!token.isEmpty() && !port.equals("0")) {
547 515
                 // This is a reverse DCC Send that we no longer care about.
548 516
             } else {
549 517
                 eventBus.post(
550 518
                         new DccSendRequestEvent((Connection) arguments[0], nickname, filename));
551
-                askQuestion("User " + nickname + " on "
552
-                        + ((Connection) arguments[0]).getAddress()
553
-                        + " would like to send you a file over DCC.\n\nFile: "
554
-                        + filename + "\n\nDo you want to continue?",
555
-                        "DCC Send Request", JOptionPane.YES_OPTION, type,
556
-                        format, arguments);
519
+                new SendRequestDialog(mainWindow, this, token, ipLong, portInt, filename, size,
520
+                        nickname, (Connection) arguments[0]).display();
557 521
             }
522
+        }
523
+    }
524
+
525
+    void handleDCCSend(final String token, final long ip, final int port, final String filename,
526
+            final long size, final String nickname, final Parser parser) {
527
+        DCCTransfer send = DCCTransfer.findByToken(token);
528
+        final boolean newSend = send == null;
529
+        if (newSend) {
530
+            send = new DCCTransfer(config.getOptionInt(getDomain(), "send.blocksize"));
531
+            send.setTurbo(config.getOptionBool(getDomain(), "send.forceturbo"));
558 532
         } else {
559
-            final boolean newSend = send == null;
560
-            if (newSend) {
561
-                send = new DCCTransfer(config.getOptionInt(getDomain(),
562
-                        "send.blocksize"));
563
-                send.setTurbo(config.getOptionBool(getDomain(), "send.forceturbo"));
564
-            }
565
-            try {
566
-                send.setAddress(Long.parseLong(ip), Integer.parseInt(port));
567
-            } catch (NumberFormatException nfe) {
568
-                return;
569
-            }
570
-            if (newSend) {
571
-                send.setFileName(filename);
572
-                send.setFileSize(size);
573
-                saveFile(nickname, send, ((Connection) arguments[0]).getParser(),
574
-                        "0".equals(port), token);
575
-            } else {
576
-                send.connect();
577
-            }
533
+            return;
534
+        }
535
+        send.setAddress(ip, port);
536
+        if (newSend) {
537
+            send.setFileName(filename);
538
+            send.setFileSize(size);
539
+            saveFile(nickname, send, parser, port == 0, token);
540
+        } else {
541
+            send.connect();
578 542
         }
579 543
     }
580 544
 

+ 73
- 0
src/com/dmdirc/addons/dcc/SendRequestDialog.java View File

@@ -0,0 +1,73 @@
1
+/*
2
+ * Copyright (c) 2006-2014 DMDirc Developers
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+
23
+package com.dmdirc.addons.dcc;
24
+
25
+import com.dmdirc.addons.ui_swing.dialogs.StandardQuestionDialog;
26
+import com.dmdirc.interfaces.Connection;
27
+import com.dmdirc.parser.interfaces.Parser;
28
+
29
+import java.awt.Window;
30
+
31
+/**
32
+ * Confirm DCC Send dialog.
33
+ */
34
+public class SendRequestDialog extends StandardQuestionDialog {
35
+
36
+    private final DCCManager manager;
37
+    private final String token;
38
+    private final long ip;
39
+    private final int port;
40
+    private final String filename;
41
+    private final long size;
42
+    private final String nickname;
43
+    private final Parser parser;
44
+
45
+    public SendRequestDialog(final Window owner, final DCCManager manager, final String token,
46
+            final long ip, final int port, final String filename, final long size,
47
+            final String nickname, final Connection connection) {
48
+        super(owner, ModalityType.APPLICATION_MODAL, "DCC Send Request",
49
+                "User " + nickname + " on "
50
+                + connection.getAddress()
51
+                + " would like to send you a file over DCC.\n\nFile: "
52
+                + filename + "\n\nDo you want to continue?");
53
+        this.manager = manager;
54
+        this.token = token;
55
+        this.ip = ip;
56
+        this.port = port;
57
+        this.filename = filename;
58
+        this.size = size;
59
+        this.nickname = nickname;
60
+        this.parser = connection.getParser();
61
+    }
62
+
63
+    @Override
64
+    public boolean save() {
65
+        manager.handleDCCSend(token, ip, port, filename, size, nickname, parser);
66
+        return true;
67
+    }
68
+
69
+    @Override
70
+    public void cancelled() {
71
+    }
72
+
73
+}

Loading…
Cancel
Save