Permalink
Browse files

Correct management of gpgPath in the setting dialog box

  • Loading branch information...
twalrant committed Dec 3, 2009
1 parent bc4c910 commit 636cfca636c89f3e4a93049ce880e9cd6127ef58
Showing with 68 additions and 8 deletions.
  1. +17 −1 OutlookGnuPG/OutlookGnuPG.cs
  2. +4 −4 OutlookGnuPG/Settings.cs
  3. +47 −3 References/OpenPGP/src/GnuPG.cs
@@ -78,6 +78,11 @@ private void OutlookGnuPG_Startup(object sender, EventArgs e)
else
{
_gnuPg = new GnuPG(null, _settings.GnuPgPath);
+ if (!_gnuPg.BinaryExists())
+ {
+ _settings.GnuPgPath = string.Empty;
+ Settings(); // Prompt for GnuPG Path
+ }
}
_gnuPg.OutputType = OutputTypes.AsciiArmor;
@@ -1240,7 +1245,10 @@ internal IList<GnuKey> GetPrivateKeys()
internal IList<GnuKey> GetPrivateKeys(string gnuPgPath)
{
_gnuPg.BinaryPath = gnuPgPath;
- return GetPrivateKeys();
+ if ( _gnuPg.BinaryExists() )
+ return GetPrivateKeys();
+ else
+ return new List<GnuKey>();
}
public IList<GnuKey> GetKeys()
@@ -1303,6 +1311,14 @@ private string RemoveInvalidAka(string msg)
}
return result;
}
+
+ public bool ValidateGnuPath(string gnuPath)
+ {
+ if (_gnuPg != null)
+ return _gnuPg.BinaryExists(gnuPath);
+ else
+ return false;
+ }
#endregion
}
}
View
@@ -104,8 +104,8 @@ private void BrowseButton_Click(object sender, System.EventArgs e)
if (result != DialogResult.OK)
return;
- GnuPgPath = GnuPgExeFolderDialog.SelectedPath;
- ValidateGnuPath();
+ GnuPgPath = GnuPgExeFolderDialog.SelectedPath;
+ OkButton.Enabled = ValidateGnuPath();
}
private void PopulatePrivateKeys(bool gotGnu)
@@ -156,10 +156,10 @@ private bool ValidateGnuPath()
return false;
}
- if (!File.Exists(Path.Combine(GnuPgPath, "gpg.exe")))
+ if ( !Globals.OutlookGnuPG.ValidateGnuPath(GnuPgPath) )
{
// No gpg.exe found, complain!
- Errors.SetError(GnuPgExe, "No gpg.exe found in directory!");
+ Errors.SetError(GnuPgExe, "No gpg(2).exe found in directory!");
GnuPgExe.Dock = DockStyle.None;
GnuPgExe.Width = _originalExeWidth - 17;
return false;
@@ -122,6 +122,7 @@ public class GnuPG : IDisposable
private string _homePath;
private string _binaryPath;
+ private string _gpgExe;
private OutputTypes _outputType;
private int _timeout = 10000; // 10 seconds
private Process _proc;
@@ -281,7 +282,25 @@ public string HomePath
public string BinaryPath
{
get { return _binaryPath; }
- set { _binaryPath = value; }
+ set { _binaryPath = value; SetExe(); }
+ }
+
+ /// <summary>
+ /// Check if the required binary is available.
+ /// </summary>
+ /// <returns>true if present or false otherwise</returns>
+ public bool BinaryExists()
+ {
+ return File.Exists(_gpgExe);
+ }
+
+ public bool BinaryExists(string gnuPath)
+ {
+ if (File.Exists(Path.Combine(gnuPath, GPG2_EXECUTABLE)))
+ return true;
+ if (File.Exists(Path.Combine(gnuPath, GPG_EXECUTABLE)))
+ return true;
+ return false;
}
/// <summary>
@@ -428,14 +447,19 @@ private StreamReader GetCommand(string command)
options.Append(command);
+#if OFF
string gpgExe = Path.Combine(GetGPGInstallLocation(), GPG2_EXECUTABLE);
if (!File.Exists(gpgExe))
gpgExe = Path.Combine(GetGPGInstallLocation(), GPG_EXECUTABLE);
if (!File.Exists(gpgExe))
throw new GnuPGException(String.Format(CultureInfo.InvariantCulture, "Unable to find GPG.EXE. The path '{0}' does not contain the GPG(2).EXE executable program. If this path is incorrect, please set the binary path on the GnuPG object to the correct GnuPG directory", GetGPGInstallLocation()));
+#else
+ if (string.IsNullOrEmpty(_gpgExe))
+ throw new GnuPGException(String.Format(CultureInfo.InvariantCulture, "Unable to find GPG(2).EXE. The path '{0}' does not contain the GPG(2).EXE executable program. If this path is incorrect, please set the binary path on the GnuPG object to the correct GnuPG directory", GetGPGInstallLocation()));
+#endif
// create a process info object with command line options
- ProcessStartInfo procInfo = new ProcessStartInfo(gpgExe, options.ToString());
+ ProcessStartInfo procInfo = new ProcessStartInfo(_gpgExe, options.ToString());
// init the procInfo object
procInfo.CreateNoWindow = true;
@@ -444,6 +468,9 @@ private StreamReader GetCommand(string command)
procInfo.RedirectStandardOutput = true;
procInfo.RedirectStandardError = true;
+ if (_gpgExe.EndsWith(GPG2_EXECUTABLE))
+ procInfo.EnvironmentVariables.Add("LANG", "C");
+
if (command.Contains("utf-8"))
procInfo.StandardOutputEncoding = _outputDataEncoding = Encoding.UTF8;
else
@@ -592,14 +619,19 @@ private void ExecuteGPG(ActionTypes action, Stream inputStream, Stream outputStr
{
string gpgErrorText = string.Empty;
+#if OFF
string gpgExe = Path.Combine(GetGPGInstallLocation(), GPG2_EXECUTABLE);
if (!File.Exists(gpgExe))
gpgExe = Path.Combine(GetGPGInstallLocation(), GPG_EXECUTABLE);
if (!File.Exists(gpgExe))
throw new GnuPGException(String.Format(CultureInfo.InvariantCulture, "Unable to find GPG.EXE. The path '{0}' does not contain the GPG.EXE executable program. If this path is incorrect, please set the binary path on the GnuPG object to the correct GnuPG directory", GetGPGInstallLocation()));
+#else
+ if (string.IsNullOrEmpty(_gpgExe))
+ throw new GnuPGException(String.Format(CultureInfo.InvariantCulture, "Unable to find GPG(2).EXE. The path '{0}' does not contain the GPG(2).EXE executable program. If this path is incorrect, please set the binary path on the GnuPG object to the correct GnuPG directory", GetGPGInstallLocation()));
+#endif
// create a process info object with command line options
- ProcessStartInfo procInfo = new ProcessStartInfo(gpgExe, GetCmdLineSwitches(action));
+ ProcessStartInfo procInfo = new ProcessStartInfo(_gpgExe, GetCmdLineSwitches(action));
// init the procInfo object
procInfo.CreateNoWindow = true;
@@ -608,6 +640,9 @@ private void ExecuteGPG(ActionTypes action, Stream inputStream, Stream outputStr
procInfo.RedirectStandardOutput = true;
procInfo.RedirectStandardError = true;
+ if ( _gpgExe.EndsWith(GPG2_EXECUTABLE) )
+ procInfo.EnvironmentVariables.Add("LANG", "C");
+
try
{
// start the gpg process and get back a process start info object
@@ -724,8 +759,17 @@ private void CopyStream(Stream input, Stream output)
private void SetDefaults()
{
_outputType = OutputTypes.AsciiArmor;
+ SetExe();
}
+ private void SetExe()
+ {
+ _gpgExe = Path.Combine(GetGPGInstallLocation(), GPG2_EXECUTABLE);
+ if (!File.Exists(_gpgExe))
+ _gpgExe = Path.Combine(GetGPGInstallLocation(), GPG_EXECUTABLE);
+ if (!File.Exists(_gpgExe))
+ _gpgExe = string.Empty;
+ }
private void AsyncOutputReader()
{

0 comments on commit 636cfca

Please sign in to comment.