Skip to content

Commit

Permalink
Store each group individually in registry
Browse files Browse the repository at this point in the history
Resolves eamodio#27
  • Loading branch information
Alexey Andronov committed Jun 14, 2020
1 parent 36b3380 commit 889ff18
Showing 1 changed file with 52 additions and 25 deletions.
77 changes: 52 additions & 25 deletions src/SaveAllTheTabs/DocumentManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
Expand All @@ -8,6 +9,8 @@
using System.Linq;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Settings;
Expand Down Expand Up @@ -68,7 +71,7 @@ internal class DocumentManager : IDocumentManager
private const int SlotMax = 9;

private const string StorageCollectionPath = "SaveAllTheTabs";
private const string SavedTabsStoragePropertyFormat = "SavedTabs.{0}";
private const string ProjectGroupsKeyPlaceholder = StorageCollectionPath + "\\{0}\\groups";

private SaveAllTheTabsPackage Package { get; }
private IServiceProvider ServiceProvider => Package;
Expand Down Expand Up @@ -426,27 +429,35 @@ private List<DocumentGroup> LoadGroupsForSolution()
{
ThreadHelper.ThrowIfNotOnUIThread();
var solution = SolutionName;
if (!string.IsNullOrWhiteSpace(solution))
{
try
{
var settingsMgr = new ShellSettingsManager(ServiceProvider);
var store = settingsMgr.GetReadOnlySettingsStore(SettingsScope.UserSettings);
List<DocumentGroup> groups = new List<DocumentGroup>();
if (string.IsNullOrWhiteSpace(solution)) {
return groups;
}

var propertyName = String.Format(SavedTabsStoragePropertyFormat, solution);
if (store.PropertyExists(StorageCollectionPath, propertyName))
{
var tabs = store.GetString(StorageCollectionPath, propertyName);
return JsonConvert.DeserializeObject<List<DocumentGroup>>(tabs);
}
try {
var settingsMgr = new ShellSettingsManager(ServiceProvider);
var store = settingsMgr.GetReadOnlySettingsStore(SettingsScope.UserSettings);

string projectKeyHash = GetHashString(solution);
string projectGroupsKey = string.Format(ProjectGroupsKeyPlaceholder, projectKeyHash);

if (!store.CollectionExists(projectGroupsKey))
{
return groups;
}
catch (Exception ex)

var groupProperties = store.GetPropertyNamesAndValues(projectGroupsKey);
foreach (var groupProperty in groupProperties)
{
Debug.Assert(false, nameof(LoadGroupsForSolution), ex.ToString());
DocumentGroup group = JsonConvert.DeserializeObject<DocumentGroup>(groupProperty.Value.ToString());
groups.Add(group);
}
} catch (Exception ex) {
Debug.Assert(false, nameof(LoadGroupsForSolution), ex.ToString());
}

return groups;
}
return new List<DocumentGroup>();
}

private void SaveGroupsForSolution(IList<DocumentGroup> groups = null)
{
Expand All @@ -465,20 +476,36 @@ private void SaveGroupsForSolution(IList<DocumentGroup> groups = null)
var settingsMgr = new ShellSettingsManager(ServiceProvider);
var store = settingsMgr.GetWritableSettingsStore(SettingsScope.UserSettings);

if (!store.CollectionExists(StorageCollectionPath))
string projectKeyHash = GetHashString(solution);
string projectGroupsKey = string.Format(ProjectGroupsKeyPlaceholder, projectKeyHash);

if (store.CollectionExists(projectGroupsKey))
{
store.CreateCollection(StorageCollectionPath);
store.DeleteProperty(StorageCollectionPath, projectGroupsKey);
}
store.CreateCollection(projectGroupsKey);

var propertyName = String.Format(SavedTabsStoragePropertyFormat, solution);
if (!groups.Any())
foreach (DocumentGroup group in groups)
{
store.DeleteProperty(StorageCollectionPath, propertyName);
return;
var serializedGroup = JsonConvert.SerializeObject(group);
store.SetString(projectGroupsKey, group.Name, serializedGroup);
}
}

var tabs = JsonConvert.SerializeObject(groups);
store.SetString(StorageCollectionPath, propertyName, tabs);
private static string GetHashString(string source)
{
using (System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create())
{
byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(source);
byte[] hashBytes = md5.ComputeHash(inputBytes);
// Convert the byte array to hexadecimal string
StringBuilder sb = new StringBuilder();
for (int i = 0; i < hashBytes.Length; i++)
{
sb.Append(hashBytes [i].ToString("X2"));
}
return sb.ToString();
}
}

public static bool IsStashGroup(string name)
Expand Down

0 comments on commit 889ff18

Please sign in to comment.