/* * Greenshot - a free and open source screenshot tool * Copyright (C) 2007-2013 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.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.IO; using System.Windows.Forms; using Greenshot.IniFile; using Greenshot.Plugin; using GreenshotPlugin.Controls; using GreenshotPlugin.Core; namespace GreenshotPhotobucketPlugin { /// /// This is the GreenshotPhotobucketPlugin base code /// public class PhotobucketPlugin : IGreenshotPlugin { private static readonly log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(PhotobucketPlugin)); private static PhotobucketConfiguration config; public static PluginAttribute Attributes; private IGreenshotHost host; private ComponentResourceManager resources; private ToolStripMenuItem itemPlugInConfig; public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } protected virtual void Dispose(bool disposing) { if (disposing) { if (itemPlugInConfig != null) { itemPlugInConfig.Dispose(); itemPlugInConfig = null; } } } public PhotobucketPlugin() { } public IEnumerable Destinations() { yield return new PhotobucketDestination(this, null); } public IEnumerable Processors() { yield break; } /// /// Implementation of the IGreenshotPlugin.Initialize /// /// Use the IGreenshotPluginHost interface to register events /// Use the ICaptureHost interface to register in the MainContextMenu /// My own attributes /// true if plugin is initialized, false if not (doesn't show) public virtual bool Initialize(IGreenshotHost pluginHost, PluginAttribute myAttributes) { this.host = (IGreenshotHost)pluginHost; Attributes = myAttributes; // Get configuration config = IniConfig.GetIniSection(); resources = new ComponentResourceManager(typeof(PhotobucketPlugin)); itemPlugInConfig = new ToolStripMenuItem(Language.GetString("photobucket", LangKey.configure)); itemPlugInConfig.Tag = host; itemPlugInConfig.Click += delegate { config.ShowConfigDialog(); }; itemPlugInConfig.Image = (Image)resources.GetObject("Photobucket"); PluginUtils.AddToContextMenu(host, itemPlugInConfig); Language.LanguageChanged += new LanguageChangedHandler(OnLanguageChanged); return true; } public void OnLanguageChanged(object sender, EventArgs e) { if (itemPlugInConfig != null) { itemPlugInConfig.Text = Language.GetString("photobucket", LangKey.configure); } } public virtual void Shutdown() { LOG.Debug("Photobucket Plugin shutdown."); Language.LanguageChanged -= new LanguageChangedHandler(OnLanguageChanged); } /// /// Implementation of the IPlugin.Configure /// public virtual void Configure() { config.ShowConfigDialog(); } /// /// This will be called when Greenshot is shutting down /// /// /// public void Closing(object sender, FormClosingEventArgs e) { LOG.Debug("Application closing, de-registering Photobucket Plugin!"); Shutdown(); } /// /// Upload the capture to Photobucket /// /// /// ISurface /// out string for the url /// true if the upload succeeded public bool Upload(ICaptureDetails captureDetails, ISurface surfaceToUpload, string albumPath, out string uploadURL) { SurfaceOutputSettings outputSettings = new SurfaceOutputSettings(config.UploadFormat, config.UploadJpegQuality, config.UploadReduceColors); try { string filename = Path.GetFileName(FilenameHelper.GetFilename(config.UploadFormat, captureDetails)); PhotobucketInfo photobucketInfo = null; // Run upload in the background new PleaseWaitForm().ShowAndWait(Attributes.Name, Language.GetString("photobucket", LangKey.communication_wait), delegate() { photobucketInfo = PhotobucketUtils.UploadToPhotobucket(surfaceToUpload, outputSettings, albumPath, captureDetails.Title, filename); } ); // This causes an exeption if the upload failed :) LOG.DebugFormat("Uploaded to Photobucket page: " + photobucketInfo.Page); uploadURL = null; try { if (config.UsePageLink) { uploadURL = photobucketInfo.Page; Clipboard.SetText(photobucketInfo.Page); } else { uploadURL = photobucketInfo.Original; Clipboard.SetText(photobucketInfo.Original); } } catch (Exception ex) { LOG.Error("Can't write to clipboard: ", ex); } return true; } catch (Exception e) { LOG.Error(e); MessageBox.Show(Language.GetString("photobucket", LangKey.upload_failure) + " " + e.Message); } uploadURL = null; return false; } } }