-
Notifications
You must be signed in to change notification settings - Fork 169
/
registry.js
319 lines (256 loc) · 10.2 KB
/
registry.js
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
import GLib from 'gi://GLib';
import Gio from 'gi://Gio';
import St from 'gi://St';
import { PrefsFields } from './constants.js';
const FileQueryInfoFlags = Gio.FileQueryInfoFlags;
const FileCopyFlags = Gio.FileCopyFlags;
const FileTest = GLib.FileTest;
export class Registry {
constructor ({ settings, uuid }) {
this.uuid = uuid;
this.settings = settings;
this.REGISTRY_FILE = 'registry.txt';
this.REGISTRY_DIR = GLib.get_user_cache_dir() + '/' + this.uuid;
this.REGISTRY_PATH = this.REGISTRY_DIR + '/' + this.REGISTRY_FILE;
this.BACKUP_REGISTRY_PATH = this.REGISTRY_PATH + '~';
}
write (entries) {
const registryContent = [];
for (let entry of entries) {
const item = {
favorite: entry.isFavorite(),
mimetype: entry.mimetype()
};
registryContent.push(item);
if (entry.isText()) {
item.contents = entry.getStringValue();
}
else if (entry.isImage()) {
const filename = this.getEntryFilename(entry);
item.contents = filename;
this.writeEntryFile(entry);
}
}
this.writeToFile(registryContent);
}
writeToFile (registry) {
let json = JSON.stringify(registry);
let contents = new GLib.Bytes(json);
// Make sure dir exists
GLib.mkdir_with_parents(this.REGISTRY_DIR, parseInt('0775', 8));
// Write contents to file asynchronously
let file = Gio.file_new_for_path(this.REGISTRY_PATH);
file.replace_async(null, false, Gio.FileCreateFlags.NONE,
GLib.PRIORITY_DEFAULT, null, (obj, res) => {
let stream = obj.replace_finish(res);
stream.write_bytes_async(contents, GLib.PRIORITY_DEFAULT,
null, (w_obj, w_res) => {
w_obj.write_bytes_finish(w_res);
stream.close(null);
});
});
}
async read () {
return new Promise(resolve => {
if (GLib.file_test(this.REGISTRY_PATH, FileTest.EXISTS)) {
let file = Gio.file_new_for_path(this.REGISTRY_PATH);
let CACHE_FILE_SIZE = this.settings.get_int(PrefsFields.CACHE_FILE_SIZE);
file.query_info_async('*', FileQueryInfoFlags.NONE,
GLib.PRIORITY_DEFAULT, null, (src, res) => {
// Check if file size is larger than CACHE_FILE_SIZE
// If so, make a backup of file, and resolve with empty array
let file_info = src.query_info_finish(res);
if (file_info.get_size() >= CACHE_FILE_SIZE * 1024 * 1024) {
let destination = Gio.file_new_for_path(this.BACKUP_REGISTRY_PATH);
file.move(destination, FileCopyFlags.OVERWRITE, null, null);
resolve([]);
return;
}
file.load_contents_async(null, (obj, res) => {
let [success, contents] = obj.load_contents_finish(res);
if (success) {
let max_size = this.settings.get_int(PrefsFields.HISTORY_SIZE);
const registry = JSON.parse(new TextDecoder().decode(contents));
const entriesPromises = registry.map(
jsonEntry => {
return ClipboardEntry.fromJSON(jsonEntry)
}
);
Promise.all(entriesPromises).then(clipboardEntries => {
clipboardEntries = clipboardEntries
.filter(entry => entry !== null);
let registryNoFavorite = clipboardEntries
.filter(entry => entry.isFavorite());
while (registryNoFavorite.length > max_size) {
let oldestNoFavorite = registryNoFavorite.shift();
let itemIdx = clipboardEntries.indexOf(oldestNoFavorite);
clipboardEntries.splice(itemIdx,1);
registryNoFavorite = clipboardEntries.filter(
entry => entry.isFavorite()
);
}
resolve(clipboardEntries);
}).catch(e => {
console.error(e);
});
}
else {
console.error('Clipboard Indicator: failed to open registry file');
}
});
});
}
else {
resolve([]);
}
});
}
#entryFileExists (entry) {
const filename = this.getEntryFilename(entry);
return GLib.file_test(filename, FileTest.EXISTS);
}
async getEntryAsImage (entry) {
const filename = this.getEntryFilename(entry);
if (entry.isImage() === false) return;
if (this.#entryFileExists(entry) == false) {
await this.writeEntryFile(entry);
}
const gicon = Gio.icon_new_for_string(this.getEntryFilename(entry));
const stIcon = new St.Icon({ gicon });
return stIcon;
}
getEntryFilename (entry) {
return `${this.REGISTRY_DIR}/${entry.asBytes().hash()}`;
}
async writeEntryFile (entry) {
if (this.#entryFileExists(entry)) return;
let file = Gio.file_new_for_path(this.getEntryFilename(entry));
return new Promise(resolve => {
file.replace_async(null, false, Gio.FileCreateFlags.NONE,
GLib.PRIORITY_DEFAULT, null, (obj, res) => {
let stream = obj.replace_finish(res);
stream.write_bytes_async(entry.asBytes(), GLib.PRIORITY_DEFAULT,
null, (w_obj, w_res) => {
w_obj.write_bytes_finish(w_res);
stream.close(null);
resolve();
});
});
});
}
async deleteEntryFile (entry) {
const file = Gio.file_new_for_path(this.getEntryFilename(entry));
try {
await file.delete_async(GLib.PRIORITY_DEFAULT, null);
}
catch (e) {
console.error(e);
}
}
clearCacheFolder() {
const CANCELLABLE = null;
try {
const folder = Gio.file_new_for_path(this.REGISTRY_DIR);
const enumerator = folder.enumerate_children("", 1, CANCELLABLE);
let file;
while ((file = enumerator.iterate(CANCELLABLE)[2]) != null) {
file.delete(CANCELLABLE);
}
}
catch (e) {
console.error(e);
}
}
}
export class ClipboardEntry {
#mimetype;
#bytes;
#favorite;
static #decode (contents) {
return Uint8Array.from(contents.match(/.{1,2}/g).map((byte) => parseInt(byte, 16)));
}
static __isText (mimetype) {
return mimetype.startsWith('text/') ||
mimetype === 'STRING' ||
mimetype === 'UTF8_STRING';
}
static async fromJSON (jsonEntry) {
const mimetype = jsonEntry.mimetype || 'text/plain;charset=utf-8';
const favorite = jsonEntry.favorite;
let bytes;
if (ClipboardEntry.__isText(mimetype)) {
bytes = new TextEncoder().encode(jsonEntry.contents);
}
else {
const filename = jsonEntry.contents;
if (!GLib.file_test(filename, FileTest.EXISTS)) return null;
let file = Gio.file_new_for_path(filename);
const contentType = await file.query_info_async('*', FileQueryInfoFlags.NONE, GLib.PRIORITY_DEFAULT, null, (obj, res) => {
try {
const fileInfo = obj.query_info_finish(res);
return fileInfo.get_content_type();
} catch (e) {
console.error(e);
}
});
if (contentType && !contentType.startsWith('image/') && !contentType.startsWith('text/')) {
bytes = new TextEncoder().encode(jsonEntry.contents);
}
else {
bytes = await new Promise((resolve, reject) => file.load_contents_async(null, (obj, res) => {
let [success, contents] = obj.load_contents_finish(res);
if (success) {
resolve(contents);
}
else {
reject(
new Error('Clipboard Indicator: could not read image file from cache')
);
}
}));
}
}
return new ClipboardEntry(mimetype, bytes, favorite);
}
constructor (mimetype, bytes, favorite) {
this.#mimetype = mimetype;
this.#bytes = bytes;
this.#favorite = favorite;
}
#encode () {
if (this.isText()) {
return this.getStringValue();
}
return [...this.#bytes]
.map(x => x.toString(16).padStart(2, '0'))
.join('');
}
getStringValue () {
if (this.isImage()) {
return `[Image ${this.asBytes().hash()}]`;
}
return new TextDecoder().decode(this.#bytes);
}
mimetype () {
return this.#mimetype;
}
isFavorite () {
return this.#favorite;
}
set favorite (val) {
this.#favorite = !!val;
}
isText () {
return ClipboardEntry.__isText(this.#mimetype);
}
isImage () {
return this.#mimetype.startsWith('image/');
}
asBytes () {
return GLib.Bytes.new(this.#bytes);
}
equals (otherEntry) {
return this.getStringValue() === otherEntry.getStringValue();
// this.asBytes().equal(otherEntry.asBytes());
}
}