private void ProfileFileWatcher_Created(object sender, FileSystemEventArgs e) { lock (lockobj) { ProfileMeta info = null; var retries = 0; do { info = Profile.Peek(Path.Combine(PROFILEFOLDER, e.Name)); if (info == null) { Thread.Sleep(TimeSpan.FromMilliseconds(500)); retries++; } } while (retries < 3 && info == null); if (info != null) { Profiles.Add(info); } else { var id = Guid.Parse(Path.GetFileNameWithoutExtension(e.Name)); Profiles.Add(new ProfileMeta() { Id = id, Location = e.FullPath, LastUsed = DateTime.MinValue, IsActive = false, Name = "UNKOWN" }); } } }
/// <summary> /// Migrate old profile.settings into new separted profile files /// Last active profile will get its LastUsed date to DateTime.Now to be selected first. /// </summary> private void MigrateOldProfile() { var s = File.ReadAllText(OLDPROFILEFILEPATH); s = s.Replace("NINA.Utility.Profile", "NINA.Profile"); var tmp = Path.Combine(Utility.Utility.APPLICATIONTEMPPATH, "migration.profiles"); File.WriteAllText(tmp, s); using (var fs = new FileStream(tmp, FileMode.Open, FileAccess.Read)) { var serializer = new DataContractSerializer(typeof(Profiles)); var obj = serializer.ReadObject(fs); var files = (Profiles)obj; foreach (Profile p in files.ProfileList) { if (p.Id == files.ActiveProfileId) { p.LastUsed = DateTime.Now; } p.Save(); var info = new ProfileMeta() { Id = p.Id, Name = p.Name, Location = p.Location, LastUsed = p.LastUsed }; p.Dispose(); Profiles.Add(info); } } }
private ProfileMeta AddDefaultProfile() { lock (lockobj) { if (profileFileWatcher != null) { profileFileWatcher.EnableRaisingEvents = false; } using (var p = new Profile("Default")) { p.Save(); var info = new ProfileMeta() { Id = p.Id, Name = p.Name, Location = p.Location }; Profiles.Add(info); if (profileFileWatcher != null) { profileFileWatcher.EnableRaisingEvents = true; } return(info); } } }
public bool SelectProfile(ProfileMeta info) { lock (lockobj) { using (MyStopWatch.Measure()) { try { var p = Profile.Load(info.Location); UnregisterChangedEventHandlers(); if (ActiveProfile != null) { ActiveProfile.Dispose(); Profiles.Where(x => x.Id == ActiveProfile.Id).First().IsActive = false; } ActiveProfile = p; info.IsActive = true; System.Threading.Thread.CurrentThread.CurrentUICulture = ActiveProfile.ApplicationSettings.Language; System.Threading.Thread.CurrentThread.CurrentCulture = ActiveProfile.ApplicationSettings.Language; Locale.Loc.Instance.ReloadLocale(ActiveProfile.ApplicationSettings.Culture); LocaleChanged?.Invoke(this, null); ProfileChanged?.Invoke(this, null); LocationChanged?.Invoke(this, null); RegisterChangedEventHandlers(); } catch (Exception ex) { Logger.Debug(ex.Message + Environment.NewLine + ex.StackTrace); return(false); } return(true); } } }
public bool RemoveProfile(ProfileMeta info) { lock (lockobj) { if (!Profile.Remove(info)) { return(false); } else { Profiles.Remove(info); return(true); } } }
public bool Clone(ProfileMeta profileInfo) { lock (lockobj) { using (MyStopWatch.Measure()) { if (profileFileWatcher != null) { profileFileWatcher.EnableRaisingEvents = false; } IProfile clone = null; if (profileInfo.Id == ActiveProfile.Id) { clone = Profile.Clone(ActiveProfile); } else { try { var p = Profile.Load(profileInfo.Location); clone = Profile.Clone(p); p.Dispose(); } catch (Exception) { //Profile is in use return(false); } } if (clone != null) { clone.Save(); var info = new ProfileMeta() { Id = clone.Id, Name = clone.Name, Location = clone.Location }; Profiles.Add(info); clone.Dispose(); if (profileFileWatcher != null) { profileFileWatcher.EnableRaisingEvents = true; } } return(true); } } }