diff --git a/FormCopy.Designer.cs b/FormCopy.Designer.cs index 99687bc..0d82625 100644 --- a/FormCopy.Designer.cs +++ b/FormCopy.Designer.cs @@ -23,64 +23,75 @@ protected override void Dispose(bool disposing) { /// the contents of this method with the code editor. /// private void InitializeComponent() { - System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FormCopy)); - this.lblText = new System.Windows.Forms.Label(); - this.txtFileName = new System.Windows.Forms.TextBox(); - this.btnCancel = new System.Windows.Forms.Button(); - this.btnOK = new System.Windows.Forms.Button(); - this.SuspendLayout(); - // - // lblText - // - this.lblText.AutoSize = true; - this.lblText.Location = new System.Drawing.Point(12, 9); - this.lblText.Name = "lblText"; - this.lblText.Size = new System.Drawing.Size(0, 13); - this.lblText.TabIndex = 0; - // - // txtFileName - // - this.txtFileName.Location = new System.Drawing.Point(12, 44); - this.txtFileName.Name = "txtFileName"; - this.txtFileName.Size = new System.Drawing.Size(307, 20); - this.txtFileName.TabIndex = 1; - // - // btnCancel - // - this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel; - this.btnCancel.Location = new System.Drawing.Point(244, 111); - this.btnCancel.Name = "btnCancel"; - this.btnCancel.Size = new System.Drawing.Size(75, 23); - this.btnCancel.TabIndex = 2; - this.btnCancel.Text = "Cancel"; - this.btnCancel.UseVisualStyleBackColor = true; - // - // btnOK - // - this.btnOK.DialogResult = System.Windows.Forms.DialogResult.OK; - this.btnOK.Location = new System.Drawing.Point(163, 111); - this.btnOK.Name = "btnOK"; - this.btnOK.Size = new System.Drawing.Size(75, 23); - this.btnOK.TabIndex = 3; - this.btnOK.Text = "OK"; - this.btnOK.UseVisualStyleBackColor = true; - // - // FormCopy - // - this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); - this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(331, 146); - this.Controls.Add(this.btnOK); - this.Controls.Add(this.btnCancel); - this.Controls.Add(this.txtFileName); - this.Controls.Add(this.lblText); - this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; - this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); - this.MaximizeBox = false; - this.MinimizeBox = false; - this.Name = "FormCopy"; - this.ResumeLayout(false); - this.PerformLayout(); + System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FormCopy)); + this.lblText = new System.Windows.Forms.Label(); + this.txtFileName = new System.Windows.Forms.TextBox(); + this.btnCancel = new System.Windows.Forms.Button(); + this.btnOK = new System.Windows.Forms.Button(); + this.SuspendLayout(); + // + // lblText + // + this.lblText.AutoSize = true; + this.lblText.Location = new System.Drawing.Point(18, 14); + this.lblText.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0); + this.lblText.Name = "lblText"; + this.lblText.Size = new System.Drawing.Size(0, 20); + this.lblText.TabIndex = 0; + // + // txtFileName + // + this.txtFileName.Location = new System.Drawing.Point(45, 44); + this.txtFileName.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5); + this.txtFileName.Name = "txtFileName"; + this.txtFileName.Size = new System.Drawing.Size(409, 26); + this.txtFileName.TabIndex = 1; + this.txtFileName.Text = "My New Profile"; + // + // btnCancel + // + this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel; + this.btnCancel.Location = new System.Drawing.Point(342, 80); + this.btnCancel.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5); + this.btnCancel.Name = "btnCancel"; + this.btnCancel.Size = new System.Drawing.Size(112, 35); + this.btnCancel.TabIndex = 2; + this.btnCancel.Text = "Cancel"; + this.btnCancel.UseVisualStyleBackColor = true; + // + // btnOK + // + this.btnOK.DialogResult = System.Windows.Forms.DialogResult.OK; + this.btnOK.Location = new System.Drawing.Point(222, 80); + this.btnOK.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5); + this.btnOK.Name = "btnOK"; + this.btnOK.Size = new System.Drawing.Size(112, 35); + this.btnOK.TabIndex = 3; + this.btnOK.Text = "OK"; + this.btnOK.UseVisualStyleBackColor = true; + this.btnOK.Click += new System.EventHandler(this.btnOK_Click); + // + // FormCopy + // + this.AcceptButton = this.btnOK; + this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 20F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.CancelButton = this.btnCancel; + this.ClientSize = new System.Drawing.Size(496, 129); + this.Controls.Add(this.btnOK); + this.Controls.Add(this.btnCancel); + this.Controls.Add(this.txtFileName); + this.Controls.Add(this.lblText); + this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; + this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); + this.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5); + this.MaximizeBox = false; + this.MinimizeBox = false; + this.Name = "FormCopy"; + this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent; + this.Text = "Enter new name"; + this.ResumeLayout(false); + this.PerformLayout(); } diff --git a/FormCopy.cs b/FormCopy.cs index 40a12a6..7a86c00 100644 --- a/FormCopy.cs +++ b/FormCopy.cs @@ -15,7 +15,25 @@ public string FileName { public FormCopy(string inputText) { InitializeComponent(); lblText.Text = inputText; - txtFileName.Text = string.Empty; + txtFileName.Text = "NewProfile"; + } + + public FormCopy(string inputText,string startingFileName) + { + InitializeComponent(); + lblText.Text = inputText; + txtFileName.Text = startingFileName; + } + + private void btnOK_Click(object sender, EventArgs e) + { + txtFileName.Text=txtFileName.Text.Replace(" ", "_"); + txtFileName.Text = txtFileName.Text.Replace("(", ""); + txtFileName.Text = txtFileName.Text.Replace(")", ""); + if (txtFileName.Text.Length==0) + { + this.DialogResult = DialogResult.Cancel; + } + } } - } } diff --git a/FormMain.Designer.cs b/FormMain.Designer.cs index a534055..ec620a3 100644 --- a/FormMain.Designer.cs +++ b/FormMain.Designer.cs @@ -25,112 +25,58 @@ protected override void Dispose(bool disposing) { private void InitializeComponent() { this.components = new System.ComponentModel.Container(); System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FormMain)); - this.toolStrip = new System.Windows.Forms.ToolStrip(); - this.btnUseAsHosts = new System.Windows.Forms.ToolStripButton(); - this.btnCopy = new System.Windows.Forms.ToolStripButton(); - this.btnDelete = new System.Windows.Forms.ToolStripButton(); - this.btnViewEdit = new System.Windows.Forms.ToolStripButton(); - this.btnOpenFolder = new System.Windows.Forms.ToolStripButton(); - this.toolStripSeparator1 = new System.Windows.Forms.ToolStripSeparator(); - this.btnExit = new System.Windows.Forms.ToolStripButton(); + System.Windows.Forms.ListViewItem listViewItem1 = new System.Windows.Forms.ListViewItem("Deneme..."); this.notifyIcon = new System.Windows.Forms.NotifyIcon(this.components); this.menuStrip = new System.Windows.Forms.ContextMenuStrip(this.components); this.menuStripShow = new System.Windows.Forms.ToolStripMenuItem(); this.menuStripExit = new System.Windows.Forms.ToolStripMenuItem(); + this.quickSwitchToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.panContent = new System.Windows.Forms.Panel(); - this.lblHosts = new System.Windows.Forms.Label(); - this.lblUsing = new System.Windows.Forms.Label(); + this.listView1 = new ListViewEx.ListViewEx(); this.panel1 = new System.Windows.Forms.Panel(); - this.listHosts = new System.Windows.Forms.ListBox(); + this.label1 = new System.Windows.Forms.Label(); + this.listBox_hosts = new System.Windows.Forms.ListBox(); + this.groupBox1 = new System.Windows.Forms.GroupBox(); + this.button_new = new System.Windows.Forms.Button(); + this.button_delete = new System.Windows.Forms.Button(); + this.button_copy = new System.Windows.Forms.Button(); + this.menuStrip1 = new System.Windows.Forms.MenuStrip(); + this.fileToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.saveToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.toolStripSeparator3 = new System.Windows.Forms.ToolStripSeparator(); + this.newToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.copyToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.deleteToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.toolStripSeparator1 = new System.Windows.Forms.ToolStripSeparator(); + this.importToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.exportToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.toolStripSeparator2 = new System.Windows.Forms.ToolStripSeparator(); + this.exitToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.editToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.addToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.removeToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.hostsFileToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.viewCurrentToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.openFolderToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.button_save = new System.Windows.Forms.Button(); + this.button_removeDomain = new System.Windows.Forms.Button(); + this.button_add_domain = new System.Windows.Forms.Button(); this.panBottom = new System.Windows.Forms.Panel(); + this.button_add_multiple = new System.Windows.Forms.Button(); + this.button_use_as_hosts = new System.Windows.Forms.Button(); this.txtLog = new System.Windows.Forms.TextBox(); this.lblLog = new System.Windows.Forms.Label(); - this.quickSwitchToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); - this.toolStrip.SuspendLayout(); + this.label3 = new System.Windows.Forms.Label(); + this.label2 = new System.Windows.Forms.Label(); + this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components); this.menuStrip.SuspendLayout(); this.panContent.SuspendLayout(); this.panel1.SuspendLayout(); + this.groupBox1.SuspendLayout(); + this.menuStrip1.SuspendLayout(); this.panBottom.SuspendLayout(); this.SuspendLayout(); // - // toolStrip - // - this.toolStrip.AutoSize = false; - this.toolStrip.Dock = System.Windows.Forms.DockStyle.Right; - this.toolStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { - this.btnUseAsHosts, - this.btnCopy, - this.btnDelete, - this.btnViewEdit, - this.btnOpenFolder, - this.toolStripSeparator1, - this.btnExit}); - this.toolStrip.Location = new System.Drawing.Point(3, 0); - this.toolStrip.Name = "toolStrip"; - this.toolStrip.Size = new System.Drawing.Size(111, 152); - this.toolStrip.TabIndex = 1; - this.toolStrip.Text = "toolStrip1"; - this.toolStrip.ItemClicked += new System.Windows.Forms.ToolStripItemClickedEventHandler(this.toolStrip_ItemClicked); - // - // btnUseAsHosts - // - this.btnUseAsHosts.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text; - this.btnUseAsHosts.Image = ((System.Drawing.Image)(resources.GetObject("btnUseAsHosts.Image"))); - this.btnUseAsHosts.ImageTransparentColor = System.Drawing.Color.Magenta; - this.btnUseAsHosts.Name = "btnUseAsHosts"; - this.btnUseAsHosts.Size = new System.Drawing.Size(109, 17); - this.btnUseAsHosts.Text = "&Use as HOSTS"; - // - // btnCopy - // - this.btnCopy.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text; - this.btnCopy.Image = ((System.Drawing.Image)(resources.GetObject("btnCopy.Image"))); - this.btnCopy.ImageTransparentColor = System.Drawing.Color.Magenta; - this.btnCopy.Name = "btnCopy"; - this.btnCopy.Size = new System.Drawing.Size(109, 17); - this.btnCopy.Text = "&Copy"; - // - // btnDelete - // - this.btnDelete.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text; - this.btnDelete.Image = ((System.Drawing.Image)(resources.GetObject("btnDelete.Image"))); - this.btnDelete.ImageTransparentColor = System.Drawing.Color.Magenta; - this.btnDelete.Name = "btnDelete"; - this.btnDelete.Size = new System.Drawing.Size(109, 17); - this.btnDelete.Text = "&Delete"; - // - // btnViewEdit - // - this.btnViewEdit.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text; - this.btnViewEdit.Image = ((System.Drawing.Image)(resources.GetObject("btnViewEdit.Image"))); - this.btnViewEdit.ImageTransparentColor = System.Drawing.Color.Magenta; - this.btnViewEdit.Name = "btnViewEdit"; - this.btnViewEdit.Size = new System.Drawing.Size(109, 17); - this.btnViewEdit.Text = "&View/Edit"; - // - // btnOpenFolder - // - this.btnOpenFolder.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text; - this.btnOpenFolder.Image = ((System.Drawing.Image)(resources.GetObject("btnOpenFolder.Image"))); - this.btnOpenFolder.ImageTransparentColor = System.Drawing.Color.Magenta; - this.btnOpenFolder.Name = "btnOpenFolder"; - this.btnOpenFolder.Size = new System.Drawing.Size(109, 17); - this.btnOpenFolder.Text = "&Open Folder"; - // - // toolStripSeparator1 - // - this.toolStripSeparator1.Name = "toolStripSeparator1"; - this.toolStripSeparator1.Size = new System.Drawing.Size(109, 6); - // - // btnExit - // - this.btnExit.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text; - this.btnExit.Image = ((System.Drawing.Image)(resources.GetObject("btnExit.Image"))); - this.btnExit.ImageTransparentColor = System.Drawing.Color.Magenta; - this.btnExit.Name = "btnExit"; - this.btnExit.Size = new System.Drawing.Size(109, 17); - this.btnExit.Text = "&Exit"; - // // notifyIcon // this.notifyIcon.ContextMenuStrip = this.menuStrip; @@ -141,124 +87,416 @@ private void InitializeComponent() { // // menuStrip // + this.menuStrip.ImageScalingSize = new System.Drawing.Size(24, 24); this.menuStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { this.menuStripShow, this.menuStripExit, this.quickSwitchToolStripMenuItem}); this.menuStrip.Name = "menuStrip"; - this.menuStrip.Size = new System.Drawing.Size(153, 92); + this.menuStrip.Size = new System.Drawing.Size(186, 100); // // menuStripShow // this.menuStripShow.Name = "menuStripShow"; - this.menuStripShow.Size = new System.Drawing.Size(152, 22); + this.menuStripShow.Size = new System.Drawing.Size(185, 32); this.menuStripShow.Text = "&Show"; this.menuStripShow.Click += new System.EventHandler(this.menuStripShow_Click); // // menuStripExit // this.menuStripExit.Name = "menuStripExit"; - this.menuStripExit.Size = new System.Drawing.Size(152, 22); + this.menuStripExit.Size = new System.Drawing.Size(185, 32); this.menuStripExit.Text = "&Exit"; this.menuStripExit.Click += new System.EventHandler(this.menuStripExit_Click); // + // quickSwitchToolStripMenuItem + // + this.quickSwitchToolStripMenuItem.Name = "quickSwitchToolStripMenuItem"; + this.quickSwitchToolStripMenuItem.Size = new System.Drawing.Size(185, 32); + this.quickSwitchToolStripMenuItem.Text = "&Quick Switch"; + // // panContent // - this.panContent.Controls.Add(this.lblHosts); - this.panContent.Controls.Add(this.lblUsing); + this.panContent.Controls.Add(this.listView1); this.panContent.Controls.Add(this.panel1); - this.panContent.Controls.Add(this.listHosts); + this.panContent.Controls.Add(this.menuStrip1); this.panContent.Dock = System.Windows.Forms.DockStyle.Top; this.panContent.Location = new System.Drawing.Point(0, 0); + this.panContent.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5); this.panContent.Name = "panContent"; - this.panContent.Size = new System.Drawing.Size(361, 259); + this.panContent.Size = new System.Drawing.Size(1014, 640); this.panContent.TabIndex = 4; // - // lblHosts + // listView1 + // + this.listView1.AllowColumnReorder = true; + this.listView1.CheckBoxes = true; + this.listView1.DoubleClickActivation = false; + this.listView1.FullRowSelect = true; + this.listView1.GridLines = true; + this.listView1.HideSelection = false; + listViewItem1.StateImageIndex = 0; + this.listView1.Items.AddRange(new System.Windows.Forms.ListViewItem[] { + listViewItem1}); + this.listView1.Location = new System.Drawing.Point(251, 36); + this.listView1.Margin = new System.Windows.Forms.Padding(2, 3, 2, 3); + this.listView1.MultiSelect = false; + this.listView1.Name = "listView1"; + this.listView1.Size = new System.Drawing.Size(754, 604); + this.listView1.TabIndex = 19; + this.listView1.UseCompatibleStateImageBehavior = false; + this.listView1.View = System.Windows.Forms.View.Details; // - this.lblHosts.AutoSize = true; - this.lblHosts.Font = new System.Drawing.Font("Consolas", 9.75F); - this.lblHosts.Location = new System.Drawing.Point(240, 195); - this.lblHosts.Name = "lblHosts"; - this.lblHosts.Size = new System.Drawing.Size(42, 15); - this.lblHosts.TabIndex = 5; - this.lblHosts.Text = "hosts"; + // panel1 // - // lblUsing + this.panel1.Controls.Add(this.label1); + this.panel1.Controls.Add(this.listBox_hosts); + this.panel1.Controls.Add(this.groupBox1); + this.panel1.Dock = System.Windows.Forms.DockStyle.Left; + this.panel1.Location = new System.Drawing.Point(0, 33); + this.panel1.Name = "panel1"; + this.panel1.Size = new System.Drawing.Size(236, 607); + this.panel1.TabIndex = 18; + // + // label1 + // + this.label1.AutoSize = true; + this.label1.Location = new System.Drawing.Point(12, 4); + this.label1.Name = "label1"; + this.label1.Size = new System.Drawing.Size(65, 20); + this.label1.TabIndex = 14; + this.label1.Text = "Profiles:"; + // + // listBox_hosts + // + this.listBox_hosts.FormattingEnabled = true; + this.listBox_hosts.ItemHeight = 20; + this.listBox_hosts.Location = new System.Drawing.Point(16, 27); + this.listBox_hosts.Name = "listBox_hosts"; + this.listBox_hosts.Size = new System.Drawing.Size(202, 384); + this.listBox_hosts.TabIndex = 11; + this.listBox_hosts.SelectedIndexChanged += new System.EventHandler(this.listBox_hosts_SelectedIndexChanged); + this.listBox_hosts.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(this.listBox_hosts_MouseDoubleClick); + // + // groupBox1 + // + this.groupBox1.Controls.Add(this.button_new); + this.groupBox1.Controls.Add(this.button_delete); + this.groupBox1.Controls.Add(this.button_copy); + this.groupBox1.Location = new System.Drawing.Point(16, 417); + this.groupBox1.Name = "groupBox1"; + this.groupBox1.Size = new System.Drawing.Size(202, 169); + this.groupBox1.TabIndex = 8; + this.groupBox1.TabStop = false; + this.groupBox1.Text = "Host Profiles"; + // + // button_new + // + this.button_new.Location = new System.Drawing.Point(36, 34); + this.button_new.Name = "button_new"; + this.button_new.Size = new System.Drawing.Size(121, 35); + this.button_new.TabIndex = 10; + this.button_new.Text = "New"; + this.button_new.UseVisualStyleBackColor = true; + this.button_new.Click += new System.EventHandler(this.button_new_Click); + // + // button_delete + // + this.button_delete.Location = new System.Drawing.Point(36, 123); + this.button_delete.Name = "button_delete"; + this.button_delete.Size = new System.Drawing.Size(121, 35); + this.button_delete.TabIndex = 9; + this.button_delete.Text = "Delete"; + this.button_delete.UseVisualStyleBackColor = true; + this.button_delete.Click += new System.EventHandler(this.button_delete_Click); + // + // button_copy + // + this.button_copy.Location = new System.Drawing.Point(36, 82); + this.button_copy.Name = "button_copy"; + this.button_copy.Size = new System.Drawing.Size(121, 35); + this.button_copy.TabIndex = 8; + this.button_copy.Text = "Copy"; + this.button_copy.UseVisualStyleBackColor = true; + this.button_copy.Click += new System.EventHandler(this.button_copy_Click); + // + // menuStrip1 + // + this.menuStrip1.GripMargin = new System.Windows.Forms.Padding(2, 2, 0, 2); + this.menuStrip1.ImageScalingSize = new System.Drawing.Size(24, 24); + this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.fileToolStripMenuItem, + this.editToolStripMenuItem, + this.hostsFileToolStripMenuItem}); + this.menuStrip1.Location = new System.Drawing.Point(0, 0); + this.menuStrip1.Name = "menuStrip1"; + this.menuStrip1.Size = new System.Drawing.Size(1014, 33); + this.menuStrip1.TabIndex = 9; + this.menuStrip1.Text = "menuStrip1"; + // + // fileToolStripMenuItem + // + this.fileToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.saveToolStripMenuItem, + this.toolStripSeparator3, + this.newToolStripMenuItem, + this.copyToolStripMenuItem, + this.deleteToolStripMenuItem, + this.toolStripSeparator1, + this.importToolStripMenuItem, + this.exportToolStripMenuItem, + this.toolStripSeparator2, + this.exitToolStripMenuItem}); + this.fileToolStripMenuItem.Name = "fileToolStripMenuItem"; + this.fileToolStripMenuItem.Size = new System.Drawing.Size(54, 29); + this.fileToolStripMenuItem.Text = "File"; // - this.lblUsing.AutoSize = true; - this.lblUsing.Font = new System.Drawing.Font("Consolas", 9.75F); - this.lblUsing.Location = new System.Drawing.Point(240, 171); - this.lblUsing.Name = "lblUsing"; - this.lblUsing.Size = new System.Drawing.Size(119, 15); - this.lblUsing.TabIndex = 4; - this.lblUsing.Text = "Currently using:"; + // saveToolStripMenuItem // - // panel1 + this.saveToolStripMenuItem.Name = "saveToolStripMenuItem"; + this.saveToolStripMenuItem.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.S))); + this.saveToolStripMenuItem.Size = new System.Drawing.Size(241, 34); + this.saveToolStripMenuItem.Text = "Save"; + this.saveToolStripMenuItem.Click += new System.EventHandler(this.saveToolStripMenuItem_Click); // - this.panel1.Controls.Add(this.toolStrip); - this.panel1.Location = new System.Drawing.Point(237, 12); - this.panel1.Name = "panel1"; - this.panel1.Size = new System.Drawing.Size(114, 152); - this.panel1.TabIndex = 3; + // toolStripSeparator3 + // + this.toolStripSeparator3.Name = "toolStripSeparator3"; + this.toolStripSeparator3.Size = new System.Drawing.Size(238, 6); + // + // newToolStripMenuItem + // + this.newToolStripMenuItem.Name = "newToolStripMenuItem"; + this.newToolStripMenuItem.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.N))); + this.newToolStripMenuItem.Size = new System.Drawing.Size(241, 34); + this.newToolStripMenuItem.Text = "New"; + this.newToolStripMenuItem.Click += new System.EventHandler(this.newToolStripMenuItem_Click); + // + // copyToolStripMenuItem + // + this.copyToolStripMenuItem.Name = "copyToolStripMenuItem"; + this.copyToolStripMenuItem.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.C))); + this.copyToolStripMenuItem.Size = new System.Drawing.Size(241, 34); + this.copyToolStripMenuItem.Text = "Copy"; + this.copyToolStripMenuItem.Click += new System.EventHandler(this.copyToolStripMenuItem_Click); + // + // deleteToolStripMenuItem + // + this.deleteToolStripMenuItem.Name = "deleteToolStripMenuItem"; + this.deleteToolStripMenuItem.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.Delete))); + this.deleteToolStripMenuItem.Size = new System.Drawing.Size(241, 34); + this.deleteToolStripMenuItem.Text = "Delete"; + this.deleteToolStripMenuItem.Click += new System.EventHandler(this.deleteToolStripMenuItem_Click); + // + // toolStripSeparator1 + // + this.toolStripSeparator1.Name = "toolStripSeparator1"; + this.toolStripSeparator1.Size = new System.Drawing.Size(238, 6); + // + // importToolStripMenuItem // - // listHosts + this.importToolStripMenuItem.Name = "importToolStripMenuItem"; + this.importToolStripMenuItem.Size = new System.Drawing.Size(241, 34); + this.importToolStripMenuItem.Text = "Import Profiles"; + this.importToolStripMenuItem.Click += new System.EventHandler(this.importToolStripMenuItem_Click); + // + // exportToolStripMenuItem // - this.listHosts.Font = new System.Drawing.Font("Consolas", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); - this.listHosts.FormattingEnabled = true; - this.listHosts.ItemHeight = 15; - this.listHosts.Location = new System.Drawing.Point(12, 12); - this.listHosts.Name = "listHosts"; - this.listHosts.Size = new System.Drawing.Size(216, 229); - this.listHosts.TabIndex = 2; - this.listHosts.DoubleClick += new System.EventHandler(this.listHosts_DoubleClick); + this.exportToolStripMenuItem.Name = "exportToolStripMenuItem"; + this.exportToolStripMenuItem.Size = new System.Drawing.Size(241, 34); + this.exportToolStripMenuItem.Text = "Export Profiles"; + this.exportToolStripMenuItem.Click += new System.EventHandler(this.exportToolStripMenuItem_Click); + // + // toolStripSeparator2 + // + this.toolStripSeparator2.Name = "toolStripSeparator2"; + this.toolStripSeparator2.Size = new System.Drawing.Size(238, 6); + // + // exitToolStripMenuItem + // + this.exitToolStripMenuItem.Name = "exitToolStripMenuItem"; + this.exitToolStripMenuItem.Size = new System.Drawing.Size(241, 34); + this.exitToolStripMenuItem.Text = "Exit"; + this.exitToolStripMenuItem.Click += new System.EventHandler(this.exitToolStripMenuItem_Click); + // + // editToolStripMenuItem + // + this.editToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.addToolStripMenuItem, + this.removeToolStripMenuItem}); + this.editToolStripMenuItem.Name = "editToolStripMenuItem"; + this.editToolStripMenuItem.Size = new System.Drawing.Size(58, 29); + this.editToolStripMenuItem.Text = "Edit"; + // + // addToolStripMenuItem + // + this.addToolStripMenuItem.Name = "addToolStripMenuItem"; + this.addToolStripMenuItem.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.Space))); + this.addToolStripMenuItem.Size = new System.Drawing.Size(246, 34); + this.addToolStripMenuItem.Text = "Add"; + this.addToolStripMenuItem.Click += new System.EventHandler(this.addToolStripMenuItem_Click); + // + // removeToolStripMenuItem + // + this.removeToolStripMenuItem.Name = "removeToolStripMenuItem"; + this.removeToolStripMenuItem.ShortcutKeys = System.Windows.Forms.Keys.Delete; + this.removeToolStripMenuItem.Size = new System.Drawing.Size(246, 34); + this.removeToolStripMenuItem.Text = "Remove"; + this.removeToolStripMenuItem.Click += new System.EventHandler(this.removeToolStripMenuItem_Click); + // + // hostsFileToolStripMenuItem + // + this.hostsFileToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.viewCurrentToolStripMenuItem, + this.openFolderToolStripMenuItem}); + this.hostsFileToolStripMenuItem.Name = "hostsFileToolStripMenuItem"; + this.hostsFileToolStripMenuItem.Size = new System.Drawing.Size(105, 29); + this.hostsFileToolStripMenuItem.Text = "Hosts File"; + // + // viewCurrentToolStripMenuItem + // + this.viewCurrentToolStripMenuItem.Name = "viewCurrentToolStripMenuItem"; + this.viewCurrentToolStripMenuItem.Size = new System.Drawing.Size(214, 34); + this.viewCurrentToolStripMenuItem.Text = "View Current"; + this.viewCurrentToolStripMenuItem.Click += new System.EventHandler(this.viewCurrentToolStripMenuItem_Click); + // + // openFolderToolStripMenuItem + // + this.openFolderToolStripMenuItem.Name = "openFolderToolStripMenuItem"; + this.openFolderToolStripMenuItem.Size = new System.Drawing.Size(214, 34); + this.openFolderToolStripMenuItem.Text = "Open Folder"; + this.openFolderToolStripMenuItem.Click += new System.EventHandler(this.openFolderToolStripMenuItem_Click); + // + // button_save + // + this.button_save.Location = new System.Drawing.Point(629, 9); + this.button_save.Name = "button_save"; + this.button_save.Size = new System.Drawing.Size(121, 35); + this.button_save.TabIndex = 11; + this.button_save.Text = "Save"; + this.button_save.UseVisualStyleBackColor = true; + this.button_save.Click += new System.EventHandler(this.button_save_Click); + // + // button_removeDomain + // + this.button_removeDomain.Font = new System.Drawing.Font("Microsoft Sans Serif", 14F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.button_removeDomain.ImageAlign = System.Drawing.ContentAlignment.TopCenter; + this.button_removeDomain.Location = new System.Drawing.Point(821, 5); + this.button_removeDomain.Name = "button_removeDomain"; + this.button_removeDomain.Size = new System.Drawing.Size(49, 35); + this.button_removeDomain.TabIndex = 12; + this.button_removeDomain.Text = "-"; + this.button_removeDomain.TextAlign = System.Drawing.ContentAlignment.TopCenter; + this.button_removeDomain.UseVisualStyleBackColor = true; + this.button_removeDomain.Click += new System.EventHandler(this.button_removeDomain_Click); + // + // button_add_domain + // + this.button_add_domain.Font = new System.Drawing.Font("Microsoft Sans Serif", 14F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.button_add_domain.Location = new System.Drawing.Point(879, 5); + this.button_add_domain.Name = "button_add_domain"; + this.button_add_domain.Size = new System.Drawing.Size(49, 35); + this.button_add_domain.TabIndex = 11; + this.button_add_domain.Text = "+"; + this.button_add_domain.UseVisualStyleBackColor = true; + this.button_add_domain.Click += new System.EventHandler(this.button_add_domain_Click); // // panBottom // + this.panBottom.Controls.Add(this.button_add_multiple); + this.panBottom.Controls.Add(this.button_use_as_hosts); + this.panBottom.Controls.Add(this.button_save); + this.panBottom.Controls.Add(this.button_removeDomain); this.panBottom.Controls.Add(this.txtLog); + this.panBottom.Controls.Add(this.button_add_domain); this.panBottom.Controls.Add(this.lblLog); + this.panBottom.Controls.Add(this.label3); + this.panBottom.Controls.Add(this.label2); this.panBottom.Dock = System.Windows.Forms.DockStyle.Bottom; - this.panBottom.Location = new System.Drawing.Point(0, 265); + this.panBottom.Location = new System.Drawing.Point(0, 640); + this.panBottom.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5); this.panBottom.Name = "panBottom"; - this.panBottom.Size = new System.Drawing.Size(361, 111); + this.panBottom.Size = new System.Drawing.Size(1014, 61); this.panBottom.TabIndex = 5; // + // button_add_multiple + // + this.button_add_multiple.Font = new System.Drawing.Font("Microsoft Sans Serif", 8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.button_add_multiple.Location = new System.Drawing.Point(947, 6); + this.button_add_multiple.Name = "button_add_multiple"; + this.button_add_multiple.Size = new System.Drawing.Size(49, 35); + this.button_add_multiple.TabIndex = 10; + this.button_add_multiple.Text = "+++"; + this.button_add_multiple.UseVisualStyleBackColor = true; + this.button_add_multiple.Click += new System.EventHandler(this.button_add_multiple_Click); + // + // button_use_as_hosts + // + this.button_use_as_hosts.Location = new System.Drawing.Point(489, 9); + this.button_use_as_hosts.Name = "button_use_as_hosts"; + this.button_use_as_hosts.Size = new System.Drawing.Size(121, 35); + this.button_use_as_hosts.TabIndex = 13; + this.button_use_as_hosts.Text = "Apply"; + this.button_use_as_hosts.UseVisualStyleBackColor = true; + this.button_use_as_hosts.Click += new System.EventHandler(this.button_use_as_hosts_Click); + // // txtLog // this.txtLog.Font = new System.Drawing.Font("Consolas", 9.75F); - this.txtLog.Location = new System.Drawing.Point(7, 20); + this.txtLog.Location = new System.Drawing.Point(57, 11); + this.txtLog.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5); this.txtLog.Multiline = true; this.txtLog.Name = "txtLog"; this.txtLog.ReadOnly = true; this.txtLog.ScrollBars = System.Windows.Forms.ScrollBars.Vertical; - this.txtLog.Size = new System.Drawing.Size(344, 79); + this.txtLog.Size = new System.Drawing.Size(399, 35); this.txtLog.TabIndex = 1; // // lblLog // this.lblLog.AutoSize = true; this.lblLog.Font = new System.Drawing.Font("Consolas", 9.75F); - this.lblLog.Location = new System.Drawing.Point(4, 4); + this.lblLog.Location = new System.Drawing.Point(6, 6); + this.lblLog.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0); this.lblLog.Name = "lblLog"; - this.lblLog.Size = new System.Drawing.Size(28, 15); + this.lblLog.Size = new System.Drawing.Size(43, 23); this.lblLog.TabIndex = 0; this.lblLog.Text = "Log"; // - // quickSwitchToolStripMenuItem + // label3 // - this.quickSwitchToolStripMenuItem.Name = "quickSwitchToolStripMenuItem"; - this.quickSwitchToolStripMenuItem.Size = new System.Drawing.Size(152, 22); - this.quickSwitchToolStripMenuItem.Text = "&Quick Switch"; + this.label3.AutoSize = true; + this.label3.Location = new System.Drawing.Point(814, 37); + this.label3.Name = "label3"; + this.label3.Size = new System.Drawing.Size(68, 20); + this.label3.TabIndex = 16; + this.label3.Text = "Remove"; + // + // label2 + // + this.label2.AutoSize = true; + this.label2.Location = new System.Drawing.Point(884, 37); + this.label2.Name = "label2"; + this.label2.Size = new System.Drawing.Size(38, 20); + this.label2.TabIndex = 15; + this.label2.Text = "Add"; + // + // contextMenuStrip1 + // + this.contextMenuStrip1.ImageScalingSize = new System.Drawing.Size(24, 24); + this.contextMenuStrip1.Name = "contextMenuStrip1"; + this.contextMenuStrip1.Size = new System.Drawing.Size(61, 4); // // FormMain // - this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); + this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 20F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(361, 376); + this.ClientSize = new System.Drawing.Size(1014, 701); this.Controls.Add(this.panBottom); this.Controls.Add(this.panContent); - this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); + this.MainMenuStrip = this.menuStrip1; + this.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5); this.MaximizeBox = false; this.MinimizeBox = false; this.Name = "FormMain"; @@ -266,12 +504,14 @@ private void InitializeComponent() { this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "Hosts Manager"; this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.FormMain_FormClosing); - this.toolStrip.ResumeLayout(false); - this.toolStrip.PerformLayout(); this.menuStrip.ResumeLayout(false); this.panContent.ResumeLayout(false); this.panContent.PerformLayout(); this.panel1.ResumeLayout(false); + this.panel1.PerformLayout(); + this.groupBox1.ResumeLayout(false); + this.menuStrip1.ResumeLayout(false); + this.menuStrip1.PerformLayout(); this.panBottom.ResumeLayout(false); this.panBottom.PerformLayout(); this.ResumeLayout(false); @@ -279,28 +519,49 @@ private void InitializeComponent() { } #endregion - - private System.Windows.Forms.ToolStrip toolStrip; - private System.Windows.Forms.ToolStripButton btnUseAsHosts; - private System.Windows.Forms.ToolStripButton btnCopy; - private System.Windows.Forms.ToolStripButton btnDelete; - private System.Windows.Forms.ToolStripButton btnViewEdit; - private System.Windows.Forms.ToolStripButton btnOpenFolder; - private System.Windows.Forms.ToolStripSeparator toolStripSeparator1; - private System.Windows.Forms.ToolStripButton btnExit; private System.Windows.Forms.NotifyIcon notifyIcon; private System.Windows.Forms.Panel panContent; - private System.Windows.Forms.ListBox listHosts; private System.Windows.Forms.Panel panBottom; private System.Windows.Forms.Label lblLog; private System.Windows.Forms.TextBox txtLog; - private System.Windows.Forms.Panel panel1; private System.Windows.Forms.ContextMenuStrip menuStrip; private System.Windows.Forms.ToolStripMenuItem menuStripExit; private System.Windows.Forms.ToolStripMenuItem menuStripShow; - private System.Windows.Forms.Label lblHosts; - private System.Windows.Forms.Label lblUsing; private System.Windows.Forms.ToolStripMenuItem quickSwitchToolStripMenuItem; - } + private System.Windows.Forms.ContextMenuStrip contextMenuStrip1; + private System.Windows.Forms.MenuStrip menuStrip1; + private System.Windows.Forms.ToolStripMenuItem fileToolStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem exitToolStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem newToolStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem copyToolStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem deleteToolStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem saveToolStripMenuItem; + private System.Windows.Forms.Button button_removeDomain; + private System.Windows.Forms.Button button_add_domain; + private System.Windows.Forms.Button button_save; + private System.Windows.Forms.ToolStripMenuItem editToolStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem addToolStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem removeToolStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem hostsFileToolStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem viewCurrentToolStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem openFolderToolStripMenuItem; + private System.Windows.Forms.Button button_use_as_hosts; + private ListViewEx.ListViewEx listView1; + private System.Windows.Forms.Panel panel1; + private System.Windows.Forms.Label label1; + private System.Windows.Forms.ListBox listBox_hosts; + private System.Windows.Forms.GroupBox groupBox1; + private System.Windows.Forms.Button button_new; + private System.Windows.Forms.Button button_delete; + private System.Windows.Forms.Button button_copy; + private System.Windows.Forms.ToolStripSeparator toolStripSeparator1; + private System.Windows.Forms.ToolStripMenuItem importToolStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem exportToolStripMenuItem; + private System.Windows.Forms.ToolStripSeparator toolStripSeparator2; + private System.Windows.Forms.Label label3; + private System.Windows.Forms.Label label2; + private System.Windows.Forms.ToolStripSeparator toolStripSeparator3; + private System.Windows.Forms.Button button_add_multiple; + } } diff --git a/FormMain.cs b/FormMain.cs index 4f785f9..aeafc92 100644 --- a/FormMain.cs +++ b/FormMain.cs @@ -2,123 +2,678 @@ using System.Globalization; using System.Windows.Forms; using Barbar.HostsSwitcher.Provider; +using System.Xml; +using System.Collections; +using System.Collections.Generic; +using System.IO; +using System.Drawing; +using System.Drawing.Text; -namespace Barbar.HostsSwitcher { - public partial class FormMain : Form { - private readonly IHostProvider m_HostsProvider; +namespace Barbar.HostsSwitcher +{ + public partial class FormMain : Form + { + private readonly IHostProvider m_HostsProvider; - public FormMain() { - InitializeComponent(); + public HostsProfile currentHosts; + public HostsProfile selectedProfile; - m_HostsProvider = new HostProvider(); - quickSwitchToolStripMenuItem.DropDownItemClicked += new ToolStripItemClickedEventHandler(quickSwitchToolStripMenuItem_DropDownItemClicked); - foreach (var host in m_HostsProvider.GetHostFiles()) { - listHosts.Items.Add(host); - quickSwitchToolStripMenuItem.DropDownItems.Add(host); - } + public List profiles = new List(); - Text = string.Format("Hosts Switcher - v.{0}", typeof(FormMain).Assembly.GetName().Version); - } + public string appSettingsDir = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); + public string appSettingsFilePath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\HostsSwitcherProfiles.xml"; + + + private TextBox textBoxAddress; + private TextBox textBoxRedir; + private CheckBox isWWW; + private CheckBox isLocalHost; + private Control[] Editors; + + public FormMain() + { + InitializeComponent(); + + m_HostsProvider = new HostProvider(); + quickSwitchToolStripMenuItem.DropDownItemClicked += new ToolStripItemClickedEventHandler(quickSwitchToolStripMenuItem_DropDownItemClicked); + + Text = string.Format("Hosts Switcher - v.{0}", typeof(FormMain).Assembly.GetName().Version); + + listLocked = true; + if (File.Exists(appSettingsFilePath)) + { + profiles = HostsProfile.readFromXML(appSettingsFilePath); + currentHosts = HostsProfile.getCurrentProfile(profiles); + } + else + { + + List profiles = new List(); + currentHosts = new HostsProfile(); + profiles.Add(currentHosts); + HostsProfile.writeToSettingsXML(profiles); + } + initializeListView(); + + selectedProfile = currentHosts; + customizeHosts(selectedProfile); + updateListBox(); + listView1.ItemChecked += new ItemCheckedEventHandler(listView_CheckedChanged); + + + + + listView1.CheckBoxes = true; + listView1.SubItemClicked += new ListViewEx.SubItemEventHandler(listView1_SubItemClicked); + listView1.SubItemEndEditing += new ListViewEx.SubItemEndEditingEventHandler(listView1_SubItemEndEditing); + listView1.DoubleClickActivation = false; - private void quickSwitchToolStripMenuItem_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e) { - var clicked = e.ClickedItem.Text; - if (!string.IsNullOrEmpty(clicked)) { - UseAsHosts(clicked); - } - } - private void RefreshList() { - var selectedItem = (string)listHosts.SelectedItem; - bool setSelected = false; - listHosts.Items.Clear(); - foreach (var host in m_HostsProvider.GetHostFiles()) { - if (!string.IsNullOrEmpty(selectedItem) && string.Compare(host, selectedItem, StringComparison.OrdinalIgnoreCase) == 0) { - setSelected = true; } - listHosts.Items.Add(host); - } + volatile bool listLocked = false; - if (setSelected) { - listHosts.SelectedItem = selectedItem; - } - } + private void customizeHosts(HostsProfile hp) + { + listLocked = true; + listView1.Items.Clear(); + int i = 1; - private void FormMain_FormClosing(object sender, FormClosingEventArgs e) { - if (e.CloseReason == CloseReason.UserClosing) { - e.Cancel = true; - this.Visible = false; - } - } + foreach (Domain d in hp.domains) + { + ListViewItem a = new ListViewItem(new string[] { i.ToString(), d.useLocalhost.ToString(), d.alsoWWW.ToString(), d.redirectedAddress, d.domainToRedirect }); + if (d.isEnabled) + { + a.BackColor = Color.PaleGreen; + a.ForeColor = Color.DarkGreen; + a.Checked = true; + } + else + { + a.BackColor = Color.White; + a.ForeColor = Color.DarkGray; + a.Checked = false; + } + listView1.Items.Add(a); + i++; + } + listLocked = false; + } + private void initializeListView() + { + listLocked = true; + isLocalHost = new CheckBox(); + isLocalHost.Name = "isLocalHost"; + isLocalHost.Visible = false; + isLocalHost.Location = new System.Drawing.Point(32, 152); + isLocalHost.Size = new System.Drawing.Size(20, 20); + isLocalHost.CheckedChanged += new EventHandler(control_CheckedChanged); - private void notifyIcon_MouseClick(object sender, MouseEventArgs e) { - if (e.Button == MouseButtons.Left) { - WindowState = FormWindowState.Normal; - this.Visible = true; - this.Focus(); - } - } - private void LogInfo(string format, params object[] args) { - txtLog.Text += string.Format(CultureInfo.InvariantCulture, format, args); - txtLog.SelectionStart = txtLog.Text.Length; - txtLog.ScrollToCaret(); - } - private void toolStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e) { - if (e.ClickedItem == btnExit) { - Application.Exit(); - } - if (e.ClickedItem == btnUseAsHosts && listHosts.SelectedItem != null) { - UseAsHosts((string)listHosts.SelectedItem); - } - if (e.ClickedItem == btnCopy && listHosts.SelectedItem != null) { - var formCopy = new FormCopy(string.Format("Copy {0} to which file ?", listHosts.SelectedItem)); - var result = formCopy.ShowDialog(this); - if (result == DialogResult.OK && !string.IsNullOrEmpty(formCopy.FileName)) { - m_HostsProvider.CopyHosts((string)listHosts.SelectedItem, formCopy.FileName); - LogInfo("Copied {0} to {1}\r\n", listHosts.SelectedItem, formCopy.FileName); - RefreshList(); - } - } - if (e.ClickedItem == btnDelete && listHosts.SelectedItem != null) { - if (MessageBox.Show(string.Format("Really delete {0} ?", listHosts.SelectedItem), string.Empty, MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes) { - m_HostsProvider.DeleteHosts((string)listHosts.SelectedItem); - LogInfo("Deleted {0}\r\n", listHosts.SelectedItem); - RefreshList(); - } - } - if (e.ClickedItem == btnViewEdit && listHosts.SelectedItem != null) { - m_HostsProvider.LaunchEditor((string)listHosts.SelectedItem); - } - if (e.ClickedItem == btnOpenFolder) { - m_HostsProvider.OpenFolder(); - } - } - private void UseAsHosts(string selectedItem) { - m_HostsProvider.ReplaceHosts(selectedItem); - lblHosts.Text = selectedItem; - LogInfo("Copied {0} to hosts\r\n", selectedItem); - } + isWWW = new CheckBox(); + isWWW.Name = "isWWW"; + isWWW.Visible = false; + isWWW.Location = new System.Drawing.Point(32, 152); + isWWW.Size = new System.Drawing.Size(20, 20); + isWWW.CheckedChanged += new EventHandler(control_CheckedChanged); - private void menuStripExit_Click(object sender, EventArgs e) { - Application.Exit(); - } - private void menuStripShow_Click(object sender, EventArgs e) { - WindowState = FormWindowState.Normal; - this.Visible = true; - this.Focus(); - } + textBoxRedir = new TextBox(); + textBoxRedir.Location = new Point(-100, -100); + + textBoxAddress = new TextBox(); + textBoxAddress.Location = new Point(-100, -100); + + //this.Controls.Add(this.numericTrial); + this.Controls.Add(this.isLocalHost); + this.Controls.Add(this.isWWW); + this.Controls.Add(this.textBoxRedir); + this.Controls.Add(this.textBoxAddress); + //this.Controls.Add(this.comboBoxMazeTo); + + //arrowBox = new Button(); + //arrowBox.BackgroundImage = Properties.Resources.LoadToIcon; + //arrowBox.Enabled = false; + + + + + + Editors = new Control[] {null, + isLocalHost, // for column 3 + isWWW, //for Column 4 + textBoxRedir, // for column 5 + textBoxAddress, // for column 6 + }; + + //ImageList listViewBG = new ImageList(); + //listViewBG.Images.Add(Properties.Resources.LoadToIcon); + + listView1.Items.Clear(); + //listView1.LargeImageList = listViewBG; + //listView1.SmallImageList = listViewBG; + + listView1.Columns.Add("Enabled"); + listView1.Columns.Add("Local"); + listView1.Columns.Add("www"); + listView1.Columns.Add("Redirected"); + listView1.Columns.Add("Address"); + //listView1.Columns.Add(""); + listView1.Columns[0].Width = 60; + listView1.Columns[1].Width = 55; + listView1.Columns[2].Width = 45; + listView1.Columns[3].Width = 125; + //listView1.Columns[3].ImageIndex = 0; + + listView1.Columns[4].Width = 200; + + //listView1.Columns[6].Width = 5; + listView1.View = View.Details; + listLocked = false; + } + + private void listView_CheckedChanged(object sender, System.EventArgs e) + { + if (!listLocked) + { + + listView1.EndEditing(true); + listView1_updateItems(); + } + + } + + private void control_CheckedChanged(object sender, System.EventArgs e) + { + CheckBox checkControl = (CheckBox)sender; + if (!listLocked) + { + checkControl.Text = checkControl.Checked.ToString(); + listView1.EndEditing(true); + listView1_updateItems(); + } + + } + + + + + private string cleanURL(string inputUrl) + { + string cleaned = inputUrl.Replace(" ", ""); + return cleaned; + } + + private void listView1_updateItems() + { + listLocked = true; + int i = 0; + //HostsProfile hp = getProfileByIndex(listBox_hosts.SelectedIndex); + + foreach (ListViewItem lvItem in listView1.Items) + { + if (lvItem != null) + { + Domain d = new Domain(cleanURL(lvItem.SubItems[3].Text), cleanURL(lvItem.SubItems[4].Text)); + d.isEnabled = lvItem.Checked; + d.useLocalhost = bool.Parse(lvItem.SubItems[1].Text); + d.alsoWWW = bool.Parse(lvItem.SubItems[2].Text); + selectedProfile.domains[i] = d; + + + if (lvItem.Checked) + { + lvItem.BackColor = Color.PaleGreen; + lvItem.ForeColor = Color.DarkGreen; + } + else + { + lvItem.BackColor = Color.White; + lvItem.ForeColor = Color.DarkGray; + } + } + + i++; + } + profiles[listBox_hosts.SelectedIndex] = selectedProfile; + listLocked = false; + } + + private void listView1_SelectedIndexChanged(object sender, EventArgs e) + { + listView1.EndEditing(true); + listView1_updateItems(); + } + + + + private void listView1_SubItemClicked(object sender, ListViewEx.SubItemEventArgs e) + { + + listView1.StartEditing(Editors[e.SubItem], e.Item, e.SubItem); + } + + private void listView1_SubItemEndEditing(object sender, ListViewEx.SubItemEndEditingEventArgs e) + { + listView1_updateItems(); + } + + private void updateListBox() + { + listBox_hosts.Items.Clear(); + quickSwitchToolStripMenuItem.DropDownItems.Clear(); + quickSwitchToolStripMenuItem.CheckOnClick = true; + + int i = 0; + foreach (HostsProfile hp in profiles) + { + if (hp.isCurrent) + { + listBox_hosts.Items.Add(hp.profileName + " (Current)"); + quickSwitchToolStripMenuItem.DropDownItems.Add(hp.profileName); + quickSwitchToolStripMenuItem.DropDownItems[i].Enabled = false; + listBox_hosts.SelectedIndex = i; + quickSwitchToolStripMenuItem.DropDownItems[i].Name = i.ToString(); + } + else + { + listBox_hosts.Items.Add(hp.profileName); + quickSwitchToolStripMenuItem.DropDownItems.Add(hp.profileName); + quickSwitchToolStripMenuItem.DropDownItems[i].Enabled = true; + quickSwitchToolStripMenuItem.DropDownItems[i].Name = i.ToString(); + } + i++; + } + + + } + + + private void quickSwitchToolStripMenuItem_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e) + { + var clicked = e.ClickedItem.Text; + if (!string.IsNullOrEmpty(clicked)) + { + updateListBox(); + setCurrentByIndex(Int32.Parse(e.ClickedItem.Name)); + WriteHosts(); + } + } + + private void FormMain_FormClosing(object sender, FormClosingEventArgs e) + { + if (e.CloseReason == CloseReason.UserClosing) + { + e.Cancel = true; + this.Visible = false; + } + } + + private void notifyIcon_MouseClick(object sender, MouseEventArgs e) + { + if (e.Button == MouseButtons.Left) + { + WindowState = FormWindowState.Normal; + this.Visible = true; + this.Focus(); + } + } + + private void LogInfo(string format, params object[] args) + { + txtLog.Text += string.Format(CultureInfo.InvariantCulture, format, args); + txtLog.SelectionStart = txtLog.Text.Length; + txtLog.ScrollToCaret(); + } + - private void listHosts_DoubleClick(object sender, EventArgs e) { - if (listHosts.SelectedItem != null) { - m_HostsProvider.ReplaceHosts((string)listHosts.SelectedItem); - lblHosts.Text = (string)listHosts.SelectedItem; - LogInfo("Copied {0} to hosts\r\n", listHosts.SelectedItem); - } + private void WriteHosts() + { + m_HostsProvider.WriteHosts(currentHosts.toHosts()); + //lblHosts.Text = selectedItem; + LogInfo("Written {0} to hosts\r\n", currentHosts.profileName); + } + + private void menuStripExit_Click(object sender, EventArgs e) + { + Application.Exit(); + } + + private void menuStripShow_Click(object sender, EventArgs e) + { + WindowState = FormWindowState.Normal; + this.Visible = true; + this.Focus(); + } + + //private void listHosts_DoubleClick(object sender, EventArgs e) { + // if (listHosts.SelectedItem != null) { + // m_HostsProvider.ReplaceHosts((string)listHosts.SelectedItem); + // lblHosts.Text = (string)listHosts.SelectedItem; + // LogInfo("Copied {0} to hosts\r\n", listHosts.SelectedItem); + // } + //} + + + + private void button_use_as_hosts_Click(object sender, EventArgs e) + { + setCurrentByIndex(listBox_hosts.SelectedIndex); + HostsProfile.writeToSettingsXML(profiles); + WriteHosts(); + } + + private HostsProfile getProfileByIndex(int index) + { + int i = 0; + foreach (HostsProfile hp in profiles) + { + if (i == index) + { + return hp; + } + i++; + } + return null; + + } + + private void button_copy_Click(object sender, EventArgs e) + { + HostsProfile hp = getProfileByIndex(listBox_hosts.SelectedIndex); + var formCopy = new FormCopy(string.Format("Copy {0} to which file ?", hp.profileName),hp.profileName+"_Copy"); + var result = formCopy.ShowDialog(this); + if (result == DialogResult.OK && !string.IsNullOrEmpty(formCopy.FileName)) + { + if (checkForDuplicates(formCopy.FileName)) + { + MessageBox.Show("Error: '" + formCopy.FileName + "' is already an existing profile name"); + } + else + { + + HostsProfile newHp = new HostsProfile(); + + foreach (Domain d in hp.domains) + { + newHp.domains.Add(d); + } + + newHp.profileName = formCopy.FileName; + newHp.isCurrent = false; + profiles.Add(newHp); + updateListBox(); + //m_HostsProvider.CopyHosts((string)listHosts.SelectedItem, formCopy.FileName); + //LogInfo("Copied {0} to {1}\r\n", listHosts.SelectedItem, formCopy.FileName); + //RefreshList(); + } + } + } + + private void button_delete_Click(object sender, EventArgs e) + { + HostsProfile hp = getProfileByIndex(listBox_hosts.SelectedIndex); + //if (e.ClickedItem == btnDelete && listHosts.SelectedItem != null) + //{ + if (hp.profileName != "Default" && MessageBox.Show(string.Format("Really delete {0} ?", hp.profileName), string.Empty, MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes) + { + if (hp.isCurrent) + setCurrentByIndex(0); + profiles.RemoveAt(listBox_hosts.SelectedIndex); + listBox_hosts.SelectedIndex = 0; + + //m_HostsProvider.DeleteHosts((string)listHosts.SelectedItem); + //LogInfo("Deleted {0}\r\n", listHosts.SelectedItem); + //RefreshList(); + updateListBox(); + } + //} + } + + + private void exitToolStripMenuItem_Click(object sender, EventArgs e) + { + Application.Exit(); + } + + + private void button_new_Click(object sender, EventArgs e) + { + var formCopy = new FormCopy("Enter new profile name"); + var result = formCopy.ShowDialog(this); + if (result == DialogResult.OK && !string.IsNullOrEmpty(formCopy.FileName)) + { + if(checkForDuplicates(formCopy.FileName)) + { + MessageBox.Show("Error: '"+formCopy.FileName+"' is already an existing profile name"); + } + else + { + HostsProfile newHp = new HostsProfile(); + newHp.profileName = formCopy.FileName; + newHp.isCurrent = false; + profiles.Add(newHp); + //m_HostsProvider.CopyHosts((string)listHosts.SelectedItem, formCopy.FileName); + //LogInfo("Copied {0} to {1}\r\n", listHosts.SelectedItem, formCopy.FileName); + updateListBox(); + } + + } + } + + private bool checkForDuplicates(string namecheck) + { + int i = 0; + foreach (HostsProfile hp in profiles) + { + if (hp.profileName==namecheck) + { + return true; + } + + i++; + } + + return false; + } + + private void copyToolStripMenuItem_Click(object sender, EventArgs e) + { + button_copy_Click(null, e); + } + + private void deleteToolStripMenuItem_Click(object sender, EventArgs e) + { + button_delete_Click(null, e); + } + + private void saveToolStripMenuItem_Click(object sender, EventArgs e) + { + HostsProfile.writeToSettingsXML(profiles); + + } + + private void newToolStripMenuItem_Click(object sender, EventArgs e) + { + button_new_Click(sender, e); + + } + + private void setCurrentByIndex(int curID) + { + int i = 0; + foreach (HostsProfile hp in profiles) + { + if (i == curID) + { + hp.isCurrent = true; + currentHosts = hp; + } + else + hp.isCurrent = false; + i++; + } + + updateListBox(); + } + + + + private void listBox_hosts_SelectedIndexChanged(object sender, EventArgs e) + { + selectedProfile = getProfileByIndex(listBox_hosts.SelectedIndex); + customizeHosts(selectedProfile); + } + + private void button_add_domain_Click(object sender, EventArgs e) + { + //HostsProfile hp = getProfileByIndex(listBox_hosts.SelectedIndex); + if (selectedProfile.profileName != "Default") + { + selectedProfile.domains.Add(new Domain("localhost", "theaddress.com")); + customizeHosts(selectedProfile); + } + } + + private void button_save_Click(object sender, EventArgs e) + { + HostsProfile.writeToSettingsXML(profiles); + } + + private void button_removeDomain_Click(object sender, EventArgs e) + { + //HostsProfile hp = getProfileByIndex(listBox_hosts.SelectedIndex); + if (selectedProfile.profileName != "Default") + { + int len = selectedProfile.domains.Count; + + for (int i = listView1.SelectedIndices.Count - 1; i >= 0; i--) + { + selectedProfile.domains.RemoveAt(listView1.SelectedIndices[i]); + } + customizeHosts(selectedProfile); + } + } + + private void addToolStripMenuItem_Click(object sender, EventArgs e) + { + button_add_domain_Click(sender, e); + } + + private void removeToolStripMenuItem_Click(object sender, EventArgs e) + { + button_removeDomain_Click(sender, e); + } + + private void viewCurrentToolStripMenuItem_Click(object sender, EventArgs e) + { + m_HostsProvider.LaunchEditor((string)"hosts"); + } + + private void openFolderToolStripMenuItem_Click(object sender, EventArgs e) + { + m_HostsProvider.OpenFolder(); + } + + + + private void exportToolStripMenuItem_Click(object sender, EventArgs e) + { + SaveFileDialog a = new SaveFileDialog(); + a.Filter = "Exported Hosts Profiles (*.xml)|*.*"; + a.FilterIndex = 1; + a.DefaultExt = ".xml"; + a.RestoreDirectory = true; + + + if (a.ShowDialog() == DialogResult.OK) + { + HostsProfile.writeToSettingsXML(profiles, a.FileName); + } + } + + private void importToolStripMenuItem_Click(object sender, EventArgs e) + { + OpenFileDialog a = new OpenFileDialog(); + a.Filter = "Exported Hosts Profiles (*.xml)|*.*"; + a.FilterIndex = 1; + a.RestoreDirectory = true; + if (a.ShowDialog() == DialogResult.OK) + { + profiles = HostsProfile.readFromXML(a.FileName); + currentHosts = HostsProfile.getCurrentProfile(profiles); + + selectedProfile = currentHosts; + customizeHosts(selectedProfile); + updateListBox(); + } + } + + private void button_add_multiple_Click(object sender, EventArgs e) + { + TextBox inputHosts = new TextBox(); + inputHosts.Dock = DockStyle.Fill; + inputHosts.BorderStyle = BorderStyle.None; + inputHosts.Multiline = true; + //inputHosts.Size.Y = 600; + + Button okButt = new Button(); + okButt.Text = "Import"; + okButt.DialogResult = DialogResult.OK; + okButt.Dock = DockStyle.Bottom; + + Button canButt = new Button(); + canButt.Text = "Cancel"; + canButt.DialogResult = DialogResult.Cancel; + canButt.Dock = DockStyle.Bottom; + + Form InputMessageBox = new Form(); + InputMessageBox.Text = "Import Hosts"; + InputMessageBox.StartPosition = FormStartPosition.CenterScreen; + + InputMessageBox.Controls.Add(inputHosts); + InputMessageBox.Controls.Add(okButt); + InputMessageBox.Controls.Add(canButt); + if (InputMessageBox.ShowDialog() == DialogResult.OK) + { + importHOSTS(inputHosts.Text); + } + + } + + private void importHOSTS(string text) + { + if (selectedProfile.profileName != "Default") + { + using (StringReader reader = new StringReader(text)) + { + string line; + while ((line = reader.ReadLine()) != null) + { + + String[] preCommentParts = line.Split('#'); + line = preCommentParts[0]; + line.Replace(" ", "\t"); + String[] parts = line.Split('\t'); + if(parts.Length>1&&parts[0].Length>1&&parts[1].Length>1) + selectedProfile.domains.Add(new Domain(parts[0], parts[1]));// Do something with the line + else if(parts.Length>0&&parts[0].Length>1) + selectedProfile.domains.Add(new Domain("localhost", parts[0]));// Do something with the line + } + customizeHosts(selectedProfile); + } + // + } + } + + private void listBox_hosts_MouseDoubleClick(object sender, MouseEventArgs e) + { + setCurrentByIndex(listBox_hosts.SelectedIndex); + HostsProfile.writeToSettingsXML(profiles); + WriteHosts(); + } } - } } diff --git a/FormMain.resx b/FormMain.resx index 4d95e9a..fd6b1a1 100644 --- a/FormMain.resx +++ b/FormMain.resx @@ -112,111 +112,18 @@ 2.0 - System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - 17, 17 - - - - - iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8 - YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAIISURBVDhPpZP7S1NxGMbPPxKaXVUkMEq8IpKUCoY/hGgI - ymqkDYYXcCjDZOANURSjCNGFQUTsl4GXVMxKk62YU4fXQpaIlygHQxBRH8/zwvyaIAYe+HLgnPN8nue9 - HA3nvDTq63oW/jm13XOwvPTB3DYFY5MH+bXfcN8ygfTSMSSXfESicQDxBqdYHwH29g9w2tnZ3UcguIvN - rR3417exuBJE5N1n/wfwLgX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6bkliE/jKuYxrd6oVgDWfjB+K - TWeKMyrGEVfowITvD9re/9ABVQrAhh0HHK+ZselMMaN/mvwtDb+aVqkA7HYIwIj3ysfluPTorJnP6Ezx - oHsD1s5ZXEktUwCOioB5f1CEPR9+wTG6iuiserTo8dkwng7HT/R+XUPF8xlcTjErAOdMcW6NW8STiwG8 - 7vej8oUPN/PsEv3t8Ao0TZP3T1u8uJRkUgAuSYHtO97oLxmXd5t9Ho8aPTK+GzntqNfrLm2fFoihwYOI - xGIF4KjoGBLzY1OrF9k6OOFxnwDC4wxIMX1G0pMhgVyMNyoA13PAtS7OrJk1PrC69LUdQWxuF6IybHrX - LRI7JrtZdoDAo1XmbjMyD+tjSXxGcXRmnYg5ttD9QuxDhN0uUgDOmbvNTpPOJaGAo2K36cyaGZvOFIfd - KlSA8/zRh9ABIDUG+1JpAAAAAElFTkSuQmCC - - - + 115, 17 - + 221, 17 + AAABAAEAAAAAAAEAIAAoIAQAFgAAACgAAAAAAQAAAAIAAAEAIAAAAAAAACAEAAAAAAAAAAAAAAAAAAAA @@ -4728,6 +4635,12 @@ //////////////////////////////////////////////////8= + + 584, 17 + + + 367, 17 + AAABAAEAAAAAAAEAIAAoIAQAFgAAACgAAAAAAQAAAAIAAAEAIAAAAAAAACAEAAAAAAAAAAAAAAAAAAAA diff --git a/HostsProfile.cs b/HostsProfile.cs new file mode 100644 index 0000000..eae7d65 --- /dev/null +++ b/HostsProfile.cs @@ -0,0 +1,246 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Xml; +using System.IO; + +namespace Barbar.HostsSwitcher +{ + public struct Domain + { + public bool isEnabled; + public bool alsoWWW; + public bool useLocalhost; + public Domain(string redirectedAddress, string domainToRedirect) + { + this.domainToRedirect = domainToRedirect; + this.redirectedAddress = redirectedAddress; + this.isEnabled = false; + this.alsoWWW = true; + this.useLocalhost = true; + } + public string domainToRedirect; + public string redirectedAddress; + + } + public class HostsProfile + { + private static string header = "# Copyright (c) 1993-2009 Microsoft Corp.\n#\n#\n# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.\n#\n# This file contains the mappings of IP addresses to host names. Each\n# entry should be kept on an individual line. The IP address should\n# be placed in the first column followed by the corresponding host name.\n# The IP address and the host name should be separated by at least one\n# space.\n#\n# Additionally, comments (such as these) may be inserted on individual\n# lines or following the machine name denoted by a '#' symbol.\n#\n# For example:\n#\n# 102.54.94.97 rhino.acme.com # source server\n# 38.25.63.10 x.acme.com # x client host\n\n# localhost name resolution is handled within DNS itself.\n# 127.0.0.1 localhost\n# ::1 localhost\n"; + public string profileName; + public bool isCurrent = false; + public List domains=new List(); + + + + public HostsProfile() + { + this.profileName = "Default"; + this.domains = new List(); + isCurrent = true; + } + public HostsProfile(string name) + { + if (name != "Default") + this.profileName = name; + else + this.profileName = "DefaultClone"; + this.domains = new List(); + } + + public HostsProfile(XmlElement profileXML) + { + this.profileName = getStringFromAttribute(profileXML, "ProfileName"); + this.isCurrent= getBoolFromAttribute(profileXML, "isCurrent"); + this.domains = new List(); + foreach (XmlNode node in profileXML.ChildNodes) + { + + switch (node.Name) + { + case "Domain": + string url= getStringFromAttribute(node, "URL"); + string redir = getStringFromAttribute(node, "Redirect"); + Domain d = new Domain(redir, url); + d.isEnabled = getBoolFromAttribute(node, "Enabled"); + domains.Add(d); + break; + } + + } + } + + public XmlElement toXML(XmlDocument doc) + { + XmlElement hostsProfile = doc.CreateElement(string.Empty, "HostsProfile", string.Empty); + hostsProfile.SetAttribute("ProfileName", this.profileName); + hostsProfile.SetAttribute("isCurrent", this.isCurrent.ToString()); + for (int i=0;i myprofiles,string filepath) + { + + + XmlDocument doc = new XmlDocument(); + + XmlDeclaration xmlDeclaration = doc.CreateXmlDeclaration("1.0", "UTF-8", null); + XmlElement root = doc.DocumentElement; + doc.InsertBefore(xmlDeclaration, root); + + XmlElement profilesXMLnode = doc.CreateElement(string.Empty, "HostsProfiles", string.Empty); + + bool hasCurrent = false; + + foreach (HostsProfile hp in myprofiles) + { + if (hasCurrent) + hp.isCurrent = false; + + profilesXMLnode.AppendChild(hp.toXML(doc)); + + if (hp.isCurrent) + hasCurrent = true; + } + + doc.AppendChild(profilesXMLnode); + doc.Save(filepath); + + return true; + } + + public static bool writeToSettingsXML(List myprofiles) + { + // Writes to default path + + return writeToSettingsXML(myprofiles, getSettingsFilepath()); + } + + public static HostsProfile getCurrentProfile(List myprofiles) + { + foreach (HostsProfile hp in myprofiles) + { + if(hp.isCurrent) + { + return hp; + } + } + + return new HostsProfile(); + } + + public static List readFromXML(string file) + { + + List myProfiles = new List(); + bool failed = false; + if (File.Exists(getSettingsFilepath())) + { + XmlDocument doc = new XmlDocument(); + doc.Load(getSettingsFilepath()); + + XmlNode root = doc.DocumentElement.FirstChild; + + if (root == null) //XML file doestn' contain maze + { + failed = true; + } + else + { + + XmlNode fileVersionNode = root.Attributes["version"]; + string fileVersion = "unknown"; + if (fileVersionNode != null) + fileVersion = fileVersionNode.InnerText; + + foreach (XmlNode node in doc.DocumentElement.ChildNodes) + { + switch(node.Name) + { + case "HostsProfile": + HostsProfile profile = new HostsProfile((XmlElement)node); + myProfiles.Add(profile); + break; + } + } + } + + } + else + failed = true; + + if(failed) + { + HostsProfile currentHost = new HostsProfile(); + currentHost.isCurrent = true; + myProfiles.Add(currentHost); + } + + return myProfiles; + } + } + +} diff --git a/HostsSwitcher.csproj b/HostsSwitcher.csproj index d0120ad..e8fd1c3 100644 --- a/HostsSwitcher.csproj +++ b/HostsSwitcher.csproj @@ -8,10 +8,11 @@ {36F0F014-2BE4-45F1-807A-4D5BCDA64CA4} WinExe Properties - Barbar.HostsSwitcher + Barber.HostsSwitcher HostsSwitcher - v2.0 + v4.0 512 + x86 @@ -32,9 +33,18 @@ prompt 4 + + + network.ico + + + app.manifest + + Barbar.HostsSwitcher.Program + @@ -56,6 +66,8 @@ FormMain.cs + + @@ -76,6 +88,7 @@ Resources.resx True + SettingsSingleFileGenerator @@ -89,6 +102,7 @@ + + @@ -68,9 +69,10 @@ - + + @@ -85,9 +87,10 @@ - + + @@ -109,9 +112,13 @@ 2.0 - System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + ..\Resources\seo-and-web-glyph-25-01-512.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a + \ No newline at end of file diff --git a/Properties/Settings.Designer.cs b/Properties/Settings.Designer.cs index f0b8e9b..a810ec9 100644 --- a/Properties/Settings.Designer.cs +++ b/Properties/Settings.Designer.cs @@ -1,18 +1,18 @@ //------------------------------------------------------------------------------ // // This code was generated by a tool. -// Runtime Version:4.0.30319.296 +// Runtime Version:4.0.30319.42000 // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated. // //------------------------------------------------------------------------------ -namespace Barbar.HostsSwitcher.Properties { +namespace Curtin.HostsSwitcher.Properties { [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] - [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "10.0.0.0")] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "16.7.0.0")] internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase { private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); diff --git a/Provider/HostProvider.cs b/Provider/HostProvider.cs index f9c0d38..f602676 100644 --- a/Provider/HostProvider.cs +++ b/Provider/HostProvider.cs @@ -37,8 +37,16 @@ public void ReplaceHosts(string fileName) { public void CopyHosts(string sourceFileName, string targetFileName) { File.Copy(Path.Combine(m_Directory, sourceFileName), Path.Combine(m_Directory, targetFileName), true); } - - public void DeleteHosts(string fileName) { + + public void WriteHosts(string hostsText) + { + string filepath = Path.Combine(m_Directory, "hosts"); + //System.IO.StreamWriter fp = new System.IO.StreamWriter(filepath, true); + File.WriteAllText(filepath,hostsText); + //fp.Close(); + } + + public void DeleteHosts(string fileName) { File.Delete(Path.Combine(m_Directory, fileName)); } diff --git a/Provider/IHostProvider.cs b/Provider/IHostProvider.cs index d4f19ba..5b01d83 100644 --- a/Provider/IHostProvider.cs +++ b/Provider/IHostProvider.cs @@ -3,7 +3,8 @@ namespace Barbar.HostsSwitcher.Provider { public interface IHostProvider { string[] GetHostFiles(); void ReplaceHosts(string fileName); - void CopyHosts(string sourceFileName, string targetFileName); + void WriteHosts(string hostsText); + void CopyHosts(string sourceFileName, string targetFileName); void DeleteHosts(string fileName); void LaunchEditor(string fileName); void OpenFolder(); diff --git a/README.md b/README.md index abb270d..ae5a6ef 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,25 @@ This small tray icon utility takes care about your host files, so developer can Web developers in big companies (such as myself) often struggles with different environments - local, QA, production, performance test environment, etc. To make things more difficult, we're usually taking care about multiple web sites. This simple tool helps you out to change hosts quickly - by overwriting your C:\Windows\System32\drivers\etc\hosts file (which is serving as first level dns proxy) by set of predefined hosts. +This version operates by creating and saving different host profiles which can be rapidly written to the windows hosts file. This prevents having multiple hosts files in the directory at a time and allows for easy manipulation of which sites are blocked (or rerouted). The XML document specifies a few parameters for each address: + +Enabled: Identifies if the line is to be used at all + +Local: Will use localhost instead of any specified Redirection + +Www: Will add both the original domain (originaldomain.com) and with www. appended (www.originaldomain.com) so that the redirect will apply to both entered addresses. + +Redirected: the IP address that the domain will be directed to + +Address: The domain that will be redirected + + +This xml file is saved to your application data folder and can be Exported or Imported using the Export/Import profiles under the File menu. + Please note web browsers have dns cache on their own so once you change hosts; you have to either close and reopen your browser once you do the change or use some plugin - such as DNS flusher in Mozilla. ![Hosts Switcher Screenshot](/hosts-switcher.png) + +Using the Import Hosts tool (+++ icon in lower right) You can copy and paste in domains to be added, or whole hosts files for rapid entry. The Import tool also will accept lists of names separated by linebreaks (ex: google.com) or space/tab separated addressess and domain pairs (ex: 127.0.0.1 google.com). Comments denoted by # and following text will be ignored. If only a domain name is included in the import, the link will be imported into the current profile with a localhost redirect by default. + +![Hosts Import Tool](/ImportHosts-Example.png) diff --git a/Resources/seo-and-web-glyph-25-01-512.png b/Resources/seo-and-web-glyph-25-01-512.png new file mode 100644 index 0000000..2bc18eb Binary files /dev/null and b/Resources/seo-and-web-glyph-25-01-512.png differ diff --git a/app.config b/app.config new file mode 100644 index 0000000..fcd0c93 --- /dev/null +++ b/app.config @@ -0,0 +1,3 @@ + + + diff --git a/hosts-switcher.png b/hosts-switcher.png index ff57d91..1cb6db5 100644 Binary files a/hosts-switcher.png and b/hosts-switcher.png differ