From a5f900f92e1a54c9e0fee30889e9e5ead7cfcdf2 Mon Sep 17 00:00:00 2001 From: RKrom Date: Mon, 24 Sep 2012 14:57:02 +0000 Subject: [PATCH] Some small fixes for different parts of code. git-svn-id: http://svn.code.sf.net/p/greenshot/code/trunk@2087 7dccd23d-a4a3-4e1f-8c07-b4c1b4018ab4 --- .../ExternalCommandConfiguration.cs | 20 ++++++- .../ExternalCommandDestination.cs | 56 +++++++++++++------ GreenshotOCRCommand/COMWrapper.cs | 16 +++--- GreenshotPlugin/Controls/OAuthLoginForm.cs | 4 +- GreenshotPlugin/Core/Cache.cs | 12 +++- 5 files changed, 79 insertions(+), 29 deletions(-) diff --git a/GreenshotExternalCommandPlugin/ExternalCommandConfiguration.cs b/GreenshotExternalCommandPlugin/ExternalCommandConfiguration.cs index 6780aa0ac..4825b2467 100644 --- a/GreenshotExternalCommandPlugin/ExternalCommandConfiguration.cs +++ b/GreenshotExternalCommandPlugin/ExternalCommandConfiguration.cs @@ -39,7 +39,10 @@ namespace ExternalCommand { [IniProperty("Argument", Description="The arguments for the output command.")] public Dictionary arguments; - + + [IniProperty("RunInbackground", Description = "Should the command be started in the background.")] + public Dictionary runInbackground; + private const string MSPAINT = "MS Paint"; private static string paintPath; private static bool hasPaint = false; @@ -84,11 +87,22 @@ namespace ExternalCommand { return commandlineDefaults; case "Argument": Dictionary argumentDefaults = new Dictionary(); - argumentDefaults.Add(PAINTDOTNET, "\"{0}\""); + if (hasPaintDotNet) { + argumentDefaults.Add(PAINTDOTNET, "\"{0}\""); + } if (hasPaint) { argumentDefaults.Add(MSPAINT, "\"{0}\""); } - return argumentDefaults; + return argumentDefaults; + case "RunInBackground": + Dictionary runInBackground = new Dictionary(); + if (hasPaintDotNet) { + runInBackground.Add(PAINTDOTNET, true); + } + if (hasPaint) { + runInBackground.Add(MSPAINT, true); + } + return runInBackground; } return null; } diff --git a/GreenshotExternalCommandPlugin/ExternalCommandDestination.cs b/GreenshotExternalCommandPlugin/ExternalCommandDestination.cs index abdc978ed..fdd6e036a 100644 --- a/GreenshotExternalCommandPlugin/ExternalCommandDestination.cs +++ b/GreenshotExternalCommandPlugin/ExternalCommandDestination.cs @@ -70,29 +70,50 @@ namespace ExternalCommand { ExportInformation exportInformation = new ExportInformation(this.Designation, this.Description); OutputSettings outputSettings = new OutputSettings(); - string fullPath = captureDetails.Filename; - if (fullPath == null) { - using (Image image = surface.GetImageForExport()) { - fullPath = host.SaveNamedTmpFile(image, captureDetails, outputSettings); - } - } + if (presetCommand != null) { - Thread commandThread = new Thread (delegate() { - CallExternalCommand(presetCommand, fullPath); - }); - commandThread.Name = "Running " + presetCommand; - commandThread.IsBackground = true; - commandThread.Start(); - exportInformation.ExportMade = true; + if (!config.runInbackground.ContainsKey(presetCommand)) { + config.runInbackground.Add(presetCommand, true); + } + bool runInBackground = config.runInbackground[presetCommand]; + string fullPath = captureDetails.Filename; + if (fullPath == null) { + using (Image image = surface.GetImageForExport()) { + fullPath = host.SaveNamedTmpFile(image, captureDetails, outputSettings); + } + } + + string output = null; + if (runInBackground) { + Thread commandThread = new Thread(delegate() { + CallExternalCommand(presetCommand, fullPath, out output); + }); + commandThread.Name = "Running " + presetCommand; + commandThread.IsBackground = true; + commandThread.Start(); + exportInformation.ExportMade = true; + } else { + try { + if (CallExternalCommand(presetCommand, fullPath, out output) == 0) { + exportInformation.ExportMade = true; + } else { + exportInformation.ErrorMessage = output; + } + } catch (Exception ex) { + exportInformation.ErrorMessage = ex.Message; + } + } + //exportInformation.Uri = "file://" + fullPath; } ProcessExport(exportInformation, surface); return exportInformation; } - private void CallExternalCommand(string commando, string fullPath) { + private int CallExternalCommand(string commando, string fullPath, out string output) { string commandline = config.commandlines[commando]; string arguments = config.arguments[commando]; + output = null; if (commandline != null && commandline.Length > 0) { Process p = new Process(); p.StartInfo.FileName = commandline; @@ -101,12 +122,15 @@ namespace ExternalCommand { p.StartInfo.RedirectStandardOutput = true; LOG.Info("Starting : " + p.StartInfo.FileName + " " + p.StartInfo.Arguments); p.Start(); - string output = p.StandardOutput.ReadToEnd(); + p.WaitForExit(); + output = p.StandardOutput.ReadToEnd(); if (output != null && output.Trim().Length > 0) { LOG.Info("Output:\n" + output); } LOG.Info("Finished : " + p.StartInfo.FileName + " " + p.StartInfo.Arguments); - } + return p.ExitCode; + } + return -1; } } } diff --git a/GreenshotOCRCommand/COMWrapper.cs b/GreenshotOCRCommand/COMWrapper.cs index e0bc63b17..473937038 100644 --- a/GreenshotOCRCommand/COMWrapper.cs +++ b/GreenshotOCRCommand/COMWrapper.cs @@ -99,7 +99,7 @@ namespace Greenshot.Interop { } else { //LOG.Warn("Error getting active object for " + progId, comE); } - } catch (Exception e) { + } catch (Exception) { //LOG.Warn("Error getting active object for " + progId, e); } @@ -157,14 +157,14 @@ namespace Greenshot.Interop { } else { //LOG.Warn("Error getting active object for " + progId, comE); } - } catch (Exception e) { + } catch (Exception) { //LOG.Warn("Error getting active object for " + progId, e); } // Did we get the current instance? If not, try to create a new if (comObject == null) { try { comType = Type.GetTypeFromProgID(progId, true); - } catch (Exception ex) { + } catch (Exception) { //LOG.Warn("Error type for " + progId, ex); } if (comType != null) { @@ -173,7 +173,7 @@ namespace Greenshot.Interop { if (comObject != null) { //LOG.DebugFormat("Created new instance of {0} object.", progId); } - } catch (Exception e) { + } catch (Exception) { //LOG.Warn("Error creating object for " + progId, e); } } @@ -254,7 +254,7 @@ namespace Greenshot.Interop { try { while (Marshal.ReleaseComObject(this._COMObject) > 0) ; - } catch (Exception ex) { + } catch (Exception) { //LOG.WarnFormat("Problem releasing {0}", _COMType); //LOG.Warn("Error: ", ex); } @@ -407,21 +407,21 @@ namespace Greenshot.Interop { foreach (MemberInfo memberInfo in type.GetMembers()) { //LOG.InfoFormat("Member: {0};", memberInfo.ToString()); } - } catch (Exception memberException) { + } catch (Exception) { //LOG.Error(memberException); } try { foreach (PropertyInfo propertyInfo in type.GetProperties()) { //LOG.InfoFormat("Property: {0};", propertyInfo.ToString()); } - } catch (Exception propertyException) { + } catch (Exception) { //LOG.Error(propertyException); } try { foreach (FieldInfo fieldInfo in type.GetFields()) { //LOG.InfoFormat("Field: {0};", fieldInfo.ToString()); } - } catch (Exception fieldException) { + } catch (Exception) { //LOG.Error(fieldException); } //LOG.InfoFormat("Type information end."); diff --git a/GreenshotPlugin/Controls/OAuthLoginForm.cs b/GreenshotPlugin/Controls/OAuthLoginForm.cs index 405e9883a..0212a1e14 100644 --- a/GreenshotPlugin/Controls/OAuthLoginForm.cs +++ b/GreenshotPlugin/Controls/OAuthLoginForm.cs @@ -58,9 +58,11 @@ namespace GreenshotPlugin.Controls { this.Icon = GreenshotPlugin.Core.GreenshotResources.getGreenshotIcon(); this.Text = browserTitle; this.addressTextBox.Text = authorizationLink; + + browser.ScriptErrorsSuppressed = true; browser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(browser_DocumentCompleted); browser.Navigate(new Uri(authorizationLink)); - + WindowDetails.ToForeground(this.Handle); } diff --git a/GreenshotPlugin/Core/Cache.cs b/GreenshotPlugin/Core/Cache.cs index cfa4de59b..8cfa8c4ba 100644 --- a/GreenshotPlugin/Core/Cache.cs +++ b/GreenshotPlugin/Core/Cache.cs @@ -114,8 +114,18 @@ namespace GreenshotPlugin.Core { /// /// public void Add(TK key, TV value) { + Add(key, value, null); + } + + /// + /// Add a value to the cache + /// + /// + /// + /// optional value for the seconds to expire + public void Add(TK key, TV value, int? secondsToExpire) { lock (lockObject) { - var cachedItem = new CachedItem(key, value, secondsToExpire); + var cachedItem = new CachedItem(key, value, secondsToExpire.HasValue ? secondsToExpire.Value : this.secondsToExpire); cachedItem.Expired += delegate(TK cacheKey, TV cacheValue) { if (internalCache.ContainsKey(cacheKey)) { LOG.DebugFormat("Expiring object with Key: {0}", cacheKey);