Refactored to use the new configuration framework, showing how much less work it is.

git-svn-id: http://svn.code.sf.net/p/greenshot/code/trunk@813 7dccd23d-a4a3-4e1f-8c07-b4c1b4018ab4
This commit is contained in:
RKrom 2010-08-18 05:40:48 +00:00
commit 5e37adbd0c
4 changed files with 59 additions and 66 deletions

View file

@ -56,6 +56,7 @@
<ItemGroup> <ItemGroup>
<Compile Include="Language.cs" /> <Compile Include="Language.cs" />
<Compile Include="LanguageKeys.cs" /> <Compile Include="LanguageKeys.cs" />
<Compile Include="OCRConfiguration.cs" />
<Compile Include="OCRPlugin.cs" /> <Compile Include="OCRPlugin.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SettingsForm.cs" /> <Compile Include="SettingsForm.cs" />

View file

@ -0,0 +1,38 @@
/*
* 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 GreenshotOCR {
/// <summary>
/// Description of CoreConfiguration.
/// </summary>
[IniSection("OCR", Description="Greenshot OCR configuration")]
public class OCRConfiguration : IniSection {
[IniProperty("Language", Description="Language for OCR", DefaultValue="miLANG_ENGLISH")]
public string Language;
[IniProperty("orientimage", Description="Orient image?", DefaultValue="true")]
public bool Orientimage;
[IniProperty("straightenImage", Description="Straighten image?", DefaultValue="true")]
public bool StraightenImage;
}
}

View file

@ -39,15 +39,13 @@ namespace GreenshotOCR {
/// </summary> /// </summary>
public class OcrPlugin : IGreenshotPlugin { public class OcrPlugin : IGreenshotPlugin {
private static log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(OcrPlugin)); private static log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(OcrPlugin));
private const string CONFIG_FILENAME = "ocr-config.properties";
private const string MODI_OFFICE11 = @"Software\Microsoft\Office\11.0\MODI"; private const string MODI_OFFICE11 = @"Software\Microsoft\Office\11.0\MODI";
private const string MODI_OFFICE12 = @"Software\Microsoft\Office\12.0\MODI"; private const string MODI_OFFICE12 = @"Software\Microsoft\Office\12.0\MODI";
private IGreenshotPluginHost host; private IGreenshotPluginHost host;
private ICaptureHost captureHost = null; private ICaptureHost captureHost = null;
private PluginAttribute myAttributes; private PluginAttribute myAttributes;
private Properties config = new Properties(); private OCRConfiguration config;
public OcrPlugin() { } public OcrPlugin() { }
@ -70,7 +68,12 @@ namespace GreenshotOCR {
LOG.Warn("No MODI found!"); LOG.Warn("No MODI found!");
return; return;
} }
LoadConfig();
// Load configuration
config = IniConfig.GetInstance().GetSection<OCRConfiguration>();
if (config.IsDirty) {
IniConfig.GetInstance().Save();
}
this.host.RegisterHotKey(3, 0x2C, new HotKeyHandler(MyHotkeyHandler)); this.host.RegisterHotKey(3, 0x2C, new HotKeyHandler(MyHotkeyHandler));
// Here we can hang ourselves to the main context menu! // Here we can hang ourselves to the main context menu!
@ -109,13 +112,10 @@ namespace GreenshotOCR {
MessageBox.Show("Sorry, is seems that Microsoft Office Document Imaging (MODI) is not installed, therefor the OCR Plugin cannot work."); MessageBox.Show("Sorry, is seems that Microsoft Office Document Imaging (MODI) is not installed, therefor the OCR Plugin cannot work.");
return; return;
} }
SettingsForm settingsForm = new SettingsForm(GetLanguages(),config); SettingsForm settingsForm = new SettingsForm(GetLanguages(), config);
DialogResult result = settingsForm.ShowDialog(); DialogResult result = settingsForm.ShowDialog();
if (result == DialogResult.OK) { if (result == DialogResult.OK) {
config.ChangeProperty("language", settingsForm.OCRLanguage); IniConfig.GetInstance().Save();
config.ChangeBoolProperty("orientImage", settingsForm.OrientImage);
config.ChangeBoolProperty("straightenImage", settingsForm.StraightenImage);;
SaveConfig();
} }
} }
@ -135,41 +135,6 @@ namespace GreenshotOCR {
return null; return null;
} }
private void LoadConfig() {
string filename = Path.Combine(host.ConfigurationPath, CONFIG_FILENAME);
if (File.Exists(filename)) {
LOG.Debug("Loading configuration from: " + filename);
config = Properties.read(filename);
}
bool changed = false;
if (config == null) {
config = new Properties();
changed = true;
}
if (!config.ContainsKey("language")) {
config.AddProperty("language", "miLANG_ENGLISH");
changed = true;
}
if (!config.ContainsKey("straightenImage")) {
config.AddBoolProperty("straightenImage", false);
changed = true;
}
if (!config.ContainsKey("orientImage")) {
config.AddBoolProperty("orientImage", false);
changed = true;
}
if (changed) {
SaveConfig();
}
}
private void SaveConfig() {
string filename = Path.Combine(host.ConfigurationPath, CONFIG_FILENAME);
LOG.Debug("Saving configuration to: " + filename);
config.write(filename, "# The configuration file for the Greenshot OCR Plugin");
}
private void StartOCRRegion() { private void StartOCRRegion() {
LOG.Debug("Starting OCR!"); LOG.Debug("Starting OCR!");
captureHost.MakeCapture(CaptureMode.Region, false, new CaptureHandler(DoOCR)); captureHost.MakeCapture(CaptureMode.Region, false, new CaptureHandler(DoOCR));
@ -236,7 +201,7 @@ namespace GreenshotOCR {
//md.OnOCRProgress += ; //md.OnOCRProgress += ;
// Do the OCR. // Do the OCR.
modi11Document.OCR((MODI11.MiLANGUAGES)Enum.Parse(typeof(MODI11.MiLANGUAGES), config.GetProperty("language")), config.GetBoolProperty("orientImage"), config.GetBoolProperty("straightenImage")); modi11Document.OCR((MODI11.MiLANGUAGES)Enum.Parse(typeof(MODI11.MiLANGUAGES), config.Language), config.Orientimage, config.StraightenImage);
// Get the first (and only image) // Get the first (and only image)
MODI11.Image modi11Image = (MODI11.Image)modi11Document.Images[0]; MODI11.Image modi11Image = (MODI11.Image)modi11Document.Images[0];
// Get the layout. // Get the layout.
@ -255,7 +220,7 @@ namespace GreenshotOCR {
//md.OnOCRProgress += ; //md.OnOCRProgress += ;
// Do the OCR. // Do the OCR.
modi12Document.OCR((MODI12.MiLANGUAGES)Enum.Parse(typeof(MODI12.MiLANGUAGES), config.GetProperty("language")), config.GetBoolProperty("orientImage"), config.GetBoolProperty("straightenImage")); modi12Document.OCR((MODI12.MiLANGUAGES)Enum.Parse(typeof(MODI12.MiLANGUAGES), config.Language), config.Orientimage, config.StraightenImage);
// Get the first (and only image) // Get the first (and only image)
MODI12.Image modi12Image = (MODI12.Image)modi12Document.Images[0]; MODI12.Image modi12Image = (MODI12.Image)modi12Document.Images[0];
// Get the layout. // Get the layout.

View file

@ -31,14 +31,13 @@ namespace GreenshotOCR {
/// </summary> /// </summary>
public partial class SettingsForm : Form { public partial class SettingsForm : Form {
private ILanguage language = Language.GetInstance(); private ILanguage language = Language.GetInstance();
private string selectedLanguage; private OCRConfiguration config;
private bool orientImage;
private bool straightenImage;
public SettingsForm(string [] languages, Properties config) { public SettingsForm(string [] languages, OCRConfiguration config) {
// //
// The InitializeComponent() call is required for Windows Forms designer support. // The InitializeComponent() call is required for Windows Forms designer support.
// //
this.config = config;
InitializeComponent(); InitializeComponent();
language.SynchronizeLanguageToCulture(); language.SynchronizeLanguageToCulture();
initializeComponentText(); initializeComponentText();
@ -48,13 +47,13 @@ namespace GreenshotOCR {
foreach(string availableLanguage in languages) { foreach(string availableLanguage in languages) {
string displayLanguage = cleanLanguage(availableLanguage); string displayLanguage = cleanLanguage(availableLanguage);
comboBox_languages.Items.Add(displayLanguage); comboBox_languages.Items.Add(displayLanguage);
if (availableLanguage.Equals(config["language"])) { if (availableLanguage.Equals(config.Language)) {
comboBox_languages.SelectedIndex = index; comboBox_languages.SelectedIndex = index;
} }
index++; index++;
} }
checkBox_orientImage.Checked = config.GetBoolProperty("orientImage"); checkBox_orientImage.Checked = config.Orientimage;
checkBox_straightenImage.Checked = config.GetBoolProperty("straightenImage"); checkBox_straightenImage.Checked = config.StraightenImage;
} }
private void initializeComponentText() { private void initializeComponentText() {
this.label_language.Text = language.GetString(LangKey.language); this.label_language.Text = language.GetString(LangKey.language);
@ -70,16 +69,6 @@ namespace GreenshotOCR {
return displayLanguage; return displayLanguage;
} }
public string OCRLanguage {
get {return selectedLanguage;}
}
public bool OrientImage {
get {return orientImage;}
}
public bool StraightenImage {
get {return straightenImage;}
}
void ButtonCancelClick(object sender, EventArgs e) { void ButtonCancelClick(object sender, EventArgs e) {
DialogResult = DialogResult.Cancel; DialogResult = DialogResult.Cancel;
} }
@ -87,10 +76,10 @@ namespace GreenshotOCR {
void ButtonOKClick(object sender, EventArgs e) { void ButtonOKClick(object sender, EventArgs e) {
string selectedString = (string) comboBox_languages.SelectedItem; string selectedString = (string) comboBox_languages.SelectedItem;
if (selectedString != null) { if (selectedString != null) {
selectedLanguage = "miLANG_" + selectedString.ToUpper().Replace(" ", "_"); config.Language = "miLANG_" + selectedString.ToUpper().Replace(" ", "_");
} }
orientImage = checkBox_orientImage.Checked; config.Orientimage = checkBox_orientImage.Checked;
straightenImage = checkBox_straightenImage.Checked; config.StraightenImage = checkBox_straightenImage.Checked;
DialogResult = DialogResult.OK; DialogResult = DialogResult.OK;