// Copyright (c) 2013 Runxia Electronics Co. Ltd
// Some of the code are integrated from original Virtual Router Project
/*
* Virtual Router v1.0 -
http://virtualrouter.codeplex.com* Wifi Hot Spot for Windows 7 and 2008 R2
* Copyright (c) 2011 Chris Pietschmann (
http://pietschsoft.com)
* Licensed under the Microsoft Public License (Ms-PL)
*
http://virtualrouter...codeplex.com/license*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using VirtualRouterPlus.Properties;
namespace VirtualRouterPlus
{
public partial class MainForm : Form
{
WlanManager wlanManager = new WlanManager();
IcsManager icsManager = new IcsManager();
bool isStarted;
public MainForm()
{
InitializeComponent();
}
private void MainForm_Load(object sender, EventArgs e)
{
//Added by Stephen66515
this.passwordTextBox.PasswordChar = "*"; AutoUpdater.BackgroundUpdate();
Text = Resource.ApplicationName;
ssidLabel.Text = Resource.LabelSSID;
passwordLabel.Text = Resource.LabelPassword;
connectionLabel.Text = Resource.LabelSharedConnection;
helpButton.Text = Resource.Help;
startButton.Text = Resource.StartVirtualRouterPlus;
if (Settings.Default.FirstLaunch)
{
Process.Start(Resource.FirstLaunchUrl);
Settings.Default.FirstLaunch = false;
Settings.Default.Save();
}
wlanManager.HostedNetworkAvailable += wlanManager_HostedNetworkAvailable;
wlanManager.StationJoin += wlanManager_StationJoin;
RefreshConnection();
ssidTextBox.Text = Settings.Default.SSID;
passwordTextBox.Text = Settings.Default.Password;
foreach (IcsConnection connection in connectionComboBox.Items)
{
if (connection.Name == Settings.Default.Connection)
{
connectionComboBox.SelectedItem = connection;
break;
}
}
}
void wlanManager_StationJoin(object sender, EventArgs e)
{
}
void wlanManager_HostedNetworkAvailable(object sender, EventArgs e)
{
}
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
Settings.Default.SSID = ssidTextBox.Text;
Settings.Default.Password = passwordTextBox.Text;
Settings.Default.Connection = connectionComboBox.Text;
Settings.Default.Save();
Stop();
}
private void startButton_Click(object sender, EventArgs e)
{
startButton.Enabled = false;
ssidTextBox.Enabled = false;
passwordTextBox.Enabled = false;
connectionComboBox.Enabled = false;
refreshConnectionButton.Enabled = false;
startButton.Text = Resource.Working;
if (isStarted)
{
if (Stop())
{
isStarted = false;
notifyIcon.ShowBalloonTip(5000, Resource.Success, Resource.VirtualRouterPlusStopped, ToolTipIcon.Info);
}
else
{
MessageBox.Show(Resource.VirtualRouterPlusStopError, Resource.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
else
{
if (!ValidateFields())
return;
if (Start(ssidTextBox.Text, passwordTextBox.Text, (IcsConnection)connectionComboBox.SelectedItem, 16))
{
isStarted = true;
WindowState = FormWindowState.Minimized;
notifyIcon.ShowBalloonTip(5000, Resource.Success, Resource.VirtualRouterPlusStarted, ToolTipIcon.Info);
}
else
{
MessageBox.Show(Resource.VirtualRouterPlusStartError, Resource.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
notifyIcon.Icon = isStarted ? Resources.VirtualRouterPlusStarted : Resources.VirtualRouterPlusStopped;
startButton.Text = isStarted ? Resource.StopVirtualRouterPlus : Resource.StartVirtualRouterPlus;
ssidTextBox.Enabled = !isStarted;
passwordTextBox.Enabled = !isStarted;
connectionComboBox.Enabled = !isStarted;
refreshConnectionButton.Enabled = !isStarted;
startButton.Enabled = true;
}
private bool Start(string ssid, string password, IcsConnection connection, int maxClients)
{
try
{
Stop();
wlanManager.SetConnectionSettings(ssid, 32);
wlanManager.SetSecondaryKey(password);
wlanManager.StartHostedNetwork();
var privateConnectionGuid = wlanManager.HostedNetworkInterfaceGuid;
icsManager.EnableIcs(connection.Guid, privateConnectionGuid);
return true;
}
catch
{
return false;
}
}
private bool Stop()
{
try
{
if (this.icsManager.SharingInstalled)
{
this.icsManager.DisableIcsOnAll();
}
this.wlanManager.StopHostedNetwork();
return true;
}
catch
{
return false;
}
}
private bool ValidateFields()
{
if (ssidTextBox.Text.Length <= 0)
{
errorProvider.SetError(ssidTextBox, Resource.SSIDRequiredError);
return false;
}
if (ssidTextBox.Text.Length > 32)
{
errorProvider.SetError(ssidTextBox, Resource.SSIDTooLongError);
return false;
}
if (passwordTextBox.Text.Length <
{
errorProvider.SetError(ssidTextBox, Resource.PasswordTooShortError);
return false;
}
if (passwordTextBox.Text.Length > 64)
{
errorProvider.SetError(ssidTextBox, Resource.PasswordTooLongError);
return false;
}
return true;
}
private void refreshConnectionButton_Click(object sender, EventArgs e)
{
RefreshConnection();
}
private void RefreshConnection()
{
connectionComboBox.Items.Clear();
foreach (var connection in icsManager.Connections)
{
if (connection.IsConnected && connection.IsSupported)
{
connectionComboBox.Items.Add(connection);
}
}
if (connectionComboBox.Items.Count > 0)
connectionComboBox.SelectedIndex = 0;
}
private void MainForm_SizeChanged(object sender, EventArgs e)
{
if (WindowState == FormWindowState.Minimized)
{
Hide();
}
}
private void notifyIcon_MouseClick(object sender, MouseEventArgs e)
{
Show();
WindowState = FormWindowState.Normal;
}
private void helpButton_Click(object sender, EventArgs e)
{
Process.Start(Resource.HelpUrl);
}
}
}