From 546eb4e9d3dd2482a6a1ebc198db290de315e65e Mon Sep 17 00:00:00 2001 From: Zlatin Balevsky Date: Mon, 17 Jun 2019 11:25:21 +0100 Subject: [PATCH] only allow one download per infohash from gui --- .../com/muwire/gui/MainFrameController.groovy | 8 +++-- .../com/muwire/gui/MainFrameModel.groovy | 10 ++++++- .../views/com/muwire/gui/MainFrameView.groovy | 6 ++-- .../views/com/muwire/gui/SearchTabView.groovy | 29 +++++++++++-------- 4 files changed, 35 insertions(+), 18 deletions(-) diff --git a/gui/griffon-app/controllers/com/muwire/gui/MainFrameController.groovy b/gui/griffon-app/controllers/com/muwire/gui/MainFrameController.groovy index e356841b..7102eff1 100644 --- a/gui/griffon-app/controllers/com/muwire/gui/MainFrameController.groovy +++ b/gui/griffon-app/controllers/com/muwire/gui/MainFrameController.groovy @@ -118,8 +118,11 @@ class MainFrameController { void download() { def result = selectedResult() if (result == null) - return // TODO disable button - + return + + if (!model.canDownload(result.infohash)) + return + def file = new File(application.context.get("muwire-settings").downloadLocation, result.name) def selected = builder.getVariable("result-tabs").getSelectedComponent() @@ -150,6 +153,7 @@ class MainFrameController { void cancel() { def downloader = model.downloads[selectedDownload()].downloader downloader.cancel() + model.downloadInfoHashes.remove(downloader.getInfoHash()) core.eventBus.publish(new UIDownloadCancelledEvent(downloader : downloader)) } diff --git a/gui/griffon-app/models/com/muwire/gui/MainFrameModel.groovy b/gui/griffon-app/models/com/muwire/gui/MainFrameModel.groovy index e16513a5..e50c3d38 100644 --- a/gui/griffon-app/models/com/muwire/gui/MainFrameModel.groovy +++ b/gui/griffon-app/models/com/muwire/gui/MainFrameModel.groovy @@ -60,11 +60,14 @@ class MainFrameModel { @Observable int connections @Observable String me - @Observable boolean searchButtonsEnabled + @Observable boolean downloadActionEnabled + @Observable boolean trustButtonsEnabled @Observable boolean cancelButtonEnabled @Observable boolean retryButtonEnabled private final Set infoHashes = new HashSet<>() + + private final Set downloadInfoHashes = new HashSet<>() volatile Core core @@ -171,6 +174,7 @@ class MainFrameModel { void onDownloadStartedEvent(DownloadStartedEvent e) { runInsideUIAsync { downloads << e + downloadInfoHashes.add(e.downloader.infoHash) } } @@ -340,4 +344,8 @@ class MainFrameModel { return destination == other.destination } } + + boolean canDownload(InfoHash hash) { + !downloadInfoHashes.contains(hash) + } } \ No newline at end of file diff --git a/gui/griffon-app/views/com/muwire/gui/MainFrameView.groovy b/gui/griffon-app/views/com/muwire/gui/MainFrameView.groovy index 35bfd263..3a5b941d 100644 --- a/gui/griffon-app/views/com/muwire/gui/MainFrameView.groovy +++ b/gui/griffon-app/views/com/muwire/gui/MainFrameView.groovy @@ -105,9 +105,9 @@ class MainFrameView { borderLayout() tabbedPane(id : "result-tabs", constraints: BorderLayout.CENTER) panel(constraints : BorderLayout.SOUTH) { - button(text : "Download", enabled : bind {model.searchButtonsEnabled}, downloadAction) - button(text : "Trust", enabled: bind {model.searchButtonsEnabled }, trustAction) - button(text : "Distrust", enabled : bind {model.searchButtonsEnabled}, distrustAction) + button(text : "Download", enabled : bind {model.downloadActionEnabled}, downloadAction) + button(text : "Trust", enabled: bind {model.trustButtonsEnabled }, trustAction) + button(text : "Distrust", enabled : bind {model.trustButtonsEnabled}, distrustAction) } } panel (constraints : JSplitPane.BOTTOM) { diff --git a/gui/griffon-app/views/com/muwire/gui/SearchTabView.groovy b/gui/griffon-app/views/com/muwire/gui/SearchTabView.groovy index ecff7391..0ee3985e 100644 --- a/gui/griffon-app/views/com/muwire/gui/SearchTabView.groovy +++ b/gui/griffon-app/views/com/muwire/gui/SearchTabView.groovy @@ -66,7 +66,11 @@ class SearchTabView { def selectionModel = resultsTable.getSelectionModel() selectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION) selectionModel.addListSelectionListener( { - mvcGroup.parentGroup.model.searchButtonsEnabled = true + mvcGroup.parentGroup.model.trustButtonsEnabled = true + int row = resultsTable.getSelectedRow() + if (lastSortEvent != null) + row = resultsTable.rowSorter.convertRowIndexToModel(row) + mvcGroup.parentGroup.model.downloadActionEnabled = mvcGroup.parentGroup.model.canDownload(model.results[row].infohash) }) } } @@ -105,25 +109,18 @@ class SearchTabView { resultsTable.rowSorter.setSortsOnUpdates(true) - JPopupMenu menu = new JPopupMenu() - JMenuItem download = new JMenuItem("Download") - download.addActionListener({mvcGroup.parentGroup.controller.download()}) - menu.add(download) - JMenuItem copyHashToClipboard = new JMenuItem("Copy hash to clipboard") - copyHashToClipboard.addActionListener({mvcGroup.view.copyHashToClipboard()}) - menu.add(copyHashToClipboard) resultsTable.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { if (e.button == MouseEvent.BUTTON3) - showPopupMenu(menu, e) + showPopupMenu(e) else if (e.button == MouseEvent.BUTTON1 && e.clickCount == 2) mvcGroup.parentGroup.controller.download() } @Override public void mouseReleased(MouseEvent e) { if (e.button == MouseEvent.BUTTON3) - showPopupMenu(menu, e) + showPopupMenu(e) } }) } @@ -135,8 +132,16 @@ class SearchTabView { mvcGroup.destroy() } - def showPopupMenu(JPopupMenu menu, MouseEvent e) { - println "showing popup menu" + def showPopupMenu(MouseEvent e) { + JPopupMenu menu = new JPopupMenu() + if (mvcGroup.parentGroup.model.downloadActionEnabled) { + JMenuItem download = new JMenuItem("Download") + download.addActionListener({mvcGroup.parentGroup.controller.download()}) + menu.add(download) + } + JMenuItem copyHashToClipboard = new JMenuItem("Copy hash to clipboard") + copyHashToClipboard.addActionListener({mvcGroup.view.copyHashToClipboard()}) + menu.add(copyHashToClipboard) menu.show(e.getComponent(), e.getX(), e.getY()) }