/* * Greenshot - a free and open source screenshot tool * Copyright (C) 2007-2015 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 Greenshot.Plugin; using GreenshotPlugin.Core; using Greenshot.IniFile; using log4net; namespace Greenshot.Helpers { /// /// Description of DestinationHelper. /// public static class DestinationHelper { private static readonly ILog LOG = LogManager.GetLogger(typeof(DestinationHelper)); private static readonly Dictionary RegisteredDestinations = new Dictionary(); private static readonly CoreConfiguration coreConfig = IniConfig.GetIniSection(); /// Initialize the destinations static DestinationHelper() { foreach(Type destinationType in InterfaceUtils.GetSubclassesOf(typeof(IDestination),true)) { // Only take our own if (!"Greenshot.Destinations".Equals(destinationType.Namespace)) { continue; } if (!destinationType.IsAbstract) { IDestination destination; try { destination = (IDestination)Activator.CreateInstance(destinationType); } catch (Exception e) { LOG.ErrorFormat("Can't create instance of {0}", destinationType); LOG.Error(e); continue; } if (destination.isActive) { LOG.DebugFormat("Found destination {0} with designation {1}", destinationType.Name, destination.Designation); RegisterDestination(destination); } else { LOG.DebugFormat("Ignoring destination {0} with designation {1}", destinationType.Name, destination.Designation); } } } } /// /// Register your destination here, if it doesn't come from a plugin and needs to be available /// /// public static void RegisterDestination(IDestination destination) { if (coreConfig.ExcludeDestinations == null || !coreConfig.ExcludeDestinations.Contains(destination.Designation)) { // don't test the key, an exception should happen wenn it's not unique RegisteredDestinations.Add(destination.Designation, destination); } } /// /// Method to get all the destinations from the plugins /// /// List private static List GetPluginDestinations() { List destinations = new List(); foreach (PluginAttribute pluginAttribute in PluginHelper.Instance.Plugins.Keys) { IGreenshotPlugin plugin = PluginHelper.Instance.Plugins[pluginAttribute]; try { foreach (IDestination destination in plugin.Destinations()) { if (coreConfig.ExcludeDestinations == null || !coreConfig.ExcludeDestinations.Contains(destination.Designation)) { destinations.Add(destination); } } } catch (Exception ex) { LOG.ErrorFormat("Couldn't get destinations from the plugin {0}", pluginAttribute.Name); LOG.Error(ex); } } destinations.Sort(); return destinations; } /// /// Get a list of all destinations, registered or supplied by a plugin /// /// public static List GetAllDestinations() { List destinations = new List(); destinations.AddRange(RegisteredDestinations.Values); destinations.AddRange(GetPluginDestinations()); destinations.Sort(); return destinations; } /// /// Get a destination by a designation /// /// Designation of the destination /// IDestination or null public static IDestination GetDestination(string designation) { if (designation == null) { return null; } if (RegisteredDestinations.ContainsKey(designation)) { return RegisteredDestinations[designation]; } foreach (IDestination destination in GetPluginDestinations()) { if (designation.Equals(destination.Designation)) { return destination; } } return null; } /// /// A simple helper method which will call ExportCapture for the destination with the specified designation /// /// /// /// public static ExportInformation ExportCapture(bool manuallyInitiated, string designation, ISurface surface, ICaptureDetails captureDetails) { IDestination destination = GetDestination(designation); if (destination != null && destination.isActive) { return destination.ExportCapture(manuallyInitiated, surface, captureDetails); } return null; } } }