From 69d29992a2a4c8e3e10f02239909e9a5539369e8 Mon Sep 17 00:00:00 2001 From: RKrom Date: Sun, 14 Oct 2012 12:34:31 +0000 Subject: [PATCH] Changes for Box Plugin so it can re-use the OAuthLoginForm. Also fixed a startup issue with some plug-ins. git-svn-id: http://svn.code.sf.net/p/greenshot/code/trunk@2143 7dccd23d-a4a3-4e1f-8c07-b4c1b4018ab4 --- Greenshot/Forms/MainForm.cs | 2 +- Greenshot/Helpers/PluginHelper.cs | 17 ++++++---- GreenshotImgurPlugin/ImgurCredentials.cs | 24 +++++++++++-- GreenshotImgurPlugin/ImgurPlugin.cs | 2 +- GreenshotPlugin/Controls/OAuthLoginForm.cs | 34 +++++++------------ GreenshotPlugin/Core/NetworkHelper.cs | 27 ++++++--------- GreenshotPlugin/Core/OAuthHelper.cs | 17 +++++++--- .../Interfaces/Plugin/PluginInterfaces.cs | 5 +++ 8 files changed, 74 insertions(+), 54 deletions(-) diff --git a/Greenshot/Forms/MainForm.cs b/Greenshot/Forms/MainForm.cs index 4b0840ac3..9db778fa0 100644 --- a/Greenshot/Forms/MainForm.cs +++ b/Greenshot/Forms/MainForm.cs @@ -344,7 +344,7 @@ namespace Greenshot { ProcessorHelper.GetAllProcessors(); // Load all the plugins - PluginHelper.Instance.LoadPlugins(this); + PluginHelper.Instance.LoadPlugins(); // Check destinations, remove all that don't exist foreach(string destination in conf.OutputDestinations.ToArray()) { diff --git a/Greenshot/Helpers/PluginHelper.cs b/Greenshot/Helpers/PluginHelper.cs index 78eba9e76..4b8bbcfc5 100644 --- a/Greenshot/Helpers/PluginHelper.cs +++ b/Greenshot/Helpers/PluginHelper.cs @@ -50,11 +50,16 @@ namespace Greenshot.Helpers { } } - private PluginHelper() { PluginUtils.Host = this; } + public Form GreenshotForm { + get { + return MainForm.Instance; + } + } + public NotifyIcon NotifyIcon { get { return MainForm.Instance.NotifyIcon; @@ -105,7 +110,6 @@ namespace Greenshot.Helpers { } #region Implementation of IGreenshotPluginHost - private ContextMenuStrip mainMenu = null; /// /// Create a Thumbnail @@ -125,7 +129,9 @@ namespace Greenshot.Helpers { } public ContextMenuStrip MainMenu { - get { return mainMenu;} + get { + return MainForm.Instance.MainMenu; + } } public IDictionary Plugins { @@ -202,10 +208,7 @@ namespace Greenshot.Helpers { return false; } - public void LoadPlugins(MainForm mainForm) { - // Copy ContextMenu - mainMenu = mainForm.MainMenu; - + public void LoadPlugins() { List pluginFiles = new List(); if (IniConfig.IsPortable && Directory.Exists(pafPath)) { diff --git a/GreenshotImgurPlugin/ImgurCredentials.cs b/GreenshotImgurPlugin/ImgurCredentials.cs index e767bcd2a..e4d5f8cad 100644 --- a/GreenshotImgurPlugin/ImgurCredentials.cs +++ b/GreenshotImgurPlugin/ImgurCredentials.cs @@ -1,4 +1,24 @@ -using System; +/* + * Greenshot - a free and open source screenshot tool + * Copyright (C) 2007-2012 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; namespace GreenshotImgurPlugin { /// @@ -9,4 +29,4 @@ namespace GreenshotImgurPlugin { public static string CONSUMER_KEY = empty; public static string CONSUMER_SECRET = empty; } -} +} \ No newline at end of file diff --git a/GreenshotImgurPlugin/ImgurPlugin.cs b/GreenshotImgurPlugin/ImgurPlugin.cs index bed56c407..2553fd2ad 100644 --- a/GreenshotImgurPlugin/ImgurPlugin.cs +++ b/GreenshotImgurPlugin/ImgurPlugin.cs @@ -100,7 +100,7 @@ namespace GreenshotImgurPlugin { private void CheckHistory() { try { ImgurUtils.LoadHistory(); - host.MainMenu.BeginInvoke((MethodInvoker)delegate { + host.GreenshotForm.BeginInvoke((MethodInvoker)delegate { if (config.ImgurUploadHistory.Count > 0) { historyMenuItem.Enabled = true; } else { diff --git a/GreenshotPlugin/Controls/OAuthLoginForm.cs b/GreenshotPlugin/Controls/OAuthLoginForm.cs index 54a0a6e38..900f6800a 100644 --- a/GreenshotPlugin/Controls/OAuthLoginForm.cs +++ b/GreenshotPlugin/Controls/OAuthLoginForm.cs @@ -35,24 +35,20 @@ namespace GreenshotPlugin.Controls { public partial class OAuthLoginForm : Form { private static readonly log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(OAuthLoginForm)); private string callbackUrl = null; - private String _token; - private String _verifier; - - public String Token { + private IDictionary callbackParameters = null; + + public IDictionary CallbackParameters { + get { return callbackParameters; } + } + + public bool isOk { get { - return _token; + return DialogResult == DialogResult.OK; } } - - public String Verifier { - get { - return _verifier; - } - } - - public OAuthLoginForm(OAuthSession o, string browserTitle, Size size, string authorizationLink, string callbackUrl) { + + public OAuthLoginForm(string browserTitle, Size size, string authorizationLink, string callbackUrl) { this.callbackUrl = callbackUrl; - _token = null; InitializeComponent(); this.ClientSize = size; this.Icon = GreenshotPlugin.Core.GreenshotResources.getGreenshotIcon(); @@ -86,15 +82,9 @@ namespace GreenshotPlugin.Controls { if (queryParams.Length > 0) { queryParams = NetworkHelper.UrlDecode(queryParams); //Store the Token and Token Secret - IDictionary qs = NetworkHelper.ParseQueryString(queryParams); - if (qs.ContainsKey("oauth_token") && qs["oauth_token"] != null) { - _token = qs["oauth_token"]; - } - if (qs.ContainsKey("oauth_verifier") && qs["oauth_verifier"] != null) { - _verifier = qs["oauth_verifier"]; - } + callbackParameters = NetworkHelper.ParseQueryString(queryParams); } - this.Close(); + DialogResult = DialogResult.OK; } } diff --git a/GreenshotPlugin/Core/NetworkHelper.cs b/GreenshotPlugin/Core/NetworkHelper.cs index 9aa63e357..47205e20c 100644 --- a/GreenshotPlugin/Core/NetworkHelper.cs +++ b/GreenshotPlugin/Core/NetworkHelper.cs @@ -49,25 +49,16 @@ namespace GreenshotPlugin.Core { }; } /// - /// Download a file as string + /// Download a url response as string /// /// An Uri to specify the download location - /// The encoding to use /// string with the file content - public static string DownloadFileAsString(Uri url, Encoding encoding) { - try { - HttpWebRequest request = (HttpWebRequest)CreateWebRequest(url); - HttpWebResponse response = (HttpWebResponse)request.GetResponse(); - if (request.HaveResponse) { - using (StreamReader reader = new StreamReader(response.GetResponseStream(), encoding)) { - return reader.ReadToEnd(); - } - } - - } catch (Exception e) { - LOG.Error("Problem downloading from: " + url.ToString(), e); - } - return null; + public static string GetAsString(Uri url) { + HttpWebRequest webRequest = (HttpWebRequest)NetworkHelper.CreateWebRequest(url); + webRequest.Method = "GET"; + webRequest.KeepAlive = true; + webRequest.Credentials = System.Net.CredentialCache.DefaultCredentials; + return GetResponse(webRequest); } /// @@ -290,9 +281,11 @@ namespace GreenshotPlugin.Core { public static string GetResponse(HttpWebRequest webRequest) { string responseData; try { - using (StreamReader reader = new StreamReader(webRequest.GetResponse().GetResponseStream(), true)) { + HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse(); + using (StreamReader reader = new StreamReader(response.GetResponseStream(), true)) { responseData = reader.ReadToEnd(); } + LOG.DebugFormat("Response status: {0}", response.StatusCode); } catch (WebException e) { HttpWebResponse response = (HttpWebResponse)e.Response; using (Stream responseStream = response.GetResponseStream()) { diff --git a/GreenshotPlugin/Core/OAuthHelper.cs b/GreenshotPlugin/Core/OAuthHelper.cs index 5f781294c..a18db6275 100644 --- a/GreenshotPlugin/Core/OAuthHelper.cs +++ b/GreenshotPlugin/Core/OAuthHelper.cs @@ -295,11 +295,20 @@ namespace GreenshotPlugin.Core { Exception e = new Exception("The request token is not set"); throw e; } + Token = null; + Verifier = null; LOG.DebugFormat("Opening AuthorizationLink: {0}", authorizationLink); - OAuthLoginForm oAuthLoginForm = new OAuthLoginForm(this, LoginTitle, BrowserSize, authorizationLink, CallbackUrl); - oAuthLoginForm.ShowDialog(); - Token = oAuthLoginForm.Token; - Verifier = oAuthLoginForm.Verifier; + OAuthLoginForm oAuthLoginForm = new OAuthLoginForm(LoginTitle, BrowserSize, authorizationLink, CallbackUrl); + if (oAuthLoginForm.isOk) { + if (oAuthLoginForm.CallbackParameters != null) { + if (oAuthLoginForm.CallbackParameters.ContainsKey(OAUTH_TOKEN_KEY)) { + Token = oAuthLoginForm.CallbackParameters[OAUTH_TOKEN_KEY]; + } + if (oAuthLoginForm.CallbackParameters.ContainsKey(OAUTH_VERIFIER_KEY)) { + Verifier = oAuthLoginForm.CallbackParameters[OAUTH_VERIFIER_KEY]; + } + } + } if (CheckVerifier) { if (!string.IsNullOrEmpty(Verifier)) { return Token; diff --git a/GreenshotPlugin/Interfaces/Plugin/PluginInterfaces.cs b/GreenshotPlugin/Interfaces/Plugin/PluginInterfaces.cs index d59fe7c6b..40361554c 100644 --- a/GreenshotPlugin/Interfaces/Plugin/PluginInterfaces.cs +++ b/GreenshotPlugin/Interfaces/Plugin/PluginInterfaces.cs @@ -128,6 +128,11 @@ namespace Greenshot.Plugin { ContextMenuStrip MainMenu { get; } + + // This is a reference to the MainForm, can be used for Invoking on the UI thread. + Form GreenshotForm { + get; + } NotifyIcon NotifyIcon { get;