forked from wmjordan/PDFPatcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AppContext.cs
287 lines (264 loc) · 9.75 KB
/
AppContext.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;
using MuPdfSharp;
using PDFPatcher.Common;
using PowerJson;
namespace PDFPatcher
{
internal static class AppContext
{
static readonly string AppConfigFilePath = FileHelper.CombinePath(
Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath),
"AppConfig.json");
static readonly SerializationManager JsonSm = new SerializationManager(new JsonReflectionController(true)) {
UseExtensions = false,
SerializeEmptyCollections = false,
SerializeNullValues = false,
SerializeReadOnlyFields = false,
SerializeReadOnlyProperties = false,
CanSerializePrivateMembers = true
};
internal const int MaxHistoryItemCount = 16;
internal static MainForm MainForm { get; set; }
static AppContext() {
SaveAppSettings = true;
BookmarkFile = String.Empty;
TargetFile = String.Empty;
CheckUpdateDate = DateTime.Now;
CheckUpdateInterval = 14;
Exporter = new ExporterOptions();
Importer = new ImporterOptions();
Merger = new MergerOptions();
Patcher = new PatcherOptions();
Editor = new PatcherOptions();
AutoBookmarker = new AutoBookmarkOptions();
Encodings = new EncodingOptions();
ImageExtracter = new ImageExtracterOptions();
ImageRenderer = new ImageRendererOptions();
ExtractPage = new ExtractPageOptions();
Ocr = new OcrOptions();
Toolbar = new ToolbarOptions();
WindowStatus = new WindowStatus();
Recent = new RecentItems();
}
public static bool SaveAppSettings { get; set; }
///<summary>获取或指定是否在加载 PDF 文档时仅加载部分文档。</summary>
public static bool LoadPartialPdfFile { get; set; }
private static string[] _SourceFiles = new string[0];
///<summary>获取或指定要处理的源文件路径列表。</summary>
public static string[] SourceFiles {
get => _SourceFiles;
set => _SourceFiles = value ?? new string[0];
}
///<summary>获取或指定检查更新的日期。</summary>
public static DateTime CheckUpdateDate { get; set; }
///<summary>获取或指定检查更新的日期间隔。</summary>
public static int CheckUpdateInterval { get; set; }
///<summary>获取或指定是否应取消批处理操作。</summary>
public static bool Abort { get; set; }
///<summary>获取或指定书签文件的路径。</summary>
public static string BookmarkFile { get; set; }
///<summary>获取或指定目标文件的路径。</summary>
public static string TargetFile { get; set; }
///<summary>获取导出设置。</summary>
public static ExporterOptions Exporter { get; internal set; }
///<summary>获取导入设置。</summary>
public static ImporterOptions Importer { get; internal set; }
///<summary>获取生成文档的设置。</summary>
public static MergerOptions Merger { get; internal set; }
///<summary>获取生成文档的设置。</summary>
public static PatcherOptions Patcher { get; internal set; }
///<summary>获取文档编辑器的设置。</summary>
public static PatcherOptions Editor { get; internal set; }
///<summary>获取自动生成书签的设置。</summary>
public static AutoBookmarkOptions AutoBookmarker { get; internal set; }
///<summary>获取应用程序设置。</summary>
public static EncodingOptions Encodings { get; internal set; }
///<summary>获取导出图像的设置。</summary>
public static ImageExtracterOptions ImageExtracter { get; internal set; }
///<summary>获取转换为图片的设置。</summary>
public static ImageRendererOptions ImageRenderer { get; internal set; }
///<summary>获取提取页面的设置。</summary>
public static ExtractPageOptions ExtractPage { get; internal set; }
///<summary>获取光学字符识别功能的设置。</summary>
public static OcrOptions Ocr { get; internal set; }
///<summary>获取或指定自定义工具栏的项目。</summary>
public static ToolbarOptions Toolbar { get; internal set; }
///<summary>获取或指定窗口状态。</summary>
public static WindowStatus WindowStatus { get; internal set; }
public static RecentItems Recent { get; internal set; }
[JsonSerializable]
public sealed class RecentItems
{
///<summary>获取最近使用的 PDF 文件列表。</summary>
[JsonField("源文件")]
public List<string> SourcePdfFiles { get; } = new List<string>();
///<summary>获取最近使用的 PDF 输出文件列表。</summary>
[JsonField("输出文件")]
public List<string> TargetPdfFiles { get; } = new List<string>();
///<summary>获取最近使用的信息文件列表。</summary>
[JsonField("信息文件")]
public List<string> InfoDocuments { get; } = new List<string>();
///<summary>获取最近使用的文件名模板列表。</summary>
[JsonField("文件名模板")]
public List<string> FileNameTemplates { get; } = new List<string>();
///<summary>获取最近使用的文件夹列表。</summary>
[JsonField("文件夹")]
public List<string> Folders { get; } = new List<string>();
///<summary>获取最近使用的查找字符串列表。</summary>
[JsonField("查找项")]
public List<string> SearchPatterns { get; } = new List<string>();
///<summary>获取最近使用的替换字符串列表。</summary>
[JsonField("替换项")]
public List<string> ReplacePatterns { get; } = new List<string>();
internal static void AddHistoryItem(IList<string> list, string item) {
if (String.IsNullOrEmpty(item)) {
return;
}
var i = -1;
var m = false;
foreach (var li in list) {
i++;
if (String.Equals(li, item, StringComparison.OrdinalIgnoreCase)) {
m = true;
break;
}
}
if (m) {
if (i == 0) {
return;
}
if (i != -1) {
list.RemoveAt(i);
}
}
list.Insert(0, item);
while (list.Count > MaxHistoryItemCount) {
list.RemoveAt(list.Count - 1);
}
}
}
internal static void CleanUpInexistentFiles(List<string> list) {
list.RemoveAll(item => FileHelper.HasFileNameMacro(item) == false && File.Exists(item) == false);
}
internal static void CleanUpInexistentFolders(List<string> list) {
list.RemoveAll(item => FileHelper.HasFileNameMacro(item) == false && Directory.Exists(item) == false);
}
internal static bool Load(string path) {
return LoadJson(path);
}
internal static bool LoadJson(string path) {
if (String.IsNullOrEmpty(path)) {
path = AppConfigFilePath;
}
if (File.Exists(path) == false) {
return false;
}
ConfigurationSerialization conf;
try {
conf = Json.ToObject<ConfigurationSerialization>(File.ReadAllText(path, Encoding.UTF8), JsonSm);
if (conf == null || conf.SaveAppSettings == false) {
SaveAppSettings = false;
return false;
}
}
catch (Exception) {
return false;
}
CheckUpdateDate = conf.CheckUpdateDate;
CheckUpdateInterval = conf.CheckUpdateInterval;
LoadPartialPdfFile = conf.PdfLoadMode == Configuration.OptimalMemoryUsage;
if (conf.Recent != null) {
Recent = conf.Recent;
}
if (conf.ExporterOptions != null) {
Exporter = conf.ExporterOptions;
}
if (conf.ImporterOptions != null) {
Importer = conf.ImporterOptions;
}
if (conf.MergerOptions != null) {
Merger = conf.MergerOptions;
}
if (conf.PatcherOptions != null) {
Patcher = conf.PatcherOptions;
}
if (conf.EditorOptions != null) {
Editor = conf.EditorOptions;
}
if (conf.AutoBookmarkOptions != null) {
AutoBookmarker = conf.AutoBookmarkOptions;
}
if (conf.Encodings != null) {
Encodings = conf.Encodings;
}
if (conf.ImageExporterOptions != null) {
ImageExtracter = conf.ImageExporterOptions;
}
if (conf.ImageRendererOptions != null) {
ImageRenderer = conf.ImageRendererOptions;
}
if (conf.OcrOptions != null) {
Ocr = conf.OcrOptions;
}
if (conf.ExtractPageOptions != null) {
ExtractPage = conf.ExtractPageOptions;
}
if (conf.ToolbarOptions != null) {
Toolbar = conf.ToolbarOptions;
}
if (conf.WindowStatus != null) {
WindowStatus = conf.WindowStatus;
}
return true;
}
/// <summary>
/// 保存应用程序配置。
/// </summary>
/// <param name="path">保存路径。路径为空时,保存到默认位置。</param>
/// <param name="saveHistoryFileList">是否保存历史文件列表。</param>
internal static void Save(string path, bool saveHistoryFileList) {
try {
SaveJson(path ?? AppConfigFilePath, saveHistoryFileList);
}
catch (Exception ex) {
FormHelper.ErrorBox("在保存程序设置时出错" + ex.Message);
}
}
static void SaveJson(string path, bool saveHistoryFileList) {
var s = SaveAppSettings
? new ConfigurationSerialization {
SaveAppSettings = true,
CheckUpdateDate = CheckUpdateDate,
CheckUpdateInterval = CheckUpdateInterval,
PdfLoadMode = LoadPartialPdfFile ? Configuration.OptimalMemoryUsage : Configuration.OptimalSpeed,
MergerOptions = Merger,
ExporterOptions = Exporter,
ImporterOptions = Importer,
PatcherOptions = Patcher,
EditorOptions = Editor,
AutoBookmarkOptions = AutoBookmarker,
Encodings = Encodings,
ImageExporterOptions = ImageExtracter,
ImageRendererOptions = ImageRenderer,
ExtractPageOptions = ExtractPage,
OcrOptions = Ocr,
ToolbarOptions = Toolbar,
WindowStatus = new WindowStatus(MainForm),
Recent = saveHistoryFileList ? Recent : null
}
: new ConfigurationSerialization { SaveAppSettings = false };
File.WriteAllText(path, Json.ToJson(s, JsonSm), Encoding.UTF8);
}
private static void WriteRecentFiles(XmlWriter writer, IList<string> list, string name) {
foreach (var item in list) {
writer.WriteStartElement(name);
writer.WriteAttributeString(Configuration.Path, item);
writer.WriteEndElement();
}
}
}
}