Skip to content

Commit

Permalink
Move websearch images to %APPDATA%
Browse files Browse the repository at this point in the history
  • Loading branch information
bao-qian committed May 8, 2016
1 parent ac1ed92 commit 68f8054
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 40 deletions.
35 changes: 8 additions & 27 deletions Plugins/Wox.Plugin.Everything/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows;
using Wox.Infrastructure;
using Wox.Infrastructure.Storage;
using Wox.Plugin.Everything.Everything;

Expand All @@ -15,9 +16,7 @@ public class Main : IPlugin, IPluginI18n, IContextMenu, ISavable
{
private readonly EverythingAPI _api = new EverythingAPI();

public const string SDK = "EverythingSDK";
public const string DLL = "Everything.dll";
internal static string SDKPath;

private PluginInitContext _context;

Expand Down Expand Up @@ -130,34 +129,16 @@ public void Init(PluginInitContext context)
_settings = _storage.Load();

var pluginDirectory = context.CurrentPluginMetadata.PluginDirectory;
var bundledSDKDirectory = Path.Combine(pluginDirectory, SDK, CpuType());
var bundledSDKPath = Path.Combine(bundledSDKDirectory, DLL);
const string sdk = "EverythingSDK";
var bundledSDKDirectory = Path.Combine(pluginDirectory, sdk, CpuType());
var sdkDirectory = Path.Combine(_storage.DirectoryPath, sdk, CpuType());
Helper.ValidateDataDirectory(bundledSDKDirectory, sdkDirectory);

var SDKDirectory = Path.Combine(_storage.DirectoryPath, SDK, CpuType());
SDKPath = Path.Combine(SDKDirectory, DLL);
if (!Directory.Exists(SDKDirectory))
{
Directory.CreateDirectory(SDKDirectory);
}

if (!File.Exists(SDKPath))
{
File.Copy(bundledSDKPath, SDKPath);
}
else
{
var newSDK = new FileInfo(bundledSDKPath).LastWriteTimeUtc;
var oldSDK = new FileInfo(SDKPath).LastWriteTimeUtc;
if (oldSDK != newSDK)
{
File.Copy(bundledSDKPath, SDKPath, true);
}
}

LoadLibrary(SDKPath);
var sdkPath = Path.Combine(sdkDirectory, DLL);
LoadLibrary(sdkPath);
}

private string CpuType()
private static string CpuType()
{
return Environment.Is64BitOperatingSystem ? "x64" : "x86";
}
Expand Down
22 changes: 18 additions & 4 deletions Plugins/Wox.Plugin.WebSearch/Main.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Controls;
using JetBrains.Annotations;
using Wox.Infrastructure;
using Wox.Infrastructure.Storage;
using Wox.Plugin.WebSearch.SuggestionSources;

Expand All @@ -20,8 +22,8 @@ public class Main : IPlugin, ISettingProvider, IPluginI18n, IMultipleActionKeywo
private CancellationTokenSource _updateSource;
private CancellationToken _updateToken;

public const string ImageDirectory = "Images";
public static string PluginDirectory;
public const string Images = "Images";
public static string ImagesDirectory;

public void Save()
{
Expand Down Expand Up @@ -86,7 +88,7 @@ private void UpdateResultsFromSuggestion(List<Result> results, string keyword, s
var task = Task.Run(() =>
{
results.AddRange(ResultsFromSuggestions(keyword, subtitle, webSearch));

}, _updateToken);

if (!task.Wait(waittime))
Expand Down Expand Up @@ -123,12 +125,24 @@ private IEnumerable<Result> ResultsFromSuggestions(string keyword, string subtit
return new List<Result>();
}

static Main()
{
var plugins = Infrastructure.Wox.Plugins;
var assemblyName = typeof(Main).Assembly.GetName().Name;
var pluginDirectory = Path.Combine(Infrastructure.Wox.SettingsPath, plugins, assemblyName);
ImagesDirectory = Path.Combine(pluginDirectory, Images);
}

public void Init(PluginInitContext context)
{
Context = context;
PluginDirectory = Context.CurrentPluginMetadata.PluginDirectory;

_storage = new PluginJsonStorage<Settings>();
_settings = _storage.Load();

var pluginDirectory = context.CurrentPluginMetadata.PluginDirectory;
var bundledImagesDirectory = Path.Combine(pluginDirectory, Images);
Helper.ValidateDataDirectory(bundledImagesDirectory, ImagesDirectory);
}

#region ISettingProvider Members
Expand Down
4 changes: 2 additions & 2 deletions Plugins/Wox.Plugin.WebSearch/WebSearch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public string Icon
set
{
_icon = value;
IconPath = Path.Combine(Main.PluginDirectory, Main.ImageDirectory, value);
IconPath = Path.Combine(Main.ImagesDirectory, value);
}
}

Expand All @@ -31,7 +31,7 @@ public string Icon
[JsonIgnore]
internal string IconPath { get; private set; } = Path.Combine
(
Main.PluginDirectory, Main.ImageDirectory, DefaultIcon
Main.ImagesDirectory, DefaultIcon
);

public string Url { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion Plugins/Wox.Plugin.WebSearch/WebSearchSetting.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ private void ConfirmButtonOnClick(object sender, RoutedEventArgs e)

private void SelectIconButtonOnClick(object sender, RoutedEventArgs e)
{
var directory = Path.Combine(Main.PluginDirectory, Main.ImageDirectory);
var directory = Path.Combine(Main.ImagesDirectory, Main.Images);
var dlg = new OpenFileDialog
{
InitialDirectory = directory,
Expand Down
1 change: 1 addition & 0 deletions Plugins/Wox.Plugin.WebSearch/Wox.Plugin.WebSearch.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
<Reference Include="PresentationCore" />
<Reference Include="PresentationFramework" />
<Reference Include="System" />
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="System.Core" />
<Reference Include="System.Xaml" />
<Reference Include="System.Xml.Linq" />
Expand Down
41 changes: 39 additions & 2 deletions Wox.Infrastructure/Helper.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
using System;
using System.IO;

namespace Wox.Infrastructure
{
static class Helper
public static class Helper
{
/// <summary>
/// http://www.yinwang.org/blog-cn/2015/11/21/programming-philosophy
/// </summary>
public static T RequireNonNull<T>(this T obj)
public static T NonNull<T>(this T obj)
{
if (obj == null)
{
Expand All @@ -18,5 +19,41 @@ public static T RequireNonNull<T>(this T obj)
return obj;
}
}

public static void RequireNonNull<T>(this T obj)
{
if (obj == null)
{
throw new NullReferenceException();
}
}

public static void ValidateDataDirectory(string bundledDataDirectory, string dataDirectory)
{

if (!Directory.Exists(dataDirectory))
{
Directory.CreateDirectory(dataDirectory);
}

foreach (var bundledDataPath in Directory.GetFiles(bundledDataDirectory))
{
var data = Path.GetFileName(bundledDataPath);
var dataPath = Path.Combine(dataDirectory, data.NonNull());
if (!File.Exists(dataPath))
{
File.Copy(bundledDataPath, dataPath);
}
else
{
var time1 = new FileInfo(bundledDataPath).LastWriteTimeUtc;
var time2 = new FileInfo(dataPath).LastWriteTimeUtc;
if (time1 != time2)
{
File.Copy(bundledDataPath, dataPath, true);
}
}
}
}
}
}
4 changes: 2 additions & 2 deletions Wox.Infrastructure/Logger/Log.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ static Log()
private static string CallerType()
{
var stackTrace = new StackTrace();
var stackFrames = stackTrace.GetFrames().RequireNonNull();
var stackFrames = stackTrace.GetFrames().NonNull();
var callingFrame = stackFrames[2];
var method = callingFrame.GetMethod();
var type = $"{method.DeclaringType.RequireNonNull().FullName}.{method.Name}";
var type = $"{method.DeclaringType.NonNull().FullName}.{method.Name}";
return type;
}
public static void Error(System.Exception e)
Expand Down
5 changes: 3 additions & 2 deletions Wox.Infrastructure/Storage/JsonStorage.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.IO;
using Newtonsoft.Json;
using Wox.Infrastructure;
using Wox.Infrastructure.Logger;

namespace Wox.Infrastructure.Storage
Expand All @@ -14,8 +15,8 @@ namespace Wox.Infrastructure.Storage
internal JsonStrorage()
{
FileSuffix = ".json";
DirectoryName = "Settings";
DirectoryPath = Path.Combine(DirectoryPath, DirectoryName);
DirectoryName = Wox.Settings;
DirectoryPath = Wox.SettingsPath;
FilePath = Path.Combine(DirectoryPath, FileName + FileSuffix);

ValidateDirectory();
Expand Down
3 changes: 3 additions & 0 deletions Wox.Infrastructure/Wox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ public static class Wox
{
public const string Name = "Wox";
public const string Plugins = "Plugins";
public const string Settings = "Settings";

public static readonly string ProgramPath = Directory.GetParent(Assembly.GetExecutingAssembly().Location).ToString();
public static readonly string DataPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), Name);
public static readonly string UserDirectory = Path.Combine(DataPath, Plugins);
public static readonly string PreinstalledDirectory = Path.Combine(ProgramPath, Plugins);
public static readonly string SettingsPath = Path.Combine(DataPath, Settings);
}
}

0 comments on commit 68f8054

Please sign in to comment.