Moving back to trunk!

git-svn-id: http://svn.code.sf.net/p/greenshot/code/trunk@1602 7dccd23d-a4a3-4e1f-8c07-b4c1b4018ab4
This commit is contained in:
RKrom 2012-01-24 19:24:36 +00:00
parent ad265b2c54
commit 8d458998a1
332 changed files with 17647 additions and 9466 deletions

View file

@ -20,6 +20,7 @@
*/
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Net;
@ -28,16 +29,19 @@ using System.Threading;
using Greenshot.Plugin;
using GreenshotPlugin.Core;
using GreenshotPlugin.UnmanagedHelpers;
using IniFile;
namespace GreenshotRemotePlugin {
class Server {
public class Server {
private static log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(Server));
private static CoreConfiguration conf = IniConfig.GetIniSection<CoreConfiguration>();
private string url;
private string accessKey;
private volatile bool keepGoing = true;
private HttpListener listener = null;
private ICaptureHost captureHost;
private IGreenshotPluginHost host;
private IGreenshotHost host;
public Server(string url, string accessKey) {
this.url = url;
this.accessKey = accessKey;
@ -49,23 +53,23 @@ namespace GreenshotRemotePlugin {
}
public void StopListening() {
keepGoing = false;
Close();
}
public void SetCaptureHost(ICaptureHost captureHost) {
this.captureHost = captureHost;
}
public void SetGreenshotPluginHost(IGreenshotPluginHost host) {
public void SetGreenshotPluginHost(IGreenshotHost host) {
this.host = host;
}
private void Listen() {
listener = new HttpListener();
//listener.AuthenticationSchemes = AuthenticationSchemes.Ntlm;
listener.Prefixes.Add(url);
if (!listener.Prefixes.Contains(url)) {
listener.Prefixes.Add(url);
}
listener.Start();
LOG.DebugFormat("Listening on: {0}", url);
while (true) {
while (true) {
IAsyncResult result = listener.BeginGetContext(new AsyncCallback(ListenerCallback), listener);
while(!result.AsyncWaitHandle.WaitOne(1000)) {
if (!keepGoing) {
@ -77,15 +81,17 @@ namespace GreenshotRemotePlugin {
Close();
return;
}
}
}
}
private void Close() {
LOG.Debug("Cleaning up HttpListener");
listener.Stop();
listener.Prefixes.Remove(url);
listener.Close();
listener = null;
if (listener != null) {
LOG.Debug("Cleaning up HttpListener");
listener.Stop();
listener.Prefixes.Remove(url);
listener.Close();
listener = null;
}
}
private void ListenerCallback(IAsyncResult result) {
@ -134,11 +140,13 @@ namespace GreenshotRemotePlugin {
restored = true;
}
LOG.DebugFormat("Capturing window of class: {0}", captureWindow.ClassName);
using (Bitmap image = captureWindow.PrintWindow()) {
if (image != null) {
ICapture capture = null;
try {
capture = CaptureWindow(captureWindow, null, conf.WindowCaptureMode);
if (capture.Image != null) {
using (MemoryStream stream = new MemoryStream()) {
host.SaveToStream(image, stream, OutputFormat.png, 100);
host.SaveToStream(capture.Image, stream, OutputFormat.png, 100);
byte [] buffer = stream.GetBuffer();
response.ContentType = "image/png";
response.ContentLength64 = buffer.Length;
@ -149,12 +157,18 @@ namespace GreenshotRemotePlugin {
} else {
LOG.Debug("null image??");
}
} finally {
if (capture != null) {
capture.Dispose();
capture = null;
}
}
if (restored) {
captureWindow.Iconic = true;
}
} catch (Exception e) {
byte[] errorBuffer = Encoding.UTF8.GetBytes(e.StackTrace);
LOG.Error(e);
byte[] errorBuffer = Encoding.UTF8.GetBytes("An error occured...");
response.ContentLength64 = errorBuffer.Length;
response.OutputStream.Write(errorBuffer, 0, errorBuffer.Length);
response.OutputStream.Close();
@ -169,14 +183,12 @@ namespace GreenshotRemotePlugin {
if (user != null) {
sb.Append("Hello " + user + " ");
}
List<WindowDetails>windows = WindowDetails.GetAllWindows();
List<WindowDetails>windows = WindowDetails.GetVisibleWindows();
foreach(WindowDetails window in windows) {
if (window.Text.Length > 0 && window.Visible && !window.ClassName.StartsWith("Progman")) {
sb.Append("<A HREF=\"capture?handle=" + window.Handle +"&key="+ request.QueryString["key"] + "\">");
sb.Append(window.Text);
sb.Append("</A>");
sb.AppendLine("<br/>");
}
sb.Append("<A HREF=\"capture?handle=" + window.Handle +"&key="+ request.QueryString["key"] + "\">");
sb.Append(window.Text);
sb.Append("</A>");
sb.AppendLine("<br/>");
}
sb.Append("</body>");
sb.Append("</html>");
@ -185,5 +197,112 @@ namespace GreenshotRemotePlugin {
response.OutputStream.Write(b, 0, b.Length);
response.OutputStream.Close();
}
/// <summary>
/// Capture the supplied Window
/// </summary>
/// <param name="windowToCapture">Window to capture</param>
/// <param name="captureForWindow">The capture to store the details</param>
/// <param name="windowCaptureMode">What WindowCaptureMode to use</param>
/// <returns></returns>
public static ICapture CaptureWindow(WindowDetails windowToCapture, ICapture captureForWindow, WindowCaptureMode windowCaptureMode) {
if (captureForWindow == null) {
captureForWindow = new Capture();
}
Rectangle windowRectangle = windowToCapture.WindowRectangle;
if (windowToCapture.Iconic) {
// Restore the window making sure it's visible!
windowToCapture.Restore();
}
// When Vista & DWM (Aero) enabled
bool dwmEnabled = DWM.isDWMEnabled();
// get process name to be able to exclude certain processes from certain capture modes
Process process = windowToCapture.Process;
bool isAutoMode = windowCaptureMode == WindowCaptureMode.Auto;
// For WindowCaptureMode.Auto we check:
// 1) Is window IE, use IE Capture
// 2) Is Windows >= Vista & DWM enabled: use DWM
// 3) Otherwise use GDI (Screen might be also okay but might lose content)
if (isAutoMode) {
// Take default screen
windowCaptureMode = WindowCaptureMode.Screen;
// Change to GDI, if allowed
if (conf.isGDIAllowed(process)) {
windowCaptureMode = WindowCaptureMode.GDI;
}
// Change to DWM, if enabled and allowed
if (dwmEnabled) {
if (conf.isDWMAllowed(process)) {
windowCaptureMode = WindowCaptureMode.Aero;
}
}
} else if (windowCaptureMode == WindowCaptureMode.Aero || windowCaptureMode == WindowCaptureMode.AeroTransparent) {
if (!dwmEnabled || !conf.isDWMAllowed(process)) {
// Take default screen
windowCaptureMode = WindowCaptureMode.Screen;
// Change to GDI, if allowed
if (conf.isGDIAllowed(process)) {
windowCaptureMode = WindowCaptureMode.GDI;
}
}
} else if (windowCaptureMode == WindowCaptureMode.GDI && !conf.isGDIAllowed(process)) {
// GDI not allowed, take screen
windowCaptureMode = WindowCaptureMode.Screen;
}
LOG.DebugFormat("Capturing window with mode {0}", windowCaptureMode);
bool captureTaken = false;
// Try to capture
while (!captureTaken) {
if (windowCaptureMode == WindowCaptureMode.GDI) {
ICapture tmpCapture = null;
if (conf.isGDIAllowed(process)) {
tmpCapture = windowToCapture.CaptureWindow(captureForWindow);
}
if (tmpCapture != null) {
captureForWindow = tmpCapture;
captureTaken = true;
} else {
// A problem, try Screen
windowCaptureMode = WindowCaptureMode.Screen;
}
} else if (windowCaptureMode == WindowCaptureMode.Aero || windowCaptureMode == WindowCaptureMode.AeroTransparent) {
ICapture tmpCapture = null;
if (conf.isDWMAllowed(process)) {
tmpCapture = windowToCapture.CaptureDWMWindow(captureForWindow, windowCaptureMode, isAutoMode);
}
if (tmpCapture != null) {
captureForWindow = tmpCapture;
captureTaken = true;
} else {
// A problem, try GDI
windowCaptureMode = WindowCaptureMode.GDI;
}
} else {
// Screen capture
windowRectangle.Intersect(captureForWindow.ScreenBounds);
try {
captureForWindow = WindowCapture.CaptureRectangle(captureForWindow, windowRectangle);
captureTaken = true;
} catch (Exception e) {
LOG.Error("Problem capturing", e);
return null;
}
}
}
if (captureForWindow != null && windowToCapture != null && captureForWindow.Image != null) {
captureForWindow.CaptureDetails.Title = windowToCapture.Text;
using (Graphics graphics = Graphics.FromHwnd(windowToCapture.Handle)) {
((Bitmap)captureForWindow.Image).SetResolution(graphics.DpiX, graphics.DpiY);
}
}
return captureForWindow;
}
}
}