From 35a26e2a475bb90a2ddafe0061699c599c16bd74 Mon Sep 17 00:00:00 2001 From: Zlatin Balevsky Date: Tue, 12 Nov 2019 15:47:38 +0000 Subject: [PATCH] advertise chat ability in search results --- .../core/connection/ConnectionAcceptor.groovy | 5 +++++ .../com/muwire/core/search/ResultsSender.groovy | 11 +++++++++-- .../com/muwire/core/search/UIResultEvent.groovy | 1 + .../com/muwire/gui/MainFrameController.groovy | 8 ++++++-- .../com/muwire/gui/SearchTabController.groovy | 11 +++++++++++ .../models/com/muwire/gui/SearchTabModel.groovy | 1 + .../views/com/muwire/gui/SearchTabView.groovy | 17 +++++++++++++++++ 7 files changed, 50 insertions(+), 4 deletions(-) diff --git a/core/src/main/groovy/com/muwire/core/connection/ConnectionAcceptor.groovy b/core/src/main/groovy/com/muwire/core/connection/ConnectionAcceptor.groovy index 56a19319..9b100f16 100644 --- a/core/src/main/groovy/com/muwire/core/connection/ConnectionAcceptor.groovy +++ b/core/src/main/groovy/com/muwire/core/connection/ConnectionAcceptor.groovy @@ -306,6 +306,10 @@ class ConnectionAcceptor { throw new IOException("No Sender header") if (!headers.containsKey("Count")) throw new IOException("No Count header") + + boolean chat = false + if (headers.containsKey('Chat')) + chat = Boolean.parseBoolean(headers['Chat']) byte [] personaBytes = Base64.decode(headers['Sender']) Persona sender = new Persona(new ByteArrayInputStream(personaBytes)) @@ -324,6 +328,7 @@ class ConnectionAcceptor { dis.readFully(payload) def json = slurper.parse(payload) results[i] = ResultsParser.parse(sender, resultsUUID, json) + results[i].chat = chat } eventBus.publish(new UIResultBatchEvent(uuid: resultsUUID, results: results)) } catch (IOException bad) { diff --git a/core/src/main/groovy/com/muwire/core/search/ResultsSender.groovy b/core/src/main/groovy/com/muwire/core/search/ResultsSender.groovy index 06f0ad60..3b2e0bc5 100644 --- a/core/src/main/groovy/com/muwire/core/search/ResultsSender.groovy +++ b/core/src/main/groovy/com/muwire/core/search/ResultsSender.groovy @@ -1,6 +1,7 @@ package com.muwire.core.search import com.muwire.core.SharedFile +import com.muwire.core.chat.ChatServer import com.muwire.core.connection.Endpoint import com.muwire.core.connection.I2PConnector import com.muwire.core.filecert.CertificateManager @@ -48,13 +49,16 @@ class ResultsSender { private final EventBus eventBus private final MuWireSettings settings private final CertificateManager certificateManager + private final ChatServer chatServer - ResultsSender(EventBus eventBus, I2PConnector connector, Persona me, MuWireSettings settings, CertificateManager certificateManager) { + ResultsSender(EventBus eventBus, I2PConnector connector, Persona me, MuWireSettings settings, + CertificateManager certificateManager, ChatServer chatServer) { this.connector = connector; this.eventBus = eventBus this.me = me this.settings = settings this.certificateManager = certificateManager + this.chatServer = chatServer } void sendResults(UUID uuid, SharedFile[] results, Destination target, boolean oobInfohash, boolean compressedResults) { @@ -82,7 +86,8 @@ class ResultsSender { uuid : uuid, sources : suggested, comment : comment, - certificates : certificates + certificates : certificates, + chat : chatServer.running.get() && settings.advertiseChat ) uiResultEvents << uiResultEvent } @@ -130,6 +135,8 @@ class ResultsSender { os.write("RESULTS $uuid\r\n".getBytes(StandardCharsets.US_ASCII)) os.write("Sender: ${me.toBase64()}\r\n".getBytes(StandardCharsets.US_ASCII)) os.write("Count: $results.length\r\n".getBytes(StandardCharsets.US_ASCII)) + boolean chat = chatServer.running.get() && settings.advertiseChat + os.write("Chat: $chat\r\n".getBytes(StandardCharsets.US_ASCII)) os.write("\r\n".getBytes(StandardCharsets.US_ASCII)) DataOutputStream dos = new DataOutputStream(new GZIPOutputStream(os)) results.each { diff --git a/core/src/main/groovy/com/muwire/core/search/UIResultEvent.groovy b/core/src/main/groovy/com/muwire/core/search/UIResultEvent.groovy index 7a148744..4fad4f90 100644 --- a/core/src/main/groovy/com/muwire/core/search/UIResultEvent.groovy +++ b/core/src/main/groovy/com/muwire/core/search/UIResultEvent.groovy @@ -17,6 +17,7 @@ class UIResultEvent extends Event { String comment boolean browse int certificates + boolean chat @Override public String toString() { diff --git a/gui/griffon-app/controllers/com/muwire/gui/MainFrameController.groovy b/gui/griffon-app/controllers/com/muwire/gui/MainFrameController.groovy index e13fb969..e8645c6b 100644 --- a/gui/griffon-app/controllers/com/muwire/gui/MainFrameController.groovy +++ b/gui/griffon-app/controllers/com/muwire/gui/MainFrameController.groovy @@ -452,7 +452,11 @@ class MainFrameController { JOptionPane.showMessageDialog(null, "Invalid server address", "Invalid server address", JOptionPane.ERROR_MESSAGE) return } - + + startChat(p) + } + + void startChat(Persona p) { if (!mvcGroup.getChildrenGroups().containsKey(p.getHumanReadableName())) { def params = [:] params['core'] = model.core @@ -460,7 +464,7 @@ class MainFrameController { mvcGroup.createMVCGroup("chat-server", p.getHumanReadableName(), params) } } - + void saveMuWireSettings() { core.saveMuSettings() } diff --git a/gui/griffon-app/controllers/com/muwire/gui/SearchTabController.groovy b/gui/griffon-app/controllers/com/muwire/gui/SearchTabController.groovy index faaea420..070640a0 100644 --- a/gui/griffon-app/controllers/com/muwire/gui/SearchTabController.groovy +++ b/gui/griffon-app/controllers/com/muwire/gui/SearchTabController.groovy @@ -106,6 +106,17 @@ class SearchTabController { mvcGroup.createMVCGroup("browse", groupId, params) } + + @ControllerAction + void chat() { + def sender = view.selectedSender() + if (sender == null) + return + + def parent = mvcGroup.parentGroup + parent.controller.startChat(sender) + parent.view.showChatWindow.call() + } @ControllerAction void showComment() { diff --git a/gui/griffon-app/models/com/muwire/gui/SearchTabModel.groovy b/gui/griffon-app/models/com/muwire/gui/SearchTabModel.groovy index 716dd218..6e7144aa 100644 --- a/gui/griffon-app/models/com/muwire/gui/SearchTabModel.groovy +++ b/gui/griffon-app/models/com/muwire/gui/SearchTabModel.groovy @@ -24,6 +24,7 @@ class SearchTabModel { @Observable boolean browseActionEnabled @Observable boolean viewCommentActionEnabled @Observable boolean viewCertificatesActionEnabled + @Observable boolean chatActionEnabled @Observable boolean groupedByFile Core core diff --git a/gui/griffon-app/views/com/muwire/gui/SearchTabView.groovy b/gui/griffon-app/views/com/muwire/gui/SearchTabView.groovy index 5ec148ca..c83e2e88 100644 --- a/gui/griffon-app/views/com/muwire/gui/SearchTabView.groovy +++ b/gui/griffon-app/views/com/muwire/gui/SearchTabView.groovy @@ -74,6 +74,7 @@ class SearchTabView { closureColumn(header : "Sender", preferredWidth : 500, type: String, read : {row -> row.getHumanReadableName()}) closureColumn(header : "Results", preferredWidth : 20, type: Integer, read : {row -> model.sendersBucket[row].size()}) closureColumn(header : "Browse", preferredWidth : 20, type: Boolean, read : {row -> model.sendersBucket[row].first().browse}) + closureColumn(header : "Chat", preferredWidth : 20, type : Boolean, read : {row -> model.sendersBucket[row].first().chat}) closureColumn(header : "Trust", preferredWidth : 50, type: String, read : { row -> model.core.trustService.getLevel(row.destination).toString() }) @@ -84,6 +85,7 @@ class SearchTabView { gridLayout(rows: 1, cols : 2) panel (border : etchedBorder()){ button(text : "Browse Host", enabled : bind {model.browseActionEnabled}, browseAction) + button(text : "Chat", enabled : bind{model.chatActionEnabled}, chatAction) } panel (border : etchedBorder()){ button(text : "Trust", enabled: bind {model.trustButtonsEnabled }, trustAction) @@ -154,6 +156,14 @@ class SearchTabView { } count }) + closureColumn(header : "Chat Hosts", preferredWidth : 20, type : Integer, read : { + int count = 0 + model.hashBucket[it].each { + if (it.chat) + count++ + } + count + }) } } } @@ -177,6 +187,7 @@ class SearchTabView { tableModel(list : model.senders2) { closureColumn(header : "Sender", preferredWidth : 350, type : String, read : {it.sender.getHumanReadableName()}) closureColumn(header : "Browse", preferredWidth : 20, type : Boolean, read : {it.browse}) + closureColumn(header : "Chat", preferredWidth : 20, type : Boolean, read : {it.chat}) closureColumn(header : "Comment", preferredWidth : 20, type : Boolean, read : {it.comment != null}) closureColumn(header : "Certificates", preferredWidth : 20, type: Integer, read : {it.certificates}) closureColumn(header : "Trust", preferredWidth : 50, type : String, read : { @@ -189,6 +200,7 @@ class SearchTabView { gridLayout(rows : 1, cols : 2) panel (border : etchedBorder()) { button(text : "Browse Host", enabled : bind {model.browseActionEnabled}, browseAction) + button(text : "Chat", enabled : bind{model.chatActionEnabled}, chatAction) button(text : "View Comment", enabled : bind {model.viewCommentActionEnabled}, showCommentAction) button(text : "View Certificates", enabled : bind {model.viewCertificatesActionEnabled}, viewCertificatesAction) } @@ -314,10 +326,12 @@ class SearchTabView { if (row < 0) { model.trustButtonsEnabled = false model.browseActionEnabled = false + model.chatActionEnabled = false return } else { Persona sender = model.senders[row] model.browseActionEnabled = model.sendersBucket[sender].first().browse + model.chatActionEnabled = model.sendersBucket[sender].first().chat model.trustButtonsEnabled = true model.results.clear() model.results.addAll(model.sendersBucket[sender]) @@ -337,6 +351,7 @@ class SearchTabView { if (e == null) { model.trustButtonsEnabled = false model.browseActionEnabled = false + model.chatActionEnabled = false model.viewCertificatesActionEnabled = false return } @@ -370,12 +385,14 @@ class SearchTabView { int row = selectedSenderRow() if (row < 0 || model.senders2[row] == null) { model.browseActionEnabled = false + model.chatActionEnabled = false model.viewCertificatesActionEnabled = false model.trustButtonsEnabled = false model.viewCommentActionEnabled = false return } model.browseActionEnabled = model.senders2[row].browse + model.chatActionEnabled = model.senders2[row].chat model.trustButtonsEnabled = true model.viewCommentActionEnabled = model.senders2[row].comment != null model.viewCertificatesActionEnabled = model.senders2[row].certificates > 0