Inheritance: PropertyChangedBase
        public static void Serialize(ListViewModel vm, Stream stream)
        {
            if (vm == null)
                throw new ArgumentNullException(nameof(vm));
            if (stream == null)
                throw new ArgumentNullException(nameof(stream));

            var data = new ToolWindowStateProjectData();

            foreach (var item in vm.DataCollection)
            {
                data.DataCollection.Add(new ToolWindowStateProjectData.ListEntryData()
                {
                    Id = item.Id,
                    Command = item.Command,
                    //Project = item.Project,   // deprecated
                    //Enabled = item.Enabled
                });
            }

            string jsonStr = JsonConvert.SerializeObject(data, Formatting.Indented);

            StreamWriter sw = new StreamWriter(stream, new UTF8Encoding(encoderShouldEmitUTF8Identifier: false));
            sw.Write(jsonStr);
            sw.Flush();
        }
Esempio n. 2
0
        protected override void OnSaveOptions(string key, Stream stream)
        {
            base.OnSaveOptions(key, stream);
            if (key == SolutionOptionKey)
            {
                Logger.Info("Saving all commands.");

                if (IsVcsSupportEnabled)
                {
                    Logger.Info("VcsSupport is enabled.");

                    foreach (EnvDTE.Project project in vsHelper.FindAllProjects())
                    {
                        ViewModel.ListViewModel vm = null;
                        if (ToolWindowViewModel.SolutionArguments.TryGetValue(project, out vm))
                        {
                            string            filePath  = FullFilenameForProjectJsonFile(project);
                            FileSystemWatcher fsWatcher = projectFsWatchers.GetValueOrDefault(project);

                            if (vm.DataCollection.Count != 0)
                            {
                                using (fsWatcher?.TemporarilyDisable())
                                {
                                    try
                                    {
                                        using (Stream fileStream = File.Open(filePath, FileMode.Create, FileAccess.Write))
                                        {
                                            Logic.ToolWindowProjectDataSerializer.Serialize(vm, fileStream);
                                        }
                                    }
                                    catch (Exception e)
                                    {
                                        Logger.Warn($"Failed to write to file '{filePath}' with error '{e}'.");
                                    }
                                }
                            }
                            else if (File.Exists(filePath))
                            {
                                Logger.Info("Deleting json file because command list is empty but json-file exists.");

                                try
                                {
                                    File.Delete(filePath);
                                }
                                catch (Exception e)
                                {
                                    Logger.Warn($"Failed to delete file '{filePath}' with error '{e}'.");
                                }
                            }
                        }
                    }
                }

                Logic.ToolWindowSolutionDataSerializer.Serialize(ToolWindowViewModel, stream);
            }
            Logger.Info("All Commands Saved.");
        }
 public ListViewModel GetListViewModel(Project project)
 {
     ListViewModel listVM;
     if (!solutionArguments.TryGetValue(project, out listVM))
     {
         listVM = new ListViewModel();
         solutionArguments.Add(project, listVM);
     }
     return listVM;
 }