From 2429bbf59e876a0f32de9b08ee96e5d61c9bd2b6 Mon Sep 17 00:00:00 2001 From: Zlatin Balevsky Date: Thu, 31 Oct 2019 20:51:09 +0000 Subject: [PATCH] Add update notification window --- .../core/update/UpdateAvailableEvent.groovy | 1 + .../muwire/core/update/UpdateClient.groovy | 11 ++-- .../core/update/UpdateDownloadedEvent.groovy | 1 + gui/griffon-app/conf/Config.groovy | 5 ++ .../com/muwire/gui/UpdateController.groovy | 27 ++++++++ .../com/muwire/gui/MainFrameModel.groovy | 18 +++--- .../models/com/muwire/gui/UpdateModel.groovy | 14 +++++ .../views/com/muwire/gui/UpdateView.groovy | 61 +++++++++++++++++++ 8 files changed, 125 insertions(+), 13 deletions(-) create mode 100644 gui/griffon-app/controllers/com/muwire/gui/UpdateController.groovy create mode 100644 gui/griffon-app/models/com/muwire/gui/UpdateModel.groovy create mode 100644 gui/griffon-app/views/com/muwire/gui/UpdateView.groovy diff --git a/core/src/main/groovy/com/muwire/core/update/UpdateAvailableEvent.groovy b/core/src/main/groovy/com/muwire/core/update/UpdateAvailableEvent.groovy index d66a10ae..530af335 100644 --- a/core/src/main/groovy/com/muwire/core/update/UpdateAvailableEvent.groovy +++ b/core/src/main/groovy/com/muwire/core/update/UpdateAvailableEvent.groovy @@ -7,4 +7,5 @@ class UpdateAvailableEvent extends Event { String version String signer String infoHash + String text } diff --git a/core/src/main/groovy/com/muwire/core/update/UpdateClient.groovy b/core/src/main/groovy/com/muwire/core/update/UpdateClient.groovy index 8a6727f8..f55f2590 100644 --- a/core/src/main/groovy/com/muwire/core/update/UpdateClient.groovy +++ b/core/src/main/groovy/com/muwire/core/update/UpdateClient.groovy @@ -40,6 +40,8 @@ class UpdateClient { private volatile InfoHash updateInfoHash private volatile String version, signer private volatile boolean updateDownloading + + private volatile String text UpdateClient(EventBus eventBus, I2PSession session, String myVersion, MuWireSettings settings, FileManager fileManager, Persona me) { this.eventBus = eventBus @@ -75,7 +77,7 @@ class UpdateClient { if (e.downloadedFile.infoHash != updateInfoHash) return updateDownloading = false - eventBus.publish(new UpdateDownloadedEvent(version : version, signer : signer)) + eventBus.publish(new UpdateDownloadedEvent(version : version, signer : signer, text : text)) } private void checkUpdate() { @@ -147,15 +149,16 @@ class UpdateClient { } else infoHash = payload[settings.updateType] - + text = payload.text + if (!settings.autoDownloadUpdate) { log.info("new version $payload.version available, publishing event") - eventBus.publish(new UpdateAvailableEvent(version : payload.version, signer : payload.signer, infoHash : infoHash)) + eventBus.publish(new UpdateAvailableEvent(version : payload.version, signer : payload.signer, infoHash : infoHash, text : text)) } else { log.info("new version $payload.version available") updateInfoHash = new InfoHash(Base64.decode(infoHash)) if (fileManager.rootToFiles.containsKey(updateInfoHash)) - eventBus.publish(new UpdateDownloadedEvent(version : payload.version, signer : payload.signer)) + eventBus.publish(new UpdateDownloadedEvent(version : payload.version, signer : payload.signer, text : text)) else { updateDownloading = false version = payload.version diff --git a/core/src/main/groovy/com/muwire/core/update/UpdateDownloadedEvent.groovy b/core/src/main/groovy/com/muwire/core/update/UpdateDownloadedEvent.groovy index aae9ca08..c9a9d1ac 100644 --- a/core/src/main/groovy/com/muwire/core/update/UpdateDownloadedEvent.groovy +++ b/core/src/main/groovy/com/muwire/core/update/UpdateDownloadedEvent.groovy @@ -5,4 +5,5 @@ import com.muwire.core.Event class UpdateDownloadedEvent extends Event { String version String signer + String text } diff --git a/gui/griffon-app/conf/Config.groovy b/gui/griffon-app/conf/Config.groovy index f6965e9c..54c6015d 100644 --- a/gui/griffon-app/conf/Config.groovy +++ b/gui/griffon-app/conf/Config.groovy @@ -76,4 +76,9 @@ mvcGroups { view = 'com.muwire.gui.CloseWarningView' controller = 'com.muwire.gui.CloseWarningController' } + 'update' { + model = 'com.muwire.gui.UpdateModel' + view = 'com.muwire.gui.UpdateView' + controller = 'com.muwire.gui.UpdateController' + } } diff --git a/gui/griffon-app/controllers/com/muwire/gui/UpdateController.groovy b/gui/griffon-app/controllers/com/muwire/gui/UpdateController.groovy new file mode 100644 index 00000000..c93917ae --- /dev/null +++ b/gui/griffon-app/controllers/com/muwire/gui/UpdateController.groovy @@ -0,0 +1,27 @@ +package com.muwire.gui + +import griffon.core.artifact.GriffonController +import griffon.core.controller.ControllerAction +import griffon.inject.MVCMember +import griffon.metadata.ArtifactProviderFor +import javax.annotation.Nonnull + +@ArtifactProviderFor(GriffonController) +class UpdateController { + @MVCMember @Nonnull + UpdateView view + @MVCMember @Nonnull + UpdateModel model + + @ControllerAction + void close() { + view.dialog.setVisible(false) + mvcGroup.destroy() + } + + @ControllerAction + void search() { + mvcGroup.parentGroup.controller.search(model.available.infoHash, "MuWire update") + close() + } +} \ No newline at end of file diff --git a/gui/griffon-app/models/com/muwire/gui/MainFrameModel.groovy b/gui/griffon-app/models/com/muwire/gui/MainFrameModel.groovy index a9484043..6c58a6af 100644 --- a/gui/griffon-app/models/com/muwire/gui/MainFrameModel.groovy +++ b/gui/griffon-app/models/com/muwire/gui/MainFrameModel.groovy @@ -1,3 +1,4 @@ + package com.muwire.gui import java.util.concurrent.ConcurrentHashMap @@ -264,8 +265,10 @@ class MainFrameModel { void onUpdateDownloadedEvent(UpdateDownloadedEvent e) { runInsideUIAsync { - JOptionPane.showMessageDialog(null, "MuWire $e.version has been downloaded. You can update now", - "Update Downloaded", JOptionPane.INFORMATION_MESSAGE) + Map args = new HashMap<>() + args['available'] = null + args['downloaded'] = e + mvcGroup.createMVCGroup("update", "update", args) } } @@ -529,13 +532,10 @@ class MainFrameModel { void onUpdateAvailableEvent(UpdateAvailableEvent e) { runInsideUIAsync { - - int option = JOptionPane.showConfirmDialog(null, - "MuWire $e.version is available from $e.signer. You have "+ metadata["application.version"]+" Update?", - "New MuWire version availble", JOptionPane.OK_CANCEL_OPTION) - if (option == JOptionPane.CANCEL_OPTION) - return - controller.search(e.infoHash,"MuWire update") + Map args = new HashMap<>() + args['available'] = e + args['downloaded'] = null + mvcGroup.createMVCGroup("update", "update", args) } } diff --git a/gui/griffon-app/models/com/muwire/gui/UpdateModel.groovy b/gui/griffon-app/models/com/muwire/gui/UpdateModel.groovy new file mode 100644 index 00000000..b359cf4c --- /dev/null +++ b/gui/griffon-app/models/com/muwire/gui/UpdateModel.groovy @@ -0,0 +1,14 @@ +package com.muwire.gui + +import com.muwire.core.update.UpdateAvailableEvent +import com.muwire.core.update.UpdateDownloadedEvent + +import griffon.core.artifact.GriffonModel +import griffon.transform.Observable +import griffon.metadata.ArtifactProviderFor + +@ArtifactProviderFor(GriffonModel) +class UpdateModel { + UpdateAvailableEvent available + UpdateDownloadedEvent downloaded +} \ No newline at end of file diff --git a/gui/griffon-app/views/com/muwire/gui/UpdateView.groovy b/gui/griffon-app/views/com/muwire/gui/UpdateView.groovy new file mode 100644 index 00000000..bcdd6e01 --- /dev/null +++ b/gui/griffon-app/views/com/muwire/gui/UpdateView.groovy @@ -0,0 +1,61 @@ +package com.muwire.gui + +import griffon.core.artifact.GriffonView +import griffon.inject.MVCMember +import griffon.metadata.ArtifactProviderFor + +import javax.swing.JDialog +import javax.swing.SwingConstants + +import java.awt.BorderLayout +import java.awt.event.WindowAdapter +import java.awt.event.WindowEvent + +import javax.annotation.Nonnull + +@ArtifactProviderFor(GriffonView) +class UpdateView { + @MVCMember @Nonnull + FactoryBuilderSupport builder + @MVCMember @Nonnull + UpdateModel model + + def mainFrame + def dialog + def p + + void initUI() { + mainFrame = application.windowManager.findWindow("main-frame") + String title = model.downloaded != null ? "Update Downloaded" : "Update Available" + dialog = new JDialog(mainFrame, title, true) + dialog.setResizable(true) + + p = builder.panel { + borderLayout() + panel (constraints : BorderLayout.CENTER) { + scrollPane { + def text = model.downloaded != null ? model.downloaded.text : model.available.text + textArea(text : text, rows : 20, columns : 50, editable : false, lineWrap : true, wrapStyleWord : true) + } + } + panel (constraints : BorderLayout.SOUTH) { + if (model.available != null) + button(text : "Find", searchAction) + button(text : "Close", closeAction) + } + } + } + + void mvcGroupInit(Map args) { + dialog.getContentPane().add(p) + dialog.pack() + dialog.setLocationRelativeTo(mainFrame) + dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE) + dialog.addWindowListener( new WindowAdapter() { + public void windowClosed(WindowEvent e) { + mvcGroup.destroy() + } + }) + dialog.show() + } +} \ No newline at end of file