Click here to Skip to main content
15,884,901 members
Articles / Programming Languages / C#

Linkify Add-in for Visual Studio

Rate me:
Please Sign up or sign in to vote.
4.59/5 (23 votes)
2 Aug 2008CPOL9 min read 170.5K   433   99  
Link source code comments to your bug tracker, MSDN, development Wiki and more.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace Linkify
{
  public partial class FormConfig : Form
  {
    public Settings EditSettings = null;
    private ListViewItem m_editedItem = null;

    string m_lastFileName = null;

    Utility.ComboBoxTagger ddSeparatorTag;

    public FormConfig()
    {
      InitializeComponent();

      ddSeparatorTag = new Utility.ComboBoxTagger(ddSeparator);

      ddSeparatorTag.Add("Default", Protocol.Separator.SpecialChar);
      ddSeparatorTag.Add("Space", Protocol.Separator.Space);
      ddSeparatorTag.Add("Remainder of line", Protocol.Separator.LineRemainder);
      ddSeparatorTag.Add("RegEx", Protocol.Separator.RegEx);
      ddSeparatorTag.SelectedItem = Protocol.Separator.SpecialChar;
    }


    // ----- BeginEditSettings ---------------------------
    private void BeginEditSettings(ListViewItem lvi)
    {
      Protocol p = lvi.Tag as Protocol;

      edPrefix.Text = p.prefix;
      edExe.Text = p.exe;
      edArgs.Text = p.args;
      edInfo.Text = p.info;
      cbURLEscape.Checked = p.urlEscape;
      m_editedItem = lvi;
      ddSeparatorTag.SelectedItem = p.separator;
      cbConfirm.Checked = p.confirmExecution;
      edUtilityURL.Text = p.utilityUrl;
      cbExpandEnvStr.Checked = p.expandEnvVars;
      edRegEx.Text = p.regEx;

      EnableEdits(true);
      UpdateDynamics();

    }

    private void UpdateDynamics()
    {
      lblRegEx.Visible = edRegEx.Visible = 
        (ddSeparatorTag.SelectedItem != null ? (Protocol.Separator) ddSeparatorTag.SelectedItem : Protocol.Separator.Space) == Protocol.Separator.RegEx;
    }

    // ----- SaveChangedSettings ---------------------------
    private void SaveChangedSettings()
    {
      if (m_editedItem == null)
        return;

      ListViewItem lvi = m_editedItem;
      m_editedItem = null;

      Protocol p = lvi.Tag as Protocol;

      String prefix = edPrefix.Text.Trim();
      if (prefix.Length == 0)
      {
        EditSettings.protocols.Remove(p);
        UpdateList();
        return;
      }
      p.prefix = prefix;
      p.exe = edExe.Text.Trim();
      p.args = edArgs.Text.Trim();
      p.info = edInfo.Text.Trim();
      p.urlEscape = cbURLEscape.Checked;
      p.confirmExecution = cbConfirm.Checked;
      p.enabled = lvi.Checked;
      p.expandEnvVars = cbExpandEnvStr.Checked;

      p.regEx = String.Empty;
      if (ddSeparatorTag.SelectedItem != null)
      {
        p.separator = (Protocol.Separator)ddSeparatorTag.SelectedItem;
        if (p.separator == Protocol.Separator.RegEx)
          p.regEx = edRegEx.Text;
      }
      p.utilityUrl = edUtilityURL.Text;

      EnableEdits(false);
      UpdateList();
    }

    // ----- EnableEdits -----------------------------------
    private void EnableEdits(bool enable)
    {
      edPrefix.Enabled = enable;
      edExe.Enabled = enable;
      edArgs.Enabled = enable;
      edInfo.Enabled = enable;
      cbBrowseTarget.Enabled = enable;
      cbURLEscape.Enabled = enable;
      ddSeparator.Enabled = enable;
      cbConfirm.Enabled = enable;
      edRegEx.Enabled = enable;
      edUtilityURL.Enabled = enable;
      lblGotoUtilityURL.Enabled = enable;

      if (!enable)
      {
        edPrefix.Text = "";
        edExe.Text = "";
        edArgs.Text = "";
        edInfo.Text = "";
        cbURLEscape.Checked = false;
        cbConfirm.Checked = false;
        ddSeparatorTag.SelectedItem = null;
        edRegEx.Text = "";
        edUtilityURL.Text = "";
      }
    }


    // ----- Load ---------------------------
    private void ConfigForm_Load(object sender, EventArgs e)
    {
    }


    // ----- UpdateItem ---------------------------
    private void UpdateItem(ListViewItem lvi, Protocol p)
    {
      if (p == null)
      {
        p = lvi.Tag as Protocol;
        if (p == null)
          p = new Protocol();
      }

      lvi.Text = p.prefix;
      lvi.Checked = p.enabled;

      // other columns: removed in 1.3
      //while (lvi.SubItems.Count < 4)
      //  lvi.SubItems.Add("");

      //lvi.SubItems[1].Text = p.info;
      //lvi.SubItems[2].Text = p.exe;
      //lvi.SubItems[3].Text = p.args;

      lvi.Tag = p;
    }

    private void UpdateList()
    {
      int count = EditSettings.protocols.Count;
      while (lv.Items.Count > count)
        lv.Items.RemoveAt(lv.Items.Count - 1);
      while (lv.Items.Count < count)
        lv.Items.Add(new ListViewItem());

      for (int i = 0; i < lv.Items.Count; ++i)
        UpdateItem(lv.Items[i], EditSettings.protocols[i]);
    }

    private void ConfigForm_Shown(object sender, EventArgs e)
    {
      EnableEdits(false);
      UpdateList();
      enableTestModeToolStripMenuItem.Checked = EditSettings.TestMode;
      shiftForcesConfigDialogToolStripMenuItem.Checked = EditSettings.ShiftForcesConfigMode;
    }

    private void cbNew_Click(object sender, EventArgs e)
    {
      SaveChangedSettings();
      Protocol p = new Protocol();
      EditSettings.protocols.Add(p);
      UpdateList();
      lv.Items[EditSettings.protocols.Count - 1].Selected = true;
      edPrefix.Select();
    }

    private void cbRemove_Click(object sender, EventArgs e)
    {
      SaveChangedSettings();
      Protocol p = GetSelectedProtocol();
      if (p != null)
      {
        EditSettings.protocols.Remove(p);
        UpdateList();
      }
    }

    private void lv_RetrieveVirtualItem(object sender, RetrieveVirtualItemEventArgs e)
    {
      if (e.ItemIndex < EditSettings.protocols.Count)
      {
        if (e.Item == null)
          e.Item = new ListViewItem();
        UpdateItem(e.Item, EditSettings.protocols[e.ItemIndex]);
      }
    }

    private void cbOK_Click(object sender, EventArgs e)
    {
      SaveChangedSettings();
    }

    int GetSelectedIndex()
    {
      if (lv.SelectedIndices.Count == 1)
        return lv.SelectedIndices[0];
      return -1;
    }

    Protocol GetSelectedProtocol()
    {
      int idx = GetSelectedIndex();
      if (idx >= 0)
        return lv.Items[idx].Tag as Protocol;
      return null;
    }

    private void lv_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
    {
      SaveChangedSettings();
      if (e.IsSelected)
        BeginEditSettings(e.Item);
    }


    private void edits_KeyDown(object sender, KeyEventArgs e)
    {

      int newSelPos = GetSelectedIndex();
      if (e.KeyCode == Keys.Up && e.Modifiers == 0)
      {
        --newSelPos;
      }
      else if (e.KeyCode == Keys.Down && e.Modifiers == 0)
      {
        ++newSelPos;
      }
      else
        return;

      if (lv.VirtualListSize != 0)
      {
        if (newSelPos < 0)
          newSelPos = 0;
        else if (newSelPos >= lv.VirtualListSize)
          newSelPos = lv.VirtualListSize - 1;

        lv.Items[newSelPos].Selected = true;
      }

      e.SuppressKeyPress = true;

      Control c = sender as Control;
      if (c != null)
        c.Select();
    }

    private void cbMore_Click(object sender, EventArgs e)
    {
      mnuMore.Show(cbMore, 0, cbMore.Height);
    }

    const string LinkifyFilter = "Linkify Settings (*.linkify)|*.linkify|All Files (*.*)|*.*";

    private void exportToolStripMenuItem_Click(object sender, EventArgs e)
    {
      SaveFileDialog fdlg = new SaveFileDialog();
      fdlg.CheckPathExists = true;
      fdlg.DefaultExt = "linkify";
      fdlg.Filter = LinkifyFilter;
      fdlg.ValidateNames = true;
      fdlg.Title = "Export Linkify Settings to...";

      if (fdlg.ShowDialog(this) != DialogResult.OK)
        return;

      m_lastFileName = fdlg.FileName;
      lblGotoFile.Visible = true;
      try
      {
        EditSettings.Save(fdlg.FileName);
      }
      catch (Exception x)
      {
        MessageBox.Show("Cannot export settings.\n" + x.Message);
      }
    }

    private Settings ImportLoadFile(string dlgTitle)
    {
      OpenFileDialog dlg = new OpenFileDialog();
      dlg.CheckFileExists = true;
      dlg.Multiselect = false;
      dlg.ShowReadOnly = false;
      dlg.Filter = LinkifyFilter;
      dlg.Title = dlgTitle;

      if (dlg.ShowDialog(this) != DialogResult.OK)
        return null;

      m_lastFileName = dlg.FileName;
      lblGotoFile.Visible = true;

      try
      {
        Settings settings = Settings.Load(dlg.FileName);
        return settings;
      }
      catch (Exception x)
      {
        MessageBox.Show("Cannot import settings\n" + x.Message);
        return null;
      }

    }

    private void importToolStripMenuItem_Click(object sender, EventArgs e)
    {
      Settings newSettings = ImportLoadFile("Import from file (replaces all)");

      if (newSettings != null)
      {
        EditSettings = newSettings;
        UpdateList();
      }
    }

    private void importaddToolStripMenuItem_Click(object sender, EventArgs e)
    {
      Settings newSettings = ImportLoadFile("Import from file (only add new)");

      if (newSettings != null)
      {
        EditSettings.Merge(newSettings, false);
        UpdateList();
      }
    }

    private void importmergeToolStripMenuItem_Click(object sender, EventArgs e)
    {
      Settings newSettings = ImportLoadFile("Import from file (overwrites existing)");

      if (newSettings != null)
      {
        EditSettings.Merge(newSettings, true);
        UpdateList();
      }
    }

    private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
    {
      // TODO: show about.html from resource
      FormAbout form = new FormAbout();
      form.Show(this);
    }

    private void addDefaultsToolStripMenuItem_Click(object sender, EventArgs e)
    {

      EditSettings.Merge(Settings.GenerateSample(), false);
      UpdateList();
    }

    private void lblGotoFile_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
    {
      if (m_lastFileName != null)
      {
        string args = "/select," + '"' + m_lastFileName + '"';
        ProcessStartInfo psi = new ProcessStartInfo("explorer.exe", args);
        psi.UseShellExecute = true;
        System.Diagnostics.Process.Start(psi);
      }
    }

    private void cbBrowseTarget_Click(object sender, EventArgs e)
    {
      OpenFileDialog dlg = new OpenFileDialog();
      dlg.CheckFileExists = false;
      dlg.Multiselect = false;
      dlg.ShowReadOnly = false;
      dlg.Filter = "All files (*.*)|*.*|Executable files (*.exe)|*.exe";
      dlg.Title = "Select Target URL / Executable";
      dlg.FileName = edExe.Text;

      if (dlg.ShowDialog(this) != DialogResult.OK)
        return;

      edExe.Text = dlg.FileName;
    }

    private void enableTestModeToolStripMenuItem_Click(object sender, EventArgs e)
    {
      EditSettings.TestMode = !EditSettings.TestMode;
      enableTestModeToolStripMenuItem.Checked = EditSettings.TestMode;
    }

    private void shiftForcesConfigDialogToolStripMenuItem_Click(object sender, EventArgs e)
    {
      EditSettings.ShiftForcesConfigMode = !EditSettings.ShiftForcesConfigMode;
      shiftForcesConfigDialogToolStripMenuItem.Checked = EditSettings.ShiftForcesConfigMode;
    }

    private void ddSeparator_SelectedIndexChanged(object sender, EventArgs e)
    {
      UpdateDynamics();
    }

    private void lblGotoUtilityURL_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
    {
      string url = edUtilityURL.Text.Trim();
      if (url == "")
        return;

      // TODO: if we could turn foobar.com into an url, and validate that we have an URL, we could use the default browser
      ProcessStartInfo psi = new ProcessStartInfo("iexplore.exe", url);
      psi.UseShellExecute = true;
      System.Diagnostics.Process.Start(psi);

    }

    private void lv_ItemChecked(object sender, ItemCheckedEventArgs e)
    {
      Protocol p = e.Item.Tag as Protocol;
      if (p == null)
        return;

      p.enabled = !p.enabled; //  e.Item.Checked;
    }


  }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Klippel
Germany Germany
Peter is tired of being called "Mr. Chen", even so certain individuals insist on it. No, he's not chinese.

Peter has seen lots of boxes you youngsters wouldn't even accept as calculators. He is proud of having visited the insides of a 16 Bit Machine.

In his spare time he ponders new ways of turning groceries into biohazards, or tries to coax South American officials to add some stamps to his passport.

Beyond these trivialities Peter works for Klippel[^], a small german company that wants to make mankind happier by selling them novel loudspeaker measurement equipment.


Where are you from?[^]



Please, if you are using one of my articles for anything, just leave me a comment. Seeing that this stuff is actually useful to someone is what keeps me posting and updating them.
Should you happen to not like it, tell me, too

Comments and Discussions