public SchemeCache(StudioStylesService studioStylesServiceService) { StudioStylesService = studioStylesServiceService; IsCacheValid = false; IsCacheRefreshing = false; IsImageCacheRefreshing = false; CurrentSchemeNumber = 0; Schemes = new ObservableCollection <Scheme>(); SchemesDataPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), Path.Combine("Twainsoft", "StudioStyler")); if (!Directory.Exists(SchemesDataPath)) { Directory.CreateDirectory(SchemesDataPath); } schemesCacheFile = "SchemesCache.xml"; SchemesPreviewPath = Path.Combine(SchemesDataPath, "Previews"); if (!Directory.Exists(SchemesPreviewPath)) { Directory.CreateDirectory(SchemesPreviewPath); } }
public async Task Refresh() { IsCacheRefreshing = true; var schemes = await StudioStylesService.AllAsync(); Schemes.Clear(); foreach (var scheme in schemes) { Schemes.Add(scheme); } LastRefresh = DateTime.Now; SerializeCachedSchemes(false); IsCacheRefreshing = false; IsImageCacheRefreshing = true; await CheckSchemes(false); SerializeCachedSchemes(true); IsCacheValid = true; IsImageCacheRefreshing = false; }
private async void DownloadScheme() { var scheme = PagedSchemesView.CurrentItem as Scheme; if (scheme != null) { var file = await StudioStylesService.DownloadAsync(scheme.DownloadPath); if (SettingsActivator.LoadScheme(file)) { SchemesHistory.Add(scheme); StatusBarHelper.Update(string.Format("The Style '{0}' was activated successfully!", scheme.Title)); } } }
public HistoryModel(SchemeHistory schemesHistory, OptionsStore optionsStore, StudioStylesService studioStylesService, SettingsActivator settingsActivator) { SchemesHistory = schemesHistory; OptionsStore = optionsStore; StudioStylesService = studioStylesService; SettingsActivator = settingsActivator; var stylesPerPage = optionsStore.StylesPerPage; PagedHistoryView = new PagedCollectionView(SchemesHistory.History) { PageSize = stylesPerPage }; PagedHistoryView.SortDescriptions.Add(new SortDescription("Activations", ListSortDirection.Descending)); CurrentSearchString = ""; SearchValues = new List <string>(); }
protected override void Initialize() { base.Initialize(); var mcs = GetService(typeof(IMenuCommandService)) as OleMenuCommandService; if (null != mcs) { // The ToolWindow menu command. var toolWindowCommandId = new CommandID(GuidList.guidTwainsoft_StudioStyler_VSPackageCmdSet, CommandIds.ShowStudioStylesId); var menuToolWin = new MenuCommand(OnShowToolWindow, toolWindowCommandId); mcs.AddCommand(menuToolWin); // The search combobox command. var searchStringCommandId = new CommandID(GuidList.GuidSchemesToolbarCmdSet, CommandIds.SearchStringComboId); var searchStringCommand = new OleMenuCommand(OnSearchString, searchStringCommandId); mcs.AddCommand(searchStringCommand); // The search combobox value list command. var searchStringValuesCommandId = new CommandID(GuidList.GuidSchemesToolbarCmdSet, CommandIds.SearchStringValuesId); var searchSTringValuesCommand = new OleMenuCommand(OnSearchStringValues, searchStringValuesCommandId); mcs.AddCommand(searchSTringValuesCommand); // The clear search command. var clearSearchCommandId = new CommandID(GuidList.GuidSchemesToolbarCmdSet, CommandIds.ClearSearch); var clearSearchCommand = new OleMenuCommand(OnClearSearch, clearSearchCommandId); mcs.AddCommand(clearSearchCommand); // The refresh schemes cache command. var refreshSchemesCacheCommandId = new CommandID(GuidList.GuidSchemesToolbarCmdSet, CommandIds.RefreshSchemesCache); var refreshSchemesCacheCommand = new OleMenuCommand(OnRefreshSchemesCache, refreshSchemesCacheCommandId); mcs.AddCommand(refreshSchemesCacheCommand); // The activate scheme command. var activateSchemeCommandId = new CommandID(GuidList.GuidSchemesToolbarCmdSet, CommandIds.ActivateScheme); var activateSchemeCommand = new OleMenuCommand(OnActivateScheme, activateSchemeCommandId); mcs.AddCommand(activateSchemeCommand); // The history command. var historyCommandId = new CommandID(GuidList.GuidSchemesToolbarCmdSet, CommandIds.SchemesHistory); var historyCommand = new OleMenuCommand(OnHistory, historyCommandId); historyCommand.BeforeQueryStatus += HistoryCommandOnBeforeQueryStatus; mcs.AddCommand(historyCommand); // The first page command. var firstPageCommandId = new CommandID(GuidList.GuidSchemesToolbarCmdSet, CommandIds.FirstPageNavigation); var firstPageCommand = new OleMenuCommand(OnFirstPage, firstPageCommandId); mcs.AddCommand(firstPageCommand); // The previous page command. var previousPageCommandId = new CommandID(GuidList.GuidSchemesToolbarCmdSet, CommandIds.PreviousPageNavigation); var previousPageCommand = new OleMenuCommand(OnPreviousPage, previousPageCommandId); mcs.AddCommand(previousPageCommand); // The next page command. var nextPageCommandId = new CommandID(GuidList.GuidSchemesToolbarCmdSet, CommandIds.NextPageNavigation); var nextPageCommand = new OleMenuCommand(OnNextPage, nextPageCommandId); mcs.AddCommand(nextPageCommand); // The last page command. var lastPageCommandId = new CommandID(GuidList.GuidSchemesToolbarCmdSet, CommandIds.LastPageNavigation); var lastPageCommand = new OleMenuCommand(OnLastPage, lastPageCommandId); mcs.AddCommand(lastPageCommand); } // The general options for this package. var optionsStore = GetDialogPage(typeof(OptionsStore)) as OptionsStore; // Some services that provides important functionalities. StudioStylesService = new StudioStylesService(); SettingsActivator = new SettingsActivator(); // The cache holds all studio styles (called schemes) that are available. SchemeCache = new SchemeCache(StudioStylesService); SchemeHistory = new SchemeHistory(SchemeCache); // Instantiate the models. They are the main classes to interact with the ui and the data. SchemeModel = new SchemeModel(SchemeCache, SchemeHistory, optionsStore, StudioStylesService, SettingsActivator); HistoryModel = new HistoryModel(SchemeHistory, optionsStore, StudioStylesService, SettingsActivator); // Instantiate the views. This are the WPF controls that visualize the styles and the history. SchemeView = new SchemeView(SchemeModel); HistoryView = new HistoryView(HistoryModel); // Some Visual Studio visuals can fire some events so that the model is need very early. Model = SchemeModel; // Save those objects. We need them more than once! Window = FindToolWindow(typeof(SchemeToolWindow), 0, true); if (Window == null || Window.Frame == null) { throw new NotSupportedException(Resources.Resources.CanNotCreateWindow); } var schemesOverview = Window as SchemeToolWindow; if (schemesOverview == null) { throw new InvalidOperationException("Cannot find the SchemesOverviewWindow!"); } SchemeToolWindowView = schemesOverview.Content as SchemeToolWindowView; if (SchemeToolWindowView == null) { throw new InvalidOperationException("Cannot find the StudioStylesView!"); } SchemeToolWindowView.Dock.Children.Add(SchemeView); // Check both model caches (Styles and History) so that saved data is present. SchemeModel.CheckCache(); HistoryModel.CheckCache(); }
private async Task CheckSchemes(bool imagesCompletelyLoaded) { CurrentSchemeNumber = 0; foreach (var scheme in Schemes) { try { if (++CurrentSchemeNumber % 50 == 0) { SerializeCachedSchemes(false); } var file = Path.Combine(SchemesPreviewPath, TransformTitle(scheme.Title) + ".png"); var image = new BitmapImage(); image.BeginInit(); if (File.Exists(file)) { image.StreamSource = new FileStream(file, FileMode.Open, FileAccess.Read); image.EndInit(); scheme.Preview = image; scheme.ImagePresent = true; scheme.ImageDownloadTried = true; } else if (!imagesCompletelyLoaded && !scheme.ImageDownloadTried && !scheme.ImagePresent) { var png = await StudioStylesService.Preview(scheme.Title); image.StreamSource = new MemoryStream(png); image.EndInit(); scheme.Preview = image; using (var fileStream = new FileStream(file, FileMode.Create)) { var encoder = new PngBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(image)); encoder.Save(fileStream); } scheme.ImagePresent = true; scheme.ImageDownloadTried = true; } else if (scheme.ImagePresent) { image.StreamSource = new FileStream(file, FileMode.Open, FileAccess.Read); image.EndInit(); scheme.Preview = image; scheme.ImagePresent = true; scheme.ImageDownloadTried = true; } } catch (InvalidOperationException e) { Console.WriteLine(e); scheme.ImagePresent = false; scheme.ImageDownloadTried = true; } catch (NotSupportedException e) { Console.WriteLine(e); scheme.ImagePresent = false; scheme.ImageDownloadTried = true; } catch (Exception e) { Console.WriteLine(e); scheme.ImagePresent = false; scheme.ImageDownloadTried = true; } } }