Пример #1
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;
            }
        }
Пример #2
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);
        }