-
-
Notifications
You must be signed in to change notification settings - Fork 445
Expand file tree
/
Copy pathEtoDialogHelper.cs
More file actions
153 lines (143 loc) · 5.06 KB
/
Copy pathEtoDialogHelper.cs
File metadata and controls
153 lines (143 loc) · 5.06 KB
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
152
153
using Eto.Forms;
using NAPS2.ImportExport;
namespace NAPS2.EtoForms;
public class EtoDialogHelper : DialogHelper
{
private readonly Naps2Config _config;
private readonly FileFilters _fileFilters;
private bool _addExt = EtoPlatform.Current.IsGtk;
public EtoDialogHelper(Naps2Config config, FileFilters fileFilters)
{
_config = config;
_fileFilters = fileFilters;
}
public override bool PromptToSavePdfOrImage(string? defaultPath, out string? savePath)
{
var lastExt = _config.Get(c => c.LastPdfOrImageExt)?.ToLowerInvariant();
if (string.IsNullOrEmpty(lastExt))
{
lastExt = "pdf";
}
var sd = new SaveFileDialog
{
FileName = GetDefaultFileName(defaultPath, lastExt!)
};
_fileFilters.Set(sd, FileFilterGroup.Pdf | FileFilterGroup.Image, lastExt);
SetDir(sd, defaultPath);
EtoPlatform.Current.ConfigureFileDialog(sd);
if (sd.ShowDialog(null) == DialogResult.Ok)
{
savePath = sd.FileName;
_config.User.Set(c => c.LastPdfOrImageExt, (Path.GetExtension(sd.FileName) ?? "").Replace(".", ""));
return true;
}
savePath = null;
return false;
}
public override bool PromptToSavePdf(string? defaultPath, out string? savePath)
{
var sd = new SaveFileDialog
{
FileName = GetDefaultFileName(defaultPath, "pdf")
};
_fileFilters.Set(sd, FileFilterGroup.Pdf);
SetDir(sd, defaultPath);
EtoPlatform.Current.ConfigureFileDialog(sd);
if (sd.ShowDialog(null) == DialogResult.Ok)
{
savePath = sd.FileName;
return true;
}
savePath = null;
return false;
}
public override bool PromptToSaveImage(string? defaultPath, out string? savePath)
{
var lastExt = _config.Get(c => c.LastImageExt)?.ToLowerInvariant();
if (string.IsNullOrEmpty(lastExt))
{
lastExt = "jpg";
}
var sd = new SaveFileDialog
{
FileName = GetDefaultFileName(defaultPath, lastExt!)
};
var filterGroups = EtoPlatform.Current.IsGtk
? FileFilterGroup.AllImages | FileFilterGroup.Image
: FileFilterGroup.Image;
_fileFilters.Set(sd, filterGroups, lastExt);
SetDir(sd, defaultPath);
EtoPlatform.Current.ConfigureFileDialog(sd);
if (sd.ShowDialog(null) == DialogResult.Ok)
{
savePath = sd.FileName;
_config.User.Set(c => c.LastImageExt, (Path.GetExtension(sd.FileName) ?? "").Replace(".", ""));
return true;
}
savePath = null;
return false;
}
private string? GetDefaultFileName(string? defaultPath, string ext)
{
if (string.IsNullOrEmpty(defaultPath))
{
// Provide a default timestamped placeholder when no path is configured
return _addExt ? $"NAPS2_Scanned_$(YYYY)-$(MM)-$(DD)$(hh)-$(mm)-$(ss).{ext}" : null;
}
var normPath = NormalizePath(defaultPath);
return _addExt && !Path.HasExtension(normPath)
? $"{normPath}.{ext}"
: normPath;
}
private void SetDir(SaveFileDialog dialog, string? defaultPath)
{
string? path = null;
if (Paths.IsTestAppDataPath)
{
// For UI test automation we choose the appdata folder for test isolation and consistency
path = Paths.AppData;
}
else if (Path.IsPathRooted(defaultPath))
{
path = Path.GetDirectoryName(NormalizePath(defaultPath));
}
if (path != null)
{
dialog.Directory = UriHelper.FilePathToFileUri(Path.GetFullPath(path));
}
}
private static string NormalizePath(string path)
{
string normPath = Placeholders.NonNumeric.Substitute(path);
// If the path points to a directory, it should end in a trailing slash.
// Otherwise, path functions will assume that the directory name is a file name.
if (Directory.Exists(normPath) && !normPath.EndsWith(Path.DirectorySeparatorChar))
{
normPath += Path.DirectorySeparatorChar;
}
return normPath;
}
public override bool PromptToImport(out string[]? filePaths)
{
var ofd = new OpenFileDialog
{
MultiSelect = true,
CheckFileExists = true
};
_fileFilters.Set(ofd,
FileFilterGroup.AllFiles | FileFilterGroup.Pdf | FileFilterGroup.AllImages | FileFilterGroup.Image);
if (Paths.IsTestAppDataPath)
{
// For UI test automation we choose the appdata folder to find the prepared files to import
ofd.Directory = UriHelper.FilePathToFileUri(Path.GetFullPath(Paths.AppData));
}
EtoPlatform.Current.ConfigureFileDialog(ofd);
if (ofd.ShowDialog(null) == DialogResult.Ok)
{
filePaths = ofd.Filenames.ToArray();
return true;
}
filePaths = null;
return false;
}
}