1
0
mirror of https://github.com/proxysu/ProxySU.git synced 2024-11-27 07:36:06 +03:00
ProxySU/ProxySU_Core/Views/MainWindow.xaml.cs

205 lines
6.5 KiB
C#
Raw Normal View History

2021-02-25 04:59:06 +03:00
using MahApps.Metro.Controls.Dialogs;
using Newtonsoft.Json;
2021-03-06 12:26:17 +03:00
using Newtonsoft.Json.Serialization;
2021-03-17 06:58:56 +03:00
using ProxySU_Core.Common;
2021-03-04 11:25:36 +03:00
using ProxySU_Core.Models;
2021-02-25 04:59:06 +03:00
using ProxySU_Core.ViewModels;
2021-03-04 11:25:36 +03:00
using ProxySU_Core.Views;
2021-04-17 06:50:31 +03:00
using ProxySU_Core.Views.ClientInfo;
using ProxySU_Core.Views.RecordEditor;
2021-02-25 04:59:06 +03:00
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
namespace ProxySU_Core
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow
{
private const string RecordPath = @"Data\Record.json";
2021-03-04 11:25:36 +03:00
public ObservableCollection<RecordViewModel> Records { get; set; }
2021-02-25 04:59:06 +03:00
public MainWindow()
{
WindowStartupLocation = WindowStartupLocation.CenterScreen;
InitializeComponent();
2021-03-04 11:25:36 +03:00
Records = new ObservableCollection<RecordViewModel>();
2021-02-25 04:59:06 +03:00
if (File.Exists(RecordPath))
{
var recordsJson = File.ReadAllText(RecordPath, Encoding.UTF8);
2021-03-08 18:15:18 +03:00
if (!string.IsNullOrEmpty(recordsJson))
2021-03-04 11:25:36 +03:00
{
2021-03-08 18:15:18 +03:00
var records = JsonConvert.DeserializeObject<List<Record>>(recordsJson);
records.ForEach(item =>
{
Records.Add(new RecordViewModel(item));
});
}
2021-02-25 04:59:06 +03:00
}
2021-03-04 11:25:36 +03:00
2021-02-25 04:59:06 +03:00
DataContext = this;
}
2021-03-06 12:26:17 +03:00
private void SaveRecord()
{
var recordList = Records.Select(x => x.record);
var json = JsonConvert.SerializeObject(recordList,
Formatting.Indented,
new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
});
2021-03-08 18:15:18 +03:00
if (!Directory.Exists("Data"))
2021-03-06 12:26:17 +03:00
{
2021-03-08 18:15:18 +03:00
Directory.CreateDirectory("Data");
2021-03-06 12:26:17 +03:00
}
2021-03-08 18:15:18 +03:00
File.WriteAllText("Data\\Record.json", json, Encoding.UTF8);
2021-03-06 12:26:17 +03:00
}
2021-02-25 04:59:06 +03:00
private void LaunchGitHubSite(object sender, RoutedEventArgs e)
{
System.Diagnostics.Process.Start("explorer.exe", "https://github.com/proxysu/ProxySU");
}
private void LaunchCoffeeSite(object sender, RoutedEventArgs e)
{
System.Diagnostics.Process.Start("explorer.exe", "https://github.com/proxysu/ProxySU");
}
private void ChangeLanguage(object sender, SelectionChangedEventArgs e)
{
var selection = cmbLanguage.SelectedValue as ComboBoxItem;
if (selection.Name == "zh_cn")
{
ChangeLanguage("zh_cn");
}
else if (selection.Name == "en")
{
ChangeLanguage("en");
}
}
private void ChangeLanguage(string culture)
{
ResourceDictionary resource = new ResourceDictionary();
if (string.Equals(culture, "zh_cn", StringComparison.OrdinalIgnoreCase))
{
resource.Source = new Uri(@"Resources\Languages\zh_cn.xaml", UriKind.Relative);
}
else if (string.Equals(culture, "en", StringComparison.OrdinalIgnoreCase))
{
resource.Source = new Uri(@"Resources\Languages\en.xaml", UriKind.Relative);
}
Application.Current.Resources.MergedDictionaries[0] = resource;
}
2021-03-17 06:58:56 +03:00
private void ExportXraySettings(object sender, RoutedEventArgs e)
{
StringBuilder sb = new StringBuilder();
foreach (var record in Records.Where(x => x.IsChecked))
{
record.Settings.Types.ForEach(type =>
{
var link = ShareLink.Build(type, record.Settings);
sb.AppendLine(link);
});
}
var tbx = new TextBoxWindow("分享链接", sb.ToString());
tbx.ShowDialog();
}
private void ExportXraySub(object sender, RoutedEventArgs e)
{
StringBuilder sb = new StringBuilder();
foreach (var record in Records.Where(x => x.IsChecked))
{
record.Settings.Types.ForEach(type =>
{
var link = ShareLink.Build(type, record.Settings);
sb.AppendLine(link);
});
}
var result = Base64.Encode(sb.ToString());
var tbx = new TextBoxWindow("订阅内容", result);
tbx.ShowDialog();
}
2021-02-25 04:59:06 +03:00
private void AddHost(object sender, RoutedEventArgs e)
{
2021-03-04 13:25:52 +03:00
var newRecord = new Record();
var hostWindow = new RecordEditorWindow(newRecord);
var result = hostWindow.ShowDialog();
if (result == true)
{
Records.Add(new RecordViewModel(newRecord));
2021-03-06 12:26:17 +03:00
SaveRecord();
2021-03-04 13:25:52 +03:00
}
2021-02-25 04:59:06 +03:00
}
private void EditHost(object sender, RoutedEventArgs e)
{
2021-03-04 11:25:36 +03:00
if (DataGrid.SelectedItem is RecordViewModel project)
2021-02-25 04:59:06 +03:00
{
2021-03-04 11:25:36 +03:00
var hostWindow = new RecordEditorWindow(project.record);
2021-03-04 13:25:52 +03:00
var result = hostWindow.ShowDialog();
if (result == true)
{
project.Notify();
2021-03-06 12:26:17 +03:00
SaveRecord();
2021-03-04 13:25:52 +03:00
}
2021-02-25 04:59:06 +03:00
}
}
2021-03-06 12:26:17 +03:00
private void ShowClientInfo(object sender, RoutedEventArgs e)
{
if (DataGrid.SelectedItem is RecordViewModel project)
{
var dialog = new ClientInfoWindow(project.record);
dialog.ShowDialog();
}
}
2021-03-04 11:25:36 +03:00
2021-02-25 04:59:06 +03:00
private void DeleteHost(object sender, RoutedEventArgs e)
{
2021-03-04 11:25:36 +03:00
if (DataGrid.SelectedItem is RecordViewModel project)
2021-02-25 04:59:06 +03:00
{
var result = MessageBox.Show($"您确认删除主机{project.Host.Tag}吗?", "提示", MessageBoxButton.OKCancel);
if (result == MessageBoxResult.OK)
{
2021-03-04 11:25:36 +03:00
Records.Remove(project);
2021-02-25 04:59:06 +03:00
}
}
}
private void Connect(object sender, RoutedEventArgs e)
{
2021-03-04 13:25:52 +03:00
var project = DataGrid.SelectedItem as RecordViewModel;
2021-02-25 04:59:06 +03:00
if (project == null)
{
DialogManager.ShowMessageAsync(this, "提示", "请选择一个服务器");
}
2021-03-04 13:25:52 +03:00
TerminalWindow terminalWindow = new TerminalWindow(project.record);
2021-02-25 04:59:06 +03:00
terminalWindow.Show();
}
}
}