Skip to content

Commit 85f506a

Browse files
authored
trilium-api@beatlink: Allow settings to be overriden from API (#7936)
1 parent c51f22b commit 85f506a

File tree

1 file changed

+81
-59
lines changed

1 file changed

+81
-59
lines changed

trilium-api@beatlink/files/trilium-api@beatlink/applet.js

Lines changed: 81 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,33 @@
66
// https://github.com/linuxmint/cinnamon/blob/master/files/usr/share/cinnamon/applets/settings-example%40cinnamon.org/settings-schema.json
77

88
// Imports
9-
const Lang = imports.lang;
10-
const Applet = imports.ui.applet;
11-
const GLib = imports.gi.GLib;
12-
const Soup = imports.gi.Soup;
13-
const Util = imports.misc.util;
14-
const ByteArray = imports.byteArray;
15-
const Settings = imports.ui.settings;
16-
const PopupMenu = imports.ui.popupMenu;
17-
const Mainloop = imports.mainloop; // for repeated updating
9+
const Lang = imports.lang
10+
const Applet = imports.ui.applet
11+
const GLib = imports.gi.GLib
12+
const Soup = imports.gi.Soup
13+
const Util = imports.misc.util
14+
const ByteArray = imports.byteArray
15+
const Settings = imports.ui.settings
16+
const PopupMenu = imports.ui.popupMenu
17+
const Mainloop = imports.mainloop // for repeated updating
1818

1919
// Constants
20-
const UUID = "trilium-api@beatlink";
20+
const UUID = "trilium-api@beatlink"
2121

2222
// Helper function to send HTTP Requests ------------------------------------------------------------------------------
2323
function sendPostRequest(url, postData, callback) {
2424
let encodedForm = Soup.form_encode_hash(postData)
25-
let message = Soup.Message.new_from_encoded_form('POST', url, encodedForm);
26-
let session = new Soup.Session();
25+
let message = Soup.Message.new_from_encoded_form('POST', url, encodedForm)
26+
let session = new Soup.Session()
2727
session.send_and_read_async(message, GLib.PRIORITY_DEFAULT, null, (session, result) => {
2828
try {
29-
let responseBytes = session.send_and_read_finish(result);
30-
let responseText = ByteArray.toString(ByteArray.fromGBytes(responseBytes));
31-
callback(null, responseText);
29+
let responseBytes = session.send_and_read_finish(result)
30+
let responseText = ByteArray.toString(ByteArray.fromGBytes(responseBytes))
31+
callback(null, responseText)
3232
} catch (error) {
33-
callback(error, null);
33+
callback(error, null)
3434
}
35-
});
35+
})
3636
}
3737

3838

@@ -49,18 +49,18 @@ class TriliumAPIAppletSettings {
4949
"fetch_action",
5050
"click_action",
5151
"prefix_string",
52-
"suffix_string",
53-
"text_width",
52+
"suffix_string",
53+
"text_width",
5454
"reminder_enabled",
55-
"reminder_time",
56-
"reminder_delay",
57-
"reminder_color",
55+
"reminder_time",
56+
"reminder_delay",
57+
"reminder_color",
5858
"open_on_click_toggle",
5959
"open_command"
60-
];
61-
this.settingsProvider = new Settings.AppletSettings(this, UUID, instanceId);
62-
for (let setting of this._setting_keys){
63-
this.settingsProvider.bindProperty(Settings.BindingDirection.BIDIRECTIONAL, setting, setting, callback, null);
60+
]
61+
this.settingsProvider = new Settings.AppletSettings(this, UUID, instanceId)
62+
for (let setting of this._setting_keys) {
63+
this.settingsProvider.bindProperty(Settings.BindingDirection.BIDIRECTIONAL, setting, setting, callback, null)
6464
}
6565
}
6666
}
@@ -69,8 +69,8 @@ class TriliumAPIAppletSettings {
6969
class TriliumAPIApplet extends Applet.TextIconApplet {
7070

7171
constructor(metadata, orientation, panelHeight, instanceId) {
72-
super(orientation, panelHeight, instanceId);
73-
this._metadata = metadata;
72+
super(orientation, panelHeight, instanceId)
73+
this._metadata = metadata
7474

7575
// Configure Settings
7676
this.settings = new TriliumAPIAppletSettings(instanceId, this.settings_callback.bind(this))
@@ -80,9 +80,9 @@ class TriliumAPIApplet extends Applet.TextIconApplet {
8080
this.full_url = `${this.base_url}:${this.port}/custom/${this.endpoint}`
8181

8282
// Setup menus
83-
this.toggle_reminder_menu = new PopupMenu.PopupSwitchMenuItem(_("Toggle Reminders"), this.settings.reminder_enabled);
83+
this.toggle_reminder_menu = new PopupMenu.PopupSwitchMenuItem(_("Toggle Reminders"), this.settings.reminder_enabled)
8484
this.toggle_reminder_menu.connect('activate', this.toggle_reminder.bind(this))
85-
this._applet_context_menu.addMenuItem(this.toggle_reminder_menu);
85+
this._applet_context_menu.addMenuItem(this.toggle_reminder_menu)
8686

8787
// Setup data persistence
8888
this.response_data = {
@@ -91,27 +91,27 @@ class TriliumAPIApplet extends Applet.TextIconApplet {
9191
}
9292

9393
// Start Fetching Text and Updating Reminders
94-
try {
95-
this.fetch();
96-
this.update_reminder();
94+
try {
95+
this.fetch()
96+
this.update_reminder()
9797
} catch (e) {
98-
global.logError(e);
99-
}
98+
global.logError(e)
99+
}
100100
}
101101

102-
settings_callback(){
102+
settings_callback() {
103103
this.fetch()
104-
this.toggle_reminder_menu.setToggleState(this.settings.reminder_enabled);
104+
this.toggle_reminder_menu.setToggleState(this.settings.reminder_enabled)
105105
}
106106

107107

108108
// Panel Updating -------------------------------------------------------------------------------------------------
109109
fetch() {
110-
let body = {
110+
let body = {
111111
api_key: this.settings.api_key,
112112
action: this.settings.fetch_action,
113113
}
114-
sendPostRequest(this.full_url, body, Lang.bind(this,
114+
sendPostRequest(this.full_url, body, Lang.bind(this,
115115
(err, response) => {
116116
if (err) {
117117
this.response_data = {
@@ -121,17 +121,39 @@ class TriliumAPIApplet extends Applet.TextIconApplet {
121121
} else {
122122
this.response_data = JSON.parse(response)
123123
}
124-
Mainloop.timeout_add(1000 * this.settings.api_polling_interval, Lang.bind(this, this.fetch));
124+
Mainloop.timeout_add(1000 * this.settings.api_polling_interval, Lang.bind(this, this.fetch))
125125
this.update_panel()
126126
}
127-
));
127+
))
128128
}
129129

130-
update_panel(){
131-
if (this.response_data.text){
130+
update_panel() {
131+
if (this.response_data.text) {
132+
// Save settings if settings override recieved from API
133+
if (this.response_data.prefix_string) {
134+
this.settings.prefix_string = this.response_data.prefix_string
135+
}
136+
if (this.response_data.suffix_string) {
137+
this.settings.suffix_string = this.response_data.suffix_string
138+
}
139+
if (this.response_data.text_width) {
140+
this.settings.text_width = this.response_data.text_width
141+
}
142+
if (this.response_data.reminder_enabled) {
143+
this.settings.reminder_enabled = this.response_data.reminder_enabled
144+
}
145+
if (this.response_data.reminder_time) {
146+
this.settings.reminder_time = this.response_data.reminder_time
147+
}
148+
if (this.response_data.reminder_delay) {
149+
this.settings.reminder_delay = this.response_data.reminder_delay
150+
}
151+
if (this.response_data.reminder_color) {
152+
this.settings.reminder_color = this.response_data.reminder_color
153+
}
132154
let prefix = this.settings.prefix_string
133155
let suffix = this.settings.suffix_string
134-
this.set_applet_label(prefix + this.response_data.text.replaceAll('"', "").substring(0, this.settings.text_width) + suffix);
156+
this.set_applet_label(prefix + this.response_data.text.replaceAll('"', "").substring(0, this.settings.text_width) + suffix)
135157
} else {
136158
this.set_applet_label("")
137159
}
@@ -143,42 +165,42 @@ class TriliumAPIApplet extends Applet.TextIconApplet {
143165
}
144166

145167
update_reminder() {
146-
if (this.settings.reminder_enabled && this.response_data.text) {
147-
this.actor.style = "background-color:" + this.settings.reminder_color;
148-
if (this.settings.reminder_time >= 100){
149-
Mainloop.timeout_add(this.settings.reminder_time, Lang.bind(this, this.cancel_reminder_background));
168+
if (this.settings.reminder_enabled && this.response_data.text) {
169+
this.actor.style = "background-color:" + this.settings.reminder_color
170+
if (this.settings.reminder_time >= 100) {
171+
Mainloop.timeout_add(this.settings.reminder_time, Lang.bind(this, this.cancel_reminder_background))
150172
}
151-
Mainloop.timeout_add((this.settings.reminder_delay * 1000), Lang.bind(this, this.update_reminder));
152-
} else {
153-
Mainloop.timeout_add(1000, Lang.bind(this, this.update_reminder));
173+
Mainloop.timeout_add((this.settings.reminder_delay * 1000), Lang.bind(this, this.update_reminder))
174+
} else {
175+
Mainloop.timeout_add(1000, Lang.bind(this, this.update_reminder))
154176
}
155177
}
156178

157179
cancel_reminder_background() {
158-
this.actor.style = "";
180+
this.actor.style = ""
159181
}
160182

161183
// Opening Trilium ------------------------------------------------------------------------------------------------
162-
on_applet_clicked(){
184+
on_applet_clicked() {
163185
if (this.settings.open_on_click_toggle) {
164186
let commandLine = this.settings.open_command
165-
Util.spawnCommandLine(commandLine);
187+
Util.spawnCommandLine(commandLine)
166188
}
167-
let body = {
189+
let body = {
168190
api_key: this.settings.api_key,
169191
action: this.settings.click_action,
170192
onclick_data: this.response_data.onclick_data
171193
}
172-
sendPostRequest(this.full_url, body, Lang.bind(this,
194+
sendPostRequest(this.full_url, body, Lang.bind(this,
173195
(err, response) => {
174196
if (err) {
175-
global.logError("POST request failed: " + err.message);
197+
global.logError("POST request failed: " + err.message)
176198
}
177199
}
178-
));
200+
))
179201
}
180202
}
181203

182204
function main(metadata, orientation, panel_height, instanceId) {
183-
return new TriliumAPIApplet (metadata, orientation, panel_height, instanceId)
205+
return new TriliumAPIApplet(metadata, orientation, panel_height, instanceId)
184206
}

0 commit comments

Comments
 (0)