Search in sources :

Example 1 with CoreOperationListener

use of com.biglybt.core.CoreOperationListener in project BiglyBT by BiglySoftware.

the class ProgressWindow method register.

public static void register(Core core) {
    canCloseListener = new canCloseListener() {

        @Override
        public boolean canClose() {
            try {
                Supplier<String> getActiveOps = () -> {
                    List<CoreOperation> ops = core.getOperations();
                    String active = "";
                    for (CoreOperation op : ops) {
                        int type = op.getOperationType();
                        if (type == CoreOperation.OP_DOWNLOAD_CHECKING || type == CoreOperation.OP_DOWNLOAD_COPY || type == CoreOperation.OP_DOWNLOAD_EXPORT || type == CoreOperation.OP_FILE_MOVE) {
                            CoreOperationTask task = op.getTask();
                            if (task != null) {
                                if (active.length() > 128) {
                                    active += ", ...";
                                    break;
                                }
                                active += (active.isEmpty() ? "" : ", ") + task.getName();
                            }
                        }
                    }
                    return (active);
                };
                String active = getActiveOps.get();
                if (active.isEmpty()) {
                    return (true);
                } else {
                    String title = MessageText.getString("coreops.active.quit.title");
                    String text = MessageText.getString("coreops.active.quit.text", new String[] { active });
                    MessageBoxShell mb = new MessageBoxShell(title, text, new String[] { MessageText.getString("UpdateWindow.quit"), MessageText.getString("Content.alert.notuploaded.button.abort") }, 1);
                    mb.open(null);
                    boolean[] close_on_idle = { false };
                    AEThread2.createAndStartDaemon("opschecker", () -> {
                        long idle_start = -1;
                        while (true) {
                            try {
                                Thread.sleep(250);
                            } catch (Throwable e) {
                            }
                            if (getActiveOps.get().isEmpty()) {
                                long now = SystemTime.getMonotonousTime();
                                if (idle_start == -1) {
                                    idle_start = now;
                                } else {
                                    if (now - idle_start > 2500) {
                                        synchronized (close_on_idle) {
                                            close_on_idle[0] = true;
                                        }
                                        mb.close();
                                        break;
                                    }
                                }
                            } else {
                                idle_start = -1;
                            }
                        }
                    });
                    mb.waitUntilClosed();
                    int result = mb.getResult();
                    if (result == 0) {
                        return (true);
                    } else {
                        synchronized (close_on_idle) {
                            return (close_on_idle[0]);
                        }
                    }
                }
            } catch (Throwable e) {
                Debug.out(e);
                return true;
            }
        }
    };
    UIExitUtilsSWT.addListener(canCloseListener);
    core.addOperationListener(new CoreOperationListener() {

        @Override
        public boolean operationExecuteRequest(CoreOperation operation) {
            int type = operation.getOperationType();
            if ((type == CoreOperation.OP_FILE_MOVE || type == CoreOperation.OP_DOWNLOAD_EXPORT || type == CoreOperation.OP_DOWNLOAD_COPY || type == CoreOperation.OP_PROGRESS) && Utils.isThisThreadSWT()) {
                CoreOperationTask task = operation.getTask();
                if (task != null) {
                    if (type == CoreOperation.OP_FILE_MOVE && COConfigurationManager.getBooleanParameter("Suppress File Move Dialog")) {
                        AEThread2.createAndStartDaemon("Core Operation", () -> {
                            try {
                                task.run(operation);
                            } finally {
                                core.removeOperation(operation);
                            }
                        });
                    } else {
                        new ProgressWindow(operation);
                    }
                    return (true);
                }
            }
            return (false);
        }

        @Override
        public void operationAdded(CoreOperation operation) {
        }

        @Override
        public void operationRemoved(CoreOperation operation) {
        }
    });
}
Also used : CoreOperation(com.biglybt.core.CoreOperation) CoreOperationListener(com.biglybt.core.CoreOperationListener) CoreOperationTask(com.biglybt.core.CoreOperationTask) MessageBoxShell(com.biglybt.ui.swt.shells.MessageBoxShell) Supplier(java.util.function.Supplier) UIExitUtilsSWT.canCloseListener(com.biglybt.ui.swt.UIExitUtilsSWT.canCloseListener)

Aggregations

CoreOperation (com.biglybt.core.CoreOperation)1 CoreOperationListener (com.biglybt.core.CoreOperationListener)1 CoreOperationTask (com.biglybt.core.CoreOperationTask)1 UIExitUtilsSWT.canCloseListener (com.biglybt.ui.swt.UIExitUtilsSWT.canCloseListener)1 MessageBoxShell (com.biglybt.ui.swt.shells.MessageBoxShell)1 Supplier (java.util.function.Supplier)1