-
Notifications
You must be signed in to change notification settings - Fork 8
/
FtpSettingsWindow.xaml.cs
151 lines (141 loc) · 6.07 KB
/
FtpSettingsWindow.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using WinSCP;
namespace ValheimSaveShield
{
/// <summary>
/// Interaction logic for FtpSettingsWindow.xaml
/// </summary>
public partial class FtpSettingsWindow : Window
{
public FtpSettingsWindow()
{
InitializeComponent();
WindowStartupLocation = WindowStartupLocation.CenterOwner;
txtIP.Text = Properties.Settings.Default.FtpIpAddress;
txtPort.Text = Properties.Settings.Default.FtpPort;
txtWorldsPath.Text = Properties.Settings.Default.FtpFilePath;
txtUsername.Text = Properties.Settings.Default.FtpUsername;
txtPassword.Text = Properties.Settings.Default.FtpPassword;
foreach (var path in Properties.Settings.Default.SaveFolders)
{
lstSaveFolder.Items.Add(path);
}
if (Properties.Settings.Default.FtpSaveDest.Length > 0)
{
lstSaveFolder.SelectedItem = Properties.Settings.Default.FtpSaveDest;
}
else
{
lstSaveFolder.SelectedIndex = 0;
}
foreach (int i in Enum.GetValues(typeof(FtpMode)))
{
cmbFtpMode.Items.Add(Enum.GetName(typeof(FtpMode), i));
}
cmbFtpMode.SelectedIndex = Properties.Settings.Default.FtpMode;
}
private void btnSave_Click(object sender, RoutedEventArgs e)
{
if (!txtWorldsPath.Text.StartsWith("/"))
{
//txtWorldsPath.Text = txtWorldsPath.Text.Remove(0, 1);
txtWorldsPath.Text = "/"+txtWorldsPath.Text;
}
if (txtWorldsPath.Text.EndsWith("/"))
{
txtWorldsPath.Text = txtWorldsPath.Text.Remove(txtWorldsPath.Text.Length - 1, 1);
}
Properties.Settings.Default.FtpIpAddress = txtIP.Text;
Properties.Settings.Default.FtpPort = txtPort.Text;
Properties.Settings.Default.FtpFilePath = txtWorldsPath.Text;
Properties.Settings.Default.FtpUsername = txtUsername.Text;
Properties.Settings.Default.FtpPassword = txtPassword.Text;
Properties.Settings.Default.FtpMode = cmbFtpMode.SelectedIndex;
Properties.Settings.Default.FtpSaveDest = lstSaveFolder.SelectedItem.ToString();
Properties.Settings.Default.Save();
DialogResult = true;
this.Close();
}
private void btnCancel_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
private void btnTest_Click(object sender, RoutedEventArgs e)
{
try
{
if (txtIP.Text.Length == 0 || txtPort.Text.Length == 0 || txtWorldsPath.Text.Length == 0 || txtUsername.Text.Length == 0 || txtPassword.Text.Length == 0)
{
ModernMessageBox mmb = new ModernMessageBox(this);
mmb.Show("All fields are required.", "Missing FTP information", MessageBoxButton.OK, MessageBoxImage.Warning);
return;
}
SessionOptions sessionOptions = new SessionOptions
{
Protocol = Protocol.Ftp,
HostName = txtIP.Text,
PortNumber = Int32.Parse(txtPort.Text),
UserName = txtUsername.Text,
Password = txtPassword.Text,
FtpMode = (FtpMode)cmbFtpMode.SelectedIndex
};
using (Session session = new Session())
{
// Connect
try
{
session.Open(sessionOptions);
if (session.FileExists(txtWorldsPath.Text))
{
var dbFound = false;
foreach (var file in session.ListDirectory(txtWorldsPath.Text).Files)
{
if (file.ToString().EndsWith(".db"))
{
dbFound = true;
}
}
if (dbFound)
{
ModernMessageBox mmb = new ModernMessageBox(this);
mmb.Show("Connection successful.", "Connection Successful", MessageBoxButton.OK, MessageBoxImage.Information);
}
else
{
ModernMessageBox mmb = new ModernMessageBox(this);
mmb.Show($"Connected successfully, but no world saves found at {txtWorldsPath.Text}.", "No Worlds Found", MessageBoxButton.OK, MessageBoxImage.Warning);
}
}
else
{
ModernMessageBox mmb = new ModernMessageBox(this);
mmb.Show($"Connected successfully to FTP server, but path {txtWorldsPath.Text} does not exist.", "Path Not Found", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
catch (Exception ex)
{
ModernMessageBox mmb = new ModernMessageBox(this);
mmb.Show($"Error connecting to FTP server: {ex.Message}", "Connection Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}
catch (Exception ex)
{
Debug.WriteLine($"Error testing FTP connection: {ex.Message}");
}
}
}
}