-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdesklet.js
executable file
·157 lines (132 loc) · 5.77 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
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
const Cinnamon = imports.gi.Cinnamon;
const Desklet = imports.ui.desklet;
const GLib = imports.gi.GLib;
const Lang = imports.lang;
const Mainloop = imports.mainloop;
const Settings = imports.ui.settings;
const St = imports.gi.St;
function MyDesklet(metadata, desklet_id) {
this._init(metadata, desklet_id);
}
MyDesklet.prototype = {
__proto__: Desklet.Desklet.prototype,
_init: function(metadata, desklet_id) {
Desklet.Desklet.prototype._init.call(this, metadata, desklet_id);
// Would like to change the style class name to qotd-desklet-label,
// but leave it as it is for backwards compatibility with user modifications to stylesheet.css
this._quote = new St.Label({style_class: "quote-container"});
this.setContent(this._quote);
this.setHeader(_("Quote of the day"));
this.update_id = null;
this.updateInProgress = false;
this.sep = "%"; // Space
this.maxSize = 7000; // Cinnamon can crash if this int is too high
try {
this.settings = new Settings.DeskletSettings(
this, this.metadata["uuid"], this.instance_id);
this.settings.bindProperty(Settings.BindingDirection.IN, "file", "file", this.on_setting_changed, null);
this.settings.bindProperty(Settings.BindingDirection.IN, "delay", "delay", this.on_setting_changed, null);
this.settings.bindProperty(Settings.BindingDirection.IN, "font-size", "fontSize", this.on_font_setting_changed, null);
this.settings.bindProperty(Settings.BindingDirection.IN, "text-color", "fontColor", this.on_font_setting_changed, null);
this.settings.bindProperty(Settings.BindingDirection.IN, "horizontal-shadow", "horShadow", this.on_font_setting_changed, null);
this.settings.bindProperty(Settings.BindingDirection.IN, "vertical-shadow", "vertShadow", this.on_font_setting_changed, null);
this.settings.bindProperty(Settings.BindingDirection.IN, "shadow-blur", "shadowBlur", this.on_font_setting_changed, null);
this.settings.bindProperty(Settings.BindingDirection.IN, "shadow-color", "shadowColor", this.on_font_setting_changed, null);
this._menu.addAction(_("Copy"), Lang.bind(this, function() {
cb = St.Clipboard.get_default();
cb.set_text(this._quote.get_text());
}));
}
catch (e) {
global.logError(e);
}
this.on_setting_changed();
this.on_font_setting_changed();
},
on_setting_changed: function() {
// Avoid accidently having more than one Mainloop timeout active at a time
if (this.updateInProgress) {
return;
}
this.updateInProgress = true;
if (this.update_id > 0) {
Mainloop.source_remove(this.update_id);
}
this.update_id = Mainloop.timeout_add_seconds(this.delay*60, Lang.bind(this, this._update));
this.file = this.file.replace('~', GLib.get_home_dir());
this._update();
this.updateInProgress = false;
},
on_font_setting_changed: function() {
let shadow = this.horShadow + "px " + this.vertShadow + "px " + this.shadowBlur + "px " + this.shadowColor + ";";
this._quote.style="font-size: " + this.fontSize + "pt;\n" +
"color: " + this.fontColor + ";\n" +
"text-shadow: " + shadow;
},
on_desklet_clicked: function(event) {
this._update();
},
on_desklet_removed: function() {
Mainloop.source_remove(this.update_id);
},
/**
* Display a new quote
**/
_update: function() {
this._quote.set_text(this._getNewQuote());
return true; // Returns true so that the timeout keeps getting called
},
/**
* Return a string containing a quote
**/
_getNewQuote: function() {
let allQuotes = "";
try {
// Since we update infrequently, reread the file in case it has changed
if (!GLib.file_test(this.file, GLib.FileTest.EXISTS)) {
return "";
}
let quoteFileContents = Cinnamon.get_file_contents_utf8_sync(this.file);
allQuotes = quoteFileContents.toString();
}
catch (e) {
global.logError(e);
return "";
}
// Ensure first and last chars are 'sep', for symmetry
if (allQuotes.charAt(0) !== this.sep) {
allQuotes = this.sep + allQuotes;
}
if (allQuotes.lastIndexOf(this.sep) !== allQuotes.length - 1) {
allQuotes = allQuotes + this.sep;
}
// Now find the beginning and end of each quotation
this._findSeparators(allQuotes);
// Choose a quote randomly, subtract 1 so we don't select the ending separator
let index = Math.floor(Math.random() * (this.separators.length - 1));
// Parse chosen quote for display
let currentQuote = allQuotes.substring(this.separators[index] + 1, this.separators[index+1]);
// Truncate if needed
currentQuote = currentQuote.substring(0, Math.min(currentQuote.length, this.maxSize));
return currentQuote;
},
/**
* Helper function for _getNewQuote
* - Returns an array containing the indicies of the separators in the allQuotes string
**/
_findSeparators: function(allQuotes) {
this.separators = [];
let index = 0;
while (index < allQuotes.length) {
index = allQuotes.indexOf(this.sep, index);
if (index === -1) {
break; // no more separators
}
this.separators.push(index);
index++;
}
}
}
function main(metadata, desklet_id) {
return new MyDesklet(metadata, desklet_id);
}