/*
* Greenshot - a free and open source screenshot tool
* Copyright (C) 2007-2020 Thomas Braun, Jens Klingen, Robin Krom
*
* For more information see: http://getgreenshot.org/
* The Greenshot project is hosted on GitHub https://github.com/greenshot/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 .
*/
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;
using Greenshot.Plugin;
using GreenshotPlugin.Core;
using Greenshot.IniFile;
using log4net;
namespace Greenshot.Helpers {
///
/// The PluginHelper takes care of all plugin related functionality
///
[Serializable]
public class PluginHelper : IGreenshotHost {
private static readonly ILog Log = LogManager.GetLogger(typeof(PluginHelper));
private static readonly CoreConfiguration CoreConfig = IniConfig.GetIniSection();
private static readonly string PluginPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),Application.ProductName);
private static readonly string ApplicationPath = Path.GetDirectoryName(Application.ExecutablePath);
private static readonly string PafPath = Path.Combine(Application.StartupPath, @"App\Greenshot");
public static PluginHelper Instance { get; } = new PluginHelper();
public void Shutdown() {
foreach(var plugin in SimpleServiceProvider.Current.GetAllInstances()) {
plugin.Shutdown();
plugin.Dispose();
}
}
///
/// Add plugins to the ListView
///
///
public void FillListView(ListView listView) {
foreach (var plugin in SimpleServiceProvider.Current.GetAllInstances())
{
var pluginAttribute = plugin.GetType().GetCustomAttribute();
var item = new ListViewItem(pluginAttribute.Name)
{
Tag = pluginAttribute
};
var assembly = plugin.GetType().Assembly;
var company = assembly.GetCustomAttribute();
item.SubItems.Add(assembly.GetName().Version.ToString());
item.SubItems.Add(company.Company);
item.SubItems.Add(assembly.Location);
listView.Items.Add(item);
}
}
public bool IsSelectedItemConfigurable(ListView listView) {
if (listView.SelectedItems.Count <= 0)
{
return false;
}
var pluginAttribute = (PluginAttribute)listView.SelectedItems[0].Tag;
return pluginAttribute != null && pluginAttribute.Configurable;
}
public void ConfigureSelectedItem(ListView listView) {
if (listView.SelectedItems.Count <= 0)
{
return;
}
var pluginAttribute = (PluginAttribute)listView.SelectedItems[0].Tag;
if (pluginAttribute == null)
{
return;
}
var plugin = SimpleServiceProvider.Current.GetAllInstances().FirstOrDefault(p =>
p.GetType().GetCustomAttribute().Name == pluginAttribute.Name);
plugin?.Configure();
}
///
/// Create a Thumbnail
///
/// Image of which we need a Thumbnail
/// Thumbnail width
/// Thumbnail height
/// Image with Thumbnail
public Image GetThumbnail(Image image, int width, int height) {
return image.GetThumbnailImage(width, height, ThumbnailCallback, IntPtr.Zero);
}
///
/// Required for GetThumbnail, but not used
///
/// true
private bool ThumbnailCallback() {
return true;
}
public ExportInformation ExportCapture(bool manuallyInitiated, string designation, ISurface surface, ICaptureDetails captureDetails) {
return DestinationHelper.ExportCapture(manuallyInitiated, designation, surface, captureDetails);
}
///
/// Make Capture with specified Handler
///
/// bool false if the mouse should not be captured, true if the configuration should be checked
/// IDestination
public void CaptureRegion(bool captureMouseCursor, IDestination destination) {
CaptureHelper.CaptureRegion(captureMouseCursor, destination);
}
///
/// Use the supplied image, and handle it as if it's captured.
///
/// Image to handle
public void ImportCapture(ICapture captureToImport)
{
var mainForm = SimpleServiceProvider.Current.GetInstance