diff --git a/cli-lanterna/src/main/groovy/com/muwire/clilanterna/TrustListModel.groovy b/cli-lanterna/src/main/groovy/com/muwire/clilanterna/TrustListModel.groovy new file mode 100644 index 00000000..f3b85935 --- /dev/null +++ b/cli-lanterna/src/main/groovy/com/muwire/clilanterna/TrustListModel.groovy @@ -0,0 +1,49 @@ +package com.muwire.clilanterna + +import com.googlecode.lanterna.gui2.TextGUIThread +import com.googlecode.lanterna.gui2.table.TableModel +import com.muwire.core.Core +import com.muwire.core.trust.RemoteTrustList +import com.muwire.core.trust.TrustEvent + +class TrustListModel { + private final TextGUIThread guiThread + private final RemoteTrustList trustList + private final Core core + private final TableModel trustedTableModel, distrustedTableModel + + TrustListModel(RemoteTrustList trustList, Core core) { + this.trustList = trustList + this.core = core + + trustedTableModel = new TableModel("Trusted User","Your Trust") + distrustedTableModel = new TableModel("Distrusted User", "Your Trust") + refreshModels() + + core.eventBus.register(TrustEvent.class, this) + } + + void onTrustEvent(TrustEvent e) { + guiThread.invokeLater { + refreshModels() + } + } + + private void refreshModels() { + int trustRows = trustedTableModel.getRowCount() + trustRows.times { trustedTableModel.removeRow(0) } + int distrustRows = distrustedTableModel.getRowCount() + distrustRows.times { distrustedTableModel.removeRow(0) } + + trustList.good.each { + trustedTableModel.addRow(new PersonaWrapper(it), core.trustService.getLevel(it.destination)) + } + trustList.bad.each { + distrustedTableModel.addRow(new PersonaWrapper(it), core.trustService.getLevel(it.destination)) + } + } + + void unregister() { + core.eventBus.unregister(TrustEvent.class, this) + } +} diff --git a/cli-lanterna/src/main/groovy/com/muwire/clilanterna/TrustListView.groovy b/cli-lanterna/src/main/groovy/com/muwire/clilanterna/TrustListView.groovy new file mode 100644 index 00000000..eab077a9 --- /dev/null +++ b/cli-lanterna/src/main/groovy/com/muwire/clilanterna/TrustListView.groovy @@ -0,0 +1,118 @@ +package com.muwire.clilanterna + +import com.googlecode.lanterna.TerminalSize +import com.googlecode.lanterna.gui2.BasicWindow +import com.googlecode.lanterna.gui2.Button +import com.googlecode.lanterna.gui2.GridLayout +import com.googlecode.lanterna.gui2.GridLayout.Alignment +import com.googlecode.lanterna.gui2.Label +import com.googlecode.lanterna.gui2.LayoutData +import com.googlecode.lanterna.gui2.Panel +import com.googlecode.lanterna.gui2.TextGUI +import com.googlecode.lanterna.gui2.Window +import com.googlecode.lanterna.gui2.dialogs.MessageDialog +import com.googlecode.lanterna.gui2.dialogs.MessageDialogButton +import com.googlecode.lanterna.gui2.table.Table +import com.muwire.core.Core +import com.muwire.core.Persona +import com.muwire.core.trust.TrustEvent +import com.muwire.core.trust.TrustLevel + +class TrustListView extends BasicWindow { + private final TrustListModel model + private final TextGUI textGUI + private final Core core + private final TerminalSize terminalSize + private final Table trusted, distrusted + + TrustListView(TrustListModel model, TextGUI textGUI, Core core, TerminalSize terminalSize) { + this.model = model + this.textGUI = textGUI + this.core = core + this.terminalSize = terminalSize + + int tableSize = terminalSize.getRows() - 10 + + setHints([Window.Hint.EXPANDED]) + + Panel contentPanel = new Panel() + contentPanel.setLayoutManager(new GridLayout(1)) + LayoutData layoutData = GridLayout.createLayoutData(Alignment.CENTER, Alignment.CENTER) + + Label nameLabel = new Label("Trust list for "+model.trustList.persona.getHumanReadableName()) + Label lastUpdatedLabel = new Label("Last updated "+new Date(model.trustList.timestamp)) + contentPanel.addComponent(nameLabel, layoutData) + contentPanel.addComponent(lastUpdatedLabel, layoutData) + + + Panel topPanel = new Panel() + topPanel.setLayoutManager(new GridLayout(2)) + + trusted = new Table("Trusted User","Your Trust") + trusted.with { + setCellSelection(false) + setTableModel(model.trustedTableModel) + setVisibleRows(tableSize) + } + trusted.setSelectAction({ actionsForUser(true) }) + topPanel.addComponent(trusted, layoutData) + + distrusted = new Table("Distrusted User", "Your Trust") + distrusted.with { + setCellSelection(false) + setTableModel(model.distrustedTableModel) + setVisibleRows(tableSize) + } + distrusted.setSelectAction({actionsForUser(false)}) + topPanel.addComponent(distrusted, layoutData) + + Button closeButton = new Button("Close",{close()}) + + contentPanel.addComponent(topPanel, layoutData) + contentPanel.addComponent(closeButton, layoutData) + + setComponent(contentPanel) + } + + private void actionsForUser(boolean trustedUser) { + def table = trustedUser ? trusted : distrusted + def model = trustedUser ? model.trustedTableModel : model.distrustedTableModel + + int selectedRow = table.getSelectedRow() + def row = model.getRow(selectedRow) + + Persona persona = row[0].persona + + Window prompt = new BasicWindow("Actions for "+persona.getHumanReadableName()) + prompt.setHints([Window.Hint.CENTERED]) + Panel contentPanel = new Panel() + contentPanel.setLayoutManager(new GridLayout(4)) + LayoutData layoutData = GridLayout.createLayoutData(Alignment.CENTER, Alignment.CENTER) + + Button trustButton = new Button("Trust",{ + core.eventBus.publish(new TrustEvent(persona : persona, level : TrustLevel.TRUSTED)) + MessageDialog.showMessageDialog(textGUI, "Marked Trusted", persona.getHumanReadableName() + "has been marked trusted", + MessageDialogButton.OK) + }) + Button neutralButton = new Button("Neutral",{ + core.eventBus.publish(new TrustEvent(persona : persona, level : TrustLevel.NEUTRAL)) + MessageDialog.showMessageDialog(textGUI, "Marked Neutral", persona.getHumanReadableName() + "has been marked neutral", + MessageDialogButton.OK) + }) + Button distrustButton = new Button("Distrust",{ + core.eventBus.publish(new TrustEvent(persona : persona, level : TrustLevel.DISTRUSTED)) + MessageDialog.showMessageDialog(textGUI, "Marked Distrusted", persona.getHumanReadableName() + "has been marked distrusted", + MessageDialogButton.OK) + }) + Button closeButton = new Button("Close",{prompt.close()}) + + contentPanel.with { + addComponent(trustButton,layoutData) + addComponent(neutralButton, layoutData) + addComponent(distrustButton, layoutData) + addComponent(closeButton, layoutData) + } + prompt.setComponent(contentPanel) + textGUI.addWindowAndWait(prompt) + } +} diff --git a/cli-lanterna/src/main/groovy/com/muwire/clilanterna/TrustView.groovy b/cli-lanterna/src/main/groovy/com/muwire/clilanterna/TrustView.groovy index 83e54fae..804023fd 100644 --- a/cli-lanterna/src/main/groovy/com/muwire/clilanterna/TrustView.groovy +++ b/cli-lanterna/src/main/groovy/com/muwire/clilanterna/TrustView.groovy @@ -51,7 +51,7 @@ class TrustView extends BasicWindow { trusted.setVisibleRows(tableSize) topPanel.addComponent(trusted, layoutData) - distrusted = new Table("distrusted users") + distrusted = new Table("Distrusted users") distrusted.setCellSelection(false) distrusted.setSelectAction({distrustedActions()}) distrusted.setTableModel(model.modelDistrusted) @@ -156,7 +156,8 @@ class TrustView extends BasicWindow { int selectedRow = subscriptions.getSelectedRow() def row = model.modelSubscriptions.getRow(selectedRow) - Persona persona = row[0].trustList.persona + def trustList = row[0].trustList + Persona persona = trustList.persona Window prompt = new BasicWindow("Trust List Actions") prompt.setHints([Window.Hint.CENTERED]) @@ -164,7 +165,7 @@ class TrustView extends BasicWindow { contentPanel.setLayoutManager(new GridLayout(4)) LayoutData layoutData = GridLayout.createLayoutData(Alignment.CENTER, Alignment.CENTER) - Button reviewButton = new Button("Review",{}) // TODO + Button reviewButton = new Button("Review",{review(trustList)}) Button updateButton = new Button("Update",{ core.eventBus.publish(new TrustSubscriptionEvent(persona : persona, subscribe : true)) MessageDialog.showMessageDialog(textGUI, "Updating...", "Trust list will update soon", MessageDialogButton.OK) @@ -189,6 +190,13 @@ class TrustView extends BasicWindow { textGUI.addWindowAndWait(prompt) } + private void review(def trustList) { + TrustListModel model = new TrustListModel(trustList, core) + TrustListView view = new TrustListView(model, textGUI, core, terminalSize) + textGUI.addWindowAndWait(view) + model.unregister() + } + private void saveMuSettings() { File settingsFile = new File(core.home,"MuWire.properties") settingsFile.withOutputStream { core.muOptions.write(it) }