Made the plug-in work with the new ini configuration framework

git-svn-id: http://svn.code.sf.net/p/greenshot/code/trunk@859 7dccd23d-a4a3-4e1f-8c07-b4c1b4018ab4
This commit is contained in:
RKrom 2010-08-24 15:25:28 +00:00
parent bb7f216567
commit f000080bc4
6 changed files with 133 additions and 137 deletions

View file

@ -1,66 +0,0 @@
/*
* Greenshot - a free and open source screenshot tool
* Copyright (C) 2007-2010 Thomas Braun, Jens Klingen, Robin Krom
*
* For more information see: http://getgreenshot.org/
* The Greenshot project is hosted on Sourceforge: http://sourceforge.net/projects/greenshot/
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 1 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Serialization;
namespace RunAtOutput {
/// <summary>
/// Description of Configuration.
/// </summary>
[XmlRoot("RunAtOutput")]
public class Configuration {
public Configuration() {
Commands = new List<Commando>();
}
[XmlElement("Commando")]
public List<Commando> Commands {
get;
set;
}
}
public class Commando {
public Commando() {
}
[XmlAttribute("Name")]
public string Name {
get;
set;
}
[XmlElement("Commandline")]
public string Commandline {
get;
set;
}
[XmlElement("Arguments")]
public string Arguments {
get;
set;
}
[XmlAttribute("Active")]
public bool Active{
get;
set;
}
}
}

View file

@ -49,15 +49,14 @@
<Reference Include="System.Xml" /> <Reference Include="System.Xml" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Configuration.cs" />
<Compile Include="RunAtOutput.cs" /> <Compile Include="RunAtOutput.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="RunAtOutputConfiguration.cs" />
<Compile Include="SettingsForm.cs" /> <Compile Include="SettingsForm.cs" />
<Compile Include="SettingsForm.Designer.cs"> <Compile Include="SettingsForm.Designer.cs">
<DependentUpon>SettingsForm.cs</DependentUpon> <DependentUpon>SettingsForm.cs</DependentUpon>
</Compile> </Compile>
<None Include="Properties\AssemblyInfo.cs.template" /> <None Include="Properties\AssemblyInfo.cs.template" />
<None Include="runatoutput.properties" />
</ItemGroup> </ItemGroup>
<PropertyGroup> <PropertyGroup>
<PreBuildEvent>"$(SolutionDir)\tools\TortoiseSVN\SubWCRev.exe" "$(ProjectDir)\" "$(ProjectDir)\Properties\AssemblyInfo.cs.template" "$(ProjectDir)\Properties\AssemblyInfo.cs"</PreBuildEvent> <PreBuildEvent>"$(SolutionDir)\tools\TortoiseSVN\SubWCRev.exe" "$(ProjectDir)\" "$(ProjectDir)\Properties\AssemblyInfo.cs.template" "$(ProjectDir)\Properties\AssemblyInfo.cs"</PreBuildEvent>

View file

@ -29,6 +29,7 @@ using System.Windows.Forms;
using System.Xml.Serialization; using System.Xml.Serialization;
using Greenshot.Capturing; using Greenshot.Capturing;
using Greenshot.Core;
using Greenshot.Plugin; using Greenshot.Plugin;
namespace RunAtOutput { namespace RunAtOutput {
@ -40,7 +41,7 @@ namespace RunAtOutput {
private IGreenshotPluginHost host; private IGreenshotPluginHost host;
private ICaptureHost captureHost = null; private ICaptureHost captureHost = null;
private PluginAttribute myAttributes; private PluginAttribute myAttributes;
private Configuration config = new Configuration(); private RunAtOutputConfiguration config;
public RunAtOutputPlugin() { public RunAtOutputPlugin() {
} }
@ -59,7 +60,7 @@ namespace RunAtOutput {
this.myAttributes = myAttributes; this.myAttributes = myAttributes;
this.host.OnImageOutput += new OnImageOutputHandler(ImageOutput); this.host.OnImageOutput += new OnImageOutputHandler(ImageOutput);
LoadConfig(); this.config = IniConfig.GetIniSection<RunAtOutputConfiguration>();
} }
public virtual void Shutdown() { public virtual void Shutdown() {
@ -72,29 +73,7 @@ namespace RunAtOutput {
/// </summary> /// </summary>
public virtual void Configure() { public virtual void Configure() {
LOG.Debug("Configure called"); LOG.Debug("Configure called");
SettingsForm settingsForm = new SettingsForm(config); new SettingsForm().ShowDialog();
if (settingsForm.ShowDialog() == DialogResult.OK) {
SaveConfig();
}
settingsForm.Dispose();
}
private void LoadConfig() {
XmlSerializer xmlSerializer = new XmlSerializer(typeof(Configuration));
string filename = Path.Combine(host.ConfigurationPath, "RunAtOutputPlugin.xml");
if (File.Exists(filename)) {
using (TextReader reader = new StreamReader(filename)) {
config = (Configuration)xmlSerializer.Deserialize(reader);
}
}
}
private void SaveConfig() {
XmlSerializer xmlSerializer = new XmlSerializer(typeof(Configuration));
string filename = Path.Combine(host.ConfigurationPath, "RunAtOutputPlugin.xml");
using (TextWriter writer = new StreamWriter(filename)) {
xmlSerializer.Serialize(writer, config);
}
} }
/// <summary> /// <summary>
@ -103,23 +82,23 @@ namespace RunAtOutput {
/// <param name="ImageOutputEventArgs">Has the FullPath to the image</param> /// <param name="ImageOutputEventArgs">Has the FullPath to the image</param>
private void ImageOutput(object sender, ImageOutputEventArgs eventArgs) { private void ImageOutput(object sender, ImageOutputEventArgs eventArgs) {
LOG.Debug("ImageOutput called with full path: " + eventArgs.FullPath); LOG.Debug("ImageOutput called with full path: " + eventArgs.FullPath);
foreach(Commando commando in config.Commands) { foreach(string commando in config.active) {
if (commando.Active) { string commandline = config.commandlines[commando];
if (commando.Commandline != null && commando.Commandline.Length > 0) { string arguments = config.arguments[commando];
Process p = new Process(); if (commandline != null && commandline.Length > 0) {
p.StartInfo.FileName = commando.Commandline; Process p = new Process();
p.StartInfo.Arguments = String.Format(commando.Arguments, eventArgs.FullPath); p.StartInfo.FileName = commandline;
p.StartInfo.UseShellExecute = false; p.StartInfo.Arguments = String.Format(arguments, eventArgs.FullPath);
p.StartInfo.RedirectStandardOutput = true; p.StartInfo.UseShellExecute = false;
LOG.Info("Starting : " + p.StartInfo.FileName + " " + p.StartInfo.Arguments); p.StartInfo.RedirectStandardOutput = true;
p.Start(); LOG.Info("Starting : " + p.StartInfo.FileName + " " + p.StartInfo.Arguments);
string output = p.StandardOutput.ReadToEnd(); p.Start();
if (output != null && output.Trim().Length > 0) { string output = p.StandardOutput.ReadToEnd();
LOG.Info("Output:\n" + output); if (output != null && output.Trim().Length > 0) {
} LOG.Info("Output:\n" + output);
LOG.Info("Finished : " + p.StartInfo.FileName + " " + p.StartInfo.Arguments); }
} LOG.Info("Finished : " + p.StartInfo.FileName + " " + p.StartInfo.Arguments);
} }
} }
} }
} }

View file

@ -0,0 +1,75 @@
/*
* Greenshot - a free and open source screenshot tool
* Copyright (C) 2007-2010 Thomas Braun, Jens Klingen, Robin Krom
*
* For more information see: http://getgreenshot.org/
* The Greenshot project is hosted on Sourceforge: http://sourceforge.net/projects/greenshot/
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 1 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using Greenshot.Core;
namespace RunAtOutput {
/// <summary>
/// Description of FlickrConfiguration.
/// </summary>
[IniSection("RunAtOutput", Description="Greenshot TitRunAtOutputleFix Plugin configuration")]
public class RunAtOutputConfiguration : IniSection {
[IniProperty("Commands", Description="The commands that are available.")]
public List<string> commands;
//[IniProperty("Matcher", Description="Match filenames or types.")]
//public Dictionary<string, string> matchers;
[IniProperty("Commandline", Description="The commandline for the output command.")]
public Dictionary<string, string> commandlines;
[IniProperty("Argument", Description="The arguments for the output command.")]
public Dictionary<string, string> arguments;
[IniProperty("ActiveCommands", Description="The commands that are active.")]
public List<string> active;
/// <summary>
/// Supply values we can't put as defaults
/// </summary>
/// <param name="property">The property to return a default for</param>
/// <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>();
commandDefaults.Add("Paint.NET");
return commandDefaults;
case "Commandline":
Dictionary<string, string> commandlineDefaults = new Dictionary<string, string>();
commandlineDefaults.Add("Paint.NET", @"C:\Programme\Paint.NET\PaintDotNet.exe");
return commandlineDefaults;
// case "Matcher":
// Dictionary<string, string> matcherDefaults = new Dictionary<string, string>();
// matcherDefaults.Add("Paint.NET", "*");
// return matcherDefaults;
case "ActiveCommands":
return new List<string>();
case "Argument":
Dictionary<string, string> argumentDefaults = new Dictionary<string, string>();
argumentDefaults.Add("Paint.NET", "\"{0}\"");
return argumentDefaults;
}
return null;
}
}
}

View file

@ -22,16 +22,18 @@ using System;
using System.Drawing; using System.Drawing;
using System.Windows.Forms; using System.Windows.Forms;
using Greenshot.Core;
namespace RunAtOutput { namespace RunAtOutput {
/// <summary> /// <summary>
/// Description of SettingsForm. /// Description of SettingsForm.
/// </summary> /// </summary>
public partial class SettingsForm : Form { public partial class SettingsForm : Form {
private static log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(SettingsForm)); private static log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(SettingsForm));
private Configuration config; private RunAtOutputConfiguration config;
public SettingsForm(Configuration config) { public SettingsForm() {
this.config = config; this.config = IniConfig.GetIniSection<RunAtOutputConfiguration>();
// //
// The InitializeComponent() call is required for Windows Forms designer support. // The InitializeComponent() call is required for Windows Forms designer support.
// //
@ -40,6 +42,7 @@ namespace RunAtOutput {
} }
void ButtonOkClick(object sender, EventArgs e) { void ButtonOkClick(object sender, EventArgs e) {
IniConfig.Save();
DialogResult = DialogResult.OK; DialogResult = DialogResult.OK;
} }
@ -49,38 +52,43 @@ namespace RunAtOutput {
void ShowSelectedItem() { void ShowSelectedItem() {
foreach ( ListViewItem item in listView1.SelectedItems ) { foreach ( ListViewItem item in listView1.SelectedItems ) {
Commando commando = item.Tag as Commando; string commando = item.Tag as string;
textBox_arguments.Text = commando.Arguments; textBox_name.Text = commando;
textBox_commandline.Text = commando.Commandline; textBox_commandline.Text = config.commandlines[commando];
textBox_name.Text = commando.Name; textBox_arguments.Text = config.arguments[commando];
} }
} }
void ButtonAddClick(object sender, EventArgs e) { void ButtonAddClick(object sender, EventArgs e) {
Commando commando = new Commando(); config.commands.Add(textBox_name.Text);
commando.Arguments = textBox_arguments.Text; config.commandlines.Add(textBox_name.Text, textBox_commandline.Text);
commando.Commandline = textBox_commandline.Text; config.arguments.Add(textBox_name.Text, textBox_arguments.Text);
commando.Name = textBox_name.Text;
config.Commands.Add(commando);
UpdateView(); UpdateView();
} }
void ButtonDeleteClick(object sender, EventArgs e) { void ButtonDeleteClick(object sender, EventArgs e) {
foreach ( ListViewItem item in listView1.SelectedItems ) { foreach ( ListViewItem item in listView1.SelectedItems ) {
Commando commando = item.Tag as Commando; string commando = item.Tag as string;
config.Commands.Remove(commando); config.active.Remove(textBox_name.Text);
config.commands.Remove(textBox_name.Text);
config.commandlines.Remove(textBox_name.Text);
config.arguments.Remove(textBox_name.Text);
} }
UpdateView(); UpdateView();
} }
void UpdateView() { void UpdateView() {
listView1.Items.Clear(); listView1.Items.Clear();
foreach(Commando commando in config.Commands) { if (config.commands != null) {
ListViewItem item = new ListViewItem(""); foreach(string commando in config.commands) {
item.SubItems.Add(commando.Name); ListViewItem item = new ListViewItem("");
item.Checked = commando.Active; item.SubItems.Add(commando);
item.Tag = commando; if (config.active != null) {
listView1.Items.Add(item); item.Checked = config.active.Contains(commando);
}
item.Tag = commando;
listView1.Items.Add(item);
}
} }
} }
@ -90,9 +98,14 @@ namespace RunAtOutput {
} }
void ListView1ItemChecked(object sender, ItemCheckedEventArgs e) { void ListView1ItemChecked(object sender, ItemCheckedEventArgs e) {
Commando commando = e.Item.Tag as Commando; string commando = e.Item.Tag as string;
LOG.Debug("ItemChecked " + commando.Name + " to " + e.Item.Checked); LOG.Debug("ItemChecked " + commando + " to " + e.Item.Checked);
commando.Active = e.Item.Checked; if (e.Item.Checked && !config.active.Contains(commando)) {
config.active.Add(commando);
}
if (!e.Item.Checked) {
config.active.Remove(commando);
}
} }
} }
} }

View file

@ -1,4 +0,0 @@
active=OptiPNG,Paint.NET
command.Paint.NET=C:\Programme\Paint.NET\PaintDotNet.exe {0}
pattern.OptiPNG=*.png
command.OptiPNG=D:\05018085\downloads\optipng-0.6.4-exe\optipng.exe {0}