-
Notifications
You must be signed in to change notification settings - Fork 0
/
Database.cs
383 lines (297 loc) · 9.77 KB
/
Database.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using static SylverInk.Common;
namespace SylverInk
{
public partial class Database
{
private NoteController Controller = new();
public string DBFile = string.Empty;
public bool Loaded = false;
public bool Changed { get => Controller.Changed; set => Controller.Changed = value; }
public NetClient? Client;
public long? Created;
public int Format { get => Controller.Format; set => Controller.Format = value; }
private StackPanel? HeaderPanel;
public string? Name { get => Controller.Name; set => Controller.Name = value; }
public int RecordCount => Controller.RecordCount;
public NetServer? Server;
public string? UUID { get => Controller.UUID; set => Controller.UUID = value; }
public Dictionary<string, double> WordPercentages => Controller.WordPercentages;
public Database()
{
Client = new(this);
Controller = new();
Loaded = Controller.Loaded;
Server = new(this);
}
public static void Create(string dbFile, bool threaded = false)
{
Database db = new();
if (threaded)
{
BackgroundWorker worker = new();
worker.DoWork += (_, _) => db.Load(dbFile);
worker.RunWorkerCompleted += (_, _) => AddDatabase(db);
worker.RunWorkerAsync();
return;
}
db.Load(dbFile);
AddDatabase(db);
}
public int CreateRecord(string entry, bool local = true)
{
int index = Controller.CreateRecord(entry);
if (local)
{
var outBuffer = new List<byte>([0, 0, 0, 0, .. IntToBytes(entry.Length)]);
if (entry.Length > 0)
outBuffer.AddRange(Encoding.UTF8.GetBytes(entry));
Transmit(Network.MessageType.RecordAdd, [.. outBuffer]);
}
DeferUpdateRecentNotes();
return index;
}
public void CreateRevision(int index, string newVersion, bool local = true)
{
Controller.CreateRevision(index, newVersion);
if (!local)
return;
var outBuffer = new List<byte>([
.. IntToBytes(index),
.. IntToBytes(newVersion.Length)
]);
if (newVersion.Length > 0)
outBuffer.AddRange(Encoding.UTF8.GetBytes(newVersion));
Transmit(Network.MessageType.TextInsert, [.. outBuffer]);
}
public void CreateRevision(NoteRecord record, string newVersion, bool local = true)
{
Controller.CreateRevision(record, newVersion);
if (!local)
return;
var outBuffer = new List<byte>([
.. IntToBytes(record.Index),
.. IntToBytes(newVersion.Length)
]);
if (newVersion.Length > 0)
outBuffer.AddRange(Encoding.UTF8.GetBytes(newVersion));
Transmit(Network.MessageType.TextInsert, [.. outBuffer]);
}
public void DeleteRecord(int index, bool local = true)
{
Controller.DeleteRecord(index);
if (local)
Transmit(Network.MessageType.RecordRemove, IntToBytes(index));
}
public void DeleteRecord(NoteRecord record, bool local = true)
{
for (int index = RecordCount - 1; index > -1; index--)
{
if (!Controller.GetRecord(index).Equals(record))
continue;
Controller.DeleteRecord(index);
if (local)
Transmit(Network.MessageType.RecordRemove, IntToBytes(index));
}
}
public void DeserializeRecords(List<byte>? inMemory = null) => Controller.DeserializeRecords(inMemory);
public void Erase()
{
if (Client?.Connected is true || Server?.Serving is true)
return;
Controller.EraseDatabase();
}
public string GetCreated()
{
if (Created is not null)
return DateTime.FromBinary((long)Created).ToString(DateFormat);
var CreatedObject = DateTime.UtcNow;
for (int i = 0; i < RecordCount; i++)
{
var other = Controller.GetRecord(i).GetCreatedObject();
if (CreatedObject.CompareTo(other) > 0)
CreatedObject = other;
}
Created = CreatedObject.ToBinary();
return CreatedObject.ToString(DateFormat);
}
public object GetHeader()
{
string? headerContent;
Label label;
if (HeaderPanel is not null)
{
HeaderPanel.Dispatcher.Invoke(() =>
{
label = (Label)HeaderPanel.Children[0];
headerContent = Name;
if (headerContent?.Length > 12)
headerContent = $"{headerContent[..10]}...";
label.Content = headerContent;
HeaderPanel.Children.RemoveAt(1);
HeaderPanel.Children.Add((Client?.Active is true ? Client?.Indicator : Server?.Indicator) ?? new System.Windows.Shapes.Ellipse());
HeaderPanel.ToolTip = Name;
});
return HeaderPanel;
}
headerContent = Name;
if (headerContent?.Length > 12)
headerContent = $"{headerContent[..10]}...";
label = new Label()
{
Content = headerContent,
Margin = new(0),
};
HeaderPanel = new StackPanel()
{
ContextMenu = (ContextMenu)Application.Current.MainWindow.TryFindResource("DatabaseContextMenu"),
Margin = new(0),
Orientation = Orientation.Horizontal,
ToolTip = Name,
};
HeaderPanel.Children.Add(label);
HeaderPanel.Children.Add((Client?.Active is true ? Client?.Indicator : Server?.Indicator) ?? new System.Windows.Shapes.Ellipse());
return HeaderPanel;
}
public NoteRecord GetRecord(int index) => Controller.GetRecord(index);
public void Initialize(bool newDatabase = true) => Controller.InitializeRecords(newDatabase);
public void Load(string dbFile)
{
Controller = new(DBFile = dbFile);
Loaded = Controller.Loaded;
if ((Name ?? string.Empty).Equals(string.Empty))
Name = Path.GetFileNameWithoutExtension(DBFile);
}
public void Lock(int index) => Controller.GetRecord(index).Lock();
public void MakeBackup(bool auto = false)
{
var DBPath = GetDatabasePath(this);
var BKPath = GetBackupPath(this);
if (auto)
{
for (int i = 2; i > 0; i--)
{
if (File.Exists($"{BKPath}_{i}.sibk"))
File.Copy($"{BKPath}_{i}.sibk", $"{BKPath}_{i + 1}.sibk", true);
}
if (File.Exists($"{DBPath}"))
File.Copy($"{DBPath}", $"{BKPath}_1.sibk", true);
return;
}
Controller.MakeBackup();
}
public bool Open(string path, bool writing = false) => Controller.Open(path, writing);
public void Rename(string newName)
{
var overwrite = false;
var oldFile = DBFile;
var oldName = Name;
var oldPath = Path.GetDirectoryName(oldFile);
Changed = true;
Name = newName;
DBFile = GetDatabasePath(CurrentDatabase);
var newFile = DBFile;
var newPath = Path.GetDirectoryName(newFile);
var panel = (TabControl)Application.Current.MainWindow.FindName("DatabasesPanel");
var currentTab = (TabItem)panel.SelectedItem;
currentTab.Header = GetHeader();
if (!File.Exists(oldFile))
return;
if (!Directory.Exists(oldPath))
return;
var directorySearch = Directory.GetDirectories(Subfolders["Databases"], "*", new EnumerationOptions() { IgnoreInaccessible = true, RecurseSubdirectories = true, MaxRecursionDepth = 3 });
if (oldPath is not null && newPath is not null && directorySearch.Contains(oldPath))
{
if (Directory.Exists(newPath))
{
if (MessageBox.Show($"A database with that name already exists in {newPath}.\n\nDo you want to overwrite it?", "Sylver Ink: Warning", MessageBoxButton.YesNo, MessageBoxImage.Warning) == MessageBoxResult.No)
{
DBFile = oldFile;
Name = oldName;
currentTab.Header = GetHeader();
return;
}
Directory.Delete(newPath, true);
overwrite = true;
}
else
Directory.Move(oldPath, newPath);
}
var adjustedPath = Path.Join(Path.GetDirectoryName(newFile), Path.GetFileName(oldFile));
if (!File.Exists(adjustedPath))
return;
if (File.Exists(newFile) && !overwrite)
{
if (MessageBox.Show($"A database with that name already exists at {newFile}.\n\nDo you want to overwrite it?", "Sylver Ink: Warning", MessageBoxButton.YesNo, MessageBoxImage.Warning) == MessageBoxResult.No)
{
DBFile = oldFile;
Name = oldName;
currentTab.Header = GetHeader();
return;
}
overwrite = true;
}
if (File.Exists(newFile) && overwrite)
File.Delete(newFile);
File.Move(adjustedPath, newFile);
}
public (int, int) Replace(string oldText, string newText, bool local = true)
{
if (local)
{
var oldLength = oldText.Length;
var newLength = newText.Length;
List<byte> outBuffer = [
0, 0, 0, 0,
.. IntToBytes(oldLength),
.. IntToBytes(newLength),
];
outBuffer.InsertRange(8, Encoding.UTF8.GetBytes(oldText));
outBuffer.AddRange(Encoding.UTF8.GetBytes(newText));
Transmit(Network.MessageType.RecordReplace, [.. outBuffer]);
}
return Controller.Replace(oldText, newText);
}
public void Revert(DateTime targetDate) => Controller.Revert(targetDate);
public void Save()
{
if (!Changed)
return;
if (DBFile.Equals(string.Empty))
DBFile = GetDatabasePath(this);
if (UUID is null || UUID.Equals(string.Empty))
UUID = MakeUUID(UUIDType.Database);
MakeBackup(true);
if (!Directory.Exists(Path.GetDirectoryName(DBFile)))
Directory.CreateDirectory(Path.GetDirectoryName(DBFile) ?? DBFile);
if (!Controller.Open($"{DBFile}", true))
return;
Controller.SerializeRecords();
if (DBFile.Contains(Subfolders["Databases"]))
File.WriteAllText(Path.Join(Path.GetDirectoryName(DBFile), "uuid.dat"), UUID);
}
public List<byte>? SerializeRecords(bool inMemory = false) => Controller.SerializeRecords(inMemory);
public void Sort(SortType type = SortType.ByIndex)
{
if (type == SortType.ByIndex)
Controller.PropagateIndices();
Controller.Sort(type);
}
public void Transmit(Network.MessageType type, byte[] data)
{
if (Client?.Connected is true)
Client?.Send(type, data);
if (Server?.Serving is true)
Server?.Broadcast(type, data);
}
public void Unlock(int index) => Controller.GetRecord(index).Unlock();
public void UpdateWordPercentages() => Controller.UpdateWordPercentages();
}
}