Пример #1
0
 public PeriodicTorrent AddTorrent(MagnetLink link, string path, bool suppressMessages = false)
 {
     if (!Directory.Exists(path))
         Directory.CreateDirectory(path);
     var name = HttpUtility.HtmlDecode(HttpUtility.UrlDecode(link.Name));
     var cache = Path.Combine(
             SettingsManager.TorrentCachePath,
             ClientManager.CleanFileName(name) + ".torrent");
     for (int i = 0; i < link.AnnounceUrls.Count; i++)
         link.AnnounceUrls[i] = HttpUtility.UrlDecode(HttpUtility.UrlDecode(link.AnnounceUrls[i]));
     var wrapper = new TorrentWrapper(link, path, new TorrentSettings(), cache);
     if (Client.Torrents.Any(t => t.Torrent.InfoHash == wrapper.InfoHash))
     {
         if (!suppressMessages)
             MessageBox.Show(name + " has already been added.", "Error");
         return null;
     }
     var periodic = Client.AddTorrent(wrapper);
     periodic.CacheFilePath = cache;
     periodic.UpdateInfo();
     var serializer = new JsonSerializer();
     using (var writer = new StreamWriter(Path.Combine(SettingsManager.TorrentCachePath,
         Path.GetFileNameWithoutExtension(periodic.CacheFilePath) + ".info")))
         serializer.Serialize(new JsonTextWriter(writer), periodic.TorrentInfo);
     return periodic;
 }
Пример #2
0
        private void ExecuteNew(object sender, ExecutedRoutedEventArgs e)
        {
            var window = new CreateTorrentWindow();

            if (window.ShowDialog().Value)
            {
                var            torrent = window.Torrent;
                TorrentWrapper wrapper;
                if (window.singleFileRadioButton.IsChecked.Value)
                {
                    wrapper = new TorrentWrapper(torrent, Path.GetDirectoryName(window.FilePath), new TorrentSettings());
                }
                else
                {
                    wrapper = new TorrentWrapper(torrent, window.FilePath, new TorrentSettings());
                }
                var periodic = Client.AddTorrent(wrapper);
                // Save torrent to cache
                var cache = Path.Combine(SettingsManager.TorrentCachePath, Path.GetFileName(torrent.TorrentPath));
                if (File.Exists(cache))
                {
                    File.Delete(cache);
                }
                File.Copy(torrent.TorrentPath, cache);
                periodic.CacheFilePath = cache;
                periodic.UpdateInfo();
                var serializer = new JsonSerializer();
                using (var writer = new StreamWriter(Path.Combine(SettingsManager.TorrentCachePath,
                                                                  Path.GetFileNameWithoutExtension(periodic.CacheFilePath) + ".info")))
                    serializer.Serialize(new JsonTextWriter(writer), periodic.TorrentInfo);
            }
        }
Пример #3
0
 public PeriodicTorrent AddTorrent(TorrentWrapper torrent)
 {
     var periodicTorrent = new PeriodicTorrent(torrent);
     Torrents.Add(periodicTorrent);
     torrent.Index = Torrents.Count;
     Task.Factory.StartNew(() =>
         {
             Client.Register(torrent);
             torrent.Start();
         });
     return periodicTorrent;
 }
Пример #4
0
 public PeriodicTorrent AddTorrent(TorrentWrapper torrent)
 {
     var periodicTorrent = new PeriodicTorrent(torrent);
     Task.Factory.StartNew(() =>
         {
             Client.Register(torrent);
             if (SettingsManager.StartTorrentsImmediately)
                 torrent.Start();
         });
     Application.Current.Dispatcher.BeginInvoke(new Action(() => Torrents.Add(periodicTorrent)));
     return periodicTorrent;
 }
Пример #5
0
 public PeriodicTorrent LoadFastResume(FastResume resume, TorrentWrapper torrent)
 {
     var periodicTorrent = new PeriodicTorrent(torrent);
     Torrents.Add(periodicTorrent);
     torrent.Index = Torrents.Count;
     Task.Factory.StartNew(() =>
         {
             torrent.LoadFastResume(resume);
             Client.Register(torrent);
             torrent.Start();
         });
     return periodicTorrent;
 }
Пример #6
0
 public PeriodicTorrent(TorrentWrapper wrapper)
 {
     Torrent = wrapper;
     PeerList = new ObservableCollection<PeerId>();
     Update();
     Name = Torrent.Name;
     Size = Torrent.Size;
     CompletedOnAdd = Torrent.Complete;
     NotifiedComplete = false;
     PiecePicker = new RandomisedPicker(new StandardPicker());
     wrapper.PieceManager.BlockReceived += PieceManager_BlockReceived;
     wrapper.PieceHashed += wrapper_PieceHashed;
 }
Пример #7
0
 public PeriodicTorrent(TorrentWrapper wrapper)
 {
     TorrentInfo = new TorrentInfo();
     Torrent = wrapper;
     PeerList = new ObservableCollection<PeerId>();
     Update();
     Name = Torrent.Name;
     Size = Torrent.Size;
     CompletedOnAdd = Torrent.Complete;
     CompletionTime = DateTime.MinValue;
     NotifiedComplete = false;
     PiecePicker = new RandomisedPicker(new StandardPicker());
     wrapper.PieceManager.BlockReceived += PieceManager_BlockReceived;
     wrapper.PieceHashed += wrapper_PieceHashed;
     TorrentInfo.Path = Torrent.Path;
     PausedFromSeeding = false;
 }
Пример #8
0
 public PeriodicTorrent AddTorrent(TorrentWrapper torrent, bool startImmediately)
 {
     // Apply settings
     torrent.Settings.UseDht = SettingsManager.EnableDHT;
     torrent.Settings.MaxConnections = SettingsManager.MaxConnectionsPerTorrent;
     torrent.Settings.MaxDownloadSpeed = SettingsManager.MaxDownloadSpeed;
     torrent.Settings.MaxUploadSpeed = SettingsManager.MaxUploadSpeed;
     torrent.Settings.UploadSlots = SettingsManager.UploadSlotsPerTorrent;
     var periodicTorrent = new PeriodicTorrent(torrent);
     Task.Factory.StartNew(() =>
         {
             Client.Register(torrent);
             if (startImmediately)
                 torrent.Start();
         });
     Application.Current.Dispatcher.BeginInvoke(new Action(() => Torrents.Add(periodicTorrent)));
     return periodicTorrent;
 }
Пример #9
0
        public PeriodicTorrent AddTorrent(Torrent torrent, string path, bool suppressMessages = false)
        {
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            var wrapper = new TorrentWrapper(torrent, path, new TorrentSettings());

            if (torrent.IsPrivate)
            {
                wrapper.Settings.UseDht             = false;
                wrapper.Settings.EnablePeerExchange = false;
            }
            if (Client.Torrents.Any(t => t.Torrent.InfoHash == wrapper.InfoHash))
            {
                if (!suppressMessages)
                {
                    MessageBox.Show(torrent.Name + " has already been added.", "Error");
                }
                return(null);
            }
            var periodic = Client.AddTorrent(wrapper);
            // Save torrent to cache
            var cache = Path.Combine(SettingsManager.TorrentCachePath, Path.GetFileName(torrent.TorrentPath));

            if (File.Exists(cache))
            {
                File.Delete(cache);
            }
            File.Copy(torrent.TorrentPath, cache);
            periodic.CacheFilePath = cache;
            periodic.UpdateInfo();
            var serializer = new JsonSerializer();

            using (var writer = new StreamWriter(Path.Combine(SettingsManager.TorrentCachePath,
                                                              Path.GetFileNameWithoutExtension(periodic.CacheFilePath) + ".info")))
                serializer.Serialize(new JsonTextWriter(writer), periodic.TorrentInfo);

            if (SettingsManager.DeleteTorrentsAfterAdd)
            {
                File.Delete(torrent.TorrentPath);
            }
            return(periodic);
        }
Пример #10
0
        public PeriodicTorrent(TorrentWrapper wrapper)
        {
            TorrentInfo = new TorrentInfo();
            Torrent     = wrapper;
            PeerList    = new ObservableCollection <PeerId>();

            Name             = Torrent.Name;
            Size             = Torrent.Size;
            CompletedOnAdd   = Torrent.Complete;
            CompletionTime   = DateTime.MinValue;
            NotifiedComplete = false;
            PiecePicker      = new RandomisedPicker(new StandardPicker());
            wrapper.PieceManager.BlockReceived += PieceManager_BlockReceived;
            wrapper.PieceHashed += wrapper_PieceHashed;
            TorrentInfo.Path     = Torrent.Path;
            PausedFromSeeding    = false;
            StoppedByUser        = false;
            ExcludeFromQueue     = false;
            Update(false);
        }
Пример #11
0
 public void AddTorrent(Torrent torrent, string path, bool suppressMessages = false)
 {
     if (!Directory.Exists(path))
         Directory.CreateDirectory(path);
     var wrapper = new TorrentWrapper(torrent, path, new TorrentSettings());
     if (Client.Torrents.Any(t => t.Torrent.InfoHash == wrapper.InfoHash))
     {
         if (!suppressMessages)
             MessageBox.Show(torrent.Name + " has already been added.", "Error");
         return;
     }
     var periodic = Client.AddTorrent(wrapper);
     // Save torrent to cache
     var cache = Path.Combine(SettingsManager.TorrentCachePath, Path.GetFileName(torrent.TorrentPath));
     if (File.Exists(cache))
         File.Delete(cache);
     File.Copy(torrent.TorrentPath, cache);
     File.WriteAllText(Path.Combine(Path.GetDirectoryName(cache),
         Path.GetFileNameWithoutExtension(cache)) + ".info", path);
     periodic.CacheFilePath = cache;
 }
Пример #12
0
        public PeriodicTorrent AddTorrent(TorrentWrapper torrent, bool startImmediately)
        {
            // Apply settings
            torrent.Settings.UseDht           = SettingsManager.EnableDHT;
            torrent.Settings.MaxConnections   = SettingsManager.MaxConnectionsPerTorrent;
            torrent.Settings.MaxDownloadSpeed = SettingsManager.MaxDownloadSpeed;
            torrent.Settings.MaxUploadSpeed   = SettingsManager.MaxUploadSpeed;
            torrent.Settings.UploadSlots      = SettingsManager.UploadSlotsPerTorrent;
            var periodicTorrent = new PeriodicTorrent(torrent);

            Task.Factory.StartNew(() =>
            {
                Client.Register(torrent);
                if (startImmediately)
                {
                    torrent.Start();
                }
            });
            Application.Current.Dispatcher.BeginInvoke(new Action(() => Torrents.Add(periodicTorrent)));
            return(periodicTorrent);
        }
Пример #13
0
 public void AddTorrent(MagnetLink link, string path, bool suppressMessages = false)
 {
     if (!Directory.Exists(path))
         Directory.CreateDirectory(path);
     var name = HttpUtility.HtmlDecode(HttpUtility.UrlDecode(link.Name));
     var cache = Path.Combine(
             SettingsManager.TorrentCachePath,
             ClientManager.CleanFileName(name) + ".torrent");
     var wrapper = new TorrentWrapper(link, path, new TorrentSettings(), cache);
     if (Client.Torrents.Any(t => t.Torrent.InfoHash == wrapper.InfoHash))
     {
         if (!suppressMessages)
             MessageBox.Show(name + " has already been added.", "Error");
         return;
     }
     var periodic = Client.AddTorrent(wrapper);
     File.WriteAllText(Path.Combine(
             SettingsManager.TorrentCachePath,
             ClientManager.CleanFileName(name) + ".info"),
             path);
     periodic.CacheFilePath = cache;
 }
Пример #14
0
        public PeriodicTorrent AddTorrent(MagnetLink link, string path, bool suppressMessages = false)
        {
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            var name  = HttpUtility.HtmlDecode(HttpUtility.UrlDecode(link.Name));
            var cache = Path.Combine(
                SettingsManager.TorrentCachePath,
                ClientManager.CleanFileName(name) + ".torrent");

            for (int i = 0; i < link.AnnounceUrls.Count; i++)
            {
                link.AnnounceUrls[i] = HttpUtility.UrlDecode(HttpUtility.UrlDecode(link.AnnounceUrls[i]));
            }
            var wrapper = new TorrentWrapper(link, path, new TorrentSettings(), cache);

            if (Client.Torrents.Any(t => t.Torrent.InfoHash == wrapper.InfoHash))
            {
                if (!suppressMessages)
                {
                    MessageBox.Show(name + " has already been added.", "Error");
                }
                return(null);
            }
            var periodic = Client.AddTorrent(wrapper);

            periodic.CacheFilePath = cache;
            periodic.UpdateInfo();
            var serializer = new JsonSerializer();

            using (var writer = new StreamWriter(Path.Combine(SettingsManager.TorrentCachePath,
                                                              Path.GetFileNameWithoutExtension(periodic.CacheFilePath) + ".info")))
                serializer.Serialize(new JsonTextWriter(writer), periodic.TorrentInfo);
            return(periodic);
        }
Пример #15
0
 private void Initialize()
 {
     AutoWatchers = new List<FileSystemWatcher>();
     SettingsManager.Initialize();
     SettingsManager = new SettingsManager();
     LoadSettings();
     Client.Initialize(SettingsManager);
     foreach (var label in SettingsManager.Labels)
         AddLabel(label);
     // Load prior session on another thread because it takes some time
     Task.Factory.StartNew(() =>
         {
             BEncodedDictionary resume = null;
             if (File.Exists(SettingsManager.FastResumePath))
             {
                 resume = BEncodedValue.Decode<BEncodedDictionary>(
                     File.ReadAllBytes(SettingsManager.FastResumePath));
                 File.Delete(SettingsManager.FastResumePath);
             }
             var torrents = Directory.GetFiles(SettingsManager.TorrentCachePath, "*.torrent");
             var toRemove = new List<string>(Directory.GetFiles(SettingsManager.TorrentCachePath, "*.info"));
             var serializer = new JsonSerializer();
             foreach (var torrent in torrents)
             {
                 var path = Path.Combine(SettingsManager.TorrentCachePath, Path.GetFileNameWithoutExtension(torrent)) + ".info";
                 if (toRemove.Contains(path))
                     toRemove.Remove(path);
                 try
                 {
                     TorrentInfo info;
                     using (var reader = new StreamReader(path))
                         info = serializer.Deserialize<TorrentInfo>(new JsonTextReader(reader));
                     var wrapper = new TorrentWrapper(Torrent.Load(torrent), info.Path, new TorrentSettings());
                     PeriodicTorrent periodicTorrent;
                     if (resume != null && resume.ContainsKey(wrapper.Torrent.InfoHash.ToHex()))
                     {
                         periodicTorrent = Client.LoadFastResume(
                             new FastResume((BEncodedDictionary)resume[wrapper.Torrent.InfoHash.ToHex()]), wrapper);
                     }
                     else
                         periodicTorrent = Client.AddTorrent(wrapper);
                     periodicTorrent.LoadInfo(info);
                     periodicTorrent.CacheFilePath = torrent;
                 }
                 catch { }
             }
             // Clean up abandoned .info files
             foreach (var file in toRemove)
                 File.Delete(file);
         });
     Timer = new Timer(o => Dispatcher.Invoke(new Action(PeriodicUpdate)),
         null, 1000, 1000);
     IsIdle = false;
     if (SettingsManager.AutoUpdate)
         CheckForUpdates();
 }
Пример #16
0
 public void MoveTorrent(TorrentWrapper torrent, string path)
 {
     Task.Factory.StartNew(() =>
         {
             path = Path.Combine(path, Path.GetFileName(torrent.Path));
             if (!Directory.Exists(path))
                 Directory.CreateDirectory(path);
             var oldPath = torrent.Path;
             torrent.Stop();
             while (torrent.State != TorrentState.Stopped) ;
             torrent.MoveFiles(path, true);
             torrent.Start();
             Directory.Delete(oldPath, true);
             var cache = Path.Combine(SettingsManager.TorrentCachePath, Path.GetFileName(oldPath));
             File.WriteAllText(Path.Combine(Path.GetDirectoryName(cache),
                 Path.GetFileName(cache)) + ".info", path);
         });
 }
Пример #17
0
 private void Initialize()
 {
     Client.Initialize();
     SettingsManager = new SettingsManager();
     SettingsManager.Initialize();
     // Load prior session
     if (File.Exists(SettingsManager.FastResumePath))
     {
         // Load on another thread because it takes some time
         new Thread(() =>
             {
                 var resume = BEncodedValue.Decode<BEncodedDictionary>(
                     File.ReadAllBytes(SettingsManager.FastResumePath));
                 var torrents = Directory.GetFiles(SettingsManager.TorrentCachePath, "*.torrent");
                 foreach (var torrent in torrents)
                 {
                     var path = File.ReadAllText(Path.Combine(
                         SettingsManager.TorrentCachePath, Path.GetFileNameWithoutExtension(torrent))
                                                 + ".info");
                     var wrapper = new TorrentWrapper(Torrent.Load(torrent), path, new TorrentSettings());
                     PeriodicTorrent periodicTorrent;
                     Dispatcher.BeginInvoke(new Action(() =>
                         {
                             if (resume.ContainsKey(wrapper.Torrent.InfoHash.ToHex()))
                             {
                                 periodicTorrent = Client.LoadFastResume(
                                     new FastResume((BEncodedDictionary)resume[wrapper.Torrent.InfoHash.ToHex()]), wrapper);
                             }
                             else
                             {
                                 periodicTorrent = Client.AddTorrent(wrapper);
                             }
                             periodicTorrent.CompletedOnAdd = wrapper.Complete;
                             periodicTorrent.CacheFilePath = torrent;
                         }));
                 }
             }).Start();
     }
     Timer = new Timer(o => Dispatcher.Invoke(new Action(PeriodicUpdate)),
         null, 1000, 1000);
 }
Пример #18
0
 public PeriodicTorrent AddTorrent(TorrentWrapper torrent)
 {
     return(AddTorrent(torrent, SettingsManager.StartTorrentsImmediately));
 }
Пример #19
0
 private void Initialize()
 {
     AutoWatchers = new List <FileSystemWatcher>();
     SettingsManager.Initialize();
     SettingsManager = new SettingsManager();
     LoadSettings();
     Client.Initialize(SettingsManager);
     SettingsManager.ForcePropertyUpdate();
     foreach (var label in SettingsManager.Labels)
     {
         AddLabel(label);
     }
     // Load prior session on another thread because it takes some time
     Task.Factory.StartNew(() =>
     {
         BEncodedDictionary resume = null;
         if (File.Exists(SettingsManager.FastResumePath))
         {
             resume = BEncodedValue.Decode <BEncodedDictionary>(
                 File.ReadAllBytes(SettingsManager.FastResumePath));
             File.Delete(SettingsManager.FastResumePath);
         }
         var torrents   = Directory.GetFiles(SettingsManager.TorrentCachePath, "*.torrent");
         var toRemove   = new List <string>(Directory.GetFiles(SettingsManager.TorrentCachePath, "*.info"));
         var serializer = new JsonSerializer();
         foreach (var torrent in torrents)
         {
             var path = Path.Combine(SettingsManager.TorrentCachePath, Path.GetFileNameWithoutExtension(torrent)) + ".info";
             if (toRemove.Contains(path))
             {
                 toRemove.Remove(path);
             }
             try
             {
                 TorrentInfo info;
                 using (var reader = new StreamReader(path))
                     info = serializer.Deserialize <TorrentInfo>(new JsonTextReader(reader));
                 var wrapper = new TorrentWrapper(Torrent.Load(torrent), info.Path, new TorrentSettings());
                 PeriodicTorrent periodicTorrent;
                 bool start = info.IsRunning;
                 if (Client.Torrents.Count > SettingsManager.MaxActiveTorrents)
                 {
                     start = false;
                 }
                 if (resume != null && resume.ContainsKey(wrapper.Torrent.InfoHash.ToHex()))
                 {
                     periodicTorrent = Client.LoadFastResume(new FastResume((BEncodedDictionary)resume[wrapper.Torrent.InfoHash.ToHex()]),
                                                             wrapper, start);
                 }
                 else
                 {
                     periodicTorrent = Client.AddTorrent(wrapper, start);
                 }
                 periodicTorrent.LoadInfo(info);
                 periodicTorrent.CacheFilePath = torrent;
             }
             catch { }
         }
         // Clean up abandoned .info files
         foreach (var file in toRemove)
         {
             File.Delete(file);
         }
     });
     NextQueueCycle = DateTime.Now.AddSeconds(10);
     Timer          = new Timer(o => Dispatcher.Invoke(new Action(PeriodicUpdate)),
                                null, 1000, 1000);
     IsIdle = false;
     if (SettingsManager.AutoUpdate)
     {
         CheckForUpdates();
     }
 }
Пример #20
0
 private void ExecuteNew(object sender, ExecutedRoutedEventArgs e)
 {
     var window = new CreateTorrentWindow();
     if (window.ShowDialog().Value)
     {
         var torrent = window.Torrent;
         TorrentWrapper wrapper;
         if (window.singleFileRadioButton.IsChecked.Value)
             wrapper = new TorrentWrapper(torrent, Path.GetDirectoryName(window.FilePath), new TorrentSettings());
         else
             wrapper = new TorrentWrapper(torrent, window.FilePath, new TorrentSettings());
         var periodic = Client.AddTorrent(wrapper);
         // Save torrent to cache
         var cache = Path.Combine(SettingsManager.TorrentCachePath, Path.GetFileName(torrent.TorrentPath));
         if (File.Exists(cache))
             File.Delete(cache);
         File.Copy(torrent.TorrentPath, cache);
         periodic.CacheFilePath = cache;
         periodic.UpdateInfo();
         var serializer = new JsonSerializer();
         using (var writer = new StreamWriter(Path.Combine(SettingsManager.TorrentCachePath,
             Path.GetFileNameWithoutExtension(periodic.CacheFilePath) + ".info")))
             serializer.Serialize(new JsonTextWriter(writer), periodic.TorrentInfo);
     }
 }
Пример #21
0
 public PeriodicTorrent AddTorrent(TorrentWrapper torrent)
 {
     return AddTorrent(torrent, SettingsManager.StartTorrentsImmediately);
 }
Пример #22
0
        public PeriodicTorrent AddTorrent(Torrent torrent, string path, bool suppressMessages = false)
        {
            if (!Directory.Exists(path))
                Directory.CreateDirectory(path);
            var wrapper = new TorrentWrapper(torrent, path, new TorrentSettings());
            if (torrent.IsPrivate)
            {
                wrapper.Settings.UseDht = false;
                wrapper.Settings.EnablePeerExchange = false;
            }
            if (Client.Torrents.Any(t => t.Torrent.InfoHash == wrapper.InfoHash))
            {
                if (!suppressMessages)
                    MessageBox.Show(torrent.Name + " has already been added.", "Error");
                return null;
            }
            var periodic = Client.AddTorrent(wrapper);
            // Save torrent to cache
            var cache = Path.Combine(SettingsManager.TorrentCachePath, Path.GetFileName(torrent.TorrentPath));
            if (File.Exists(cache))
                File.Delete(cache);
            File.Copy(torrent.TorrentPath, cache);
            periodic.CacheFilePath = cache;
            periodic.UpdateInfo();
            var serializer = new JsonSerializer();
            using (var writer = new StreamWriter(Path.Combine(SettingsManager.TorrentCachePath,
                Path.GetFileNameWithoutExtension(periodic.CacheFilePath) + ".info")))
                serializer.Serialize(new JsonTextWriter(writer), periodic.TorrentInfo);

            if (SettingsManager.DeleteTorrentsAfterAdd)
                File.Delete(torrent.TorrentPath);
            return periodic;
        }