forked from philipphoffmann/gnome3-jenkins-indicator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prefs.js
313 lines (227 loc) · 13.4 KB
/
prefs.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*
* @author Philipp Hoffmann
*/
const Lang = imports.lang;
const Gtk = imports.gi.Gtk;
const Me = imports.misc.extensionUtils.getCurrentExtension();
const Convenience = Me.imports.lib.convenience;
const Settings = Me.imports.src.settings;
const _ = imports.gettext.domain(Me.metadata['gettext-domain']).gettext;
let settings, settingsJSON;
function init() {
Convenience.initTranslations();
settings = Convenience.getSettings();
settingsJSON = Settings.getSettingsJSON(settings);
}
// builds a line (icon + label + switch) for a setting
function buildIconSwitchSetting(icon, label, setting_name, server_num) {
let hboxFilterJobs = new Gtk.Box({orientation: Gtk.Orientation.HORIZONTAL});
let iconFilterJobs = new Gtk.Image({file: Me.dir.get_path() + "/icons/prefs/" + icon + ".png"});
let labelFilterJobs = new Gtk.Label({label: label, xalign: 0});
let inputFilterJobs = new Gtk.Switch({active: settingsJSON['servers'][server_num][setting_name]});
inputFilterJobs.connect("notify::active", Lang.bind(this, function(input){ updateServerSetting(server_num, setting_name, input.get_active()); }));
hboxFilterJobs.pack_start(iconFilterJobs, false, false, 0);
hboxFilterJobs.pack_start(labelFilterJobs, true, true, 0);
hboxFilterJobs.add(inputFilterJobs);
return hboxFilterJobs;
}
// update json settings for server in settings schema
function updateServerSetting(server_num, setting, value) {
settingsJSON = Settings.getSettingsJSON(settings);
settingsJSON["servers"][server_num][setting] = value;
settings.set_string("settings-json", JSON.stringify(settingsJSON));
}
// create a new server tab and add it to the notebook
function addTabPanel(notebook, server_num) {
// use server name as tab label
let tabLabel = new Gtk.Label({ label: settingsJSON['servers'][server_num]['name']});
let vbox = new Gtk.Box({ orientation: Gtk.Orientation.VERTICAL, border_width: 10 });
// *** jenkins connection ***
let labelJenkinsConnection = new Gtk.Label({ label: "<b>" + _("Jenkins connection") + "</b>", use_markup: true, xalign: 0 });
vbox.add(labelJenkinsConnection);
let vboxJenkinsConnection = new Gtk.Box({ orientation: Gtk.Orientation.VERTICAL, margin_left: 20, margin_bottom: 15 });
// server name
let hboxServerName = new Gtk.Box({orientation: Gtk.Orientation.HORIZONTAL});
let labelServerName = new Gtk.Label({label: _("Jenkins CI Server name"), xalign: 0});
let inputServerName = new Gtk.Entry({ hexpand: true, text: settingsJSON['servers'][server_num]['name'] });
inputServerName.connect("changed", Lang.bind(this, function(input){ tabLabel.set_text(input.text); updateServerSetting(server_num, "name", input.text); }));
hboxServerName.pack_start(labelServerName, true, true, 0);
hboxServerName.add(inputServerName);
vboxJenkinsConnection.add(hboxServerName);
// jenkins url
let hboxJenkinsUrl = new Gtk.Box({orientation: Gtk.Orientation.HORIZONTAL});
let labelJenkinsUrl = new Gtk.Label({label: _("Jenkins CI Server web frontend URL"), xalign: 0});
let inputJenkinsUrl = new Gtk.Entry({ hexpand: true, text: settingsJSON['servers'][server_num]['jenkins_url'] });
inputJenkinsUrl.connect("changed", Lang.bind(this, function(input){ updateServerSetting(server_num, "jenkins_url", input.text); }));
hboxJenkinsUrl.pack_start(labelJenkinsUrl, true, true, 0);
hboxJenkinsUrl.add(inputJenkinsUrl);
vboxJenkinsConnection.add(hboxJenkinsUrl);
// use authentication
let hboxUseAuthentication = new Gtk.Box({orientation: Gtk.Orientation.HORIZONTAL});
let labelUseAuthentication = new Gtk.Label({label: _("Use authentication"), xalign: 0});
let inputUseAuthentication = new Gtk.Switch({active: settingsJSON['servers'][server_num]['use_authentication']});
inputUseAuthentication.connect("notify::active", Lang.bind(this, function(input){
updateServerSetting(server_num, 'use_authentication', input.get_active());
inputApiToken.set_editable(input.get_active());
inputAuthUser.set_editable(input.get_active());
}));
hboxUseAuthentication.pack_start(labelUseAuthentication, true, true, 0);
hboxUseAuthentication.add(inputUseAuthentication);
vboxJenkinsConnection.add(hboxUseAuthentication);
// user to authenticate
let hboxAuthUser = new Gtk.Box({orientation: Gtk.Orientation.HORIZONTAL});
let labelAuthUser = new Gtk.Label({label: _("Authentication user"), xalign: 0});
let inputAuthUser = new Gtk.Entry({ editable: inputUseAuthentication.get_active(), hexpand: true, text: settingsJSON['servers'][server_num]['auth_user'] });
inputAuthUser.connect("changed", Lang.bind(this, function(input){ updateServerSetting(server_num, "auth_user", input.text); }));
hboxAuthUser.pack_start(labelAuthUser, true, true, 0);
hboxAuthUser.add(inputAuthUser);
vboxJenkinsConnection.add(hboxAuthUser);
// api token
let hboxApiToken = new Gtk.Box({orientation: Gtk.Orientation.HORIZONTAL});
let labelApiToken = new Gtk.Label({label: _("Authentication API token"), xalign: 0});
let inputApiToken = new Gtk.Entry({ editable: inputUseAuthentication.get_active(), hexpand: true, text: settingsJSON['servers'][server_num]['api_token'] });
inputApiToken.connect("changed", Lang.bind(this, function(input){ updateServerSetting(server_num, "api_token", input.text); }));
hboxApiToken.pack_start(labelApiToken, true, true, 0);
hboxApiToken.add(inputApiToken);
vboxJenkinsConnection.add(hboxApiToken);
// green balls plugin
vboxJenkinsConnection.add(buildIconSwitchSetting("green", _("'Green Balls' plugin"), 'green_balls_plugin', server_num));
vbox.add(vboxJenkinsConnection);
// *** auto-refresh ***
let labelPreferences = new Gtk.Label({ label: "<b>" + _("Auto-refresh") + "</b>", use_markup: true, xalign: 0 });
vbox.add(labelPreferences);
let vboxAutoRefresh = new Gtk.Box({ orientation: Gtk.Orientation.VERTICAL, margin_left: 20, margin_bottom: 15 });
// auto refresh
let hboxAutoRefresh = new Gtk.Box({orientation: Gtk.Orientation.HORIZONTAL});
let labelAutoRefresh = new Gtk.Label({label: _("Auto-refresh"), xalign: 0});
let inputAutoRefresh = new Gtk.Switch({active: settingsJSON['servers'][server_num]['autorefresh']});
inputAutoRefresh.connect("notify::active", Lang.bind(this, function(input){
updateServerSetting(server_num, 'autorefresh', input.get_active());
//inputAutorefreshInterval.set_editable(input.get_active());
}));
hboxAutoRefresh.pack_start(labelAutoRefresh, true, true, 0);
hboxAutoRefresh.add(inputAutoRefresh);
vboxAutoRefresh.add(hboxAutoRefresh);
// auto refresh interval
let hboxAutorefreshInterval = new Gtk.Box({orientation: Gtk.Orientation.HORIZONTAL});
let labelAutorefreshInterval = new Gtk.Label({label: _("Auto-refresh interval (seconds)"), xalign: 0});
// had to replace the spinbutton since the change event is not triggered if the value is change by key presses
//let inputAutorefreshInterval = new Gtk.SpinButton({ numeric: true, adjustment: new Gtk.Adjustment({value: settings.get_int("autorefresh-interval"), lower: 1, upper: 86400, step_increment: 1}) });
let inputAutorefreshInterval = new Gtk.Scale({orientation: Gtk.Orientation.HORIZONTAL, adjustment: new Gtk.Adjustment({lower: 1, upper: 600, step_increment: 1})});
inputAutorefreshInterval.set_value(settingsJSON['servers'][server_num]['autorefresh_interval']);
inputAutorefreshInterval.set_size_request(200, -1);
// this doesnt work for a slider
//inputAutorefreshInterval.set_editable(inputAutoRefresh.get_active());
inputAutorefreshInterval.connect("value_changed", Lang.bind(inputAutorefreshInterval, function(){ updateServerSetting(server_num, 'autorefresh_interval', this.get_value()); }));
hboxAutorefreshInterval.pack_start(labelAutorefreshInterval, true, true, 0);
hboxAutorefreshInterval.add(inputAutorefreshInterval);
vboxAutoRefresh.add(hboxAutorefreshInterval);
vbox.add(vboxAutoRefresh);
// *** notifications ***
let labelNotifications = new Gtk.Label({ label: "<b>" + _("Notifications") + "</b>", use_markup: true, xalign: 0 });
vbox.add(labelNotifications);
let vboxNotifications = new Gtk.Box({ orientation: Gtk.Orientation.VERTICAL, margin_left: 20, margin_bottom: 15 });
// notification for finished jobs
let hboxNotificationFinishedJobs = new Gtk.Box({orientation: Gtk.Orientation.HORIZONTAL});
let labelNotificationFinishedJobs = new Gtk.Label({label: _("Notification for finished jobs"), xalign: 0});
let inputNotificationFinishedJobs = new Gtk.Switch({active: settingsJSON['servers'][server_num]['notification_finished_jobs']});
inputNotificationFinishedJobs.connect("notify::active", Lang.bind(this, function(input){
updateServerSetting(server_num, 'notification_finished_jobs', input.get_active());
}));
hboxNotificationFinishedJobs.pack_start(labelNotificationFinishedJobs, true, true, 0);
hboxNotificationFinishedJobs.add(inputNotificationFinishedJobs);
vboxNotifications.add(hboxNotificationFinishedJobs);
// stack notifications
let hboxStackNotifications = new Gtk.Box({orientation: Gtk.Orientation.HORIZONTAL});
let labelStackNotifications = new Gtk.Label({label: _("Stack notifications in message tray"), xalign: 0});
let inputStackNotifications = new Gtk.Switch({active: settingsJSON['servers'][server_num]['stack_notifications']});
inputStackNotifications.connect("notify::active", Lang.bind(this, function(input){
updateServerSetting(server_num, 'stack_notifications', input.get_active());
}));
hboxStackNotifications.pack_start(labelStackNotifications, true, true, 0);
hboxStackNotifications.add(inputStackNotifications);
vboxNotifications.add(hboxStackNotifications);
vbox.add(vboxNotifications);
// *** job filters ***
let labelFilters = new Gtk.Label({ label: "<b>" + _("Job filters") + "</b>", use_markup: true, xalign: 0 });
vbox.add(labelFilters);
let vboxFilters = new Gtk.Box({ orientation: Gtk.Orientation.VERTICAL, margin_left: 20, margin_bottom: 15 });
// show running jobs
vboxFilters.add(buildIconSwitchSetting("clock", _('Show running jobs'), 'show_running_jobs', server_num));
// show successful jobs
vboxFilters.add(buildIconSwitchSetting("blue", _('Show successful jobs'), 'show_successful_jobs', server_num));
// show unstable jobs
vboxFilters.add(buildIconSwitchSetting("yellow", _('Show unstable jobs'), 'show_unstable_jobs', server_num));
// show failed jobs
vboxFilters.add(buildIconSwitchSetting("red", _('Show failed jobs'), 'show_failed_jobs', server_num));
// show disabled jobs
vboxFilters.add(buildIconSwitchSetting("grey", _('Show never built jobs'), 'show_neverbuilt_jobs', server_num));
// show disabled jobs
vboxFilters.add(buildIconSwitchSetting("grey", _('Show disabled jobs'), 'show_disabled_jobs', server_num));
// show aborted jobs
vboxFilters.add(buildIconSwitchSetting("grey", _('Show aborted jobs'), 'show_aborted_jobs', server_num));
// Jobs to show
let hboxJobsToShow = new Gtk.Box({orientation: Gtk.Orientation.HORIZONTAL});
let labelJobsToShow = new Gtk.Label({label: _("Jobs to show"), xalign: 0});
let inputJobsToShow = new Gtk.Entry({ editable: true, hexpand: true, text: settingsJSON['servers'][server_num]['jobs_to_show'] });
inputJobsToShow.connect("changed", Lang.bind(this, function(input){ updateServerSetting(server_num, "jobs_to_show", input.text); }));
hboxJobsToShow.pack_start(labelJobsToShow, true, true, 0);
hboxJobsToShow.add(inputJobsToShow);
vboxFilters.add(hboxJobsToShow);
vbox.add(vboxFilters);
// button to remove tab
let iconRemoveServer = new Gtk.Image({file: Me.dir.get_path() + "/icons/prefs/stop.png"});
let btnRemoveServer = new Gtk.Button({image: iconRemoveServer});
btnRemoveServer.connect('clicked', Lang.bind(notebook, function(){
if( notebook.get_n_pages()>1 ) {
// remove server from settings
settingsJSON['servers'].splice(notebook.page_num(tabContent), 1);
settings.set_string("settings-json", JSON.stringify(settingsJSON));
// remove tab from notebook
notebook.remove_page(notebook.page_num(tabContent));
}
}));
// widget for tab containing label and close button
let tabWidget = new Gtk.HBox({ spacing: 5 });
tabWidget.add(tabLabel);
tabWidget.add(btnRemoveServer);
tabWidget.show_all();
// tab content
let tabContent = new Gtk.ScrolledWindow({ vexpand: true });
tabContent.add_with_viewport(vbox);
// append tab to notebook
notebook.append_page(tabContent, tabWidget);
}
function buildPrefsWidget() {
// *** tab panel ***
let notebook = new Gtk.Notebook();
for( let i=0 ; i<settingsJSON['servers'].length ; ++i ) {
// add tab panels for each server
addTabPanel(notebook, i);
}
// button to add new servers
let btnNewServer = new Gtk.Button({label: _('Add server')});
btnNewServer.connect('clicked', Lang.bind(notebook, function(){
// get default settings for this new server
settingsJSON['servers'][settingsJSON['servers'].length] = Settings.DefaultSettings['servers'][0];
// set new id
let currentDate = new Date;
settingsJSON['servers'][settingsJSON['servers'].length-1]['id'] = currentDate.getTime();
// save new settings
settings.set_string("settings-json", JSON.stringify(settingsJSON));
// add tab with copied settings
addTabPanel(notebook, settingsJSON['servers'].length-1);
notebook.show_all();
// jump to added tab
notebook.set_current_page(settingsJSON['servers'].length-1);
}));
// *** overall frame ***
let frame = new Gtk.Box({ orientation: Gtk.Orientation.VERTICAL, border_width: 10 });
// add new server button
frame.add(btnNewServer);
// add notebook
frame.add(notebook);
// show the frame
frame.show_all();
return frame;
}