-
Notifications
You must be signed in to change notification settings - Fork 7
/
gpaste_history.js
181 lines (147 loc) · 4.47 KB
/
gpaste_history.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
const Lang = imports.lang;
const ExtensionUtils = imports.misc.extensionUtils;
const Signals = imports.signals;
const Me = ExtensionUtils.getCurrentExtension();
const GPasteClient = Me.imports.gpaste_client;
const GPasteHistoryItem = Me.imports.gpaste_history_item;
const Utils = Me.imports.utils;
const CONNECTION_IDS = {
GPASTE_CHANGED: 0
};
const CHANGE_TYPE = {
ADDED: 0,
REMOVED: 1,
LIFTED: 2,
CLEARED: 3
};
const GPasteHistory = new Lang.Class({
Name: 'GPasteHistory',
_init: function() {
this._items = [];
CONNECTION_IDS.GPASTE_CHANGED =
GPasteClient.get_client().connect(
'changed',
Lang.bind(this, this._update_history)
);
this._update_history()
},
_update_history: function() {
function on_history_result(history_list) {
if(!history_list) {
Main.notify(
"GpasteIntegration: Couldn't connect to GPaste daemon"
);
this.clear();
return;
}
if(history_list.length < 1) {
this.clear();
return;
}
let old_length = this._items.length;
let new_items = [];
for each(let text in history_list) {
let history_item = this.get_by_hash(Utils.fnv32a(text));
if(history_item === null) {
history_item = new GPasteHistoryItem.GPasteHistoryItem({
text: text,
markup: false
}, this);
}
else {
this._items.splice(this._items.indexOf(history_item), 1);
}
history_item.inactive = false;
new_items.push(history_item);
}
let type;
if(old_length === new_items.length) {
type = CHANGE_TYPE.LIFTED;
}
else if(this._items.length > 0) {
type = CHANGE_TYPE.REMOVED;
}
else {
type = CHANGE_TYPE.ADDED;
}
for each(let item in this._items) item.destroy();
this.set_items(new_items);
this.emit('changed', type);
}
GPasteClient.get_client().get_history(Lang.bind(this, on_history_result));
},
_set_history: function(history_list) {
this.clear();
let new_items = [];
for each(let text in history_list) {
let item_data = {
text: text,
markup: false
};
let history_item =
new GPasteHistoryItem.GPasteHistoryItem(item_data, this);
history_item.inactive = false;
new_items.push(history_item);
}
this.set_items(new_items);
},
_destroy_all_items: function() {
for each(let item in this._items) item.destroy();
},
clear: function() {
this._destroy_all_items();
this._items = [];
this.emit('changed', CHANGE_TYPE.CLEARED);
},
destroy: function() {
this.clear();
GPasteClient.get_client().disconnect(
CONNECTION_IDS.GPASTE_CHANGED
);
this.emit('destroy');
},
get_items: function() {
return this._items;
},
set_items: function(history_items) {
this._items = history_items;
this._items[0].inactive = true;
},
get_index_for_item: function(history_item) {
let result = -1;
for(let i = 0; i < this.length; i++) {
if(history_item.hash === this._items[i].hash) {
result = i;
break;
}
}
return result;
},
get_by_hash: function(hash) {
let result = null;
for each(let history_item in this._items) {
if(history_item.hash === hash) {
result = history_item;
break;
}
}
return result;
},
switch_history: function(history_name) {
GPasteClient.get_client().switch_history(history_name);
this.emit('history-name-changed');
},
get length() {
return this._items.length;
},
set items(history_items) {
this.set_items(history_items);
},
get items() {
return this.get_items();
},
get current_item() {
return this._current_item;
}
});
Signals.addSignalMethods(GPasteHistory.prototype);