Code quality changes

This commit is contained in:
Robin 2016-09-22 20:40:13 +02:00
commit 610f45d082
189 changed files with 4609 additions and 5203 deletions

View file

@ -24,7 +24,5 @@ namespace GreenshotPhotobucketPlugin {
/// This class is needed for design-time resolving of the language files
/// </summary>
public class PhotobucketForm : GreenshotPlugin.Controls.GreenshotForm {
public PhotobucketForm() : base() {
}
}
}

View file

@ -24,7 +24,8 @@ namespace GreenshotPhotobucketPlugin {
/// Description of PasswordRequestForm.
/// </summary>
public partial class SettingsForm : PhotobucketForm {
public SettingsForm(PhotobucketConfiguration config) : base() {
public SettingsForm()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//

View file

@ -59,8 +59,8 @@ namespace GreenshotPhotobucketPlugin {
SettingsForm settingsForm = null;
new PleaseWaitForm().ShowAndWait(PhotobucketPlugin.Attributes.Name, Language.GetString("photobucket", LangKey.communication_wait),
delegate() {
settingsForm = new SettingsForm(this);
delegate {
settingsForm = new SettingsForm();
}
);
DialogResult result = settingsForm.ShowDialog();

View file

@ -21,8 +21,6 @@
using System.ComponentModel;
using System.Collections.Generic;
using System.Drawing;
using Greenshot.IniFile;
using Greenshot.Plugin;
using GreenshotPlugin.Core;
@ -31,10 +29,8 @@ namespace GreenshotPhotobucketPlugin {
/// Description of PhotobucketDestination.
/// </summary>
public class PhotobucketDestination : AbstractDestination {
private static log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(PhotobucketDestination));
private static PhotobucketConfiguration config = IniConfig.GetIniSection<PhotobucketConfiguration>();
private readonly PhotobucketPlugin plugin = null;
private readonly string albumPath = null;
private readonly PhotobucketPlugin _plugin;
private readonly string _albumPath;
/// <summary>
/// Create a Photobucket destination, which also has the path to the album in it
@ -42,20 +38,16 @@ namespace GreenshotPhotobucketPlugin {
/// <param name="plugin"></param>
/// <param name="albumPath">path to the album, null for default</param>
public PhotobucketDestination(PhotobucketPlugin plugin, string albumPath) {
this.plugin = plugin;
this.albumPath = albumPath;
_plugin = plugin;
_albumPath = albumPath;
}
public override string Designation {
get {
return "Photobucket";
}
}
public override string Designation => "Photobucket";
public override string Description {
get {
if (albumPath != null) {
return albumPath;
if (_albumPath != null) {
return _albumPath;
}
return Language.GetString("photobucket", LangKey.upload_menu_item);
}
@ -68,24 +60,23 @@ namespace GreenshotPhotobucketPlugin {
}
}
public override bool isDynamic {
get {
return true;
}
}
public override bool IsDynamic => true;
public override IEnumerable<IDestination> DynamicDestinations() {
List<string> albums = null;
IList<string> albums = null;
try {
albums = PhotobucketUtils.RetrievePhotobucketAlbums();
} catch {
}
catch
{
// ignored
}
if (albums == null || albums.Count == 0) {
yield break;
}
foreach (string album in albums) {
yield return new PhotobucketDestination(plugin, album);
yield return new PhotobucketDestination(_plugin, album);
}
}
@ -98,11 +89,11 @@ namespace GreenshotPhotobucketPlugin {
/// <returns></returns>
public override ExportInformation ExportCapture(bool manuallyInitiated, ISurface surface, ICaptureDetails captureDetails) {
ExportInformation exportInformation = new ExportInformation(Designation, Description);
string uploadURL = null;
bool uploaded = plugin.Upload(captureDetails, surface, albumPath, out uploadURL);
string uploadUrl;
bool uploaded = _plugin.Upload(captureDetails, surface, _albumPath, out uploadUrl);
if (uploaded) {
exportInformation.ExportMade = true;
exportInformation.Uri = uploadURL;
exportInformation.Uri = uploadUrl;
}
ProcessExport(exportInformation, surface);
return exportInformation;

View file

@ -27,28 +27,13 @@ namespace GreenshotPhotobucketPlugin
/// Description of PhotobucketInfo.
/// </summary>
public class PhotobucketInfo {
private static readonly log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(PhotobucketInfo));
private static readonly log4net.ILog Log = log4net.LogManager.GetLogger(typeof(PhotobucketInfo));
private string original;
public string Original {
get {return original;}
set {original = value;}
}
public string Original { get; set; }
private string page;
public string Page {
get {return page;}
set {page = value;}
}
public string Page { get; set; }
private string thumbnail;
public string Thumbnail {
get {return thumbnail;}
set {thumbnail = value;}
}
public PhotobucketInfo() {
}
public string Thumbnail { get; set; }
/// <summary>
/// Parse the upload response
@ -56,28 +41,27 @@ namespace GreenshotPhotobucketPlugin
/// <param name="response">XML</param>
/// <returns>PhotobucketInfo object</returns>
public static PhotobucketInfo FromUploadResponse(string response) {
LOG.Debug(response);
PhotobucketInfo PhotobucketInfo = new PhotobucketInfo();
Log.Debug(response);
PhotobucketInfo photobucketInfo = new PhotobucketInfo();
try {
XmlDocument doc = new XmlDocument();
doc.LoadXml(response);
XmlNodeList nodes;
nodes = doc.GetElementsByTagName("url");
var nodes = doc.GetElementsByTagName("url");
if(nodes.Count > 0) {
PhotobucketInfo.Original = nodes.Item(0).InnerText;
photobucketInfo.Original = nodes.Item(0)?.InnerText;
}
nodes = doc.GetElementsByTagName("browseurl");
if(nodes.Count > 0) {
PhotobucketInfo.Page = nodes.Item(0).InnerText;
photobucketInfo.Page = nodes.Item(0)?.InnerText;
}
nodes = doc.GetElementsByTagName("thumb");
if(nodes.Count > 0) {
PhotobucketInfo.Thumbnail = nodes.Item(0).InnerText;
photobucketInfo.Thumbnail = nodes.Item(0)?.InnerText;
}
} catch(Exception e) {
LOG.ErrorFormat("Could not parse Photobucket response due to error {0}, response was: {1}", e.Message, response);
Log.ErrorFormat("Could not parse Photobucket response due to error {0}, response was: {1}", e.Message, response);
}
return PhotobucketInfo;
return photobucketInfo;
}
}
}

View file

@ -55,9 +55,6 @@ namespace GreenshotPhotobucketPlugin {
}
}
public PhotobucketPlugin() {
}
public IEnumerable<IDestination> Destinations() {
yield return new PhotobucketDestination(this, null);
}

View file

@ -19,6 +19,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Xml;
@ -31,9 +32,9 @@ namespace GreenshotPhotobucketPlugin {
/// Description of PhotobucketUtils.
/// </summary>
public static class PhotobucketUtils {
private static readonly log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(PhotobucketUtils));
private static readonly PhotobucketConfiguration config = IniConfig.GetIniSection<PhotobucketConfiguration>();
private static List<string> albumsCache = null;
private static readonly log4net.ILog Log = log4net.LogManager.GetLogger(typeof(PhotobucketUtils));
private static readonly PhotobucketConfiguration PhotobucketConfig = IniConfig.GetIniSection<PhotobucketConfiguration>();
private static List<string> _albumsCache;
/// <summary>
/// Do the actual upload to Photobucket
@ -43,18 +44,15 @@ namespace GreenshotPhotobucketPlugin {
public static PhotobucketInfo UploadToPhotobucket(ISurface surfaceToUpload, SurfaceOutputSettings outputSettings, string albumPath, string title, string filename) {
string responseString;
if (string.IsNullOrEmpty(albumPath)) {
albumPath = "!";
}
OAuthSession oAuth = createSession(true);
var oAuth = CreateSession(true);
if (oAuth == null) {
return null;
}
IDictionary<string, object> signedParameters = new Dictionary<string, object>();
// add album
if (albumPath == null) {
signedParameters.Add("id", config.Username);
if (string.IsNullOrEmpty(albumPath))
{
signedParameters.Add("id", string.IsNullOrEmpty(PhotobucketConfig.Username) ? "!" : PhotobucketConfig.Username);
} else {
signedParameters.Add("id", albumPath);
}
@ -73,47 +71,49 @@ namespace GreenshotPhotobucketPlugin {
unsignedParameters.Add("uploadfile", new SurfaceContainer(surfaceToUpload, outputSettings, filename));
try {
string apiUrl = "http://api.photobucket.com/album/!/upload";
responseString = oAuth.MakeOAuthRequest(HTTPMethod.POST, apiUrl, apiUrl.Replace("api.photobucket.com", config.SubDomain), signedParameters, unsignedParameters, null);
responseString = oAuth.MakeOAuthRequest(HTTPMethod.POST, apiUrl, apiUrl.Replace("api.photobucket.com", PhotobucketConfig.SubDomain), signedParameters, unsignedParameters, null);
} catch (Exception ex) {
LOG.Error("Error uploading to Photobucket.", ex);
Log.Error("Error uploading to Photobucket.", ex);
throw;
} finally {
if (!string.IsNullOrEmpty(oAuth.Token)) {
config.Token = oAuth.Token;
PhotobucketConfig.Token = oAuth.Token;
}
if (!string.IsNullOrEmpty(oAuth.TokenSecret)) {
config.TokenSecret = oAuth.TokenSecret;
PhotobucketConfig.TokenSecret = oAuth.TokenSecret;
}
}
if (responseString == null) {
return null;
}
LOG.Info(responseString);
PhotobucketInfo PhotobucketInfo = PhotobucketInfo.FromUploadResponse(responseString);
LOG.Debug("Upload to Photobucket was finished");
return PhotobucketInfo;
Log.Info(responseString);
var photobucketInfo = PhotobucketInfo.FromUploadResponse(responseString);
Log.Debug("Upload to Photobucket was finished");
return photobucketInfo;
}
/// <summary>
/// Helper method to create an OAuth session object for contacting the Photobucket API
/// </summary>
/// <returns>OAuthSession</returns>
private static OAuthSession createSession(bool autoLogin) {
OAuthSession oAuth = new OAuthSession(PhotobucketCredentials.ConsumerKey, PhotobucketCredentials.ConsumerSecret);
oAuth.AutoLogin = autoLogin;
oAuth.CheckVerifier = false;
private static OAuthSession CreateSession(bool autoLogin) {
var oAuth = new OAuthSession(PhotobucketCredentials.ConsumerKey, PhotobucketCredentials.ConsumerSecret)
{
AutoLogin = autoLogin,
CheckVerifier = false,
CallbackUrl = "http://getgreenshot.org",
AccessTokenUrl = "http://api.photobucket.com/login/access",
AuthorizeUrl = "http://photobucket.com/apilogin/login",
RequestTokenUrl = "http://api.photobucket.com/login/request",
BrowserSize = new Size(1010, 400),
RequestTokenMethod = HTTPMethod.POST,
AccessTokenMethod = HTTPMethod.POST,
LoginTitle = "Photobucket authorization"
};
// This url is configured in the Photobucket API settings in the Photobucket site!!
oAuth.CallbackUrl = "http://getgreenshot.org";
oAuth.AccessTokenUrl = "http://api.photobucket.com/login/access";
oAuth.AuthorizeUrl = "http://photobucket.com/apilogin/login";
oAuth.RequestTokenUrl = "http://api.photobucket.com/login/request";
oAuth.BrowserSize = new Size(1010, 400);
// Photobucket is very particular about the used methods!
oAuth.RequestTokenMethod = HTTPMethod.POST;
oAuth.AccessTokenMethod = HTTPMethod.POST;
oAuth.LoginTitle = "Photobucket authorization";
if (string.IsNullOrEmpty(config.SubDomain) || string.IsNullOrEmpty(config.Token) || string.IsNullOrEmpty(config.Username)) {
if (string.IsNullOrEmpty(PhotobucketConfig.SubDomain) || string.IsNullOrEmpty(PhotobucketConfig.Token) || string.IsNullOrEmpty(PhotobucketConfig.Username)) {
if (!autoLogin) {
return null;
}
@ -121,21 +121,21 @@ namespace GreenshotPhotobucketPlugin {
return null;
}
if (!string.IsNullOrEmpty(oAuth.Token)) {
config.Token = oAuth.Token;
PhotobucketConfig.Token = oAuth.Token;
}
if (!string.IsNullOrEmpty(oAuth.TokenSecret)) {
config.TokenSecret = oAuth.TokenSecret;
PhotobucketConfig.TokenSecret = oAuth.TokenSecret;
}
if (oAuth.AccessTokenResponseParameters != null && oAuth.AccessTokenResponseParameters["subdomain"] != null) {
config.SubDomain = oAuth.AccessTokenResponseParameters["subdomain"];
if (oAuth.AccessTokenResponseParameters?["subdomain"] != null) {
PhotobucketConfig.SubDomain = oAuth.AccessTokenResponseParameters["subdomain"];
}
if (oAuth.AccessTokenResponseParameters != null && oAuth.AccessTokenResponseParameters["username"] != null) {
config.Username = oAuth.AccessTokenResponseParameters["username"];
if (oAuth.AccessTokenResponseParameters?["username"] != null) {
PhotobucketConfig.Username = oAuth.AccessTokenResponseParameters["username"];
}
IniConfig.Save();
}
oAuth.Token = config.Token;
oAuth.TokenSecret = config.TokenSecret;
oAuth.Token = PhotobucketConfig.Token;
oAuth.TokenSecret = PhotobucketConfig.TokenSecret;
return oAuth;
}
@ -143,29 +143,29 @@ namespace GreenshotPhotobucketPlugin {
/// Get list of photobucket albums
/// </summary>
/// <returns>List of string</returns>
public static List<string> RetrievePhotobucketAlbums() {
if (albumsCache != null) {
return albumsCache;
public static IList<string> RetrievePhotobucketAlbums() {
if (_albumsCache != null) {
return _albumsCache;
}
string responseString;
OAuthSession oAuth = createSession(false);
OAuthSession oAuth = CreateSession(false);
if (oAuth == null) {
return null;
}
IDictionary<string, object> signedParameters = new Dictionary<string, object>();
try {
string apiUrl = string.Format("http://api.photobucket.com/album/{0}", config.Username);
responseString = oAuth.MakeOAuthRequest(HTTPMethod.GET, apiUrl, apiUrl.Replace("api.photobucket.com", config.SubDomain), signedParameters, null, null);
string apiUrl = $"http://api.photobucket.com/album/{PhotobucketConfig.Username}";
responseString = oAuth.MakeOAuthRequest(HTTPMethod.GET, apiUrl, apiUrl.Replace("api.photobucket.com", PhotobucketConfig.SubDomain), signedParameters, null, null);
} catch (Exception ex) {
LOG.Error("Error uploading to Photobucket.", ex);
Log.Error("Error uploading to Photobucket.", ex);
throw;
} finally {
if (!string.IsNullOrEmpty(oAuth.Token)) {
config.Token = oAuth.Token;
PhotobucketConfig.Token = oAuth.Token;
}
if (!string.IsNullOrEmpty(oAuth.TokenSecret)) {
config.TokenSecret = oAuth.TokenSecret;
PhotobucketConfig.TokenSecret = oAuth.TokenSecret;
}
}
if (responseString == null) {
@ -175,15 +175,19 @@ namespace GreenshotPhotobucketPlugin {
XmlDocument doc = new XmlDocument();
doc.LoadXml(responseString);
List<string> albums = new List<string>();
recurseAlbums(albums, null, doc.GetElementsByTagName("content").Item(0).ChildNodes);
LOG.DebugFormat("Albums: {0}", string.Join(",", albums.ToArray()));
albumsCache = albums;
var xmlNode = doc.GetElementsByTagName("content").Item(0);
if (xmlNode != null)
{
RecurseAlbums(albums, null, xmlNode.ChildNodes);
}
Log.DebugFormat("Albums: {0}", string.Join(",", albums.ToArray()));
_albumsCache = albums;
return albums;
} catch(Exception e) {
LOG.Error("Error while Reading albums: ", e);
Log.Error("Error while Reading albums: ", e);
}
LOG.Debug("Upload to Photobucket was finished");
Log.Debug("Upload to Photobucket was finished");
return null;
}
@ -193,20 +197,23 @@ namespace GreenshotPhotobucketPlugin {
/// <param name="albums"></param>
/// <param name="path"></param>
/// <param name="nodes"></param>
private static void recurseAlbums(List<string>albums, string path, XmlNodeList nodes) {
private static void RecurseAlbums(ICollection<string> albums, string path, IEnumerable nodes) {
foreach(XmlNode node in nodes) {
if (node.Name != "album") {
continue;
}
string currentAlbum = node.Attributes["name"].Value;
string currentPath = currentAlbum;
if (path != null && path.Length > 0) {
currentPath = string.Format("{0}/{1}", path, currentAlbum);
}
if (node.Attributes != null)
{
string currentAlbum = node.Attributes["name"].Value;
string currentPath = currentAlbum;
if (!string.IsNullOrEmpty(path)) {
currentPath = $"{path}/{currentAlbum}";
}
albums.Add(currentPath);
if (node.Attributes["subalbum_count"] != null && node.Attributes["subalbum_count"].Value != "0") {
recurseAlbums(albums, currentPath, node.ChildNodes);
albums.Add(currentPath);
if (node.Attributes["subalbum_count"] != null && node.Attributes["subalbum_count"].Value != "0") {
RecurseAlbums(albums, currentPath, node.ChildNodes);
}
}
}
}