Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Drawing;
- using System.IO;
- using System.Net;
- using System.Reflection;
- using System.Windows.Forms;
- using EnvDTE;
- using Extensibility;
- using Microsoft.VisualStudio.CommandBars;
- namespace PasteBin
- {
- public class Connect : IDTExtensibility2, IDTCommandTarget
- {
- // Constants for command properties
- private const string CommandName = "PasteBin";
- private const string CommandCaption = "PasteBin";
- private const string Url = "http://pastebin.com";
- private const string RequestParameters = "parent_pid=&format={0}&code2={1}&poster=&paste=Send&expiry=f&email=";
- // Constants for file formats
- private static readonly Dictionary<string, string> FileFormat = new Dictionary<string, string>
- {
- {"c","c"},
- {"h","c"},
- {"cpp","cpp"},
- {"cs","csharp"}
- };
- // Variables for IDE and add-in instances
- private DTE _applicationObject;
- private AddIn _addInInstance;
- // Buttons that will be created on built-in command bars of Visual Studio
- // We must keep them at class level to remove them when the add-in is unloaded
- private CommandBarButton _pasteBinButton;
- public void OnConnection(object application, ext_ConnectMode connectMode,
- object addInInst, ref Array custom)
- {
- try
- {
- _applicationObject = (DTE)application;
- _addInInstance = (AddIn)addInInst;
- switch (connectMode)
- {
- case ext_ConnectMode.ext_cm_UISetup:
- // Do nothing for this add-in with temporary user interface
- break;
- case ext_ConnectMode.ext_cm_Startup:
- // The add-in was marked to load on startup
- // Do nothing at this point because the IDE may not be fully initialized
- // Visual Studio will call OnStartupComplete when fully initialized
- break;
- case ext_ConnectMode.ext_cm_AfterStartup:
- // The add-in was loaded by hand after startup using the Add-In Manager
- // Initialize it in the same way that when is loaded on startup
- AddUI();
- break;
- }
- }
- catch (Exception e)
- {
- MessageBox.Show(e.ToString());
- }
- }
- public void OnStartupComplete(ref Array custom)
- {
- AddUI();
- }
- private void AddUI()
- {
- // Constants for names of built-in command bars of Visual Studio
- const string vsCodeWindowCommandbarName = "Code Window";
- // The only command that will be created. We will create several buttons from it
- Command myCommand = null;
- // Built-in command bars of Visual Studio
- CommandBar codeCommandBar;
- // Other variables
- var contextUIGuids = new object[] { };
- try
- {
- // The collection of Visual Studio command bars
- // ------------------------------------------------------------------------------------
- // Try to retrieve the command, just in case it was already created, ignoring the
- // exception that would happen if the command was not created yet.
- try
- {
- myCommand = _applicationObject.Commands.Item(_addInInstance.ProgID + "." + CommandName, -1);
- }
- catch (Exception e)
- {
- Debug.Write(e.ToString());
- }
- // Add the command if it does not exist
- if (myCommand == null)
- {
- myCommand = _applicationObject.Commands.AddNamedCommand(_addInInstance,
- CommandName, CommandCaption, null, true, 59, ref contextUIGuids,
- (int)(vsCommandStatus.vsCommandStatusSupported | vsCommandStatus.vsCommandStatusEnabled));
- }
- // ------------------------------------------------------------------------------------
- // Retrieve the collection of command bars
- // Note:
- // - In VS.NET 2002/2003 (which uses the Office.dll reference)
- // DTE.CommandBars returns directly a CommandBars type, so a cast
- // to CommandBars is redundant
- // - In VS 2005 or higher (which uses the new Microsoft.VisualStudio.CommandBars.dll reference)
- // DTE.CommandBars returns an Object type, so we do need a cast to CommandBars
- var commandBars = (CommandBars)_applicationObject.CommandBars;
- // Retrieve some built-in command bars
- codeCommandBar = commandBars[vsCodeWindowCommandbarName];
- // ------------------------------------------------------------------------------------
- // Create the buttons from the commands
- // Note:
- // - In VS.NET 2002/2003 (which uses the Office.dll reference)
- // Command.AddControl returns directly a CommandBarControl type, so a cast
- // to CommandBarControl is redundant
- // - In VS 2005 or higher (which uses the new Microsoft.VisualStudio.CommandBars.dll reference)
- // Command.AddControl returns an Object type, so we do need a cast to CommandBarControl
- string ns = GetType().Namespace;
- Assembly currentAssembly = GetType().Assembly;
- Stream
- imgStreamPic = currentAssembly.GetManifestResourceStream(ns + "." + "klipper.png"),
- imgStreamMask = currentAssembly.GetManifestResourceStream(ns + "." + "klipper-mask.png");
- if (imgStreamPic != null && imgStreamMask != null)
- {
- Image
- pasteBinImage = Image.FromStream(imgStreamPic),
- pasteBinImageMask = Image.FromStream(imgStreamMask);
- // ------------------------------------------------------------------------------------
- // Button on the "Code Window" context menu
- // ------------------------------------------------------------------------------------
- // Add a button to the built-in "Code Window" context menu
- _pasteBinButton = (CommandBarButton) myCommand.AddControl(codeCommandBar,
- codeCommandBar.Controls.Count + 1);
- // Change some button properties
- _pasteBinButton.Caption = CommandCaption;
- _pasteBinButton.BeginGroup = true; // Separator line above button
- _pasteBinButton.Style = MsoButtonStyle.msoButtonIconAndCaption; // It could be also msoButtonIcon
- _pasteBinButton.Picture = (stdole.StdPicture) ImageConverter.ImageToIpicture(pasteBinImage);
- _pasteBinButton.Mask = (stdole.StdPicture) ImageConverter.ImageToIpicture(pasteBinImageMask);
- }
- }
- catch (Exception e)
- {
- MessageBox.Show(e.ToString());
- }
- }
- public void OnDisconnection(ext_DisconnectMode removeMode, ref Array custom)
- {
- try
- {
- switch (removeMode)
- {
- case ext_DisconnectMode.ext_dm_HostShutdown:
- case ext_DisconnectMode.ext_dm_UserClosed:
- if ((_pasteBinButton != null))
- {
- _pasteBinButton.Delete(true);
- }
- break;
- }
- }
- catch (Exception e)
- {
- MessageBox.Show(e.ToString());
- }
- }
- public void OnBeginShutdown(ref Array custom)
- {
- }
- public void OnAddInsUpdate(ref Array custom)
- {
- }
- public void Exec(string cmdName, vsCommandExecOption executeOption, ref object varIn,
- ref object varOut, ref bool handled)
- {
- handled = false;
- if (executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault)
- {
- if (cmdName == string.Format("{0}.{1}", _addInInstance.ProgID, CommandName))
- {
- handled = true;
- if (!((TextSelection)_applicationObject.ActiveDocument.Selection).IsEmpty)
- {
- // parameters: name1=value1&name2=value2
- var webRequest = (HttpWebRequest)WebRequest.Create(Url);
- webRequest.ContentType = "application/x-www-form-urlencoded";
- webRequest.Method = "POST";
- // formatting request string from document type and selection
- string request = string.Format(RequestParameters,
- FileFormat[_applicationObject.ActiveDocument.Name.Split('.')[1]],
- System.Web.HttpUtility.UrlEncode(((TextSelection)_applicationObject.ActiveDocument.Selection).Text));
- Stream os = null;
- try
- { // send the POST
- webRequest.ContentLength = request.Length; //Length of request to send
- os = webRequest.GetRequestStream();
- os.Write(System.Text.Encoding.GetEncoding("windows-1251").GetBytes(request), 0, request.Length); //Send it
- }
- catch (WebException ex)
- {
- MessageBox.Show(ex.Message, "HttpPost: Request error",
- MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- finally
- {
- if (os != null)
- {
- os.Close();
- }
- }
- try
- { // get the response
- var webResponse = (HttpWebResponse)webRequest.GetResponse();
- if (webResponse != null)
- { // copy to clipboard
- Clipboard.SetText(webResponse.ResponseUri.ToString());
- MessageBox.Show("Upload successfully. Url is copied in your clipboard.");
- }
- }
- catch (WebException ex)
- {
- MessageBox.Show(ex.Message, "HttpPost: Response error",
- MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- }
- }
- }
- public void QueryStatus(string cmdName, vsCommandStatusTextWanted neededText,
- ref vsCommandStatus statusOption, ref object commandText)
- {
- if (neededText != vsCommandStatusTextWanted.vsCommandStatusTextWantedNone) return;
- if (cmdName == string.Format("{0}.{1}", _addInInstance.ProgID, CommandName))
- {
- statusOption = vsCommandStatus.vsCommandStatusEnabled |
- vsCommandStatus.vsCommandStatusSupported;
- }
- else
- {
- statusOption = vsCommandStatus.vsCommandStatusUnsupported;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement