Example #1
0
        private void RemoveTorrent(TorrentInfo t, string action)
        {
            t.Invisible = true;
            t.UpdateList("Invisible", "ShowOnList");
            string hash = t.InfoHash;

            t.Torrent.AutoManaged = false;
            t.Torrent.Pause();

            this.state.LibtorrentSession.RemoveTorrent(t.Torrent);
            this.state.DeleteTorrentStateData(hash);

            uiContext.Send(x =>
            {
                state._torrents.Remove(hash);
                state.Torrents.Remove(t);
                state.LabelManager.ClearLabels(t);
            }, null);

            switch (action)
            {
            case "remove_torrent_torrentonly":
                DeleteTorrent(t);
                break;

            case "remove_torrent_dataonly":
                DeleteData(t);
                break;

            case "remove_torrent_both":
                DeleteData(t);
                DeleteTorrent(t);
                break;

            default:
                break;
            }


            t.OffMyself();
        }
Example #2
0
        public void QueueTorrent(TorrentInfo ti)
        {
            if (!App.Settings.SeedingTorrentsAreActive && ti.IsComplete)
            {
                //simply start it
                ti.QueueState = QueueState.Queued;
                ti.Torrent.Start();
                return;
            }

            if (App.Settings.EnableQueue)
            {
                if (!queue_info_store.ContainsKey(ti.InfoHash))
                {
                    QueueCake cake = new QueueCake();
                    cake.Torrent  = ti;
                    ti.QueueState = QueueState.Queued;
                    ti.Torrent.Stop();

                    cake.WorkerThread = new Amib.Threading.Action(() =>
                    {
                        cake.ThreadBGRunning = true;

                        ti.Torrent.Start();
                        ti.is_going_to_start = false;
                        ti.UpdateList("Status");
                        while (ti.QueueState == QueueState.Queued)
                        {
                            if (is_inactive(ti))
                            {
                                // The torrent is either:
                                // stopped or has an error.
                                // or is complete
                                break;
                            }
                            else
                            {
                                //wait until something happen, while keeping the queue busy
                                Thread.Sleep(1000);
                            }
                        }

                        cake.ThreadBGRunning = false;
                        //if we get here, then either the torrent was dequeued, or it is inactive
                        //if (ti.IsComplete) { return; }
                    });

                    ti.is_going_to_start = true;
                    cake.ThreadBG        = this.slots.QueueWorkItem(cake.WorkerThread);

                    this.queue_info_store.Add(ti.InfoHash, cake);
                    ti.UpdateList("Status");
                }
                else
                {
                    //torrent is inside the queue store
                    var cake = queue_info_store[ti.InfoHash];

                    if (cake.ThreadBGRunning)
                    {
                        //this happen if the torrent was {downloading} and it was {paused}.
                        //in this case, we start it.
                        //in other words, the torrent still have a slot inside the queue, so we can start it
                        ti.Torrent.Start();
                    }
                    else
                    {
                        //let's seen how can this happen:
                        //- if the user has stopped the torrent, the TorrentInfo class will dequeue the torrent, so the queue_info_store doesn't
                        //  contain this torrent, so this should never happen.
                        //- if the user hasn't stopped the torrent but an error has been occured, the torrent queue thread will be stopped, but
                        //  the torrent cake will remain inside the queue_info_store.
                        //- if the torrent entered seeding state, and seeding is configured as inactive.

                        //tl;dr torrent either has an error or it is seeding
                        if (ti.Torrent.State == MonoTorrent.Common.TorrentState.Error)
                        {
                            //Attempt to start it again
                            this.queue_info_store.Remove(ti.InfoHash);
                            this.QueueTorrent(ti);
                        }

                        //if (ti.Torrent.State == MonoTorrent.Common.TorrentState.Seeding)
                        //{
                        //    //the torrent is seeding and the user clicked start, so do nothing
                        //    //except maybe remove the torrent cake, but I am not sure yet.
                        //}
                    }
                }
            }
            else
            {
                ti.Torrent.Start();
            }
        }