Example #1
0
        private void OnDownloadTaskCompletedHandler(object sender, TaskCompletedEventArgs args)
        {
            HttpFileDownloadTask task = sender as HttpFileDownloadTask;
            FeedEnclosure        enc  = task.UserState as FeedEnclosure;

            if (enc != null)
            {
                if (args.Error != null || task.Status == TaskStatus.Failed)
                {
                    enc.DownloadStatus = FeedDownloadStatus.DownloadFailed;
                }
                else if (!args.Cancelled)
                {
                    if (task.Status == TaskStatus.Succeeded)
                    {
                        try {
                            enc.SetFileImpl(
                                task.RemoteUri.ToString(),
                                Path.GetDirectoryName(task.LocalPath),
                                task.MimeType,
                                Path.GetFileName(task.LocalPath)
                                );
                        } catch (Exception e) {
                            Log.Error(e);
                            enc.LastDownloadError = FeedDownloadError.DownloadFailed;
                            enc.DownloadStatus    = FeedDownloadStatus.DownloadFailed;
                            enc.Save();
                        }
                    }
                }
            }
        }
Example #2
0
        /*private void OnFeedItemRemoved (Feed feed, FeedItem item)
         * {
         *  if (feed == null) {
         *      throw new ArgumentNullException ("feed");
         *  } else if (item == null) {
         *      throw new ArgumentNullException ("item");
         *  }
         *
         *  EventHandler<FeedItemEventArgs> handler = FeedItemRemoved;
         *
         *  if (item.Enclosure != null) {
         *      lock (sync) {
         *          HttpFileDownloadTask task;
         *
         *          if (queued_downloads.TryGetValue ((FeedEnclosure)item.Enclosure, out task)) {
         *              task.CancelAsync ();
         *          }
         *      }
         *  }
         *
         *  if (handler != null) {
         *      OnFeedItemEvent (handler, new FeedItemEventArgs (feed, item));
         *  }
         * }
         *
         * private void OnFeedItemsRemoved (Feed feed, IEnumerable<FeedItem> items)
         * {
         *  if (feed == null) {
         *      throw new ArgumentNullException ("feed");
         *  } else if (items == null) {
         *      throw new ArgumentNullException ("items");
         *  }
         *
         *  EventHandler<FeedItemEventArgs> handler = FeedItemRemoved;
         *
         *  lock (sync) {
         *      HttpFileDownloadTask task;
         *
         *      foreach (FeedItem item in items) {
         *          if (item.Enclosure != null) {
         *              if (queued_downloads.TryGetValue ((FeedEnclosure)item.Enclosure, out task)) {
         *                  task.CancelAsync ();
         *              }
         *          }
         *      }
         *  }
         *
         *  if (handler != null) {
         *      OnFeedItemEvent (handler, new FeedItemEventArgs (feed, items));
         *  }
         * } */

        private HttpFileDownloadTask FindDownloadTask(FeedEnclosure enc)
        {
            if (enc == null)
            {
                throw new ArgumentNullException("enc");
            }

            return(FindDownloadTaskImpl((FeedEnclosure)enc));
        }
Example #3
0
        public void StopDownload(FeedEnclosure enc)
        {
            lock (sync) {
                HttpFileDownloadTask task = FindDownloadTask(enc);

                if (task != null)
                {
                    task.Stop();
                }
            }
        }
Example #4
0
        private HttpFileDownloadTask FindDownloadTaskImpl(FeedEnclosure enc)
        {
            HttpFileDownloadTask task = null;
            Feed parentFeed           = enc.Item.Feed as Feed;

            if (parentFeed != null && queued_downloads.ContainsKey(enc))
            {
                task = queued_downloads[enc];
            }

            return(task);
        }
Example #5
0
        public void CancelDownload(FeedEnclosure enc)
        {
            lock (sync) {
                HttpFileDownloadTask task = FindDownloadTask(enc);

                if (task != null)
                {
                    // Look into multi-cancel later
                    task.CancelAsync();
                }
            }
        }
Example #6
0
        private void TaskStatusChanged(TaskStatusChangedInfo statusInfo)
        {
            HttpFileDownloadTask task = statusInfo.Task as HttpFileDownloadTask;

            if (task == null)
            {
                return;
            }

            FeedEnclosure enc = task.UserState as FeedEnclosure;

            if (enc == null)
            {
                return;
            }

            switch (statusInfo.NewStatus)
            {
            case TaskStatus.Stopped:
            case TaskStatus.Cancelled:
                enc.DownloadStatus = FeedDownloadStatus.None;
                break;

            case TaskStatus.Failed:
                enc.DownloadStatus = FeedDownloadStatus.DownloadFailed;
                break;

            case TaskStatus.Paused:
                enc.DownloadStatus = FeedDownloadStatus.Paused;
                break;

            case TaskStatus.Ready:
                enc.DownloadStatus = FeedDownloadStatus.Pending;
                break;

            case TaskStatus.Running:
                enc.DownloadStatus = FeedDownloadStatus.Downloading;
                //enc.Item.Feed.IncrementActiveDownloadCount ();
                break;

            case TaskStatus.Succeeded:
                break;
            }

            FeedsManager.Instance.FeedManager.OnItemChanged(enc.Item);

            if (statusInfo.OldStatus == TaskStatus.Running)
            {
                //enc.Item.Feed.DecrementActiveDownloadCount ();
            }
        }
Example #7
0
        public HttpFileDownloadTask QueueDownload(FeedEnclosure enclosure, bool queue)
        {
            if (enclosure == null)
            {
                throw new ArgumentNullException("enc");
            }

            HttpFileDownloadTask task = null;

            lock (sync) {
                if (disposed)
                {
                    return(null);
                }

                if (!queued_downloads.ContainsKey(enclosure))
                {
                    Feed parentFeed = enclosure.Item.Feed;

                    if (parentFeed != null)
                    {
                        try {
                            task = download_manager.CreateDownloadTask(enclosure.Url, enclosure);
                            //Console.WriteLine ("Task DL path:  {0}", task.LocalPath);
                            task.Name = String.Format("{0} - {1}", parentFeed.Title, enclosure.Item.Title);

                            //task.StatusChanged
                            task.Completed += OnDownloadTaskCompletedHandler;

                            // Race condition...
                            // Should only be added when the task is associated or
                            // it can be canceled before it is added to the progress manager.

                            // Add a pre-association dict and move tasks to the
                            // queued dict once they've been offically added.

                            queued_downloads.Add(enclosure, task);
                        } catch (Exception e) {
                            Log.Error("Could not download podcast enclosure", e);
                        }
                    }
                }

                if (task != null && queue)
                {
                    download_manager.QueueDownload(task);
                }
            }

            return(task);
        }
Example #8
0
        // Parse one Media RSS media:content node
        // http://search.yahoo.com/mrss/
        private FeedEnclosure ParseMediaContent(XmlNode item_node)
        {
            try {
                XmlNode node = null;

                // Get the highest bitrate "full" content item
                // TODO allow a user-preference for a feed to decide what quality to get, if there
                // are options?
                int max_bitrate = 0;
                foreach (XmlNode test_node in item_node.SelectNodes("media:content", mgr))
                {
                    string expr = GetXmlNodeText(test_node, "@expression");
                    if (!(String.IsNullOrEmpty(expr) || expr == "full"))
                    {
                        continue;
                    }

                    int bitrate = GetInt32(test_node, "@bitrate");
                    if (node == null || bitrate > max_bitrate)
                    {
                        node        = test_node;
                        max_bitrate = bitrate;
                    }
                }

                if (node == null)
                {
                    return(null);
                }

                FeedEnclosure enclosure = new FeedEnclosure();
                enclosure.Url = GetXmlNodeText(node, "@url");
                if (enclosure.Url == null)
                {
                    return(null);
                }

                enclosure.FileSize = Math.Max(0, GetInt64(node, "@fileSize"));
                enclosure.MimeType = GetXmlNodeText(node, "@type");
                enclosure.Duration = TimeSpan.FromSeconds(GetInt64(node, "@duration"));
                enclosure.Keywords = GetXmlNodeText(item_node, "itunes:keywords");

                // TODO get the thumbnail URL

                return(enclosure);
            } catch (Exception e) {
                Hyena.Log.Exception("Caught error parsing RSS media:content", e);
            }

            return(null);
        }
Example #9
0
        private void OnDownloadTaskRemoved(object sender, TaskRemovedEventArgs <HttpFileDownloadTask> e)
        {
            if (e.Task != null)
            {
                FeedEnclosure enc = e.Task.UserState as FeedEnclosure;

                if (enc != null)
                {
                    lock (sync) {
                        DownloadTaskRemoved(enc, e.Task, true);
                    }
                }
            }
            else if (e.Tasks != null)
            {
                Feed                 tmpParent    = null;
                FeedEnclosure        tmpEnclosure = null;
                List <FeedEnclosure> tmpList      = null;

                Dictionary <Feed, List <FeedEnclosure> > feedDict =
                    new Dictionary <Feed, List <FeedEnclosure> > ();

                lock (sync) {
                    foreach (HttpFileDownloadTask t in e.Tasks)
                    {
                        tmpEnclosure = t.UserState as FeedEnclosure;

                        if (tmpEnclosure != null)
                        {
                            tmpParent = tmpEnclosure.Item.Feed;

                            if (!feedDict.TryGetValue(tmpParent, out tmpList))
                            {
                                tmpList = new List <FeedEnclosure> ();
                                feedDict.Add(tmpParent, tmpList);
                            }

                            tmpList.Add(tmpEnclosure);
                            DownloadTaskRemoved(tmpEnclosure, t, false);
                        }
                    }

                    //foreach (KeyValuePair<Feed,List<FeedEnclosure>> kvp in feedDict) {
                    //kvp.Key.DecrementQueuedDownloadCount (kvp.Value.Count);
                    //}
                }
            }
        }
Example #10
0
        private void LoadEnclosure()
        {
            if (!enclosure_loaded && DbId > 0)
            {
                FeedEnclosure enclosure = FeedEnclosure.Provider.FetchFirstMatching(String.Format(
                                                                                        "{0}.ItemID = {1}", FeedEnclosure.Provider.TableName, DbId
                                                                                        ));

                if (enclosure != null)
                {
                    enclosure.Item = this;
                    this.enclosure = enclosure;
                }
                enclosure_loaded = true;
            }
        }
Example #11
0
        public FeedsManager(HyenaSqliteConnection connection, DownloadManager downloadManager, string podcast_base_dir)
        {
            // Hack to work around Feeds being needy and having to call all our internal methods, instead
            // of us just listening for their events.
            Instance              = this;
            this.connection       = connection;
            this.podcast_base_dir = podcast_base_dir;

            feed_manager      = new FeedManager();
            enclosure_manager = new EnclosureManager(downloadManager);

            Feed.Init();
            FeedItem.Init();
            FeedEnclosure.Init();

            command_queue = new AsyncCommandQueue();
        }
Example #12
0
        private void DownloadTaskRemoved(FeedEnclosure enc, HttpFileDownloadTask task, bool decQueuedCount)
        {
            if (queued_downloads.ContainsKey(enc))
            {
                queued_downloads.Remove(enc);
                task.Completed -= OnDownloadTaskCompletedHandler;

                if (decQueuedCount)
                {
                    //enc.Item.Feed.DecrementQueuedDownloadCount ();
                }

                if (queued_downloads.Count == 0)
                {
                    if (download_handle != null)
                    {
                        download_handle.Set();
                    }
                }
            }
        }
Example #13
0
        private FeedEnclosure ParseEnclosure(XmlNode node)
        {
            try {
                FeedEnclosure enclosure = new FeedEnclosure();

                enclosure.Url = GetXmlNodeText(node, "enclosure/@url");
                if (enclosure.Url == null)
                {
                    return(null);
                }

                enclosure.FileSize = Math.Max(0, GetInt64(node, "enclosure/@length"));
                enclosure.MimeType = GetXmlNodeText(node, "enclosure/@type");
                enclosure.Duration = GetITunesDuration(node);
                enclosure.Keywords = GetXmlNodeText(node, "itunes:keywords");
                return(enclosure);
            } catch (Exception e) {
                Hyena.Log.Exception("Caught error parsing RSS enclosure", e);
            }

            return(null);
        }
Example #14
0
        private FeedEnclosure ParseEnclosure(XmlNode node)
        {
            try {
                FeedEnclosure enclosure = new FeedEnclosure();

                enclosure.Url = GetXmlNodeText(node, "enclosure/@url");
                if (String.IsNullOrEmpty(enclosure.Url))
                {
                    return(null);
                }

                enclosure.FileSize = Math.Max(0, GetInt64(node, "enclosure/@length"));
                enclosure.MimeType = GetXmlNodeText(node, "enclosure/@type");
                enclosure.Duration = GetITunesDuration(node);
                enclosure.Keywords = GetXmlNodeText(node, "itunes:keywords");
                return(enclosure);
            } catch (Exception e) {
                Log.Error(String.Format("Caught error parsing RSS enclosure in {0}", url), e);
            }

            return(null);
        }
Example #15
0
        private void TaskAddedAction(HttpFileDownloadTask task)
        {
            Feed          parentFeed = null;
            FeedEnclosure enc        = task.UserState as FeedEnclosure;

            if (enc != null)
            {
                lock (sync) {
                    parentFeed = enc.Item.Feed;

                    if (parentFeed != null && queued_downloads.ContainsKey(enc))
                    {
                        if (queued_downloads.Count == 0)
                        {
                            download_handle.Reset();
                        }

                        enc.DownloadStatus = FeedDownloadStatus.Pending;
                        //parentFeed.IncrementQueuedDownloadCount ();
                    }
                }
            }
        }
Example #16
0
 public HttpFileDownloadTask QueueDownload(FeedEnclosure enclosure)
 {
     return(QueueDownload(enclosure, true));
 }