Save when exit

1. fix image cache
2. fix save when exit since destructor won't be called
3. fix #583 #582 (partially)  #581 (partially)  #580
This commit is contained in:
bao-qian 2016-05-02 22:37:01 +01:00
parent a7a34ac6b2
commit c6aff8424c
12 changed files with 65 additions and 14 deletions

View file

@ -13,7 +13,7 @@ using Control = System.Windows.Controls.Control;
namespace Wox.Plugin.CMD
{
public class CMD : IPlugin, ISettingProvider, IPluginI18n, IInstantQuery, IContextMenu
public class CMD : IPlugin, ISettingProvider, IPluginI18n, IContextMenu
{
private PluginInitContext context;
private bool WinRStroked;

View file

@ -13,7 +13,7 @@ using Wox.Plugin.Everything.Everything;
namespace Wox.Plugin.Everything
{
public class Main : IPlugin, IPluginI18n, IContextMenu
public class Main : IPlugin, IPluginI18n, IContextMenu, ISavable
{
private readonly EverythingAPI _api = new EverythingAPI();
private static readonly List<string> ImageExts = new List<string> { ".png", ".jpg", ".jpeg", ".gif", ".bmp", ".tiff", ".ico" };
@ -34,7 +34,7 @@ namespace Wox.Plugin.Everything
_settings = _storage.Load();
}
~Main()
public void Save()
{
_storage.Save();
}

View file

@ -9,7 +9,7 @@ using Wox.Infrastructure.Storage;
namespace Wox.Plugin.Folder
{
public class FolderPlugin : IPlugin, ISettingProvider, IPluginI18n
public class FolderPlugin : IPlugin, ISettingProvider, IPluginI18n, ISavable
{
private static List<string> driverNames;
private PluginInitContext context;
@ -23,7 +23,7 @@ namespace Wox.Plugin.Folder
_settings = _storage.Load();
}
~FolderPlugin()
public void Save()
{
_storage.Save();
}

View file

@ -14,7 +14,7 @@ using Stopwatch = Wox.Infrastructure.Stopwatch;
namespace Wox.Plugin.Program
{
public class Programs : ISettingProvider, IPlugin, IPluginI18n, IContextMenu
public class Programs : ISettingProvider, IPlugin, IPluginI18n, IContextMenu, ISavable
{
private static object lockObject = new object();
private static List<Program> _programs = new List<Program>();
@ -42,7 +42,7 @@ namespace Wox.Plugin.Program
_cache = _cacheStorage.Load();
}
~Programs()
public void Save()
{
_settingsStorage.Save();
_cacheStorage.Save();

View file

@ -9,7 +9,7 @@ using Wox.Plugin.WebSearch.SuggestionSources;
namespace Wox.Plugin.WebSearch
{
public class WebSearchPlugin : IPlugin, ISettingProvider, IPluginI18n, IInstantQuery, IMultipleActionKeywords
public class WebSearchPlugin : IPlugin, ISettingProvider, IPluginI18n, IMultipleActionKeywords, ISavable
{
public PluginInitContext Context { get; private set; }
@ -22,7 +22,7 @@ namespace Wox.Plugin.WebSearch
_settings = _storage.Load();
}
~WebSearchPlugin()
public void Save()
{
_storage.Save();
}

View file

@ -8,6 +8,7 @@ using Wox.Core.UserSettings;
using Wox.Infrastructure;
using Wox.Infrastructure.Exception;
using Wox.Infrastructure.Logger;
using Wox.Infrastructure.Storage;
using Wox.Plugin;
namespace Wox.Core.Plugin
@ -50,6 +51,15 @@ namespace Wox.Core.Plugin
ValidateUserDirectory();
}
public static void Save()
{
foreach (var plugin in AllPlugins)
{
var savable = plugin.Plugin as ISavable;
savable?.Save();
}
}
public static void InitializePlugins(IPublicAPI api)
{
var metadatas = PluginConfig.Parse(Directories);

View file

@ -9,6 +9,7 @@ using System.Windows;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Wox.Infrastructure.Logger;
using Wox.Infrastructure.Storage;
namespace Wox.Infrastructure.Image
@ -71,7 +72,7 @@ namespace Wox.Infrastructure.Image
public static void PreloadImages()
{
Stopwatch.Debug($"Preload {_cache.TopUsedImages.Count} images", () =>
Stopwatch.Debug("Preload images from cache", () =>
{
_cache.TopUsedImages.AsParallel().Where(i => !_imageSources.ContainsKey(i.Key)).ForAll(i =>
{
@ -87,6 +88,7 @@ namespace Wox.Infrastructure.Image
}
});
});
Log.Info($"Preload {_cache.TopUsedImages.Count} images from cache");
}
public static ImageSource Load(string path, bool addToCache = true)

View file

@ -0,0 +1,11 @@
namespace Wox.Infrastructure.Storage
{
/// <summary>
/// Save plugin settings/cache,
/// todo should be merged into a abstract class intead of seperate interface
/// </summary>
public interface ISavable
{
void Save();
}
}

View file

@ -78,6 +78,7 @@
<Compile Include="Image\ImageCache.cs" />
<Compile Include="Image\ImageLoader.cs" />
<Compile Include="Logger\Log.cs" />
<Compile Include="Storage\ISavable.cs" />
<Compile Include="Storage\PluginSettingsStorage.cs" />
<Compile Include="Storage\Storage.cs" />
<Compile Include="Stopwatch.cs" />

View file

@ -1,6 +1,8 @@
<Application x:Class="Wox.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Exit="OnExit"
SessionEnding="OnSessionEnding">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>

View file

@ -13,11 +13,12 @@ using Stopwatch = Wox.Infrastructure.Stopwatch;
namespace Wox
{
public partial class App : Application, ISingleInstanceApp
public partial class App : ISingleInstanceApp
{
private const string Unique = "Wox_Unique_Application_Mutex";
public static MainWindow Window { get; private set; }
public static PublicAPIInstance API { get; private set; }
private bool _saved;
[STAThread]
public static void Main()
@ -71,5 +72,29 @@ namespace Wox
CommandArgsFactory.Execute(args);
}
}
private void OnExit(object sender, ExitEventArgs e)
{
Save();
}
private void OnSessionEnding(object sender, SessionEndingCancelEventArgs e)
{
Save();
}
private void Save()
{
// if sessionending is called, exit proverbially be called when log off / shutdown
// but if sessionending is not called, exit won't be called when log off / shutdown
if (!_saved)
{
var vm = (MainViewModel) Window.DataContext;
vm.Save();
PluginManager.Save();
ImageLoader.Save();
_saved = true;
}
}
}
}

View file

@ -18,7 +18,7 @@ using Wox.Storage;
namespace Wox.ViewModel
{
public class MainViewModel : BaseViewModel
public class MainViewModel : BaseViewModel, ISavable
{
#region Private Fields
@ -81,7 +81,7 @@ namespace Wox.ViewModel
InitializeKeyCommands();
}
~MainViewModel()
public void Save()
{
_settingsStorage.Save();
_queryHistoryStorage.Save();