-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ginger.cs
119 lines (103 loc) · 2.75 KB
/
Ginger.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
using System;
using Xwt;
using System.Diagnostics;
using System.Collections.Generic;
using System.Threading;
using Newtonsoft.Json.Linq;
using System.IO;
using System.Reflection;
using Xwt.Drawing;
using Newtonsoft.Json;
namespace ginger
{
// プログラム全体の挙動。
// ブラウザウィンドウが 0 個になったら終了する。
public class Ginger
{
public List<Browser> Browsers = new List<Browser>();
public List<Server> Servers = new List<Server>();
public Image Icon;
// エントリーポイント
[STAThread]
static void Main()
{
InitializeApplication();
var ginger = new Ginger();
ginger.LoadSettings();
ginger.AddBrowser(new Browser(ginger));
Application.Run();
}
Ginger()
{
var image_path = Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location),
"ginger_icon.png");
Icon = Image.FromFile(image_path);
}
static void InitializeApplication()
{
Application.Initialize(ToolkitType.Gtk);
}
void AddBrowser(Browser browser)
{
Browsers.Add(browser);
browser.Closed += (sender, e) => {
Browsers.Remove(browser);
if (Browsers.Count == 0)
Application.Exit();
};
}
string SettingsDirectoryPath()
{
var appData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
return Path.Combine(appData, "ginger");
}
string SettingsFilePath()
{
return Path.Combine(SettingsDirectoryPath(), "settings.json");
}
GingerSettings Settings;
void LoadSettings()
{
Directory.CreateDirectory(SettingsDirectoryPath());
string text;
try {
text = File.ReadAllText(SettingsFilePath());
Settings = JsonConvert.DeserializeObject<GingerSettings>(text);
} catch (FileNotFoundException) {
Settings = new GingerSettings();
SaveSettings();
}
Servers = Settings.servers;
// Servers.Add(new Server("ゲストOS", "localhost", 7144));
// Servers.Add(new Server("ホストOS", "windows", 7144));
}
public void SaveSettings()
{
var text = JsonConvert.SerializeObject(Settings, Formatting.Indented);
Directory.CreateDirectory(SettingsDirectoryPath());
File.WriteAllText(SettingsFilePath(), text);
}
public void RequestExit()
{
foreach (var browser in Browsers) {
browser.Dispose();
}
Application.Exit();
}
public void OnStart()
{
LoadSettings();
}
public string VersionString {
get { return "ginger version 0.0.1-delta"; }
}
}
class GingerSettings
{
public List<Server> servers;
public GingerSettings()
{
servers = new List<Server>();
}
}
}