using Newtonsoft.Json; using Perforce.Model; using System; using System.Collections.Generic; using System.IO; using System.Windows.Forms; namespace Perforce.Helper { public class FavoritesHelper { private static string FAVORITE_FOLDERS_KEY = "{0}.favoriteFolders"; private static string FAVORITE_TAGS_KEY = "{0}.favoriteTags"; private string _dataFileName = Application.LocalUserAppDataPath + @"\favorites.json"; private Dictionary _data; public FavoritesHelper() { LoadData(); } public Dictionary Data { get { return _data; } set { _data = value; } } public HashSet GetFavoriteFolders(string workspace = null) { if(workspace == null) { workspace = GetWorkspaceName(); } var favs = new HashSet(); if (!string.IsNullOrEmpty(workspace)) { var key = string.Format(FAVORITE_FOLDERS_KEY, workspace); var value = GetDataValue(key); if (value != null) { favs = JsonConvert.DeserializeObject>(value); } } return favs; } public void SaveFavorite(FavoriteFolderItem item, string workspace = null) { if (workspace == null) { workspace = GetWorkspaceName(); } if (!string.IsNullOrEmpty(workspace)) { var favs = GetFavoriteFolders(workspace); if (favs.Contains(item)) { favs.Remove(item); } favs.Add(item); SaveFavoriteFolders(favs, workspace); } } public void RemoveFavorite(FavoriteFolderItem item, string workspace = null) { if (workspace == null) { workspace = GetWorkspaceName(); } if (!string.IsNullOrEmpty(workspace)) { var favs = GetFavoriteFolders(workspace); if (favs.Contains(item)) { favs.Remove(item); } SaveFavoriteFolders(favs, workspace); } } public void SaveFavoriteFolders(HashSet favs, string workspace = null) { if (workspace == null) { workspace = GetWorkspaceName(); } if (!string.IsNullOrEmpty(workspace)) { var key = string.Format(FAVORITE_FOLDERS_KEY, workspace); var value = JsonConvert.SerializeObject(favs); SetDataValue(key, value); SaveData(); } } private HashSet _selected; public HashSet GetSelectedTags(string workspace = null) { if (_selected == null) { UpdateSelectedTags(); } return _selected; } private void UpdateSelectedTags() { _selected = new HashSet(); var favs = GetFavoriteTags(); foreach (var f in favs) { if (f.Selected) { _selected.Add(f.TagName); } } } public HashSet GetFavoriteTags(string workspace = null) { if (workspace == null) { workspace = GetWorkspaceName(); } var favs = new HashSet(); if (!string.IsNullOrEmpty(workspace)) { var key = string.Format(FAVORITE_TAGS_KEY, workspace); var value = GetDataValue(key); if (value != null) { favs = JsonConvert.DeserializeObject>(value); } } return favs; } public void SaveFavoriteTag(FavoriteTagItem tag, string workspace = null) { if (workspace == null) { workspace = GetWorkspaceName(); } if (!string.IsNullOrEmpty(workspace)) { var favs = GetFavoriteTags(workspace); if (favs.Contains(tag)) { favs.Remove(tag); } favs.Add(tag); SaveFavoriteTags(favs, workspace); UpdateSelectedTags(); } } public void RemoveFavoriteTag(FavoriteTagItem tag, string workspace = null) { if (workspace == null) { workspace = GetWorkspaceName(); } if (!string.IsNullOrEmpty(workspace)) { var favs = GetFavoriteTags(workspace); if (favs.Contains(tag)) { favs.Remove(tag); } SaveFavoriteTags(favs, workspace); UpdateSelectedTags(); } } public void SaveFavoriteTags(HashSet favs, string workspace = null) { if (workspace == null) { workspace = GetWorkspaceName(); } if (!string.IsNullOrEmpty(workspace)) { var key = string.Format(FAVORITE_TAGS_KEY, workspace); var value = JsonConvert.SerializeObject(favs); SetDataValue(key, value); SaveData(); } } public string GetDataValue(string key) { string value = null; _data.TryGetValue(key, out value); return value; } public void SetDataValue(string key, string value) { if (_data == null) { _data = new Dictionary(StringComparer.CurrentCultureIgnoreCase); } if (_data.ContainsKey(key)) { _data[key] = value; } else { _data.Add(key, value); } } public void SaveData() { // convert mappings to json string json = JsonConvert.SerializeObject(_data, Formatting.Indented); // write to file File.WriteAllText(_dataFileName, json); } public void LoadData() { if (_dataFileName != null && File.Exists(_dataFileName)) { // read json from file var json = File.ReadAllText(_dataFileName); // convert to dictionary _data = JsonConvert.DeserializeObject>(json); } else { _data = new Dictionary(StringComparer.CurrentCultureIgnoreCase); SaveData(); } } private string _workspace; private string GetWorkspaceName() { if (_workspace == null) { var helper = Utility.GetPerforceHelper(); if (helper.CurrentClient != null) { _workspace = helper.CurrentClient.Name; } } return _workspace; } } }