show warning when unsharing files in collections

pull/53/head
Zlatin Balevsky 2020-11-03 10:00:22 +00:00
parent 71c53ef643
commit 033d3bc552
No known key found for this signature in database
GPG Key ID: A72832072D525E41
7 changed files with 160 additions and 4 deletions

View File

@ -161,4 +161,9 @@ mvcGroups {
view = 'com.muwire.gui.CollectionTabView'
controller = 'com.muwire.gui.CollectionTabController'
}
'collection-warning' {
model = "com.muwire.gui.CollectionWarningModel"
view = "com.muwire.gui.CollectionWarningView"
controller = "com.muwire.gui.CollectionWarningController"
}
}

View File

@ -0,0 +1,36 @@
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 CollectionWarningController {
@MVCMember @Nonnull
CollectionWarningModel model
@MVCMember @Nonnull
CollectionWarningView view
@ControllerAction
void unshare() {
model.answer[0] = true
if (view.rememberCheckbox.model.isSelected()) {
model.settings.collectionWarning = false
File propsFile = new File(model.home, "gui.properties")
propsFile.withOutputStream {
model.settings.write(it)
}
}
cancel()
}
@ControllerAction
void cancel() {
view.dialog.setVisible(false)
mvcGroup.destroy()
}
}

View File

@ -403,11 +403,31 @@ class MainFrameController {
}
void unshareSelectedFile() {
def sf = view.selectedSharedFiles()
if (sf == null)
def sfs = view.selectedSharedFiles()
if (sfs == null)
return
sf.each {
core.eventBus.publish(new FileUnsharedEvent(unsharedFile : it))
sfs.each { SharedFile sf ->
if (view.settings.collectionWarning) {
Set<InfoHash> collectionsInfoHashes = core.collectionManager.collectionsForFile(new InfoHash(sf.root))
if (collectionsInfoHashes != null) {
String[] affected = collectionsInfoHashes.collect({core.collectionManager.getByInfoHash(it)}).collect{it.name}.toArray(new String[0])
boolean [] answer = new boolean[1]
def props = [:]
props.collections = affected
props.answer = answer
props.fileName = sf.file.getName()
props.settings = view.settings
props.home = core.home
def mvc = mvcGroup.createMVCGroup("collection-warning", props)
mvc.destroy()
if (!answer[0])
return
}
}
core.eventBus.publish(new FileUnsharedEvent(unsharedFile : sf))
}
}

View File

@ -555,3 +555,9 @@ COLLECTION_VIEWS=Collection views
COLLECTION_SELECT=Select a collection to view it's description
DESCRIPTION=Description
COLLECTION_DOWNLOAD=Download Full Collection
## Collections warning dialog
COLLECTION_WARNING_TITLE=Collections will be deleted
COLLECTION_WARNING_BODY1=The file {0} is part of one or more collections.
COLLECTION_WARNING_BODY2=If you unshare this file the following collections will be deleted.
UNSHARE=Unshare

View File

@ -0,0 +1,16 @@
package com.muwire.gui
import com.muwire.core.collections.FileCollection
import griffon.core.artifact.GriffonModel
import griffon.transform.Observable
import griffon.metadata.ArtifactProviderFor
@ArtifactProviderFor(GriffonModel)
class CollectionWarningModel {
String[] collections
String fileName
boolean [] answer
UISettings settings
File home
}

View File

@ -0,0 +1,70 @@
package com.muwire.gui
import griffon.core.artifact.GriffonView
import griffon.inject.MVCMember
import griffon.metadata.ArtifactProviderFor
import javax.swing.JCheckBox
import javax.swing.JDialog
import javax.swing.SwingConstants
import javax.annotation.Nonnull
import static com.muwire.gui.Translator.trans
import java.awt.BorderLayout
import java.awt.event.WindowAdapter
import java.awt.event.WindowEvent
@ArtifactProviderFor(GriffonView)
class CollectionWarningView {
@MVCMember @Nonnull
FactoryBuilderSupport builder
@MVCMember @Nonnull
CollectionWarningModel model
JCheckBox rememberCheckbox
def mainFrame
def dialog
def mainPanel
void initUI() {
mainFrame = application.windowManager.findWindow("main-frame")
dialog = new JDialog(mainFrame, trans("COLLECTION_WARNING_TITLE"), true)
dialog.setResizable(true)
mainPanel = builder.panel {
borderLayout()
panel(constraints : BorderLayout.NORTH) {
label(text : trans("COLLECTION_WARNING_BODY1", model.fileName))
label(text : trans("COLLECTION_WARNING_BODY2"))
}
scrollPane(constraints : BorderLayout.CENTER) {
list(items : model.collections)
}
panel(constraints : BorderLayout.SOUTH) {
rememberCheckbox = checkBox(selected : false)
label(text : trans("REMEMBER_DECISION"))
button(text : trans("UNSHARE"), unshareAction )
button(text : trans("CANCEL"), cancelAction)
}
}
dialog.getContentPane().add(mainPanel)
dialog.pack()
dialog.setResizable(false)
dialog.setLocationRelativeTo(mainFrame)
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE)
dialog.addWindowListener(new WindowAdapter() {
public void windowClosed(WindowEvent e) {
dialog.setVisible(false)
mvcGroup.destroy()
}
})
}
void mvcGroupInit(Map<String,String> args) {
dialog.show()
}
}

View File

@ -18,6 +18,7 @@ class UISettings {
boolean excludeLocalResult
boolean showSearchHashes
boolean closeWarning
boolean collectionWarning
boolean certificateWarning
boolean exitOnClose
boolean clearUploads
@ -40,6 +41,7 @@ class UISettings {
fontSize = Integer.parseInt(props.getProperty("fontSize","12"))
fontStyle = Integer.parseInt(props.getProperty("fontStyle", String.valueOf(Font.PLAIN)))
closeWarning = Boolean.parseBoolean(props.getProperty("closeWarning","true"))
collectionWarning = Boolean.parseBoolean(props.getProperty("collectionWarning", "true"))
certificateWarning = Boolean.parseBoolean(props.getProperty("certificateWarning","true"))
exitOnClose = Boolean.parseBoolean(props.getProperty("exitOnClose","false"))
clearUploads = Boolean.parseBoolean(props.getProperty("clearUploads","false"))
@ -66,6 +68,7 @@ class UISettings {
props.setProperty("autoFontSize", String.valueOf(autoFontSize))
props.setProperty("fontSize", String.valueOf(fontSize))
props.setProperty("closeWarning", String.valueOf(closeWarning))
props.setProperty("collectionWarning", String.valueOf(collectionWarning))
props.setProperty("certificateWarning", String.valueOf(certificateWarning))
props.setProperty("exitOnClose", String.valueOf(exitOnClose))
props.setProperty("clearUploads", String.valueOf(clearUploads))