Code quality fixes (NullReference checks, unused variables etc)

This commit is contained in:
RKrom 2014-04-26 00:34:06 +02:00
commit ac08533727
99 changed files with 1252 additions and 1312 deletions

View file

@ -20,18 +20,17 @@
*/
using System.ComponentModel;
using System.Drawing;
using Greenshot.IniFile;
using Greenshot.Plugin;
using GreenshotPlugin.Core;
using log4net;
namespace GreenshotBoxPlugin {
public class BoxDestination : AbstractDestination {
private static log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(BoxDestination));
private static BoxConfiguration config = IniConfig.GetIniSection<BoxConfiguration>();
private static ILog LOG = LogManager.GetLogger(typeof(BoxDestination));
private BoxPlugin plugin = null;
private readonly BoxPlugin _plugin;
public BoxDestination(BoxPlugin plugin) {
this.plugin = plugin;
_plugin = plugin;
}
public override string Designation {
@ -54,8 +53,8 @@ namespace GreenshotBoxPlugin {
}
public override ExportInformation ExportCapture(bool manuallyInitiated, ISurface surface, ICaptureDetails captureDetails) {
ExportInformation exportInformation = new ExportInformation(this.Designation, this.Description);
string uploadUrl = plugin.Upload(captureDetails, surface);
ExportInformation exportInformation = new ExportInformation(Designation, Description);
string uploadUrl = _plugin.Upload(captureDetails, surface);
if (uploadUrl != null) {
exportInformation.ExportMade = true;
exportInformation.Uri = uploadUrl;

View file

@ -35,11 +35,11 @@ namespace GreenshotBoxPlugin {
/// </summary>
public class BoxPlugin : IGreenshotPlugin {
private static readonly log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(BoxPlugin));
private static BoxConfiguration config;
private static BoxConfiguration _config;
public static PluginAttribute Attributes;
private IGreenshotHost host;
private ComponentResourceManager resources;
private ToolStripMenuItem itemPlugInConfig;
private IGreenshotHost _host;
private ComponentResourceManager _resources;
private ToolStripMenuItem _itemPlugInConfig;
public void Dispose() {
Dispose(true);
@ -48,16 +48,13 @@ namespace GreenshotBoxPlugin {
protected virtual void Dispose(bool disposing) {
if (disposing) {
if (itemPlugInConfig != null) {
itemPlugInConfig.Dispose();
itemPlugInConfig = null;
if (_itemPlugInConfig != null) {
_itemPlugInConfig.Dispose();
_itemPlugInConfig = null;
}
}
}
public BoxPlugin() {
}
public IEnumerable<IDestination> Destinations() {
yield return new BoxDestination(this);
}
@ -70,29 +67,30 @@ namespace GreenshotBoxPlugin {
/// <summary>
/// Implementation of the IGreenshotPlugin.Initialize
/// </summary>
/// <param name="host">Use the IGreenshotPluginHost interface to register events</param>
/// <param name="pluginHost">Use the IGreenshotPluginHost interface to register events</param>
/// <param name="pluginAttribute">My own attributes</param>
public virtual bool Initialize(IGreenshotHost pluginHost, PluginAttribute myAttributes) {
this.host = (IGreenshotHost)pluginHost;
Attributes = myAttributes;
public virtual bool Initialize(IGreenshotHost pluginHost, PluginAttribute pluginAttribute) {
_host = pluginHost;
Attributes = pluginAttribute;
// Register configuration (don't need the configuration itself)
config = IniConfig.GetIniSection<BoxConfiguration>();
resources = new ComponentResourceManager(typeof(BoxPlugin));
_config = IniConfig.GetIniSection<BoxConfiguration>();
_resources = new ComponentResourceManager(typeof(BoxPlugin));
itemPlugInConfig = new ToolStripMenuItem();
itemPlugInConfig.Image = (Image)resources.GetObject("Box");
itemPlugInConfig.Text = Language.GetString("box", LangKey.Configure);
itemPlugInConfig.Click += new System.EventHandler(ConfigMenuClick);
_itemPlugInConfig = new ToolStripMenuItem {
Image = (Image) _resources.GetObject("Box"),
Text = Language.GetString("box", LangKey.Configure)
};
_itemPlugInConfig.Click += ConfigMenuClick;
PluginUtils.AddToContextMenu(host, itemPlugInConfig);
Language.LanguageChanged += new LanguageChangedHandler(OnLanguageChanged);
PluginUtils.AddToContextMenu(_host, _itemPlugInConfig);
Language.LanguageChanged += OnLanguageChanged;
return true;
}
public void OnLanguageChanged(object sender, EventArgs e) {
if (itemPlugInConfig != null) {
itemPlugInConfig.Text = Language.GetString("box", LangKey.Configure);
if (_itemPlugInConfig != null) {
_itemPlugInConfig.Text = Language.GetString("box", LangKey.Configure);
}
}
@ -104,7 +102,7 @@ namespace GreenshotBoxPlugin {
/// Implementation of the IPlugin.Configure
/// </summary>
public virtual void Configure() {
config.ShowConfigDialog();
_config.ShowConfigDialog();
}
/// <summary>
@ -118,26 +116,26 @@ namespace GreenshotBoxPlugin {
}
public void ConfigMenuClick(object sender, EventArgs eventArgs) {
config.ShowConfigDialog();
_config.ShowConfigDialog();
}
/// <summary>
/// This will be called when the menu item in the Editor is clicked
/// </summary>
public string Upload(ICaptureDetails captureDetails, ISurface surfaceToUpload) {
SurfaceOutputSettings outputSettings = new SurfaceOutputSettings(config.UploadFormat, config.UploadJpegQuality, false);
SurfaceOutputSettings outputSettings = new SurfaceOutputSettings(_config.UploadFormat, _config.UploadJpegQuality, false);
try {
string url = null;
string filename = Path.GetFileName(FilenameHelper.GetFilename(config.UploadFormat, captureDetails));
string filename = Path.GetFileName(FilenameHelper.GetFilename(_config.UploadFormat, captureDetails));
SurfaceContainer imageToUpload = new SurfaceContainer(surfaceToUpload, outputSettings, filename);
new PleaseWaitForm().ShowAndWait(BoxPlugin.Attributes.Name, Language.GetString("box", LangKey.communication_wait),
delegate() {
new PleaseWaitForm().ShowAndWait(Attributes.Name, Language.GetString("box", LangKey.communication_wait),
delegate {
url = BoxUtils.UploadToBox(imageToUpload, captureDetails.Title, filename);
}
);
if (url != null && config.AfterUploadLinkToClipBoard) {
if (url != null && _config.AfterUploadLinkToClipBoard) {
ClipboardHelper.SetClipboardData(url);
}

View file

@ -57,7 +57,7 @@ namespace GreenshotBoxPlugin {
}
string authorizationResponse = PostAndReturn(new Uri(TokenUri), string.Format("grant_type=authorization_code&code={0}&client_id={1}&client_secret={2}", callbackParameters["code"], BoxCredentials.ClientId, BoxCredentials.ClientSecret));
var authorization = JSONSerializer.Deserialize<Authorization>(authorizationResponse);
var authorization = JsonSerializer.Deserialize<Authorization>(authorizationResponse);
Config.BoxToken = authorization.AccessToken;
IniConfig.Save();
@ -67,15 +67,16 @@ namespace GreenshotBoxPlugin {
/// <summary>
/// Download a url response as string
/// </summary>
/// <param name=url">An Uri to specify the download location</param>
/// <param name="url">An Uri to specify the download location</param>
/// <param name="postMessage"></param>
/// <returns>string with the file content</returns>
public static string PostAndReturn(Uri url, string postMessage) {
HttpWebRequest webRequest = (HttpWebRequest)NetworkHelper.CreateWebRequest(url);
webRequest.Method = "POST";
webRequest.KeepAlive = true;
webRequest.Credentials = System.Net.CredentialCache.DefaultCredentials;
webRequest.Credentials = CredentialCache.DefaultCredentials;
webRequest.ContentType = "application/x-www-form-urlencoded";
byte[] data = Encoding.UTF8.GetBytes(postMessage.ToString());
byte[] data = Encoding.UTF8.GetBytes(postMessage);
using (var requestStream = webRequest.GetRequestStream()) {
requestStream.Write(data, 0, data.Length);
}
@ -172,12 +173,12 @@ namespace GreenshotBoxPlugin {
IniConfig.Save();
continue;
}
var upload = JSONSerializer.Deserialize<Upload>(response);
var upload = JsonSerializer.Deserialize<Upload>(response);
if (upload == null || upload.Entries == null || upload.Entries.Count == 0) return null;
if (Config.UseSharedLink) {
string filesResponse = HttpPut(string.Format(FilesUri, upload.Entries[0].Id), "{\"shared_link\": {\"access\": \"open\"}}");
var file = JSONSerializer.Deserialize<FileEntry>(filesResponse);
var file = JsonSerializer.Deserialize<FileEntry>(filesResponse);
return file.SharedLink.Url;
}
return string.Format("http://www.box.com/files/0/f/0/1/f_{0}", upload.Entries[0].Id);
@ -187,7 +188,7 @@ namespace GreenshotBoxPlugin {
/// <summary>
/// A simple helper class for the DataContractJsonSerializer
/// </summary>
public static class JSONSerializer {
internal static class JsonSerializer {
/// <summary>
/// Helper method to serialize object to JSON
/// </summary>