Search in sources :

Example 11 with ResourceDownloaderAdapter

use of com.biglybt.pif.utils.resourcedownloader.ResourceDownloaderAdapter in project BiglyBT by BiglySoftware.

the class SimpleInstallUI method build.

protected void build(Composite parent) {
    parent.setLayout(new FormLayout());
    Button cancel_button = new Button(parent, SWT.NULL);
    cancel_button.setText("Cancel");
    cancel_button.addListener(SWT.Selection, new Listener() {

        @Override
        public void handleEvent(Event arg0) {
            synchronized (SimpleInstallUI.this) {
                cancelled = true;
                if (current_downloader != null) {
                    current_downloader.cancel();
                }
            }
            instance.cancel();
        }
    });
    FormData data = new FormData();
    data.right = new FormAttachment(100, 0);
    data.top = new FormAttachment(0, 0);
    data.bottom = new FormAttachment(100, 0);
    cancel_button.setLayoutData(data);
    final Label label = new Label(parent, SWT.NULL);
    label.setText("blah blah ");
    data = new FormData();
    data.left = new FormAttachment(0, 0);
    data.top = new FormAttachment(cancel_button, 0, SWT.CENTER);
    label.setLayoutData(data);
    final ProgressBar progress = new ProgressBar(parent, SWT.NULL);
    progress.setMinimum(0);
    progress.setMaximum(100);
    progress.setSelection(0);
    data = new FormData();
    data.left = new FormAttachment(label, 4);
    data.top = new FormAttachment(cancel_button, 0, SWT.CENTER);
    data.right = new FormAttachment(cancel_button, -4);
    progress.setLayoutData(data);
    parent.layout(true, true);
    new AEThread2("SimpleInstallerUI", true) {

        @Override
        public void run() {
            try {
                Update[] updates = instance.getUpdates();
                for (Update update : updates) {
                    String name = update.getName();
                    int pos = name.indexOf('/');
                    if (pos >= 0) {
                        name = name.substring(pos + 1);
                    }
                    setLabel(name);
                    ResourceDownloader[] downloaders = update.getDownloaders();
                    for (ResourceDownloader downloader : downloaders) {
                        synchronized (SimpleInstallUI.this) {
                            if (cancelled) {
                                return;
                            }
                            current_downloader = downloader;
                        }
                        setProgress(0);
                        downloader.addListener(new ResourceDownloaderAdapter() {

                            @Override
                            public void reportPercentComplete(ResourceDownloader downloader, int percentage) {
                                setProgress(percentage);
                            }

                            @Override
                            public void reportAmountComplete(ResourceDownloader downloader, long amount) {
                            }
                        });
                        downloader.download();
                    }
                }
                boolean restart_required = false;
                for (int i = 0; i < updates.length; i++) {
                    if (updates[i].getRestartRequired() == Update.RESTART_REQUIRED_YES) {
                        restart_required = true;
                    }
                }
                if (restart_required) {
                    monitor.handleRestart();
                }
            } catch (Throwable e) {
                Debug.out("Install failed", e);
                instance.cancel();
            }
        }

        protected void setLabel(final String str) {
            Utils.execSWTThread(new Runnable() {

                @Override
                public void run() {
                    if (label != null && !label.isDisposed()) {
                        label.setText(str);
                        label.getParent().layout();
                    }
                }
            });
        }

        protected void setProgress(final int percent) {
            Utils.execSWTThread(new Runnable() {

                @Override
                public void run() {
                    if (progress != null && !progress.isDisposed()) {
                        progress.setSelection(percent);
                    }
                }
            });
        }
    }.start();
}
Also used : FormLayout(org.eclipse.swt.layout.FormLayout) FormData(org.eclipse.swt.layout.FormData) Listener(org.eclipse.swt.widgets.Listener) Label(org.eclipse.swt.widgets.Label) ResourceDownloader(com.biglybt.pif.utils.resourcedownloader.ResourceDownloader) Update(com.biglybt.pif.update.Update) AEThread2(com.biglybt.core.util.AEThread2) ResourceDownloaderAdapter(com.biglybt.pif.utils.resourcedownloader.ResourceDownloaderAdapter) Button(org.eclipse.swt.widgets.Button) Event(org.eclipse.swt.widgets.Event) ProgressBar(org.eclipse.swt.widgets.ProgressBar) FormAttachment(org.eclipse.swt.layout.FormAttachment)

Example 12 with ResourceDownloaderAdapter

use of com.biglybt.pif.utils.resourcedownloader.ResourceDownloaderAdapter in project BiglyBT by BiglySoftware.

the class SWTUpdateChecker method checkForUpdate.

@Override
public void checkForUpdate(final UpdateChecker checker) {
    try {
        SWTVersionGetter versionGetter = new SWTVersionGetter(checker);
        boolean update_required = System.getProperty(SystemProperties.SYSPROP_SKIP_SWTCHECK) == null && versionGetter.needsUpdate();
        if (update_required) {
            int update_prevented_version = COConfigurationManager.getIntParameter("swt.update.prevented.version", -1);
            try {
                URL swt_url = SWT.class.getClassLoader().getResource("org/eclipse/swt/SWT.class");
                if (swt_url != null) {
                    String url_str = swt_url.toExternalForm();
                    if (url_str.startsWith("jar:file:")) {
                        File jar_file = FileUtil.getJarFileFromURL(url_str);
                        File expected_dir = new File(checker.getCheckInstance().getManager().getInstallDir());
                        File jar_file_dir = jar_file.getParentFile();
                        if (expected_dir.exists() && jar_file_dir.exists()) {
                            expected_dir = expected_dir.getCanonicalFile();
                            jar_file_dir = jar_file_dir.getCanonicalFile();
                            if (Constants.isUnix) {
                                if (expected_dir.equals(jar_file_dir)) {
                                    // user put it there, so skip everything
                                    return;
                                }
                                // For unix, when swt.jar is in the appdir/swt
                                expected_dir = new File(expected_dir, "swt");
                            }
                            if (expected_dir.equals(jar_file_dir)) {
                                if (update_prevented_version != -1) {
                                    update_prevented_version = -1;
                                    COConfigurationManager.setParameter("swt.update.prevented.version", update_prevented_version);
                                }
                            } else {
                                // we need to periodically remind the user there's a problem as they need to realise that
                                // it is causing ALL updates (core/plugin) to fail
                                String alert = MessageText.getString("swt.alert.cant.update", new String[] { String.valueOf(versionGetter.getCurrentVersion()), String.valueOf(versionGetter.getLatestVersion()), jar_file_dir.toString(), expected_dir.toString() });
                                checker.reportProgress(alert);
                                long last_prompt = COConfigurationManager.getLongParameter("swt.update.prevented.version.time", 0);
                                long now = SystemTime.getCurrentTime();
                                boolean force = now < last_prompt || now - last_prompt > 7 * 24 * 60 * 60 * 1000;
                                if (!checker.getCheckInstance().isAutomatic()) {
                                    force = true;
                                }
                                if (force || update_prevented_version != versionGetter.getCurrentVersion()) {
                                    Logger.log(new LogAlert(LogAlert.REPEATABLE, LogEvent.LT_ERROR, alert));
                                    update_prevented_version = versionGetter.getCurrentVersion();
                                    COConfigurationManager.setParameter("swt.update.prevented.version", update_prevented_version);
                                    COConfigurationManager.setParameter("swt.update.prevented.version.time", now);
                                }
                            }
                        }
                    }
                }
            } catch (Throwable e) {
                Debug.printStackTrace(e);
            }
            if (update_prevented_version == versionGetter.getCurrentVersion()) {
                Logger.log(new LogEvent(LOGID, LogEvent.LT_ERROR, "SWT update aborted due to previously reported issues regarding its install location"));
                checker.failed();
                checker.getCheckInstance().cancel();
                return;
            }
            String[] mirrors = versionGetter.getMirrors();
            ResourceDownloader swtDownloader = null;
            ResourceDownloaderFactory factory = ResourceDownloaderFactoryImpl.getSingleton();
            List<ResourceDownloader> downloaders = new ArrayList<>();
            for (int i = 0; i < mirrors.length; i++) {
                try {
                    downloaders.add(factory.getSuffixBasedDownloader(factory.create(new URL(mirrors[i]))));
                } catch (MalformedURLException e) {
                    // Do nothing
                    if (Logger.isEnabled())
                        Logger.log(new LogEvent(LOGID, LogEvent.LT_WARNING, "Cannot use URL " + mirrors[i] + " (not valid)"));
                }
            }
            for (int i = 0; i < mirrors.length; i++) {
                try {
                    downloaders.add(factory.getSuffixBasedDownloader(factory.createWithAutoPluginProxy(new URL(mirrors[i]))));
                } catch (MalformedURLException e) {
                }
            }
            ResourceDownloader[] resourceDownloaders = (ResourceDownloader[]) downloaders.toArray(new ResourceDownloader[downloaders.size()]);
            swtDownloader = factory.getAlternateDownloader(resourceDownloaders);
            try {
                swtDownloader.getSize();
            } catch (ResourceDownloaderException e) {
                Debug.printStackTrace(e);
            }
            String extra = "";
            if (Constants.isWindows && Constants.is64Bit) {
                extra = " (64-bit)";
            }
            final Update update = checker.addUpdate("SWT Library for " + versionGetter.getPlatform() + extra, new String[] { "SWT is the graphical library used by " + Constants.APP_NAME }, "" + versionGetter.getCurrentVersion(), "" + versionGetter.getLatestVersion(), swtDownloader, Update.RESTART_REQUIRED_YES);
            update.setDescriptionURL(versionGetter.getInfoURL());
            swtDownloader.addListener(new ResourceDownloaderAdapter() {

                @Override
                public boolean completed(ResourceDownloader downloader, InputStream data) {
                    return processData(checker, update, downloader, data);
                }

                @Override
                public void failed(ResourceDownloader downloader, ResourceDownloaderException e) {
                    Debug.out(downloader.getName() + " failed", e);
                    update.complete(false);
                }
            });
        }
    } catch (Throwable e) {
        Logger.log(new LogAlert(LogAlert.UNREPEATABLE, "SWT Version check failed", e));
        checker.failed();
    } finally {
        checker.completed();
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) ResourceDownloaderException(com.biglybt.pif.utils.resourcedownloader.ResourceDownloaderException) ZipInputStream(java.util.zip.ZipInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) ResourceDownloader(com.biglybt.pif.utils.resourcedownloader.ResourceDownloader) Update(com.biglybt.pif.update.Update) URL(java.net.URL) SWT(org.eclipse.swt.SWT) ResourceDownloaderAdapter(com.biglybt.pif.utils.resourcedownloader.ResourceDownloaderAdapter) ResourceDownloaderFactory(com.biglybt.pif.utils.resourcedownloader.ResourceDownloaderFactory) File(java.io.File)

Aggregations

ResourceDownloader (com.biglybt.pif.utils.resourcedownloader.ResourceDownloader)12 ResourceDownloaderAdapter (com.biglybt.pif.utils.resourcedownloader.ResourceDownloaderAdapter)12 Update (com.biglybt.pif.update.Update)7 ResourceDownloaderException (com.biglybt.pif.utils.resourcedownloader.ResourceDownloaderException)7 InputStream (java.io.InputStream)6 URL (java.net.URL)5 ZipInputStream (java.util.zip.ZipInputStream)5 ResourceDownloaderFactory (com.biglybt.pif.utils.resourcedownloader.ResourceDownloaderFactory)4 ArrayList (java.util.ArrayList)4 LogEvent (com.biglybt.core.logging.LogEvent)3 AESemaphore (com.biglybt.core.util.AESemaphore)3 PluginException (com.biglybt.pif.PluginException)3 InstallablePlugin (com.biglybt.pif.installer.InstallablePlugin)3 PluginInstallationListener (com.biglybt.pif.installer.PluginInstallationListener)3 StandardPlugin (com.biglybt.pif.installer.StandardPlugin)3 UpdateCheckInstance (com.biglybt.pif.update.UpdateCheckInstance)3 UpdateCheckInstanceListener (com.biglybt.pif.update.UpdateCheckInstanceListener)3 SFPluginDetails (com.biglybt.pifimpl.update.sf.SFPluginDetails)3 File (java.io.File)3 HashMap (java.util.HashMap)3