2009-01-04 13:28:28 -05:00
|
|
|
|
2008-12-31 15:11:27 -05:00
|
|
|
package net.sourceforge.filebot;
|
2009-01-04 13:28:28 -05:00
|
|
|
|
|
|
|
|
2009-05-17 13:22:44 -04:00
|
|
|
import static javax.swing.JFrame.*;
|
2010-01-26 14:08:09 -05:00
|
|
|
import static net.sourceforge.tuned.ui.TunedUtilities.*;
|
2009-03-13 20:30:24 -04:00
|
|
|
|
2010-01-22 10:19:11 -05:00
|
|
|
import java.awt.event.WindowAdapter;
|
|
|
|
import java.awt.event.WindowEvent;
|
2009-05-02 19:34:04 -04:00
|
|
|
import java.security.CodeSource;
|
|
|
|
import java.security.Permission;
|
|
|
|
import java.security.PermissionCollection;
|
|
|
|
import java.security.Permissions;
|
|
|
|
import java.security.Policy;
|
|
|
|
import java.security.ProtectionDomain;
|
2008-02-13 15:08:06 -05:00
|
|
|
import java.util.logging.Level;
|
|
|
|
import java.util.logging.Logger;
|
|
|
|
|
2009-03-13 20:30:24 -04:00
|
|
|
import javax.swing.JFrame;
|
2007-12-23 14:28:04 -05:00
|
|
|
import javax.swing.SwingUtilities;
|
|
|
|
import javax.swing.UIManager;
|
|
|
|
|
2011-09-13 14:16:38 -04:00
|
|
|
import org.kohsuke.args4j.CmdLineException;
|
2009-06-13 05:53:48 -04:00
|
|
|
|
2009-10-28 11:09:47 -04:00
|
|
|
import net.sf.ehcache.CacheManager;
|
2011-09-09 10:50:01 -04:00
|
|
|
import net.sourceforge.filebot.cli.ArgumentBean;
|
|
|
|
import net.sourceforge.filebot.cli.ArgumentProcessor;
|
2011-10-29 16:24:01 -04:00
|
|
|
import net.sourceforge.filebot.cli.CmdlineOperations;
|
2009-05-02 19:34:04 -04:00
|
|
|
import net.sourceforge.filebot.format.ExpressionFormat;
|
2009-03-13 20:30:24 -04:00
|
|
|
import net.sourceforge.filebot.ui.MainFrame;
|
|
|
|
import net.sourceforge.filebot.ui.SinglePanelFrame;
|
2011-09-22 01:03:46 -04:00
|
|
|
import net.sourceforge.filebot.ui.sfv.SfvPanelBuilder;
|
2011-09-09 10:50:01 -04:00
|
|
|
import net.sourceforge.filebot.ui.transfer.FileTransferable;
|
2009-03-14 06:20:59 -04:00
|
|
|
|
2007-12-23 14:28:04 -05:00
|
|
|
|
|
|
|
public class Main {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param args
|
|
|
|
*/
|
2009-01-24 19:08:57 -05:00
|
|
|
public static void main(String... args) throws Exception {
|
2009-07-27 18:34:42 -04:00
|
|
|
// initialize this stuff before anything else
|
2009-10-28 11:09:47 -04:00
|
|
|
initializeCache();
|
2009-07-27 18:34:42 -04:00
|
|
|
initializeSecurityManager();
|
2008-02-13 15:08:06 -05:00
|
|
|
|
2011-10-29 16:24:01 -04:00
|
|
|
try {
|
|
|
|
// parse arguments
|
|
|
|
final ArgumentProcessor cli = new ArgumentProcessor();
|
|
|
|
final ArgumentBean argumentBean = cli.parse(args);
|
|
|
|
|
|
|
|
// initialize analytics
|
|
|
|
Analytics.setEnabled(!argumentBean.disableAnalytics);
|
|
|
|
|
|
|
|
if (argumentBean.printHelp()) {
|
|
|
|
// just print help message and exit afterwards
|
|
|
|
cli.printHelp(argumentBean);
|
|
|
|
System.exit(0);
|
2011-09-13 14:16:38 -04:00
|
|
|
}
|
2009-01-24 19:08:57 -05:00
|
|
|
|
2011-10-29 16:24:01 -04:00
|
|
|
if (argumentBean.clearUserData()) {
|
|
|
|
// clear preferences and cache
|
|
|
|
Settings.forPackage(Main.class).clear();
|
|
|
|
CacheManager.getInstance().clearAll();
|
|
|
|
}
|
2007-12-23 14:28:04 -05:00
|
|
|
|
2011-10-29 16:24:01 -04:00
|
|
|
// CLI mode => run command-line interface and then exit
|
|
|
|
if (argumentBean.runCLI()) {
|
|
|
|
int status = cli.process(argumentBean, new CmdlineOperations());
|
|
|
|
System.exit(status);
|
2007-12-23 14:28:04 -05:00
|
|
|
}
|
2011-10-29 16:24:01 -04:00
|
|
|
|
|
|
|
// GUI mode => start user interface
|
|
|
|
SwingUtilities.invokeLater(new Runnable() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
try {
|
|
|
|
// use native laf an all platforms
|
|
|
|
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
|
|
|
|
} catch (Exception e) {
|
|
|
|
Logger.getLogger(Main.class.getName()).log(Level.WARNING, e.getMessage(), e);
|
|
|
|
}
|
|
|
|
|
|
|
|
startUserInterface(argumentBean);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} catch (CmdLineException e) {
|
|
|
|
// illegal arguments => just print CLI error message and stop
|
|
|
|
System.err.println(e.getMessage());
|
|
|
|
System.exit(-1);
|
|
|
|
}
|
2007-12-23 14:28:04 -05:00
|
|
|
}
|
2008-03-19 18:14:38 -04:00
|
|
|
|
2008-07-30 18:37:01 -04:00
|
|
|
|
2011-09-09 10:50:01 -04:00
|
|
|
private static void startUserInterface(ArgumentBean args) {
|
|
|
|
JFrame frame;
|
|
|
|
|
|
|
|
if (args.openSFV()) {
|
|
|
|
// single panel frame
|
|
|
|
FileTransferable files = new FileTransferable(args.getFiles(false));
|
|
|
|
frame = new SinglePanelFrame(new SfvPanelBuilder()).publish(files);
|
|
|
|
} else {
|
|
|
|
// default frame
|
|
|
|
frame = new MainFrame();
|
|
|
|
}
|
|
|
|
|
|
|
|
frame.setLocationByPlatform(true);
|
|
|
|
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
|
|
|
|
|
|
|
|
try {
|
|
|
|
// restore previous size and location
|
|
|
|
restoreWindowBounds(frame, Settings.forPackage(MainFrame.class));
|
|
|
|
} catch (Exception e) {
|
|
|
|
// don't care, doesn't make a difference
|
|
|
|
}
|
|
|
|
|
|
|
|
// start application
|
|
|
|
frame.setVisible(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-01-26 14:08:09 -05:00
|
|
|
private static void restoreWindowBounds(final JFrame window, final Settings settings) {
|
2010-01-22 10:19:11 -05:00
|
|
|
// store bounds on close
|
|
|
|
window.addWindowListener(new WindowAdapter() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void windowClosing(WindowEvent e) {
|
2010-01-26 14:08:09 -05:00
|
|
|
// don't save window bounds if window is maximized
|
|
|
|
if (!isMaximized(window)) {
|
|
|
|
settings.put("window.x", String.valueOf(window.getX()));
|
|
|
|
settings.put("window.y", String.valueOf(window.getY()));
|
|
|
|
settings.put("window.width", String.valueOf(window.getWidth()));
|
|
|
|
settings.put("window.height", String.valueOf(window.getHeight()));
|
|
|
|
}
|
2010-01-22 10:19:11 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// restore bounds
|
|
|
|
int x = Integer.parseInt(settings.get("window.x"));
|
|
|
|
int y = Integer.parseInt(settings.get("window.y"));
|
|
|
|
int width = Integer.parseInt(settings.get("window.width"));
|
|
|
|
int height = Integer.parseInt(settings.get("window.height"));
|
|
|
|
window.setBounds(x, y, width, height);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-10-28 11:09:47 -04:00
|
|
|
/**
|
|
|
|
* Shutdown ehcache properly, so that disk-persistent stores can actually be saved to disk
|
|
|
|
*/
|
|
|
|
private static void initializeCache() {
|
|
|
|
Runtime.getRuntime().addShutdownHook(new Thread() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
CacheManager.getInstance().shutdown();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-02 19:34:04 -04:00
|
|
|
/**
|
|
|
|
* Initialize default SecurityManager and grant all permissions via security policy.
|
|
|
|
* Initialization is required in order to run {@link ExpressionFormat} in a secure sandbox.
|
|
|
|
*/
|
|
|
|
private static void initializeSecurityManager() {
|
|
|
|
try {
|
|
|
|
// initialize security policy used by the default security manager
|
|
|
|
// because default the security policy is very restrictive (e.g. no FilePermission)
|
|
|
|
Policy.setPolicy(new Policy() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean implies(ProtectionDomain domain, Permission permission) {
|
|
|
|
// all permissions
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public PermissionCollection getPermissions(CodeSource codesource) {
|
|
|
|
// VisualVM can't connect if this method does return
|
|
|
|
// a checked immutable PermissionCollection
|
|
|
|
return new Permissions();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// set default security manager
|
|
|
|
System.setSecurityManager(new SecurityManager());
|
|
|
|
} catch (Exception e) {
|
|
|
|
// security manager was probably set via system property
|
|
|
|
Logger.getLogger(Main.class.getName()).log(Level.WARNING, e.toString(), e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-23 14:28:04 -05:00
|
|
|
}
|