Skip to content

Commit c018472

Browse files
committed
Saving/loading FTP parameters.
1 parent 51101fb commit c018472

File tree

7 files changed

+234
-122
lines changed

7 files changed

+234
-122
lines changed

src/SmartCommander/Models/FtpModel.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ public Ftp(string name)
2020
}
2121
public class FtpModel
2222
{
23-
public static OptionsModel Instance { get; } = new OptionsModel();
23+
public static FtpModel Instance { get; } = new FtpModel();
2424
static readonly string _settingsDir = Path.Combine(GetFolderPath(SpecialFolder.ApplicationData), "SmartCommander");
2525
static readonly string _settingsPath = Path.Combine(_settingsDir, "ftps.json");
2626
static FtpModel()
2727
{
2828
Directory.CreateDirectory(_settingsDir);
2929
if (File.Exists(_settingsPath))
3030
{
31-
var options = JsonConvert.DeserializeObject<OptionsModel>(File.ReadAllText(_settingsPath));
31+
var options = JsonConvert.DeserializeObject<FtpModel>(File.ReadAllText(_settingsPath));
3232
if (options != null)
3333
{
3434
Instance = options;
@@ -38,7 +38,6 @@ static FtpModel()
3838
public void Save() => File.WriteAllText(_settingsPath, JsonConvert.SerializeObject(this, Formatting.Indented));
3939

4040
public List<Ftp> Ftps { get; set; } = [];
41-
public Ftp? CurrentFtp { get; set; }
4241

4342
}
4443
}

src/SmartCommander/Utils.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using SmartCommander.ViewModels;
33
using System;
44
using System.Collections.Generic;
5-
using System.Diagnostics;
65
using System.IO;
76
using System.Linq;
87
using System.Threading;

src/SmartCommander/ViewModels/FTPViewModel.cs

Lines changed: 87 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,67 @@
22
using ReactiveUI;
33
using SmartCommander.Models;
44
using System.Collections.ObjectModel;
5+
using System.Linq;
56
using System.Reactive;
7+
using System.Reactive.Linq;
68

79
namespace SmartCommander.ViewModels
810
{
911
public class FtpViewModel : ViewModelBase
1012
{
1113
public ObservableCollection<Ftp> Ftps { get; set; } = [];
12-
public string? FtpName { get; private set; }
13-
public bool IsAnonymous { get; private set; }
14-
public string? UserName { get; private set; }
15-
public string? Password { get; private set; }
14+
private string? _ftpName;
15+
public string? FtpName
16+
{
17+
get => _ftpName;
18+
set => this.RaiseAndSetIfChanged(ref _ftpName, value);
19+
}
20+
21+
private bool _isAnonymous;
22+
public bool IsAnonymous
23+
{
24+
get => _isAnonymous;
25+
set => this.RaiseAndSetIfChanged(ref _isAnonymous, value);
26+
}
27+
28+
private string? _userName;
29+
public string? UserName
30+
{
31+
get => _userName;
32+
set => this.RaiseAndSetIfChanged(ref _userName, value);
33+
}
34+
35+
private string? _password;
36+
public string? Password
37+
{
38+
get => _password;
39+
set => this.RaiseAndSetIfChanged(ref _password, value);
40+
}
41+
42+
private Ftp? _selectedFtp;
43+
public Ftp? SelectedFtp
44+
{
45+
get => _selectedFtp;
46+
set
47+
{
48+
this.RaiseAndSetIfChanged(ref _selectedFtp, value);
49+
50+
if (value != null)
51+
{
52+
FtpName = value.FtpName;
53+
IsAnonymous = value.IsAnonymous;
54+
UserName = value.UserName;
55+
Password = value.Password;
56+
}
57+
else
58+
{
59+
FtpName = null;
60+
IsAnonymous = false;
61+
UserName = null;
62+
Password = null;
63+
}
64+
}
65+
}
1666

1767
public ReactiveCommand<Window, Unit> OKCommand { get; }
1868
public ReactiveCommand<Window, Unit> CancelCommand { get; }
@@ -22,13 +72,43 @@ public FtpViewModel()
2272
OKCommand = ReactiveCommand.Create<Window>(SaveClose);
2373
CancelCommand = ReactiveCommand.Create<Window>(Close);
2474

25-
// TODO: load data from model
75+
Ftps.Clear();
76+
foreach (var ftp in FtpModel.Instance.Ftps)
77+
{
78+
Ftps.Add(ftp);
79+
}
2680
}
2781

2882
public void SaveClose(Window window)
2983
{
30-
// TODO: save data to model
31-
84+
if (string.IsNullOrWhiteSpace(FtpName))
85+
{
86+
return;
87+
}
88+
89+
var existing = FtpModel.Instance.Ftps.FirstOrDefault(f => f.FtpName == FtpName);
90+
91+
if (existing != null)
92+
{
93+
existing.IsAnonymous = IsAnonymous;
94+
existing.UserName = UserName;
95+
existing.Password = Password;
96+
}
97+
else
98+
{
99+
var newFtp = new Ftp(FtpName)
100+
{
101+
IsAnonymous = IsAnonymous,
102+
UserName = UserName,
103+
Password = Password
104+
};
105+
106+
FtpModel.Instance.Ftps.Add(newFtp);
107+
Ftps.Add(newFtp);
108+
}
109+
110+
FtpModel.Instance.Save();
111+
32112
window?.Close(this);
33113

34114
}

src/SmartCommander/ViewModels/FileViewModel.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,10 @@ public FileViewModel(string fullName, bool isFolder)
4646
_name = Path.GetFileNameWithoutExtension(fullName);
4747
Extension = Path.GetExtension(fullName).TrimStart('.');
4848
}
49-
Size = new FileInfo(fullName).Length.ToString();
49+
if (!fullName.StartsWith("/"))
50+
{
51+
Size = new FileInfo(fullName).Length.ToString();
52+
}
5053
DateCreated = File.GetCreationTime(fullName);
5154
if (ImageExtensions.Contains(Extension.ToLower()))
5255
{

0 commit comments

Comments
 (0)