public FormMain()
        {
            InitializeComponent();

            using (FormUserSelection formUserSelection = new FormUserSelection()) {
                formUserSelection.ShowDialog(this);

                if (formUserSelection.DialogResult == DialogResult.OK)
                {
                    this.userProfiles = formUserSelection.userProfiles;
                    this.currentUser  = formUserSelection.selectedUser;
                    this.selectedUser = true;

                    this.dataManager   = new DataManager(currentUser);
                    this.entityManager = new EntityManager(currentUser);

                    this.currentUser.applicationSettingsField = dataManager.ReadUserSettingsFile(this.currentUser);

                    if (this.currentUser.applicationSettingsField.DefaultsRestored)
                    {
                        dataManager.SaveUserSettings();

                        this.currentUser.applicationSettingsField.DefaultsRestored = false;
                    }
                }
            }
        }
        public FormMain()
        {
            InitializeComponent();

            using (FormUserSelection formUserSelection = new FormUserSelection()) {
                formUserSelection.ShowDialog(this);

                if (formUserSelection.DialogResult == DialogResult.OK) {
                    this.userProfiles = formUserSelection.userProfiles;
                    this.currentUser = formUserSelection.selectedUser;
                    this.selectedUser = true;

                    this.dataManager = new DataManager(currentUser);
                    this.entityManager = new EntityManager(currentUser);

                    this.currentUser.applicationSettingsField = dataManager.ReadUserSettingsFile(this.currentUser);

                    if (this.currentUser.applicationSettingsField.DefaultsRestored) {
                        dataManager.SaveUserSettings();

                        this.currentUser.applicationSettingsField.DefaultsRestored = false;
                    }
                }
            }
        }
        public static void SaveUser(HMEntities.UserProfiles.User user)
        {
            HMDal.DataManager dataManager = new HMDal.DataManager();

            HMEntities.UserProfiles.UserProfiles userProfiles = dataManager.LoadUserProfiles();
            List <HMEntities.UserProfiles.User>  userList     = userProfiles.userListField;

            int index = userList.FindIndex(p => p.teamIdField == user.teamIdField);

            if (index == -1)
            {
                // If profile with this TeamId does not exist, add it...
                userList.Add(user);
            }
            else
            {
                // ... otherwise, update its data
                userList[index].CopyData(user);
            }

            dataManager.SaveUserProfiles(userProfiles);
        }
        /// <summary>
        /// Writes the UserProfiles to disk
        /// </summary>
        /// <param name="hattrickManagerData">UserProfiles to write</param>
        private void WriteUserProfilesFile(HMEntities.UserProfiles.UserProfiles hattrickManagerData)
        {
            try {
                XmlDocument xmlDocument = new XmlDocument();

                XmlElement xmlElementRoot = xmlDocument.CreateElement(Tags.HattrickManagerData);

                hattrickManagerData.savedDateField = DateTime.Now;

                xmlElementRoot.AppendChild(xmlDocument.CreateElement(Tags.SavedDate)).InnerText = hattrickManagerData.savedDateField.ToString(General.DateTimeFormat);

                XmlElement xmlElementUserList = xmlDocument.CreateElement(Tags.UserList);

                foreach (HMEntities.UserProfiles.User currentUser in hattrickManagerData.userListField)
                {
                    XmlElement xmlElementTeamId      = xmlDocument.CreateElement(Tags.TeamID);
                    XmlElement xmlElementYouthTeamId = xmlDocument.CreateElement(Tags.YouthTeamID);
                    XmlElement xmlElementLoginname   = xmlDocument.CreateElement(Tags.Loginname);
                    XmlElement xmlElementToken       = xmlDocument.CreateElement(Tags.UserToken);
                    XmlElement xmlElementTokenSecret = xmlDocument.CreateElement(Tags.UserTokenSecret);
                    XmlElement xmlElementActivation  = xmlDocument.CreateElement(Tags.ActivationDate);
                    XmlElement xmlElementDataFolder  = xmlDocument.CreateElement(Tags.DataFolder);

                    xmlElementTeamId.InnerText      = currentUser.teamIdField.ToString();
                    xmlElementYouthTeamId.InnerText = currentUser.youthTeamIdField.ToString();
                    xmlElementLoginname.InnerText   = currentUser.username;
                    xmlElementToken.InnerText       = currentUser.accessToken;
                    xmlElementTokenSecret.InnerText = currentUser.accessTokenSecret;

                    xmlElementActivation.InnerText = currentUser.activationDateField.ToString(General.DateTimeFormat);
                    xmlElementDataFolder.InnerText = currentUser.dataFolderField;

                    XmlElement xmlElementUser = xmlDocument.CreateElement(Tags.User);

                    xmlElementUser.AppendChild(xmlElementTeamId);
                    xmlElementUser.AppendChild(xmlElementYouthTeamId);
                    xmlElementUser.AppendChild(xmlElementLoginname);
                    xmlElementUser.AppendChild(xmlElementToken);
                    xmlElementUser.AppendChild(xmlElementTokenSecret);

                    xmlElementUser.AppendChild(xmlElementActivation);
                    xmlElementUser.AppendChild(xmlElementDataFolder);

                    xmlElementUserList.AppendChild(xmlElementUser);
                }

                xmlElementRoot.AppendChild(xmlElementUserList);

                xmlDocument.AppendChild(xmlElementRoot);

                string path     = GenericFunctions.GetFolderNameByFileType(FileType.WorldDetails);
                string fileName = Path.Combine(path, FileNames.UserProfiles);

                if (!System.IO.Directory.Exists(path))
                {
                    System.IO.Directory.CreateDirectory(path);
                }

                xmlDocument.Save(fileName);
            } catch (Exception ex) {
                throw ex;
            }
        }
        /// <summary>
        /// Reads UserProfile xml file
        /// </summary>
        /// <param name="xmlStream">Xml file content</param>
        /// <returns>UserProfiles object loaded with readed data</returns>
        public HMEntities.UserProfiles.UserProfiles ReadUserProfilesFile(Stream xmlStream)
        {
            try {
                XmlDocument xmlDocument = new XmlDocument();
                xmlDocument.Load(xmlStream);

                HMEntities.UserProfiles.UserProfiles hattrickManagerData = new HM.Entities.HattrickManager.UserProfiles.UserProfiles();

                if (xmlDocument.DocumentElement.ChildNodes != null)
                {
                    foreach (XmlNode xmlNodeRoot in xmlDocument.DocumentElement.ChildNodes)
                    {
                        switch (xmlNodeRoot.Name)
                        {
                        case Tags.SavedDate: {
                            hattrickManagerData.savedDateField = GenericFunctions.ConvertStringToDateTime(xmlNodeRoot.InnerText);
                            break;
                        }

                        case Tags.UserList: {
                            if (xmlNodeRoot.ChildNodes != null)
                            {
                                foreach (XmlNode xmlNodeUserList in xmlNodeRoot.ChildNodes)
                                {
                                    if (xmlNodeUserList.ChildNodes != null)
                                    {
                                        HMEntities.UserProfiles.User hattrickManagerDataUser = new HM.Entities.HattrickManager.UserProfiles.User();

                                        foreach (XmlNode xmlNodeUser in xmlNodeUserList)
                                        {
                                            switch (xmlNodeUser.Name)
                                            {
                                            case Tags.TeamID: {
                                                hattrickManagerDataUser.teamIdField = GenericFunctions.ConvertStringToUInt(xmlNodeUser.InnerText);
                                                break;
                                            }

                                            case Tags.YouthTeamID: {
                                                hattrickManagerDataUser.youthTeamIdField = GenericFunctions.ConvertStringToUInt(xmlNodeUser.InnerText);
                                                break;
                                            }

                                            case Tags.Loginname: {
                                                hattrickManagerDataUser.username = xmlNodeUser.InnerText;
                                                break;
                                            }

                                            case Tags.UserToken: {
                                                hattrickManagerDataUser.accessToken = xmlNodeUser.InnerText;
                                                break;
                                            }

                                            case Tags.UserTokenSecret: {
                                                hattrickManagerDataUser.accessTokenSecret = xmlNodeUser.InnerText;
                                                break;
                                            }

                                            case Tags.ActivationDate: {
                                                hattrickManagerDataUser.activationDateField = GenericFunctions.ConvertStringToDateTime(xmlNodeUser.InnerText);
                                                break;
                                            }

                                            case Tags.DataFolder: {
                                                hattrickManagerDataUser.dataFolderField = xmlNodeUser.InnerText;
                                                break;
                                            }
                                            }
                                        }

                                        hattrickManagerData.userListField.Add(hattrickManagerDataUser);
                                    }
                                }
                            }
                            break;
                        }
                        }
                    }
                }
                xmlStream.Close();
                return(hattrickManagerData);
            } catch (Exception ex) {
                throw ex;
            }
        }
 public void SaveUserProfiles(UserProfiles userProfiles)
 {
     WriteUserProfilesFile(userProfiles);
 }
        /// <summary>
        /// Reads UserProfile xml file
        /// </summary>
        /// <param name="xmlStream">Xml file content</param>
        /// <returns>UserProfiles object loaded with readed data</returns>
        public HMEntities.UserProfiles.UserProfiles ReadUserProfilesFile(Stream xmlStream)
        {
            try {
                XmlDocument xmlDocument = new XmlDocument();
                xmlDocument.Load(xmlStream);

                HMEntities.UserProfiles.UserProfiles hattrickManagerData = new HM.Entities.HattrickManager.UserProfiles.UserProfiles();

                if (xmlDocument.DocumentElement.ChildNodes != null) {
                    foreach (XmlNode xmlNodeRoot in xmlDocument.DocumentElement.ChildNodes) {
                        switch (xmlNodeRoot.Name) {
                            case Tags.SavedDate: {
                                    hattrickManagerData.savedDateField = GenericFunctions.ConvertStringToDateTime(xmlNodeRoot.InnerText);
                                    break;
                                }
                            case Tags.UserList: {
                                    if (xmlNodeRoot.ChildNodes != null) {
                                        foreach (XmlNode xmlNodeUserList in xmlNodeRoot.ChildNodes) {
                                            if (xmlNodeUserList.ChildNodes != null) {
                                                HMEntities.UserProfiles.User hattrickManagerDataUser = new HM.Entities.HattrickManager.UserProfiles.User();

                                                foreach (XmlNode xmlNodeUser in xmlNodeUserList) {
                                                    switch (xmlNodeUser.Name) {
                                                        case Tags.TeamID: {
                                                                hattrickManagerDataUser.teamIdField = GenericFunctions.ConvertStringToUInt(xmlNodeUser.InnerText);
                                                                break;
                                                            }
                                                        case Tags.YouthTeamID: {
                                                                hattrickManagerDataUser.youthTeamIdField = GenericFunctions.ConvertStringToUInt(xmlNodeUser.InnerText);
                                                                break;
                                                            }
                                                        case Tags.Loginname: {
                                                                hattrickManagerDataUser.username = xmlNodeUser.InnerText;
                                                                break;
                                                            }
                                                        case Tags.UserToken: {
                                                                hattrickManagerDataUser.accessToken = xmlNodeUser.InnerText;
                                                                break;
                                                            }
                                                        case Tags.UserTokenSecret: {
                                                                hattrickManagerDataUser.accessTokenSecret = xmlNodeUser.InnerText;
                                                                break;
                                                            }
                                                        case Tags.ActivationDate: {
                                                                hattrickManagerDataUser.activationDateField = GenericFunctions.ConvertStringToDateTime(xmlNodeUser.InnerText);
                                                                break;
                                                            }
                                                        case Tags.DataFolder: {
                                                                hattrickManagerDataUser.dataFolderField = xmlNodeUser.InnerText;
                                                                break;
                                                            }
                                                    }
                                                }

                                                hattrickManagerData.userListField.Add(hattrickManagerDataUser);
                                            }
                                        }
                                    }
                                    break;
                                }
                        }
                    }
                }
                xmlStream.Close();
                return hattrickManagerData;
            } catch (Exception ex) {
                throw ex;
            }
        }