Files
briar/components/net/sf/briar/plugins/file/RemovableDrivePlugin.java
akwizgran 73aa7d14d7 Split transport identifiers into two: TransportId (globally unique)
and TransportIndex (locally unique).

This is the first step towards forward secrecy. Also removed the
Writable interface and unnecessary user-defined types, moved various
constants to ProtocolConstants and renamed some classes.
2011-11-14 21:40:05 +00:00

123 lines
3.0 KiB
Java

package net.sf.briar.plugins.file;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.sf.briar.api.plugins.BatchPluginCallback;
import net.sf.briar.api.protocol.TransportId;
import net.sf.briar.util.StringUtils;
class RemovableDrivePlugin extends FilePlugin
implements RemovableDriveMonitor.Callback {
public static final byte[] TRANSPORT_ID =
StringUtils.fromHexString("7c81bf5c9b1cd557685548c85f976bbd"
+ "e633d2418ea2e230e5710fb43c6f8cc0");
private static final TransportId id = new TransportId(TRANSPORT_ID);
private static final Logger LOG =
Logger.getLogger(RemovableDrivePlugin.class.getName());
private final RemovableDriveFinder finder;
private final RemovableDriveMonitor monitor;
RemovableDrivePlugin(Executor executor, BatchPluginCallback callback,
RemovableDriveFinder finder, RemovableDriveMonitor monitor) {
super(executor, callback);
this.finder = finder;
this.monitor = monitor;
}
public TransportId getId() {
return id;
}
@Override
public synchronized void start() throws IOException {
super.start();
monitor.start(this);
}
@Override
public synchronized void stop() throws IOException {
super.stop();
monitor.stop();
}
public boolean shouldPoll() {
return false;
}
public long getPollingInterval() {
return 0L;
}
public void poll() {
throw new UnsupportedOperationException();
}
public boolean supportsInvitations() {
return true;
}
@Override
protected File chooseOutputDirectory() {
try {
List<File> drives = finder.findRemovableDrives();
if(drives.isEmpty()) return null;
String[] paths = new String[drives.size()];
for(int i = 0; i < paths.length; i++) {
paths[i] = drives.get(i).getPath();
}
int i = callback.showChoice(paths, "REMOVABLE_DRIVE_CHOOSE_DRIVE");
if(i == -1) return null;
return drives.get(i);
} catch(IOException e) {
if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.getMessage());
return null;
}
}
@Override
protected void readerFinished(File f) {
callback.showMessage("REMOVABLE_DRIVE_READ_FINISHED");
}
@Override
protected void writerFinished(File f) {
callback.showMessage("REMOVABLE_DRIVE_WRITE_FINISHED");
}
@Override
protected Collection<File> findFilesByName(String filename) {
Collection<File> matches = new ArrayList<File>();
try {
for(File drive : finder.findRemovableDrives()) {
File[] files = drive.listFiles();
if(files != null) {
for(File f : files) {
if(f.isFile() && filename.equals(f.getName()))
matches.add(f);
}
}
}
} catch(IOException e) {
if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.getMessage());
}
return matches;
}
public void driveInserted(File root) {
File[] files = root.listFiles();
if(files != null) {
for(File f : files) if(f.isFile()) createReaderFromFile(f);
}
}
}