From 722bc09aa614c2bc5212edb6785f9dc101777c0e Mon Sep 17 00:00:00 2001 From: Zlatin Balevsky Date: Tue, 22 Feb 2022 05:27:24 +0000 Subject: [PATCH] auto updates --- .../src/main/groovy/com/muwire/core/Core.groovy | 17 +++++++++++++++++ .../groovy/com/muwire/core/RestartEvent.groovy | 5 +++++ .../com/muwire/core/update/AutoUpdater.java | 7 +++++++ .../com/muwire/core/update/UpdateClient.groovy | 11 +++++++---- .../core/update/UpdateDownloadedEvent.groovy | 1 + .../com/muwire/gui/UpdateController.groovy | 7 ++++++- gui/griffon-app/i18n/messages.properties | 5 +++++ .../models/com/muwire/gui/MainFrameModel.groovy | 2 ++ .../models/com/muwire/gui/UpdateModel.groovy | 3 ++- .../views/com/muwire/gui/UpdateView.groovy | 9 +++------ 10 files changed, 55 insertions(+), 12 deletions(-) create mode 100644 core/src/main/groovy/com/muwire/core/RestartEvent.groovy create mode 100644 core/src/main/groovy/com/muwire/core/update/AutoUpdater.java diff --git a/core/src/main/groovy/com/muwire/core/Core.groovy b/core/src/main/groovy/com/muwire/core/Core.groovy index 2e76ef94..35a981c9 100644 --- a/core/src/main/groovy/com/muwire/core/Core.groovy +++ b/core/src/main/groovy/com/muwire/core/Core.groovy @@ -8,6 +8,8 @@ import com.muwire.core.files.directories.WatchedDirectoriesLoadedEvent import com.muwire.core.messenger.UIFolderCreateEvent import com.muwire.core.messenger.UIFolderDeleteEvent import com.muwire.core.messenger.UIMessageMovedEvent +import com.muwire.core.update.AutoUpdater +import com.muwire.core.update.UpdateDownloadedEvent import java.nio.charset.StandardCharsets import java.util.concurrent.atomic.AtomicBoolean @@ -530,6 +532,14 @@ public class Core { register(UIMessageMovedEvent.class, messenger) } + if (System.hasProperty("auto.updater.class")) { + String className = System.getProperty("auto.updater.class") + log.info("initializing auto-updater $className") + Class clazz = Class.forName(className)) + AutoUpdater autoUpdater = (AutoUpdater) clazz.newInstance() + autoUpdater.init(this) + } + File modulesProps = new File(home, "mwmodules.list") if (modulesProps.exists()) { log.info("loading modules") @@ -539,6 +549,13 @@ public class Core { modules.add(module) } } + + eventBus.register(RestartEvent.class, this) + } + + void onRestartEvent(RestartEvent event) { + shutdown() + System.exit(0) } public void startServices() { diff --git a/core/src/main/groovy/com/muwire/core/RestartEvent.groovy b/core/src/main/groovy/com/muwire/core/RestartEvent.groovy new file mode 100644 index 00000000..15c17be0 --- /dev/null +++ b/core/src/main/groovy/com/muwire/core/RestartEvent.groovy @@ -0,0 +1,5 @@ +package com.muwire.core + +/** triggers a restart of MuWire */ +class RestartEvent extends Event{ +} diff --git a/core/src/main/groovy/com/muwire/core/update/AutoUpdater.java b/core/src/main/groovy/com/muwire/core/update/AutoUpdater.java new file mode 100644 index 00000000..40a742f3 --- /dev/null +++ b/core/src/main/groovy/com/muwire/core/update/AutoUpdater.java @@ -0,0 +1,7 @@ +package com.muwire.core.update; + +import com.muwire.core.Core; + +public interface AutoUpdater { + void init(Core core); +} 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 ae5aff8d..7e4a8798 100644 --- a/core/src/main/groovy/com/muwire/core/update/UpdateClient.groovy +++ b/core/src/main/groovy/com/muwire/core/update/UpdateClient.groovy @@ -89,7 +89,8 @@ class UpdateClient { if (e.infoHash != updateInfoHash) return updateDownloading = false - eventBus.publish(new UpdateDownloadedEvent(version : version, signer : signer, text : text)) + eventBus.publish(new UpdateDownloadedEvent(version : version, signer : signer, + text : text, updateFile: e.downloadedFile.file)) } private void checkUpdate() { @@ -192,9 +193,11 @@ class UpdateClient { } 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, text : text)) - else { + if (fileManager.rootToFiles.containsKey(updateInfoHash)) { + File updateFile = fileManager.rootToFiles.get(updateInfoHash).first().file + eventBus.publish(new UpdateDownloadedEvent(version: payload.version, signer: payload.signer, + text : text, updateFile: updateFile)) + } else { updateDownloading = false version = payload.version signer = payload.signer 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 c9a9d1ac..5a983328 100644 --- a/core/src/main/groovy/com/muwire/core/update/UpdateDownloadedEvent.groovy +++ b/core/src/main/groovy/com/muwire/core/update/UpdateDownloadedEvent.groovy @@ -6,4 +6,5 @@ class UpdateDownloadedEvent extends Event { String version String signer String text + File updateFile } diff --git a/gui/griffon-app/controllers/com/muwire/gui/UpdateController.groovy b/gui/griffon-app/controllers/com/muwire/gui/UpdateController.groovy index c93917ae..87d210f1 100644 --- a/gui/griffon-app/controllers/com/muwire/gui/UpdateController.groovy +++ b/gui/griffon-app/controllers/com/muwire/gui/UpdateController.groovy @@ -1,5 +1,6 @@ package com.muwire.gui +import com.muwire.core.RestartEvent import griffon.core.artifact.GriffonController import griffon.core.controller.ControllerAction import griffon.inject.MVCMember @@ -16,7 +17,11 @@ class UpdateController { @ControllerAction void close() { view.dialog.setVisible(false) - mvcGroup.destroy() + } + + @ControllerAction + void restart() { + model.core.eventBus.publish(new RestartEvent()) } @ControllerAction diff --git a/gui/griffon-app/i18n/messages.properties b/gui/griffon-app/i18n/messages.properties index 25466f70..8b392037 100644 --- a/gui/griffon-app/i18n/messages.properties +++ b/gui/griffon-app/i18n/messages.properties @@ -495,6 +495,7 @@ EXIT_MUWIRE=Exit MuWire UPDATE_DOWNLOADED=Update Downloaded UPDATE_AVAILABLE=Update Available FIND=Find +RESTART=Restart ## Advanced Sharing tool DIRECTORIES_WATCHED_FOR_CHANGES=Folders watched for file changes @@ -826,5 +827,9 @@ TOOLTIP_RULE_RECORD=Record queries that match this rule TOOLTIP_RULE_DROP=Drop queries that match this rule TOOLTIP_RULE_BLOCK=Mark the sender of queries that match this rule as Distrusted +### Tooltips for update dialog +TOOLTIP_FIND=Search for the update +TOOLTIP_RESTART=Update and restart MuWire + ## Test string TEST_PLURALIZABLE_STRING={count, plural, one {You have {count} item.} other {You have {count} items.}} diff --git a/gui/griffon-app/models/com/muwire/gui/MainFrameModel.groovy b/gui/griffon-app/models/com/muwire/gui/MainFrameModel.groovy index 945f6354..dfd267bb 100644 --- a/gui/griffon-app/models/com/muwire/gui/MainFrameModel.groovy +++ b/gui/griffon-app/models/com/muwire/gui/MainFrameModel.groovy @@ -390,6 +390,7 @@ class MainFrameModel { void onUpdateDownloadedEvent(UpdateDownloadedEvent e) { runInsideUIAsync { Map args = new HashMap<>() + args['core'] = core args['available'] = null args['downloaded'] = e mvcGroup.createMVCGroup("update", "update", args) @@ -774,6 +775,7 @@ class MainFrameModel { void onUpdateAvailableEvent(UpdateAvailableEvent e) { runInsideUIAsync { Map args = new HashMap<>() + args['core'] = core 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 index b359cf4c..eb1278f1 100644 --- a/gui/griffon-app/models/com/muwire/gui/UpdateModel.groovy +++ b/gui/griffon-app/models/com/muwire/gui/UpdateModel.groovy @@ -1,14 +1,15 @@ package com.muwire.gui +import com.muwire.core.Core 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 { + Core core 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 index f809afd5..00ac18b1 100644 --- a/gui/griffon-app/views/com/muwire/gui/UpdateView.groovy +++ b/gui/griffon-app/views/com/muwire/gui/UpdateView.groovy @@ -41,7 +41,9 @@ class UpdateView { } panel (constraints : BorderLayout.SOUTH) { if (model.available != null) - button(text : trans("FIND"), searchAction) + button(text : trans("FIND"), toolTipText: trans("TOOLTIP_FIND"), searchAction) + else if (model.downloaded != null) + button(text : trans("RESTART"), toolTipText: trans("TOOLTIP_RESTART"), restartAction) button(text : trans("CLOSE"), closeAction) } } @@ -52,11 +54,6 @@ class UpdateView { 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