forked from changbowen/DesktopNote
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Helpers.cs
223 lines (198 loc) · 9.6 KB
/
Helpers.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
using Microsoft.Win32;
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Documents;
using System.Xml.Linq;
namespace DesktopNote
{
internal static class Helpers
{
internal static string OpenFileDialog(Window owner, bool save, string path = null, string filter = null)
{
if (owner == null || PresentationSource.FromVisual(owner) == null)
throw new Exception("ShowDialog needs a loaded window to attach itself.");
var dlg = new OpenFileDialog {
Title = save ? (string)App.Res["menu_savenote"] : (string)App.Res["menu_opennote"],
CheckFileExists = save ? false : true,
CheckPathExists = save ? false : true,
};
dlg.Filter = filter ?? @"DesktopNote Content|*";
if (!string.IsNullOrEmpty(path)) dlg.InitialDirectory = Path.GetDirectoryName(path);
if (dlg.ShowDialog(owner) == true) return dlg.FileName;
return null;
}
/// <summary>
/// Wrapper around MessageBox.Show() with default app name as title. If resourceKey is passed, body will be ignored. Vice versa.
/// </summary>
internal static MessageBoxResult MsgBox(string resourceKey = null, string body = null, string title = null,
MessageBoxButton button = MessageBoxButton.OK, MessageBoxImage image = MessageBoxImage.Information)
{
if (title == null) title = (string)App.Res["desktopnote"];
if (!string.IsNullOrWhiteSpace(resourceKey))
return MessageBox.Show((string)App.Res[resourceKey], title, button, image);
else
return MessageBox.Show(body, title, button, image);
}
internal static MainWindow NewNote(Setting refSetting = null)
{
var win = new MainWindow(new Setting(Setting.NoteFlag.CreateNew, refSetting));
win.Show();
win.Top += 20d; win.Left += 20d;
SaveNote(win);
return win;
}
/// <summary>
/// Perform checks before instantiating MainWindow.
/// When path file exists, note will be loaded in a new window. If refSetting is passed, settings in note file will be ignored.
/// When path file does not exist, a new note window will be created with the path specified.
/// Returns null or MainWindow.
/// </summary>
internal static MainWindow OpenNote(string path, Setting refSetting = null)
{
if (path == null) throw new ArgumentNullException(nameof(path));
//open if exists. otherwise create
if (File.Exists(path)) {
//check if note is already loaded in another window
var win = App.MainWindows.Find(w => w.CurrentSetting.Doc_Location == path);
if (win != null) {
MsgBox("msgbox_file_already_opened", button: MessageBoxButton.OK, image: MessageBoxImage.Information);
win.UnDock(); win.Activate(); win.RTB_Main.Focus();
return null;
}
return new MainWindow(new Setting(Setting.NoteFlag.Existing, refSetting, path));
}
else
return new MainWindow(new Setting(Setting.NoteFlag.CreateNew, refSetting, path));
}
internal static bool LoadNote(MainWindow win)
{
FileStream fs = null;
try {
Monitor.Enter(win.lock_save);
fs = new FileStream(win.CurrentSetting.Doc_Location, FileMode.Open);
//load setting from file when CurrentSetting is null
if (!win.CurrentSetting.Flags.HasFlag(Setting.NoteFlag.IgnoreSettingsFromFile)) {
using (var sr = new StreamReader(fs, Encoding.UTF8, false, 1024, true)) {
string settingStr = null;
while (!sr.EndOfStream) {
var line = sr.ReadLine();
if (line == Setting.SettingBeginMark) {
settingStr = sr.ReadLine();
}
sr.ReadLine();
break;
}
if (settingStr != null) win.CurrentSetting.Parse(settingStr);
}
}
//load contents from file
fs.Position = 0;
long startPos = -1;
//try get content start position
using (var sr = new StreamReader(fs, Encoding.UTF8, false, 1024, true)) {
while (!sr.EndOfStream) {
var line = sr.ReadLine();
if (line == Setting.ContentBeginMark) { startPos = sr.GetPosition(); break; }
}
}
var tr = new TextRange(win.RTB_Main.Document.ContentStart, win.RTB_Main.Document.ContentEnd);
if (startPos == -1) {
//try reading entire file as XamlPackage for backward compatibility
tr.Load(fs, DataFormats.XamlPackage);
}
else {
fs.Position = startPos;
using (var ms = new MemoryStream()) {
fs.CopyTo(ms);
tr.Load(ms, DataFormats.XamlPackage);
}
}
return true;
}
catch (Exception ex) {
MsgBox(body: string.Format((string)App.Res["msgbox_load_error"], win.CurrentSetting.Doc_Location, win.CurrentSetting.Bak_Location) + "\r\n\r\n" + ex.Message,
title: (string)App.Res["msgbox_title_load_error"], button: MessageBoxButton.OK, image: MessageBoxImage.Stop);
return false;
}
finally {
fs?.Close();
fs?.Dispose();
Monitor.Exit(win.lock_save);
}
}
/// <summary>
/// Write per-note settings and note content to file. And save app settings.
/// Returns null if succeeded. Otherwise the error message.
/// </summary>
internal static string SaveNote(MainWindow win)
{
string statusText, exMsg = null;
try {
Monitor.Enter(win.lock_save);
var tr = new TextRange(win.RTB_Main.Document.ContentStart, win.RTB_Main.Document.ContentEnd);
using (var fs = new FileStream(win.CurrentSetting.Doc_Location, FileMode.Create)) {
// write settings to file
var settingStr = win.CurrentSetting.Serialize().Trim();
var data = Encoding.UTF8.GetBytes(Setting.SettingBeginMark + "\r\n" + settingStr + "\r\n" + Setting.SettingEndMark + "\r\n");
fs.Write(data, 0, data.Length);
// write contents to file
data = Encoding.UTF8.GetBytes(Setting.ContentBeginMark + "\r\n");
fs.Write(data, 0, data.Length);
using (var ms = new MemoryStream()) {
tr.Save(ms, DataFormats.XamlPackage, true);
ms.WriteTo(fs);
}
fs.Flush();
}
//write text content to file
File.WriteAllText(win.CurrentSetting.Bak_Location, tr.Text);
statusText = (string)App.Res["status_saved"];
//save app settings
Setting.Save();
}
catch (Exception ex) {
statusText = (string)App.Res["status_save_failed"];
exMsg = ex.ToString();
}
finally {
Monitor.Exit(win.lock_save);
}
win.TB_Status.Text = statusText;
win.TB_Status.Visibility = Visibility.Visible;
return exMsg;
}
}
public static class StreamReaderExtensions
{
readonly static FieldInfo charPosField = typeof(StreamReader).GetField("charPos", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly);
readonly static FieldInfo byteLenField = typeof(StreamReader).GetField("byteLen", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly);
readonly static FieldInfo charBufferField = typeof(StreamReader).GetField("charBuffer", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly);
public static long GetPosition(this StreamReader reader)
{
//shift position back from BaseStream.Position by the number of bytes read
//into internal buffer.
int byteLen = (int)byteLenField.GetValue(reader);
var position = reader.BaseStream.Position - byteLen;
//if we have consumed chars from the buffer we need to calculate how many
//bytes they represent in the current encoding and add that to the position.
int charPos = (int)charPosField.GetValue(reader);
if (charPos > 0) {
var charBuffer = (char[])charBufferField.GetValue(reader);
var encoding = reader.CurrentEncoding;
var bytesConsumed = encoding.GetBytes(charBuffer, 0, charPos).Length;
position += bytesConsumed;
}
return position;
}
public static void SetPosition(this StreamReader reader, long position)
{
reader.DiscardBufferedData();
reader.BaseStream.Seek(position, SeekOrigin.Begin);
}
}
}