Commit 67e7efa5 authored by Robin's avatar Robin
Browse files

This is WIP, doesn't work at ALL

parent 40d9ec90
#region Greenshot GNU General Public License
// Greenshot - a free and open source screenshot tool
// Copyright (C) 2007-2018 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 <http://www.gnu.org/licenses/>.
#endregion
using System;
using System.Threading;
using System.Threading.Tasks;
using Greenshot.Core.Interfaces;
namespace Greenshot.Core.Actions {
/// <summary>
/// This IAction implements the basic capture flow:
/// Source.import, Processor.process, Destination.export
/// </summary>
public class CaptureFlowAction : IAction {
/// <summary>
/// Source
/// </summary>
public ISource Source {
get;
set;
}
/// <summary>
/// Processor
/// </summary>
public IProcessor Processor {
get;
set;
}
/// <summary>
/// Destination
/// </summary>
public IDestination Destination {
get;
set;
}
/// <summary>
/// Content
/// </summary>
public CaptureContext Context {
get;
set;
}
/// <inheritdoc />
public async Task<bool> ExecuteAsync(CancellationToken cancellationToken = default) {
if (Context == null) {
throw new ArgumentException("Missing parameter", nameof(Context));
}
if (Source == null)
{
throw new ArgumentException("Missing parameter", nameof(Source));
}
if (Destination == null)
{
throw new ArgumentException("Missing parameter", nameof(Destination));
}
if (!await Source.ImportAsync(Context, cancellationToken)) {
return false;
}
if (Processor != null) {
await Processor.ProcessAsync(Context, cancellationToken);
}
return await Destination.ExportAsync(Context, cancellationToken);
}
}
}
#region Greenshot GNU General Public License
// Greenshot - a free and open source screenshot tool
// Copyright (C) 2007-2018 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 <http://www.gnu.org/licenses/>.
#endregion
using System;
using System.Collections.Generic;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Dapplo.Windows.Common.Structs;
using Greenshot.Addons.Core;
using Greenshot.Core.Interfaces;
namespace Greenshot.Core
{
/// <summary>
/// This class contains everything on a capture, from the moment of the capture itself to the export
/// </summary>
public class CaptureContext {
private readonly Dictionary<string, string> _metaData = new Dictionary<string, string>();
private string _filenamePattern;
private string _filename;
private string _title;
private DateTimeOffset _captureTaken = new DateTimeOffset();
/// <summary>
/// the actual screen capture
/// </summary>
public IElement<BitmapSource> Capture {
get;
set;
}
/// <summary>
/// the cursor
/// </summary>
public IElement<BitmapSource> MouseCursor {
get;
set;
}
/// <summary>
/// This is used to crop the capture to a certain location/size
/// </summary>
public NativeRect CropRect {
get;
set;
}
/// <summary>
/// This can be used for a region-capture which is not rectangle based
/// </summary>
public Geometry ClipArea {
get;
set;
}
/// <summary>
/// When using this value, the default (from CoreConfiguration) FilenamePattern is given unless a value was set
/// </summary>
public string FilenamePattern {
get {
if (_filenamePattern != null) {
return _filenamePattern;
}
return Conf.OutputFileFilenamePattern;
}
set {
_filenamePattern = value;
}
}
/// <summary>
/// When using this value, the default (from FilenamePattern) Filename is given unless a value was set
/// The filename is without extension!!
/// </summary>
public string Filename {
get {
if (_filename != null) {
return _filename;
}
return FilenameHelper.GetFilenameWithoutExtensionFromPattern(FilenamePattern, this);
}
set {
_filename = value;
}
}
/// <summary>
/// Title of the capture, this can e.g. be the title of the selected window.
/// </summary>
public string Title {
get {
return _title;
}
set {
_title = value;
}
}
/// <summary>
/// Template to use, unless overridden by the destination
/// </summary>
public string Template {
get;
set;
}
/// <summary>
/// The time that the capture was taken
/// </summary>
public DateTimeOffset DateTime {
get {
return _captureTaken;
}
set {
_captureTaken = value;
}
}
/// <summary>
/// Special meta-data for this capture
/// </summary>
public Dictionary<string, string> MetaData {
get {
return _metaData;
}
}
/// <summary>
/// Add to the meta-data for this capture
/// </summary>
public void AddMetaData(string key, string value) {
if (_metaData.ContainsKey(key)) {
_metaData[key] = value;
} else {
_metaData.Add(key, value);
}
}
}
}
/*
* 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 Sourceforge: http://sourceforge.net/projects/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 <http://www.gnu.org/licenses/>.
*/
using Greenshot.Plugin.Modules;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Greenshot.Plugin.Modules.Interfaces;
namespace Greenshot.Modules {
/// <summary>
/// This is the Clipboard destination
/// </summary>
[Destination(Designation = "Clipboard", DescriptionLanguageKey = "settings_destination_clipboard", IconFilename="clipboard")]
public class ClipboardDestination : IDestination {
public Task<bool> ExportAsync(CaptureContext captureContext, CancellationToken token = default(CancellationToken)) {
// Convert from CaptureContext to image
using (var stream = new MemoryStream()) {
captureContext.ToStream(stream);
// TODO: extend the clipboard helper with something to export the capture
//ClipboardHelper.SetClipboardData(captureContext);
}
return Task.FromResult<bool>(true);
}
}
}
/*
* 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 Sourceforge: http://sourceforge.net/projects/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 <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using Greenshot.Addons.Core;
using Greenshot.Core;
using Greenshot.Core.Encoders;
using Greenshot.Core.Interfaces;
namespace Greenshot.Modules {
[Destination(Designation = "DestinationPicker", DescriptionLanguageKey = "settings_destination_picker", IconFilename="menu")]
public class DestinationPicker : IDestination {
[ImportMany]
private IEnumerable<Lazy<IDestination, IDestinationMetadata>> _destinations = null;
public Task<bool> ExportAsync(CaptureContext captureContext, CancellationToken token = default(CancellationToken)) {
ContextMenu menu = new ContextMenu();
menu.Placement = PlacementMode.Mouse;
var sortedDestination = from destination in _destinations
where !destination.Metadata.Designation.Contains("Picker")
orderby destination.Metadata.Designation
select destination;
foreach (var destination in sortedDestination) {
string translation;
MenuItem destinationItem = new MenuItem();
if (Language.TryGetString(destination.Metadata.DescriptionLanguageKey, out translation)) {
destinationItem.Header = translation;
} else {
destinationItem.Header = destination.Metadata.DescriptionLanguageKey;
}
var iconDisplayer = new IconDisplayer();
iconDisplayer.IconName = destination.Metadata.IconFilename;
iconDisplayer.Width = 24;
iconDisplayer.Height = 24;
destinationItem.Icon = iconDisplayer;
var eventDestination = destination.Value;
destinationItem.Click += async (sender, eventArgs) => {
await eventDestination.ExportAsync(captureContext, token);
};
menu.Items.Add(destinationItem);
}
menu.Items.Add(new Separator());
MenuItem exitItem = new MenuItem();
exitItem.Header = Language.GetString("editor_close");
var exitIconDisplayer = new IconDisplayer();
exitIconDisplayer.IconName = "exit";
exitIconDisplayer.Width = 30;
exitIconDisplayer.Height = 30;
exitItem.Icon = exitIconDisplayer;
menu.Items.Add(exitItem);
menu.IsOpen = true;
menu.Placement = PlacementMode.MousePoint;
return Task.FromResult<bool>(true);
}
}
}
/*
* 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 Sourceforge: http://sourceforge.net/projects/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 <http://www.gnu.org/licenses/>.
*/
using Greenshot.Helpers;
using Greenshot.Plugin.Modules;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Greenshot.Plugin.Modules.Interfaces;
namespace Greenshot.Modules {
/// <summary>
/// The EmailDestination, this is exported during startup if a MAPI client is found.
/// </summary>
public class EmailDestination : IDestination {
public Task<bool> ExportAsync(CaptureContext captureContext, CancellationToken token = default(CancellationToken)) {
bool createdFile = false;
string imageFile = Path.Combine(@"C:\LocalData", captureContext.Filename + ".png");
try {
using (var fileStream = new FileStream(imageFile, FileMode.OpenOrCreate)) {
createdFile = true;
captureContext.ToStream(fileStream);
MapiMailMessage.SendImage(imageFile, captureContext);
}
} finally {
// Cleanup imageFile if we created it here, so less tmp-files are generated and left
if (createdFile && File.Exists(imageFile)) {
File.Delete(imageFile);
}
}
return Task.FromResult<bool>(true);
}
}
}
\ No newline at end of file
/*
* 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 Sourceforge: http://sourceforge.net/projects/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 <http://www.gnu.org/licenses/>.
*/
using Greenshot.Plugin.Core;
using Greenshot.Plugin.Modules;
using System.ComponentModel.Composition;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Greenshot.Plugin.Modules.Interfaces;
namespace Greenshot.Modules {
/// <summary>
/// This is the File destination
/// </summary>
[Destination(Designation = "File", DescriptionLanguageKey = "settings_destination_file", IconFilename = "file")]
public class FileDestination : IDestination {
private static readonly NLog.Logger LOG = NLog.LogManager.GetCurrentClassLogger();
[Import]
private CoreConfiguration coreConfiguration = null;
public Task<bool> ExportAsync(CaptureContext captureContext, CancellationToken token = default(CancellationToken)) {
string filename = FilenameHelper.GetFilenameFromPattern(coreConfiguration.OutputFileFilenamePattern, coreConfiguration.OutputFileFormat, captureContext);
string filepath = FilenameHelper.FillVariables(coreConfiguration.OutputFilePath, false);
string combinedPath = Path.Combine(filepath, filename);
// Convert from CaptureContext to image
using (var stream = new FileStream(combinedPath, FileMode.OpenOrCreate, FileAccess.Write)) {
captureContext.ToStream(stream);
LOG.Info("Wrote file to {0}", combinedPath);
}
return Task.FromResult<bool>(true);
}
}
}
/*
* 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 Sourceforge: http://sourceforge.net/projects/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 <http://www.gnu.org/licenses/>.
*/
using Greenshot.Windows;
using Greenshot.Plugin.Modules;
using System.ComponentModel.Composition;
using System.Threading.Tasks;
using System.Threading;
using Greenshot.Plugin.Modules.Interfaces;
namespace Greenshot.Modules {
[Destination(Designation = "PreviewDestinationPicker", DescriptionLanguageKey = "settings_destination_picker", IconFilename = "preview")]
public class PreviewDestinationPicker : IDestination {
[Import]
private ExportFactory<DestinationPickerWindow> _destinationPickerWindowFactory = null;
public Task<bool> ExportAsync(CaptureContext captureContext, CancellationToken token = default(CancellationToken)) {
var picker = _destinationPickerWindowFactory.CreateExport().Value;
picker.CaptureContext = captureContext;
var result = picker.ShowDialog();
if (result.HasValue && result.Value) {
// Do something
}
return Task.FromResult<bool>(true);
}
}
}
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Threading;
namespace Greenshot.Core.Encoders
{
public class IconBitmapEncoder : DispatcherObject {
private readonly IconBitmapFramesCollection _frames = new IconBitmapFramesCollection();
/// <summary>
///
/// </summary>
public IList<BitmapFrame> Frames => _frames;
/// <summary>
///
/// </summary>
/// <param name="stream"></param>
public void Save(Stream stream) {
_frames.SortAscending();
BinaryWriter writer = new BinaryWriter(stream, Encoding.UTF32);
ushort framesCount = Convert.ToUInt16(_frames.Count);
const ushort fileHeaderLength = 6;
const ushort frameHeaderLength = 16;
IconDir fileHeader = new IconDir(framesCount);
writer.Write(fileHeader.idReserved);
writer.Write(fileHeader.idType);
writer.Write(fileHeader.idCount);
byte[][] data = new byte[framesCount][];
foreach (var frame in _frames) {
int frameIndex = _frames.IndexOf(frame);
if (frame.PixelWidth == 256) {
data[frameIndex] = GetPngData(frame);
} else {
data[frameIndex] = GetBmpData(frame);
}
}
uint frameDataOffset = fileHeaderLength;
frameDataOffset += (uint)(frameHeaderLength * framesCount);
foreach (var frame in _frames) {
int frameIndex = _frames.IndexOf(frame);
if (frameIndex > 0) {
frameDataOffset += Convert.ToUInt32(data[frameIndex - 1].Length);
}
IconDirEntry frameHeader = new IconDirEntry((ushort)frame.PixelWidth, (ushort)frame.PixelHeight, Convert.ToUInt16(frame.Format.BitsPerPixel), Convert.ToUInt32(data[frameIndex].Length), frameDataOffset);
writer.Write(frameHeader.bWidth);
writer.Write(frameHeader.bHeight);
writer.Write(frameHeader.bColorCount);
writer.Write(frameHeader.bReserved);
writer.Write(frameHeader.wPlanes);