-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.xaml.cs
113 lines (107 loc) · 3.57 KB
/
MainWindow.xaml.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
using System.Windows;
using System.Runtime.InteropServices;
using System;
using System.Windows.Input;
using System.IO;
using System.Diagnostics;
using System.Text.Json;
using System.Windows.Controls;
namespace Chef
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public class Profile
{
public string name { get; set; }
public string iconPath { get; set; }
public string b64hosts { get; set; }
}
public class Profiles
{
public Profile[] profiles { get; set; }
}
public string toJSON(Profiles obj)
{
return JsonSerializer.Serialize(obj);
}
public Profiles? fromJSON(string json)
{
Profiles? obj = JsonSerializer.Deserialize<Profiles>(json);
return obj;
}
private void addProfile(Button btn)
{
profileList.Items.Add(btn);
}
public MainWindow()
{
InitializeComponent();
Profiles? profiles = fromJSON(getProfileConfig());
if (profiles != null)
{
foreach (Profile profile in profiles.profiles)
{
Button button = new Button();
button.Content = profile.name;
button.Name = $"profileBtn_{profile.name}";
addProfile(button);
}
}
}
private string getProfileConfig()
{
var pathName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Chef/");
var fileName = Path.Combine(pathName, "config.json");
if (File.Exists(fileName))
{
return File.ReadAllText(fileName);
} else
{
if (!Directory.Exists(pathName))
{
Directory.CreateDirectory(pathName);
}
string defaultJSON = @"{""profiles"": [{""name"": ""Default"",""iconPath"": """",""b64hosts"": """"}]}";
File.WriteAllText(fileName, defaultJSON);
return defaultJSON;
}
}
private void setProfileConfig(string content)
{
var pathName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Chef/");
var fileName = Path.Combine(pathName, "config.json");
File.WriteAllText(fileName, content);
}
private void toTaskbar(object sender, RoutedEventArgs e)
{
this.Hide();
}
private void fromTaskbar(object sender, RoutedEventArgs e)
{
this.Show();
}
private void exit(object sender, RoutedEventArgs e)
{
this.Close();
}
private void Border_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
if (e.ChangedButton == MouseButton.Left)
{
this.DragMove();
}
}
private void openProfileConfig(object sender, RoutedEventArgs e)
{
Process process = new Process();
var pathName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Chef/");
var fileName = Path.Combine(pathName, "config.json");
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = "/K start " + fileName;
process.Start();
}
}
}