-
Notifications
You must be signed in to change notification settings - Fork 1
/
extension.js
352 lines (280 loc) · 13.6 KB
/
extension.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/** Author: Eimji (Patrick)
/** Email: [email protected]
/**
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
**/
'use strict';
const Lang = imports.lang;
const St = imports.gi.St;
const Main = imports.ui.main;
//const Util = imports.misc.util;
const Gio = imports.gi.Gio;
const GLib = imports.gi.GLib;
const PanelMenu = imports.ui.panelMenu;
const PopupMenu = imports.ui.popupMenu;
const Panel = imports.ui.panel;
const Config = imports.misc.config;
const ExtensionUtils = imports.misc.extensionUtils;
const MyExtension = ExtensionUtils.getCurrentExtension();
const ScrollablePopupMenu = MyExtension.imports.scrollablePopupMenu.ScrollablePopupMenu;
const ProxiesMenuItem = MyExtension.imports.proxiesMenuItem.ProxiesMenuItem;
const EditProxyDialog = MyExtension.imports.editProxyDialog;
const ConfirmDialog = MyExtension.imports.confirmDialog;
const Settings = MyExtension.imports.settings;
const SCHEMA_PATH = 'org.gnome.shell.extensions.scriptproxies';
const ACTIVE_PROXY = 'active-proxy';
const DEFAULT_EDITOR = 'default-editor';
const DEVELOPMENT = 'in-development';
const DisabledIcon = 'set-proxies-off-symbolic';
const EnabledIcon = 'set-proxies-on-symbolic';
const EditIcon = 'system-run-symbolic';
const AppDir = GLib.build_filenamev([global.userdatadir, 'extensions/[email protected]']);
//const HomeDir = GLib.get_home_dir();
const Gettext = imports.gettext;
const _ = Gettext.domain('gnome-shell-extensions-scriptproxies').gettext;
let _setproxies;
function strStartsWith(str, prefix) {
return str.indexOf(prefix) === 0;
}
function strEndsWith(str, suffix) {
return str.match(suffix + '$') == suffix;
}
const SetProxies = new Lang.Class({
Name: 'SetProxies',
Extends: PanelMenu.Button,
_init: function() {
this.parent(0.0, 'Set Proxies');
// schema org.gnome.shell.extensions.scriptproxies for saving settings
this._settings = Settings.getSettings(SCHEMA_PATH);
this._activeProxy = this._settings.get_string(ACTIVE_PROXY);
this._defaultEditor = this._settings.get_string(DEFAULT_EDITOR);
this._inDevelopment = this._settings.get_boolean(DEVELOPMENT);
this._settings.connect("changed::" + ACTIVE_PROXY, Lang.bind(this, function() {
this._activeProxy = this._settings.get_string(ACTIVE_PROXY);
this._updateMenu();
}));
this._icon = new St.Icon({
icon_name: this._activeProxy == 'No proxy' ? DisabledIcon : EnabledIcon,
icon_size: 19,
style_class: 'setProxies-button'
});
this.actor.add_child(this._icon);
this.popupMenu = new ScrollablePopupMenu(this.actor, St.Align.START, St.Side.TOP);
this.setMenu(this.popupMenu);
let noProxyFile = GLib.build_filenamev([AppDir, 'unsetProxy.sh']);
if (!GLib.file_test(noProxyFile, GLib.FileTest.EXISTS)) {
GLib.file_set_contents(noProxyFile, '');
Main.Util.trySpawnCommandLine('chmod u+x ' + noProxyFile);
}
this._updateMenu();
},
destroy: function() {
this.menu.removeAll();
this.parent();
},
_updateMenu: function() {
this.menu.removeAll();
let app_dir = Gio.file_new_for_path(AppDir);
let files = app_dir.enumerate_children(Gio.FILE_ATTRIBUTE_STANDARD_NAME, Gio.FileQueryInfoFlags.NONE, null);
let file, fileName;
while ((file = files.next_file(null))) {
fileName = file.get_name();
// name of a proxy script should be started with "set_" and ended with ".sh":
// name format for the script: set_My_Proxy_Name.sh, e.g. set_Office_proxy.sh
// name for the proxy is then 'Office proxy'
if (strStartsWith(fileName, 'set_') && strEndsWith(fileName, '.sh')) {
let editButton = this._createButton(EditIcon);
let proxyName = fileName.replace('set_', '').replace('.sh', '').replace(/_/g, ' ');
let newProxy = new ProxiesMenuItem(proxyName);
newProxy.connect('activate', Lang.bind(this, function() {
this.menu.close();
if (this._activeProxy != proxyName)
try {
Main.Util.trySpawnCommandLine(AppDir + '/set_' + proxyName.replace(/ /g,'_') + '.sh');
Main.notify(proxyName + _(' enabled'));
this._settings.set_string(ACTIVE_PROXY, proxyName);
} catch (err) {
Main.notify(_('Error when enabling ') + proxyName);
}
else
Main.notify(proxyName + _(' is already enabled'));
}));
newProxy.setOrnament(this._activeProxy == proxyName ? PopupMenu.Ornament.CHECK : PopupMenu.Ornament.NONE);
editButton.connect('clicked', Lang.bind(this, function() {
this.menu.close();
this._launchEditProxyDialog('edit', proxyName);
}));
newProxy.actor.add(editButton);
this.menu.addMenuItem(newProxy);
}
}
let editNoProxyButton = this._createButton(EditIcon);
let noProxy = new ProxiesMenuItem(_('No proxy'));
noProxy.connect('activate', Lang.bind(this, function() {
this.menu.close();
if (this._activeProxy != 'No proxy')
try {
Main.Util.trySpawnCommandLine(AppDir + '/unsetProxy.sh');
Main.notify(_('Proxy disabled'));
this._settings.set_string(ACTIVE_PROXY, 'No proxy');
} catch (err) {
Main.notify(_('Error when disabling proxy'));
}
else
Main.notify(_('Proxy already disabled'));
}));
noProxy.setOrnament(this._activeProxy == 'No proxy' ? PopupMenu.Ornament.CHECK : PopupMenu.Ornament.NONE);
editNoProxyButton.connect('clicked', Lang.bind(this, function() {
try {
Main.Util.trySpawnCommandLine(this._defaultEditor + ' ' + AppDir + '/unsetProxy.sh');
} catch (err) {
Main.notify(_('Error: text editor ') + this._defaultEditor + _(' not installed'));
this._launchSetEditorDialog();
}
this.menu.close();
}));
noProxy.actor.add(editNoProxyButton);
this.menu.addMenuItem(noProxy);
this.menu.addMenuItem(new PopupMenu.PopupSeparatorMenuItem());
// TODO later: GUI for the configuration of the extension settings
// for the moment, open a popup window to add a new proxy
let addNewProxy = new PopupMenu.PopupMenuItem(_('New proxy'));
addNewProxy.connect('activate', Lang.bind(this, function() {
this._launchEditProxyDialog('add', '');
}));
this.menu.addMenuItem(addNewProxy);
this._icon.icon_name = this._activeProxy == 'No proxy' ? DisabledIcon : EnabledIcon;
// This is only for development, for my own use.
if (this._inDevelopment) {
let proxyOpenFolder = new PopupMenu.PopupMenuItem(_('Open the folder'));
proxyOpenFolder.connect('activate', Lang.bind(this, function() {
Gio.app_info_launch_default_for_uri(MyExtension.dir.get_uri(), null);
}));
this.menu.addMenuItem(proxyOpenFolder);
}
},
_launchEditProxyDialog: function(action, proxyName) {
let editProxyDialog = new EditProxyDialog.EditProxyDialog(Lang.bind(this, function(userEnteredProxyName, type) {
// in the extension folder, create a file named set_'User_entered_proxy_name'.sh
let newProxyFile = AppDir + '/set_' + userEnteredProxyName.replace(/ /g,'_') + '.sh';
let oldProxyFile = AppDir + '/set_' + proxyName.replace(/ /g,'_') + '.sh';
if (type == 'new') {
// add a new proxy
if (userEnteredProxyName) {
if (!GLib.file_test(newProxyFile, GLib.FileTest.EXISTS)) {
GLib.file_set_contents(newProxyFile, '');
//Main.Util.trySpawnCommandLine('touch ' + newProxyFile);
Main.Util.trySpawnCommandLine('chmod u+x ' + newProxyFile);
try {
Main.Util.trySpawnCommandLine(this._defaultEditor + ' ' + newProxyFile);
} catch (err) {
Main.notify(_('Error: text editor ') + this._defaultEditor + _(' not installed'));
this._launchSetEditorDialog();
}
this._updateMenu();
} else {
Main.notify(_('Error: the proxy name is already used'));
}
} else {
//user forgets to set a name for the new proxy
// push an alert to the user!
let alertDialog = new ConfirmDialog.ConfirmDialog(_('Proxy title blank'), _('You forgot to set a name for your new proxy, do you want to start again?'), Lang.bind(this, function() {
this._launchEditProxyDialog('add', '');
}));
alertDialog.open();
}
} else if (type == 'rename') {
// rename an existing proxy
if (userEnteredProxyName == '') {
if (this._activeProxy == proxyName) {
// proxy is in use
Main.notify(proxyName + _(' currently used, removal is not possible'));
} else {
// requires a confirmation from user before removing
let confirmDialog = new ConfirmDialog.ConfirmDialog(_('Confirm removal'), _('Do you want to delete ') + proxyName + _('?'), Lang.bind(this, function() {
Main.Util.trySpawnCommandLine('rm ' + oldProxyFile);
this._updateMenu();
}));
confirmDialog.open();
}
} else {
if (newProxyFile != oldProxyFile) {
if (!GLib.file_test(newProxyFile, GLib.FileTest.EXISTS)) {
Main.Util.trySpawnCommandLine('mv ' + oldProxyFile + ' ' + newProxyFile);
if (this._activeProxy == proxyName)
// proxy is in use, update the schema
this._activeProxy = userEnteredProxyName;
this._updateMenu();
} else {
Main.notify(_('Error: the proxy name is already used'));
}
}
}
} else {
// here type should 'modify': modify the shell script of an existing proxy
try {
if (!strEndsWith(newProxyFile, 'set_.sh'))
Main.Util.trySpawnCommandLine(this._defaultEditor + ' ' + newProxyFile);
else
Main.Util.trySpawnCommandLine(this._defaultEditor + ' ' + oldProxyFile);
} catch (err) {
Main.notify(_('Error: text editor ') + this._defaultEditor + _(' not installed'));
this._launchSetEditorDialog();
}
}
}), action);
if (action == 'edit')
// edit a proxy
editProxyDialog.open(proxyName);
else
// action == 'add': add a new proxy
editProxyDialog.open('');
},
_launchSetEditorDialog: function() {
let editProxyDialog = new EditProxyDialog.EditProxyDialog(Lang.bind(this, function(userEnteredProxyName) {
this._settings.set_string(DEFAULT_EDITOR, userEnteredProxyName);
this._defaultEditor = userEnteredProxyName;
}), 'editor');
editProxyDialog.open('');
},
_createButton: function(icon) {
let button = new St.Button({
child: new St.Icon({
icon_name: icon,
icon_size: 15
}),
style_class: 'edit-button',
reactive: true,
y_align: St.Align.MIDDLE,
x_align: St.Align.END
});
return button;
}
});
function init(extensionMeta) {
let domain = MyExtension.metadata['gettext-domain'];
let localeDir = MyExtension.dir.get_child('locale');
if (localeDir.query_exists(null))
Gettext.bindtextdomain(domain, localeDir.get_path());
else
Gettext.bindtextdomain(domain, Config.LOCALEDIR);
let theme = imports.gi.Gtk.IconTheme.get_default();
theme.append_search_path(extensionMeta.path + '/icons');
}
function enable() {
_setproxies = new SetProxies();
Main.panel.addToStatusArea('set-proxies', _setproxies, 2);
}
function disable() {
_setproxies.destroy();
_setproxies = null;
}