Code cleanup, removed a lot of FxCop messages and added some more disposing.

This commit is contained in:
RKrom 2014-06-15 11:45:01 +02:00
commit 15253ef295
18 changed files with 119 additions and 335 deletions

View file

@ -40,7 +40,7 @@ namespace Greenshot.Helpers {
/// <summary>
/// CaptureHelper contains all the capture logic
/// </summary>
public class CaptureHelper {
public class CaptureHelper : IDisposable {
private static readonly ILog LOG = LogManager.GetLogger(typeof(CaptureHelper));
private static CoreConfiguration conf = IniConfig.GetIniSection<CoreConfiguration>();
// TODO: when we get the screen capture code working correctly, this needs to be enabled
@ -73,36 +73,48 @@ namespace Greenshot.Helpers {
if (disposing) {
// Cleanup
}
// Unfortunately we can't dispose the capture, this might still be used somewhere else.
_windows = null;
_selectedCaptureWindow = null;
_capture = null;
}
public static void CaptureClipboard() {
new CaptureHelper(CaptureMode.Clipboard).MakeCapture();
using (CaptureHelper captureHelper = new CaptureHelper(CaptureMode.Clipboard)) {
captureHelper.MakeCapture();
}
}
public static void CaptureRegion(bool captureMouse) {
new CaptureHelper(CaptureMode.Region, captureMouse).MakeCapture();
using (CaptureHelper captureHelper = new CaptureHelper(CaptureMode.Region, captureMouse)) {
captureHelper.MakeCapture();
}
}
public static void CaptureRegion(bool captureMouse, IDestination destination) {
CaptureHelper captureHelper = new CaptureHelper(CaptureMode.Region, captureMouse, destination);
captureHelper.MakeCapture();
using (CaptureHelper captureHelper = new CaptureHelper(CaptureMode.Region, captureMouse, destination)) {
captureHelper.MakeCapture();
}
}
public static void CaptureRegion(bool captureMouse, Rectangle region) {
new CaptureHelper(CaptureMode.Region, captureMouse).MakeCapture(region);
using (CaptureHelper captureHelper = new CaptureHelper(CaptureMode.Region, captureMouse)) {
captureHelper.MakeCapture(region);
}
}
public static void CaptureFullscreen(bool captureMouse, ScreenCaptureMode screenCaptureMode) {
CaptureHelper captureHelper = new CaptureHelper(CaptureMode.FullScreen, captureMouse);
captureHelper._screenCaptureMode = screenCaptureMode;
captureHelper.MakeCapture();
using (CaptureHelper captureHelper = new CaptureHelper(CaptureMode.FullScreen, captureMouse)) {
captureHelper._screenCaptureMode = screenCaptureMode;
captureHelper.MakeCapture();
}
}
public static void CaptureLastRegion(bool captureMouse) {
new CaptureHelper(CaptureMode.LastRegion, captureMouse).MakeCapture();
using (CaptureHelper captureHelper = new CaptureHelper(CaptureMode.LastRegion, captureMouse)) {
captureHelper.MakeCapture();
}
}
public static void CaptureIE(bool captureMouse, WindowDetails windowToCapture) {
CaptureHelper captureHelper = new CaptureHelper(CaptureMode.IE, captureMouse);
captureHelper.SelectedCaptureWindow = windowToCapture;
captureHelper.MakeCapture();
using (CaptureHelper captureHelper = new CaptureHelper(CaptureMode.IE, captureMouse)) {
captureHelper.SelectedCaptureWindow = windowToCapture;
captureHelper.MakeCapture();
}
}
public static void CaptureWindow(bool captureMouse) {
@ -110,27 +122,35 @@ namespace Greenshot.Helpers {
}
public static void CaptureWindow(WindowDetails windowToCapture) {
CaptureHelper captureHelper = new CaptureHelper(CaptureMode.ActiveWindow);
captureHelper.SelectedCaptureWindow = windowToCapture;
captureHelper.MakeCapture();
using (CaptureHelper captureHelper = new CaptureHelper(CaptureMode.ActiveWindow)) {
captureHelper.SelectedCaptureWindow = windowToCapture;
captureHelper.MakeCapture();
}
}
public static void CaptureWindowInteractive(bool captureMouse) {
new CaptureHelper(CaptureMode.Window, captureMouse).MakeCapture();
using (CaptureHelper captureHelper = new CaptureHelper(CaptureMode.Window)) {
captureHelper.MakeCapture();
}
}
public static void CaptureFile(string filename) {
new CaptureHelper(CaptureMode.File).MakeCapture(filename);
using (CaptureHelper captureHelper = new CaptureHelper(CaptureMode.File)) {
captureHelper.MakeCapture(filename);
}
}
public static void CaptureFile(string filename, IDestination destination) {
new CaptureHelper(CaptureMode.File).AddDestination(destination).MakeCapture(filename);
using (CaptureHelper captureHelper = new CaptureHelper(CaptureMode.File)) {
captureHelper.AddDestination(destination).MakeCapture(filename);
}
}
public static void ImportCapture(ICapture captureToImport) {
CaptureHelper captureHelper = new CaptureHelper(CaptureMode.File);
captureHelper._capture = captureToImport;
captureHelper.HandleCapture();
using (CaptureHelper captureHelper = new CaptureHelper(CaptureMode.File)) {
captureHelper._capture = captureToImport;
captureHelper.HandleCapture();
}
}
public CaptureHelper AddDestination(IDestination destination) {

View file

@ -18,6 +18,11 @@
* 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.IniFile;
using Greenshot.Plugin;
using GreenshotPlugin.Core;
using log4net;
using System;
using System.Collections;
using System.Collections.Generic;
@ -26,44 +31,40 @@ using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
using Greenshot.Plugin;
using GreenshotPlugin.Core;
using Greenshot.IniFile;
/// <summary>
/// Author: Andrew Baker
/// Datum: 10.03.2006
/// Available from: http://www.vbusers.com/codecsharp/codeget.asp?ThreadID=71&PostID=1
/// </summary>
using log4net;
namespace Greenshot.Helpers {
/// <summary>
/// Author: Andrew Baker
/// Datum: 10.03.2006
/// Available from: http://www.vbusers.com/codecsharp/codeget.asp?ThreadID=71&PostID=1
/// </summary>
#region Public MapiMailMessage Class
/// <summary>
/// Represents an email message to be sent through MAPI.
/// </summary>
public class MapiMailMessage {
public class MapiMailMessage : IDisposable {
private static readonly ILog LOG = LogManager.GetLogger(typeof(MapiMailMessage));
private static CoreConfiguration conf = IniConfig.GetIniSection<CoreConfiguration>();
private static readonly CoreConfiguration conf = IniConfig.GetIniSection<CoreConfiguration>();
/// <summary>
/// Helper Method for creating an Email with Attachment
/// </summary>
/// <param name="fullpath">Path to file</param>
/// <param name="captureDetails"></param>
/// <param name="fullPath">Path to file</param>
/// <param name="title"></param>
public static void SendImage(string fullPath, string title) {
MapiMailMessage message = new MapiMailMessage(title, null);
message.Files.Add(fullPath);
if (!string.IsNullOrEmpty(conf.MailApiTo)) {
message._recipientCollection.Add(new Recipient(conf.MailApiTo, RecipientType.To));
using (MapiMailMessage message = new MapiMailMessage(title, null)) {
message.Files.Add(fullPath);
if (!string.IsNullOrEmpty(conf.MailApiTo)) {
message._recipientCollection.Add(new Recipient(conf.MailApiTo, RecipientType.To));
}
if (!string.IsNullOrEmpty(conf.MailApiCC)) {
message._recipientCollection.Add(new Recipient(conf.MailApiCC, RecipientType.CC));
}
if (!string.IsNullOrEmpty(conf.MailApiBCC)) {
message._recipientCollection.Add(new Recipient(conf.MailApiBCC, RecipientType.BCC));
}
message.ShowDialog();
}
if (!string.IsNullOrEmpty(conf.MailApiCC)) {
message._recipientCollection.Add(new Recipient(conf.MailApiCC, RecipientType.CC));
}
if (!string.IsNullOrEmpty(conf.MailApiBCC)) {
message._recipientCollection.Add(new Recipient(conf.MailApiBCC, RecipientType.BCC));
}
message.ShowDialog();
}
@ -133,8 +134,8 @@ namespace Greenshot.Helpers {
private string _subject;
private string _body;
private RecipientCollection _recipientCollection;
private List<string> _files;
private ManualResetEvent _manualResetEvent;
private readonly List<string> _files;
private readonly ManualResetEvent _manualResetEvent;
#endregion Member Variables
@ -230,10 +231,25 @@ namespace Greenshot.Helpers {
_manualResetEvent.Reset();
}
public void Dispose() {
Dispose(true);
GC.SuppressFinalize(this);
}
#endregion Public Methods
#region Private Methods
protected virtual void Dispose(bool disposing) {
if (!disposing) {
return;
}
if (_manualResetEvent != null) {
_manualResetEvent.Close();
}
}
/// <summary>
/// Sends the mail message.
/// </summary>
@ -504,7 +520,7 @@ namespace Greenshot.Helpers {
public IntPtr EntryID = IntPtr.Zero;
}
[DllImport("MAPI32.DLL", SetLastError = true, CharSet=CharSet.Unicode)]
[DllImport("MAPI32.DLL", SetLastError = true, CharSet=CharSet.Ansi)]
public static extern int MAPISendMail(IntPtr session, IntPtr hwnd, MapiMessage message, int flg, int rsv);
#endregion Structs