This commit is contained in:
RKrom 2014-05-11 22:30:19 +02:00
parent 77a92d98c3
commit 7f72e7a3d7
4 changed files with 76 additions and 57 deletions

View file

@ -20,15 +20,14 @@
*/ */
using System.ComponentModel; using System.ComponentModel;
using System.Drawing; using System.Drawing;
using Greenshot.IniFile;
using Greenshot.Plugin; using Greenshot.Plugin;
using GreenshotPlugin.Core; using GreenshotPlugin.Core;
using log4net;
namespace GreenshotFlickrPlugin { namespace GreenshotFlickrPlugin {
public class FlickrDestination : AbstractDestination { public class FlickrDestination : AbstractDestination {
private static log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(FlickrDestination)); private static ILog LOG = LogManager.GetLogger(typeof(FlickrDestination));
private static FlickrConfiguration config = IniConfig.GetIniSection<FlickrConfiguration>(); private FlickrPlugin plugin;
private FlickrPlugin plugin = null;
public FlickrDestination(FlickrPlugin plugin) { public FlickrDestination(FlickrPlugin plugin) {
this.plugin = plugin; this.plugin = plugin;
} }
@ -53,8 +52,8 @@ namespace GreenshotFlickrPlugin {
} }
public override ExportInformation ExportCapture(bool manuallyInitiated, ISurface surface, ICaptureDetails captureDetails) { public override ExportInformation ExportCapture(bool manuallyInitiated, ISurface surface, ICaptureDetails captureDetails) {
ExportInformation exportInformation = new ExportInformation(this.Designation, this.Description); ExportInformation exportInformation = new ExportInformation(Designation, Description);
string uploadURL = null; string uploadURL;
bool uploaded = plugin.Upload(captureDetails, surface, out uploadURL); bool uploaded = plugin.Upload(captureDetails, surface, out uploadURL);
if (uploaded) { if (uploaded) {
exportInformation.ExportMade = true; exportInformation.ExportMade = true;

View file

@ -18,6 +18,7 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
@ -28,6 +29,7 @@ using Greenshot.IniFile;
using Greenshot.Plugin; using Greenshot.Plugin;
using GreenshotPlugin.Controls; using GreenshotPlugin.Controls;
using GreenshotPlugin.Core; using GreenshotPlugin.Core;
using log4net;
namespace GreenshotFlickrPlugin namespace GreenshotFlickrPlugin
{ {
@ -35,12 +37,12 @@ namespace GreenshotFlickrPlugin
/// This is the Flickr base code /// This is the Flickr base code
/// </summary> /// </summary>
public class FlickrPlugin : IGreenshotPlugin { public class FlickrPlugin : IGreenshotPlugin {
private static readonly log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(FlickrPlugin)); private static readonly ILog LOG = LogManager.GetLogger(typeof(FlickrPlugin));
private static FlickrConfiguration config; private static FlickrConfiguration _config;
public static PluginAttribute Attributes; public static PluginAttribute Attributes;
private IGreenshotHost host; private IGreenshotHost _host;
private ComponentResourceManager resources; private ComponentResourceManager _resources;
private ToolStripMenuItem itemPlugInConfig; private ToolStripMenuItem _itemPlugInConfig;
public void Dispose() { public void Dispose() {
Dispose(true); Dispose(true);
@ -48,14 +50,14 @@ namespace GreenshotFlickrPlugin
} }
protected virtual void Dispose(bool disposing) { protected virtual void Dispose(bool disposing) {
if (disposing) { if (!disposing) {
if (itemPlugInConfig != null) { return;
itemPlugInConfig.Dispose();
itemPlugInConfig = null;
}
} }
} if (_itemPlugInConfig == null) {
public FlickrPlugin() { return;
}
_itemPlugInConfig.Dispose();
_itemPlugInConfig = null;
} }
public IEnumerable<IDestination> Destinations() { public IEnumerable<IDestination> Destinations() {
@ -70,31 +72,31 @@ namespace GreenshotFlickrPlugin
/// <summary> /// <summary>
/// Implementation of the IGreenshotPlugin.Initialize /// Implementation of the IGreenshotPlugin.Initialize
/// </summary> /// </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> /// <param name="pluginAttribute">My own attributes</param>
public virtual bool Initialize(IGreenshotHost pluginHost, PluginAttribute myAttributes) { public virtual bool Initialize(IGreenshotHost pluginHost, PluginAttribute pluginAttribute) {
this.host = (IGreenshotHost)pluginHost; _host = pluginHost;
Attributes = myAttributes; Attributes = pluginAttribute;
// Register configuration (don't need the configuration itself) // Register configuration (don't need the configuration itself)
config = IniConfig.GetIniSection<FlickrConfiguration>(); _config = IniConfig.GetIniSection<FlickrConfiguration>();
resources = new ComponentResourceManager(typeof(FlickrPlugin)); _resources = new ComponentResourceManager(typeof(FlickrPlugin));
itemPlugInConfig = new ToolStripMenuItem(); _itemPlugInConfig = new ToolStripMenuItem();
itemPlugInConfig.Text = Language.GetString("flickr", LangKey.Configure); _itemPlugInConfig.Text = Language.GetString("flickr", LangKey.Configure);
itemPlugInConfig.Tag = host; _itemPlugInConfig.Tag = _host;
itemPlugInConfig.Image = (Image)resources.GetObject("flickr"); _itemPlugInConfig.Image = (Image)_resources.GetObject("flickr");
itemPlugInConfig.Click += new System.EventHandler(ConfigMenuClick); _itemPlugInConfig.Click += ConfigMenuClick;
PluginUtils.AddToContextMenu(host, itemPlugInConfig); PluginUtils.AddToContextMenu(_host, _itemPlugInConfig);
Language.LanguageChanged += new LanguageChangedHandler(OnLanguageChanged); Language.LanguageChanged += OnLanguageChanged;
return true; return true;
} }
public void OnLanguageChanged(object sender, EventArgs e) { public void OnLanguageChanged(object sender, EventArgs e) {
if (itemPlugInConfig != null) { if (_itemPlugInConfig != null) {
itemPlugInConfig.Text = Language.GetString("flickr", LangKey.Configure); _itemPlugInConfig.Text = Language.GetString("flickr", LangKey.Configure);
} }
} }
@ -106,7 +108,7 @@ namespace GreenshotFlickrPlugin
/// Implementation of the IPlugin.Configure /// Implementation of the IPlugin.Configure
/// </summary> /// </summary>
public virtual void Configure() { public virtual void Configure() {
config.ShowConfigDialog(); _config.ShowConfigDialog();
} }
/// <summary> /// <summary>
@ -120,17 +122,17 @@ namespace GreenshotFlickrPlugin
} }
public void ConfigMenuClick(object sender, EventArgs eventArgs) { public void ConfigMenuClick(object sender, EventArgs eventArgs) {
config.ShowConfigDialog(); _config.ShowConfigDialog();
} }
public bool Upload(ICaptureDetails captureDetails, ISurface surface, out String uploadUrl) { 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; uploadUrl = null;
try { try {
string flickrUrl = null; string flickrUrl = null;
new PleaseWaitForm().ShowAndWait(Attributes.Name, Language.GetString("flickr", LangKey.communication_wait), new PleaseWaitForm().ShowAndWait(Attributes.Name, Language.GetString("flickr", LangKey.communication_wait),
delegate() { delegate {
string filename = Path.GetFileName(FilenameHelper.GetFilename(config.UploadFormat, captureDetails)); string filename = Path.GetFileName(FilenameHelper.GetFilename(_config.UploadFormat, captureDetails));
flickrUrl = FlickrUtils.UploadToFlickr(surface, outputSettings, captureDetails.Title, filename); flickrUrl = FlickrUtils.UploadToFlickr(surface, outputSettings, captureDetails.Title, filename);
} }
); );
@ -140,7 +142,7 @@ namespace GreenshotFlickrPlugin
} }
uploadUrl = flickrUrl; uploadUrl = flickrUrl;
if (config.AfterUploadLinkToClipBoard) { if (_config.AfterUploadLinkToClipBoard) {
ClipboardHelper.SetClipboardData(flickrUrl); ClipboardHelper.SetClipboardData(flickrUrl);
} }
return true; return true;

View file

@ -18,6 +18,7 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Drawing; using System.Drawing;
@ -25,32 +26,43 @@ using System.Xml;
using Greenshot.IniFile; using Greenshot.IniFile;
using Greenshot.Plugin; using Greenshot.Plugin;
using GreenshotPlugin.Core; using GreenshotPlugin.Core;
using log4net;
namespace GreenshotFlickrPlugin { namespace GreenshotFlickrPlugin {
/// <summary> /// <summary>
/// Description of FlickrUtils. /// Description of FlickrUtils.
/// </summary> /// </summary>
public class 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<FlickrConfiguration>(); private static FlickrConfiguration config = IniConfig.GetIniSection<FlickrConfiguration>();
private const string FLICKR_API_BASE_URL = "https://api.flickr.com/services/";
private FlickrUtils() { 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";
/// <summary> /// <summary>
/// Do the actual upload to Flickr /// Do the actual upload to Flickr
/// For more details on the available parameters, see: http://flickrnet.codeplex.com /// For more details on the available parameters, see: http://flickrnet.codeplex.com
/// </summary> /// </summary>
/// <param name="imageData">byte[] with image data</param> /// <param name="surfaceToUpload"></param>
/// <param name="outputSettings"></param>
/// <param name="title"></param>
/// <param name="filename"></param>
/// <returns>url to image</returns> /// <returns>url to image</returns>
public static string UploadToFlickr(ISurface surfaceToUpload, SurfaceOutputSettings outputSettings, string title, string filename) { public static string UploadToFlickr(ISurface surfaceToUpload, SurfaceOutputSettings outputSettings, string title, string filename) {
OAuthSession oAuth = new OAuthSession(FlickrCredentials.ConsumerKey, FlickrCredentials.ConsumerSecret); OAuthSession oAuth = new OAuthSession(FlickrCredentials.ConsumerKey, FlickrCredentials.ConsumerSecret);
oAuth.BrowserSize = new Size(520, 800); oAuth.BrowserSize = new Size(520, 800);
oAuth.CheckVerifier = false; oAuth.CheckVerifier = false;
oAuth.AccessTokenUrl = "http://api.flickr.com/services/oauth/access_token"; oAuth.AccessTokenUrl = FLICKR_ACCESS_TOKEN_URL;
oAuth.AuthorizeUrl = "http://api.flickr.com/services/oauth/authorize"; oAuth.AuthorizeUrl = FLICKR_AUTHORIZE_URL;
oAuth.RequestTokenUrl = "http://api.flickr.com/services/oauth/request_token"; oAuth.RequestTokenUrl = FLICKR_REQUEST_TOKEN_URL;
oAuth.LoginTitle = "Flickr authorization"; oAuth.LoginTitle = "Flickr authorization";
oAuth.Token = config.FlickrToken; oAuth.Token = config.FlickrToken;
oAuth.TokenSecret = config.FlickrTokenSecret; oAuth.TokenSecret = config.FlickrTokenSecret;
@ -77,12 +89,12 @@ namespace GreenshotFlickrPlugin {
signedParameters.Add("hidden", config.HiddenFromSearch ? "1" : "2"); signedParameters.Add("hidden", config.HiddenFromSearch ? "1" : "2");
IDictionary<string, object> otherParameters = new Dictionary<string, object>(); IDictionary<string, object> otherParameters = new Dictionary<string, object>();
otherParameters.Add("photo", new SurfaceContainer(surfaceToUpload, outputSettings, filename)); 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); string photoId = GetPhotoId(response);
// Get Photo Info // Get Photo Info
signedParameters = new Dictionary<string, object> { { "photo_id", photoId } }; signedParameters = new Dictionary<string, object> { { "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); return GetUrl(photoInfo);
} catch (Exception ex) { } catch (Exception ex) {
LOG.Error("Upload error: ", ex); LOG.Error("Upload error: ", ex);
@ -104,7 +116,10 @@ namespace GreenshotFlickrPlugin {
if (config.UsePageLink) { if (config.UsePageLink) {
XmlNodeList nodes = doc.GetElementsByTagName("url"); XmlNodeList nodes = doc.GetElementsByTagName("url");
if (nodes.Count > 0) { if (nodes.Count > 0) {
return nodes.Item(0).InnerText; var xmlNode = nodes.Item(0);
if (xmlNode != null) {
return xmlNode.InnerText;
}
} }
} else { } else {
XmlNodeList nodes = doc.GetElementsByTagName("photo"); XmlNodeList nodes = doc.GetElementsByTagName("photo");
@ -116,7 +131,7 @@ namespace GreenshotFlickrPlugin {
string serverId = item.Attributes["server"].Value; string serverId = item.Attributes["server"].Value;
string photoId = item.Attributes["id"].Value; string photoId = item.Attributes["id"].Value;
string secret = item.Attributes["secret"].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); doc.LoadXml(response);
XmlNodeList nodes = doc.GetElementsByTagName("photoid"); XmlNodeList nodes = doc.GetElementsByTagName("photoid");
if (nodes.Count > 0) { if (nodes.Count > 0) {
return nodes.Item(0).InnerText; var xmlNode = nodes.Item(0);
if (xmlNode != null) {
return xmlNode.InnerText;
}
} }
} catch (Exception ex) { } catch (Exception ex) {
LOG.Error("Error parsing Flickr Response.", ex); LOG.Error("Error parsing Flickr Response.", ex);

View file

@ -37,15 +37,15 @@ namespace GreenshotFlickrPlugin {
// The InitializeComponent() call is required for Windows Forms designer support. // The InitializeComponent() call is required for Windows Forms designer support.
// //
InitializeComponent(); InitializeComponent();
this.Icon = GreenshotPlugin.Core.GreenshotResources.getGreenshotIcon(); Icon = GreenshotResources.getGreenshotIcon();
} }
void ButtonOKClick(object sender, EventArgs e) { void ButtonOKClick(object sender, EventArgs e) {
this.DialogResult = DialogResult.OK; DialogResult = DialogResult.OK;
} }
void ButtonCancelClick(object sender, System.EventArgs e) { void ButtonCancelClick(object sender, EventArgs e) {
this.DialogResult = DialogResult.Cancel; DialogResult = DialogResult.Cancel;
} }
} }
} }