mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-15 04:18:53 +01:00
Moved defunct invitation and installer code to sandpit repo.
This commit is contained in:
@@ -1,14 +0,0 @@
|
||||
package net.sf.briar.invitation;
|
||||
|
||||
import net.sf.briar.api.invitation.InvitationWorkerFactory;
|
||||
|
||||
import com.google.inject.AbstractModule;
|
||||
|
||||
public class InvitationModule extends AbstractModule {
|
||||
|
||||
@Override
|
||||
protected void configure() {
|
||||
bind(InvitationWorkerFactory.class).to(
|
||||
InvitationWorkerFactoryImpl.class);
|
||||
}
|
||||
}
|
||||
@@ -1,107 +0,0 @@
|
||||
package net.sf.briar.invitation;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import net.sf.briar.api.db.DatabaseComponent;
|
||||
import net.sf.briar.api.db.DbException;
|
||||
import net.sf.briar.api.invitation.InvitationCallback;
|
||||
import net.sf.briar.api.invitation.InvitationParameters;
|
||||
import net.sf.briar.api.protocol.Transport;
|
||||
import net.sf.briar.api.serial.Writer;
|
||||
import net.sf.briar.api.serial.WriterFactory;
|
||||
import net.sf.briar.util.FileUtils;
|
||||
|
||||
class InvitationWorker implements Runnable {
|
||||
|
||||
private final InvitationCallback callback;
|
||||
private final InvitationParameters parameters;
|
||||
private final DatabaseComponent db;
|
||||
private final WriterFactory writerFactory;
|
||||
|
||||
InvitationWorker(final InvitationCallback callback,
|
||||
InvitationParameters parameters, DatabaseComponent db,
|
||||
WriterFactory writerFactory) {
|
||||
this.callback = callback;
|
||||
this.parameters = parameters;
|
||||
this.db = db;
|
||||
this.writerFactory = writerFactory;
|
||||
}
|
||||
|
||||
public void run() {
|
||||
File dir = parameters.getChosenLocation();
|
||||
assert dir != null;
|
||||
if(!dir.exists()) {
|
||||
callback.notFound(dir);
|
||||
return;
|
||||
}
|
||||
if(!dir.isDirectory()) {
|
||||
callback.notDirectory(dir);
|
||||
return;
|
||||
}
|
||||
if(!dir.canWrite()) {
|
||||
callback.notAllowed(dir);
|
||||
return;
|
||||
}
|
||||
List<File> files = new ArrayList<File>();
|
||||
try {
|
||||
if(callback.isCancelled()) return;
|
||||
files.add(createInvitationDat(dir));
|
||||
if(callback.isCancelled()) return;
|
||||
if(parameters.shouldCreateExe()) files.add(createBriarExe(dir));
|
||||
if(callback.isCancelled()) return;
|
||||
if(parameters.shouldCreateJar()) files.add(createBriarJar(dir));
|
||||
} catch(IOException e) {
|
||||
callback.error(e.toString());
|
||||
return;
|
||||
}
|
||||
if(callback.isCancelled()) return;
|
||||
callback.created(files);
|
||||
}
|
||||
|
||||
private File createInvitationDat(File dir) throws IOException {
|
||||
char[] password = parameters.getPassword();
|
||||
assert password != null;
|
||||
Arrays.fill(password, (char) 0);
|
||||
File invitationDat = new File(dir, "invitation.dat");
|
||||
callback.encryptingFile(invitationDat);
|
||||
// FIXME: Create a real invitation
|
||||
Collection<Transport> transports;
|
||||
try {
|
||||
transports = db.getLocalTransports();
|
||||
} catch(DbException e) {
|
||||
throw new IOException(e.toString());
|
||||
}
|
||||
FileOutputStream out = new FileOutputStream(invitationDat);
|
||||
Writer w = writerFactory.createWriter(out);
|
||||
w.writeList(transports);
|
||||
out.flush();
|
||||
out.close();
|
||||
return invitationDat;
|
||||
}
|
||||
|
||||
private File createBriarExe(File dir) throws IOException {
|
||||
File f = new File(dir, "briar.exe");
|
||||
copyInstaller(f);
|
||||
return f;
|
||||
}
|
||||
|
||||
private File createBriarJar(File dir) throws IOException {
|
||||
File f = new File(dir, "briar.jar");
|
||||
copyInstaller(f);
|
||||
return f;
|
||||
}
|
||||
|
||||
private void copyInstaller(File dest) throws IOException {
|
||||
File src = parameters.getSetupDat();
|
||||
if(!src.exists() || !src.isFile())
|
||||
throw new IOException("File not found: " + src.getPath());
|
||||
callback.copyingFile(dest);
|
||||
FileUtils.copy(src, dest);
|
||||
}
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
package net.sf.briar.invitation;
|
||||
|
||||
import net.sf.briar.api.db.DatabaseComponent;
|
||||
import net.sf.briar.api.invitation.InvitationCallback;
|
||||
import net.sf.briar.api.invitation.InvitationParameters;
|
||||
import net.sf.briar.api.invitation.InvitationWorkerFactory;
|
||||
import net.sf.briar.api.serial.WriterFactory;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
|
||||
class InvitationWorkerFactoryImpl implements InvitationWorkerFactory {
|
||||
|
||||
private final DatabaseComponent databaseComponent;
|
||||
private final WriterFactory writerFactory;
|
||||
|
||||
@Inject
|
||||
InvitationWorkerFactoryImpl(DatabaseComponent databaseComponent,
|
||||
WriterFactory writerFactory) {
|
||||
this.databaseComponent = databaseComponent;
|
||||
this.writerFactory = writerFactory;
|
||||
}
|
||||
|
||||
public Runnable createWorker(InvitationCallback callback,
|
||||
InvitationParameters parameters) {
|
||||
return new InvitationWorker(callback, parameters, databaseComponent,
|
||||
writerFactory);
|
||||
}
|
||||
}
|
||||
@@ -1,161 +0,0 @@
|
||||
package net.sf.briar.setup;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintStream;
|
||||
|
||||
import net.sf.briar.api.i18n.I18n;
|
||||
import net.sf.briar.api.setup.SetupCallback;
|
||||
import net.sf.briar.api.setup.SetupParameters;
|
||||
import net.sf.briar.util.FileUtils;
|
||||
import net.sf.briar.util.OsUtils;
|
||||
import net.sf.briar.util.ZipUtils;
|
||||
|
||||
class SetupWorker implements Runnable {
|
||||
|
||||
// FIXME: Change this when we have a main class
|
||||
private static final String MAIN_CLASS = "net.sf.briar.ui.FIXME";
|
||||
|
||||
private final SetupCallback callback;
|
||||
private final SetupParameters parameters;
|
||||
private final I18n i18n;
|
||||
private final File jar;
|
||||
private final ZipUtils.Callback unzipCallback;
|
||||
|
||||
SetupWorker(final SetupCallback callback, SetupParameters parameters,
|
||||
I18n i18n, File jar) {
|
||||
this.parameters = parameters;
|
||||
this.callback = callback;
|
||||
this.i18n = i18n;
|
||||
this.jar = jar;
|
||||
unzipCallback = new ZipUtils.Callback() {
|
||||
public void processingFile(File f) {
|
||||
callback.extractingFile(f);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public void run() {
|
||||
// Don't try to proceed if we're running from Eclipse
|
||||
if(!jar.isFile()) {
|
||||
callback.error("Not running from jar");
|
||||
return;
|
||||
}
|
||||
File dir = parameters.getChosenLocation();
|
||||
assert dir != null;
|
||||
if(!dir.exists()) {
|
||||
callback.notFound(dir);
|
||||
return;
|
||||
}
|
||||
if(!dir.isDirectory()) {
|
||||
callback.notDirectory(dir);
|
||||
return;
|
||||
}
|
||||
String[] list = dir.list();
|
||||
if(list == null || !dir.canWrite()) {
|
||||
callback.notAllowed(dir);
|
||||
return;
|
||||
}
|
||||
// If the chosen directory isn't empty, install to a subdirectory
|
||||
if(list.length != 0) {
|
||||
dir = new File(dir, "Briar");
|
||||
if(!dir.exists() && !dir.mkdir()) {
|
||||
callback.notAllowed(dir);
|
||||
return;
|
||||
}
|
||||
}
|
||||
// Everything but the launchers will go in the Data directory
|
||||
File data = new File(dir, "Data");
|
||||
if(!data.exists() && !data.mkdir()) {
|
||||
callback.notAllowed(data);
|
||||
return;
|
||||
}
|
||||
try {
|
||||
if(callback.isCancelled()) return;
|
||||
copyInstaller(jar, data);
|
||||
if(callback.isCancelled()) return;
|
||||
// Only extract the Windows JRE, jars and fonts
|
||||
extractFiles(jar, data, "^jre/.*|.*\\.jar$|.*\\.ttf$");
|
||||
if(callback.isCancelled()) return;
|
||||
createLaunchers(dir);
|
||||
if(callback.isCancelled()) return;
|
||||
// Save the chosen locale for the first launch
|
||||
i18n.saveLocale(dir);
|
||||
if(callback.isCancelled()) return;
|
||||
// Installation succeeded - delete the installer
|
||||
jar.deleteOnExit();
|
||||
} catch(IOException e) {
|
||||
callback.error(e.toString());
|
||||
return;
|
||||
}
|
||||
if(callback.isCancelled()) return;
|
||||
callback.installed(dir);
|
||||
}
|
||||
|
||||
// Create a copy of the installer for use in future invitations
|
||||
private void copyInstaller(File jar, File dir) throws IOException {
|
||||
File dest = new File(dir, "setup.dat");
|
||||
callback.copyingFile(dest);
|
||||
FileUtils.copy(jar, dest);
|
||||
}
|
||||
|
||||
// Extract files matching the given regex from the jar
|
||||
private void extractFiles(File jar, File dir, String regex)
|
||||
throws IOException {
|
||||
FileInputStream in = new FileInputStream(jar);
|
||||
in.skip(parameters.getExeHeaderSize());
|
||||
ZipUtils.unzipStream(in, dir, regex, unzipCallback);
|
||||
}
|
||||
|
||||
// Create launchers for Windows, Mac and Linux
|
||||
private void createLaunchers(File dir) throws IOException {
|
||||
createWindowsLauncher(dir);
|
||||
File mac = createMacLauncher(dir);
|
||||
File lin = createLinuxLauncher(dir);
|
||||
// If the OS supports chmod, make the Mac and Linux launchers executable
|
||||
if(!OsUtils.isWindows()) {
|
||||
String[] chmod = { "chmod", "u+x", mac.getName(), lin.getName() };
|
||||
ProcessBuilder p = new ProcessBuilder(chmod);
|
||||
p.directory(dir);
|
||||
p.start();
|
||||
}
|
||||
}
|
||||
|
||||
private File createWindowsLauncher(File dir) throws IOException {
|
||||
File launcher = new File(dir, "run-windows.vbs");
|
||||
PrintStream out = new PrintStream(new FileOutputStream(launcher));
|
||||
out.print("Set Shell = CreateObject(\"WScript.Shell\")\r\n");
|
||||
out.print("Shell.Run \"Data\\jre\\bin\\javaw -ea -cp Data\\* "
|
||||
+ MAIN_CLASS + "\", 0\r\n");
|
||||
out.print("Set Shell = Nothing\r\n");
|
||||
out.flush();
|
||||
out.close();
|
||||
return launcher;
|
||||
}
|
||||
|
||||
// FIXME: If this pops up a terminal window, the Mac launcher may need
|
||||
// to be a jar
|
||||
private File createMacLauncher(File dir) throws IOException {
|
||||
File launcher = new File(dir, "run-mac.command");
|
||||
PrintStream out = new PrintStream(new FileOutputStream(launcher));
|
||||
out.print("#!/bin/sh\n");
|
||||
out.print("cd \"$(dirname \"$0\")\"\n");
|
||||
out.print("java -ea -cp 'Data/*' " + MAIN_CLASS + "\n");
|
||||
out.flush();
|
||||
out.close();
|
||||
return launcher;
|
||||
}
|
||||
|
||||
private File createLinuxLauncher(File dir) throws IOException {
|
||||
File launcher = new File(dir, "run-linux.sh");
|
||||
PrintStream out = new PrintStream(new FileOutputStream(launcher));
|
||||
out.print("#!/bin/sh\n");
|
||||
out.print("cd \"$(dirname \"$0\")\"\n");
|
||||
out.print("java -ea -cp 'Data/*' " + MAIN_CLASS + "\n");
|
||||
out.flush();
|
||||
out.close();
|
||||
return launcher;
|
||||
}
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
package net.sf.briar.setup;
|
||||
|
||||
import java.io.File;
|
||||
import java.security.CodeSource;
|
||||
|
||||
import net.sf.briar.api.i18n.I18n;
|
||||
import net.sf.briar.api.setup.SetupCallback;
|
||||
import net.sf.briar.api.setup.SetupParameters;
|
||||
import net.sf.briar.api.setup.SetupWorkerFactory;
|
||||
import net.sf.briar.util.FileUtils;
|
||||
|
||||
// Needs to be public for installer
|
||||
public class SetupWorkerFactoryImpl implements SetupWorkerFactory {
|
||||
|
||||
private final I18n i18n;
|
||||
|
||||
public SetupWorkerFactoryImpl(I18n i18n) {
|
||||
this.i18n = i18n;
|
||||
}
|
||||
|
||||
public Runnable createWorker(SetupCallback callback,
|
||||
SetupParameters parameters) {
|
||||
CodeSource c = FileUtils.class.getProtectionDomain().getCodeSource();
|
||||
File jar = new File(c.getLocation().getPath());
|
||||
assert jar.exists();
|
||||
return new SetupWorker(callback, parameters, i18n, jar);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user