Click here to Skip to main content
15,896,539 members
Articles / Programming Languages / C#

CodeProject Quoter

Rate me:
Please Sign up or sign in to vote.
4.90/5 (28 votes)
23 Jun 20036 min read 167.3K   1.1K   71  
Simple tool for storing and retrieving the quotes and using them in the signatures.
using System;
using System.Drawing; // For Rectangle
using System.ComponentModel;
using System.Windows.Forms;
using System.IO; // For Directory 
using System.Diagnostics;
using System.Runtime.InteropServices; // For DllImport
using Microsoft.Win32; // For Registry
using System.Text;
using System.Collections;
using System.Reflection; // For Assembly
using System.Xml;
using Kant.QuotesLibrary;
using System.Net;

namespace CodeProjectQuoter
{
	/// <summary>
	/// Summary description for CPQMainForm.
	/// </summary>
	public class CPQMainForm : System.Windows.Forms.Form
	{
	
		//		private static BooleanSwitch CPQDebugSwitch = new BooleanSwitch("CPQSwitch", "Debug Switch for Trace and Debug.");

		#region Fields

		static int BITMAP_SIZE = 16;
		protected static ImageList toolbarImages;
		static Color transparentColor = Color.FromArgb(192, 192, 192);
		protected QuotesCollection m_quotesCollection;
		protected bool m_bCollectionChanged = false;
		static private string m_strOutlookSigDir = null;

		string m_strOutlookTextFile = null;
		string m_strOutlookHTMLFile = null;
		string m_strDesktopBobLoc = null;
		string m_strAutoSigLoc = null;

		private enum OpenOptions
		{
			LocalFile,
			FromWeb
		}
		private OpenOptions m_selectedOpenOption = OpenOptions.LocalFile;

		private enum CopyOptions
		{
			HTMLClipbrd = 0,
			TextClipbrd = 1,
			OutlookDirHTML = 3,
			OutlookDirText = 4,
			SelectedTextToClipbrd = 6,
			FileToDesktopBobLoc = 8,
			FileToAutoSigLoc = 9
		}
		
		private CopyOptions m_selectedCopyOption = CopyOptions.SelectedTextToClipbrd;

		public enum DotNetServicePacks
		{
			FrameWork_1_0,
			FrameWork_1_0_SP2,
			FrameWork_1_1,
			NoFrameworkFound
		}

		private System.Windows.Forms.OpenFileDialog openFileDialog;
		private System.Windows.Forms.MainMenu mainMenu1;
		private System.Windows.Forms.Button button_Exit;
		private System.Windows.Forms.MenuItem menuItem_Exit;
		private System.Windows.Forms.Button button_FirstQuote;
		private System.Windows.Forms.Button button_PrevQuote;
		private System.Windows.Forms.Button button_NextQuote;
		private System.Windows.Forms.Button button_LastQuote;
		private System.ComponentModel.IContainer components;
		public string m_strFileName;

		private System.Drawing.Icon mTrayIcon;

		private System.Windows.Forms.ContextMenu contextMenu1;
		private System.Windows.Forms.HelpProvider helpProvider1;
		private System.Windows.Forms.ToolTip toolTip1;
		private System.Windows.Forms.ToolBar toolBar1;
		private System.Windows.Forms.FontDialog fontDialog1;
		private System.Windows.Forms.TabControl tabControl1;
		private System.Windows.Forms.TabPage tabQuotes;
		private System.Windows.Forms.TabPage tabHTMLSource;
		private onlyconnect.HtmlEditor htmlEditor1;
		private System.Windows.Forms.TextBox textBox_Quotes;
		private System.Windows.Forms.MenuItem menuNew;
		private System.Windows.Forms.StatusBar statusBar1;
		private System.Windows.Forms.MenuItem menuSave;
		private System.Windows.Forms.MenuItem menuItem2;
		private System.Windows.Forms.MenuItem menuItem3;
		private System.Windows.Forms.MenuItem menuSaveAs;
		private System.Windows.Forms.MenuItem menuItem10;
		private System.Windows.Forms.MenuItem menuRemove;
		private System.Windows.Forms.MenuItem menuClose;
		private System.Windows.Forms.MenuItem menuFile;
		private System.Windows.Forms.MenuItem menuEdit;
		private System.Windows.Forms.MenuItem menuHelp;
		private System.Windows.Forms.MenuItem menuCopy;
		private System.Windows.Forms.MenuItem menuCopyToClipbrd;
		private System.Windows.Forms.MenuItem menuCopyToOutlookDir;
		private System.Windows.Forms.MenuItem menuCopySelToClipbrd;
		private System.Windows.Forms.MenuItem menuCopyToClipboardAsText;
		private System.Windows.Forms.MenuItem menuNewQuote;
		private System.Windows.Forms.Button button_Random;
		private System.Windows.Forms.MenuItem menuCopyToOutlookDirAsText;
		private System.Windows.Forms.NotifyIcon notifyIcon1;
		private System.Windows.Forms.ToolBarButton toolBarButton1;
		private System.Windows.Forms.ToolBarButton toolBarFontDialog;
		private System.Windows.Forms.ToolBarButton toolBarBold;
		private System.Windows.Forms.ToolBarButton toolBarItalic;
		private System.Windows.Forms.ToolBarButton toolBarUnderline;
		private System.Windows.Forms.ToolBarButton toolBarURL;
		private System.Windows.Forms.ToolBarButton toolBarAlignLeft;
		private System.Windows.Forms.ToolBarButton toolBarAlignCenter;
		private System.Windows.Forms.ToolBarButton toolBarAlignRight;
		private System.Windows.Forms.ToolBarButton toolBarBullets;
		private System.Windows.Forms.StatusBarPanel statusBarPanelFileName;
		private System.Windows.Forms.StatusBarPanel statusBarPanelQuotes;
		private System.Windows.Forms.MenuItem menuOpenFromWeb;
		private System.Windows.Forms.MenuItem menuItem_Open;
		private System.Windows.Forms.MenuItem menuLoad;
		private System.Windows.Forms.MenuItem menuItem1;
		private System.Windows.Forms.MenuItem menuItem4;
		private System.Windows.Forms.MenuItem menuItem6;
		private System.Windows.Forms.MenuItem menuCopyXMLToDB;
		private System.Windows.Forms.MenuItem menuView;
		private System.Windows.Forms.MenuItem menuHTMLControlOption;
		private System.Windows.Forms.MenuItem menuCopyXMLToAutoSig;
		private System.Windows.Forms.MenuItem menuOptions;
		private System.Windows.Forms.MenuItem menuItem5;
		private System.Windows.Forms.SaveFileDialog saveFileDialog1;

		#endregion

		#region Single Instance code - Declaration

		// Only one instance is allowed. Code from Marc Clifton.

		[DllImport("user32.dll")] private static extern 
			bool SetForegroundWindow(IntPtr hWnd);
		[DllImport("user32.dll")] private static extern 
			bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
		[DllImport("user32.dll")] private static extern 
			bool IsIconic(IntPtr hWnd);

		private const int SW_HIDE = 0;
		private const int SW_SHOWNORMAL = 1;
		private const int SW_SHOWMINIMIZED = 2;
		private const int SW_SHOWMAXIMIZED = 3;
		private const int SW_SHOWNOACTIVATE = 4;
		private const int SW_RESTORE = 9;
		private const int SW_SHOWDEFAULT = 10;

		#endregion

		public CPQMainForm()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			m_quotesCollection = new QuotesCollection();

			SetupTrayIcon();
			LoadSettingsFromXML();	
		}



		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.components = new System.ComponentModel.Container();
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(CPQMainForm));
			this.button_FirstQuote = new System.Windows.Forms.Button();
			this.button_PrevQuote = new System.Windows.Forms.Button();
			this.button_NextQuote = new System.Windows.Forms.Button();
			this.button_LastQuote = new System.Windows.Forms.Button();
			this.button_Exit = new System.Windows.Forms.Button();
			this.openFileDialog = new System.Windows.Forms.OpenFileDialog();
			this.mainMenu1 = new System.Windows.Forms.MainMenu();
			this.menuFile = new System.Windows.Forms.MenuItem();
			this.menuNew = new System.Windows.Forms.MenuItem();
			this.menuItem_Open = new System.Windows.Forms.MenuItem();
			this.menuLoad = new System.Windows.Forms.MenuItem();
			this.menuOpenFromWeb = new System.Windows.Forms.MenuItem();
			this.menuClose = new System.Windows.Forms.MenuItem();
			this.menuItem2 = new System.Windows.Forms.MenuItem();
			this.menuSave = new System.Windows.Forms.MenuItem();
			this.menuSaveAs = new System.Windows.Forms.MenuItem();
			this.menuItem3 = new System.Windows.Forms.MenuItem();
			this.menuItem_Exit = new System.Windows.Forms.MenuItem();
			this.menuEdit = new System.Windows.Forms.MenuItem();
			this.menuCopy = new System.Windows.Forms.MenuItem();
			this.menuCopyToClipbrd = new System.Windows.Forms.MenuItem();
			this.menuCopyToClipboardAsText = new System.Windows.Forms.MenuItem();
			this.menuItem1 = new System.Windows.Forms.MenuItem();
			this.menuCopyToOutlookDir = new System.Windows.Forms.MenuItem();
			this.menuCopyToOutlookDirAsText = new System.Windows.Forms.MenuItem();
			this.menuItem4 = new System.Windows.Forms.MenuItem();
			this.menuCopySelToClipbrd = new System.Windows.Forms.MenuItem();
			this.menuItem6 = new System.Windows.Forms.MenuItem();
			this.menuCopyXMLToDB = new System.Windows.Forms.MenuItem();
			this.menuCopyXMLToAutoSig = new System.Windows.Forms.MenuItem();
			this.menuNewQuote = new System.Windows.Forms.MenuItem();
			this.menuRemove = new System.Windows.Forms.MenuItem();
			this.menuItem5 = new System.Windows.Forms.MenuItem();
			this.menuOptions = new System.Windows.Forms.MenuItem();
			this.menuView = new System.Windows.Forms.MenuItem();
			this.menuHTMLControlOption = new System.Windows.Forms.MenuItem();
			this.menuHelp = new System.Windows.Forms.MenuItem();
			this.menuItem10 = new System.Windows.Forms.MenuItem();
			this.contextMenu1 = new System.Windows.Forms.ContextMenu();
			this.helpProvider1 = new System.Windows.Forms.HelpProvider();
			this.toolTip1 = new System.Windows.Forms.ToolTip(this.components);
			this.htmlEditor1 = new onlyconnect.HtmlEditor();
			this.button_Random = new System.Windows.Forms.Button();
			this.toolBar1 = new System.Windows.Forms.ToolBar();
			this.toolBarButton1 = new System.Windows.Forms.ToolBarButton();
			this.toolBarFontDialog = new System.Windows.Forms.ToolBarButton();
			this.toolBarBold = new System.Windows.Forms.ToolBarButton();
			this.toolBarItalic = new System.Windows.Forms.ToolBarButton();
			this.toolBarUnderline = new System.Windows.Forms.ToolBarButton();
			this.toolBarURL = new System.Windows.Forms.ToolBarButton();
			this.toolBarAlignLeft = new System.Windows.Forms.ToolBarButton();
			this.toolBarAlignCenter = new System.Windows.Forms.ToolBarButton();
			this.toolBarAlignRight = new System.Windows.Forms.ToolBarButton();
			this.toolBarBullets = new System.Windows.Forms.ToolBarButton();
			this.fontDialog1 = new System.Windows.Forms.FontDialog();
			this.tabControl1 = new System.Windows.Forms.TabControl();
			this.tabQuotes = new System.Windows.Forms.TabPage();
			this.tabHTMLSource = new System.Windows.Forms.TabPage();
			this.textBox_Quotes = new System.Windows.Forms.TextBox();
			this.statusBar1 = new System.Windows.Forms.StatusBar();
			this.statusBarPanelFileName = new System.Windows.Forms.StatusBarPanel();
			this.statusBarPanelQuotes = new System.Windows.Forms.StatusBarPanel();
			this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
			this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components);
			this.tabControl1.SuspendLayout();
			this.tabQuotes.SuspendLayout();
			this.tabHTMLSource.SuspendLayout();
			((System.ComponentModel.ISupportInitialize)(this.statusBarPanelFileName)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.statusBarPanelQuotes)).BeginInit();
			this.SuspendLayout();
			// 
			// button_FirstQuote
			// 
			this.button_FirstQuote.Enabled = false;
			this.button_FirstQuote.Location = new System.Drawing.Point(8, 312);
			this.button_FirstQuote.Name = "button_FirstQuote";
			this.button_FirstQuote.Size = new System.Drawing.Size(32, 24);
			this.button_FirstQuote.TabIndex = 1;
			this.button_FirstQuote.Text = "<<";
			this.toolTip1.SetToolTip(this.button_FirstQuote, "First");
			this.button_FirstQuote.Click += new System.EventHandler(this.button_FirstQuote_Click);
			// 
			// button_PrevQuote
			// 
			this.button_PrevQuote.Enabled = false;
			this.button_PrevQuote.Location = new System.Drawing.Point(56, 312);
			this.button_PrevQuote.Name = "button_PrevQuote";
			this.button_PrevQuote.Size = new System.Drawing.Size(32, 24);
			this.button_PrevQuote.TabIndex = 2;
			this.button_PrevQuote.Text = "<";
			this.toolTip1.SetToolTip(this.button_PrevQuote, "Previous");
			this.button_PrevQuote.Click += new System.EventHandler(this.button_PrevQuote_Click);
			// 
			// button_NextQuote
			// 
			this.button_NextQuote.Enabled = false;
			this.button_NextQuote.Location = new System.Drawing.Point(104, 312);
			this.button_NextQuote.Name = "button_NextQuote";
			this.button_NextQuote.Size = new System.Drawing.Size(32, 24);
			this.button_NextQuote.TabIndex = 3;
			this.button_NextQuote.Text = ">";
			this.toolTip1.SetToolTip(this.button_NextQuote, "Next");
			this.button_NextQuote.Click += new System.EventHandler(this.button_NextQuote_Click);
			// 
			// button_LastQuote
			// 
			this.button_LastQuote.Enabled = false;
			this.button_LastQuote.Location = new System.Drawing.Point(152, 312);
			this.button_LastQuote.Name = "button_LastQuote";
			this.button_LastQuote.Size = new System.Drawing.Size(32, 23);
			this.button_LastQuote.TabIndex = 4;
			this.button_LastQuote.Text = ">>";
			this.toolTip1.SetToolTip(this.button_LastQuote, "Last");
			this.button_LastQuote.Click += new System.EventHandler(this.button_LastQuote_Click);
			// 
			// button_Exit
			// 
			this.button_Exit.Location = new System.Drawing.Point(424, 312);
			this.button_Exit.Name = "button_Exit";
			this.button_Exit.TabIndex = 6;
			this.button_Exit.Text = "E&xit";
			this.toolTip1.SetToolTip(this.button_Exit, "Exit the Application");
			this.button_Exit.Click += new System.EventHandler(this.buttonExit_Click);
			// 
			// mainMenu1
			// 
			this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.menuFile,
																					  this.menuEdit,
																					  this.menuView,
																					  this.menuHelp});
			// 
			// menuFile
			// 
			this.menuFile.Index = 0;
			this.menuFile.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					 this.menuNew,
																					 this.menuItem_Open,
																					 this.menuClose,
																					 this.menuItem2,
																					 this.menuSave,
																					 this.menuSaveAs,
																					 this.menuItem3,
																					 this.menuItem_Exit});
			this.menuFile.Text = "&File";
			this.menuFile.Popup += new System.EventHandler(this.menuFile_Popup);
			// 
			// menuNew
			// 
			this.menuNew.Index = 0;
			this.menuNew.Shortcut = System.Windows.Forms.Shortcut.CtrlN;
			this.menuNew.Text = "&New collection";
			this.menuNew.Click += new System.EventHandler(this.menuNew_Click);
			// 
			// menuItem_Open
			// 
			this.menuItem_Open.Index = 1;
			this.menuItem_Open.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																						  this.menuLoad,
																						  this.menuOpenFromWeb});
			this.menuItem_Open.Shortcut = System.Windows.Forms.Shortcut.CtrlO;
			this.menuItem_Open.Text = "&Open";
			// 
			// menuLoad
			// 
			this.menuLoad.Index = 0;
			this.menuLoad.Text = "Collection ...";
			this.menuLoad.Click += new System.EventHandler(this.menuItem_Load_Click);
			// 
			// menuOpenFromWeb
			// 
			this.menuOpenFromWeb.Index = 1;
			this.menuOpenFromWeb.Text = "Collection from Web...";
			this.menuOpenFromWeb.Click += new System.EventHandler(this.menuOpenFromWeb_Click);
			// 
			// menuClose
			// 
			this.menuClose.Index = 2;
			this.menuClose.Text = "&Close collection";
			this.menuClose.Click += new System.EventHandler(this.menuClose_Click);
			// 
			// menuItem2
			// 
			this.menuItem2.Index = 3;
			this.menuItem2.Text = "-";
			// 
			// menuSave
			// 
			this.menuSave.Index = 4;
			this.menuSave.Shortcut = System.Windows.Forms.Shortcut.CtrlS;
			this.menuSave.Text = "&Save";
			this.menuSave.Click += new System.EventHandler(this.menuSave_Click);
			// 
			// menuSaveAs
			// 
			this.menuSaveAs.Index = 5;
			this.menuSaveAs.Text = "Save &As...";
			this.menuSaveAs.Click += new System.EventHandler(this.menuSaveAs_Click);
			// 
			// menuItem3
			// 
			this.menuItem3.Index = 6;
			this.menuItem3.Text = "-";
			// 
			// menuItem_Exit
			// 
			this.menuItem_Exit.Index = 7;
			this.menuItem_Exit.Text = "E&xit";
			this.menuItem_Exit.Click += new System.EventHandler(this.menuItem_Exit_Click);
			// 
			// menuEdit
			// 
			this.menuEdit.Index = 1;
			this.menuEdit.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					 this.menuCopy,
																					 this.menuNewQuote,
																					 this.menuRemove,
																					 this.menuItem5,
																					 this.menuOptions});
			this.menuEdit.Text = "&Edit";
			this.menuEdit.Popup += new System.EventHandler(this.menuEdit_Popup);
			// 
			// menuCopy
			// 
			this.menuCopy.Index = 0;
			this.menuCopy.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					 this.menuCopyToClipbrd,
																					 this.menuCopyToClipboardAsText,
																					 this.menuItem1,
																					 this.menuCopyToOutlookDir,
																					 this.menuCopyToOutlookDirAsText,
																					 this.menuItem4,
																					 this.menuCopySelToClipbrd,
																					 this.menuItem6,
																					 this.menuCopyXMLToDB,
																					 this.menuCopyXMLToAutoSig});
			this.menuCopy.Text = "&Copy";
			// 
			// menuCopyToClipbrd
			// 
			this.menuCopyToClipbrd.Index = 0;
			this.menuCopyToClipbrd.Text = "Quote to Clipboard (HTML)";
			this.menuCopyToClipbrd.Click += new System.EventHandler(this.menuCopyOptions_Click);
			// 
			// menuCopyToClipboardAsText
			// 
			this.menuCopyToClipboardAsText.Index = 1;
			this.menuCopyToClipboardAsText.Text = "Quote to Clipboard (Text)";
			this.menuCopyToClipboardAsText.Click += new System.EventHandler(this.menuCopyOptions_Click);
			// 
			// menuItem1
			// 
			this.menuItem1.Index = 2;
			this.menuItem1.Text = "-";
			// 
			// menuCopyToOutlookDir
			// 
			this.menuCopyToOutlookDir.Index = 3;
			this.menuCopyToOutlookDir.Text = "Quote as File to Outlook (HTML)";
			this.menuCopyToOutlookDir.Click += new System.EventHandler(this.menuCopyOptions_Click);
			// 
			// menuCopyToOutlookDirAsText
			// 
			this.menuCopyToOutlookDirAsText.Index = 4;
			this.menuCopyToOutlookDirAsText.Text = "Quote as File to Outlook (Text)";
			this.menuCopyToOutlookDirAsText.Click += new System.EventHandler(this.menuCopyOptions_Click);
			// 
			// menuItem4
			// 
			this.menuItem4.Index = 5;
			this.menuItem4.Text = "-";
			// 
			// menuCopySelToClipbrd
			// 
			this.menuCopySelToClipbrd.Index = 6;
			this.menuCopySelToClipbrd.Text = "Quote Selection to Clipboard";
			this.menuCopySelToClipbrd.Click += new System.EventHandler(this.menuCopyOptions_Click);
			// 
			// menuItem6
			// 
			this.menuItem6.Index = 7;
			this.menuItem6.Text = "-";
			// 
			// menuCopyXMLToDB
			// 
			this.menuCopyXMLToDB.Index = 8;
			this.menuCopyXMLToDB.Text = "XML file to DesktopBob location";
			this.menuCopyXMLToDB.Click += new System.EventHandler(this.menuCopyOptions_Click);
			// 
			// menuCopyXMLToAutoSig
			// 
			this.menuCopyXMLToAutoSig.Index = 9;
			this.menuCopyXMLToAutoSig.Text = "XML file to AutoSig location";
			this.menuCopyXMLToAutoSig.Click += new System.EventHandler(this.menuCopyOptions_Click);
			// 
			// menuNewQuote
			// 
			this.menuNewQuote.Index = 1;
			this.menuNewQuote.Shortcut = System.Windows.Forms.Shortcut.CtrlQ;
			this.menuNewQuote.Text = "Add new &Quote...";
			this.menuNewQuote.Click += new System.EventHandler(this.menuNewQuote_Click);
			// 
			// menuRemove
			// 
			this.menuRemove.Index = 2;
			this.menuRemove.Text = "&Remove Quote";
			this.menuRemove.Click += new System.EventHandler(this.menuRemove_Click);
			// 
			// menuItem5
			// 
			this.menuItem5.Index = 3;
			this.menuItem5.Text = "-";
			// 
			// menuOptions
			// 
			this.menuOptions.Index = 4;
			this.menuOptions.Text = "&Options...";
			this.menuOptions.Click += new System.EventHandler(this.menuOptions_Click);
			// 
			// menuView
			// 
			this.menuView.Index = 2;
			this.menuView.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					 this.menuHTMLControlOption});
			this.menuView.Text = "&View";
			this.menuView.Popup += new System.EventHandler(this.menuView_Popup);
			// 
			// menuHTMLControlOption
			// 
			this.menuHTMLControlOption.Enabled = false;
			this.menuHTMLControlOption.Index = 0;
			this.menuHTMLControlOption.RadioCheck = true;
			this.menuHTMLControlOption.Text = "HTML Control";
			this.menuHTMLControlOption.Click += new System.EventHandler(this.menuHTMLControlOption_Click);
			// 
			// menuHelp
			// 
			this.menuHelp.Index = 3;
			this.menuHelp.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					 this.menuItem10});
			this.menuHelp.Text = "&Help";
			// 
			// menuItem10
			// 
			this.menuItem10.Index = 0;
			this.menuItem10.Text = "About...";
			this.menuItem10.Click += new System.EventHandler(this.DoAbout);
			// 
			// htmlEditor1
			// 
			this.htmlEditor1.DocumentEncoding = onlyconnect.EncodingType.WindowsCurrent;
			this.htmlEditor1.Font = new System.Drawing.Font("Arial", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.htmlEditor1.IsActivationEnabled = false;
			this.htmlEditor1.IsContextMenuEnabled = false;
			this.htmlEditor1.IsDesignMode = true;
			this.htmlEditor1.Location = new System.Drawing.Point(8, 8);
			this.htmlEditor1.Name = "htmlEditor1";
			this.htmlEditor1.Size = new System.Drawing.Size(472, 216);
			this.htmlEditor1.TabIndex = 16;
			this.htmlEditor1.Text = "htmlEditor1";
			this.toolTip1.SetToolTip(this.htmlEditor1, "Displays the quotes in Rich Edit style");
			// 
			// button_Random
			// 
			this.button_Random.Enabled = false;
			this.button_Random.Location = new System.Drawing.Point(200, 312);
			this.button_Random.Name = "button_Random";
			this.button_Random.TabIndex = 13;
			this.button_Random.Text = "Random";
			this.toolTip1.SetToolTip(this.button_Random, "Random");
			this.button_Random.Click += new System.EventHandler(this.buttonRandom_Click);
			// 
			// toolBar1
			// 
			this.toolBar1.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] {
																						this.toolBarButton1,
																						this.toolBarFontDialog,
																						this.toolBarBold,
																						this.toolBarItalic,
																						this.toolBarUnderline,
																						this.toolBarURL,
																						this.toolBarAlignLeft,
																						this.toolBarAlignCenter,
																						this.toolBarAlignRight,
																						this.toolBarBullets});
			this.toolBar1.DropDownArrows = true;
			this.toolBar1.Name = "toolBar1";
			this.toolBar1.ShowToolTips = true;
			this.toolBar1.Size = new System.Drawing.Size(520, 25);
			this.toolBar1.TabIndex = 9;
			this.toolBar1.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.toolBar1_ButtonClick);
			// 
			// toolBarButton1
			// 
			this.toolBarButton1.ToolTipText = "Not implemented";
			// 
			// toolBarFontDialog
			// 
			this.toolBarFontDialog.ToolTipText = "Font Dialog";
			// 
			// toolBarBold
			// 
			this.toolBarBold.ToolTipText = "Bold";
			// 
			// toolBarItalic
			// 
			this.toolBarItalic.ToolTipText = "Italic";
			// 
			// toolBarUnderline
			// 
			this.toolBarUnderline.ToolTipText = "Underline";
			// 
			// toolBarURL
			// 
			this.toolBarURL.ToolTipText = "Hyperlink";
			// 
			// toolBarAlignLeft
			// 
			this.toolBarAlignLeft.ToolTipText = "Align Left";
			// 
			// toolBarAlignCenter
			// 
			this.toolBarAlignCenter.ToolTipText = "Align Center";
			// 
			// toolBarAlignRight
			// 
			this.toolBarAlignRight.ToolTipText = "Align Right";
			// 
			// toolBarBullets
			// 
			this.toolBarBullets.ToolTipText = "Bullets";
			// 
			// tabControl1
			// 
			this.tabControl1.Controls.AddRange(new System.Windows.Forms.Control[] {
																					  this.tabQuotes,
																					  this.tabHTMLSource});
			this.tabControl1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.tabControl1.Location = new System.Drawing.Point(8, 40);
			this.tabControl1.Name = "tabControl1";
			this.tabControl1.SelectedIndex = 0;
			this.tabControl1.Size = new System.Drawing.Size(496, 256);
			this.tabControl1.TabIndex = 11;
			this.tabControl1.SelectedIndexChanged += new System.EventHandler(this.tabControl1_SelectedIndexChanged);
			// 
			// tabQuotes
			// 
			this.tabQuotes.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.htmlEditor1});
			this.tabQuotes.Location = new System.Drawing.Point(4, 22);
			this.tabQuotes.Name = "tabQuotes";
			this.tabQuotes.Size = new System.Drawing.Size(488, 230);
			this.tabQuotes.TabIndex = 0;
			this.tabQuotes.Text = "Quotes";
			// 
			// tabHTMLSource
			// 
			this.tabHTMLSource.Controls.AddRange(new System.Windows.Forms.Control[] {
																						this.textBox_Quotes});
			this.tabHTMLSource.Location = new System.Drawing.Point(4, 22);
			this.tabHTMLSource.Name = "tabHTMLSource";
			this.tabHTMLSource.Size = new System.Drawing.Size(488, 230);
			this.tabHTMLSource.TabIndex = 1;
			this.tabHTMLSource.Text = "HTML";
			// 
			// textBox_Quotes
			// 
			this.textBox_Quotes.Cursor = System.Windows.Forms.Cursors.Default;
			this.textBox_Quotes.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.textBox_Quotes.Location = new System.Drawing.Point(8, 8);
			this.textBox_Quotes.Multiline = true;
			this.textBox_Quotes.Name = "textBox_Quotes";
			this.textBox_Quotes.ReadOnly = true;
			this.textBox_Quotes.ScrollBars = System.Windows.Forms.ScrollBars.Both;
			this.textBox_Quotes.Size = new System.Drawing.Size(472, 216);
			this.textBox_Quotes.TabIndex = 15;
			this.textBox_Quotes.Text = "";
			this.textBox_Quotes.WordWrap = false;
			// 
			// statusBar1
			// 
			this.statusBar1.Location = new System.Drawing.Point(0, 345);
			this.statusBar1.Name = "statusBar1";
			this.statusBar1.Panels.AddRange(new System.Windows.Forms.StatusBarPanel[] {
																						  this.statusBarPanelFileName,
																						  this.statusBarPanelQuotes});
			this.statusBar1.ShowPanels = true;
			this.statusBar1.Size = new System.Drawing.Size(520, 22);
			this.statusBar1.TabIndex = 12;
			this.statusBar1.Text = "statusBar1";
			// 
			// statusBarPanelFileName
			// 
			this.statusBarPanelFileName.AutoSize = System.Windows.Forms.StatusBarPanelAutoSize.Spring;
			this.statusBarPanelFileName.Text = "statusBarPanelFileName";
			this.statusBarPanelFileName.Width = 376;
			// 
			// statusBarPanelQuotes
			// 
			this.statusBarPanelQuotes.AutoSize = System.Windows.Forms.StatusBarPanelAutoSize.Contents;
			this.statusBarPanelQuotes.Text = "statusBarPanelQuotes";
			this.statusBarPanelQuotes.Width = 128;
			// 
			// saveFileDialog1
			// 
			this.saveFileDialog1.FileName = "doc1";
			// 
			// notifyIcon1
			// 
			this.notifyIcon1.Text = "notifyIcon1";
			this.notifyIcon1.Visible = true;
			this.notifyIcon1.DoubleClick += new System.EventHandler(this.notifyIcon1_DoubleClick);
			// 
			// CPQMainForm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(520, 367);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.button_Random,
																		  this.statusBar1,
																		  this.tabControl1,
																		  this.toolBar1,
																		  this.button_Exit,
																		  this.button_LastQuote,
																		  this.button_NextQuote,
																		  this.button_PrevQuote,
																		  this.button_FirstQuote});
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D;
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.MaximizeBox = false;
			this.Menu = this.mainMenu1;
			this.Name = "CPQMainForm";
			this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide;
			this.Text = "Code Project Quoter";
			this.tabControl1.ResumeLayout(false);
			this.tabQuotes.ResumeLayout(false);
			this.tabHTMLSource.ResumeLayout(false);
			((System.ComponentModel.ISupportInitialize)(this.statusBarPanelFileName)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.statusBarPanelQuotes)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{

			#region Single Instance code - Implementation

			// get the name of our process
			string proc=Process.GetCurrentProcess().ProcessName;
			
			// get the list of all processes by that name
			Process[] processes=Process.GetProcessesByName(proc);
			
			// if there is more than one process...
			if (processes.Length > 1)
			{
				// Assume there is our process, which we will terminate, 
				// and the other process, which we want to bring to the 
				// foreground. This assumes there are only two processes 
				// in the processes array, and we need to find out which 
				// one is NOT us.

				// get our process
				Process p=Process.GetCurrentProcess();
				
				int n=0;        // assume the other process is at index 0
				
				// if this process id is OUR process ID...
				if (processes[0].Id==p.Id)
				{
					// then the other process is at index 1
					n=1;
				}
				// get the window handle
				IntPtr hWnd=processes[n].MainWindowHandle;
				
				// if iconic, we need to restore the window
				if (IsIconic(hWnd))
				{
					ShowWindowAsync(hWnd, SW_RESTORE);
				}
				// bring it to the foreground
				SetForegroundWindow(hWnd);

				// exit our process
				return;
			}

			#endregion

			CPQMainForm CPQ = new CPQMainForm();

			// If the pre-requisites didn't met, then close the app
			if (false == CPQ.PreRequisitesCheck())
				return;

			Application.Run(CPQ);
		}

		#region Check for Pre-requisites code

		private bool PreRequisitesCheck()
		{
			// First make sure Dot Net Framework is installed
			if(DotNetServicePacks.NoFrameworkFound == VersionCheck())
			{
				MessageBox.Show("This application requires the Microsoft� .NET Framework.", 
					"DotNet Framework Error!",
					MessageBoxButtons.OK,
					MessageBoxIcon.Error);
				return false;
			}

			// If the above condition passed, then check for VS.NET installed or not
			if(false == LookupVisualStudioDotNet())
			{
				string strMsg = "This application requires Microsoft.Mshtml.dll Primary Interop Assembly." 
					+ "\n\r" +  "Visual Studio.Net installs this in Program Files\\Microsoft.Net\\Primary Interop Assemblies"
					+ "\n\r" + "This application can NOT proceed further. It will be closed.";

				MessageBox.Show(strMsg, "Visual Studio.NET not exists", 	
					MessageBoxButtons.OK,
					MessageBoxIcon.Error);

				return false;
			}
			return true;
		}

		#endregion


		#region Tray Icon code

		private void SetupTrayIcon()
		{

			Assembly asm = Assembly.GetExecutingAssembly();
			using (Stream st = asm.GetManifestResourceStream(asm.GetName().Name + "." + "Resources" + "." + "CPQ.ico"))
			{
				mTrayIcon = new Icon(st);
			}
			
			// Setup the default icon		
			notifyIcon1.Icon = mTrayIcon;
			notifyIcon1.Text = "CP Quoter";
			notifyIcon1.Visible = true;

			//Insert all MenuItem objects into an array and add them to
			//the context menu simultaneously

			MenuItem[] mnuItms = new MenuItem[9];
			mnuItms[0] = new MenuItem();
			mnuItms[0].Text = "Restore CP Quoter";
			mnuItms[0].Click += new System.EventHandler(this.RestoreForm);
			mnuItms[0].DefaultItem = true;

			mnuItms[1] = new MenuItem();
			mnuItms[1].Text = "About CP Quoter...";
			mnuItms[1].Click += new System.EventHandler(this.DoAbout);

			mnuItms[2] = new MenuItem("-");

			mnuItms[3] = new MenuItem();
			mnuItms[3].Text = "Open collection...";
			mnuItms[3].Click += new System.EventHandler(this.menuItem_Load_Click);

			mnuItms[4] = new MenuItem("-");

			mnuItms[5] = new MenuItem();
			mnuItms[5].Text = "Add new quote...";
			mnuItms[5].Click += new System.EventHandler(this.menuNewQuote_Click);

			mnuItms[6] = new MenuItem();
			mnuItms[6].Text = "Random quote";
			mnuItms[6].Click += new System.EventHandler(this.RetrieveRandomQuote);

			mnuItms[7] = new MenuItem("-");

			mnuItms[8] = new MenuItem();
			mnuItms[8].Text = "Exit";
			mnuItms[8].Click += new System.EventHandler(this.ExitApp);

			contextMenu1 = new ContextMenu(mnuItms);
			notifyIcon1.ContextMenu = contextMenu1;		
		}

		
		public void RestoreForm(object sender, System.EventArgs e)
		{
			this.Show();
			this.WindowState = FormWindowState.Normal;
		}

		
		public void RetrieveRandomQuote(object sender, System.EventArgs e)
		{
			if(m_quotesCollection != null)
			{
				if(m_quotesCollection.Count > 0)
				{
					string strTempQuote = m_quotesCollection.ChooseRandomQuote();
					Clipboard.SetDataObject(strTempQuote,true);
					MessageBox.Show("Random quote (text) copied to the clipboard!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information );

				}
				else
				{
					// No quotes exist in the collection.
					MessageBox.Show("No quotes exist in this collection.", 
						"Error!",
						MessageBoxButtons.OK,
						MessageBoxIcon.Exclamation);
				}
			}
		}


		public void ExitApp(object sender, System.EventArgs e)
		{
			notifyIcon1.Visible = false;
			notifyIcon1.Dispose();
			this.Close();
		}

		
		private void notifyIcon1_DoubleClick(object sender, System.EventArgs e)
		{
			this.Show();
			this.WindowState = FormWindowState.Normal;
		}
		
		
		#endregion

		#region Event handlers

		private void buttonExit_Click(object sender, System.EventArgs e)
		{
			// Exit the app
			this.Close();
		
		}

		private void tabControl1_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			this.textBox_Quotes.Text = this.htmlEditor1.GetDocumentSource();
		}

		
		#endregion

		#region Properties

		public string Version
		{
			get 
			{ 
				return System.Environment.Version.ToString(); 
			}
		}


		#endregion

		#region Overrides
		

		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);
			
			LoadMyImages();
		}


		private bool LookupVisualStudioDotNet()
		{
			string strRegKey = "Software\\Microsoft\\VisualStudio\\7.0";

			Microsoft.Win32.RegistryKey oRegKey = Microsoft.Win32.Registry.LocalMachine;
			Microsoft.Win32.RegistryKey oVCDotNetKey = oRegKey.OpenSubKey(strRegKey);

			if(oVCDotNetKey == null)
			{
				oRegKey.Close();
				oVCDotNetKey.Close();
				return false;
			}
			else
			{
				oRegKey.Close();
				oVCDotNetKey.Close();
				return true; 
			}
		}

	
		protected override void OnResize(EventArgs e)
		{
			// If minimized then don't show in the taskbar
			if (this.WindowState == FormWindowState.Minimized)
				this.Hide();

			base.OnResize(e);
		}

	
		protected override void OnPaint(PaintEventArgs peArgs)
		{
			if (m_quotesCollection.Count > 0)
			{
				// Display the filename only when the FileName exist
				if(m_quotesCollection.FileName != null)
				{

					if (m_selectedOpenOption == OpenOptions.LocalFile)
					{
						FileInfo fileInfo = new FileInfo(m_quotesCollection.FileName);
				
						statusBarPanelFileName.Text = fileInfo.Name;

					}
					else // FromWeb
					{
						statusBarPanelFileName.Text = m_quotesCollection.FileName;
					}
				}
				else
				{
					// If it's a new collection and new quote added to it
					statusBarPanelFileName.Text = "";

				}

				statusBarPanelQuotes.Text = String.Format("{0}/{1}",	
											m_quotesCollection.RetrievePos+1,
											m_quotesCollection.Count);

				statusBar1.ShowPanels = true;
			}
			else
			{
				peArgs.Graphics.Clear(SystemColors.Control);

				statusBar1.Text = "No Quotes collection loaded.";
				statusBar1.ShowPanels = false;
			}

			base.OnPaint(peArgs);
			statusBar1.Invalidate();

		}

		
		protected override void OnClosing(CancelEventArgs ceArgs)
		{
			if (this.CloseCollection() == false)
				ceArgs.Cancel = true;
			else
				ceArgs.Cancel = false;

			htmlEditor1.Dispose();
			htmlEditor1.CleanupControl();

			base.OnClosing(ceArgs);
		}


		
		#endregion

		#region Toolbar code

		private void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
		{
			Debug.WriteLine("toolBar1_ButtonClick()");

			//TODO: Disable the Toolbar button when the focus is textbox_Quotes
			switch (toolBar1.Buttons.IndexOf(e.Button)) 
			{
				case 0:
					//TODO: Get another icon for this
					MessageBox.Show("Not implemented");
					break;
					
				case 1: // Font Dialog
					string fontname, fontsize, fontstyle, fontcolor = null;
					fontDialog1.ShowColor = true;

					if (fontDialog1.ShowDialog() != DialogResult.Cancel)
					{
						fontname = fontDialog1.Font.Name;
						//BUG: I am getting wrong size.
						fontsize = fontDialog1.Font.SizeInPoints.ToString(); 
						fontstyle = fontDialog1.Font.Style.ToString();
						fontcolor = fontDialog1.Color.Name;
						
						this.htmlEditor1.Document.execCommand("FontName",false, fontname);
						this.htmlEditor1.Document.execCommand("FontSize",false, fontsize);
						this.htmlEditor1.Document.execCommand("ForeColor",false, fontcolor);
						
						if(fontstyle == "Bold")
							this.htmlEditor1.Document.execCommand("bold",false,null);
						if(fontstyle == "Italic")
							this.htmlEditor1.Document.execCommand("italic",false,null);
					}
					break; 

				case 2: 
					if (this.htmlEditor1.IsDesignMode)
					{
						this.htmlEditor1.Document.execCommand("Bold",false,null);
					}
					else
					{
						MessageBox.Show("HTML Design mode is turned off. Enable it via View menu.", 
							"HTML Control Error!",
							MessageBoxButtons.OK,
							MessageBoxIcon.Information);

					}
					break;
				case 3: 
					this.htmlEditor1.Document.execCommand("Italic",false,null);
					break;
				case 4: 
					this.htmlEditor1.Document.execCommand("Underline",false,null);
					break;
				case 5: //CHANGE THIS ICON
					this.htmlEditor1.Document.execCommand("CreateLink",false,null);
					break; 
				case 6: 
					this.htmlEditor1.Document.execCommand("JustifyLeft",false,null);
					break;
				case 7: 
					this.htmlEditor1.Document.execCommand("JustifyCenter",false,null);
					break;
				case 8: 
					this.htmlEditor1.Document.execCommand("JustifyRight",false,null);
					break;
				case 9: 
					this.htmlEditor1.Document.execCommand("InsertOrderedList",false,null);
					break;
				default:
					break;
			}

		}


		public void LoadMyImages()
		{
			int x;

			toolbarImages = new ImageList();

			toolbarImages.ImageSize = new Size(BITMAP_SIZE, 15);

			// Retrieve an embedded bitmap from the current assembly.
			Assembly Asm = Assembly.GetExecutingAssembly();
			Stream BmpStrm = Asm.GetManifestResourceStream("CodeProjectQuoter.Resources.ToolbarImages.bmp");
			Bitmap TempBmp = new Bitmap(BmpStrm);

			TempBmp.MakeTransparent(transparentColor);
			
			toolbarImages.Images.AddStrip(TempBmp); 

			toolBar1.ImageList = toolbarImages;

			for(x = 0; x < toolbarImages.Images.Count; x++)
			{
				toolBar1.Buttons[x].ImageIndex = x;
			}
		}


		#endregion
		
		#region Navigation controls
		
		private void button_NextQuote_Click(object sender, System.EventArgs e)
		{
			// Debug.WriteLine("button_NextQuote_Click()");

			if (m_quotesCollection.NextQuote())
			{
				string strQuote  = m_quotesCollection.RetrieveCurrentQuote.ToString();
				this.htmlEditor1.LoadDocument(strQuote);
				EnableControls();
				this.Invalidate();
			}
		}

	
		private void button_PrevQuote_Click(object sender, System.EventArgs e)
		{
			//Debug.WriteLine("button_PrevQuote_Click()");

			if (m_quotesCollection.PrevQuote())
			{
				string strQuote  = m_quotesCollection.RetrieveCurrentQuote.ToString();
				this.htmlEditor1.LoadDocument(strQuote);
				EnableControls();
				this.Invalidate();
			}
		}

		
		private void buttonRandom_Click(object sender, System.EventArgs e)
		{
			//Debug.WriteLine("buttonRandom_Click()");

			string strQuote = m_quotesCollection.ChooseRandomQuote();
			this.htmlEditor1.LoadDocument(strQuote);
			EnableControls();
			this.Invalidate();
			statusBar1.Invalidate(); 
		}

		
		private void button_LastQuote_Click(object sender, System.EventArgs e)
		{
			//Debug.WriteLine("button_LastQuote_Click()");

			m_quotesCollection.RetrievePos = m_quotesCollection.Count;
			string strQuote  = m_quotesCollection.RetrieveCurrentQuote.ToString();
			this.htmlEditor1.LoadDocument(strQuote);
			EnableControls();
			this.Invalidate();
		}

		
		private void button_FirstQuote_Click(object sender, System.EventArgs e)
		{
			//Debug.WriteLine("button_FirstQuote_Click()");

			//BUG: Add couple of quotes, then close the collection without saving it
			// The current quote still gets displayed in the HTML Editor
			// And also the Quote count shows..and also First, Prev buttons are enabled.
			// If I press the First quote...CRASH....
			m_quotesCollection.RetrievePos = 0;
			string strQuote  = m_quotesCollection.RetrieveCurrentQuote.ToString();
			EnableControls();
			this.htmlEditor1.LoadDocument(strQuote);
			this.Invalidate();

		}

		
		private void EnableControls()
		{

			// Debug.WriteLine("EnableControls()");

			//BUG: Disbale the controls when the focus is in the textBox_Quotes

			bool bUp = !(m_quotesCollection.IsLast());
			bool bDown = !(m_quotesCollection.IsFirst());

			// Argh!!...Is there any better way..
			if(bDown)
			{
				button_FirstQuote.Enabled = true;
				button_PrevQuote.Enabled = true;
			}
			else
			{
				button_FirstQuote.Enabled = false;
				button_PrevQuote.Enabled = false;
			}

			if(bUp)
			{
				button_NextQuote.Enabled = true;
				button_LastQuote.Enabled = true;
			}
			else
			{
				button_NextQuote.Enabled = false;
				button_LastQuote.Enabled = false;
			}

			if(m_quotesCollection.Count > 0)
				button_Random.Enabled = true;
			else
				button_Random.Enabled = false;
		}


		#endregion

		#region Menu Items code

		private void menuItem_Exit_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

	
		private void DoAbout(object sender, EventArgs e)
		{
			//Create a new instance of the MDI child template form
			AboutForm AboutDlg = new AboutForm(); 

			//Display the child window
			AboutDlg.Show();			
		}

		
		private void menuSave_Click(object sender, System.EventArgs e)
		{
			// Debug.WriteLine("menuSave_Click");

			string strFileName = m_quotesCollection.FileName;

			// If it's new collection and filename is not provided 
			// then provide the option of saving it first
			if ((strFileName == null) && (string.Empty != strFileName))
			{
				menuSaveAs_Click(sender, e);
			}
			else
			{
				//TODO: What if WriteToXML() fails, close the app gracefully
				m_quotesCollection.WriteToXML(strFileName);
				m_bCollectionChanged = false;
			}

			// This required b'cos if we saved the collection as new name, that should reflect on the statusbar
			statusBar1.Invalidate();
			this.Validate();
		}


		private void menuSaveAs_Click(object sender, System.EventArgs e)
		{
			// Debug.WriteLine("menuSaveAs_Click");

			saveFileDialog1.Title = "Save Quotes Collection";
			saveFileDialog1.DefaultExt = "xml";
			saveFileDialog1.Filter = "Quote files (*.xml)|*.xml|" + "All files|*.*";
			saveFileDialog1.InitialDirectory = Application.StartupPath;
			saveFileDialog1.RestoreDirectory = true;

			if (saveFileDialog1.ShowDialog() == DialogResult.OK)
			{
				// store the selected Quotes FileName in the collection.
				m_quotesCollection.FileName = saveFileDialog1.FileName;
				
				menuSave_Click(sender, e);
			}
		}

		
		private void menuNewQuote_Click(object sender, System.EventArgs e)
		{
			// Debug.WriteLine("menuNewQuote_Click");

			NewQuote oNewQuoteDlg = new NewQuote();

			DialogResult userResponse = oNewQuoteDlg.ShowDialog();
			
			if (userResponse == DialogResult.OK)
			{
				string strTempQuote = oNewQuoteDlg.Quote; 				
				int index = 0;

				// If the quote is not empty
				if(string.Empty != strTempQuote)
				{
					Quotes quote = new Quotes(strTempQuote);

					index = m_quotesCollection.Add(quote);

					//if(CPQDebugSwitch.Enabled)
					//if(m_quotesCollection.FileName != null)
					//	Debug.WriteLine("Added new quote: " + "'" + m_quotesCollection.FileName.ToString() + " ' " + "at index :" + index );
				
					// show the new quote in HTMLEditor control.
					string strQuote  = m_quotesCollection.RetrieveCurrentQuote.ToString();
					this.htmlEditor1.LoadDocument(strQuote);
					
					EnableControls();
					this.Invalidate();

					m_bCollectionChanged = true;

				}
				else
				{
					MessageBox.Show("You entered entered empty quote. It's not added to the collection.", 
										"New Quote Error!", MessageBoxButtons.OK,
										MessageBoxIcon.Information);
				}
			}
			else
			{
				return;
			}

			oNewQuoteDlg.Dispose();

		}
		
		
		private void menuRemove_Click(object sender, System.EventArgs e)
		{
			// Debug.WriteLine("menuRemove_Click");

			if(m_quotesCollection.Count > 0)
			{
				DialogResult userResponse = MessageBox.Show("Do you want to remove the current quote?", 
											"Remove Quote?",
											MessageBoxButtons.YesNo,
											MessageBoxIcon.Question);

				if (userResponse == DialogResult.Yes)
				{
					m_quotesCollection.Remove(m_quotesCollection.RetrieveCurrentQuote);
					
					//BUG: If you deleted the last entry then it fails
					//TODO: Check if the nextquote availble before I load next quote

					if (m_quotesCollection.NextQuote())
					{
						// Position is updated whenever quote is removed from the collection.
						string strQuote  = m_quotesCollection.RetrieveCurrentQuote.ToString();
						this.htmlEditor1.LoadDocument(strQuote);
						EnableControls();
						this.Invalidate();
					}
					
					m_bCollectionChanged = true; 
				}
			}
		}

	
		private void menuClose_Click(object sender, System.EventArgs e)
		{
			// Debug.WriteLine("menuClose_Click");
			CloseCollection();
		}

		
		private void menuEdit_Popup(object sender, System.EventArgs e)
		{
			// Debug.WriteLine("menuEdit_Popup");
			if (sender is MenuItem) 
			{
				bool bQuotesExist = ( m_quotesCollection.Count > 0 );

				if (m_selectedOpenOption == OpenOptions.LocalFile)
				{
					//bool bQuotesExist = ( m_quotesCollection.Count > 0 );
					
					// Disable the "Remove" and "Copy" menu items when there are no quotes exist.
					// Leave the "Add new quote" menu item as it is.
					menuRemove.Enabled = bQuotesExist;
					menuCopy.Enabled = bQuotesExist;

					// If the menuNew is disabled when opened the collection from web, make it enable again
					if(menuNewQuote.Enabled == false)
					{
						menuNewQuote.Enabled = true;
					}
				}
				else if (m_selectedOpenOption == OpenOptions.FromWeb)
				{
					// Disable the following when the collection opened from Web.
					menuNewQuote.Enabled = false;
					menuRemove.Enabled = false;
					menuCopy.Enabled = bQuotesExist;

				}
			}
		}


		private void menuFile_Popup(object sender, System.EventArgs e)
		{
		//	Debug.WriteLine("menuFile_Popup");

			if (sender is MenuItem) 
			{
				// Disable the Save options if the collection opened from web
				if(m_selectedOpenOption == OpenOptions.FromWeb)
				{
					menuSave.Enabled = false;
					menuSaveAs.Enabled = false;
				}
				else
				{
					// Enable the "Close collection" menu item only when the collection is changed
					if ((m_quotesCollection != null) && (m_bCollectionChanged))
					{
						menuClose.Enabled = true;
						// If the MenuSave and MenuSave disabled b'cos of any previous 
						// loading the quotes from web then enable those options
						if (menuSave.Enabled == false)
							menuSave.Enabled = true;
						if (menuSaveAs.Enabled == false)
							menuSaveAs.Enabled = true;

					}
					else
						menuClose.Enabled = false;
				}
			}		
		}

	
		private void menuCopyOptions_Click(object sender, System.EventArgs e)
		{
			// Debug.WriteLine("menuCopyOptions_Click");

			if (sender is MenuItem)
			{
				MenuItem menuItem = (MenuItem)sender;

				m_selectedCopyOption = (CopyOptions) menuItem.Index;

				// TODO: Create new two contextmenu, one for HTML Control and Textbox control.
				switch (m_selectedCopyOption)                                  
				{
					case CopyOptions.HTMLClipbrd:
						Clipboard.SetDataObject(textBox_Quotes.Text,true);
						break;

					case CopyOptions.TextClipbrd:
						// Copy the current quote from HTMLEditor
						string temp = m_quotesCollection.RetrieveCurrentQuote.ToString();

						Clipboard.SetDataObject(temp,true);
						break;

					case CopyOptions.OutlookDirHTML:
						WriteToOutlookSigDir(true);
						break;

					case CopyOptions.OutlookDirText:
						WriteToOutlookSigDir(false);
						break;

					case CopyOptions.SelectedTextToClipbrd:
						if(textBox_Quotes.ContainsFocus)
						{
							bool bSelected = (textBox_Quotes.SelectedText.Length > 0);
					
							if(bSelected)
								Clipboard.SetDataObject(textBox_Quotes.SelectedText,true);
							else
								MessageBox.Show("Nothing is selected to copy.", "Copy Selected Text?");
						}
						else
						{
							MessageBox.Show("Select the text from HTML Source under HTML tab.", "SelectedTextToClipbrd", 
								MessageBoxButtons.OK, MessageBoxIcon.Information);
						}
						break;

					case CopyOptions.FileToDesktopBobLoc:
						
						if (string.Empty != m_strDesktopBobLoc)
						{
							// For any new collection
							Debug.Assert(m_quotesCollection != null);

							if(m_quotesCollection != null)
							{
								// If the new collection is not saved yet
								if ((m_quotesCollection.FileName == null) || (string.Empty == m_quotesCollection.FileName))
								{
									DialogResult userResponse = MessageBox.Show(
										"Do you want to save the current Quotes collection?", 
										"Collection is not saved yet!!", 
										MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information);

									// Show the SaveDialog so that user can save the collection first
									if (userResponse == DialogResult.Yes)
									{
										menuSaveAs_Click(sender, e);
										this.statusBar1.Invalidate();
										//BUG: It's not showing the new filename in the status bar
									}
									else if ((userResponse == DialogResult.Cancel) || (userResponse == DialogResult.No))
									{
										return;
									}

									// Now check for the filename again
									if ((m_quotesCollection.FileName != null) || (string.Empty != m_quotesCollection.FileName))
									{
										CopyFileToDestination(m_quotesCollection.FileName, m_strDesktopBobLoc);
									}	
								}
								else
								{
									CopyFileToDestination(m_quotesCollection.FileName, m_strDesktopBobLoc);
								}
							}
						}
						break;

					case CopyOptions.FileToAutoSigLoc:

						if (string.Empty != m_strAutoSigLoc)
						{
							// For any new collection
							Debug.Assert(m_quotesCollection != null);

							if(m_quotesCollection != null)
							{
								// If the new collection is not saved yet
								if ((m_quotesCollection.FileName == null) || (string.Empty == m_quotesCollection.FileName))
								{
									DialogResult userResponse = MessageBox.Show(
										"Do you want to save the current Quotes collection?", 
										"Collection is not saved yet!!", 
										MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information);

									// Show the SaveDialog so that user can save the collection first
									if (userResponse == DialogResult.Yes)
									{
										menuSaveAs_Click(sender, e);
										this.statusBar1.Invalidate();
										//BUG: It's not showing the new filename in the status bar
									}
									else if ((userResponse == DialogResult.Cancel) || (userResponse == DialogResult.No))
									{
										return;
									}

									// Now check for the filename again
									if ((m_quotesCollection.FileName != null) || (string.Empty != m_quotesCollection.FileName))
									{
										CopyFileToDestination(m_quotesCollection.FileName, m_strAutoSigLoc);
									}	
								}
								else
								{
									CopyFileToDestination(m_quotesCollection.FileName, m_strAutoSigLoc);

								}
							}
						}
						break;

				}
			}
		}

	
		private void menuNew_Click(object sender, System.EventArgs e)
		{
			// Debug.WriteLine("menuNew_Click");

			m_quotesCollection = new QuotesCollection();

			// Clear any contents in the HTMLEditor control
			this.htmlEditor1.LoadDocument(""); 
			this.htmlEditor1.Refresh();

			// Forgot this in the earlier build..
			EnableControls();

			this.Invalidate();
		}

		
		private void menuItem_Load_Click(object sender, System.EventArgs e)
		{
			// Debug.WriteLine("menuItem_Load_Click");

			string strQuote = null;

			openFileDialog.Title = "Open Quotes";
			openFileDialog.Filter = "XML Files (*.xml) |*.xml|" + "All files (*.*)|*.*";
			openFileDialog.InitialDirectory = Application.StartupPath;
			openFileDialog.RestoreDirectory = true;

			DialogResult userResponse = openFileDialog.ShowDialog();
			
			if (userResponse == DialogResult.OK)
			{
				try
				{
					m_selectedOpenOption = OpenOptions.LocalFile;

					// First close the current collection and create new m_quotesCollection
					CloseCollection();

				//	m_quotesCollection.ReadAndParseXML(openFileDialog.FileName);

					FileInfo fileInfo = new FileInfo(openFileDialog.FileName);

					Stream strm = fileInfo.OpenRead();

					m_quotesCollection.ReadAndParseXML(strm, false);

						


					//Now store the new Quotes filename in the collection
					m_quotesCollection.FileName = openFileDialog.FileName;
				}
				catch (Exception ex)
				{

					statusBarPanelFileName.Text = "Unable to load " + openFileDialog.FileName;
					MessageBox.Show("Unable to load file: " + ex.Message);
				}

				if(m_quotesCollection.Count > 0)
				{
					strQuote = m_quotesCollection.RetrieveCurrentQuote.ToString();

					if(strQuote != null)
					{
						this.htmlEditor1.LoadDocument(strQuote);
						EnableControls();
						m_bCollectionChanged = false;
						this.Invalidate();

					}
					else
					{
						// No quotes exist in the collection.
						MessageBox.Show("No quotes exist in this collection.", 
							"Load Quotes Collection Error!",
							MessageBoxButtons.OK,
							MessageBoxIcon.Information);

						return;
					}
				}
			}

		}

		
		private void menuView_Popup(object sender, System.EventArgs e)
		{
			if (sender is MenuItem) 
			{
				// TODO:
			}
		}

		
		private void menuHTMLControlOption_Click(object sender, System.EventArgs e)
		{
			//TODO
		}

		
		private void menuOpenFromWeb_Click(object sender, System.EventArgs e)
		{
			// local variables
			string strTempURL = null;
			WebRequest webRequest = null;
			WebResponse webResponse = null;
			string strQuote = null;

			OpenFromWebForm oFromWebFormDlg = new OpenFromWebForm();

			DialogResult userResponse = oFromWebFormDlg.ShowDialog();
			
			if (userResponse == DialogResult.OK)
			{
				strTempURL = oFromWebFormDlg.URLSource; 				
				m_selectedOpenOption = OpenOptions.FromWeb;
				webRequest = System.Net.WebRequest.Create(new Uri(strTempURL));
				webResponse = webRequest.GetResponse();
				Stream st = webResponse.GetResponseStream();
			
				CloseCollection();

				m_quotesCollection.ReadAndParseXML(st, true);

				//Now store the new Quotes filename in the collection
				m_quotesCollection.FileName = strTempURL;
			
				if(m_quotesCollection.Count > 0)
				{
					strQuote = m_quotesCollection.RetrieveCurrentQuote.ToString();

					if(strQuote != null)
					{
						this.htmlEditor1.LoadDocument(strQuote);
						EnableControls();
						m_bCollectionChanged = false;
						this.Invalidate();
					}
					else
					{
						// No quotes exist in the collection.
						MessageBox.Show("No quotes exist in this collection.", 
							"Load Quotes Collection Error!",
							MessageBoxButtons.OK,
							MessageBoxIcon.Information);

						return;
					}
				}
			}
			else
			{
				return;
			}

			oFromWebFormDlg.Dispose();
		}

		
		protected bool CloseCollection()
		{
			// Go ahead only when the collection changed 
			if (m_bCollectionChanged)
			{
				string strMsg;

				if (m_quotesCollection.FileName == null)
				{
					strMsg = "Do you want to save the current Quotes collection?";
				}
				else
				{
					strMsg = String.Format("Do you want to save your changes to \n{0}?",
											m_quotesCollection.FileName);
				}

				DialogResult userResponse = MessageBox.Show(this, strMsg, "Save?",
					MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);

				if (userResponse == DialogResult.Yes)
				{
					menuSave_Click(this, EventArgs.Empty);
				}
				else if (userResponse == DialogResult.Cancel)
				{
					return false;
				}
			}

		
			if (m_quotesCollection != null)
			{
				m_quotesCollection.Clear();
				this.htmlEditor1.LoadDocument(""); 
				this.htmlEditor1.Refresh();
				EnableControls();
				this.Invalidate();
			}

			//TODO: Don't do the following if we are exiting the app
			// As this function gets called on Closing()

			// Create the new collection, 
			// so that the user can add new quotes this collection
			m_quotesCollection = new QuotesCollection();

			m_bCollectionChanged  = false;

			return true;
		}

	
		private void menuOptions_Click(object sender, System.EventArgs e)
		{
			ShowSettingsForm();
		}


		public static void FindDesktopBob()
		{
			OpenFileDialog oFileDlg = new OpenFileDialog();
			oFileDlg.InitialDirectory = "C:\\Program Files";
			oFileDlg.Filter = ".Net assemblies (*.exe, *.dll)|*.dll;*.exe|All files (*.*)|*.*" ;
			
			if(oFileDlg.ShowDialog() == DialogResult.OK) 
			{
				//Make sure selected file is an assembly file
				Assembly assembly;
				try 
				{
					assembly = Assembly.LoadFrom(oFileDlg.FileName);
					FileInfo oFileInfo = new FileInfo(oFileDlg.FileName); 

				}
				catch (Exception) 
				{
					MessageBox.Show("This is not a valid assembly.", 
						"Gotcha! Trying to be smart huah! eh!",
						MessageBoxButtons.OK,
						MessageBoxIcon.Information);

					return;
				}

			}
			else if (oFileDlg.ShowDialog() == DialogResult.Cancel)
			{
				oFileDlg.Dispose();
				return;
			}

			oFileDlg.Dispose();
		}


		#endregion
	
		#region Outlook related code
		
		public void WriteToOutlookSigDir(bool bOutlookHTMLFile)
		{
			// Using the local variable
			string strOutlookDir = null;

			// Get the OutlookDirectory only if it's not retrieved before
			if((m_strOutlookSigDir == null) && (string.Empty != m_strOutlookSigDir) )
			{
				// This works on MS Outlook 2000 vesion
				m_strOutlookSigDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
				m_strOutlookSigDir += @"\Microsoft\Signatures";
				strOutlookDir = m_strOutlookSigDir; 
			}
			else
			{
				strOutlookDir = m_strOutlookSigDir; 
			}

			// Make sure that directory exist before we go further
			DirectoryInfo oDirInfo = new DirectoryInfo(strOutlookDir);

			// Before we ahead, check that directory exist
			if(oDirInfo.Exists)
			{
				string strMsg = String.Format("File successfully written!" + "\n");
				StreamWriter oStreamWriter = null;

				try
				{
					if(bOutlookHTMLFile)
					{
						strOutlookDir += System.IO.Path.DirectorySeparatorChar +  m_strOutlookHTMLFile; 
						oStreamWriter = File.CreateText(strOutlookDir);

						//BUG: If the focus not been to HTML Tab then textBox_Quotes.Text is empty...

						// Get the HTML Source from textbox_Quotes
						oStreamWriter.WriteLine(textBox_Quotes.Text.ToString());
					}
					else
					{
						strOutlookDir += System.IO.Path.DirectorySeparatorChar +  m_strOutlookTextFile; 
						oStreamWriter = File.CreateText(strOutlookDir);

						// Get the current quote 
						oStreamWriter.WriteLine(m_quotesCollection.RetrieveCurrentQuote.ToString());
					}

					oStreamWriter.Close();
					strMsg += strOutlookDir;
					MessageBox.Show(strMsg, "WriteFile", MessageBoxButtons.OK, MessageBoxIcon.Information);
				}
				catch (Exception ex)
				{
					MessageBox.Show(ex.Message);
				}
			}
			else
			{
				MessageBox.Show("Outlook Signatures directory not exist!", "Outlook", MessageBoxButtons.OK, MessageBoxIcon.Hand);
			}
		}
		
		#endregion

		public DotNetServicePacks VersionCheck()
		{
			switch (Version)                                  
			{
				case "1.0.3705.0":
					return DotNetServicePacks.FrameWork_1_0;
				case "1.0.3705.288":
					return DotNetServicePacks.FrameWork_1_0_SP2;
				case "1.1.4322.0":
					return DotNetServicePacks.FrameWork_1_1;
				default:
					return DotNetServicePacks.NoFrameworkFound;
			}
		}

		#region Reading/Writing Settings code
	
		private void ShowSettingsForm()
		{

			SettingsForm oSettingsDlg = new SettingsForm(m_strOutlookTextFile, m_strOutlookHTMLFile,
				m_strDesktopBobLoc, m_strAutoSigLoc);
			
			if (oSettingsDlg.ShowDialog() == DialogResult.OK)
			{
				m_strOutlookTextFile = oSettingsDlg.OutlookTxtFileName;
				m_strOutlookHTMLFile = oSettingsDlg.OutlookHTMLFileName;

				m_strDesktopBobLoc= oSettingsDlg.DesktopBobLoc;
				m_strAutoSigLoc = oSettingsDlg.AutoSigLoc;
				
				this.Refresh();
				StoreSettingsInXMLFile();
			}
		}


		private void StoreSettingsInXMLFile()
		{

			FileStream oFileStream = new FileStream("Settings.xml", FileMode.Create, 
											FileAccess.Write);
			StreamWriter oStreamWriter = new StreamWriter(oFileStream);

			XmlTextWriter oXMLTextWriter = new XmlTextWriter(oStreamWriter);
			oXMLTextWriter.Indentation = 4;	
			oXMLTextWriter.Formatting = Formatting.Indented;
			
			oXMLTextWriter.WriteStartDocument();			
			oXMLTextWriter.WriteStartElement("Settings");	

				oXMLTextWriter.WriteStartElement("OutlookSignatureFileNames");			

					oXMLTextWriter.WriteStartElement("TextFileName");	
						if(string.Empty != m_strOutlookTextFile)
							oXMLTextWriter.WriteString(m_strOutlookTextFile);
						else
							oXMLTextWriter.WriteString("");
					oXMLTextWriter.WriteEndElement();

					oXMLTextWriter.WriteStartElement("HTMLFileName");
						if(string.Empty != m_strOutlookHTMLFile)
							oXMLTextWriter.WriteString(m_strOutlookHTMLFile);
						else
							oXMLTextWriter.WriteString("");
					oXMLTextWriter.WriteEndElement();

				oXMLTextWriter.WriteEndElement();

				oXMLTextWriter.WriteStartElement("ApplicationPaths");

					oXMLTextWriter.WriteStartElement("DesktopBobLocation");			
						if(string.Empty != m_strDesktopBobLoc)
							oXMLTextWriter.WriteString(m_strDesktopBobLoc);
						else
							oXMLTextWriter.WriteString("");
					oXMLTextWriter.WriteEndElement();

					oXMLTextWriter.WriteStartElement("AutoSigLocation");			
						if(string.Empty != m_strDesktopBobLoc)
							oXMLTextWriter.WriteString(m_strAutoSigLoc);
						else
							oXMLTextWriter.WriteString("");
					oXMLTextWriter.WriteEndElement();

				oXMLTextWriter.WriteEndElement();

			oXMLTextWriter.WriteEndElement();

			oXMLTextWriter.WriteEndDocument();

			oFileStream.Flush();
			oStreamWriter.Flush();

			oXMLTextWriter.Close();			
		}

		
		private void LoadSettingsFromXML()
		{
			XmlTextReader oXMLTextReader = null;

			try
			{
				FileStream oFileStream = new FileStream("Settings.xml", FileMode.Open, 
										FileAccess.Read);

				StreamReader oStreamReader = new StreamReader(oFileStream);
				oStreamReader.BaseStream.Seek(0,SeekOrigin.Begin);

				oXMLTextReader = new XmlTextReader(oStreamReader);
								
				while(oXMLTextReader.Read())
				{
					XmlNodeType oXMLNodeType = oXMLTextReader.NodeType;

					if (oXMLNodeType == XmlNodeType.Element)
					{	
						if (oXMLTextReader.Name == "TextFileName")
						{
							m_strOutlookTextFile = oXMLTextReader.ReadElementString();
						}
						
						if (oXMLTextReader.Name == "HTMLFileName")
						{
							m_strOutlookHTMLFile = oXMLTextReader.ReadElementString();
						}

						if (oXMLTextReader.Name == "DesktopBobLocation")
						{
							m_strDesktopBobLoc = oXMLTextReader.ReadElementString();
						}

						if (oXMLTextReader.Name == "AutoSigLocation")
						{
							m_strAutoSigLoc = oXMLTextReader.ReadElementString();
						}
					}
				}
			}
			catch(Exception)
			{	
				// If first time the file is not found, create the settings file
				StoreSettingsInXMLFile();
			}				
			finally
			{
				if (oXMLTextReader != null)
				{
					oXMLTextReader.Close();
				}								
			}
		}


		#endregion

		private void CopyFileToDestination(string strOrigLoc, string strDestLoc)
		{
			bool bResult = false;
			string strDBTemp = "Signature.xml";
			strDestLoc = strDestLoc + "\\" + strDBTemp; 

			try
			{
				File.Copy(strOrigLoc, strDestLoc, true);
				bResult = true;
			}
			catch (Exception ex)
			{
				string strErrMsg = String.Format("Unable to copy {0}" + " to " + "\n\r" 
					+ "{1}" + "\n\r" + "Error Message : {2}" + "\n\r" + "Try to find the path via Edit->Options...", 
					strOrigLoc, strDestLoc, ex.Message);
										
				MessageBox.Show( strErrMsg, "Copy file error!", 
					MessageBoxButtons.OK, MessageBoxIcon.Error);
										
				Debug.WriteLine(ex.Message);
									
				bResult = false;
			}

			if(bResult)
			{
				string strMsg =  String.Format("The {0} copied to" + "\n\r" + "{1}",
					strOrigLoc, strDestLoc);

				MessageBox.Show(
					strMsg, "Success!", 
					MessageBoxButtons.OK, MessageBoxIcon.Information);

			}
		}
	}
}

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Product Manager
United States United States
Under Construction. Please check back later.

రవికాంత్

Comments and Discussions