Fix numerous web search plugin bugs
This commit is contained in:
bao-qian 2016-04-26 02:40:23 +01:00
parent fc2d7b41d8
commit 57c33fe693
11 changed files with 132 additions and 103 deletions

View File

@ -12,7 +12,7 @@ namespace Wox.Plugin.WebSearch
{
Title = "Google",
ActionKeyword = "g",
IconPath = "Images\\google.png",
IconPath = "google.png",
Url = "https://www.google.com/search?q={q}",
Enabled = true
},
@ -20,7 +20,7 @@ namespace Wox.Plugin.WebSearch
{
Title = "Wikipedia",
ActionKeyword = "wiki",
IconPath = "Images\\wiki.png",
IconPath = "wiki.png",
Url = "http://en.wikipedia.org/wiki/{q}",
Enabled = true
},
@ -28,7 +28,7 @@ namespace Wox.Plugin.WebSearch
{
Title = "FindIcon",
ActionKeyword = "findicon",
IconPath = "Images\\pictures.png",
IconPath = "pictures.png",
Url = "http://findicons.com/search/{q}",
Enabled = true
},
@ -36,7 +36,7 @@ namespace Wox.Plugin.WebSearch
{
Title = "Facebook",
ActionKeyword = "facebook",
IconPath = "Images\\facebook.png",
IconPath = "facebook.png",
Url = "http://www.facebook.com/search/?q={q}",
Enabled = true
},
@ -44,7 +44,7 @@ namespace Wox.Plugin.WebSearch
{
Title = "Twitter",
ActionKeyword = "twitter",
IconPath = "Images\\twitter.png",
IconPath = "twitter.png",
Url = "http://twitter.com/search?q={q}",
Enabled = true
},
@ -52,7 +52,7 @@ namespace Wox.Plugin.WebSearch
{
Title = "Google Maps",
ActionKeyword = "maps",
IconPath = "Images\\google_maps.png",
IconPath = "google_maps.png",
Url = "http://maps.google.com/maps?q={q}",
Enabled = true
},
@ -60,7 +60,7 @@ namespace Wox.Plugin.WebSearch
{
Title = "Google Translate",
ActionKeyword = "translate",
IconPath = "Images\\google_translate.png",
IconPath = "google_translate.png",
Url = "http://translate.google.com/#auto|en|{q}",
Enabled = true
},
@ -68,7 +68,7 @@ namespace Wox.Plugin.WebSearch
{
Title = "Duckduckgo",
ActionKeyword = "duckduckgo",
IconPath = "Images\\duckduckgo.png",
IconPath = "duckduckgo.png",
Url = "https://duckduckgo.com/?q={q}",
Enabled = true
},
@ -76,7 +76,7 @@ namespace Wox.Plugin.WebSearch
{
Title = "Github",
ActionKeyword = "github",
IconPath = "Images\\github.png",
IconPath = "github.png",
Url = "https://github.com/search?q={q}",
Enabled = true
},
@ -84,7 +84,7 @@ namespace Wox.Plugin.WebSearch
{
Title = "Github Gist",
ActionKeyword = "gist",
IconPath = "Images\\gist.png",
IconPath = "gist.png",
Url = "https://gist.github.com/search?q={q}",
Enabled = true
},
@ -92,7 +92,7 @@ namespace Wox.Plugin.WebSearch
{
Title = "Gmail",
ActionKeyword = "gmail",
IconPath = "Images\\gmail.png",
IconPath = "gmail.png",
Url = "https://mail.google.com/mail/ca/u/0/#apps/{q}",
Enabled = true
},
@ -100,7 +100,7 @@ namespace Wox.Plugin.WebSearch
{
Title = "Google Drive",
ActionKeyword = "drive",
IconPath = "Images\\google_drive.png",
IconPath = "google_drive.png",
Url = "http://drive.google.com/?hl=en&tab=bo#search/{q}",
Enabled = true
},
@ -108,7 +108,7 @@ namespace Wox.Plugin.WebSearch
{
Title = "Wolframalpha",
ActionKeyword = "wolframalpha",
IconPath = "Images\\wolframalpha.png",
IconPath = "wolframalpha.png",
Url = "http://www.wolframalpha.com/input/?i={q}",
Enabled = true
},
@ -116,7 +116,7 @@ namespace Wox.Plugin.WebSearch
{
Title = "Stackoverflow",
ActionKeyword = "stackoverflow",
IconPath = "Images\\stackoverflow.png",
IconPath = "stackoverflow.png",
Url = "http://stackoverflow.com/search?q={q}",
Enabled = true
},
@ -124,7 +124,7 @@ namespace Wox.Plugin.WebSearch
{
Title = "I'm Feeling Lucky",
ActionKeyword = "lucky",
IconPath = "Images\\google.png",
IconPath = "google.png",
Url = "http://google.com/search?q={q}&btnI=I",
Enabled = true
},
@ -132,7 +132,7 @@ namespace Wox.Plugin.WebSearch
{
Title = "Google Image",
ActionKeyword = "image",
IconPath = "Images\\google.png",
IconPath = "google.png",
Url = "https://www.google.com/search?q={q}&tbm=isch",
Enabled = true
},
@ -140,7 +140,7 @@ namespace Wox.Plugin.WebSearch
{
Title = "Youtube",
ActionKeyword = "youtube",
IconPath = "Images\\youtube.png",
IconPath = "youtube.png",
Url = "http://www.youtube.com/results?search_query={q}",
Enabled = true
},
@ -148,7 +148,7 @@ namespace Wox.Plugin.WebSearch
{
Title = "Bing",
ActionKeyword = "bing",
IconPath = "Images\\bing.png",
IconPath = "bing.png",
Url = "https://www.bing.com/search?q={q}",
Enabled = true
},
@ -156,7 +156,7 @@ namespace Wox.Plugin.WebSearch
{
Title = "Yahoo",
ActionKeyword = "yahoo",
IconPath = "Images\\yahoo.png",
IconPath = "yahoo.png",
Url = "http://www.search.yahoo.com/search?p={q}",
Enabled = true
}

View File

@ -18,27 +18,25 @@
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBlock Margin="10" FontSize="14" Grid.Row="0" Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Right" Text="{DynamicResource wox_plugin_websearch_title}"></TextBlock>
<TextBox x:Name="tbTitle" Margin="10" Grid.Row="0" Width="400" Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Left"></TextBox>
<TextBox x:Name="WebSearchName" Margin="10" Grid.Row="0" Width="400" Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Left"></TextBox>
<TextBlock Margin="10" FontSize="14" Grid.Row="1" Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Right" Text="{DynamicResource wox_plugin_websearch_url}"></TextBlock>
<TextBox x:Name="tbUrl" Margin="10" Grid.Row="1" Width="400" Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Left"></TextBox>
<TextBox x:Name="Url" Margin="10" Grid.Row="1" Width="400" Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Left"></TextBox>
<TextBlock Margin="10" FontSize="14" Grid.Row="2" Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Right" Text="{DynamicResource wox_plugin_websearch_action_keyword}"></TextBlock>
<TextBox x:Name="tbActionword" Margin="10" Grid.Row="2" Width="400" Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Left"></TextBox>
<TextBox x:Name="Actionword" Margin="10" Grid.Row="2" Width="400" Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Left"></TextBox>
<TextBlock Margin="10" FontSize="14" Grid.Row="3" Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Right" Text="{DynamicResource wox_plugin_websearch_enable}"></TextBlock>
<CheckBox x:Name="cbEnable" IsChecked="True" Margin="10" Grid.Row="3" Grid.Column="1" VerticalAlignment="Center"></CheckBox>
<CheckBox x:Name="EnableCheckBox" IsChecked="True" Margin="10" Grid.Row="3" Grid.Column="1" VerticalAlignment="Center"></CheckBox>
<TextBlock Margin="10" FontSize="14" Grid.Row="4" Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Right" Text="{DynamicResource wox_plugin_websearch_icon}"></TextBlock>
<StackPanel Orientation="Horizontal" Grid.Row="4" Grid.Column="1" Margin="10">
<Image x:Name="imgIcon" Width="24" Height="24" Margin="0 0 10 0" />
<Button x:Name="btnSelectIcon" Height="24" Click="BtnSelectIcon_OnClick" Content="{DynamicResource wox_plugin_websearch_select_icon}"></Button>
<TextBlock x:Name="tbIconPath" Visibility="Hidden"></TextBlock>
<Image x:Name="WebSearchIcon" Width="24" Height="24" Margin="0 0 10 0" />
<Button x:Name="SelectIconButton" Height="24" Click="SelectIconButtonOnClick" Content="{DynamicResource wox_plugin_websearch_select_icon}"></Button>
</StackPanel>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Grid.Row="5" Grid.Column="1">
<Button x:Name="btnCancel" Click="BtnCancel_OnClick" Margin="10 0 10 0" Width="80" Height="25" Content="{DynamicResource wox_plugin_websearch_cancel}"></Button>
<Button x:Name="btnConfirm" Margin="10 0 10 0" Width="80" Height="25" Click="btnConfirm_OnClick">
<TextBlock x:Name="lblAdd" Text="{DynamicResource wox_plugin_websearch_add}"></TextBlock></Button>
<Button x:Name="CancelButton" Click="CancelButtonOnClick" Margin="10 0 10 0" Width="80" Height="25" Content="{DynamicResource wox_plugin_websearch_cancel}"/>
<Button x:Name="ConfirmButton" Margin="10 0 10 0" Width="80" Height="25" Click="ConfirmButtonOnClick" Content="{DynamicResource wox_plugin_websearch_add}"/>
</StackPanel>
</Grid>
</Window>

View File

@ -2,37 +2,41 @@
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Microsoft.Win32;
using Wox.Infrastructure;
using Wox.Infrastructure.Exception;
using Wox.Infrastructure.Image;
namespace Wox.Plugin.WebSearch
{
public partial class WebSearchSetting : Window
{
private const string _imageDirectoryName = "Images";
private string _pluginDirectory = WoxDirectroy.Executable;
private const string ImageDirectory = "Images";
private const string DefaultIcon = "web_search.png";
private readonly string _pluginDirectory;
private readonly WebSearchesSetting _settingWindow;
private bool _isUpdate;
private WebSearch _updateWebSearch;
private WebSearch _webSearch;
private readonly PluginInitContext _context;
private readonly WebSearchPlugin _plugin;
private Settings _settings;
private readonly Settings _settings;
public WebSearchSetting(WebSearchesSetting settingWidow, Settings settings)
{
InitializeComponent();
_plugin = settingWidow.Plugin;
_context = settingWidow.Context;
_settingWindow = settingWidow;
InitializeComponent();
_settings = settings;
_pluginDirectory = _settingWindow.Context.CurrentPluginMetadata.PluginDirectory;
}
public void UpdateItem(WebSearch webSearch)
{
_updateWebSearch = _settings.WebSearches.FirstOrDefault(o => o == webSearch);
if (_updateWebSearch == null || string.IsNullOrEmpty(_updateWebSearch.Url))
_webSearch = _settings.WebSearches.FirstOrDefault(o => o == webSearch);
if (_webSearch == null || string.IsNullOrEmpty(_webSearch.Url))
{
string warning = _context.API.GetTranslation("wox_plugin_websearch_invalid_web_search");
@ -42,31 +46,61 @@ namespace Wox.Plugin.WebSearch
}
_isUpdate = true;
lblAdd.Text = "Update";
tbIconPath.Text = webSearch.IconPath;
ShowIcon(webSearch.IconPath);
cbEnable.IsChecked = webSearch.Enabled;
tbTitle.Text = webSearch.Title;
tbUrl.Text = webSearch.Url;
tbActionword.Text = webSearch.ActionKeyword;
ConfirmButton.Content = "Update";
WebSearchIcon.Source = LoadIcon(webSearch.IconPath);
EnableCheckBox.IsChecked = webSearch.Enabled;
WebSearchName.Text = webSearch.Title;
Url.Text = webSearch.Url;
Actionword.Text = webSearch.ActionKeyword;
}
private void ShowIcon(string path)
public void AddItem(WebSearch websearch)
{
imgIcon.Source = new BitmapImage(new Uri(Path.Combine(_pluginDirectory, path), UriKind.Absolute));
_webSearch = websearch;
if (string.IsNullOrEmpty(_webSearch.IconPath))
{
_webSearch.IconPath = DefaultIcon;
WebSearchIcon.Source = LoadIcon(_webSearch.IconPath);
}
}
private void BtnCancel_OnClick(object sender, RoutedEventArgs e)
private void CancelButtonOnClick(object sender, RoutedEventArgs e)
{
Close();
}
private ImageSource LoadIcon(string path)
{
if (path != null)
{
var releativePath = Path.Combine(_pluginDirectory, ImageDirectory, Path.GetFileName(path));
if (File.Exists(releativePath))
{
_webSearch.IconPath = path;
var source = ImageLoader.Load(releativePath);
return source;
}
else
{
_webSearch.IconPath = path;
var source = ImageLoader.Load(path);
return source;
}
}
else
{
var source = ImageLoader.Load(Path.Combine(_pluginDirectory, ImageDirectory, DefaultIcon));
return source;
}
}
/// <summary>
/// Confirm button for both add and update
/// </summary>
private void btnConfirm_OnClick(object sender, RoutedEventArgs e)
private void ConfirmButtonOnClick(object sender, RoutedEventArgs e)
{
string title = tbTitle.Text;
string title = WebSearchName.Text;
if (string.IsNullOrEmpty(title))
{
string warning = _context.API.GetTranslation("wox_plugin_websearch_input_title");
@ -74,7 +108,7 @@ namespace Wox.Plugin.WebSearch
return;
}
string url = tbUrl.Text;
string url = Url.Text;
if (string.IsNullOrEmpty(url))
{
string warning = _context.API.GetTranslation("wox_plugin_websearch_input_url");
@ -82,24 +116,19 @@ namespace Wox.Plugin.WebSearch
return;
}
string newActionKeyword = tbActionword.Text.Trim();
string newActionKeyword = Actionword.Text.Trim();
if (_isUpdate)
{
try
{
_plugin.NotifyActionKeywordsUpdated(_updateWebSearch.ActionKeyword, newActionKeyword);
_plugin.NotifyActionKeywordsUpdated(_webSearch.ActionKeyword, newActionKeyword);
}
catch (WoxPluginException exception)
{
MessageBox.Show(exception.Message);
return;
}
_updateWebSearch.ActionKeyword = newActionKeyword;
_updateWebSearch.IconPath = tbIconPath.Text;
_updateWebSearch.Enabled = cbEnable.IsChecked ?? false;
_updateWebSearch.Url = url;
_updateWebSearch.Title = title;
}
else
{
@ -112,42 +141,34 @@ namespace Wox.Plugin.WebSearch
MessageBox.Show(exception.Message);
return;
}
_settings.WebSearches.Add(new WebSearch
{
ActionKeyword = newActionKeyword,
Enabled = cbEnable.IsChecked ?? false,
IconPath = tbIconPath.Text,
Url = url,
Title = title
});
_settings.WebSearches.Add(_webSearch);
}
_webSearch.ActionKeyword = newActionKeyword;
_webSearch.Enabled = EnableCheckBox.IsChecked ?? false;
_webSearch.Url = url;
_webSearch.Title = title;
_settingWindow.ReloadWebSearchView();
Close();
}
private void BtnSelectIcon_OnClick(object sender, RoutedEventArgs e)
private void SelectIconButtonOnClick(object sender, RoutedEventArgs e)
{
if (!Directory.Exists(_pluginDirectory))
{
_pluginDirectory =
Path.GetDirectoryName(WoxDirectroy.Executable);
}
var dlg = new OpenFileDialog
{
InitialDirectory = Path.Combine(_pluginDirectory, _imageDirectoryName),
InitialDirectory = Path.Combine(_pluginDirectory, ImageDirectory),
Filter = "Image files (*.jpg, *.jpeg, *.gif, *.png, *.bmp) |*.jpg; *.jpeg; *.gif; *.png; *.bmp"
};
bool? result = dlg.ShowDialog();
if (result == true)
if (result != null && result == true)
{
string filename = dlg.FileName;
if (filename != null)
string fullpath = dlg.FileName;
if (fullpath != null)
{
tbIconPath.Text = Path.Combine(_imageDirectoryName, Path.GetFileName(filename));
ShowIcon(tbIconPath.Text);
WebSearchIcon.Source = LoadIcon(fullpath);
}
}
}

View File

@ -53,8 +53,12 @@ namespace Wox.Plugin.WebSearch
private void btnAddWebSearch_OnClick(object sender, RoutedEventArgs e)
{
WebSearchSetting webSearch = new WebSearchSetting(this, _settings);
webSearch.ShowDialog();
var setting = new WebSearchSetting(this, _settings);
var webSearch = new WebSearch();
setting.AddItem(webSearch);
setting.ShowDialog();
}
private void btnDeleteWebSearch_OnClick(object sender, RoutedEventArgs e)

View File

@ -115,8 +115,8 @@ namespace Wox.Core.Updater
}
catch (Exception e)
{
Log.Error(e);
updManager.CleanUp();
Log.Error(e);
return;
}

View File

@ -13,11 +13,11 @@ using Wox.Infrastructure.Storage;
namespace Wox.Infrastructure.Image
{
public class ImageLoader
public static class ImageLoader
{
private readonly ConcurrentDictionary<string, ImageSource> _imageSources = new ConcurrentDictionary<string, ImageSource>();
private static readonly ConcurrentDictionary<string, ImageSource> _imageSources = new ConcurrentDictionary<string, ImageSource>();
private readonly List<string> ImageExts = new List<string>
private static readonly List<string> ImageExts = new List<string>
{
".png",
".jpg",
@ -28,7 +28,7 @@ namespace Wox.Infrastructure.Image
".ico"
};
private readonly List<string> SelfExts = new List<string>
private static readonly List<string> SelfExts = new List<string>
{
".exe",
".lnk",
@ -38,21 +38,21 @@ namespace Wox.Infrastructure.Image
".appref-ms"
};
private readonly ImageCache _cache;
private readonly BinaryStorage<ImageCache> _storage;
private static readonly ImageCache _cache;
private static readonly BinaryStorage<ImageCache> _storage;
public ImageLoader()
static ImageLoader()
{
_storage = new BinaryStorage<ImageCache>();
_cache = _storage.Load();
}
~ImageLoader()
public static void Save()
{
_storage.Save();
}
private ImageSource GetIcon(string fileName)
private static ImageSource GetIcon(string fileName)
{
try
{
@ -69,7 +69,7 @@ namespace Wox.Infrastructure.Image
return null;
}
public void PreloadImages()
public static void PreloadImages()
{
Stopwatch.Debug($"Preload {_cache.TopUsedImages.Count} images", () =>
{
@ -89,10 +89,15 @@ namespace Wox.Infrastructure.Image
});
}
public ImageSource Load(string path, bool addToCache = true)
public static ImageSource Load(string path, bool addToCache = true)
{
if (string.IsNullOrEmpty(path)) return null;
ImageSource image = null;
if (string.IsNullOrEmpty(path))
{
path = Path.Combine(WoxDirectroy.Executable, "Images", "app.png");
image = new BitmapImage(new Uri(path));
return image;
}
Stopwatch.Debug($"Loading image path: {path}", () =>
{
@ -152,7 +157,7 @@ namespace Wox.Infrastructure.Image
}
// http://blogs.msdn.com/b/oldnewthing/archive/2011/01/27/10120844.aspx
private Icon GetFileIcon(string name)
private static Icon GetFileIcon(string name)
{
SHFILEINFO shfi = new SHFILEINFO();
uint flags = SHGFI_SYSICONINDEX;

View File

@ -73,13 +73,13 @@ namespace Wox.Infrastructure.Storage
}
catch (SerializationException e)
{
Log.Error(e);
LoadDefault();
Log.Error(e);
}
catch (InvalidCastException e)
{
Log.Error(e);
LoadDefault();
Log.Error(e);
}
finally
{

View File

@ -21,7 +21,6 @@ namespace Wox
{
private const string Unique = "Wox_Unique_Application_Mutex";
public static MainWindow Window { get; private set; }
public static ImageLoader ImageLoader;
public static PublicAPIInstance API { get; private set; }
[STAThread]
@ -44,7 +43,6 @@ namespace Wox
WoxDirectroy.Executable = Directory.GetParent(Assembly.GetExecutingAssembly().Location).ToString();
RegisterUnhandledException();
ImageLoader = new ImageLoader();
Task.Factory.StartNew(ImageLoader.PreloadImages);
PluginManager.Initialize();

View File

@ -2,6 +2,7 @@ using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;
using Wox.Infrastructure.Image;
namespace Wox.Converters
{
@ -13,7 +14,7 @@ namespace Wox.Converters
{
return null;
}
var image = App.ImageLoader.Load(value.ToString());
var image = ImageLoader.Load(value.ToString());
return image;
}

View File

@ -7,6 +7,7 @@ using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using Wox.Helper;
using Wox.Infrastructure;
using Wox.Infrastructure.Image;
namespace Wox
{
@ -37,7 +38,7 @@ namespace Wox
Storyboard.SetTargetProperty(fadeOutAnimation, new PropertyPath(TopProperty));
fadeOutStoryboard.Children.Add(fadeOutAnimation);
imgClose.Source = App.ImageLoader.Load(Path.Combine(WoxDirectroy.Executable, "Images\\close.png"));
imgClose.Source = ImageLoader.Load(Path.Combine(WoxDirectroy.Executable, "Images\\close.png"));
imgClose.MouseUp += imgClose_MouseUp;
}
@ -65,10 +66,10 @@ namespace Wox
}
if (!File.Exists(iconPath))
{
imgIco.Source = App.ImageLoader.Load(Path.Combine(WoxDirectroy.Executable, "Images\\app.png"));
imgIco.Source = ImageLoader.Load(Path.Combine(WoxDirectroy.Executable, "Images\\app.png"));
}
else {
imgIco.Source = App.ImageLoader.Load(iconPath);
imgIco.Source = ImageLoader.Load(iconPath);
}
Show();

View File

@ -19,6 +19,7 @@ using Wox.Core.Updater;
using Wox.Core.UserSettings;
using Wox.Helper;
using Wox.Infrastructure.Hotkey;
using Wox.Infrastructure.Image;
using Wox.Plugin;
using Wox.ViewModel;
using Application = System.Windows.Forms.Application;
@ -550,7 +551,7 @@ namespace Wox
pluginAuthor.Text = InternationalizationManager.Instance.GetTranslation("author") + ": " + pair.Metadata.Author;
pluginSubTitle.Text = pair.Metadata.Description;
pluginId = pair.Metadata.ID;
pluginIcon.Source = App.ImageLoader.Load(pair.Metadata.FullIcoPath);
pluginIcon.Source = ImageLoader.Load(pair.Metadata.FullIcoPath);
var customizedPluginConfig = _settings.PluginSettings[pluginId];
cbDisablePlugin.IsChecked = customizedPluginConfig != null && customizedPluginConfig.Disabled;