From fd3f55ab4daeba600949f2964dcdbc2ee758f08c Mon Sep 17 00:00:00 2001 From: Zlatin Balevsky Date: Sun, 3 Nov 2019 10:06:55 +0000 Subject: [PATCH] implement restore session --- .../com/muwire/gui/MainFrameController.groovy | 29 ++++++++--- .../com/muwire/gui/MainFrameModel.groovy | 4 ++ .../views/com/muwire/gui/MainFrameView.groovy | 49 +++++++++++++++++-- .../groovy/com/muwire/gui/UISettings.groovy | 3 ++ 4 files changed, 74 insertions(+), 11 deletions(-) diff --git a/gui/griffon-app/controllers/com/muwire/gui/MainFrameController.groovy b/gui/griffon-app/controllers/com/muwire/gui/MainFrameController.groovy index 692c3920..30e5e9c7 100644 --- a/gui/griffon-app/controllers/com/muwire/gui/MainFrameController.groovy +++ b/gui/griffon-app/controllers/com/muwire/gui/MainFrameController.groovy @@ -53,7 +53,7 @@ class MainFrameController { @ControllerAction void search(ActionEvent evt) { - if (evt.getActionCommand() == null) + if (evt?.getActionCommand() == null) return def cardsPanel = builder.getVariable("cards-panel") cardsPanel.getLayout().show(cardsPanel, "search window") @@ -61,6 +61,12 @@ class MainFrameController { def searchField = builder.getVariable("search-field") def search = searchField.getSelectedItem() searchField.model.addElement(search) + performSearch(search) + } + + private void performSearch(String search) { + + model.sessionRestored = true search = search.trim() if (search.length() == 0) @@ -98,16 +104,17 @@ class MainFrameController { def nonEmpty = [] terms.each { if (it.length() > 0) nonEmpty << it } payload = String.join(" ",nonEmpty).getBytes(StandardCharsets.UTF_8) - searchEvent = new SearchEvent(searchTerms : nonEmpty, uuid : uuid, oobInfohash: true, - searchComments : core.muOptions.searchComments, compressedResults : true) + searchEvent = new SearchEvent(searchTerms : nonEmpty, uuid : uuid, oobInfohash: true, + searchComments : core.muOptions.searchComments, compressedResults : true) } boolean firstHop = core.muOptions.allowUntrusted || core.muOptions.searchExtraHop - + Signature sig = DSAEngine.getInstance().sign(payload, core.spk) - + core.eventBus.publish(new QueryEvent(searchEvent : searchEvent, firstHop : firstHop, - replyTo: core.me.destination, receivedOn: core.me.destination, - originator : core.me, sig : sig.data)) + replyTo: core.me.destination, receivedOn: core.me.destination, + originator : core.me, sig : sig.data)) + } void search(String infoHash, String tabTitle) { @@ -313,6 +320,14 @@ class MainFrameController { void clearUploads() { model.uploads.removeAll { it.finished } } + + @ControllerAction + void restoreSession() { + model.sessionRestored = true + view.settings.openTabs.each { + performSearch(it) + } + } void saveMuWireSettings() { core.saveMuSettings() diff --git a/gui/griffon-app/models/com/muwire/gui/MainFrameModel.groovy b/gui/griffon-app/models/com/muwire/gui/MainFrameModel.groovy index 4b3a665d..74345c30 100644 --- a/gui/griffon-app/models/com/muwire/gui/MainFrameModel.groovy +++ b/gui/griffon-app/models/com/muwire/gui/MainFrameModel.groovy @@ -88,6 +88,8 @@ class MainFrameModel { def trusted = [] def distrusted = [] def subscriptions = [] + + boolean sessionRestored @Observable int connections @Observable String me @@ -295,6 +297,8 @@ class MainFrameModel { runInsideUIAsync { connections = core.connectionManager.getConnections().size() + view.showRestoreOrEmpty() + if (connections > 0) { def topPanel = builder.getVariable("top-panel") topPanel.getLayout().show(topPanel, "top-search-panel") diff --git a/gui/griffon-app/views/com/muwire/gui/MainFrameView.groovy b/gui/griffon-app/views/com/muwire/gui/MainFrameView.groovy index 4c5989bf..856328c0 100644 --- a/gui/griffon-app/views/com/muwire/gui/MainFrameView.groovy +++ b/gui/griffon-app/views/com/muwire/gui/MainFrameView.groovy @@ -73,6 +73,7 @@ class MainFrameView { def lastSharedSortEvent def trustTablesSortEvents = [:] def expansionListener = new TreeExpansions() + UISettings settings @@ -159,9 +160,23 @@ class MainFrameView { } panel (id: "cards-panel", constraints : BorderLayout.CENTER) { cardLayout() - panel (constraints : "search window") { - borderLayout() - tabbedPane(id : "result-tabs", constraints: BorderLayout.CENTER) + panel (id : "search window", constraints : "search window") { + cardLayout() + panel (constraints : "tabs-panel") { + borderLayout() + tabbedPane(id : "result-tabs", constraints: BorderLayout.CENTER) + } + panel(constraints : "restore session") { + borderLayout() + panel (constraints : BorderLayout.CENTER) { + gridBagLayout() + label(text : "Saved Tabs:", constraints : gbc(gridx : 0, gridy : 0)) + scrollPane (constraints : gbc(gridx : 0, gridy : 1)) { + list(items : new ArrayList(settings.openTabs)) + } + button(text : "Restore Session", constraints : gbc(gridx :0, gridy : 2), restoreSessionAction) + } + } } panel (constraints: "downloads window") { gridLayout(rows : 1, cols : 1) @@ -725,6 +740,9 @@ class MainFrameView { // show tree by default showSharedFilesTree.call() + + // show search panel by default + showSearchWindow.call() } private static void showPopupMenu(JPopupMenu menu, MouseEvent event) { @@ -869,10 +887,23 @@ class MainFrameView { showPopupMenu(menu, e) } + + void showRestoreOrEmpty() { + def searchWindow = builder.getVariable("search window") + String id + if (!model.sessionRestored && !settings.openTabs.isEmpty()) + id = model.connections > 0 ? "restore session" : "tabs-panel" + else + id = "tabs-panel" + searchWindow.getLayout().show(searchWindow, id) + } def showSearchWindow = { def cardsPanel = builder.getVariable("cards-panel") cardsPanel.getLayout().show(cardsPanel, "search window") + + showRestoreOrEmpty() + model.searchesPaneButtonEnabled = false model.downloadsPaneButtonEnabled = true model.uploadsPaneButtonEnabled = true @@ -971,10 +1002,20 @@ class MainFrameView { } private void closeApplication() { + Core core = application.getContext().get("core") + + def tabbedPane = builder.getVariable("result-tabs") + settings.openTabs.clear() + int count = tabbedPane.getTabCount() + for (int i = 0; i < count; i++) + settings.openTabs.add(tabbedPane.getTitleAt(i)) + + File uiPropsFile = new File(core.home, "gui.properties") + uiPropsFile.withOutputStream { settings.write(it) } + def mainFrame = builder.getVariable("main-frame") mainFrame.setVisible(false) application.getWindowManager().findWindow("shutdown-window").setVisible(true) - Core core = application.getContext().get("core") if (core != null) { Thread t = new Thread({ core.shutdown() diff --git a/gui/src/main/groovy/com/muwire/gui/UISettings.groovy b/gui/src/main/groovy/com/muwire/gui/UISettings.groovy index 6e8343f8..a7c2304b 100644 --- a/gui/src/main/groovy/com/muwire/gui/UISettings.groovy +++ b/gui/src/main/groovy/com/muwire/gui/UISettings.groovy @@ -18,6 +18,7 @@ class UISettings { boolean clearUploads boolean storeSearchHistory Set searchHistory + Set openTabs UISettings(Properties props) { lnf = props.getProperty("lnf", "system") @@ -35,6 +36,7 @@ class UISettings { storeSearchHistory = Boolean.parseBoolean(props.getProperty("storeSearchHistory","true")) searchHistory = DataUtil.readEncodedSet(props, "searchHistory") + openTabs = DataUtil.readEncodedSet(props, "openTabs") } void write(OutputStream out) throws IOException { @@ -55,6 +57,7 @@ class UISettings { props.setProperty("font", font) DataUtil.writeEncodedSet(searchHistory, "searchHistory", props) + DataUtil.writeEncodedSet(openTabs, "openTabs", props) props.store(out, "UI Properties") }