/// <summary>
 ///     Initializes a new instance of this class.
 /// </summary>
 /// <param name="name">Name of file to download.</param>
 /// <param name="url">URL of file to download.</param>
 /// <param name="type">Type of file being downloaded.</param>
 /// <param name="description">Description of game being downloaded.</param>
 public DownloadItem(string name, string url, DownloadType type, GameDescription description)
 {
     _name = name;
     _gameDescription = description;
     _downloadType = type;
     _downloader = new FileDownloader(url);
     _downloader.Start();
 }
        /// <summary>
        ///     Loads in details on all games that are on the local drive.
        /// </summary>
        private void LoadMyGames()
        {
            _myGames.Clear();
            _myGamesImageList.Images.Clear();
            _myGamesImageList.ColorDepth = ColorDepth.Depth32Bit;
            _myGamesImageList.ImageSize = new Size(48, 48);

            string[] directories = Directory.GetDirectories(Fusion.GlobalInstance.BasePath);
            for (int i = 0; i < directories.Length; i++)
            {
                string gameDir = directories[i];
                string gameName = gameDir.Substring(gameDir.LastIndexOf('\\') + 1);
                if (File.Exists(gameDir + "\\" + gameName + ".xml") == false)
                    continue;

                // Grab data out of the config file.
                XmlConfigFile configFile = new XmlConfigFile(gameDir + "\\" + gameName + ".xml");
                GameDescription description = new GameDescription(gameName, configFile["title", "Unknown"], configFile["description", "Unknown"], configFile["shortDescription", "Unknown"], configFile["rating", "Unknown"], configFile["language", "Unknown"], configFile["requirements", "Unknown"], configFile["players", "Unknown"], configFile["version", "Unknown"], configFile["publisher", "Unknown"]);

                // Load an icon?
                string icon = configFile["icon", ""];
                if (icon != "" && File.Exists(gameDir + "\\" + icon))
                {
                    byte[] data = File.ReadAllBytes(gameDir + "\\" + icon);
                    Stream iconStream = new MemoryStream(data);
                    description.Icon = Image.FromStream(iconStream);
                    //iconStream.Close();

                    _myGamesImageList.Images.Add(description.Icon);
                }

                _myGames.Add(description);
            }

            myGamesStatusLabel.Text = "You currently don't own any games.";

            RefreshMyGames();
        }
        /// <summary>
        ///     Downloads the games library from the central server.
        /// </summary>
        private void LoadGamesLibrary()
        {
            // Make sure we are logged in.
            if (Fusion.GlobalInstance.CurrentUsername == "" || Fusion.GlobalInstance.CurrentPassword == "")
                return;

            // See if we can connect to the internet.
            FileDownloader downloader = new FileDownloader("http://www.binaryphoenix.com/index.php?action=fusiongameslist&username="******"&password="******"Downloading Games Library ...";
                statusToolStripStatusLabel.Text = "Downloading Games Library ...";
                statusToolStripProgressBar.Style = ProgressBarStyle.Marquee;

                while (downloader.Complete == false)
                {
                    if (downloader.Error != "")
                        throw new Exception("An error occured while connecting to the server.");
                    Application.DoEvents();
                }

                statusToolStripProgressBar.Style = ProgressBarStyle.Continuous;
                statusToolStripStatusLabel.Text = "Downloaded Games Library.";

                // Get the data.
                ASCIIEncoding encoding = new ASCIIEncoding();
                string data = encoding.GetString(downloader.FileData);

                // Clear up.
                _gamesLibrary.Clear();
                _gamesLibraryImageList.Images.Clear();
                _gamesLibraryImageList.ColorDepth = ColorDepth.Depth32Bit;
                _gamesLibraryImageList.ImageSize = new Size(48, 48);

                // Ok now parse the data.
                if (data.Length != 4 && data.ToLower() != "none")
                {
            #if RELEASE || PROFILE
                    try
                    {
            #endif
                        XmlDocument document = new XmlDocument();
                        document.LoadXml(data);

                        if (document["games"] != null)
                        {
                            for (int i = 0; i < document["games"].ChildNodes.Count; i++)
                            {
                                string title = document["games"].ChildNodes[i]["title"].InnerText;
                                string version = document["games"].ChildNodes[i]["version"].InnerText;
                                string language = document["games"].ChildNodes[i]["language"].InnerText;
                                string shortdescription = document["games"].ChildNodes[i]["shortdescription"].InnerText;
                                string description = document["games"].ChildNodes[i]["description"].InnerText;
                                string rating = document["games"].ChildNodes[i]["rating"].InnerText;
                                string players = document["games"].ChildNodes[i]["players"].InnerText;
                                string requirements = document["games"].ChildNodes[i]["requirements"].InnerText;
                                string publisher = document["games"].ChildNodes[i]["publisher"].InnerText;

                                // Download the icon.
                                FileDownloader iconDownloader = new FileDownloader("http://www.binaryphoenix.com/index.php?action=fusiongamedownload&username="******"&password="******"&game=" + System.Web.HttpUtility.UrlEncode(title) + "&file=0");
                                try
                                {
                                    iconDownloader.Start();
                                    while (iconDownloader.Complete == false)
                                    {
                                        if (iconDownloader.Error != "")
                                            break; // Not important enough for an error message.
                                        Application.DoEvents();
                                    }
                                }
                                catch {}

                                Stream iconDataStream = new MemoryStream(iconDownloader.FileData);
                                Image iconImage = Image.FromStream(iconDataStream);
                                //iconDataStream.Close();

                                GameDescription gameDescription = new GameDescription("", title, description, shortdescription, rating, language, requirements, players, version, publisher);
                                gameDescription.Icon = iconImage;
                                _gamesLibrary.Add(gameDescription);

                                _gamesLibraryImageList.Images.Add(iconImage);
                            }
                            SyncronizeWindow();
                            statusToolStripStatusLabel.Text = "Idle.";
                        }
            #if RELEASE || PROFILE
                    }
                    catch (Exception)
                    {
                        BinaryPhoenix.Fusion.Runtime.Debug.DebugLogger.WriteLog("Unable to parse game library XML, the root node dosen't exist.");
                        statusToolStripStatusLabel.Text = "An error occured while parsing game library XML.";
                    }
            #endif
                }
            #if RELEASE || PROFILE
            }
            catch (Exception)
            {
                _connectedToNet = false;
            }
            #endif

            // Is there an update available for any of our games?
            foreach (GameDescription myGameDescription in _myGames)
                foreach (GameDescription gamesLibraryDescription in _gamesLibrary)
                    if (gamesLibraryDescription.Name == myGameDescription.Name && float.Parse(gamesLibraryDescription.Version) > float.Parse(myGameDescription.Version))
                        if (MessageBox.Show("There are updates available for "+myGameDescription.Name+", would you like to download the updates now?", "Updates Available", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
                        {
                            gamesLibraryDescription.Downloading = true;
                            AddDownload(gamesLibraryDescription.Name + " (Version " + gamesLibraryDescription.Version + ")", "http://www.binaryphoenix.com/index.php?action=fusiongamedownload&username="******"&password="******"&game=" + System.Web.HttpUtility.UrlEncode(gamesLibraryDescription.Name) + "&file=1", DownloadType.Game, gamesLibraryDescription);
                        }
        }
 /// <summary>
 ///     Starts a new download item and adds it to the downloads page.
 /// </summary>
 /// <param name="name">Name of the download.</param>
 /// <param name="url">URL of file to download.</param>
 /// <param name="type">Type of download.</param>
 /// <param name="description">Game description, used in the case of game downloads.</param>
 private void AddDownload(string name, string url, DownloadType type, GameDescription description)
 {
     DownloadItem download = new DownloadItem(name, url, type, description);
     download.ListViewItem = new ListViewItem(new string[] { "", download.Name, "Unknown", "Unknown", "Unknown" });
     download.ListViewItem.StateImageIndex = 0;
     downloadsListView.Items.Add(download.ListViewItem);
     _downloads.Add(download);
     SyncronizeWindow();
 }