示例#1
0
        // --------------------------------------------------------------------------------------------------
        #region Load/Save class functions

        /// <summary>
        /// Generic function to Load the Settings of LimeLauncher from an XML file.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="path">Path of the file/link setting representation.</param>
        /// <returns>The resulting LimeConfig (typically a singleton instance).</returns>
        public static T Load <T>(string path) where T : LimeConfig
        {
            LimeMsg.Debug("LimeConfig Load: {0}", path);

            try
            {
                XmlSerializer reader = new XmlSerializer(typeof(T));
                using (StreamReader file = new StreamReader(path))
                {
                    XmlReader xread = XmlReader.Create(file);                     // required to preserve whitespaces
                    T         ret   = (T)reader.Deserialize(xread);
                    ret.Modified = false;
                    return(ret);
                }
            }
            catch (FileNotFoundException)
            {
                LimeMsg.Debug("Setting file not found: {0}", path);
            }
            catch (Exception ex)
            {
                LimeMsg.Error("ErrLoadFile", path);
                LimeMsg.Debug("LimeConfig Load error: {0}", ex.ToString());
            }

            return((T)Activator.CreateInstance(typeof(T)));
        }
示例#2
0
        /// <summary>
        /// Try to save the Person data to the local database.
        /// </summary>
        public void Save()
        {
            if (IsBusy)
            {
                return;
            }
            if (string.IsNullOrEmpty(Name))
            {
                return;
            }

            // Make filename from person name
            string path    = null;
            string localDb = LimeLib.ResolvePath(LocalDbPath);

            if (localDb != null)
            {
                var name = Name;

                // Remove characters forbidden in filenames
                string invalid = new string(Path.GetInvalidFileNameChars()) + new string(Path.GetInvalidPathChars());
                foreach (char c in invalid)
                {
                    name = name.Replace(c.ToString(), "");
                }

                path = Path.Combine(localDb, name + Extension);
            }

            try
            {
                IsSaving = true;
                LimeMsg.Debug("Person SaveAsync: {0}", Name);
                using (Stream file = File.Open(path, FileMode.Create))
                {
                    ZeroFormatterSerializer.Serialize(file, this);
                }
            }
            catch
            {
                LimeMsg.Error("UnableFileDir", path);
            }
            finally
            {
                IsSaving = false;
                IsLoaded = true;
            }
        }
示例#3
0
        /// <summary>
        /// Save the Settings of LimeLauncher to an XML file.
        /// </summary>
        /// <param name="path">File to save the settings to.</param>
        /// <param name="data">Instance of the data to be saved.</param>
        /// <returns>true if successful, false otherwise.</returns>
        public static bool Save <T>(string path, T data) where T : LimeConfig
        {
            if (data == null)
            {
                LimeMsg.Error("CfgInit");
                return(false);
            }

            LimeMsg.Debug("LimeConfig Save: {0}", path);

            try
            {
                XmlSerializer writer = new XmlSerializer(typeof(T));
                using (StreamWriter file = new StreamWriter(path))
                    writer.Serialize(file, data);
                data.Modified = false;
                return(true);
            }
            catch
            {
                LimeMsg.Error("ErrSavFile", path);
                return(false);
            }
        }
示例#4
0
        /// <summary>
        /// Try to retrieve the Person data from the local database, or Internet
        /// </summary>
        /// <returns>true if successful (false if not found)</returns>
        public bool Load()
        {
            if (IsLoading || IsSaving || IsLoaded)
            {
                return(true);
            }
            if (string.IsNullOrEmpty(Name))
            {
                return(false);
            }


            // Make filename from person name
            string path    = null;
            string localDb = LimeLib.ResolvePath(LocalDbPath);

            if (localDb != null)
            {
                var name = Name;

                // Remove characters forbidden in filenames
                string invalid = new string(Path.GetInvalidFileNameChars()) + new string(Path.GetInvalidPathChars());
                foreach (char c in invalid)
                {
                    name = name.Replace(c.ToString(), "");
                }

                path = Path.Combine(localDb, name + Extension);
            }

            try
            {
                IsLoading = true;

                if (File.Exists(path))
                {
                    LimePerson lp;
                    // Local Database
                    using (Stream file = File.Open(path, FileMode.Open))
                    {
                        lp = ZeroFormatterSerializer.Deserialize <LimePerson>(file);
                    }

                    // Properties to be preserved
                    var roles         = Roles;
                    var rolesReadOnly = RolesReadOnly;

                    // Copy properties
                    LimeLib.CopyPropertyValues(lp, this);

                    // Restore Properties to be preserved
                    Roles         = roles;
                    RolesReadOnly = rolesReadOnly;

                    // Done
                    IsLoaded = true;
                    return(true);
                }
            }
            catch
            {
                LimeMsg.Error("UnableFileDir", path);
            }
            finally
            {
                IsLoading = false;
            }

            if (IsDownloading && MetaSearch != null)
            {
                bool ret = false;
                try
                {
                    IsLoading = true;
                    LimeMsg.Debug("Person Load: Download {0}", Name);
                    ret = MetaSearch.GetPersonAsync(this).Result;
                }
                catch
                {
                    LimeMsg.Error("ErrPersonDownload", Name);
                }
                finally
                {
                    IsLoading     = false;
                    IsDownloading = false;
                }

                IsLoaded = true;

                if (ret && AutoSave)
                {
                    Save();
                }

                return(ret);
            }

            return(false);
        }