Skip to content

Commit

Permalink
implement window events
Browse files Browse the repository at this point in the history
  • Loading branch information
clowwindy committed Jan 14, 2013
1 parent 4b491f6 commit e01a50c
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 17 deletions.
23 changes: 21 additions & 2 deletions shadowsocks-csharp/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Runtime.Serialization.Json;
using System.Text;
using System.IO;
using System.Diagnostics;

namespace shadowsocks_csharp
{
Expand All @@ -14,6 +15,17 @@ public class Config
public int local_port;
public string password;

[NonSerialized]
public bool isDefault;

private static void assert(bool condition)
{
if(!condition)
{
throw new Exception("assertion failure");
}
}

public static Config Load()
{
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Config));
Expand All @@ -22,17 +34,24 @@ public static Config Load()
using (FileStream fs = File.OpenRead(@"config.json"))
{
Config config = ser.ReadObject(fs) as Config;
assert(!string.IsNullOrEmpty(config.server));
assert(!string.IsNullOrEmpty(config.password));
assert(config.local_port > 0);
assert(config.server_port > 0);
config.isDefault = false;
return config;
}
}
catch (IOException)
catch (Exception e)
{
Console.WriteLine(e);
return new Config
{
server = "127.0.0.1",
server_port = 8388,
local_port = 1081,
password = "barfoo!"
password = "barfoo!",
isDefault = true
};
}
}
Expand Down
34 changes: 27 additions & 7 deletions shadowsocks-csharp/Form1.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

70 changes: 63 additions & 7 deletions shadowsocks-csharp/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace shadowsocks_csharp
{
Expand All @@ -16,14 +17,36 @@ public partial class Form1 : Form
public Form1()
{
config = Config.Load();
reload(config);
InitializeComponent();
configToTextBox();
}

private void showWindow()
{
this.Opacity = 1;
this.Show();
}

private void configToTextBox()
{
textBox1.Text = config.server;
textBox2.Text = config.server_port.ToString();
textBox3.Text = config.password;
textBox4.Text = config.local_port.ToString();
}

private void Form1_Load(object sender, EventArgs e)
{
if (!config.isDefault)
{
this.Opacity = 0;
reload(config); BeginInvoke(new MethodInvoker(delegate
{
this.Hide();
}));
}
}

private void reload(Config config)
{
if (local != null)
Expand All @@ -37,28 +60,61 @@ private void reload(Config config)

private void Config_Click(object sender, EventArgs e)
{

showWindow();
}

private void Quit_Click(object sender, EventArgs e)
{

this.Close();
}

private void button1_Click(object sender, EventArgs e)
private void OKButton_Click(object sender, EventArgs e)
{
reload(Config.Load());
try
{
Config config = new Config
{
server = textBox1.Text,
server_port = int.Parse(textBox2.Text),
password = textBox3.Text,
local_port = int.Parse(textBox4.Text),
isDefault = false
};
Config.Save(config);
this.config = config;
reload(config);
this.Hide();
}
catch (FormatException)
{
MessageBox.Show("there is format problem");
}
catch (Exception)
{
MessageBox.Show("there is some problem with parameters");
}
}

private void button2_Click(object sender, EventArgs e)
private void cancelButton_Click(object sender, EventArgs e)
{

this.Hide();
configToTextBox();
}

private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
local.Stop();
}

private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
Process.Start("https://github.com/clowwindy/shadowsocks-csharp");
}

private void notifyIcon1_DoubleClick(object sender, EventArgs e)
{
showWindow();
}

}
}
2 changes: 1 addition & 1 deletion shadowsocks-csharp/shadowsocks-csharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<ProductVersion>9.0.21022</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{8C02D2F7-7CDB-4D55-9F25-CD03EF4AA062}</ProjectGuid>
<OutputType>Exe</OutputType>
<OutputType>WinExe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>shadowsocks_csharp</RootNamespace>
<AssemblyName>shadowsocks-csharp</AssemblyName>
Expand Down

0 comments on commit e01a50c

Please sign in to comment.