BUG-1908: Made Greenshot recheck & add the external commands like Ms-Paint and Paint.NET if they are not deleted manually and not yet available. Also cleaned up some code.

This commit is contained in:
Robin 2016-09-16 21:48:57 +02:00
commit 6efc7796b8
49 changed files with 322 additions and 294 deletions

View file

@ -52,28 +52,85 @@ namespace ExternalCommand {
public bool UriToClipboard { get; set; }
[IniProperty("Commandline", Description="The commandline for the output command.")]
public Dictionary<string, string> commandlines { get; set; }
public Dictionary<string, string> Commandline { get; set; }
[IniProperty("Argument", Description="The arguments for the output command.")]
public Dictionary<string, string> arguments { get; set; }
public Dictionary<string, string> Argument { get; set; }
[IniProperty("RunInbackground", Description = "Should the command be started in the background.")]
public Dictionary<string, bool> runInbackground { get; set; }
public Dictionary<string, bool> RunInbackground { get; set; }
private const string MSPAINT = "MS Paint";
private static readonly string paintPath;
private static readonly bool hasPaint = false;
[IniProperty("DeletedBuildInCommands", Description = "If a build in command was deleted manually, it should not be recreated.")]
public List<string> DeletedBuildInCommands { get; set; }
private const string PAINTDOTNET = "Paint.NET";
private static readonly string paintDotNetPath;
private static readonly bool hasPaintDotNet = false;
private const string MsPaint = "MS Paint";
private static readonly string PaintPath;
private static readonly bool HasPaint;
private const string PaintDotNet = "Paint.NET";
private static readonly string PaintDotNetPath;
private static readonly bool HasPaintDotNet;
static ExternalCommandConfiguration() {
try {
paintPath = PluginUtils.GetExePath("pbrush.exe");
hasPaint = !string.IsNullOrEmpty(paintPath) && File.Exists(paintPath);
paintDotNetPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), @"Paint.NET\PaintDotNet.exe");
hasPaintDotNet = !string.IsNullOrEmpty(paintDotNetPath) && File.Exists(paintDotNetPath);
PaintPath = PluginUtils.GetExePath("pbrush.exe");
HasPaint = !string.IsNullOrEmpty(PaintPath) && File.Exists(PaintPath);
} catch {
// Ignore
}
try
{
PaintDotNetPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), @"Paint.NET\PaintDotNet.exe");
HasPaintDotNet = !string.IsNullOrEmpty(PaintDotNetPath) && File.Exists(PaintDotNetPath);
}
catch
{
// Ignore
}
}
/// <summary>
/// Delete the configuration for the specified command
/// </summary>
/// <param name="command">string with command</param>
public void Delete(string command)
{
if (string.IsNullOrEmpty(command))
{
return;
}
Commands.Remove(command);
Commandline.Remove(command);
Argument.Remove(command);
RunInbackground.Remove(command);
if (MsPaint.Equals(command) || PaintDotNet.Equals(command))
{
if (!DeletedBuildInCommands.Contains(command))
{
DeletedBuildInCommands.Add(command);
}
}
}
public override void AfterLoad()
{
base.AfterLoad();
// Check if we need to add MsPaint
if (HasPaint && !Commands.Contains(MsPaint) && !DeletedBuildInCommands.Contains(MsPaint))
{
Commands.Add(MsPaint);
Commandline.Add(MsPaint, PaintPath);
Argument.Add(MsPaint, "\"{0}\"");
RunInbackground.Add(MsPaint, true);
}
// Check if we need to add Paint.NET
if (HasPaintDotNet && !Commands.Contains(PaintDotNet) && !DeletedBuildInCommands.Contains(PaintDotNet))
{
Commands.Add(PaintDotNet);
Commandline.Add(PaintDotNet, PaintDotNetPath);
Argument.Add(PaintDotNet, "\"{0}\"");
RunInbackground.Add(PaintDotNet, true);
}
}
@ -84,42 +141,16 @@ namespace ExternalCommand {
/// <returns>object with the default value for the supplied property</returns>
public override object GetDefault(string property) {
switch(property) {
case "Commands":
List<string> commandDefaults = new List<string>();
if (hasPaintDotNet) {
commandDefaults.Add(PAINTDOTNET);
}
if (hasPaint) {
commandDefaults.Add(MSPAINT);
}
return commandDefaults;
case "Commandline":
Dictionary<string, string> commandlineDefaults = new Dictionary<string, string>();
if (hasPaintDotNet) {
commandlineDefaults.Add(PAINTDOTNET, paintDotNetPath);
}
if (hasPaint) {
commandlineDefaults.Add(MSPAINT, paintPath);
}
return commandlineDefaults;
case "Argument":
Dictionary<string, string> argumentDefaults = new Dictionary<string, string>();
if (hasPaintDotNet) {
argumentDefaults.Add(PAINTDOTNET, "\"{0}\"");
}
if (hasPaint) {
argumentDefaults.Add(MSPAINT, "\"{0}\"");
}
return argumentDefaults;
case "RunInbackground":
Dictionary<string, bool> runInBackground = new Dictionary<string, bool>();
if (hasPaintDotNet) {
runInBackground.Add(PAINTDOTNET, true);
}
if (hasPaint) {
runInBackground.Add(MSPAINT, true);
}
return runInBackground;
case nameof(DeletedBuildInCommands):
return new List<string>();
case nameof(Commands):
return new List<string>();
case nameof(Commandline):
return new Dictionary<string, string>();
case nameof(Argument):
return new Dictionary<string, string>();
case nameof(RunInbackground):
return new Dictionary<string, bool>();
}
return null;
}

View file

@ -71,10 +71,10 @@ namespace ExternalCommand {
if (_presetCommand != null) {
if (!config.runInbackground.ContainsKey(_presetCommand)) {
config.runInbackground.Add(_presetCommand, true);
if (!config.RunInbackground.ContainsKey(_presetCommand)) {
config.RunInbackground.Add(_presetCommand, true);
}
bool runInBackground = config.runInbackground[_presetCommand];
bool runInBackground = config.RunInbackground[_presetCommand];
string fullPath = captureDetails.Filename;
if (fullPath == null) {
fullPath = ImageOutput.SaveNamedTmpFile(surface, captureDetails, outputSettings);
@ -139,7 +139,7 @@ namespace ExternalCommand {
} catch (Exception ex) {
exportInformation.ExportMade = false;
exportInformation.ErrorMessage = ex.Message;
LOG.WarnFormat("Error calling external command: {0} ", exportInformation.ErrorMessage);
LOG.WarnFormat("Error calling external command: {0} ", exportInformation.ErrorMessage);
}
}
@ -158,13 +158,13 @@ namespace ExternalCommand {
try {
return CallExternalCommand(commando, fullPath, "runas", out output, out error);
} catch {
w32ex.Data.Add("commandline", config.commandlines[_presetCommand]);
w32ex.Data.Add("arguments", config.arguments[_presetCommand]);
w32ex.Data.Add("commandline", config.Commandline[_presetCommand]);
w32ex.Data.Add("arguments", config.Argument[_presetCommand]);
throw;
}
} catch (Exception ex) {
ex.Data.Add("commandline", config.commandlines[_presetCommand]);
ex.Data.Add("arguments", config.arguments[_presetCommand]);
ex.Data.Add("commandline", config.Commandline[_presetCommand]);
ex.Data.Add("arguments", config.Argument[_presetCommand]);
throw;
}
}
@ -179,8 +179,8 @@ namespace ExternalCommand {
/// <param name="error"></param>
/// <returns></returns>
private int CallExternalCommand(string commando, string fullPath, string verb, out string output, out string error) {
string commandline = config.commandlines[commando];
string arguments = config.arguments[commando];
string commandline = config.Commandline[commando];
string arguments = config.Argument[commando];
output = null;
error = null;
if (!string.IsNullOrEmpty(commandline)) {
@ -227,9 +227,9 @@ namespace ExternalCommand {
return -1;
}
public static string FormatArguments(string arguments, string fullpath)
{
return string.Format(arguments, fullpath);
}
}
public static string FormatArguments(string arguments, string fullpath)
{
return string.Format(arguments, fullpath);
}
}
}

View file

@ -33,9 +33,9 @@ namespace ExternalCommand {
/// An Plugin to run commands after an image was written
/// </summary>
public class ExternalCommandPlugin : IGreenshotPlugin {
private static readonly log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(ExternalCommandPlugin));
private static readonly CoreConfiguration coreConfig = IniConfig.GetIniSection<CoreConfiguration>();
private static readonly ExternalCommandConfiguration config = IniConfig.GetIniSection<ExternalCommandConfiguration>();
private static readonly log4net.ILog Log = log4net.LogManager.GetLogger(typeof(ExternalCommandPlugin));
private static readonly CoreConfiguration CoreConfig = IniConfig.GetIniSection<CoreConfiguration>();
private static readonly ExternalCommandConfiguration ExternalCommandConfig = IniConfig.GetIniSection<ExternalCommandConfiguration>();
private IGreenshotHost _host;
private PluginAttribute _myAttributes;
private ToolStripMenuItem _itemPlugInRoot;
@ -55,7 +55,7 @@ namespace ExternalCommand {
}
public IEnumerable<IDestination> Destinations() {
foreach(string command in config.Commands) {
foreach(string command in ExternalCommandConfig.Commands) {
yield return new ExternalCommandDestination(command);
}
}
@ -69,26 +69,26 @@ namespace ExternalCommand {
/// </summary>
/// <param name="command"></param>
/// <returns>false if the command is not correctly configured</returns>
private bool isCommandValid(string command) {
if (!config.runInbackground.ContainsKey(command)) {
LOG.WarnFormat("Found missing runInbackground for {0}", command);
private bool IsCommandValid(string command) {
if (!ExternalCommandConfig.RunInbackground.ContainsKey(command)) {
Log.WarnFormat("Found missing runInbackground for {0}", command);
// Fix it
config.runInbackground.Add(command, true);
ExternalCommandConfig.RunInbackground.Add(command, true);
}
if (!config.arguments.ContainsKey(command)) {
LOG.WarnFormat("Found missing argument for {0}", command);
if (!ExternalCommandConfig.Argument.ContainsKey(command)) {
Log.WarnFormat("Found missing argument for {0}", command);
// Fix it
config.arguments.Add(command, "{0}");
ExternalCommandConfig.Argument.Add(command, "{0}");
}
if (!config.commandlines.ContainsKey(command)) {
LOG.WarnFormat("Found missing commandline for {0}", command);
if (!ExternalCommandConfig.Commandline.ContainsKey(command)) {
Log.WarnFormat("Found missing commandline for {0}", command);
return false;
}
string commandline = FilenameHelper.FillVariables(config.commandlines[command], true);
string commandline = FilenameHelper.FillVariables(ExternalCommandConfig.Commandline[command], true);
commandline = FilenameHelper.FillCmdVariables(commandline, true);
if (!File.Exists(commandline)) {
LOG.WarnFormat("Found 'invalid' commandline {0} for command {1}", config.commandlines[command], command);
Log.WarnFormat("Found 'invalid' commandline {0} for command {1}", ExternalCommandConfig.Commandline[command], command);
return false;
}
return true;
@ -99,22 +99,19 @@ namespace ExternalCommand {
/// <param name="pluginHost">Use the IGreenshotPluginHost interface to register events</param>
/// <param name="myAttributes">My own attributes</param>
public virtual bool Initialize(IGreenshotHost pluginHost, PluginAttribute myAttributes) {
LOG.DebugFormat("Initialize called of {0}", myAttributes.Name);
Log.DebugFormat("Initialize called of {0}", myAttributes.Name);
List<string> commandsToDelete = new List<string>();
// Check configuration
foreach(string command in config.Commands) {
if (!isCommandValid(command)) {
foreach(string command in ExternalCommandConfig.Commands) {
if (!IsCommandValid(command)) {
commandsToDelete.Add(command);
}
}
// cleanup
foreach (string command in commandsToDelete) {
config.runInbackground.Remove(command);
config.commandlines.Remove(command);
config.arguments.Remove(command);
config.Commands.Remove(command);
ExternalCommandConfig.Delete(command);
}
_host = pluginHost;
@ -128,7 +125,7 @@ namespace ExternalCommand {
PluginUtils.AddToContextMenu(_host, _itemPlugInRoot);
Language.LanguageChanged += OnLanguageChanged;
coreConfig.PropertyChanged += OnIconSizeChanged;
CoreConfig.PropertyChanged += OnIconSizeChanged;
return true;
}
@ -145,7 +142,7 @@ namespace ExternalCommand {
_itemPlugInRoot.Image = PluginUtils.GetCachedExeIcon(exePath, 0);
}
} catch (Exception ex) {
LOG.Warn("Couldn't get the cmd.exe image", ex);
Log.Warn("Couldn't get the cmd.exe image", ex);
}
}
}
@ -157,7 +154,7 @@ namespace ExternalCommand {
}
public virtual void Shutdown() {
LOG.Debug("Shutdown of " + _myAttributes.Name);
Log.Debug("Shutdown of " + _myAttributes.Name);
}
private void ConfigMenuClick(object sender, EventArgs eventArgs) {
@ -168,7 +165,7 @@ namespace ExternalCommand {
/// Implementation of the IPlugin.Configure
/// </summary>
public virtual void Configure() {
LOG.Debug("Configure called");
Log.Debug("Configure called");
new SettingsForm().ShowDialog();
}
}

View file

@ -33,11 +33,11 @@ namespace ExternalCommand {
public static Image IconForCommand(string commandName) {
Image icon = null;
if (commandName != null) {
if (config.commandlines.ContainsKey(commandName) && File.Exists(config.commandlines[commandName])) {
if (config.Commandline.ContainsKey(commandName) && File.Exists(config.Commandline[commandName])) {
try {
icon = PluginUtils.GetCachedExeIcon(config.commandlines[commandName], 0);
icon = PluginUtils.GetCachedExeIcon(config.Commandline[commandName], 0);
} catch (Exception ex) {
LOG.Warn("Problem loading icon for " + config.commandlines[commandName], ex);
LOG.Warn("Problem loading icon for " + config.Commandline[commandName], ex);
}
}
}

View file

@ -29,8 +29,7 @@ namespace ExternalCommand {
/// Description of SettingsForm.
/// </summary>
public partial class SettingsForm : ExternalCommandForm {
private static readonly log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(SettingsForm));
private static readonly ExternalCommandConfiguration config = IniConfig.GetIniSection<ExternalCommandConfiguration>();
private static readonly ExternalCommandConfiguration ExternalCommandConfig = IniConfig.GetIniSection<ExternalCommandConfiguration>();
public SettingsForm() {
//
@ -42,35 +41,34 @@ namespace ExternalCommand {
UpdateView();
}
void ButtonOkClick(object sender, EventArgs e) {
private void ButtonOkClick(object sender, EventArgs e) {
IniConfig.Save();
}
void ButtonAddClick(object sender, EventArgs e) {
SettingsFormDetail form = new SettingsFormDetail(null);
private void ButtonAddClick(object sender, EventArgs e) {
var form = new SettingsFormDetail(null);
form.ShowDialog();
UpdateView();
}
void ButtonDeleteClick(object sender, EventArgs e) {
private void ButtonDeleteClick(object sender, EventArgs e) {
foreach(ListViewItem item in listView1.SelectedItems) {
string commando = item.Tag as string;
config.Commands.Remove(commando);
config.commandlines.Remove(commando);
config.arguments.Remove(commando);
ExternalCommandConfig.Delete(commando);
}
UpdateView();
}
void UpdateView() {
private void UpdateView() {
listView1.Items.Clear();
if(config.Commands != null) {
if(ExternalCommandConfig.Commands != null) {
listView1.ListViewItemSorter = new ListviewComparer();
ImageList imageList = new ImageList();
listView1.SmallImageList = imageList;
int imageNr = 0;
foreach(string commando in config.Commands) {
foreach(string commando in ExternalCommandConfig.Commands) {
ListViewItem item;
Image iconForExe = IconCache.IconForCommand(commando);
if(iconForExe != null) {
@ -87,15 +85,15 @@ namespace ExternalCommand {
button_edit.Enabled = listView1.SelectedItems.Count > 0;
}
void ListView1ItemSelectionChanged(object sender, EventArgs e) {
private void ListView1ItemSelectionChanged(object sender, EventArgs e) {
button_edit.Enabled = listView1.SelectedItems.Count > 0;
}
void ButtonEditClick(object sender, EventArgs e) {
private void ButtonEditClick(object sender, EventArgs e) {
ListView1DoubleClick(sender, e);
}
void ListView1DoubleClick(object sender, EventArgs e) {
private void ListView1DoubleClick(object sender, EventArgs e) {
// Safety check for bug #1484
bool selectionActive = listView1.SelectedItems.Count > 0;
if(!selectionActive) {
@ -104,7 +102,7 @@ namespace ExternalCommand {
}
string commando = listView1.SelectedItems[0].Tag as string;
SettingsFormDetail form = new SettingsFormDetail(commando);
var form = new SettingsFormDetail(commando);
form.ShowDialog();
UpdateView();
@ -120,12 +118,9 @@ namespace ExternalCommand {
return (0);
}
ListViewItem l1 = (ListViewItem)x;
ListViewItem l2 = (ListViewItem)y;
if(l2 == null) {
return 1;
}
return l1.Text.CompareTo(l2.Text);
var l1 = (ListViewItem)x;
var l2 = (ListViewItem)y;
return string.Compare(l1.Text, l2.Text, StringComparison.Ordinal);
}
}
}

View file

@ -45,8 +45,8 @@ namespace ExternalCommand {
if(commando != null) {
textBox_name.Text = commando;
textBox_commandline.Text = config.commandlines[commando];
textBox_arguments.Text = config.arguments[commando];
textBox_commandline.Text = config.Commandline[commando];
textBox_arguments.Text = config.Argument[commando];
_commandIndex = config.Commands.FindIndex(delegate(string s) { return s == commando; });
} else {
textBox_arguments.Text = "\"{0}\"";
@ -54,25 +54,25 @@ namespace ExternalCommand {
OkButtonState();
}
void ButtonOkClick(object sender, EventArgs e) {
private void ButtonOkClick(object sender, EventArgs e) {
string commandName = textBox_name.Text;
string commandLine = textBox_commandline.Text;
string arguments = textBox_arguments.Text;
if(_commando != null) {
config.Commands[_commandIndex] = commandName;
config.commandlines.Remove(_commando);
config.commandlines.Add(commandName, commandLine);
config.arguments.Remove(_commando);
config.arguments.Add(commandName, arguments);
config.Commandline.Remove(_commando);
config.Commandline.Add(commandName, commandLine);
config.Argument.Remove(_commando);
config.Argument.Add(commandName, arguments);
} else {
config.Commands.Add(commandName);
config.commandlines.Add(commandName, commandLine);
config.arguments.Add(commandName, arguments);
config.Commandline.Add(commandName, commandLine);
config.Argument.Add(commandName, arguments);
}
}
void Button3Click(object sender, EventArgs e) {
OpenFileDialog openFileDialog = new OpenFileDialog
private void Button3Click(object sender, EventArgs e) {
var openFileDialog = new OpenFileDialog
{
Filter = "Executables (*.exe, *.bat, *.com)|*.exe; *.bat; *.com|All files (*)|*",
FilterIndex = 1,
@ -106,7 +106,7 @@ namespace ExternalCommand {
buttonOk.Enabled = true;
textBox_name.BackColor = Color.White;
textBox_commandline.BackColor = Color.White;
textBox_arguments.BackColor = Color.White;
textBox_arguments.BackColor = Color.White;
// Is there a text in the name field
if(string.IsNullOrEmpty(textBox_name.Text)) {
buttonOk.Enabled = false;
@ -136,17 +136,17 @@ namespace ExternalCommand {
}
// Are the arguments in a valid format?
try
{
{
string arguments = FilenameHelper.FillVariables(textBox_arguments.Text, false);
arguments = FilenameHelper.FillCmdVariables(arguments, false);
ExternalCommandDestination.FormatArguments(arguments, string.Empty);
}
catch
{
buttonOk.Enabled = false;
textBox_arguments.BackColor = Color.Red;
}
}
catch
{
buttonOk.Enabled = false;
textBox_arguments.BackColor = Color.Red;
}
}
private void textBox_name_TextChanged(object sender, EventArgs e) {
@ -157,10 +157,10 @@ namespace ExternalCommand {
OkButtonState();
}
private void textBox_arguments_TextChanged(object sender, EventArgs e)
{
OkButtonState();
}
private void textBox_arguments_TextChanged(object sender, EventArgs e)
{
OkButtonState();
}
}
}