Files
muwire/gui/griffon-app/lifecycle/Ready.groovy

112 lines
4.0 KiB
Groovy
Raw Normal View History

2019-05-29 18:22:48 +01:00
import griffon.core.GriffonApplication
2019-06-03 23:11:03 +01:00
import griffon.core.env.Metadata
2019-05-29 18:22:48 +01:00
import groovy.util.logging.Log
2019-06-08 07:10:24 +01:00
import net.i2p.util.SystemVersion
2019-05-29 18:22:48 +01:00
import org.codehaus.griffon.runtime.core.AbstractLifecycleHandler
import com.muwire.core.Core
import com.muwire.core.MuWireSettings
2019-06-09 15:44:06 +01:00
import com.muwire.core.UILoadedEvent
import com.muwire.core.files.FileSharedEvent
2019-05-29 18:22:48 +01:00
import javax.annotation.Nonnull
import javax.inject.Inject
import javax.swing.JFileChooser
2019-05-31 11:16:04 +01:00
import javax.swing.JOptionPane
2019-05-29 18:22:48 +01:00
import static griffon.util.GriffonApplicationUtils.isMacOSX
import static groovy.swing.SwingBuilder.lookAndFeel
2019-05-29 19:05:19 +01:00
import java.beans.PropertyChangeEvent
2019-06-01 16:36:23 +01:00
import java.util.logging.Level
2019-05-29 19:05:19 +01:00
2019-05-29 18:22:48 +01:00
@Log
class Ready extends AbstractLifecycleHandler {
2019-06-03 23:11:03 +01:00
@Inject Metadata metadata
2019-05-29 18:22:48 +01:00
@Inject
Ready(@Nonnull GriffonApplication application) {
super(application)
}
@Override
void execute() {
log.info "starting core services"
def home = new File(application.getContext().getAsString("muwire-home"))
2019-05-29 18:22:48 +01:00
def props = new Properties()
def propsFile = new File(home, "MuWire.properties")
if (propsFile.exists()) {
log.info("loading existing props file")
propsFile.withInputStream {
props.load(it)
}
props = new MuWireSettings(props)
} else {
2019-05-31 11:16:04 +01:00
log.info("creating new properties")
2019-05-29 18:22:48 +01:00
props = new MuWireSettings()
2019-05-31 11:16:04 +01:00
def nickname
while (true) {
nickname = JOptionPane.showInputDialog(null,
"Your nickname is displayed when you send search results so other MuWire users can choose to trust you",
"Please choose a nickname", JOptionPane.PLAIN_MESSAGE)
if (nickname == null || nickname.trim().length() == 0) {
JOptionPane.showMessageDialog(null, "Nickname cannot be empty", "Select another nickname",
JOptionPane.WARNING_MESSAGE)
2019-05-31 11:16:04 +01:00
continue
}
if (nickname.contains("@")) {
JOptionPane.showMessageDialog(null, "Nickname cannot contain @, choose another",
"Select another nickname", JOptionPane.WARNING_MESSAGE)
2019-05-31 11:16:04 +01:00
continue
}
nickname = nickname.trim()
break
}
props.setNickname(nickname)
def portableDownloads = System.getProperty("portable.downloads")
if (portableDownloads != null) {
props.downloadLocation = new File(portableDownloads)
} else {
def chooser = new JFileChooser()
chooser.setDialogTitle("Select a directory where downloads will be saved")
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY)
int rv = chooser.showOpenDialog(null)
if (rv != JFileChooser.APPROVE_OPTION) {
JOptionPane.showMessageDialog(null, "MuWire will now exit")
System.exit(0)
}
props.downloadLocation = chooser.getSelectedFile()
}
2019-05-29 18:22:48 +01:00
propsFile.withOutputStream {
props.write(it)
}
}
2019-06-03 23:11:03 +01:00
2019-06-01 16:36:23 +01:00
Core core
try {
2019-06-03 23:11:03 +01:00
core = new Core(props, home, metadata["application.version"])
2019-06-01 16:36:23 +01:00
} catch (Exception bad) {
log.log(Level.SEVERE,"couldn't initialize core",bad)
JOptionPane.showMessageDialog(null, "Couldn't connect to I2P router. Make sure I2P is running and restart MuWire",
"Can't connect to I2P router", JOptionPane.WARNING_MESSAGE)
System.exit(0)
}
2019-05-29 18:22:48 +01:00
core.startServices()
2019-05-31 11:16:04 +01:00
application.context.put("muwire-settings", props)
2019-05-29 18:22:48 +01:00
application.context.put("core",core)
2019-05-29 19:05:19 +01:00
application.getPropertyChangeListeners("core").each {
it.propertyChange(new PropertyChangeEvent(this, "core", null, core))
}
2019-06-09 15:44:06 +01:00
core.eventBus.publish(new UILoadedEvent())
2019-05-29 18:22:48 +01:00
}
}