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.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<FlickrConfiguration>();
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;

View file

@ -18,6 +18,7 @@
* 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 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
/// </summary>
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;
}
if (_itemPlugInConfig == null) {
return;
}
}
public FlickrPlugin() {
_itemPlugInConfig.Dispose();
_itemPlugInConfig = null;
}
public IEnumerable<IDestination> Destinations() {
@ -70,31 +72,31 @@ namespace GreenshotFlickrPlugin
/// <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<FlickrConfiguration>();
resources = new ComponentResourceManager(typeof(FlickrPlugin));
_config = IniConfig.GetIniSection<FlickrConfiguration>();
_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
/// </summary>
public virtual void Configure() {
config.ShowConfigDialog();
_config.ShowConfigDialog();
}
/// <summary>
@ -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;

View file

@ -18,6 +18,7 @@
* 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 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 {
/// <summary>
/// Description of FlickrUtils.
/// </summary>
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 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";
/// <summary>
/// Do the actual upload to Flickr
/// For more details on the available parameters, see: http://flickrnet.codeplex.com
/// </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>
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<string, object> otherParameters = new Dictionary<string, object>();
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<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);
} 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);

View file

@ -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;
}
}
}