Browse Source

Migrate DCC plugin to use events.

pull/453/head
Chris Smith 8 years ago
parent
commit
d95097c3ea

+ 0
- 9
dcc/plugin.config View File

@@ -8,7 +8,6 @@ keysections:
8 8
   requires
9 9
   updates
10 10
   defaults
11
-  formatters
12 11
   icons
13 12
   version
14 13
 
@@ -52,14 +51,6 @@ defaults:
52 51
   firewall.ports.startPort=11000
53 52
   firewall.ports.endPort=11019
54 53
 
55
-formatters:
56
-  DCCChatStarting=Starting DCC Chat with: %1$s on %2$s:%3$s
57
-  DCCChatInfo=%1$s
58
-  DCCChatError=Error: %1$s
59
-  DCCSendError=Error: %1$s
60
-  DCCChatSelfMessage=<%1$s> %2$s
61
-  DCCChatMessage=<%1$s> %2$s
62
-
63 54
 icons:
64 55
   category-dcc=plugin://dcc:com/dmdirc/addons/dcc/res/transfers.png
65 56
   dcc=plugin://dcc:com/dmdirc/addons/dcc/res/transfers.png

+ 11
- 0
dcc/res/META-INF/format.yml View File

@@ -0,0 +1,11 @@
1
+---
2
+DccChatStartingEvent:
3
+  format: "Starting DCC Chat with: {{target}} on {{host}}:{{port}}..."
4
+DccChatSocketClosedEvent:
5
+  format: "Socket closed."
6
+DccChatSocketOpenedEvent:
7
+  format: "Socket opened."
8
+DccChatMessageEvent:
9
+  format: "<{{nickname}}> {{message}}"
10
+DccChatSelfMessageEvent:
11
+  format: "<{{nickname}}> {{message}}"

+ 9
- 17
dcc/src/com/dmdirc/addons/dcc/ChatContainer.java View File

@@ -24,17 +24,17 @@ package com.dmdirc.addons.dcc;
24 24
 
25 25
 import com.dmdirc.DMDircMBassador;
26 26
 import com.dmdirc.addons.dcc.events.DccChatMessageEvent;
27
-import com.dmdirc.addons.dcc.events.DccChatSelfmessageEvent;
28
-import com.dmdirc.addons.dcc.events.DccChatSocketclosedEvent;
29
-import com.dmdirc.addons.dcc.events.DccChatSocketopenedEvent;
27
+import com.dmdirc.addons.dcc.events.DccChatSelfMessageEvent;
28
+import com.dmdirc.addons.dcc.events.DccChatSocketClosedEvent;
29
+import com.dmdirc.addons.dcc.events.DccChatSocketOpenedEvent;
30 30
 import com.dmdirc.addons.dcc.io.DCCChat;
31
+import com.dmdirc.events.CommandErrorEvent;
31 32
 import com.dmdirc.interfaces.CommandController;
32 33
 import com.dmdirc.interfaces.WindowModel;
33 34
 import com.dmdirc.interfaces.config.AggregateConfigProvider;
34 35
 import com.dmdirc.ui.core.components.WindowComponent;
35 36
 import com.dmdirc.ui.input.TabCompleterFactory;
36 37
 import com.dmdirc.ui.messages.BackBufferFactory;
37
-import com.dmdirc.util.EventUtils;
38 38
 
39 39
 import java.util.Arrays;
40 40
 
@@ -105,27 +105,21 @@ public class ChatContainer extends DCCFrameContainer implements DCCChatHandler {
105 105
     @Override
106 106
     public void sendLine(final String line) {
107 107
         if (dccChat.isWriteable()) {
108
-            final DccChatSelfmessageEvent event = new DccChatSelfmessageEvent(this, line);
109
-            final String format = EventUtils.postDisplayable(eventBus, event, "DCCChatSelfMessage");
110
-            addLine(format, nickname, line);
108
+            eventBus.publishAsync(new DccChatSelfMessageEvent(this, nickname, line));
111 109
             dccChat.sendLine(line);
112 110
         } else {
113
-            addLine("DCCChatError", "Socket is closed.", line);
111
+            eventBus.publishAsync(new CommandErrorEvent(this, "Socket is closed."));
114 112
         }
115 113
     }
116 114
 
117 115
     @Override
118 116
     public void handleChatMessage(final DCCChat dcc, final String message) {
119
-        final DccChatMessageEvent event = new DccChatMessageEvent(this, otherNickname, message);
120
-        final String format = EventUtils.postDisplayable(eventBus, event, "DCCChatMessage");
121
-        addLine(format, otherNickname, message);
117
+        eventBus.publishAsync(new DccChatMessageEvent(this, otherNickname, message));
122 118
     }
123 119
 
124 120
     @Override
125 121
     public void socketClosed(final DCCChat dcc) {
126
-        final DccChatSocketclosedEvent event = new DccChatSocketclosedEvent(this);
127
-        final String format = EventUtils.postDisplayable(eventBus, event, "DCCChatInfo");
128
-        addLine(format, "Socket closed");
122
+        eventBus.publishAsync(new DccChatSocketClosedEvent(this));
129 123
         if (!isWindowClosing()) {
130 124
             setIcon("dcc-chat-inactive");
131 125
         }
@@ -133,9 +127,7 @@ public class ChatContainer extends DCCFrameContainer implements DCCChatHandler {
133 127
 
134 128
     @Override
135 129
     public void socketOpened(final DCCChat dcc) {
136
-        final DccChatSocketopenedEvent event = new DccChatSocketopenedEvent(this);
137
-        final String format = EventUtils.postDisplayable(eventBus, event, "DCCChatInfo");
138
-        addLine(format, "Socket opened");
130
+        eventBus.publishAsync(new DccChatSocketOpenedEvent(this));
139 131
         setIcon("dcc-chat-active");
140 132
     }
141 133
 

+ 10
- 7
dcc/src/com/dmdirc/addons/dcc/DCCCommand.java View File

@@ -24,6 +24,7 @@ package com.dmdirc.addons.dcc;
24 24
 
25 25
 import com.dmdirc.DMDircMBassador;
26 26
 import com.dmdirc.addons.dcc.events.DccChatRequestSentEvent;
27
+import com.dmdirc.addons.dcc.events.DccChatStartingEvent;
27 28
 import com.dmdirc.addons.dcc.events.DccSendRequestEvent;
28 29
 import com.dmdirc.addons.dcc.io.DCC;
29 30
 import com.dmdirc.addons.dcc.io.DCCChat;
@@ -170,12 +171,15 @@ public class DCCCommand extends Command implements IntelligentCommand {
170 171
             parser.sendCTCP(target, "DCC", "CHAT chat " + DCC.ipToLong(
171 172
                     myPlugin.getListenIP(parser)) + ' ' + chat.getPort());
172 173
             eventBus.publish(new DccChatRequestSentEvent(connection, target));
173
-            sendLine(origin, isSilent, "DCCChatStarting", target, chat.getHost(), chat.getPort());
174
-            window.addLine("DCCChatStarting", target, chat.getHost(), chat.getPort());
174
+
175
+            // Send the starting event to both the source window, and the new DCC window.
176
+            window.getEventBus().publishAsync(new DccChatStartingEvent(
177
+                    window, target, chat.getHost(), chat.getPort()));
178
+            origin.getEventBus().publishAsync(new DccChatStartingEvent(
179
+                    origin, target, chat.getHost(), chat.getPort()));
175 180
         } else {
176
-            sendLine(origin, isSilent, "DCCChatError",
177
-                    "Unable to start chat with " + target
178
-                    + " - unable to create listen socket");
181
+            showError(origin, isSilent,
182
+                    "Unable to start chat with " + target + " - unable to create listen socket");
179 183
         }
180 184
     }
181 185
 
@@ -253,8 +257,7 @@ public class DCCCommand extends Command implements IntelligentCommand {
253 257
                             + " " + send.getPort() + " " + send.getFileSize()
254 258
                             + (send.isTurbo() ? " T" : ""));
255 259
                 } else {
256
-                    sendLine(origin, isSilent, "DCCSendError",
257
-                            "Unable to start dcc send with " + target
260
+                    showError(origin, isSilent, "Unable to start dcc send with " + target
258 261
                             + " - unable to create listen socket");
259 262
                 }
260 263
             }

+ 5
- 3
dcc/src/com/dmdirc/addons/dcc/DCCManager.java View File

@@ -25,6 +25,7 @@ package com.dmdirc.addons.dcc;
25 25
 import com.dmdirc.ClientModule.GlobalConfig;
26 26
 import com.dmdirc.DMDircMBassador;
27 27
 import com.dmdirc.addons.dcc.events.DccChatRequestEvent;
28
+import com.dmdirc.addons.dcc.events.DccChatStartingEvent;
28 29
 import com.dmdirc.addons.dcc.events.DccSendRequestEvent;
29 30
 import com.dmdirc.addons.dcc.io.DCC;
30 31
 import com.dmdirc.addons.dcc.io.DCCChat;
@@ -461,7 +462,7 @@ public class DCCManager {
461 462
         final DCCChat chat = new DCCChat();
462 463
         chat.setAddress(ipAddress, port);
463 464
         final String myNickname = parser.getLocalClient().getNickname();
464
-        final DCCFrameContainer f = new ChatContainer(
465
+        final DCCFrameContainer container = new ChatContainer(
465 466
                 getContainer(),
466 467
                 chat,
467 468
                 config,
@@ -472,8 +473,9 @@ public class DCCManager {
472 473
                 nickname,
473 474
                 tabCompleterFactory,
474 475
                 eventBus);
475
-        windowManager.addWindow(getContainer(), f);
476
-        f.addLine("DCCChatStarting", nickname, chat.getHost(), chat.getPort());
476
+        windowManager.addWindow(getContainer(), container);
477
+        container.getEventBus().publishAsync(new DccChatStartingEvent(
478
+                container, nickname, chat.getHost(), chat.getPort()));
477 479
         chat.connect();
478 480
     }
479 481
 

dcc/src/com/dmdirc/addons/dcc/events/DccChatSelfmessageEvent.java → dcc/src/com/dmdirc/addons/dcc/events/DccChatSelfMessageEvent.java View File

@@ -28,13 +28,16 @@ import com.dmdirc.interfaces.WindowModel;
28 28
 /**
29 29
  * Fired on a DCC chat self message.
30 30
  */
31
-public class DccChatSelfmessageEvent extends DccDisplayableEvent {
31
+public class DccChatSelfMessageEvent extends DccDisplayableEvent {
32 32
 
33 33
     private final ChatContainer chatWindow;
34
+    private final String nickname;
34 35
     private final String message;
35 36
 
36
-    public DccChatSelfmessageEvent(final ChatContainer chatWindow, final String message) {
37
+    public DccChatSelfMessageEvent(final ChatContainer chatWindow, final String nickname,
38
+            final String message) {
37 39
         this.chatWindow = chatWindow;
40
+        this.nickname = nickname;
38 41
         this.message = message;
39 42
     }
40 43
 
@@ -42,6 +45,10 @@ public class DccChatSelfmessageEvent extends DccDisplayableEvent {
42 45
         return chatWindow;
43 46
     }
44 47
 
48
+    public String getNickname() {
49
+        return nickname;
50
+    }
51
+
45 52
     public String getMessage() {
46 53
         return message;
47 54
     }

dcc/src/com/dmdirc/addons/dcc/events/DccChatSocketclosedEvent.java → dcc/src/com/dmdirc/addons/dcc/events/DccChatSocketClosedEvent.java View File

@@ -28,11 +28,11 @@ import com.dmdirc.interfaces.WindowModel;
28 28
 /**
29 29
  * Fired when a chat socket is closed.
30 30
  */
31
-public class DccChatSocketclosedEvent extends DccDisplayableEvent {
31
+public class DccChatSocketClosedEvent extends DccDisplayableEvent {
32 32
 
33 33
     private final ChatContainer chatWindow;
34 34
 
35
-    public DccChatSocketclosedEvent(final ChatContainer chatWindow) {
35
+    public DccChatSocketClosedEvent(final ChatContainer chatWindow) {
36 36
         this.chatWindow = chatWindow;
37 37
     }
38 38
 

dcc/src/com/dmdirc/addons/dcc/events/DccChatSocketopenedEvent.java → dcc/src/com/dmdirc/addons/dcc/events/DccChatSocketOpenedEvent.java View File

@@ -28,11 +28,11 @@ import com.dmdirc.interfaces.WindowModel;
28 28
 /**
29 29
  * Fired when a chat socket is opened.
30 30
  */
31
-public class DccChatSocketopenedEvent extends DccDisplayableEvent {
31
+public class DccChatSocketOpenedEvent extends DccDisplayableEvent {
32 32
 
33 33
     private final ChatContainer chatWindow;
34 34
 
35
-    public DccChatSocketopenedEvent(final ChatContainer chatWindow) {
35
+    public DccChatSocketOpenedEvent(final ChatContainer chatWindow) {
36 36
         this.chatWindow = chatWindow;
37 37
     }
38 38
 

+ 62
- 0
dcc/src/com/dmdirc/addons/dcc/events/DccChatStartingEvent.java View File

@@ -0,0 +1,62 @@
1
+/*
2
+ * Copyright (c) 2006-2015 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.events;
24
+
25
+import com.dmdirc.interfaces.WindowModel;
26
+
27
+/**
28
+ * Event raised when a DCC chat is starting.
29
+ */
30
+public class DccChatStartingEvent extends DccDisplayableEvent {
31
+
32
+    private final WindowModel source;
33
+    private final String target;
34
+    private final String host;
35
+    private final int port;
36
+
37
+    public DccChatStartingEvent(final WindowModel source, final String target, final String host,
38
+            final int port) {
39
+        this.source = source;
40
+        this.target = target;
41
+        this.host = host;
42
+        this.port = port;
43
+    }
44
+
45
+    @Override
46
+    public WindowModel getSource() {
47
+        return source;
48
+    }
49
+
50
+    public String getTarget() {
51
+        return target;
52
+    }
53
+
54
+    public String getHost() {
55
+        return host;
56
+    }
57
+
58
+    public int getPort() {
59
+        return port;
60
+    }
61
+
62
+}

Loading…
Cancel
Save