Search in sources :

Example 1 with AdvRenameWindow

use of com.biglybt.ui.swt.shells.AdvRenameWindow 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);
                }
            });
        }
    }
}
Also used : AdvRenameWindow(com.biglybt.ui.swt.shells.AdvRenameWindow) DownloadManager(com.biglybt.core.download.DownloadManager) TextViewerWindow(com.biglybt.ui.swt.TextViewerWindow) List(java.util.List) DiskManagerFileInfo(com.biglybt.core.disk.DiskManagerFileInfo) File(java.io.File)

Example 2 with AdvRenameWindow

use of com.biglybt.ui.swt.shells.AdvRenameWindow in project BiglyBT by BiglySoftware.

the class ManagerUtils method advancedRename.

private static void advancedRename(List<DownloadManager> list) {
    if (list.isEmpty()) {
        return;
    }
    DownloadManager dm = list.remove(0);
    AdvRenameWindow window = new AdvRenameWindow(dm);
    window.open(result -> {
        if (result == -1 || list.isEmpty()) {
            return;
        }
        advancedRename(list);
    });
}
Also used : AdvRenameWindow(com.biglybt.ui.swt.shells.AdvRenameWindow) DownloadManager(com.biglybt.core.download.DownloadManager)

Aggregations

DownloadManager (com.biglybt.core.download.DownloadManager)2 AdvRenameWindow (com.biglybt.ui.swt.shells.AdvRenameWindow)2 DiskManagerFileInfo (com.biglybt.core.disk.DiskManagerFileInfo)1 TextViewerWindow (com.biglybt.ui.swt.TextViewerWindow)1 File (java.io.File)1 List (java.util.List)1