Files
shadowsocks-windows/shadowsocks-csharp/Model/Configuration.cs
T
Syrone Wong 99bdec334c Add global hotkey support (#724)
Design:

Use the plain string for both config and hotkey parsing
and make use of KeyConverter, ModifierKeysConverter
to make it more rubust.

Find corresponding label (to display operation result) and callback (to do the real work) based on textbox name. In this case, they should be named as `<Feature-Name><Control-Name>`.

ShowLogsTextBox,
ShowLogsLabel,
ShowLogsCallback.

If this requirement doesn't meet, throw an exception at runtime.

Logic:

How to input keys:
1. put focus in the corresponding textbox
2. press the key combination you want to use
3. when you think it is ready, release all keys
4. the textbox shows your input

How to change keys:
1. put focus in the corresponding textbox
2. press BackSpace to clear content
3. re-input new one

How to deactivate:
1. clear content in the textbox
2. press OK button

Meaning of label color:
- Green: this combination is not occupied by other programs and register successfully
- Yellow: this combination is occupied by other programs and you have to change to another one
- Transparent without color: initial status

Signed-off-by: Syrone Wong <wong.syrone@gmail.com>
2016-09-16 23:42:16 -05:00

147 lines
4.7 KiB
C#
Executable File

using System;
using System.Collections.Generic;
using System.IO;
using Shadowsocks.Controller;
using Newtonsoft.Json;
namespace Shadowsocks.Model
{
[Serializable]
public class Configuration
{
public List<Server> configs;
// when strategy is set, index is ignored
public string strategy;
public int index;
public bool global;
public bool enabled;
public bool shareOverLan;
public bool isDefault;
public int localPort;
public string pacUrl;
public bool useOnlinePac;
public bool availabilityStatistics;
public bool autoCheckUpdate;
public bool isVerboseLogging;
public LogViewerConfig logViewer;
public ProxyConfig proxy;
public HotkeyConfig hotkey;
private static string CONFIG_FILE = "gui-config.json";
public Server GetCurrentServer()
{
if (index >= 0 && index < configs.Count)
return configs[index];
else
return GetDefaultServer();
}
public static void CheckServer(Server server)
{
CheckPort(server.server_port);
CheckPassword(server.password);
CheckServer(server.server);
}
public static Configuration Load()
{
try
{
string configContent = File.ReadAllText(CONFIG_FILE);
Configuration config = JsonConvert.DeserializeObject<Configuration>(configContent);
config.isDefault = false;
if (config.localPort == 0)
config.localPort = 1080;
if (config.index == -1 && config.strategy == null)
config.index = 0;
if (config.logViewer == null)
config.logViewer = new LogViewerConfig();
if (config.proxy == null)
config.proxy = new ProxyConfig();
if (config.hotkey == null)
config.hotkey = new HotkeyConfig();
return config;
}
catch (Exception e)
{
if (!(e is FileNotFoundException))
Logging.LogUsefulException(e);
return new Configuration
{
index = 0,
isDefault = true,
localPort = 1080,
autoCheckUpdate = true,
configs = new List<Server>()
{
GetDefaultServer()
}
};
}
}
public static void Save(Configuration config)
{
if (config.index >= config.configs.Count)
config.index = config.configs.Count - 1;
if (config.index < -1)
config.index = -1;
if (config.index == -1 && config.strategy == null)
config.index = 0;
config.isDefault = false;
try
{
using (StreamWriter sw = new StreamWriter(File.Open(CONFIG_FILE, FileMode.Create)))
{
string jsonString = JsonConvert.SerializeObject(config, Formatting.Indented);
sw.Write(jsonString);
sw.Flush();
}
}
catch (IOException e)
{
Logging.LogUsefulException(e);
}
}
public static Server GetDefaultServer()
{
return new Server();
}
private static void Assert(bool condition)
{
if (!condition)
throw new Exception(I18N.GetString("assertion failure"));
}
public static void CheckPort(int port)
{
if (port <= 0 || port > 65535)
throw new ArgumentException(I18N.GetString("Port out of range"));
}
public static void CheckLocalPort(int port)
{
CheckPort(port);
if (port == 8123)
throw new ArgumentException(I18N.GetString("Port can't be 8123"));
}
private static void CheckPassword(string password)
{
if (password.IsNullOrEmpty())
throw new ArgumentException(I18N.GetString("Password can not be blank"));
}
public static void CheckServer(string server)
{
if (server.IsNullOrEmpty())
throw new ArgumentException(I18N.GetString("Server IP can not be blank"));
}
}
}