-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathSettings.cs
192 lines (159 loc) · 7.22 KB
/
Settings.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
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Win32; // Registry access
using iText.Kernel.Pdf;
namespace PDFEncrypt
{
class Settings
{
public enum EncryptionType
{
AES_256 = EncryptionConstants.ENCRYPTION_AES_256,
AES_128 = EncryptionConstants.ENCRYPTION_AES_128,
RC4_128 = EncryptionConstants.STANDARD_ENCRYPTION_128,
RC4_40 = EncryptionConstants.STANDARD_ENCRYPTION_40
}
public static bool run_after; // Run program after encrypting?
public static string run_after_file; // File to run after encrypting
public static string run_after_arguments; // Arguments to pass to the run_after file.
public static bool password_confirm; // Confirm password?
public static bool close_after; // Close after encrypting?
public static bool show_folder_after; // Show folder in Explorer after encrypting?
public static bool open_after; // Open the destination file in its default program?
// Encryption options:
public static EncryptionType encryption_type; // Type of encryption to use
public static bool encrypt_metadata; // Should metadata be encrypted?
public static bool allow_printing; // Should end user be allowed to print PDF?
public static bool allow_degraded_printing; // Should end user be allowed to print PDF degraded?
public static bool allow_modifying; // Should end user be allowed to modify the PDF?
public static bool allow_modifying_annotations; // Should end user be allowed to modify annotations?
public static bool allow_copying; // Should end user be allowed to copy from PDF?
public static bool allow_form_fill; // Should end user be allowed to fill in form fields?
public static bool allow_assembly; // Should end user be allowed to assemble the document?
public static bool allow_screenreaders; // Should screenreaders be allowed to access the document?
// Events to execute upon setting changes
public delegate void SettingChangedNotification();
public static List<SettingChangedNotification> notify = new List<SettingChangedNotification>(); // Add delegate functions to this list to be notified.
// Constants:
const string REG_KEY = "HKEY_CURRENT_USER\\Software\\PDFEncrypt\\"; // Main registry key
public static void load()
{
// Read settings from registry.
object x;
// Run program after encryption?
x = Registry.GetValue(REG_KEY, "run_after", 0);
if (x == null) { x = 0; }
run_after = (int)x == 1; // Convert to boolean.
// Program to run:
x = Registry.GetValue(REG_KEY, "run_after_file", null);
if (x == null) { x = ""; }
run_after_file = (string)x;
// Run After arguments
x = Registry.GetValue(REG_KEY, "run_after_arguments", null);
if (x == null) { x = ""; }
run_after_arguments = (string)x;
// Require password confirmation
x = Registry.GetValue(REG_KEY, "password_confirm", 0);
if (x == null) { x = 0; }
password_confirm = (int)x == 1;
// Close after encrypting
x = Registry.GetValue(REG_KEY, "close_after", 0);
if (x == null) { x = 0; }
close_after = (int)x == 1;
// Show folder after encrypting
x = Registry.GetValue(REG_KEY, "show_folder_after", 0);
if (x == null) { x = 0; }
show_folder_after = (int)x == 1;
// Open file after encrypting
x = Registry.GetValue(REG_KEY, "open_after", 0);
if (x == null) { x = 0; }
open_after = (int)x == 1;
// Encryption options:
// Encryption type:
x = Registry.GetValue(REG_KEY, "encryption_type", 0);
if (x == null) { x = (int)EncryptionType.AES_256; }
if (!Enum.IsDefined(typeof(EncryptionType), (int)x)) // If not a valid option, use default:
{
encryption_type = EncryptionType.AES_256; // Default to AES_256
}
else
{
encryption_type = (EncryptionType)x;
}
// Encrypt metadata
x = Registry.GetValue(REG_KEY, "encrypt_metadata", 0);
if (x == null) { x = 0; }
encrypt_metadata = (int)x == 1;
// Allow printing
x = Registry.GetValue(REG_KEY, "allow_printing", 0);
if (x == null) { x = 0; }
allow_printing = (int)x == 1;
// Allow degraded printing
x = Registry.GetValue(REG_KEY, "allow_degraded_printing", 0);
if (x == null) { x = 0; }
allow_degraded_printing = (int)x == 1;
// Allow modifying
x = Registry.GetValue(REG_KEY, "allow_modifying", 0);
if (x == null) { x = 0; }
allow_modifying = (int)x == 1;
// Allow modifying notations
x = Registry.GetValue(REG_KEY, "allow_modifying_annotations", 0);
if (x == null) { x = 0; }
allow_modifying_annotations = (int)x == 1;
// Allow copying
x = Registry.GetValue(REG_KEY, "allow_copying", 0);
if (x == null) { x = 0; }
allow_copying = (int)x == 1;
// Allow form fill
x = Registry.GetValue(REG_KEY, "allow_form_fill", 0);
if (x == null) { x = 0; }
allow_form_fill = (int)x == 1;
// Allow assembly
x = Registry.GetValue(REG_KEY, "allow_assembly", 0);
if (x == null) { x = 0; }
allow_assembly = (int)x == 1;
// Allow screenreaders
x = Registry.GetValue(REG_KEY, "allow_screenreaders", 0);
if (x == null) { x = 0; }
allow_screenreaders = (int)x == 1;
// Notify all listeners of updates.
callNotify();
}
public static void callNotify()
// Notify all listeners of updates.
{
// Notify each listener of the updates.
foreach (SettingChangedNotification s in notify)
{
s(); // Call the function.
}
}
public static void save()
// Write all settings to registry
{
Registry.SetValue(REG_KEY, "run_after", (object)run_after, RegistryValueKind.DWord);
Registry.SetValue(REG_KEY, "run_after_file", (object)run_after_file, RegistryValueKind.String);
Registry.SetValue(REG_KEY, "run_after_arguments", (object)run_after_arguments, RegistryValueKind.String);
Registry.SetValue(REG_KEY, "password_confirm", (object)password_confirm, RegistryValueKind.DWord);
Registry.SetValue(REG_KEY, "close_after", (object)close_after, RegistryValueKind.DWord);
Registry.SetValue(REG_KEY, "show_folder_after", (object)show_folder_after, RegistryValueKind.DWord);
Registry.SetValue(REG_KEY, "open_after", (object)open_after, RegistryValueKind.DWord);
// Encryption options:
Registry.SetValue(REG_KEY, "encryption_type", (object)encryption_type, RegistryValueKind.DWord);
Registry.SetValue(REG_KEY, "encrypt_metadata", (object)encrypt_metadata, RegistryValueKind.DWord);
Registry.SetValue(REG_KEY, "allow_printing", (object)allow_printing, RegistryValueKind.DWord);
Registry.SetValue(REG_KEY, "allow_degraded_printing", (object)allow_degraded_printing, RegistryValueKind.DWord);
Registry.SetValue(REG_KEY, "allow_modifying", (object)allow_modifying, RegistryValueKind.DWord);
Registry.SetValue(REG_KEY, "allow_modifying_annotations", (object)allow_modifying_annotations, RegistryValueKind.DWord);
Registry.SetValue(REG_KEY, "allow_copying", (object)allow_copying, RegistryValueKind.DWord);
Registry.SetValue(REG_KEY, "allow_form_fill", (object)allow_form_fill, RegistryValueKind.DWord);
Registry.SetValue(REG_KEY, "allow_assembly", (object)allow_assembly, RegistryValueKind.DWord);
Registry.SetValue(REG_KEY, "allow_screenreaders", (object)allow_screenreaders, RegistryValueKind.DWord);
// Notify all listeners
callNotify();
}
}
}