-
-
Notifications
You must be signed in to change notification settings - Fork 445
Expand file tree
/
Copy pathPdfSettingsForm.cs
More file actions
209 lines (190 loc) · 8.73 KB
/
Copy pathPdfSettingsForm.cs
File metadata and controls
209 lines (190 loc) · 8.73 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
using Eto.Drawing;
using Eto.Forms;
using NAPS2.EtoForms.Layout;
using NAPS2.EtoForms.Widgets;
using NAPS2.Pdf;
namespace NAPS2.EtoForms.Ui;
public class PdfSettingsForm : EtoDialogBase
{
private readonly FilePathWithPlaceholders _defaultFilePath;
private readonly CheckBox _skipSavePrompt = new() { Text = UiStrings.SkipSavePrompt };
private readonly CheckBox _useDefaultFileNamePlaceholder = new() { Text = UiStrings.UseDefaultFileNamePlaceholder };
private readonly CheckBox _singlePagePdfs = new() { Text = UiStrings.SinglePageFiles };
private readonly TextBox _title = new();
private readonly TextBox _author = new();
private readonly TextBox _subject = new();
private readonly TextBox _keywords = new();
private readonly CheckBox _encryptPdf = new() { Text = UiStrings.EncryptPdf };
private readonly PasswordBoxWithToggle _ownerPassword = new() { Title = UiStrings.OwnerPasswordLabel };
private readonly PasswordBoxWithToggle _userPassword = new() { Title = UiStrings.UserPasswordLabel };
private readonly CheckBox _rememberSettings = new() { Text = UiStrings.RememberTheseSettings };
private readonly Button _restoreDefaults = new() { Text = UiStrings.RestoreDefaults };
private readonly LayoutVisibility _encryptVis = new(false);
private readonly List<CheckBox> _permissions =
[
new CheckBox { Text = UiStrings.AllowPrinting },
new CheckBox { Text = UiStrings.AllowFullQualityPrinting },
new CheckBox { Text = UiStrings.AllowDocumentModification },
new CheckBox { Text = UiStrings.AllowDocumentAssembly },
new CheckBox { Text = UiStrings.AllowContentCopying },
new CheckBox { Text = UiStrings.AllowContentCopyingForAccessibility },
new CheckBox { Text = UiStrings.AllowAnnotations },
new CheckBox { Text = UiStrings.AllowFormFilling }
];
private readonly EnumDropDownWidget<PdfCompat> _compat = new();
public PdfSettingsForm(Naps2Config config, DialogHelper dialogHelper, IIconProvider iconProvider) : base(config)
{
Title = UiStrings.PdfSettingsFormTitle;
IconName = "file_extension_pdf_small";
_defaultFilePath = new(this, dialogHelper) { PdfOnly = true };
_compat.Format = compat => compat switch
{
PdfCompat.Default => UiStrings.Default,
PdfCompat.PdfA1B => "PDF/A-1b",
PdfCompat.PdfA2B => "PDF/A-2b",
PdfCompat.PdfA3B => "PDF/A-3b",
PdfCompat.PdfA3U => "PDF/A-3u",
_ => throw new ArgumentException()
};
UpdateValues(Config);
UpdateEnabled();
_restoreDefaults.Click += RestoreDefaults_Click;
_defaultFilePath.TextChanged += DefaultFilePath_TextChanged;
_encryptPdf.CheckedChanged += EncryptPdf_CheckedChanged;
}
protected override void BuildLayout()
{
FormStateController.DefaultExtraLayoutSize = new Size(60, 0);
FormStateController.FixedHeightLayout = true;
LayoutController.Content = L.Column(
C.Label(UiStrings.DefaultFilePathLabel),
_defaultFilePath,
_skipSavePrompt,
_useDefaultFileNamePlaceholder,
_singlePagePdfs,
L.GroupBox(
UiStrings.Metadata,
L.Column(
C.Label(UiStrings.TitleLabel),
_title,
C.Label(UiStrings.AuthorLabel),
_author,
C.Label(UiStrings.SubjectLabel),
_subject,
C.Label(UiStrings.KeywordsLabel),
_keywords
)
),
L.GroupBox(
UiStrings.Encryption,
L.Column(
_encryptPdf,
L.Column(
_ownerPassword,
_userPassword,
L.Column(_permissions.Expand()).Spacing(0)
).Visible(_encryptVis)
)
),
L.GroupBox(
UiStrings.Compatibility,
_compat
),
C.Filler(),
_rememberSettings,
L.Row(
_restoreDefaults.MinWidth(140),
C.Filler(),
L.OkCancel(
C.OkButton(this, Save),
C.CancelButton(this))
)
);
}
private void UpdateValues(Naps2Config config)
{
_defaultFilePath.Text = config.Get(c => c.PdfSettings.DefaultFileName);
_skipSavePrompt.Checked = config.Get(c => c.PdfSettings.SkipSavePrompt);
_useDefaultFileNamePlaceholder.Checked = config.Get(c => c.PdfSettings.UseDefaultFileNamePlaceholder);
_singlePagePdfs.Checked = config.Get(c => c.PdfSettings.SinglePagePdfs);
_title.Text = config.Get(c => c.PdfSettings.Metadata.Title);
_author.Text = config.Get(c => c.PdfSettings.Metadata.Author);
_subject.Text = config.Get(c => c.PdfSettings.Metadata.Subject);
_keywords.Text = config.Get(c => c.PdfSettings.Metadata.Keywords);
_encryptPdf.Checked = config.Get(c => c.PdfSettings.Encryption.EncryptPdf);
_ownerPassword.Text = config.Get(c => c.PdfSettings.Encryption.OwnerPassword);
_userPassword.Text = config.Get(c => c.PdfSettings.Encryption.UserPassword);
_permissions[0].Checked = config.Get(c => c.PdfSettings.Encryption.AllowPrinting);
_permissions[1].Checked = config.Get(c => c.PdfSettings.Encryption.AllowFullQualityPrinting);
_permissions[2].Checked = config.Get(c => c.PdfSettings.Encryption.AllowDocumentModification);
_permissions[3].Checked = config.Get(c => c.PdfSettings.Encryption.AllowDocumentAssembly);
_permissions[4].Checked = config.Get(c => c.PdfSettings.Encryption.AllowContentCopying);
_permissions[5].Checked =
config.Get(c => c.PdfSettings.Encryption.AllowContentCopyingForAccessibility);
_permissions[6].Checked = config.Get(c => c.PdfSettings.Encryption.AllowAnnotations);
_permissions[7].Checked = config.Get(c => c.PdfSettings.Encryption.AllowFormFilling);
_compat.SelectedItem = config.Get(c => c.PdfSettings.Compat);
_rememberSettings.Checked = config.Get(c => c.RememberPdfSettings);
}
private void UpdateEnabled()
{
_skipSavePrompt.Enabled = Path.IsPathRooted(_defaultFilePath.Text);
_encryptVis.IsVisible = _encryptPdf.IsChecked();
_compat.Enabled = !Config.AppLocked.Has(c => c.PdfSettings.Compat);
}
private void Save()
{
var pdfSettings = new PdfSettings
{
DefaultFileName = _defaultFilePath.Text,
SkipSavePrompt = _skipSavePrompt.IsChecked(),
UseDefaultFileNamePlaceholder = _useDefaultFileNamePlaceholder.IsChecked(),
SinglePagePdfs = _singlePagePdfs.IsChecked(),
Metadata = new PdfMetadata
{
Title = _title.Text,
Author = _author.Text,
Subject = _subject.Text,
Keywords = _keywords.Text
},
Encryption = new PdfEncryption
{
EncryptPdf = _encryptPdf.IsChecked(),
OwnerPassword = _ownerPassword.Text,
UserPassword = _userPassword.Text,
AllowPrinting = _permissions[0].IsChecked(),
AllowFullQualityPrinting = _permissions[1].IsChecked(),
AllowDocumentModification = _permissions[2].IsChecked(),
AllowDocumentAssembly = _permissions[3].IsChecked(),
AllowContentCopying = _permissions[4].IsChecked(),
AllowContentCopyingForAccessibility = _permissions[5].IsChecked(),
AllowAnnotations = _permissions[6].IsChecked(),
AllowFormFilling = _permissions[7].IsChecked()
},
Compat = _compat.SelectedItem
};
var runTransact = Config.Run.BeginTransaction();
var userTransact = Config.User.BeginTransaction();
bool remember = _rememberSettings.IsChecked();
var transactToWrite = remember ? userTransact : runTransact;
runTransact.Remove(c => c.PdfSettings);
userTransact.Remove(c => c.PdfSettings);
transactToWrite.Set(c => c.PdfSettings, pdfSettings);
userTransact.Set(c => c.RememberPdfSettings, remember);
runTransact.Commit();
userTransact.Commit();
}
private void RestoreDefaults_Click(object? sender, EventArgs e)
{
UpdateValues(Config.DefaultsOnly);
UpdateEnabled();
}
private void DefaultFilePath_TextChanged(object? sender, EventArgs e)
{
UpdateEnabled();
}
private void EncryptPdf_CheckedChanged(object? sender, EventArgs e)
{
UpdateEnabled();
}
}