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
This commit is contained in:
RKrom 2012-10-14 12:34:31 +00:00
commit 69d29992a2
8 changed files with 74 additions and 54 deletions

View file

@ -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()) {

View file

@ -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;
/// <summary>
/// Create a Thumbnail
@ -125,7 +129,9 @@ namespace Greenshot.Helpers {
}
public ContextMenuStrip MainMenu {
get { return mainMenu;}
get {
return MainForm.Instance.MainMenu;
}
}
public IDictionary<PluginAttribute, IGreenshotPlugin> Plugins {
@ -202,10 +208,7 @@ namespace Greenshot.Helpers {
return false;
}
public void LoadPlugins(MainForm mainForm) {
// Copy ContextMenu
mainMenu = mainForm.MainMenu;
public void LoadPlugins() {
List<string> pluginFiles = new List<string>();
if (IniConfig.IsPortable && Directory.Exists(pafPath)) {

View file

@ -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 <http://www.gnu.org/licenses/>.
*/
using System;
namespace GreenshotImgurPlugin {
/// <summary>
@ -9,4 +29,4 @@ namespace GreenshotImgurPlugin {
public static string CONSUMER_KEY = empty;
public static string CONSUMER_SECRET = empty;
}
}
}

View file

@ -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 {

View file

@ -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<string, string> callbackParameters = null;
public IDictionary<string, string> 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<string, string> 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;
}
}

View file

@ -49,25 +49,16 @@ namespace GreenshotPlugin.Core {
};
}
/// <summary>
/// Download a file as string
/// Download a url response as string
/// </summary>
/// <param name=url">An Uri to specify the download location</param>
/// <param name=encoding">The encoding to use</param>
/// <returns>string with the file content</returns>
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);
}
/// <summary>
@ -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()) {

View file

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

View file

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