diff --git a/GreenshotFlickrPlugin/FlickrPlugin.cs b/GreenshotFlickrPlugin/FlickrPlugin.cs index 544651d98..8d1aeb23c 100644 --- a/GreenshotFlickrPlugin/FlickrPlugin.cs +++ b/GreenshotFlickrPlugin/FlickrPlugin.cs @@ -28,6 +28,7 @@ using System.Runtime.Serialization.Formatters.Binary; using System.Text; using System.Windows.Forms; +using FlickrNet; using Greenshot.Capturing; using Greenshot.Forms; using Greenshot.Plugin; @@ -41,11 +42,14 @@ namespace GreenshotFlickrPlugin { public class FlickrPlugin : IGreenshotPlugin { private static log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(FlickrPlugin)); private const string CONFIG_FILENAME = "flickr-config.properties"; + private const string AUTHENTICATION_TOKEN_PROPERTY = "authentication.token"; + private const string ApiKey = "f967e5148945cb3c4e149cc5be97796a"; + private const string SharedSecret = "4180a21a1d2f8666"; private ILanguage lang = Language.GetInstance(); private IGreenshotPluginHost host; private ICaptureHost captureHost = null; private PluginAttribute myAttributes; - private Properties config = new Properties(); + private Properties config = null; public FlickrPlugin() { } @@ -110,7 +114,7 @@ namespace GreenshotFlickrPlugin { string dllPath = Path.GetDirectoryName(myAttributes.DllFile); string dllFilename = args.Name.Substring(0, args.Name.IndexOf(",")) + ".dll"; LOG.Debug("Resolving: " + dllFilename); - if (dllFilename.StartsWith("Flickr")) { + if (dllFilename.ToLower().StartsWith("flickr")) { return Assembly.LoadFile(Path.Combine(dllPath, dllFilename)); } return null; @@ -119,18 +123,13 @@ namespace GreenshotFlickrPlugin { private void LoadConfig() { string filename = Path.Combine(host.ConfigurationPath, CONFIG_FILENAME); if (File.Exists(filename)) { - LOG.Debug("Loading configuration from: " + filename); config = Properties.read(filename); + } else { + LOG.Debug("No flickr configuration found at: " + filename); } - bool changed = false; if (config == null) { config = new Properties(); - changed = true; } - if (changed) { - SaveConfig(); - } - } private void SaveConfig() { @@ -145,14 +144,74 @@ namespace GreenshotFlickrPlugin { public void EditMenuClick(object sender, EventArgs eventArgs) { ToolStripMenuItem item = (ToolStripMenuItem)sender; IImageEditor imageEditor = (IImageEditor)item.Tag; + bool authentication = false; + + Flickr flickr = new Flickr(ApiKey, SharedSecret); + if(config.GetProperty(AUTHENTICATION_TOKEN_PROPERTY) == null) { + string frob = flickr.AuthGetFrob(); + // Calculate the URL at Flickr to redirect the user to + string flickrUrl = flickr.AuthCalcUrl(frob, AuthLevel.Write); + FlickrAuthenticationForm authForm = new FlickrAuthenticationForm(flickrUrl); + DialogResult authFormResult = authForm.ShowDialog(); + if (authFormResult == DialogResult.OK) { + try { + // use the temporary Frob to get the authentication + Auth auth = flickr.AuthGetToken(frob); + // Store this Token for later usage, + // or set your Flickr instance to use it. + LOG.Debug("User authenticated successfully"); + LOG.Debug("Authentication token is " + auth.Token); + LOG.Debug("User id is " + auth.User.UserId); + LOG.Debug("User name is " + auth.User.UserName); + LOG.Debug("User fullname is " + auth.User.FullName); + config.AddProperty(AUTHENTICATION_TOKEN_PROPERTY, auth.Token); + SaveConfig(); + authentication = true; + } catch (FlickrException ex) { + // If user did not authenticat your application + // then a FlickrException will be thrown. + LOG.Debug("User did not authenticate you", ex); + } + } + } else { + // Token is there + authentication = true; + } + if (!authentication) { + MessageBox.Show("No Authentication made!"); + return; + } + flickr.AuthToken = config.GetProperty(AUTHENTICATION_TOKEN_PROPERTY); FlickrUploadForm uploader = new FlickrUploadForm(); - DialogResult result = uploader.ShowDialog(); - if (result == DialogResult.OK) { + DialogResult uploaderResult = uploader.ShowDialog(); + if (uploaderResult == DialogResult.OK) { using (MemoryStream stream = new MemoryStream()) { imageEditor.SaveToStream(stream, "PNG", 100); stream.Seek(0, SeekOrigin.Begin); try { - uploader.upload(stream); + string file = "test.png"; + string title = "Test Photo"; + string description = "This is the description of the photo"; + string tags = "greenshot,screenshot"; + string photoId = flickr.UploadPicture(stream, file, title, description, tags, false, false, false, ContentType.Screenshot, SafetyLevel.Restricted, HiddenFromSearch.Hidden); + + flickr.PhotosSetMeta(photoId, "Greenshot screenshot", "This is a greenshot screenshot!"); +// // Get list of users sets +// PhotosetCollection sets = flickr.PhotosetsGetList(); +// string photosetId = null; +// if (sets.Count == 0) { +// LOG.Debug("Not photosets found, creating"); +// Photoset photoset = flickr.PhotosetsCreate("Greenshot", "Greenshot screenshots", photoId); +// LOG.Debug("Created photoset with description: " + photoset.Description); +// LOG.Debug("Created photoset with id: " + photoset.PhotosetId); +// photosetId = photoset.PhotosetId; +// } else { +// // Get the first set in the collection +// photosetId = sets[0].PhotosetId; +// } +// +// // Add the photo to that set +// flickr.PhotosetsAddPhoto(photosetId, photoId); MessageBox.Show(lang.GetString(LangKey.upload_success)); } catch(Exception e) { LOG.Error("Problem uploading", e); diff --git a/GreenshotFlickrPlugin/Forms/FlickrAuthenticationForm.Designer.cs b/GreenshotFlickrPlugin/Forms/FlickrAuthenticationForm.Designer.cs new file mode 100644 index 000000000..abd73a592 --- /dev/null +++ b/GreenshotFlickrPlugin/Forms/FlickrAuthenticationForm.Designer.cs @@ -0,0 +1,110 @@ +/* + * Greenshot - a free and open source screenshot tool + * Copyright (C) 2007-2010 Thomas Braun, Jens Klingen, Robin Krom + * + * For more information see: http://getgreenshot.org/ + * The Greenshot project is hosted on Sourceforge: http://sourceforge.net/projects/greenshot/ + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 1 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +namespace GreenshotFlickrPlugin { + partial class FlickrAuthenticationForm { + /// + /// Designer variable used to keep track of non-visual components. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Disposes resources used by the form. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) { + if (disposing) { + if (components != null) { + components.Dispose(); + } + } + base.Dispose(disposing); + } + + /// + /// This method is required for Windows Forms designer support. + /// Do not change the method contents inside the source code editor. The Forms designer might + /// not be able to load this method if it was changed manually. + /// + private void InitializeComponent() { + this.okButton = new System.Windows.Forms.Button(); + this.cancelButton = new System.Windows.Forms.Button(); + this.button1 = new System.Windows.Forms.Button(); + this.label1 = new System.Windows.Forms.Label(); + this.SuspendLayout(); + // + // okButton + // + this.okButton.Location = new System.Drawing.Point(116, 229); + this.okButton.Name = "okButton"; + this.okButton.Size = new System.Drawing.Size(75, 23); + this.okButton.TabIndex = 6; + this.okButton.Text = "OK"; + this.okButton.UseVisualStyleBackColor = true; + this.okButton.Click += new System.EventHandler(this.OkButtonClick); + // + // cancelButton + // + this.cancelButton.Location = new System.Drawing.Point(197, 229); + this.cancelButton.Name = "cancelButton"; + this.cancelButton.Size = new System.Drawing.Size(75, 23); + this.cancelButton.TabIndex = 5; + this.cancelButton.Text = "Cancel"; + this.cancelButton.UseVisualStyleBackColor = true; + this.cancelButton.Click += new System.EventHandler(this.CancelButtonClick); + // + // button1 + // + this.button1.Location = new System.Drawing.Point(102, 89); + this.button1.Name = "button1"; + this.button1.Size = new System.Drawing.Size(89, 23); + this.button1.TabIndex = 4; + this.button1.Text = "Authentication"; + this.button1.UseVisualStyleBackColor = true; + this.button1.Click += new System.EventHandler(this.AuthenticationButtonClick); + // + // label1 + // + this.label1.Location = new System.Drawing.Point(13, 35); + this.label1.Name = "label1"; + this.label1.Size = new System.Drawing.Size(259, 51); + this.label1.TabIndex = 7; + this.label1.Text = "Please press the authentication button, this will open a browser. Login to Flickr" + + " and allow Greenshot access"; + // + // FlickrAuthenticationForm + // + this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(284, 264); + this.Controls.Add(this.label1); + this.Controls.Add(this.okButton); + this.Controls.Add(this.cancelButton); + this.Controls.Add(this.button1); + this.Name = "FlickrAuthenticationForm"; + this.Text = "FlickrAuthenticationForm"; + this.ResumeLayout(false); + } + private System.Windows.Forms.Label label1; + private System.Windows.Forms.Button button1; + private System.Windows.Forms.Button cancelButton; + private System.Windows.Forms.Button okButton; + } +} diff --git a/GreenshotFlickrPlugin/Forms/FlickrAuthenticationForm.cs b/GreenshotFlickrPlugin/Forms/FlickrAuthenticationForm.cs new file mode 100644 index 000000000..e66ef3ed0 --- /dev/null +++ b/GreenshotFlickrPlugin/Forms/FlickrAuthenticationForm.cs @@ -0,0 +1,52 @@ +/* + * Greenshot - a free and open source screenshot tool + * Copyright (C) 2007-2010 Thomas Braun, Jens Klingen, Robin Krom + * + * For more information see: http://getgreenshot.org/ + * The Greenshot project is hosted on Sourceforge: http://sourceforge.net/projects/greenshot/ + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 1 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +using System; +using System.Drawing; +using System.Windows.Forms; + +namespace GreenshotFlickrPlugin { + /// + /// Description of FlickrAuthenticationForm. + /// + public partial class FlickrAuthenticationForm : Form { + private string authenticationUrl = null; + public FlickrAuthenticationForm(string authenticationUrl) { + // + // The InitializeComponent() call is required for Windows Forms designer support. + // + InitializeComponent(); + this.authenticationUrl = authenticationUrl; + } + + void AuthenticationButtonClick(object sender, EventArgs e) { + // The following line will load the URL in the users default browser. + System.Diagnostics.Process.Start(authenticationUrl); + } + + void OkButtonClick(object sender, EventArgs e) { + DialogResult = DialogResult.OK; + } + + void CancelButtonClick(object sender, EventArgs e) { + DialogResult = DialogResult.Cancel; + } + } +} diff --git a/GreenshotFlickrPlugin/Forms/FlickrUploadForm.Designer.cs b/GreenshotFlickrPlugin/Forms/FlickrUploadForm.Designer.cs index 45188d7aa..09f3924d7 100644 --- a/GreenshotFlickrPlugin/Forms/FlickrUploadForm.Designer.cs +++ b/GreenshotFlickrPlugin/Forms/FlickrUploadForm.Designer.cs @@ -44,32 +44,10 @@ namespace GreenshotFlickrPlugin { /// not be able to load this method if it was changed manually. /// private void InitializeComponent() { - this.button1 = new System.Windows.Forms.Button(); - this.button2 = new System.Windows.Forms.Button(); this.cancelButton = new System.Windows.Forms.Button(); this.okButton = new System.Windows.Forms.Button(); this.SuspendLayout(); // - // button1 - // - this.button1.Location = new System.Drawing.Point(13, 13); - this.button1.Name = "button1"; - this.button1.Size = new System.Drawing.Size(75, 23); - this.button1.TabIndex = 0; - this.button1.Text = "Auth"; - this.button1.UseVisualStyleBackColor = true; - this.button1.Click += new System.EventHandler(this.AuthMeButton_Click); - // - // button2 - // - this.button2.Location = new System.Drawing.Point(13, 43); - this.button2.Name = "button2"; - this.button2.Size = new System.Drawing.Size(75, 23); - this.button2.TabIndex = 1; - this.button2.Text = "Complete Auth"; - this.button2.UseVisualStyleBackColor = true; - this.button2.Click += new System.EventHandler(this.CompleteAuthButton_Click); - // // cancelButton // this.cancelButton.Location = new System.Drawing.Point(197, 229); @@ -97,15 +75,11 @@ namespace GreenshotFlickrPlugin { this.ClientSize = new System.Drawing.Size(284, 264); this.Controls.Add(this.okButton); this.Controls.Add(this.cancelButton); - this.Controls.Add(this.button2); - this.Controls.Add(this.button1); this.Name = "FlickrUploadForm"; this.Text = "FlickrUploadForm"; this.ResumeLayout(false); } private System.Windows.Forms.Button cancelButton; private System.Windows.Forms.Button okButton; - private System.Windows.Forms.Button button2; - private System.Windows.Forms.Button button1; } } diff --git a/GreenshotFlickrPlugin/Forms/FlickrUploadForm.cs b/GreenshotFlickrPlugin/Forms/FlickrUploadForm.cs index 61bedbe31..95726c70c 100644 --- a/GreenshotFlickrPlugin/Forms/FlickrUploadForm.cs +++ b/GreenshotFlickrPlugin/Forms/FlickrUploadForm.cs @@ -23,73 +23,17 @@ using System.Drawing; using System.IO; using System.Windows.Forms; -using FlickrNet; - namespace GreenshotFlickrPlugin { /// /// Description of FlickrUploadForm. /// public partial class FlickrUploadForm : Form { - // Store the Frob in a private variable - private string tempFrob; - private string ApiKey = "f967e5148945cb3c4e149cc5be97796a"; - private string SharedSecret = "4180a21a1d2f8666"; - private Flickr flickr; public FlickrUploadForm() { // // The InitializeComponent() call is required for Windows Forms designer support. // InitializeComponent(); - // Create Flickr instance - flickr = new Flickr(ApiKey, SharedSecret); - } - - protected void AuthMeButton_Click(object sender, EventArgs e) { - // Get Frob - tempFrob = flickr.AuthGetFrob(); - // Calculate the URL at Flickr to redirect the user to - string flickrUrl = flickr.AuthCalcUrl(tempFrob, AuthLevel.Write); - // The following line will load the URL in the users default browser. - System.Diagnostics.Process.Start(flickrUrl); - } - - protected void CompleteAuthButton_Click(object sender, EventArgs e) { - try { - // use the temporary Frob to get the authentication - Auth auth = flickr.AuthGetToken(tempFrob); - // Store this Token for later usage, - // or set your Flickr instance to use it. - Console.WriteLine("User authenticated successfully"); - Console.WriteLine("Authentication token is " + auth.Token); - flickr.AuthToken = auth.Token; - Console.WriteLine("User id is " + auth.User); - } catch(FlickrException ex) { - // If user did not authenticat your application - // then a FlickrException will be thrown. - Console.WriteLine("User did not authenticate you"); - Console.WriteLine(ex.ToString()); - } - } - - public void upload(Stream buffer) { - string file = "test.png"; - string title = "Test Photo"; - string description = "This is the description of the photo"; - string tags = "tag1,tag2,tag3"; - string photoId = flickr.UploadPicture(buffer, file, title, description, tags, false, false, false, ContentType.Screenshot, SafetyLevel.Restricted, HiddenFromSearch.Hidden); - - flickr.PhotosSetMeta(photoId, "New Title", "New Description"); - // Get list of users sets - PhotosetCollection sets = flickr.PhotosetsGetList(); - if (sets.Count == 0) { - flickr.PhotosetsCreate("Greenshot", "Greenshot screenshots", photoId); - sets = flickr.PhotosetsGetList(); - } - // Get the first set in the collection - Photoset set = sets[0]; - // Add the photo to that set - flickr.PhotosetsAddPhoto(set.PhotosetId, photoId); } void OkButtonClick(object sender, EventArgs e) { diff --git a/GreenshotFlickrPlugin/Forms/FlickrUploadForm.resx b/GreenshotFlickrPlugin/Forms/FlickrUploadForm.resx deleted file mode 100644 index 5ea0895e3..000000000 --- a/GreenshotFlickrPlugin/Forms/FlickrUploadForm.resx +++ /dev/null @@ -1,120 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - text/microsoft-resx - - - 2.0 - - - System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - \ No newline at end of file diff --git a/GreenshotFlickrPlugin/GreenshotFlickrPlugin.csproj b/GreenshotFlickrPlugin/GreenshotFlickrPlugin.csproj index e69dde4f8..1670186b7 100644 --- a/GreenshotFlickrPlugin/GreenshotFlickrPlugin.csproj +++ b/GreenshotFlickrPlugin/GreenshotFlickrPlugin.csproj @@ -52,6 +52,10 @@ + + + FlickrAuthenticationForm.cs + FlickrUploadForm.cs @@ -70,9 +74,6 @@ - - FlickrUploadForm.cs -