diff --git a/GreenshotFlickrPlugin/FlickrDestination.cs b/GreenshotFlickrPlugin/FlickrDestination.cs index 8393f4f9b..5c0f47fff 100644 --- a/GreenshotFlickrPlugin/FlickrDestination.cs +++ b/GreenshotFlickrPlugin/FlickrDestination.cs @@ -20,15 +20,14 @@ */ using System.ComponentModel; using System.Drawing; -using Greenshot.IniFile; using Greenshot.Plugin; using GreenshotPlugin.Core; +using log4net; namespace GreenshotFlickrPlugin { public class FlickrDestination : AbstractDestination { - private static log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(FlickrDestination)); - private static FlickrConfiguration config = IniConfig.GetIniSection(); - private FlickrPlugin plugin = null; + private static ILog LOG = LogManager.GetLogger(typeof(FlickrDestination)); + private FlickrPlugin plugin; public FlickrDestination(FlickrPlugin plugin) { this.plugin = plugin; } @@ -53,8 +52,8 @@ namespace GreenshotFlickrPlugin { } public override ExportInformation ExportCapture(bool manuallyInitiated, ISurface surface, ICaptureDetails captureDetails) { - ExportInformation exportInformation = new ExportInformation(this.Designation, this.Description); - string uploadURL = null; + ExportInformation exportInformation = new ExportInformation(Designation, Description); + string uploadURL; bool uploaded = plugin.Upload(captureDetails, surface, out uploadURL); if (uploaded) { exportInformation.ExportMade = true; diff --git a/GreenshotFlickrPlugin/FlickrPlugin.cs b/GreenshotFlickrPlugin/FlickrPlugin.cs index ed8bc4c93..ba33e43b5 100644 --- a/GreenshotFlickrPlugin/FlickrPlugin.cs +++ b/GreenshotFlickrPlugin/FlickrPlugin.cs @@ -18,6 +18,7 @@ * 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 System.ComponentModel; @@ -28,6 +29,7 @@ using Greenshot.IniFile; using Greenshot.Plugin; using GreenshotPlugin.Controls; using GreenshotPlugin.Core; +using log4net; namespace GreenshotFlickrPlugin { @@ -35,12 +37,12 @@ namespace GreenshotFlickrPlugin /// This is the Flickr base code /// public class FlickrPlugin : IGreenshotPlugin { - private static readonly log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(FlickrPlugin)); - private static FlickrConfiguration config; + private static readonly ILog LOG = LogManager.GetLogger(typeof(FlickrPlugin)); + private static FlickrConfiguration _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,14 +50,14 @@ namespace GreenshotFlickrPlugin } protected virtual void Dispose(bool disposing) { - if (disposing) { - if (itemPlugInConfig != null) { - itemPlugInConfig.Dispose(); - itemPlugInConfig = null; - } + if (!disposing) { + return; } - } - public FlickrPlugin() { + if (_itemPlugInConfig == null) { + return; + } + _itemPlugInConfig.Dispose(); + _itemPlugInConfig = null; } public IEnumerable Destinations() { @@ -70,31 +72,31 @@ namespace GreenshotFlickrPlugin /// /// Implementation of the IGreenshotPlugin.Initialize /// - /// Use the IGreenshotPluginHost interface to register events + /// Use the IGreenshotPluginHost interface to register events /// My own attributes - 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(); - resources = new ComponentResourceManager(typeof(FlickrPlugin)); + _config = IniConfig.GetIniSection(); + _resources = new ComponentResourceManager(typeof(FlickrPlugin)); - itemPlugInConfig = new ToolStripMenuItem(); - itemPlugInConfig.Text = Language.GetString("flickr", LangKey.Configure); - itemPlugInConfig.Tag = host; - itemPlugInConfig.Image = (Image)resources.GetObject("flickr"); - itemPlugInConfig.Click += new System.EventHandler(ConfigMenuClick); + _itemPlugInConfig = new ToolStripMenuItem(); + _itemPlugInConfig.Text = Language.GetString("flickr", LangKey.Configure); + _itemPlugInConfig.Tag = _host; + _itemPlugInConfig.Image = (Image)_resources.GetObject("flickr"); + _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("flickr", LangKey.Configure); + if (_itemPlugInConfig != null) { + _itemPlugInConfig.Text = Language.GetString("flickr", LangKey.Configure); } } @@ -106,7 +108,7 @@ namespace GreenshotFlickrPlugin /// Implementation of the IPlugin.Configure /// public virtual void Configure() { - config.ShowConfigDialog(); + _config.ShowConfigDialog(); } /// @@ -120,17 +122,17 @@ namespace GreenshotFlickrPlugin } public void ConfigMenuClick(object sender, EventArgs eventArgs) { - config.ShowConfigDialog(); + _config.ShowConfigDialog(); } public bool Upload(ICaptureDetails captureDetails, ISurface surface, out String uploadUrl) { - SurfaceOutputSettings outputSettings = new SurfaceOutputSettings(config.UploadFormat, config.UploadJpegQuality, false); + SurfaceOutputSettings outputSettings = new SurfaceOutputSettings(_config.UploadFormat, _config.UploadJpegQuality, false); uploadUrl = null; try { string flickrUrl = null; new PleaseWaitForm().ShowAndWait(Attributes.Name, Language.GetString("flickr", LangKey.communication_wait), - delegate() { - string filename = Path.GetFileName(FilenameHelper.GetFilename(config.UploadFormat, captureDetails)); + delegate { + string filename = Path.GetFileName(FilenameHelper.GetFilename(_config.UploadFormat, captureDetails)); flickrUrl = FlickrUtils.UploadToFlickr(surface, outputSettings, captureDetails.Title, filename); } ); @@ -140,7 +142,7 @@ namespace GreenshotFlickrPlugin } uploadUrl = flickrUrl; - if (config.AfterUploadLinkToClipBoard) { + if (_config.AfterUploadLinkToClipBoard) { ClipboardHelper.SetClipboardData(flickrUrl); } return true; diff --git a/GreenshotFlickrPlugin/FlickrUtils.cs b/GreenshotFlickrPlugin/FlickrUtils.cs index 4c1b73534..4e4fd694e 100644 --- a/GreenshotFlickrPlugin/FlickrUtils.cs +++ b/GreenshotFlickrPlugin/FlickrUtils.cs @@ -18,6 +18,7 @@ * 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 System.Drawing; @@ -25,32 +26,43 @@ using System.Xml; using Greenshot.IniFile; using Greenshot.Plugin; using GreenshotPlugin.Core; +using log4net; namespace GreenshotFlickrPlugin { /// /// Description of FlickrUtils. /// public class FlickrUtils { - private static readonly log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(FlickrUtils)); + private static readonly ILog LOG = LogManager.GetLogger(typeof(FlickrUtils)); private static FlickrConfiguration config = IniConfig.GetIniSection(); - - private FlickrUtils() { - } - + private const string FLICKR_API_BASE_URL = "https://api.flickr.com/services/"; + private const string FLICKR_UPLOAD_URL = FLICKR_API_BASE_URL + "upload/"; + // OAUTH + private const string FLICKR_OAUTH_BASE_URL = FLICKR_API_BASE_URL + "oauth/"; + private const string FLICKR_ACCESS_TOKEN_URL = FLICKR_OAUTH_BASE_URL + "access_token"; + private const string FLICKR_AUTHORIZE_URL = FLICKR_OAUTH_BASE_URL + "authorize"; + private const string FLICKR_REQUEST_TOKEN_URL = FLICKR_OAUTH_BASE_URL + "request_token"; + private const string FLICKR_FARM_URL = "https://farm{0}.staticflickr.com/{1}/{2}_{3}.jpg"; + // REST + private const string FLICKR_REST_URL = FLICKR_API_BASE_URL + "rest/"; + private const string FLICKR_GET_INFO_URL = FLICKR_REST_URL + "?method=flickr.photos.getInfo"; /// /// Do the actual upload to Flickr /// For more details on the available parameters, see: http://flickrnet.codeplex.com /// - /// byte[] with image data + /// + /// + /// + /// /// url to image public static string UploadToFlickr(ISurface surfaceToUpload, SurfaceOutputSettings outputSettings, string title, string filename) { OAuthSession oAuth = new OAuthSession(FlickrCredentials.ConsumerKey, FlickrCredentials.ConsumerSecret); oAuth.BrowserSize = new Size(520, 800); oAuth.CheckVerifier = false; - oAuth.AccessTokenUrl = "http://api.flickr.com/services/oauth/access_token"; - oAuth.AuthorizeUrl = "http://api.flickr.com/services/oauth/authorize"; - oAuth.RequestTokenUrl = "http://api.flickr.com/services/oauth/request_token"; + oAuth.AccessTokenUrl = FLICKR_ACCESS_TOKEN_URL; + oAuth.AuthorizeUrl = FLICKR_AUTHORIZE_URL; + oAuth.RequestTokenUrl = FLICKR_REQUEST_TOKEN_URL; oAuth.LoginTitle = "Flickr authorization"; oAuth.Token = config.FlickrToken; oAuth.TokenSecret = config.FlickrTokenSecret; @@ -77,12 +89,12 @@ namespace GreenshotFlickrPlugin { signedParameters.Add("hidden", config.HiddenFromSearch ? "1" : "2"); IDictionary otherParameters = new Dictionary(); otherParameters.Add("photo", new SurfaceContainer(surfaceToUpload, outputSettings, filename)); - string response = oAuth.MakeOAuthRequest(HTTPMethod.POST, "http://api.flickr.com/services/upload/", signedParameters, otherParameters, null); + string response = oAuth.MakeOAuthRequest(HTTPMethod.POST, FLICKR_UPLOAD_URL, signedParameters, otherParameters, null); string photoId = GetPhotoId(response); // Get Photo Info signedParameters = new Dictionary { { "photo_id", photoId } }; - string photoInfo = oAuth.MakeOAuthRequest(HTTPMethod.POST, "http://api.flickr.com/services/rest/?method=flickr.photos.getInfo", signedParameters, null, null); + string photoInfo = oAuth.MakeOAuthRequest(HTTPMethod.POST, FLICKR_GET_INFO_URL, signedParameters, null, null); return GetUrl(photoInfo); } catch (Exception ex) { LOG.Error("Upload error: ", ex); @@ -104,7 +116,10 @@ namespace GreenshotFlickrPlugin { if (config.UsePageLink) { XmlNodeList nodes = doc.GetElementsByTagName("url"); if (nodes.Count > 0) { - return nodes.Item(0).InnerText; + var xmlNode = nodes.Item(0); + if (xmlNode != null) { + return xmlNode.InnerText; + } } } else { XmlNodeList nodes = doc.GetElementsByTagName("photo"); @@ -116,7 +131,7 @@ namespace GreenshotFlickrPlugin { string serverId = item.Attributes["server"].Value; string photoId = item.Attributes["id"].Value; string secret = item.Attributes["secret"].Value; - return string.Format("http://farm{0}.staticflickr.com/{1}/{2}_{3}.jpg", farmId, serverId, photoId, secret); + return string.Format(FLICKR_FARM_URL, farmId, serverId, photoId, secret); } } @@ -134,7 +149,10 @@ namespace GreenshotFlickrPlugin { doc.LoadXml(response); XmlNodeList nodes = doc.GetElementsByTagName("photoid"); if (nodes.Count > 0) { - return nodes.Item(0).InnerText; + var xmlNode = nodes.Item(0); + if (xmlNode != null) { + return xmlNode.InnerText; + } } } catch (Exception ex) { LOG.Error("Error parsing Flickr Response.", ex); diff --git a/GreenshotFlickrPlugin/Forms/SettingsForm.cs b/GreenshotFlickrPlugin/Forms/SettingsForm.cs index 4d0d7536e..b20083182 100644 --- a/GreenshotFlickrPlugin/Forms/SettingsForm.cs +++ b/GreenshotFlickrPlugin/Forms/SettingsForm.cs @@ -37,15 +37,15 @@ namespace GreenshotFlickrPlugin { // The InitializeComponent() call is required for Windows Forms designer support. // InitializeComponent(); - this.Icon = GreenshotPlugin.Core.GreenshotResources.getGreenshotIcon(); + Icon = GreenshotResources.getGreenshotIcon(); } void ButtonOKClick(object sender, EventArgs e) { - this.DialogResult = DialogResult.OK; + DialogResult = DialogResult.OK; } - void ButtonCancelClick(object sender, System.EventArgs e) { - this.DialogResult = DialogResult.Cancel; + void ButtonCancelClick(object sender, EventArgs e) { + DialogResult = DialogResult.Cancel; } } }