private void LoadTorrents()
        {
            var documents = Environment.GetFolderPath (Environment.SpecialFolder.MyDocuments);

            var filename = Path.Combine (documents, "delugemobile_torrents.xml");

            if (System.IO.File.Exists (filename)) {

                XmlSerializer deserializer = new XmlSerializer (typeof(Torrents));
                TextReader textReader = new StreamReader (filename);
                ts = (Torrents)deserializer.Deserialize (textReader);
                textReader.Close ();
            }

            List<CodeRinseRepeat.Deluge.Torrent> torrents = new List<Torrent>();

            if (ts != null) {

                foreach (TorrentsDetails td in ts.TorrentsList) {

                    CodeRinseRepeat.Deluge.Torrent t = new CodeRinseRepeat.Deluge.Torrent ();

                    t.TotalSize = td.dl;
                    t.TotalPayloadUpload = td.ul;
                    t.TrackerHost = td.host;
                    t.Name = td.name;
                    t.Ratio = td.ratio;
                    t.State = td.status;

                    torrents.Add (t);
                }

                var sortedTorrents = torrents.OrderBy (c => c.TrackerHost).ThenBy (c => c.TimeAdded);
                curTorrents = sortedTorrents;
            }
        }
        /*
         * Interesting methods: { "core.upload_plugin", "core.glob",
         * "core.remove_torrent", "core.resume_all_torrents", "core.queue_top", "core.set_torrent_options",
         * "core.set_torrent_prioritize_first_last", "core.get_session_state", "core.set_torrent_move_completed",
         * "core.set_torrent_file_priorities", "core.get_config", "core.disable_plugin",
         * "core.test_listen_port", "core.connect_peer", "core.enable_plugin", "core.get_filter_tree",
         * "core.set_torrent_remove_at_ratio", "core.get_config_values", "core.pause_torrent",
         * "core.move_storage", "core.force_reannounce", "core.add_torrent_file", "core.get_listen_port",
         * "core.set_torrent_move_completed_path", "core.set_torrent_stop_at_ratio", "core.rename_folder",
         * "core.add_torrent_url", "core.get_enabled_plugins", "core.get_libtorrent_version", "core.get_path_size",
         * "core.set_torrent_max_connections", "core.get_config_value", "core.get_session_status", "core.create_torrent",
         * "core.add_torrent_magnet", "core.set_torrent_stop_ratio", "core.set_torrent_auto_managed",
         * "core.pause_all_torrents", "core.rename_files", "core.get_free_space",
         * "core.queue_bottom", "core.set_torrent_max_upload_speed", "core.resume_torrent",
         * "core.set_torrent_max_upload_slots", "core.set_config", "core.get_cache_status", "core.queue_down",
         * "core.get_num_connections", "core.set_torrent_max_download_speed", "core.queue_up", "core.set_torrent_trackers"
         * }
         *
         * Implemented methods: {
         *  auth.login, system.listMethods, core.get_torrents_status, core.get_available_plugins,
         *  core.rescan_plugins, core.get_torrent_status
         * }
         */
        public Torrent GetTorrentStatus(Torrent t, string[] fields)
        {
            var result = DoServiceCall ("core.get_torrent_status", t.Hash, fields);
            var torrentObject = (JsonObject) result["result"];

            if (!t.Hash.Equals ((string) torrentObject["hash"]))
                throw new ApplicationException (String.Format ("Hashes don't match, expected {0}, got {1}!", t.Hash, torrentObject["hash"]));

            return t.UpdateFrom (torrentObject);
        }
 public Torrent GetTorrentStatus(Torrent t)
 {
     return GetTorrentStatus (t, emptyStringArray);
 }
 internal static Torrent TorrentFromJsonObject(JsonObject torrentObject)
 {
     unchecked {
         Torrent t = new Torrent ();
         t.Comment = (string) torrentObject[Torrent.Fields.Comment];
         t.ActiveTime = TimeSpan.FromSeconds ((long) torrentObject[Torrent.Fields.ActiveTime]);
         t.Seeding = (bool) torrentObject[Torrent.Fields.Seeding];
         t.UploadSpeed = (double) torrentObject[Torrent.Fields.UploadSpeed];
         t.Private = (bool) torrentObject[Torrent.Fields.Private];
         t.TotalPayloadUpload = (long) torrentObject[Torrent.Fields.TotalPayloadUpload];
         t.Paused = (bool) torrentObject[Torrent.Fields.Paused];
         t.SeedRank = Convert.ToInt32 (torrentObject[Torrent.Fields.SeedRank]);
         t.SeedingTime = TimeSpan.FromSeconds ((long) torrentObject[Torrent.Fields.SeedingTime]);
         t.MaxUploadSlots = Convert.ToInt32 (torrentObject[Torrent.Fields.MaxUploadSlots]);
         t.PrioritizeFirstLast = (bool) torrentObject[Torrent.Fields.PrioritizeFirstLast];
         t.DistributedCopies = Convert.ToDouble (torrentObject[Torrent.Fields.DistributedCopies]);
         t.DownloadSpeed = Convert.ToDouble (torrentObject[Torrent.Fields.DownloadSpeed]);
         t.Message = (string) torrentObject[Torrent.Fields.Message];
         t.ConnectedPeers = Convert.ToInt32 (torrentObject[Torrent.Fields.ConnectedPeers]);
         t.MaxDownloadSpeed = Convert.ToDouble (torrentObject[Torrent.Fields.MaxDownloadSpeed]);
         t.MaxConnections = Convert.ToInt32 (torrentObject[Torrent.Fields.MaxConnections]);
         t.Compact = (bool) torrentObject[Torrent.Fields.Compact];
         t.Ratio = Convert.ToDouble (torrentObject[Torrent.Fields.Ratio]);
         t.TotalPeers = Convert.ToInt32 (torrentObject[Torrent.Fields.TotalPeers]);
         t.TotalSize = (long) torrentObject[Torrent.Fields.TotalSize];
         t.TotalWanted = (long) torrentObject[Torrent.Fields.TotalWanted];
         t.State = (State) Enum.Parse (typeof (State), (string) torrentObject[Torrent.Fields.State]);
         t.Files = GetFiles (torrentObject);
         t.MaxUploadSpeed = Convert.ToDouble (torrentObject[Torrent.Fields.MaxUploadSpeed]);
         t.RemoveAtRatio = (bool) torrentObject[Torrent.Fields.RemoveAtRatio];
         t.Trackers = GetTrackers (torrentObject);
         t.Tracker = t.Trackers.Where (tracker => tracker.Url.Equals ((string) torrentObject[Torrent.Fields.Tracker])).FirstOrDefault ();
         t.TrackerStatus = (string) torrentObject[Torrent.Fields.TrackerStatus];
         t.SavePath = (string) torrentObject[Torrent.Fields.SavePath];
         t.Progress = Convert.ToDouble (torrentObject[Torrent.Fields.Progress]);
         t.TimeAdded = unixTime.AddSeconds (Convert.ToDouble (torrentObject[Torrent.Fields.TimeAdded]));
         t.TrackerHost = (string) torrentObject[Torrent.Fields.TrackerHost];
         t.TotalUploaded = (long) torrentObject[Torrent.Fields.Uploaded];
         t.TotalSeeds = Convert.ToInt32 (torrentObject[Torrent.Fields.TotalSeeds]);
         t.MoveOnCompleted = (bool) torrentObject[Torrent.Fields.MoveOnCompleted];
         t.NextAnnounce = TimeSpan.FromSeconds ((long) torrentObject[Torrent.Fields.NextAnnounce]);
         t.StopAtRatio = (bool) torrentObject[Torrent.Fields.StopAtRatio];
         t.PieceSize = (long) torrentObject[Torrent.Fields.PieceSize];
         t.AllTimeDownload = (long) torrentObject[Torrent.Fields.AllTimeDownloaded];
         t.MoveOnCompletedPath = (string) torrentObject[Torrent.Fields.MoveOnCompletedPath];
         t.ConnectedSeeds = Convert.ToInt32 (torrentObject[Torrent.Fields.ConnectedSeeds]);
         t.Peers = GetPeers (torrentObject);
         t.Name = (string) torrentObject[Torrent.Fields.Name];
         t.TotalPayloadDownload = (long) torrentObject[Torrent.Fields.TotalPayloadDownload];
         t.IsAutoManaged = (bool) torrentObject[Torrent.Fields.IsAutoManaged];
         t.Queue = Convert.ToInt32 (torrentObject[Torrent.Fields.Queue]);
         t.ETA = DateTime.Now.AddSeconds ((long) torrentObject[Torrent.Fields.ETA]);
         t.StopRatio = Convert.ToDouble (torrentObject[Torrent.Fields.StopAtRatio]);
         t.Finished = (bool) torrentObject[Torrent.Fields.IsFinished];
         return t;
     }
 }