use of com.biglybt.core.disk.DiskManagerFileInfo in project BiglyBT by BiglySoftware.
the class FilesViewMenuUtil method invalidateRows.
private static void invalidateRows(TableView tv, List<DiskManagerFileInfo> files) {
if (tv == null) {
return;
}
Set<TableRowCore> done = new HashSet<>();
for (DiskManagerFileInfo file : files) {
TableRowCore row = tv.getRow(file);
if (row == null) {
row = tv.getRow(file.getDownloadManager());
if (row != null) {
TableRowCore[] subrows = row.getSubRowsWithNull();
if (subrows != null) {
for (TableRowCore sr : subrows) {
if (sr.getDataSource(true) == file) {
row = sr;
break;
}
}
}
}
}
if (row != null && !done.contains(row)) {
done.add(row);
row.invalidate(true);
row.refresh(true);
}
}
}
use of com.biglybt.core.disk.DiskManagerFileInfo in project BiglyBT by BiglySoftware.
the class FilesViewMenuUtil method changePriorityManual.
private static void changePriorityManual(final List<DiskManagerFileInfo> file_list) {
SimpleTextEntryWindow entryWindow = new SimpleTextEntryWindow("FilesView.dialog.priority.title", "FilesView.dialog.priority.text");
entryWindow.prompt(new UIInputReceiverListener() {
@Override
public void UIInputReceiverClosed(UIInputReceiver entryWindow) {
if (!entryWindow.hasSubmittedInput()) {
return;
}
String sReturn = entryWindow.getSubmittedInput();
if (sReturn == null)
return;
int priority = 0;
try {
priority = Integer.valueOf(sReturn).intValue();
} catch (NumberFormatException er) {
Debug.out("Invalid priority: " + sReturn);
new MessageBoxShell(SWT.ICON_ERROR | SWT.OK, MessageText.getString("FilePriority.invalid.title"), MessageText.getString("FilePriority.invalid.text", new String[] { sReturn })).open(null);
return;
}
Map<DownloadManager, ArrayList<DiskManagerFileInfo>> mapDMtoDMFI = new IdentityHashMap<>();
for (DiskManagerFileInfo file : file_list) {
DownloadManager dm = file.getDownloadManager();
ArrayList<DiskManagerFileInfo> listFileInfos = mapDMtoDMFI.get(dm);
if (listFileInfos == null) {
listFileInfos = new ArrayList<>(1);
mapDMtoDMFI.put(dm, listFileInfos);
}
listFileInfos.add(file);
file.setPriority(priority);
}
for (DownloadManager dm : mapDMtoDMFI.keySet()) {
ArrayList<DiskManagerFileInfo> list = mapDMtoDMFI.get(dm);
DiskManagerFileInfo[] fileInfos = list.toArray(new DiskManagerFileInfo[0]);
boolean paused = setSkipped(dm, fileInfos, false, false);
if (paused) {
dm.resume();
}
}
}
});
}
use of com.biglybt.core.disk.DiskManagerFileInfo in project BiglyBT by BiglySoftware.
the class FilesViewMenuUtil method rename.
public static void rename(final TableView tv, final Object[] datasources, boolean rename_it, boolean retarget_it, boolean batch) {
if (datasources.length == 0) {
return;
}
final List<DownloadManager> pausedDownloads = new ArrayList<>(0);
final AESemaphore task_sem = new AESemaphore("tasksem");
final List<DiskManagerFileInfo> affected_files = new ArrayList<>();
try {
if (batch) {
StringBuilder details = new StringBuilder(32 * 1024);
Map<DownloadManager, Integer> dm_map = new IdentityHashMap<>();
Map<String, DownloadManager> dm_name_map = new HashMap<>();
for (int i = 0; i < datasources.length; i++) {
DiskManagerFileInfo fileInfo = (DiskManagerFileInfo) datasources[i];
DownloadManager dm = fileInfo.getDownloadManager();
if (!dm_map.containsKey(dm)) {
dm_map.put(dm, i);
dm_name_map.put(dm.getInternalName(), dm);
}
}
Arrays.sort(datasources, new Comparator<Object>() {
@Override
public int compare(Object o1, Object o2) {
DiskManagerFileInfo f1 = (DiskManagerFileInfo) o1;
DiskManagerFileInfo f2 = (DiskManagerFileInfo) o2;
DownloadManager d1 = f1.getDownloadManager();
DownloadManager d2 = f2.getDownloadManager();
if (d1 == d2) {
return (0);
} else {
return (dm_map.get(d1) - dm_map.get(d2));
}
}
});
DownloadManager current_dm = null;
for (int i = 0; i < datasources.length; i++) {
DiskManagerFileInfo fileInfo = (DiskManagerFileInfo) datasources[i];
DownloadManager dm = fileInfo.getDownloadManager();
if (dm != current_dm) {
if (dm_map.size() > 1) {
if (current_dm != null) {
details.append("\n");
}
details.append("# " + dm.getInternalName() + " - ");
details.append(dm.getDisplayName());
details.append("\n\n");
}
current_dm = dm;
}
String index_str = String.valueOf(fileInfo.getIndex() + 1);
while (index_str.length() < 5) {
index_str += " ";
}
details.append(index_str);
details.append(fileInfo.getFile(true).getAbsolutePath());
details.append("\n");
}
TextViewerWindow viewer = new TextViewerWindow(Utils.findAnyShell(), "batch.retarget.title", "batch.retarget.text", details.toString(), true, true);
viewer.setEditable(true);
viewer.setNonProportionalFont();
DownloadManager f_dm = current_dm;
viewer.addListener(new TextViewerWindow.TextViewerWindowListener() {
@Override
public void closed() {
if (!viewer.getOKPressed()) {
return;
}
String text = viewer.getText();
if (text.equals(details.toString())) {
return;
}
String[] lines = text.split("\n");
StringBuilder result = new StringBuilder(23 * 1024);
List<Object[]> actions = new ArrayList<>();
DiskManagerFileInfo[] current_files = f_dm.getDiskManagerFileInfoSet().getFiles();
for (String line : lines) {
line = line.trim();
if (line.isEmpty()) {
continue;
}
if (line.startsWith("#")) {
try {
String[] bits = line.split("\\s+", 3);
DownloadManager dm = dm_name_map.get(bits[1].trim());
current_files = dm.getDiskManagerFileInfoSet().getFiles();
} catch (Throwable e) {
result.append("Invalid line: " + line + "\n");
}
} else {
try {
String[] bits = line.split("\\s+", 2);
int index = Integer.parseInt(bits[0].trim());
DiskManagerFileInfo file = current_files[index - 1];
String path = bits[1].trim();
File existing_file = file.getFile(true);
if (!existing_file.getAbsolutePath().equals(path)) {
File target_file = new File(path);
actions.add(new Object[] { file, existing_file, target_file });
}
} catch (Throwable e) {
result.append("Invalid line: " + line + "\n");
}
}
}
if (result.length() > 0) {
Utils.execSWTThreadLater(1, new Runnable() {
public void run() {
TextViewerWindow viewer = new TextViewerWindow(Utils.findAnyShell(), "batch.retarget.title", "batch.retarget.error.text", result.toString(), true, true);
viewer.setNonProportionalFont();
viewer.goModal();
}
});
} else if (!actions.isEmpty()) {
for (Object[] action : actions) {
DiskManagerFileInfo file = (DiskManagerFileInfo) action[0];
File existing_file = (File) action[1];
File target_file = (File) action[2];
DownloadManager manager = file.getDownloadManager();
if (!pausedDownloads.contains(manager)) {
if (manager.pause()) {
pausedDownloads.add(manager);
}
}
boolean dont_delete_existing = false;
if (target_file.exists()) {
if (target_file.equals(existing_file)) {
continue;
} else {
if (checkRetargetOK(file, target_file)) {
dont_delete_existing = true;
} else {
continue;
}
}
}
affected_files.add(file);
result.append(existing_file + " -> " + target_file + "\n");
moveFile(manager, file, target_file, dont_delete_existing, new Runnable() {
@Override
public void run() {
task_sem.release();
}
});
}
if (result.length() > 0) {
Utils.execSWTThreadLater(1, new Runnable() {
public void run() {
TextViewerWindow viewer = new TextViewerWindow(Utils.findAnyShell(), "batch.retarget.title", "batch.retarget.result.text", result.toString(), true, true);
viewer.setNonProportionalFont();
viewer.goModal();
}
});
}
}
}
});
viewer.goModal();
} else {
String save_dir = null;
if (!rename_it && retarget_it) {
// better count (text based on rename/retarget)
String s = MessageText.getString("label.num_selected", new String[] { Integer.toString(datasources.length) });
save_dir = askForSaveDirectory((DiskManagerFileInfo) datasources[0], s);
if (save_dir == null) {
return;
}
}
for (int i = 0; i < datasources.length; i++) {
if (datasources[i] instanceof DownloadManager) {
AdvRenameWindow window = new AdvRenameWindow();
window.open((DownloadManager) datasources[i]);
continue;
}
if (!(datasources[i] instanceof DiskManagerFileInfo)) {
continue;
}
final DiskManagerFileInfo fileInfo = (DiskManagerFileInfo) datasources[i];
File existing_file = fileInfo.getFile(true);
File f_target = null;
if (rename_it && retarget_it) {
String s_target = askForRetargetedFilename(fileInfo);
if (s_target != null)
f_target = new File(s_target);
} else if (rename_it) {
askForRenameFilenameAndExec(fileInfo, tv);
continue;
} else {
// Parent directory has changed.
f_target = new File(save_dir, existing_file.getName());
}
// If the user has decided against it - abort the op.
if (f_target == null) {
return;
}
DownloadManager manager = fileInfo.getDownloadManager();
if (!pausedDownloads.contains(manager)) {
if (manager.pause()) {
pausedDownloads.add(manager);
}
}
boolean dont_delete_existing = false;
if (f_target.exists()) {
if (f_target.equals(existing_file)) {
continue;
} else if (retarget_it) {
if (checkRetargetOK(fileInfo, f_target)) {
dont_delete_existing = true;
} else {
continue;
}
} else if (existing_file.exists() && !askCanOverwrite(existing_file)) {
continue;
}
// If we reach here, then it means we are doing a real move, but there is
// no existing file.
}
final File ff_target = f_target;
final boolean f_dont_delete_existing = dont_delete_existing;
affected_files.add(fileInfo);
moveFile(fileInfo.getDownloadManager(), fileInfo, ff_target, f_dont_delete_existing, new Runnable() {
@Override
public void run() {
task_sem.release();
}
});
}
}
} finally {
if (affected_files.size() > 0) {
Utils.getOffOfSWTThread(new AERunnable() {
@Override
public void runSupport() {
for (int i = 0; i < affected_files.size(); i++) {
task_sem.reserve();
}
for (DownloadManager manager : pausedDownloads) {
manager.resume();
}
invalidateRows(tv, affected_files);
}
});
}
}
}
use of com.biglybt.core.disk.DiskManagerFileInfo in project BiglyBT by BiglySoftware.
the class MyTorrentsView method createDragDrop.
private void createDragDrop() {
try {
Transfer[] types = new Transfer[] { TextTransfer.getInstance() };
if (dragSource != null && !dragSource.isDisposed()) {
dragSource.dispose();
}
if (dropTarget != null && !dropTarget.isDisposed()) {
dropTarget.dispose();
}
dragSource = tv.createDragSource(DND.DROP_MOVE | DND.DROP_COPY);
if (dragSource != null) {
dragSource.setTransfer(types);
dragSource.addDragListener(new DragSourceAdapter() {
private String eventData;
@Override
public void dragStart(DragSourceEvent event) {
TableRowCore[] rows = tv.getSelectedRows();
if (rows.length != 0) {
event.doit = true;
// System.out.println("DragStart");
drag_drop_line_start = rows[0].getIndex();
drag_drop_rows = rows;
} else {
event.doit = false;
drag_drop_line_start = -1;
drag_drop_rows = null;
}
// Build eventData here because on OSX, selection gets cleared
// by the time dragSetData occurs
boolean onlyDMs = true;
StringBuilder sb = new StringBuilder();
Object[] selectedDataSources = tv.getSelectedDataSources(true);
for (Object ds : selectedDataSources) {
if (ds instanceof DownloadManager) {
DownloadManager dm = (DownloadManager) ds;
TOTorrent torrent = dm.getTorrent();
if (torrent != null) {
try {
sb.append(torrent.getHashWrapper().toBase32String());
sb.append('\n');
} catch (TOTorrentException e) {
}
}
} else if (ds instanceof DiskManagerFileInfo) {
DiskManagerFileInfo fileInfo = (DiskManagerFileInfo) ds;
DownloadManager dm = fileInfo.getDownloadManager();
TOTorrent torrent = dm.getTorrent();
if (torrent != null) {
try {
sb.append(torrent.getHashWrapper().toBase32String());
sb.append(';');
sb.append(fileInfo.getIndex());
sb.append('\n');
onlyDMs = false;
} catch (TOTorrentException e) {
}
}
}
}
eventData = (onlyDMs ? "DownloadManager\n" : "DiskManagerFileInfo\n") + sb.toString();
}
@Override
public void dragSetData(DragSourceEvent event) {
// System.out.println("DragSetData");
event.data = eventData;
}
});
}
dropTarget = tv.createDropTarget(DND.DROP_DEFAULT | DND.DROP_MOVE | DND.DROP_COPY | DND.DROP_LINK | DND.DROP_TARGET_MOVE);
if (dropTarget != null) {
dropTarget.setTransfer(new Transfer[] { FixedHTMLTransfer.getInstance(), FixedURLTransfer.getInstance(), FileTransfer.getInstance(), TextTransfer.getInstance() });
dropTarget.addDropListener(new DropTargetAdapter() {
Point enterPoint = null;
@Override
public void dropAccept(DropTargetEvent event) {
event.currentDataType = FixedURLTransfer.pickBestType(event.dataTypes, event.currentDataType);
}
@Override
public void dragEnter(DropTargetEvent event) {
// if ours
if (drag_drop_line_start < 0) {
if (event.detail != DND.DROP_COPY) {
if ((event.operations & DND.DROP_LINK) > 0)
event.detail = DND.DROP_LINK;
else if ((event.operations & DND.DROP_COPY) > 0)
event.detail = DND.DROP_COPY;
}
} else if (TextTransfer.getInstance().isSupportedType(event.currentDataType)) {
event.detail = tv.getTableRowWithCursor() == null ? DND.DROP_NONE : DND.DROP_MOVE;
event.feedback = DND.FEEDBACK_SCROLL;
enterPoint = new Point(event.x, event.y);
}
}
// @see org.eclipse.swt.dnd.DropTargetAdapter#dragLeave(org.eclipse.swt.dnd.DropTargetEvent)
@Override
public void dragLeave(DropTargetEvent event) {
super.dragLeave(event);
tv.getComposite().redraw();
}
@Override
public void dragOver(DropTargetEvent event) {
if (drag_drop_line_start >= 0) {
if (drag_drop_rows.length > 0 && !(drag_drop_rows[0].getDataSource(true) instanceof DownloadManager)) {
event.detail = DND.DROP_NONE;
return;
}
TableRowCore row = tv.getTableRowWithCursor();
if (row instanceof TableRowPainted) {
boolean dragging_down = row.getIndex() > drag_drop_line_start;
Rectangle bounds = ((TableRowPainted) row).getBounds();
tv.getComposite().redraw();
tv.getComposite().update();
GC gc = new GC(tv.getComposite());
gc.setLineWidth(2);
int y_pos = bounds.y;
if (dragging_down) {
y_pos += bounds.height;
}
gc.drawLine(bounds.x, y_pos, bounds.x + bounds.width, y_pos);
gc.dispose();
}
event.detail = row == null ? DND.DROP_NONE : DND.DROP_MOVE;
event.feedback = DND.FEEDBACK_SCROLL | ((enterPoint != null && enterPoint.y > event.y) ? DND.FEEDBACK_INSERT_BEFORE : DND.FEEDBACK_INSERT_AFTER);
}
}
@Override
public void drop(DropTargetEvent event) {
if (!(event.data instanceof String)) {
TorrentOpener.openDroppedTorrents(event, true);
return;
}
String data = (String) event.data;
if (data.startsWith("DiskManagerFileInfo\n")) {
return;
}
if (!data.startsWith("DownloadManager\n")) {
TorrentOpener.openDroppedTorrents(event, true);
return;
}
event.detail = DND.DROP_NONE;
// Torrent file from shell dropped
if (drag_drop_line_start >= 0) {
// event.data == null
event.detail = DND.DROP_NONE;
TableRowCore row = tv.getRow(event);
if (row == null)
return;
if (row.getParentRowCore() != null) {
row = row.getParentRowCore();
}
int drag_drop_line_end = row.getIndex();
if (drag_drop_line_end != drag_drop_line_start) {
DownloadManager dm = (DownloadManager) row.getDataSource(true);
moveRowsTo(drag_drop_rows, dm.getPosition());
event.detail = DND.DROP_MOVE;
}
drag_drop_line_start = -1;
drag_drop_rows = null;
}
}
});
}
} catch (Throwable t) {
Logger.log(new LogEvent(LOGID, "failed to init drag-n-drop", t));
}
}
use of com.biglybt.core.disk.DiskManagerFileInfo in project BiglyBT by BiglySoftware.
the class MyTorrentsView method rowRefresh.
// @see com.biglybt.pif.ui.tables.TableRowRefreshListener#rowRefresh(com.biglybt.pif.ui.tables.TableRow)
@Override
public void rowRefresh(TableRow row) {
if (!(row instanceof TableRowCore)) {
return;
}
TableRowCore rowCore = (TableRowCore) row;
Object ds = rowCore.getDataSource(true);
if (!(ds instanceof DownloadManager)) {
return;
}
DownloadManager dm = (DownloadManager) ds;
if (rowCore.getSubItemCount() == 0 && dm.getTorrent() != null && !dm.getTorrent().isSimpleTorrent() && rowCore.isVisible() && dm.getNumFileInfos() > 0) {
DiskManagerFileInfoSet fileInfos = dm.getDiskManagerFileInfoSet();
if (fileInfos != null) {
DiskManagerFileInfo[] files = fileInfos.getFiles();
boolean copied = false;
int pos = 0;
for (int i = 0; i < files.length; i++) {
DiskManagerFileInfo fileInfo = files[i];
if (fileInfo.isSkipped() && (fileInfo.getStorageType() == DiskManagerFileInfo.ST_COMPACT || fileInfo.getStorageType() == DiskManagerFileInfo.ST_REORDER_COMPACT)) {
continue;
}
if (pos != i) {
if (!copied) {
// we *MUSTN'T* modify the returned array!!!!
DiskManagerFileInfo[] oldFiles = files;
files = new DiskManagerFileInfo[files.length];
System.arraycopy(oldFiles, 0, files, 0, files.length);
copied = true;
}
files[pos] = files[i];
}
pos++;
}
if (pos != files.length) {
DiskManagerFileInfo[] oldFiles = files;
files = new DiskManagerFileInfo[pos];
System.arraycopy(oldFiles, 0, files, 0, pos);
}
rowCore.setSubItems(files);
}
}
}
Aggregations