-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdata.js
129 lines (110 loc) · 3.06 KB
/
data.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
/*
* KeyMan - A gnome shell extension to access the keyring in a convenient way
* (c) 2014 David Poetzsch-Heffter <keyman@poehe.de>
* This file is distributed under the same licence as the KeyMan package.
* See file LICENSE for details.
*/
const Me = imports.misc.extensionUtils.getCurrentExtension();
const Keyring = Me.imports.keyringDbus;
const Utils = Me.imports.utils;
const Settings = Me.imports.settings;
const assert = Utils.assert;
class DataManager {
constructor(dataDir, filename) {
this.dataDir = dataDir;
this.filePath = Utils.joinPaths([dataDir, filename]);
}
_load(defaultValue) {
if (Utils.fileExists(this.filePath)) {
// load contents from file
let content = Utils.readFromFile(this.filePath);
return JSON.parse(content);
}
return defaultValue;
}
_write(data) {
Utils.mkdirP(this.dataDir);
Utils.writeToFile(this.filePath, JSON.stringify(data));
}
}
var History = class History extends DataManager {
constructor(settings, dataDir) {
super(dataDir, "history.json");
this.settings = settings;
// an array with entries like
// {path: "/org/freedesktop/secrets/...", label: "TestItem"}
this.history = this._load([]);
}
getMaxSize() {
return this.settings.get_int(Settings.KEY_HISTORY_SIZE);
}
close() {
// write to file
if (this.history.length > 0) {
this._write(this.history);
}
}
*[Symbol.iterator]() {
for (let i in this.history) {
yield this.history[i];
}
}
trimToMaxSize() {
let maxSize = this.getMaxSize();
while (this.history.length > maxSize) {
this.history.pop();
}
assert(this.history.length <= maxSize);
}
add(item) {
let idx = 0;
while (idx < this.history.length && this.history[idx].path != item.path) {
idx += 1;
}
if (idx == this.history.length) {
// not found -> add new
while (this.history.length >= this.getMaxSize()) {
this.history.pop();
}
this.history.unshift(item);
} else {
// only move it up
this.history.splice(idx, 1);
this.history.unshift(item);
this.trimToMaxSize();
}
}
};
// This is currently unused because history replaces bookmarks for now.
// If I somehow figure out how to program drag&drop and context menus for menu
// items it will be used again.
class Bookmarks extends DataManager {
constructor(dataDir) {
super(dataDir, "bookmarks.json");
// an array with entries like
// {path: "/org/freedesktop/secrets/...", label: "TestItem"}
this.bookmarks = this._load([]);
}
close() {
// write to file
if (this.bookmarks.length > 0) {
this._write(this.bookmarks);
}
}
*[Symbol.iterator]() {
for (let bm in this.bookmarks) {
yield this.bookmarks[bm];
}
}
add(label, path) {
this.bookmarks.push(Keyring.makeItem(label, path));
}
remove(path) {
let idx = 0;
while (idx < this.bookmarks.length && this.bookmarks[idx].path != path) {
idx += 1;
}
assert(this.bookmarks[idx].path == path);
this.bookmarks.splice(idx, 1);
}
}