Esempio n. 1
0
 public void ImportEmx(string[] uris)
 {
     foreach (string uri in uris)
     {
         using (var xml_reader = new XmlTextReader(uri))
         {
             while (xml_reader.Read())
             {
                 if (xml_reader.NodeType == XmlNodeType.Element &&
                     xml_reader.Name == "TRACKURL")
                 {
                     xml_reader.Read();
                     Hyena.Log.DebugFormat("Downloading: {0}", xml_reader.Value);
                     HttpFileDownloadTask task = download_manager.CreateDownloadTask(xml_reader.Value);
                     if (File.Exists(task.LocalPath))
                     {
                         File.Delete(task.LocalPath);  // FIXME: We go into a download loop if we don't.
                     }
                     task.Completed += OnDownloadCompleted;
                     download_manager.QueueDownload(task);
                     tasks.Add(task.LocalPath, task);
                 }
             }
         }
     }
 }
Esempio n. 2
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();
                        }
                    }
                }
            }
        }
Esempio n. 3
0
        private void OnDownloadCompleted(object sender, TaskCompletedEventArgs args)
        {
            HttpFileDownloadTask task = sender as HttpFileDownloadTask;

            if (task.Status != TaskStatus.Succeeded)
            {
                task.Completed -= OnDownloadCompleted;

                if (File.Exists(task.LocalPath))
                {
                    File.Delete(task.LocalPath);
                }

                if (Directory.Exists(Path.GetDirectoryName(task.LocalPath)))
                {
                    Directory.Delete(Path.GetDirectoryName(task.LocalPath));
                }

                tasks.Remove(task.LocalPath);

                Hyena.Log.ErrorFormat("Could not download eMusic track: {0}",
                                      task.Error.ToString());
            }
            else
            {
                import_manager.Enqueue(task.LocalPath);
            }
        }
Esempio n. 4
0
        public void StopDownload(FeedEnclosure enc)
        {
            lock (sync) {
                HttpFileDownloadTask task = FindDownloadTask(enc);

                if (task != null)
                {
                    task.Stop();
                }
            }
        }
Esempio n. 5
0
 void HandleImportResult(object o, DatabaseImportResultArgs args)
 {
     if (tasks.ContainsKey(args.Path))
     {
         HttpFileDownloadTask task = tasks[args.Path];
         task.Completed -= OnDownloadCompleted;
         File.Delete(args.Path);
         Directory.Delete(Path.GetDirectoryName(args.Path));
         tasks.Remove(args.Path);
     }
 }
Esempio n. 6
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);
        }
Esempio n. 7
0
        public void CancelDownload(FeedEnclosure enc)
        {
            lock (sync) {
                HttpFileDownloadTask task = FindDownloadTask(enc);

                if (task != null)
                {
                    // Look into multi-cancel later
                    task.CancelAsync();
                }
            }
        }
Esempio n. 8
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 ();
            }
        }
Esempio n. 9
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);
        }
Esempio n. 10
0
 private void OnEnclosureDownloadCompleted(HttpFileDownloadTask task)
 {
     /*EventHandler<TaskEventArgs<HttpFileDownloadTask>> handler = EnclosureDownloadCompleted;
      *
      * if (handler != null) {
      *  AsyncCommandQueue<ICommand> cmdQCpy = command_queue;
      *
      *  if (cmdQCpy != null) {
      *      cmdQCpy.Register (new EventWrapper<TaskEventArgs<HttpFileDownloadTask>> (
      *              handler, this, new TaskEventArgs<HttpFileDownloadTask> (task))
      *          );
      *  }
      * }   */
 }
Esempio n. 11
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();
                    }
                }
            }
        }
Esempio n. 12
0
        public IEnumerable <HttpFileDownloadTask> QueueDownloads(IEnumerable <FeedEnclosure> encs)
        {
            if (encs == null)
            {
                throw new ArgumentNullException("encs");
            }

            ICollection <HttpFileDownloadTask> encsCol = encs as ICollection <HttpFileDownloadTask>;

            List <HttpFileDownloadTask> tasks = (encsCol == null) ?
                                                new List <HttpFileDownloadTask> () :
                                                new List <HttpFileDownloadTask> (encsCol.Count);

            HttpFileDownloadTask tmpTask = null;

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

                foreach (FeedEnclosure enc in encs)
                {
                    tmpTask = QueueDownload(enc, false);
                    if (tmpTask != null)
                    {
                        tasks.Add(tmpTask);
                    }
                }

                if (tasks.Count > 0)
                {
                    download_manager.QueueDownload(tasks);
                }
            }

            return(tasks);
        }
Esempio n. 13
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 ();
                    }
                }
            }
        }