Search in sources :

Example 1 with MessageSlideShell

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

the class Alerts method showAlert.

/**
 * @param alert
 *
 * @since 3.0.0.9
 */
protected static void showAlert(final LogAlert alert) {
    final SWTThread instance = SWTThread.getInstance();
    final Display display = instance == null ? null : instance.getDisplay();
    if (alert.err != null) {
        alert.details = Debug.getStackTrace(alert.err);
    }
    for (Iterator<AlertListener> iter = listeners.iterator(); iter.hasNext(); ) {
        AlertListener l = (AlertListener) iter.next();
        if (!l.allowPopup(alert.relatedTo, alert.entryType)) {
            return;
        }
    }
    if (stopping || display == null || display.isDisposed()) {
        try {
            alert_queue_mon.enter();
            List close_alerts = COConfigurationManager.getListParameter("Alerts.raised.at.close", new ArrayList());
            Map alert_map = new HashMap();
            alert_map.put("type", new Long(alert.entryType));
            alert_map.put("message", alert.text);
            alert_map.put("timeout", new Long(alert.getGivenTimeoutSecs()));
            if (alert.details != null) {
                alert_map.put("details", alert.details);
            }
            close_alerts.add(alert_map);
            COConfigurationManager.setParameter("Alerts.raised.at.close", close_alerts);
            return;
        } finally {
            alert_queue_mon.exit();
        }
    }
    if (display == null || display.isDisposed()) {
        return;
    }
    String key = (alert.err == null) ? alert.text : alert.text + ":" + alert.err.toString();
    try {
        alert_history_mon.enter();
        if (!alert.repeatable) {
            if (alert_history.contains(key)) {
                return;
            }
            alert_history.add(key);
            if (alert_history.size() > 512) {
                alert_history.remove(0);
            }
        }
        listUnviewedLogAlerts.add(alert);
    } finally {
        alert_history_mon.exit();
    }
    AlertHistoryListener[] array = listMessageHistoryListeners.toArray(new AlertHistoryListener[0]);
    for (AlertHistoryListener l : array) {
        l.alertHistoryAdded(alert);
    }
    if (alert.forceNotify) {
        Utils.execSWTThread(new AERunnable() {

            @Override
            public void runSupport() {
                int swtIconID = SWT.ICON_INFORMATION;
                switch(alert.getType()) {
                    case LogAlert.LT_WARNING:
                        swtIconID = SWT.ICON_WARNING;
                        break;
                    case LogAlert.LT_ERROR:
                        swtIconID = SWT.ICON_ERROR;
                        break;
                }
                String text = alert.getText();
                int pos = text.indexOf(":");
                String title;
                if (pos == -1) {
                    title = "";
                } else {
                    title = text.substring(0, pos).trim();
                    text = text.substring(pos + 1).trim();
                }
                new MessageSlideShell(display, swtIconID, title, text, alert.details, alert.getContext(), alert.getTimeoutSecs());
            }
        });
    }
}
Also used : MessageSlideShell(com.biglybt.ui.swt.shells.MessageSlideShell) ILogAlertListener(com.biglybt.core.logging.ILogAlertListener) SWTThread(com.biglybt.ui.swt.mainwindow.SWTThread) Display(org.eclipse.swt.widgets.Display)

Example 2 with MessageSlideShell

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

the class UIFunctionsImpl method addTorrentWithOptions.

@Override
public boolean addTorrentWithOptions(final TorrentOpenOptions torrentOptions, Map<String, Object> addOptions) {
    Boolean is_silent = (Boolean) addOptions.get(UIFunctions.OTO_SILENT);
    if (is_silent == null) {
        is_silent = UIFunctions.OTO_SILENT_DEFAULT;
    }
    if (CoreFactory.isCoreRunning()) {
        Core core = CoreFactory.getSingleton();
        GlobalManager gm = core.getGlobalManager();
        // Check if torrent already exists in gm, and add if not
        TOTorrent torrent = torrentOptions.getTorrent();
        DownloadManager existingDownload = gm.getDownloadManager(torrent);
        if (existingDownload != null) {
            if (!is_silent) {
                final String fExistingName = existingDownload.getDisplayName();
                final DownloadManager fExistingDownload = existingDownload;
                fExistingDownload.fireGlobalManagerEvent(GlobalManagerEvent.ET_REQUEST_ATTENTION);
                Utils.execSWTThread(new AERunnable() {

                    @Override
                    public void runSupport() {
                        boolean can_merge = TorrentUtils.canMergeAnnounceURLs(torrentOptions.getTorrent(), fExistingDownload.getTorrent());
                        long existed_for = SystemTime.getCurrentTime() - fExistingDownload.getCreationTime();
                        Shell mainShell = UIFunctionsManagerSWT.getUIFunctionsSWT().getMainShell();
                        if ((Display.getDefault().getActiveShell() == null || !mainShell.isVisible() || mainShell.getMinimized()) && (!can_merge)) {
                            if (existed_for > 15 * 1000) {
                                new MessageSlideShell(Display.getCurrent(), SWT.ICON_INFORMATION, MSG_ALREADY_EXISTS, null, new String[] { // : prefix is deliberate to disable click on ref in message as might be an unwanted action
                                ":" + torrentOptions.sOriginatingLocation, fExistingName, MessageText.getString(MSG_ALREADY_EXISTS_NAME) }, new Object[] { fExistingDownload }, -1);
                            }
                        } else {
                            if (can_merge) {
                                String text = MessageText.getString(MSG_ALREADY_EXISTS + ".text", new String[] { ":" + torrentOptions.sOriginatingLocation, fExistingName, MessageText.getString(MSG_ALREADY_EXISTS_NAME) });
                                text += "\n\n" + MessageText.getString("openTorrentWindow.mb.alreadyExists.merge");
                                MessageBoxShell mb = new MessageBoxShell(SWT.YES | SWT.NO, MessageText.getString(MSG_ALREADY_EXISTS + ".title"), text);
                                mb.open(new UserPrompterResultListener() {

                                    @Override
                                    public void prompterClosed(int result) {
                                        if (result == SWT.YES) {
                                            TorrentUtils.mergeAnnounceURLs(torrentOptions.getTorrent(), fExistingDownload.getTorrent());
                                        }
                                    }
                                });
                            } else {
                                if (existed_for > 15 * 1000) {
                                    MessageBoxShell mb = new MessageBoxShell(SWT.OK, MSG_ALREADY_EXISTS, new String[] { ":" + torrentOptions.sOriginatingLocation, fExistingName, MessageText.getString(MSG_ALREADY_EXISTS_NAME) });
                                    mb.open(null);
                                }
                            }
                        }
                    }
                });
            }
            if (torrentOptions.getDeleteFileOnCancel()) {
                File torrentFile = new File(torrentOptions.sFileName);
                torrentFile.delete();
            }
            return (true);
        } else {
            try {
                final DownloadStub archived = core.getPluginManager().getDefaultPluginInterface().getDownloadManager().lookupDownloadStub(torrent.getHash());
                if (archived != null) {
                    if (is_silent) {
                        // restore it for them
                        archived.destubbify();
                        if (torrentOptions.getDeleteFileOnCancel()) {
                            File torrentFile = new File(torrentOptions.sFileName);
                            torrentFile.delete();
                        }
                        return (true);
                    } else {
                        Utils.execSWTThread(new AERunnable() {

                            @Override
                            public void runSupport() {
                                Shell mainShell = UIFunctionsManagerSWT.getUIFunctionsSWT().getMainShell();
                                String existingName = archived.getName();
                                if (Display.getDefault().getActiveShell() == null || !mainShell.isVisible() || mainShell.getMinimized()) {
                                    new MessageSlideShell(Display.getCurrent(), SWT.ICON_INFORMATION, "OpenTorrentWindow.mb.inArchive", null, new String[] { existingName }, new Object[0], -1);
                                } else {
                                    MessageBoxShell mb = new MessageBoxShell(SWT.OK, "OpenTorrentWindow.mb.inArchive", new String[] { existingName });
                                    mb.open(null);
                                }
                            }
                        });
                        return (true);
                    }
                }
            } catch (Throwable e) {
                Debug.out(e);
            }
            if (!is_silent) {
                try {
                    DownloadHistoryManager dlm = (DownloadHistoryManager) core.getGlobalManager().getDownloadHistoryManager();
                    final long[] existing = dlm.getDates(torrentOptions.getTorrent().getHash());
                    if (existing != null) {
                        long redownloaded = existing[3];
                        if (SystemTime.getCurrentTime() - redownloaded > 60 * 10 * 1000) {
                            Utils.execSWTThread(new AERunnable() {

                                @Override
                                public void runSupport() {
                                    Shell mainShell = UIFunctionsManagerSWT.getUIFunctionsSWT().getMainShell();
                                    if (mainShell != null && !mainShell.isDisposed()) {
                                        new MessageSlideShell(mainShell.getDisplay(), SWT.ICON_INFORMATION, "OpenTorrentWindow.mb.inHistory", null, new String[] { torrentOptions.getTorrentName(), new SimpleDateFormat().format(new Date(existing[0])) }, new Object[] {}, -1);
                                    }
                                }
                            });
                        }
                    }
                } catch (Throwable e) {
                    Debug.out(e);
                }
            }
        }
    }
    Boolean force = (Boolean) addOptions.get(UIFunctions.OTO_FORCE_OPEN);
    if (force == null) {
        force = UIFunctions.OTO_FORCE_OPEN_DEFAULT;
    }
    if (!force) {
        TOTorrent torrent = torrentOptions.getTorrent();
        boolean is_featured = torrent != null && PlatformTorrentUtils.isFeaturedContent(torrent);
        String showAgainMode = COConfigurationManager.getStringParameter(ConfigurationDefaults.CFG_TORRENTADD_OPENOPTIONS);
        if (is_featured || is_silent || (showAgainMode != null && ((showAgainMode.equals(ConfigurationDefaults.CFG_TORRENTADD_OPENOPTIONS_NEVER)) || (showAgainMode.equals(ConfigurationDefaults.CFG_TORRENTADD_OPENOPTIONS_MANY) && torrentOptions.getFiles() != null && torrentOptions.getFiles().length == 1)))) {
            // we're about to silently add the download - ensure that it is going to be saved somewhere vaguely sensible
            // as the current save location is simply taken from the 'default download' config which can be blank (for example)
            boolean looks_good = false;
            String save_loc = torrentOptions.getParentDir().trim();
            if (save_loc.length() == 0) {
            // blank :(
            } else if (save_loc.startsWith(".")) {
            // relative to who knows where
            } else {
                File f = new File(save_loc);
                if (!f.exists()) {
                    f.mkdirs();
                }
                if (f.isDirectory() && FileUtil.canWriteToDirectory(f)) {
                    if (!f.equals(AETemporaryFileHandler.getTempDirectory())) {
                        looks_good = true;
                    }
                }
            }
            if (looks_good) {
                TorrentManagerImpl t_man = TorrentManagerImpl.getSingleton();
                t_man.optionsAdded(torrentOptions);
                t_man.optionsAccepted(torrentOptions);
                boolean ok = TorrentOpener.addTorrent(torrentOptions);
                t_man.optionsRemoved(torrentOptions);
                return (ok);
            }
            torrentOptions.setParentDir("");
            if (is_silent) {
                return (false);
            } else {
                MessageBoxShell mb = new MessageBoxShell(SWT.OK | SWT.ICON_ERROR, "OpenTorrentWindow.mb.invaliddefsave", new String[] { save_loc });
                mb.open(new UserPrompterResultListener() {

                    @Override
                    public void prompterClosed(int result) {
                        OpenTorrentOptionsWindow.addTorrent(torrentOptions);
                    }
                });
            }
            return (true);
        }
    }
    if (is_silent) {
        return (false);
    } else {
        OpenTorrentOptionsWindow.addTorrent(torrentOptions);
        return (true);
    }
}
Also used : DownloadHistoryManager(com.biglybt.core.history.DownloadHistoryManager) MessageSlideShell(com.biglybt.ui.swt.shells.MessageSlideShell) MessageBoxShell(com.biglybt.ui.swt.shells.MessageBoxShell) TorrentManagerImpl(com.biglybt.pifimpl.local.torrent.TorrentManagerImpl) DownloadManager(com.biglybt.core.download.DownloadManager) Date(java.util.Date) Point(org.eclipse.swt.graphics.Point) DownloadStub(com.biglybt.pif.download.DownloadStub) MessageSlideShell(com.biglybt.ui.swt.shells.MessageSlideShell) MessageBoxShell(com.biglybt.ui.swt.shells.MessageBoxShell) GlobalManager(com.biglybt.core.global.GlobalManager) TOTorrent(com.biglybt.core.torrent.TOTorrent) File(java.io.File) SimpleDateFormat(java.text.SimpleDateFormat) UISWTViewCore(com.biglybt.ui.swt.pifimpl.UISWTViewCore) Core(com.biglybt.core.Core)

Aggregations

MessageSlideShell (com.biglybt.ui.swt.shells.MessageSlideShell)2 Core (com.biglybt.core.Core)1 DownloadManager (com.biglybt.core.download.DownloadManager)1 GlobalManager (com.biglybt.core.global.GlobalManager)1 DownloadHistoryManager (com.biglybt.core.history.DownloadHistoryManager)1 ILogAlertListener (com.biglybt.core.logging.ILogAlertListener)1 TOTorrent (com.biglybt.core.torrent.TOTorrent)1 DownloadStub (com.biglybt.pif.download.DownloadStub)1 TorrentManagerImpl (com.biglybt.pifimpl.local.torrent.TorrentManagerImpl)1 SWTThread (com.biglybt.ui.swt.mainwindow.SWTThread)1 UISWTViewCore (com.biglybt.ui.swt.pifimpl.UISWTViewCore)1 MessageBoxShell (com.biglybt.ui.swt.shells.MessageBoxShell)1 File (java.io.File)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 Point (org.eclipse.swt.graphics.Point)1 Display (org.eclipse.swt.widgets.Display)1