Search in sources :

Example 1 with SubscriptionManager

use of com.biglybt.pif.utils.subscriptions.SubscriptionManager in project BiglyBT by BiglySoftware.

the class SubscriptionRSSFeed method generate.

@Override
public boolean generate(TrackerWebPageRequest request, TrackerWebPageResponse response) throws IOException {
    InetSocketAddress local_address = request.getLocalAddress();
    if (local_address == null) {
        return (false);
    }
    URL url = request.getAbsoluteURL();
    String path = url.getPath();
    path = path.substring(PROVIDER.length() + 1);
    try {
        SubscriptionManager sman = plugin_interface.getUtilities().getSubscriptionManager();
        Subscription[] subs = sman.getSubscriptions();
        OutputStream os = response.getOutputStream();
        PrintWriter pw = new PrintWriter(new OutputStreamWriter(os, "UTF-8"));
        if (path.length() <= 1) {
            response.setContentType("text/html; charset=UTF-8");
            pw.println("<HTML><HEAD><TITLE>" + Constants.APP_NAME + " Subscription Feeds</TITLE></HEAD><BODY>");
            for (Subscription s : subs) {
                if (s.isSearchTemplate()) {
                    continue;
                }
                String name = s.getName();
                pw.println("<LI><A href=\"" + PROVIDER + "/" + s.getID() + "\">" + name + "</A></LI>");
            }
            pw.println("</BODY></HTML>");
        } else {
            String id = path.substring(1);
            Subscription subscription = null;
            for (Subscription s : subs) {
                if (s.getID().equals(id)) {
                    subscription = s;
                    break;
                }
            }
            if (subscription == null) {
                response.setReplyStatus(404);
                return (true);
            }
            URL feed_url = url;
            // absolute url is borked as it doesn't set the host properly. hack
            String host = (String) request.getHeaders().get("host");
            if (host != null) {
                int pos = host.indexOf(':');
                if (pos != -1) {
                    host = host.substring(0, pos);
                }
                feed_url = UrlUtils.setHost(url, host);
            }
            response.setContentType("application/xml; charset=UTF-8");
            pw.println("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
            pw.println("<rss version=\"2.0\" " + "xmlns:vuze=\"http://www.vuze.com\" " + "xmlns:media=\"http://search.yahoo.com/mrss/\" " + "xmlns:atom=\"http://www.w3.org/2005/Atom\" " + "xmlns:itunes=\"http://www.itunes.com/dtds/podcast-1.0.dtd\">");
            pw.println("<channel>");
            String channel_title = "Vuze Subscription: " + escape(subscription.getName());
            pw.println("<title>" + channel_title + "</title>");
            pw.println("<link>http://vuze.com</link>");
            pw.println("<atom:link href=\"" + escape(feed_url.toExternalForm()) + "\" rel=\"self\" type=\"application/rss+xml\" />");
            pw.println("<description>" + Constants.APP_NAME + " RSS Feed for subscription " + escape(subscription.getName()) + "</description>");
            pw.println("<itunes:image href=\"http://www.vuze.com/img/vuze_icon_128.png\"/>");
            pw.println("<image><url>http://www.vuze.com/img/vuze_icon_128.png</url><title>" + channel_title + "</title><link>http://vuze.com</link></image>");
            SubscriptionResult[] results = subscription.getResults();
            String feed_date_key = "subscriptions.feed_date." + subscription.getID();
            long feed_date = COConfigurationManager.getLongParameter(feed_date_key);
            boolean new_date = false;
            for (SubscriptionResult result : results) {
                Date date = (Date) result.getProperty(SearchResult.PR_PUB_DATE);
                long millis = date.getTime();
                if (millis > feed_date) {
                    feed_date = millis;
                    new_date = true;
                }
            }
            if (new_date) {
                COConfigurationManager.setParameter(feed_date_key, feed_date);
            }
            pw.println("<pubDate>" + TimeFormatter.getHTTPDate(feed_date) + "</pubDate>");
            for (SubscriptionResult result : results) {
                try {
                    pw.println("<item>");
                    String name = (String) result.getProperty(SearchResult.PR_NAME);
                    pw.println("<title>" + escape(name) + "</title>");
                    Date date = (Date) result.getProperty(SearchResult.PR_PUB_DATE);
                    if (date != null) {
                        pw.println("<pubDate>" + TimeFormatter.getHTTPDate(date.getTime()) + "</pubDate>");
                    }
                    String uid = (String) result.getProperty(SearchResult.PR_UID);
                    if (uid != null) {
                        pw.println("<guid isPermaLink=\"false\">" + escape(uid) + "</guid>");
                    }
                    String link = (String) result.getProperty(SearchResult.PR_DOWNLOAD_LINK);
                    Long size = (Long) result.getProperty(SearchResult.PR_SIZE);
                    if (link != null) {
                        pw.println("<link>" + escape(link) + "</link>");
                        if (size != null) {
                            pw.println("<media:content fileSize=\"" + size + "\" url=\"" + escape(link) + "\"/>");
                        }
                    }
                    if (size != null) {
                        pw.println("<vuze:size>" + size + "</vuze:size>");
                    }
                    Long seeds = (Long) result.getProperty(SearchResult.PR_SEED_COUNT);
                    if (seeds != null) {
                        pw.println("<vuze:seeds>" + seeds + "</vuze:seeds>");
                    }
                    Long peers = (Long) result.getProperty(SearchResult.PR_LEECHER_COUNT);
                    if (peers != null) {
                        pw.println("<vuze:peers>" + peers + "</vuze:peers>");
                    }
                    Long rank = (Long) result.getProperty(SearchResult.PR_RANK);
                    if (rank != null) {
                        pw.println("<vuze:rank>" + rank + "</vuze:rank>");
                    }
                    pw.println("</item>");
                } catch (Throwable e) {
                    Debug.out(e);
                }
            }
            pw.println("</channel>");
            pw.println("</rss>");
        }
        pw.flush();
    } catch (Throwable e) {
        Debug.out(e);
        throw (new IOException(Debug.getNestedExceptionMessage(e)));
    }
    return (true);
}
Also used : SubscriptionResult(com.biglybt.pif.utils.subscriptions.SubscriptionResult) InetSocketAddress(java.net.InetSocketAddress) OutputStream(java.io.OutputStream) SubscriptionManager(com.biglybt.pif.utils.subscriptions.SubscriptionManager) IOException(java.io.IOException) URL(java.net.URL) Date(java.util.Date) OutputStreamWriter(java.io.OutputStreamWriter) Subscription(com.biglybt.pif.utils.subscriptions.Subscription) PrintWriter(java.io.PrintWriter)

Example 2 with SubscriptionManager

use of com.biglybt.pif.utils.subscriptions.SubscriptionManager in project BiglyBT by BiglySoftware.

the class OpenTorrentWindow method TorrentDownloaderEvent.

@Override
public void TorrentDownloaderEvent(int state, TorrentDownloader inf) {
    // We set this flag to false if we detected the file was not a torrent
    if (!inf.getDeleteFileOnCancel() && (state == TorrentDownloader.STATE_CANCELLED || state == TorrentDownloader.STATE_ERROR || state == TorrentDownloader.STATE_DUPLICATE || state == TorrentDownloader.STATE_FINISHED)) {
        File file = inf.getFile();
        // we already know it isn't a torrent.. we are just using the call
        // to popup the message
        boolean done = false;
        if (RSSUtils.isRSSFeed(file)) {
            try {
                URL url = new URL(inf.getURL());
                UIManager ui_manager = StaticUtilities.getUIManager(10 * 1000);
                if (ui_manager != null) {
                    String details = MessageText.getString("subscription.request.add.message", new String[] { inf.getURL() });
                    long res = ui_manager.showMessageBox("subscription.request.add.title", "!" + details + "!", UIManagerEvent.MT_YES | UIManagerEvent.MT_NO);
                    if (res == UIManagerEvent.MT_YES) {
                        SubscriptionManager sm = PluginInitializer.getDefaultInterface().getUtilities().getSubscriptionManager();
                        sm.requestSubscription(url);
                        done = true;
                    }
                }
            } catch (Throwable e) {
                Debug.out(e);
            }
        }
        if (!done) {
            TorrentUtil.isFileTorrent(inf.getURL(), file, inf.getURL(), true);
        }
        if (file.exists()) {
            file.delete();
        }
        return;
    }
    if (state == TorrentDownloader.STATE_INIT) {
    } else if (state == TorrentDownloader.STATE_FINISHED) {
        File file = inf.getFile();
        TorrentOpenOptions torrentOptions = new TorrentOpenOptions();
        if (!TorrentOpener.mergeFileIntoTorrentInfo(file.getAbsolutePath(), inf.getURL(), torrentOptions)) {
            if (file.exists())
                file.delete();
        } else {
            UIFunctions uif = UIFunctionsManager.getUIFunctions();
            boolean b = uif.addTorrentWithOptions(false, torrentOptions);
            if (!b && file.exists()) {
                file.delete();
            }
        }
    } else if (state == TorrentDownloader.STATE_CANCELLED || state == TorrentDownloader.STATE_ERROR || state == TorrentDownloader.STATE_DUPLICATE) {
    } else if (state == TorrentDownloader.STATE_DOWNLOADING) {
        int count = inf.getLastReadCount();
        int numRead = inf.getTotalRead();
        if (!inf.getDeleteFileOnCancel() && numRead >= 16384) {
            inf.cancel();
        } else if (numRead == count && count > 0) {
            final byte[] bytes = inf.getLastReadBytes();
            if (bytes[0] != 'd' && bytes[0] != '<') {
                inf.setDeleteFileOnCancel(false);
            }
        }
    } else {
        return;
    }
}
Also used : TorrentOpenOptions(com.biglybt.core.torrent.impl.TorrentOpenOptions) UIFunctions(com.biglybt.ui.UIFunctions) UIManager(com.biglybt.pif.ui.UIManager) SubscriptionManager(com.biglybt.pif.utils.subscriptions.SubscriptionManager) File(java.io.File) URL(java.net.URL)

Example 3 with SubscriptionManager

use of com.biglybt.pif.utils.subscriptions.SubscriptionManager in project BiglyBT by BiglySoftware.

the class BuddyPluginViewBetaChat method buildRSSButton.

private void buildRSSButton(Composite parent) {
    boolean sharing_view = chat.getViewType() == BuddyPluginBeta.VIEW_TYPE_SHARING;
    Runnable create_it = new Runnable() {

        public void run() {
            try {
                String url = encodeRSSURL(chat);
                SubscriptionManager sm = PluginInitializer.getDefaultInterface().getUtilities().getSubscriptionManager();
                Map<String, Object> options = new HashMap<>();
                if (chat.isAnonymous()) {
                    options.put(SubscriptionManager.SO_ANONYMOUS, true);
                }
                options.put(SubscriptionManager.SO_NAME, chat.getName());
                sm.requestSubscription(new URL(url), options);
            } catch (Throwable e) {
                Debug.out(e);
            }
        }
    };
    if (sharing_view) {
        Label rss_button = new Label(parent, SWT.NONE);
        final Image rss_image_normal = ImageLoader.getInstance().getImage("image.sidebar.subscriptions");
        final Image rss_image_gray = ImageLoader.getInstance().getImage("image.sidebar.subscriptions-gray");
        rss_button.setImage(rss_image_gray);
        rss_button.setCursor(parent.getDisplay().getSystemCursor(SWT.CURSOR_HAND));
        GridData grid_data = new GridData(GridData.FILL_HORIZONTAL);
        grid_data.widthHint = rss_image_gray.getBounds().width;
        grid_data.heightHint = rss_image_gray.getBounds().height;
        rss_button.setLayoutData(grid_data);
        rss_button.setToolTipText(MessageText.getString("azbuddy.dchat.rss.subscribe.info"));
        rss_button.addMouseTrackListener(new MouseTrackAdapter() {

            @Override
            public void mouseExit(MouseEvent arg0) {
                rss_button.setImage(rss_image_gray);
                rss_button.redraw();
            }

            @Override
            public void mouseEnter(MouseEvent arg0) {
                rss_button.setImage(rss_image_normal);
                rss_button.redraw();
            }
        });
        rss_button.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseUp(MouseEvent e) {
                create_it.run();
            }
        });
    } else {
        Button rss_button = new Button(parent, SWT.PUSH);
        Image rss_image = ImageLoader.getInstance().getImage("image.sidebar.subscriptions");
        rss_button.setImage(rss_image);
        GridData grid_data = new GridData(GridData.FILL_HORIZONTAL);
        grid_data.widthHint = rss_image.getBounds().width;
        grid_data.heightHint = rss_image.getBounds().height;
        rss_button.setLayoutData(grid_data);
        // rss_button.setEnabled(false);
        rss_button.setToolTipText(MessageText.getString("azbuddy.dchat.rss.subscribe.info"));
        rss_button.addSelectionListener(new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent ev) {
                create_it.run();
            }
        });
    }
}
Also used : MouseEvent(org.eclipse.swt.events.MouseEvent) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) BufferedLabel(com.biglybt.ui.swt.components.BufferedLabel) Label(org.eclipse.swt.widgets.Label) LinkLabel(com.biglybt.ui.swt.components.LinkLabel) MouseTrackAdapter(org.eclipse.swt.events.MouseTrackAdapter) MouseAdapter(org.eclipse.swt.events.MouseAdapter) SubscriptionManager(com.biglybt.pif.utils.subscriptions.SubscriptionManager) Image(org.eclipse.swt.graphics.Image) URL(java.net.URL) Button(org.eclipse.swt.widgets.Button) AERunnable(com.biglybt.core.util.AERunnable) GridData(org.eclipse.swt.layout.GridData) SelectionEvent(org.eclipse.swt.events.SelectionEvent)

Example 4 with SubscriptionManager

use of com.biglybt.pif.utils.subscriptions.SubscriptionManager in project BiglyBT by BiglySoftware.

the class UtilitiesImpl method getSubscriptionManager.

@Override
public SubscriptionManager getSubscriptionManager() throws SubscriptionException {
    try {
        Method m = Class.forName("com.biglybt.core.subs.SubscriptionManagerFactory").getMethod("getSingleton");
        final PluginSubscriptionManager sm = (PluginSubscriptionManager) m.invoke(null);
        return (new SubscriptionManager() {

            @Override
            public void requestSubscription(URL url) {
                sm.requestSubscription(url, new HashMap<String, Object>());
            }

            @Override
            public void requestSubscription(URL url, Map<String, Object> options) {
                sm.requestSubscription(url, options);
            }

            @Override
            public void requestSubscription(SearchProvider sp, Map<String, Object> search_parameters) throws SubscriptionException {
                sm.requestSubscription(sp, search_parameters);
            }

            @Override
            public Subscription[] getSubscriptions() {
                PluginSubscription[] p_subs = sm.getSubscriptions(true);
                Subscription[] subs = new Subscription[p_subs.length];
                for (int i = 0; i < subs.length; i++) {
                    final PluginSubscription p_sub = p_subs[i];
                    subs[i] = new Subscription() {

                        @Override
                        public String getID() {
                            return (p_sub.getID());
                        }

                        @Override
                        public String getName() {
                            return (p_sub.getName());
                        }

                        @Override
                        public boolean isSearchTemplate() {
                            return (p_sub.isSearchTemplate());
                        }

                        @Override
                        public SubscriptionResult[] getResults() {
                            PluginSubscriptionResult[] p_results = p_sub.getResults(false);
                            SubscriptionResult[] results = new SubscriptionResult[p_results.length];
                            for (int i = 0; i < results.length; i++) {
                                final PluginSubscriptionResult p_res = p_results[i];
                                results[i] = new SubscriptionResult() {

                                    private Map<Integer, Object> map = p_res.toPropertyMap();

                                    @Override
                                    public Object getProperty(int property_name) {
                                        return (map.get(property_name));
                                    }

                                    @Override
                                    public boolean isRead() {
                                        return (p_res.getRead());
                                    }

                                    @Override
                                    public void setRead(boolean read) {
                                        p_res.setRead(read);
                                    }
                                };
                            }
                            return (results);
                        }
                    };
                }
                return (subs);
            }
        });
    } catch (Throwable e) {
        throw (new SubscriptionException("Subscriptions unavailable", e));
    }
}
Also used : SubscriptionException(com.biglybt.pif.utils.subscriptions.SubscriptionException) SubscriptionResult(com.biglybt.pif.utils.subscriptions.SubscriptionResult) SearchProvider(com.biglybt.pif.utils.search.SearchProvider) Method(java.lang.reflect.Method) SubscriptionManager(com.biglybt.pif.utils.subscriptions.SubscriptionManager) URL(java.net.URL) Subscription(com.biglybt.pif.utils.subscriptions.Subscription)

Example 5 with SubscriptionManager

use of com.biglybt.pif.utils.subscriptions.SubscriptionManager in project BiglyBT by BiglySoftware.

the class TorrentOpener method mergeFileIntoTorrentInfo.

/**
 * Creates a TorrentInfo from a file.  Prompts user if the file is invalid,
 * torrent already exists
 *
 * @param sFileName
 * @param sOriginatingLocation
 * @return
 * @since 5.0.0.1
 */
// TODO: i18n
public static boolean mergeFileIntoTorrentInfo(String sFileName, final String sOriginatingLocation, TorrentOpenOptions torrentOptions) {
    TOTorrent torrent = null;
    File torrentFile;
    boolean bDeleteFileOnCancel = false;
    // actually made a copy.
    try {
        if (sFileName.startsWith("file://localhost/")) {
            sFileName = UrlUtils.decode(sFileName.substring(16));
        }
        final File fOriginal = new File(sFileName);
        if (!fOriginal.isFile() || !fOriginal.exists()) {
            UIFunctionsManager.getUIFunctions().showErrorMessage("OpenTorrentWindow.mb.openError", fOriginal.toString(), new String[] { UrlUtils.decode(sOriginatingLocation), "Not a File" });
            return false;
        }
        if (fOriginal.length() > TorrentUtils.MAX_TORRENT_FILE_SIZE) {
            UIFunctionsManager.getUIFunctions().showErrorMessage("OpenTorrentWindow.mb.openError", fOriginal.toString(), new String[] { UrlUtils.decode(sOriginatingLocation), "Too large to be a torrent" });
            return false;
        }
        torrentFile = TorrentUtils.copyTorrentFileToSaveDir(fOriginal, true);
        bDeleteFileOnCancel = !fOriginal.equals(torrentFile);
    // TODO if the files are still equal, and it isn't in the save
    // dir, we should copy it to a temp file in case something
    // re-writes it.  No need to copy a torrent coming from the
    // downloader though..
    } catch (IOException e1) {
        // Use torrent in wherever it is and hope for the best
        // XXX Should error instead?
        Debug.out(e1);
        torrentFile = new File(sFileName);
    }
    VuzeFileHandler vfh = VuzeFileHandler.getSingleton();
    VuzeFile vf = vfh.loadVuzeFile(torrentFile);
    if (vf != null) {
        vfh.handleFiles(new VuzeFile[] { vf }, VuzeFileComponent.COMP_TYPE_NONE);
        return false;
    }
    if (RSSUtils.isRSSFeed(torrentFile)) {
        boolean done = false;
        try {
            URL url = new URL(sOriginatingLocation);
            UIManager ui_manager = StaticUtilities.getUIManager(10 * 1000);
            if (ui_manager != null) {
                String details = MessageText.getString("subscription.request.add.message", new String[] { sOriginatingLocation });
                long res = ui_manager.showMessageBox("subscription.request.add.title", "!" + details + "!", UIManagerEvent.MT_YES | UIManagerEvent.MT_NO);
                if (res == UIManagerEvent.MT_YES) {
                    SubscriptionManager sm = PluginInitializer.getDefaultInterface().getUtilities().getSubscriptionManager();
                    sm.requestSubscription(url);
                    done = true;
                }
            }
        } catch (Throwable e) {
            Debug.out(e);
        }
        if (done) {
            if (bDeleteFileOnCancel) {
                torrentFile.delete();
            }
            return false;
        }
    }
    // Do a quick check to see if it's a torrent
    if (!TorrentUtil.isFileTorrent(sOriginatingLocation, torrentFile, torrentFile.getName(), !torrentOptions.getHideErrors())) {
        if (bDeleteFileOnCancel) {
            torrentFile.delete();
        }
        return false;
    }
    // Load up the torrent, see it it's real
    try {
        torrent = TorrentUtils.readFromFile(torrentFile, false);
    } catch (final TOTorrentException e) {
        UIFunctionsManager.getUIFunctions().showErrorMessage("OpenTorrentWindow.mb.openError", Debug.getStackTrace(e), new String[] { sOriginatingLocation, e.getMessage() });
        if (bDeleteFileOnCancel)
            torrentFile.delete();
        return false;
    }
    if (bDeleteFileOnCancel) {
        torrentOptions.setDeleteFileOnCancel(bDeleteFileOnCancel);
    }
    torrentOptions.sFileName = torrentFile.getAbsolutePath();
    torrentOptions.setTorrent(torrent);
    torrentOptions.sOriginatingLocation = sOriginatingLocation;
    return torrentOptions.getTorrent() != null;
}
Also used : UIManager(com.biglybt.pif.ui.UIManager) IOException(java.io.IOException) SubscriptionManager(com.biglybt.pif.utils.subscriptions.SubscriptionManager) URL(java.net.URL) TOTorrentException(com.biglybt.core.torrent.TOTorrentException) TOTorrent(com.biglybt.core.torrent.TOTorrent) VuzeFile(com.biglybt.core.vuzefile.VuzeFile) VuzeFile(com.biglybt.core.vuzefile.VuzeFile) File(java.io.File) VuzeFileHandler(com.biglybt.core.vuzefile.VuzeFileHandler)

Aggregations

SubscriptionManager (com.biglybt.pif.utils.subscriptions.SubscriptionManager)5 URL (java.net.URL)5 UIManager (com.biglybt.pif.ui.UIManager)2 Subscription (com.biglybt.pif.utils.subscriptions.Subscription)2 SubscriptionResult (com.biglybt.pif.utils.subscriptions.SubscriptionResult)2 File (java.io.File)2 IOException (java.io.IOException)2 TOTorrent (com.biglybt.core.torrent.TOTorrent)1 TOTorrentException (com.biglybt.core.torrent.TOTorrentException)1 TorrentOpenOptions (com.biglybt.core.torrent.impl.TorrentOpenOptions)1 AERunnable (com.biglybt.core.util.AERunnable)1 VuzeFile (com.biglybt.core.vuzefile.VuzeFile)1 VuzeFileHandler (com.biglybt.core.vuzefile.VuzeFileHandler)1 SearchProvider (com.biglybt.pif.utils.search.SearchProvider)1 SubscriptionException (com.biglybt.pif.utils.subscriptions.SubscriptionException)1 UIFunctions (com.biglybt.ui.UIFunctions)1 BufferedLabel (com.biglybt.ui.swt.components.BufferedLabel)1 LinkLabel (com.biglybt.ui.swt.components.LinkLabel)1 OutputStream (java.io.OutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1