-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdesklet.js
102 lines (85 loc) · 3.06 KB
/
desklet.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
const Desklet = imports.ui.desklet;
const St = imports.gi.St;
const Settings = imports.ui.settings;
const Lang = imports.lang;
const Soup = imports.gi.Soup;
const GLib = imports.gi.GLib;
const DEFAULT_RATE = "Loading...";
const DEFAULT_SOURCE = "ws://zenrus.ru:8888/";
const DEFAULT_CURRENCY = "usd";
const ZENRUS_WS_MAPPING = {
usd: 0,
eur: 1,
brent: 2,
bitcoin: 9,
eth: 10,
bch: 11,
}
function HelloDesklet(metadata, desklet_id) {
this._init(metadata, desklet_id);
}
HelloDesklet.prototype = {
__proto__: Desklet.Desklet.prototype,
// called when extension is added
_init: function(metadata, desklet_id) {
Desklet.Desklet.prototype._init.call(this, metadata, desklet_id);
this.initSettings(metadata, desklet_id);
this.initConnection();
this.setupUI();
this.run();
},
// called when extension is disabled/removed
on_desklet_removed: function () {
this._websocketConnection.close(Soup.WebsocketCloseCode.NORMAL, "");
this._httpSession.close();
},
initConnection: function() {
this._httpSession = new Soup.Session();
Soup.Session.prototype.add_feature.call(this._httpSession, new Soup.ProxyResolverDefault());
},
onSettingsChanged: function() {
global.log("onSettingsChanged called");
},
onCurrencyChanged: function() {
this.updateExchangeRate();
},
initSettings: function(metadata, desklet_id) {
this.settings = new Settings.DeskletSettings(this, metadata.uuid, desklet_id);
this.settings.bind("currency", "cfgCurrency", this.onCurrencyChanged);
this.cfgCurrency = this.cfgCurrency || DEFAULT_CURRENCY;
this.cfgSource = DEFAULT_SOURCE;
this.exchangeRate = DEFAULT_RATE;
this._lastReceivedRates = [];
},
setupUI: function() {
// main container for the desklet
this.window = new St.Bin();
this.text = new St.Label({
style_class: 'exchange-rate'
});
this.text.set_text(this.exchangeRate);
this.window.add_actor(this.text);
this.setContent(this.window);
},
updateExchangeRate: function() {
this.exchangeRate = this._lastReceivedRates[ZENRUS_WS_MAPPING[this.cfgCurrency]];
this.text.set_text(this.cfgCurrency.toUpperCase() + " " + this.exchangeRate);
},
run: function() {
let message = new Soup.Message({
method: "GET",
uri: new Soup.URI(this.cfgSource)
});
this._httpSession.websocket_connect_async(message, null, null, null, Lang.bind(this, function(session, res) {
this._websocketConnection = session.websocket_connect_finish(res);
this._websocketConnection.connect("message", Lang.bind(this, function(connection, type, message) {
let data = message.get_data();
this._lastReceivedRates = data.toString().split(';');
this.updateExchangeRate();
}));
}));
}
}
function main(metadata, desklet_id) {
return new HelloDesklet(metadata, desklet_id);
}