Refactored PluginUtils to have the GetExePath and GetExeIcon, which was needed for giving the ExternalCommandPlugin an icon.

git-svn-id: http://svn.code.sf.net/p/greenshot/code/trunk@2057 7dccd23d-a4a3-4e1f-8c07-b4c1b4018ab4
This commit is contained in:
RKrom 2012-09-19 10:05:26 +00:00
commit 91385b074e
13 changed files with 119 additions and 83 deletions

View file

@ -69,12 +69,12 @@ namespace Greenshot.Destinations {
mailIcon = GreenshotPlugin.Core.GreenshotResources.getImage("Email.Image"); mailIcon = GreenshotPlugin.Core.GreenshotResources.getImage("Email.Image");
if (isOutlookUsed) { if (isOutlookUsed) {
exePath = GetExePath("OUTLOOK.EXE"); exePath = PluginUtils.GetExePath("OUTLOOK.EXE");
if (exePath != null && File.Exists(exePath)) { if (exePath != null && File.Exists(exePath)) {
applicationIcon = GetExeIcon(exePath, 0); applicationIcon = PluginUtils.GetExeIcon(exePath, 0);
WindowDetails.AddProcessToExcludeFromFreeze("outlook"); WindowDetails.AddProcessToExcludeFromFreeze("outlook");
if (conf.OutlookAllowExportInMeetings) { if (conf.OutlookAllowExportInMeetings) {
meetingIcon = GetExeIcon(exePath, 2); meetingIcon = PluginUtils.GetExeIcon(exePath, 2);
} }
} else { } else {
exePath = null; exePath = null;
@ -151,7 +151,7 @@ namespace Greenshot.Destinations {
if (OlObjectClass.olAppointment.Equals(outlookInspectorType)) { if (OlObjectClass.olAppointment.Equals(outlookInspectorType)) {
// Make sure we loaded the icon, maybe the configuration has been changed! // Make sure we loaded the icon, maybe the configuration has been changed!
if (meetingIcon == null) { if (meetingIcon == null) {
meetingIcon = GetExeIcon(exePath, 2); meetingIcon = PluginUtils.GetExeIcon(exePath, 2);
} }
return meetingIcon; return meetingIcon;
} else { } else {

View file

@ -44,10 +44,10 @@ namespace Greenshot.Destinations {
private string workbookName = null; private string workbookName = null;
static ExcelDestination() { static ExcelDestination() {
exePath = GetExePath("EXCEL.EXE"); exePath = PluginUtils.GetExePath("EXCEL.EXE");
if (exePath != null && File.Exists(exePath)) { if (exePath != null && File.Exists(exePath)) {
applicationIcon = GetExeIcon(exePath, 0); applicationIcon = PluginUtils.GetExeIcon(exePath, 0);
workbookIcon = GetExeIcon(exePath, 1); workbookIcon = PluginUtils.GetExeIcon(exePath, 1);
WindowDetails.AddProcessToExcludeFromFreeze("excel"); WindowDetails.AddProcessToExcludeFromFreeze("excel");
} else { } else {
exePath = null; exePath = null;

View file

@ -43,10 +43,10 @@ namespace Greenshot.Destinations {
private OneNotePage page = null; private OneNotePage page = null;
static OneNoteDestination() { static OneNoteDestination() {
exePath = GetExePath("ONENOTE.EXE"); exePath = PluginUtils.GetExePath("ONENOTE.EXE");
if (exePath != null && File.Exists(exePath)) { if (exePath != null && File.Exists(exePath)) {
applicationIcon = GetExeIcon(exePath, 0); applicationIcon = PluginUtils.GetExeIcon(exePath, 0);
notebookIcon = GetExeIcon(exePath, 0); notebookIcon = PluginUtils.GetExeIcon(exePath, 0);
WindowDetails.AddProcessToExcludeFromFreeze("onenote"); WindowDetails.AddProcessToExcludeFromFreeze("onenote");
} else { } else {
exePath = null; exePath = null;

View file

@ -44,10 +44,10 @@ namespace Greenshot.Destinations {
private string presentationName = null; private string presentationName = null;
static PowerpointDestination() { static PowerpointDestination() {
exePath = GetExePath("POWERPNT.EXE"); exePath = PluginUtils.GetExePath("POWERPNT.EXE");
if (exePath != null && File.Exists(exePath)) { if (exePath != null && File.Exists(exePath)) {
applicationIcon = GetExeIcon(exePath, 0); applicationIcon = PluginUtils.GetExeIcon(exePath, 0);
presentationIcon = GetExeIcon(exePath, 1); presentationIcon = PluginUtils.GetExeIcon(exePath, 1);
WindowDetails.AddProcessToExcludeFromFreeze("powerpnt"); WindowDetails.AddProcessToExcludeFromFreeze("powerpnt");
} else { } else {
exePath = null; exePath = null;

View file

@ -45,10 +45,10 @@ namespace Greenshot.Destinations {
private string documentCaption = null; private string documentCaption = null;
static WordDestination() { static WordDestination() {
exePath = GetExePath("WINWORD.EXE"); exePath = PluginUtils.GetExePath("WINWORD.EXE");
if (exePath != null && File.Exists(exePath)) { if (exePath != null && File.Exists(exePath)) {
applicationIcon = GetExeIcon(exePath, 0); applicationIcon = PluginUtils.GetExeIcon(exePath, 0);
documentIcon = GetExeIcon(exePath, 1); documentIcon = PluginUtils.GetExeIcon(exePath, 1);
} else { } else {
exePath = null; exePath = null;
} }

View file

@ -224,6 +224,7 @@ All Greenshot features still work directly from the tray icon context menu witho
<resource name="expertsettings_suppresssavedialogatclose">Suppress the save dialog when closing the editor</resource> <resource name="expertsettings_suppresssavedialogatclose">Suppress the save dialog when closing the editor</resource>
<resource name="expertsettings_thumbnailpreview">Show window thumbnails in context menu (for Vista and windows 7)</resource> <resource name="expertsettings_thumbnailpreview">Show window thumbnails in context menu (for Vista and windows 7)</resource>
<resource name="exported_to">Exported to: {0}</resource> <resource name="exported_to">Exported to: {0}</resource>
<resource name="exported_to_error">An error occured while exporting to {0}:</resource>
<resource name="hotkey_fullscreen">Capture screen</resource> <resource name="hotkey_fullscreen">Capture screen</resource>
<resource name="hotkey_ie">Capture Internet Explorer</resource> <resource name="hotkey_ie">Capture Internet Explorer</resource>
<resource name="hotkey_lastregion">Capture last region</resource> <resource name="hotkey_lastregion">Capture last region</resource>

View file

@ -49,7 +49,7 @@ namespace ExternalCommand {
private static bool hasPaintDotNet = false; private static bool hasPaintDotNet = false;
static ExternalCommandConfiguration() { static ExternalCommandConfiguration() {
try { try {
paintPath = AbstractDestination.GetExePath("pbrush.exe"); paintPath = PluginUtils.GetExePath("pbrush.exe");
hasPaint = !string.IsNullOrEmpty(paintPath) && File.Exists(paintPath); hasPaint = !string.IsNullOrEmpty(paintPath) && File.Exists(paintPath);
paintDotNetPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), @"Paint.NET\PaintDotNet.exe"); paintDotNetPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), @"Paint.NET\PaintDotNet.exe");
hasPaintDotNet = !string.IsNullOrEmpty(paintDotNetPath) && File.Exists(paintDotNetPath); hasPaintDotNet = !string.IsNullOrEmpty(paintDotNetPath) && File.Exists(paintDotNetPath);

View file

@ -24,6 +24,8 @@ using System.Diagnostics;
using System.IO; using System.IO;
using Greenshot.Plugin; using Greenshot.Plugin;
using Greenshot.IniFile; using Greenshot.IniFile;
using System.Windows.Forms;
using GreenshotPlugin.Core;
namespace ExternalCommand { namespace ExternalCommand {
/// <summary> /// <summary>
@ -65,6 +67,18 @@ namespace ExternalCommand {
} }
this.host = pluginHost; this.host = pluginHost;
this.myAttributes = myAttributes; this.myAttributes = myAttributes;
ToolStripMenuItem itemPlugInRoot = new ToolStripMenuItem();
itemPlugInRoot.Text = Language.GetString("externalcommand", "contextmenu_configure");
itemPlugInRoot.Tag = host;
string exePath = PluginUtils.GetExePath("cmd.exe");
if (exePath != null && File.Exists(exePath)) {
itemPlugInRoot.Image = PluginUtils.GetExeIcon(exePath, 0);
}
itemPlugInRoot.Click += new System.EventHandler(ConfigMenuClick);
PluginUtils.AddToContextMenu(host, itemPlugInRoot);
return true; return true;
} }
@ -72,6 +86,10 @@ namespace ExternalCommand {
LOG.Debug("Shutdown of " + myAttributes.Name); LOG.Debug("Shutdown of " + myAttributes.Name);
} }
private void ConfigMenuClick(object sender, EventArgs eventArgs) {
Configure();
}
/// <summary> /// <summary>
/// Implementation of the IPlugin.Configure /// Implementation of the IPlugin.Configure
/// </summary> /// </summary>

View file

@ -18,7 +18,7 @@ namespace ExternalCommand {
Image icon = null; Image icon = null;
if (File.Exists(config.commandlines[exepath])) { if (File.Exists(config.commandlines[exepath])) {
try { try {
icon = AbstractDestination.GetExeIcon(config.commandlines[exepath], 0); icon = PluginUtils.GetExeIcon(config.commandlines[exepath], 0);
} catch (Exception ex) { } catch (Exception ex) {
LOG.Warn("Problem loading icon for " + config.commandlines[exepath], ex); LOG.Warn("Problem loading icon for " + config.commandlines[exepath], ex);
} }

View file

@ -1,7 +1,10 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<language description="English" ietf="en-US" version="1.0.0"> <language description="English" ietf="en-US" version="1.0.0">
<resources> <resources>
<resource name="settings_title"> <resource name="contextmenu_configure">
Configure external commands
</resource>
<resource name="settings_title">
External command settings External command settings
</resource> </resource>
<resource name="settings_detail_title"> <resource name="settings_detail_title">

View file

@ -43,9 +43,9 @@ namespace GreenshotOfficeCommunicatorPlugin {
private CommunicatorConversation conversation = null; private CommunicatorConversation conversation = null;
static OfficeCommunicatorDestination() { static OfficeCommunicatorDestination() {
exePath = GetExePath("communicator.exe"); exePath = PluginUtils.GetExePath("communicator.exe");
if (exePath != null && File.Exists(exePath)) { if (exePath != null && File.Exists(exePath)) {
applicationIcon = GetExeIcon(exePath, 0); applicationIcon = PluginUtils.GetExeIcon(exePath, 0);
} else { } else {
exePath = null; exePath = null;
} }

View file

@ -36,42 +36,9 @@ namespace GreenshotPlugin.Core {
/// Description of AbstractDestination. /// Description of AbstractDestination.
/// </summary> /// </summary>
public abstract class AbstractDestination : IDestination { public abstract class AbstractDestination : IDestination {
private const string PATH_KEY = @"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\";
private static readonly log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(AbstractDestination)); private static readonly log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(AbstractDestination));
private static CoreConfiguration configuration = IniConfig.GetIniSection<CoreConfiguration>(); private static CoreConfiguration configuration = IniConfig.GetIniSection<CoreConfiguration>();
public static string GetExePath(string exeName) {
using (RegistryKey key = Registry.LocalMachine.OpenSubKey(PATH_KEY + exeName, false)) {
if (key != null) {
// "" is the default key, which should point to the outlook location
return (string)key.GetValue("");
}
}
return null;
}
/// <summary>
/// Internaly used to create an icon
/// </summary>
/// <param name="path">path to the exe or dll</param>
/// <param name="index">index of the icon</param>
/// <returns>Bitmap with the icon or null if something happended</returns>
public static Bitmap GetExeIcon(string path, int index) {
if (!File.Exists(path)) {
return null;
}
try {
using (Icon appIcon = ImageHelper.ExtractAssociatedIcon(path, index, false)) {
if (appIcon != null) {
return appIcon.ToBitmap();
}
}
} catch (Exception exIcon) {
LOG.Error("error retrieving icon: ", exIcon);
}
return null;
}
public virtual int CompareTo(object obj) { public virtual int CompareTo(object obj) {
IDestination other = obj as IDestination; IDestination other = obj as IDestination;
if (other == null) { if (other == null) {

View file

@ -23,12 +23,59 @@ using System.Drawing;
using System.Windows.Forms; using System.Windows.Forms;
using Greenshot.Plugin; using Greenshot.Plugin;
using Microsoft.Win32;
using System.IO;
namespace GreenshotPlugin.Core { namespace GreenshotPlugin.Core {
/// <summary> /// <summary>
/// Description of PluginUtils. /// Description of PluginUtils.
/// </summary> /// </summary>
public static class PluginUtils { public static class PluginUtils {
private static readonly log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(PluginUtils));
private const string PATH_KEY = @"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\";
/// <summary>
/// Get the path of an executable
/// </summary>
/// <param name="exeName">e.g. cmd.exe</param>
/// <returns>Path to file</returns>
public static string GetExePath(string exeName) {
using (RegistryKey key = Registry.LocalMachine.OpenSubKey(PATH_KEY + exeName, false)) {
if (key != null) {
// "" is the default key, which should point to the requested location
return (string)key.GetValue("");
}
}
foreach (string test in (Environment.GetEnvironmentVariable("PATH") ?? "").Split(';')) {
string path = test.Trim();
if (!String.IsNullOrEmpty(path) && File.Exists(path = Path.Combine(path, exeName))) {
return Path.GetFullPath(path);
}
}
return null;
}
/// <summary>
/// Get icon for executable
/// </summary>
/// <param name="path">path to the exe or dll</param>
/// <param name="index">index of the icon</param>
/// <returns>Bitmap with the icon or null if something happended</returns>
public static Bitmap GetExeIcon(string path, int index) {
if (!File.Exists(path)) {
return null;
}
try {
using (Icon appIcon = ImageHelper.ExtractAssociatedIcon(path, index, false)) {
if (appIcon != null) {
return appIcon.ToBitmap();
}
}
} catch (Exception exIcon) {
LOG.Error("error retrieving icon: ", exIcon);
}
return null;
}
/// <summary> /// <summary>
/// Helper method to add a MenuItem to the File MenuItem of an ImageEditor /// Helper method to add a MenuItem to the File MenuItem of an ImageEditor