Search in sources :

Example 36 with PluginInterface

use of com.biglybt.pif.PluginInterface in project BiglyBT by BiglySoftware.

the class RemotePairingWindow method _open.

private void _open() {
    alreadyTested = false;
    pairingManager = PairingManagerFactory.getSingleton();
    PluginInterface piWebUI = getWebUI();
    boolean showFTUX = piWebUI == null || !pairingManager.isEnabled();
    if (skinnedDialog == null || skinnedDialog.isDisposed()) {
        skinnedDialog = new SkinnedDialog("skin3_dlg_remotepairing", "shell", SWT.DIALOG_TRIM);
        skin = skinnedDialog.getSkin();
        soCodeArea = skin.getSkinObject("code-area");
        control = soCodeArea.getControl();
        soEnablePairing = (SWTSkinObjectButton) skin.getSkinObject("enable-pairing");
        soEnablePairing.addSelectionListener(new ButtonListenerAdapter() {

            // @see SWTSkinButtonUtility.ButtonListenerAdapter#pressed(SWTSkinButtonUtility, SWTSkinObject, int)
            @Override
            public void pressed(SWTSkinButtonUtility buttonUtility, SWTSkinObject skinObject, int stateMask) {
                skinObject.getControl().setEnabled(false);
                if (!pairingManager.isEnabled()) {
                    // enabling will automatically get access code and trigger
                    // somethingChanged
                    pairingManager.setEnabled(true);
                    if (SHOW_SPEW) {
                        System.out.println("PAIR] SetEnabled");
                    }
                } else {
                    // picked up
                    if (SHOW_SPEW) {
                        System.out.println("PAIR] AlreadyEnabled");
                    }
                    somethingChanged(pairingManager);
                }
                if (getWebUI() == null) {
                    installWebUI();
                } else {
                    switchToCode();
                }
            }
        });
        soFTUX = skin.getSkinObject("pairing-ftux");
        soCode = skin.getSkinObject("pairing-code");
        soQR = (SWTSkinObjectImage) skin.getSkinObject("pairing-qr");
        if (accessCode != null) {
            setupQR(accessCode);
        }
        soStatusText = (SWTSkinObjectText) skin.getSkinObject("status-text");
        soStatusText.addUrlClickedListener(new SWTSkinObjectText_UrlClickedListener() {

            @Override
            public boolean urlClicked(URLInfo urlInfo) {
                if (urlInfo.url.equals("retry")) {
                    if (DEBUG) {
                        testPairingClass.inc();
                    }
                    alreadyTested = false;
                    testPairing(false);
                    return true;
                }
                return false;
            }
        });
        pairingManager.addListener(this);
        Font font = control.getFont();
        GC gc = new GC(control);
        fontCode = FontUtils.getFontWithHeight(font, gc, Constants.isWindows ? 20 : 18, SWT.BOLD);
        gc.dispose();
        control.setFont(fontCode);
        control.addPaintListener(new PaintListener() {

            @Override
            public void paintControl(PaintEvent e) {
                Color oldColor = e.gc.getForeground();
                Rectangle printArea = ((Composite) e.widget).getClientArea();
                printArea.y += 10;
                printArea.height -= 20;
                int fullWidth = printArea.width;
                int fullHeight = printArea.height;
                GCStringPrinter sp = new GCStringPrinter(e.gc, MessageText.getString("remote.pairing.accesscode"), printArea, false, false, SWT.NONE);
                sp.calculateMetrics();
                Point sizeAccess = sp.getCalculatedSize();
                String drawAccessCode = accessCode == null ? "      " : accessCode;
                int numBoxes = drawAccessCode.length();
                int boxSize = 25;
                int boxSizeAndPadding = 30;
                int allBoxesWidth = numBoxes * boxSizeAndPadding;
                int textPadding = 15;
                printArea.y = (fullHeight - boxSizeAndPadding - sizeAccess.y + textPadding) / 2;
                sp.printString(e.gc, printArea, SWT.CENTER | SWT.TOP);
                e.gc.setBackground(Colors.white);
                e.gc.setForeground(Colors.blue);
                int xStart = (fullWidth - allBoxesWidth) / 2;
                int yStart = printArea.y + sizeAccess.y + textPadding;
                for (int i = 0; i < numBoxes; i++) {
                    Rectangle r = new Rectangle(xStart + (i * boxSizeAndPadding), yStart, boxSize, boxSize);
                    e.gc.fillRectangle(r);
                    e.gc.setForeground(Colors.blues[Colors.BLUES_DARKEST]);
                    e.gc.drawRectangle(r);
                    if (isCodeVisible()) {
                        e.gc.setForeground(oldColor);
                        GCStringPrinter.printString(e.gc, "" + drawAccessCode.charAt(i), r, false, false, SWT.CENTER);
                    }
                }
            }
        });
        soToClipboard = (SWTSkinObjectText) skin.getSkinObject("pair-clipboard");
        soToClipboard.addUrlClickedListener(new SWTSkinObjectText_UrlClickedListener() {

            @Override
            public boolean urlClicked(URLInfo urlInfo) {
                if (urlInfo.url.equals("new")) {
                    try {
                        accessCode = pairingManager.getReplacementAccessCode();
                    } catch (PairingException e) {
                    // ignore.. if error, lastErrorUpdates will trigger
                    }
                    control.redraw();
                    setupQR(accessCode);
                    String s = soToClipboard.getText();
                    int i = s.indexOf("|");
                    if (i > 0) {
                        soToClipboard.setText(s.substring(0, i - 1));
                    }
                } else if (urlInfo.url.equals("clip")) {
                    ClipboardCopy.copyToClipBoard(accessCode);
                }
                return true;
            }
        });
        SWTSkinButtonUtility btnToClipboard = new SWTSkinButtonUtility(soToClipboard);
        btnToClipboard.addSelectionListener(new ButtonListenerAdapter() {

            @Override
            public void pressed(SWTSkinButtonUtility buttonUtility, SWTSkinObject skinObject, int stateMask) {
            }
        });
        skinnedDialog.addCloseListener(new SkinnedDialogClosedListener() {

            @Override
            public void skinDialogClosed(SkinnedDialog dialog) {
                skinnedDialog = null;
                pairingManager.removeListener(RemotePairingWindow.this);
                Utils.disposeSWTObjects(new Object[] { fontCode });
                if (pairingTest != null) {
                    pairingTest.cancel();
                }
            }
        });
        if (showFTUX) {
            soFTUX.getControl().moveAbove(null);
        }
    }
    setCodeVisible(false);
    skinnedDialog.open();
    if (showFTUX) {
        switchToFTUX();
    } else {
        switchToCode();
    }
}
Also used : PaintEvent(org.eclipse.swt.events.PaintEvent) PaintListener(org.eclipse.swt.events.PaintListener) PluginInterface(com.biglybt.pif.PluginInterface) URLInfo(com.biglybt.ui.swt.shells.GCStringPrinter.URLInfo) ButtonListenerAdapter(com.biglybt.ui.swt.skin.SWTSkinButtonUtility.ButtonListenerAdapter) SkinnedDialog(com.biglybt.ui.swt.views.skin.SkinnedDialog) SkinnedDialogClosedListener(com.biglybt.ui.swt.views.skin.SkinnedDialog.SkinnedDialogClosedListener)

Example 37 with PluginInterface

use of com.biglybt.pif.PluginInterface in project BiglyBT by BiglySoftware.

the class MainMDISetup method setupSidebarVuzeUI.

private static void setupSidebarVuzeUI(final MultipleDocumentInterfaceSWT mdi) {
    String[] preferredOrder = new String[] { MultipleDocumentInterface.SIDEBAR_HEADER_DASHBOARD, MultipleDocumentInterface.SIDEBAR_HEADER_TRANSFERS, MultipleDocumentInterface.SIDEBAR_HEADER_VUZE, MultipleDocumentInterface.SIDEBAR_HEADER_DISCOVERY, MultipleDocumentInterface.SIDEBAR_HEADER_DEVICES, MultipleDocumentInterface.SIDEBAR_HEADER_PLUGINS };
    mdi.setPreferredOrder(preferredOrder);
    sb_dashboard = new SB_Dashboard(mdi);
    for (int i = 0; i < preferredOrder.length; i++) {
        String id = preferredOrder[i];
        mdi.registerEntry(id, new MdiEntryCreationListener() {

            @Override
            public MdiEntry createMDiEntry(String id) {
                MdiEntry entry;
                if (id.equals(MultipleDocumentInterface.SIDEBAR_HEADER_DASHBOARD)) {
                    entry = sb_dashboard.setupMDIEntry();
                } else {
                    entry = mdi.createHeader(id, "sidebar." + id, null);
                    if (entry == null) {
                        return (null);
                    }
                    entry.setDefaultExpanded(true);
                    if (id.equals(MultipleDocumentInterface.SIDEBAR_HEADER_PLUGINS)) {
                        entry.addListener(new MdiChildCloseListener() {

                            @Override
                            public void mdiChildEntryClosed(MdiEntry parent, MdiEntry child, boolean user) {
                                if (mdi.getChildrenOf(parent.getId()).size() == 0) {
                                    parent.close(true);
                                }
                            }
                        });
                        PluginInterface pi = PluginInitializer.getDefaultInterface();
                        UIManager uim = pi.getUIManager();
                        MenuManager menuManager = uim.getMenuManager();
                        MenuItem menuItem;
                        menuItem = menuManager.addMenuItem("sidebar." + MultipleDocumentInterface.SIDEBAR_HEADER_PLUGINS, "label.plugin.options");
                        menuItem.setDisposeWithUIDetach(UIInstance.UIT_SWT);
                        menuItem.addListener(new MenuItemListener() {

                            @Override
                            public void selected(MenuItem menu, Object target) {
                                UIFunctions uif = UIFunctionsManager.getUIFunctions();
                                if (uif != null) {
                                    uif.getMDI().showEntryByID(MultipleDocumentInterface.SIDEBAR_SECTION_CONFIG, "plugins");
                                }
                            }
                        });
                    }
                }
                return entry;
            }
        });
    }
    sb_transfers = new SB_Transfers(mdi, true);
    sb_vuze = new SB_Vuze(mdi);
    new SB_Discovery(mdi);
    mdi.loadEntryByID(MultipleDocumentInterface.SIDEBAR_HEADER_DASHBOARD, false);
    mdi.loadEntryByID(MultipleDocumentInterface.SIDEBAR_SECTION_LIBRARY, false);
    if (COConfigurationManager.getBooleanParameter("Show New In Side Bar")) {
        mdi.loadEntryByID(MultipleDocumentInterface.SIDEBAR_SECTION_LIBRARY_UNOPENED, false);
    }
    mdi.loadEntryByID(MultipleDocumentInterface.SIDEBAR_SECTION_SUBSCRIPTIONS, false);
    mdi.loadEntryByID(MultipleDocumentInterface.SIDEBAR_SECTION_DEVICES, false);
    mdi.loadEntryByID(MultipleDocumentInterface.SIDEBAR_SECTION_ACTIVITIES, false);
}
Also used : PluginInterface(com.biglybt.pif.PluginInterface) UIManager(com.biglybt.pif.ui.UIManager) MenuItem(com.biglybt.pif.ui.menus.MenuItem) UIFunctions(com.biglybt.ui.UIFunctions) MenuManager(com.biglybt.pif.ui.menus.MenuManager) MenuItemListener(com.biglybt.pif.ui.menus.MenuItemListener)

Example 38 with PluginInterface

use of com.biglybt.pif.PluginInterface in project BiglyBT by BiglySoftware.

the class TranscodeQueueImpl method process.

protected boolean process(final TranscodeJobImpl job) {
    TranscodePipe pipe = null;
    current_job = job;
    DeviceImpl device = job.getDevice();
    device.setTranscoding(true);
    try {
        job.starts();
        TranscodeProvider provider = job.getProfile().getProvider();
        final TranscodeException[] error = { null };
        TranscodeProfile profile = job.getProfile();
        final TranscodeFileImpl transcode_file = job.getTranscodeFile();
        TranscodeProviderAnalysis provider_analysis;
        boolean xcode_required;
        if (provider == null) {
            xcode_required = false;
            provider_analysis = null;
        } else {
            provider_analysis = analyse(job);
            xcode_required = provider_analysis.getBooleanProperty(TranscodeProviderAnalysis.PT_TRANSCODE_REQUIRED);
            int tt_req;
            if (job.isStream()) {
                // already advertised as a transcoded asset so no option not to
                // transcode (as name/format would change if decided not to transcode and then
                // this would confuse the clients)
                tt_req = TranscodeTarget.TRANSCODE_ALWAYS;
            } else {
                tt_req = job.getTranscodeRequirement();
                if (device instanceof TranscodeTarget) {
                    if (provider_analysis.getLongProperty(TranscodeProviderAnalysis.PT_VIDEO_HEIGHT) == 0) {
                        if (((TranscodeTarget) device).isAudioCompatible(transcode_file)) {
                            tt_req = TranscodeTarget.TRANSCODE_NEVER;
                        }
                    }
                }
            }
            if (tt_req == TranscodeTarget.TRANSCODE_NEVER) {
                xcode_required = false;
            } else if (tt_req == TranscodeTarget.TRANSCODE_ALWAYS) {
                xcode_required = true;
                provider_analysis.setBooleanProperty(TranscodeProviderAnalysis.PT_FORCE_TRANSCODE, true);
            }
        }
        if (xcode_required) {
            final AESemaphore xcode_sem = new AESemaphore("xcode:proc");
            final TranscodeProviderJob[] provider_job = { null };
            TranscodeProviderAdapter xcode_adapter = new TranscodeProviderAdapter() {

                private boolean resolution_updated;

                private final int ETA_AVERAGE_SIZE = 10;

                private int last_eta;

                private int eta_samples;

                private Average eta_average = AverageFactory.MovingAverage(ETA_AVERAGE_SIZE);

                private int last_percent;

                private long initial_file_downloaded = job.getFile().getDownloaded();

                private long file_size = job.getFile().getLength();

                @Override
                public void updateProgress(int percent, int eta_secs, int new_width, int new_height) {
                    last_eta = eta_secs;
                    last_percent = percent;
                    TranscodeProviderJob prov_job = provider_job[0];
                    if (prov_job == null) {
                        return;
                    }
                    int job_state = job.getState();
                    if (job_state == TranscodeJob.ST_CANCELLED || job_state == TranscodeJob.ST_REMOVED) {
                        prov_job.cancel();
                    } else if (paused || job_state == TranscodeJob.ST_PAUSED) {
                        prov_job.pause();
                    } else {
                        if (job_state == TranscodeJob.ST_RUNNING) {
                            prov_job.resume();
                        }
                        job.updateProgress(percent, eta_secs);
                        prov_job.setMaxBytesPerSecond(max_bytes_per_sec);
                        if (!resolution_updated) {
                            if (new_width > 0 && new_height > 0) {
                                transcode_file.setResolution(new_width, new_height);
                                resolution_updated = true;
                            }
                        }
                    }
                }

                @Override
                public void streamStats(long connect_rate, long write_speed) {
                    if (Constants.isOSX && job.getEnableAutoRetry() && job.canUseDirectInput() && job.getAutoRetryCount() == 0) {
                        if (connect_rate > 5 && last_percent < 100) {
                            long eta = (long) eta_average.update(last_eta);
                            eta_samples++;
                            if (eta_samples >= ETA_AVERAGE_SIZE) {
                                long total_time = (eta * 100) / (100 - last_percent);
                                long total_write = total_time * write_speed;
                                DiskManagerFileInfo file = job.getFile();
                                long length = file.getLength();
                                if (length > 0) {
                                    double over_write = ((double) total_write) / length;
                                    if (over_write > 5.0) {
                                        failed(new TranscodeException("Overwrite limit exceeded, abandoning transcode"));
                                        provider_job[0].cancel();
                                    }
                                }
                            }
                        } else {
                            eta_samples = 0;
                        }
                    }
                }

                @Override
                public void failed(TranscodeException e) {
                    try {
                        if (error[0] == null) {
                            error[0] = e;
                        }
                        if (e.isRetryDisabled()) {
                            job.setEnableAutoRetry(false);
                        }
                    } finally {
                        xcode_sem.release();
                    }
                }

                @Override
                public void complete() {
                    try {
                        // sanity check: for incomplete files at the start of the process ensure that they have completed
                        long current_downloaded = job.getFile().getDownloaded();
                        if (file_size > 0 && initial_file_downloaded < file_size && current_downloaded < file_size) {
                            if (error[0] == null) {
                                // actually this ain't so simple as we stream data prior to hash check completion (otherwise for
                                // large piece sizes we could be waiting for 4MB to complete downloading before playback)
                                // and getDownloaded() only returns the verified data size
                                long contiguous_downloaded = 0;
                                try {
                                    DiskManagerFileInfo _file_info = job.getFile();
                                    Download download = _file_info.getDownload();
                                    com.biglybt.core.disk.DiskManagerFileInfo file_info = PluginCoreUtils.unwrap(_file_info);
                                    TOTorrentFile torrent_file = file_info.getTorrentFile();
                                    TOTorrent torrent = torrent_file.getTorrent();
                                    TOTorrentFile[] torrent_files = torrent.getFiles();
                                    long byte_start = 0;
                                    for (TOTorrentFile tf : torrent_files) {
                                        if (tf == torrent_file) {
                                            break;
                                        }
                                        byte_start += tf.getLength();
                                    }
                                    DiskManager dm = download.getDiskManager();
                                    if (dm == null) {
                                        throw (new Exception("Download stopped"));
                                    }
                                    DiskManagerPiece[] pieces = PluginCoreUtils.unwrap(dm).getPieces();
                                    long piece_size = torrent.getPieceLength();
                                    int first_piece_index = (int) (byte_start / piece_size);
                                    int first_piece_offset = (int) (byte_start % piece_size);
                                    int last_piece_index = torrent_file.getLastPieceNumber();
                                    DiskManagerPiece first_piece = pieces[first_piece_index];
                                    if (!first_piece.isDone()) {
                                        boolean[] blocks = first_piece.getWritten();
                                        if (blocks == null) {
                                            if (first_piece.isDone()) {
                                                contiguous_downloaded = first_piece.getLength() - first_piece_offset;
                                            }
                                        } else {
                                            int piece_offset = 0;
                                            for (int j = 0; j < blocks.length; j++) {
                                                if (blocks[j]) {
                                                    int block_size = first_piece.getBlockSize(j);
                                                    piece_offset = piece_offset + block_size;
                                                    if (contiguous_downloaded == 0) {
                                                        if (piece_offset > first_piece_offset) {
                                                            contiguous_downloaded = piece_offset - first_piece_offset;
                                                        }
                                                    } else {
                                                        contiguous_downloaded += block_size;
                                                    }
                                                } else {
                                                    break;
                                                }
                                            }
                                        }
                                    } else {
                                        contiguous_downloaded = first_piece.getLength() - first_piece_offset;
                                        for (int i = first_piece_index + 1; i <= last_piece_index; i++) {
                                            DiskManagerPiece piece = pieces[i];
                                            if (piece.isDone()) {
                                                contiguous_downloaded += piece.getLength();
                                            } else {
                                                boolean[] blocks = piece.getWritten();
                                                if (blocks == null) {
                                                    if (piece.isDone()) {
                                                        contiguous_downloaded += piece.getLength();
                                                    } else {
                                                        break;
                                                    }
                                                } else {
                                                    for (int j = 0; j < blocks.length; j++) {
                                                        if (blocks[j]) {
                                                            contiguous_downloaded += piece.getBlockSize(j);
                                                        } else {
                                                            break;
                                                        }
                                                    }
                                                }
                                                break;
                                            }
                                        }
                                    }
                                } catch (Throwable e) {
                                // Debug.out( e );
                                }
                                if (contiguous_downloaded < file_size) {
                                    // things might have improved, check again
                                    current_downloaded = job.getFile().getDownloaded();
                                    if (current_downloaded < file_size) {
                                        Debug.out("Premature transcode termination: init=" + initial_file_downloaded + ", curr=" + current_downloaded + ", len=" + file_size);
                                        error[0] = new TranscodeException("Transcode terminated prematurely");
                                    }
                                }
                            }
                        }
                    } finally {
                        xcode_sem.release();
                    }
                }
            };
            boolean direct_input = job.useDirectInput();
            if (job.isStream()) {
                /*
					provider_job[0] =
						provider.transcode(
							adapter,
							job.getFile(),
							profile,
							new File( "C:\\temp\\arse").toURI().toURL());
					*/
                pipe = new TranscodePipeStreamSource2(new TranscodePipeStreamSource2.streamListener() {

                    @Override
                    public void gotStream(InputStream is) {
                        job.setStream(is);
                    }
                });
                provider_job[0] = provider.transcode(xcode_adapter, provider_analysis, direct_input, job.getFile(), profile, new URL("tcp://127.0.0.1:" + pipe.getPort()));
            } else {
                File output_file = transcode_file.getCacheFile();
                provider_job[0] = provider.transcode(xcode_adapter, provider_analysis, direct_input, job.getFile(), profile, output_file.toURI().toURL());
            }
            provider_job[0].setMaxBytesPerSecond(max_bytes_per_sec);
            TranscodeQueueListener listener = new TranscodeQueueListener() {

                @Override
                public void jobAdded(TranscodeJob job) {
                }

                @Override
                public void jobChanged(TranscodeJob changed_job) {
                    if (changed_job == job) {
                        int state = job.getState();
                        if (state == TranscodeJob.ST_PAUSED) {
                            provider_job[0].pause();
                        } else if (state == TranscodeJob.ST_RUNNING) {
                            provider_job[0].resume();
                        } else if (state == TranscodeJob.ST_CANCELLED || state == TranscodeJob.ST_STOPPED) {
                            provider_job[0].cancel();
                        }
                    }
                }

                @Override
                public void jobRemoved(TranscodeJob removed_job) {
                    if (removed_job == job) {
                        provider_job[0].cancel();
                    }
                }
            };
            try {
                addListener(listener);
                xcode_sem.reserve();
            } finally {
                removeListener(listener);
            }
            if (error[0] != null) {
                throw (error[0]);
            }
        } else {
            // no transcode required...
            DiskManagerFileInfo source = job.getFile();
            transcode_file.setTranscodeRequired(false);
            if (job.isStream()) {
                PluginInterface av_pi = PluginInitializer.getDefaultInterface().getPluginManager().getPluginInterfaceByID("azupnpav");
                if (av_pi == null) {
                    throw (new TranscodeException("Media Server plugin not found"));
                }
                IPCInterface av_ipc = av_pi.getIPC();
                String url_str = (String) av_ipc.invoke("getContentURL", new Object[] { source });
                if (url_str == null || url_str.length() == 0) {
                    // see if we can use the file directly
                    File source_file = source.getFile();
                    if (source_file.exists()) {
                        job.setStream(new BufferedInputStream(new FileInputStream(source_file)));
                    } else {
                        throw (new TranscodeException("No UPnPAV URL and file doesn't exist"));
                    }
                } else {
                    URL source_url = new URL(url_str);
                    job.setStream(source_url.openConnection().getInputStream());
                }
            } else {
                boolean url_input_source = source instanceof DiskManagerFileInfoURL;
                if (device.getAlwaysCacheFiles() || url_input_source) {
                    PluginInterface av_pi = PluginInitializer.getDefaultInterface().getPluginManager().getPluginInterfaceByID("azupnpav");
                    if (av_pi == null) {
                        throw (new TranscodeException("Media Server plugin not found"));
                    }
                    IPCInterface av_ipc = av_pi.getIPC();
                    String url_str = (String) av_ipc.invoke("getContentURL", new Object[] { source });
                    InputStream is;
                    long length;
                    if (url_str == null || url_str.length() == 0) {
                        if (url_input_source) {
                            ((DiskManagerFileInfoURL) source).download();
                        }
                        File source_file = source.getFile();
                        if (source_file.exists()) {
                            is = new BufferedInputStream(new FileInputStream(source_file));
                            length = source_file.length();
                        } else {
                            throw (new TranscodeException("No UPnPAV URL and file doesn't exist"));
                        }
                    } else {
                        URL source_url = new URL(url_str);
                        URLConnection connection = source_url.openConnection();
                        is = source_url.openConnection().getInputStream();
                        String s = connection.getHeaderField("content-length");
                        if (s != null) {
                            length = Long.parseLong(s);
                        } else {
                            length = -1;
                        }
                    }
                    OutputStream os = null;
                    final boolean[] cancel_copy = { false };
                    TranscodeQueueListener copy_listener = new TranscodeQueueListener() {

                        @Override
                        public void jobAdded(TranscodeJob job) {
                        }

                        @Override
                        public void jobChanged(TranscodeJob changed_job) {
                            if (changed_job == job) {
                                int state = job.getState();
                                if (state == TranscodeJob.ST_PAUSED) {
                                } else if (state == TranscodeJob.ST_RUNNING) {
                                } else if (state == TranscodeJob.ST_CANCELLED || state == TranscodeJob.ST_STOPPED) {
                                    cancel_copy[0] = true;
                                }
                            }
                        }

                        @Override
                        public void jobRemoved(TranscodeJob removed_job) {
                            if (removed_job == job) {
                                cancel_copy[0] = true;
                            }
                        }
                    };
                    try {
                        addListener(copy_listener);
                        os = new FileOutputStream(transcode_file.getCacheFile());
                        long total_copied = 0;
                        byte[] buffer = new byte[128 * 1024];
                        while (true) {
                            if (cancel_copy[0]) {
                                throw (new TranscodeException("Copy cancelled"));
                            }
                            int len = is.read(buffer);
                            if (len <= 0) {
                                break;
                            }
                            os.write(buffer, 0, len);
                            total_copied += len;
                            if (length > 0) {
                                job.updateProgress((int) (total_copied * 100 / length), -1);
                            }
                            total_copied += len;
                        }
                    } finally {
                        try {
                            is.close();
                        } catch (Throwable e) {
                            Debug.out(e);
                        }
                        try {
                            if (os != null) {
                                os.close();
                            }
                        } catch (Throwable e) {
                            Debug.out(e);
                        }
                        removeListener(copy_listener);
                    }
                }
            }
        }
        job.complete();
        return (true);
    } catch (Throwable e) {
        job.failed(e);
        e.printStackTrace();
        if (!job.isStream() && job.getEnableAutoRetry() && job.getAutoRetryCount() == 0 && job.canUseDirectInput() && !job.useDirectInput()) {
            log("Auto-retrying transcode with direct input");
            job.setUseDirectInput();
            job.setAutoRetry(true);
            queue_sem.release();
        }
        return (false);
    } finally {
        if (pipe != null) {
            pipe.destroy();
        }
        device.setTranscoding(false);
        current_job = null;
    }
}
Also used : DiskManager(com.biglybt.pif.disk.DiskManager) TOTorrentFile(com.biglybt.core.torrent.TOTorrentFile) DiskManagerFileInfoURL(com.biglybt.core.download.DiskManagerFileInfoURL) TOTorrentFile(com.biglybt.core.torrent.TOTorrentFile) DiskManagerFileInfoURL(com.biglybt.core.download.DiskManagerFileInfoURL) URL(java.net.URL) DiskManagerPiece(com.biglybt.core.disk.DiskManagerPiece) Download(com.biglybt.pif.download.Download) IPCInterface(com.biglybt.pif.ipc.IPCInterface) DiskManagerFileInfo(com.biglybt.pif.disk.DiskManagerFileInfo) PluginInterface(com.biglybt.pif.PluginInterface) Average(com.biglybt.core.util.average.Average) URLConnection(java.net.URLConnection) TOTorrent(com.biglybt.core.torrent.TOTorrent)

Example 39 with PluginInterface

use of com.biglybt.pif.PluginInterface in project BiglyBT by BiglySoftware.

the class BackupManagerImpl method restoreSupport.

void restoreSupport(File backup_folder, BackupListener listener) {
    try {
        UpdateInstaller installer = null;
        File temp_dir = null;
        boolean ok = false;
        try {
            listener.reportProgress("Reading from " + backup_folder.getAbsolutePath());
            if (!backup_folder.isDirectory()) {
                throw (new Exception("Location '" + backup_folder.getAbsolutePath() + "' must be a directory"));
            }
            listener.reportProgress("Analysing backup");
            File config = new File(backup_folder, ConfigurationManager.CONFIG_FILENAME);
            if (!config.exists()) {
                throw (new Exception("Invalid backup: " + ConfigurationManager.CONFIG_FILENAME + " not found"));
            }
            Map config_map = BDecoder.decode(FileUtil.readFileAsByteArray(config));
            byte[] temp = (byte[]) config_map.get("azureus.user.directory");
            if (temp == null) {
                throw (new Exception("Invalid backup: " + ConfigurationManager.CONFIG_FILENAME + " doesn't contain user directory details"));
            }
            File current_user_dir = new File(SystemProperties.getUserPath());
            File backup_user_dir = new File(new String(temp, "UTF-8"));
            listener.reportProgress("Current user directory:\t" + current_user_dir.getAbsolutePath());
            listener.reportProgress("Backup's user directory:\t" + backup_user_dir.getAbsolutePath());
            temp_dir = AETemporaryFileHandler.createTempDir();
            PluginInterface pi = core.getPluginManager().getDefaultPluginInterface();
            installer = pi.getUpdateManager().createInstaller();
            File[] files = backup_folder.listFiles();
            if (current_user_dir.equals(backup_user_dir)) {
                listener.reportProgress("Directories are the same, no patching required");
                for (File f : files) {
                    File source = new File(temp_dir, f.getName());
                    listener.reportProgress("Creating restore action for '" + f.getName() + "'");
                    copyFiles(f, source);
                    File target = new File(current_user_dir, f.getName());
                    addActions(installer, source, target);
                }
            } else {
                listener.reportProgress("Directories are different, backup requires patching");
                for (File f : files) {
                    File source = new File(temp_dir, f.getName());
                    listener.reportProgress("Creating restore action for '" + f.getName() + "'");
                    if (f.isDirectory() || !f.getName().contains(".config")) {
                        copyFiles(f, source);
                    } else {
                        boolean patched = false;
                        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f), 1024 * 1024);
                        try {
                            Map m = BDecoder.decode(bis);
                            bis.close();
                            bis = null;
                            if (m.size() > 0) {
                                int applied = patch(m, backup_user_dir.getAbsolutePath(), current_user_dir.getAbsolutePath());
                                if (applied > 0) {
                                    listener.reportProgress("    Applied " + applied + " patches");
                                    patched = FileUtil.writeBytesAsFile2(source.getAbsolutePath(), BEncoder.encode(m));
                                    if (!patched) {
                                        throw (new Exception("Failed to write " + source));
                                    }
                                }
                            }
                        } catch (Throwable e) {
                            String name = f.getName();
                            if (name.contains(".bad") || name.contains(".bak")) {
                                listener.reportProgress("    Ignored failure to patch bad configuration file");
                            } else {
                                throw (e);
                            }
                        } finally {
                            if (bis != null) {
                                try {
                                    bis.close();
                                } catch (Throwable e) {
                                }
                            }
                        }
                        if (!patched) {
                            copyFiles(f, source);
                        }
                    }
                    File target = new File(current_user_dir, f.getName());
                    addActions(installer, source, target);
                }
            }
            listener.reportProgress("Restore action creation complete, restart required to complete the operation");
            listener.reportComplete();
            ok = true;
        } finally {
            if (!ok) {
                if (installer != null) {
                    installer.destroy();
                }
                if (temp_dir != null) {
                    FileUtil.recursiveDeleteNoCheck(temp_dir);
                }
            }
        }
    } catch (Throwable e) {
        listener.reportError(e);
    }
}
Also used : PluginInterface(com.biglybt.pif.PluginInterface) FileInputStream(java.io.FileInputStream) BufferedInputStream(java.io.BufferedInputStream) UpdateInstaller(com.biglybt.pif.update.UpdateInstaller) File(java.io.File)

Example 40 with PluginInterface

use of com.biglybt.pif.PluginInterface in project BiglyBT by BiglySoftware.

the class StreamManager method isStreamingUsable.

public boolean isStreamingUsable() {
    if (!(Constants.isWindows || Constants.isOSX)) {
        return (false);
    }
    try {
        PluginManager plug_man = CoreFactory.getSingleton().getPluginManager();
        PluginInterface xcode_pi = plug_man.getPluginInterfaceByID("vuzexcode", false);
        if (xcode_pi != null && !xcode_pi.getPluginState().isOperational()) {
            return (false);
        }
        // otherwise xcode will be installed on demand
        PluginInterface emp_pi = plug_man.getPluginInterfaceByID("azemp", false);
        if (emp_pi == null) {
            return (true);
        }
        if (!emp_pi.getPluginState().isOperational()) {
            return (false);
        }
        // emp installed but need version with prepareWindow, wait for update
        Class<?> epwClass = emp_pi.getPlugin().getClass().getClassLoader().loadClass("com.azureus.plugins.azemp.ui.swt.emp.EmbeddedPlayerWindowSWT");
        Method method = epwClass.getMethod("prepareWindow", new Class[] { String.class });
        return (method != null);
    } catch (Throwable e) {
        return (false);
    }
}
Also used : PluginManager(com.biglybt.pif.PluginManager) PluginInterface(com.biglybt.pif.PluginInterface) Method(java.lang.reflect.Method)

Aggregations

PluginInterface (com.biglybt.pif.PluginInterface)120 URL (java.net.URL)15 IPCInterface (com.biglybt.pif.ipc.IPCInterface)14 UIManager (com.biglybt.pif.ui.UIManager)13 PluginManager (com.biglybt.pif.PluginManager)12 HashMap (java.util.HashMap)12 List (java.util.List)12 Download (com.biglybt.pif.download.Download)10 MenuItemListener (com.biglybt.pif.ui.menus.MenuItemListener)10 File (java.io.File)10 Map (java.util.Map)10 DownloadManager (com.biglybt.core.download.DownloadManager)9 UIFunctions (com.biglybt.ui.UIFunctions)9 ArrayList (java.util.ArrayList)9 PluginException (com.biglybt.pif.PluginException)8 MenuItem (com.biglybt.pif.ui.menus.MenuItem)8 MenuManager (com.biglybt.pif.ui.menus.MenuManager)8 DHTPlugin (com.biglybt.plugin.dht.DHTPlugin)8 DHT (com.biglybt.core.dht.DHT)7 SelectionEvent (org.eclipse.swt.events.SelectionEvent)7