-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplet.js
380 lines (325 loc) · 13.3 KB
/
applet.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
const Applet = imports.ui.applet;
const PopupMenu = imports.ui.popupMenu;
const Lang = imports.lang;
const Settings = imports.ui.settings;
const Gtk = imports.gi.Gtk;
const Homestead = imports.applet.homestead;
const Util = imports.applet.util;
const MessageTray = imports.ui.messageTray;
const Main = imports.ui.main;
const St = imports.gi.St;
const GLib = imports.gi.GLib;
const UUID = "rancher@centurix";
const HOMESTEAD_PROJECT_FOLDER = "~/Homestead";
const HOMESTEAD_CONFIG_FOLDER = "~/.homestead";
const VAGRANT_CMD = '/usr/bin/vagrant';
const EDITOR = '/usr/bin/xed';
const APPLET_FOLDER = global.userdatadir + "/applets/rancher@centurix/";
const ICON_UP = APPLET_FOLDER + "icons/laravel_up_128x128.png";
const ICON_DOWN = APPLET_FOLDER + "icons/laravel_down_128x128.png";
const ICON_MISSING = APPLET_FOLDER + "icons/laravel_missing_128x128.png";
/**
* Applet manager
**/
function Rancher(metadata, orientation, panelHeight, instanceId) {
this.settings = new Settings.AppletSettings(this, UUID, instanceId);
this._init(orientation, panelHeight, instanceId);
}
Rancher.prototype = {
__proto__: Applet.IconApplet.prototype,
_init: function(orientation, panelHeight, instanceId) {
Applet.IconApplet.prototype._init.call(this, orientation, panelHeight, instanceId);
try {
this.menu = new Applet.AppletPopupMenu(this, orientation);
this._menuManager.addMenu(this.menu);
this._msgsrc = new MessageTray.SystemNotificationSource("Rancher");
Main.messageTray.add(this._msgsrc);
this.homestead_project_folder = HOMESTEAD_PROJECT_FOLDER;
this.homestead_config_folder = HOMESTEAD_CONFIG_FOLDER;
this.vagrant_cmd = VAGRANT_CMD;
this.editor = EDITOR;
this.settings.bindProperty(
Settings.BindingDirection.IN,
"homesteadProjectFolder",
"homestead_project_folder",
this.onProjectFolderUpdate,
null
);
this.settings.bindProperty(
Settings.BindingDirection.IN,
"homesteadConfigFolder",
"homestead_config_folder",
this.onConfigFolderUpdate,
null
);
this.settings.bindProperty(
Settings.BindingDirection.IN,
"vagrantCmd",
"vagrant_cmd",
this.onVagrantCmdUpdate,
null
);
this.settings.bindProperty(
Settings.BindingDirection.IN,
"editor",
"editor",
this.onEditorUpdate,
null
);
this.settingsApiCheck();
this.homestead = new Homestead.Homestead(
this.homestead_project_folder,
this.homestead_config_folder,
this.vagrant_cmd,
this.editor
);
this.refreshApplet();
} catch (e) {
global.log(UUID + "::_init: " + e);
}
},
onProjectFolderUpdate: function() {
this.homestead.setProjectFolder(this.homestead_project_folder);
},
onConfigFolderUpdate: function() {
this.homestead.setConfigFolder(this.homestead_config_folder);
},
onVagrantCmdUpdate: function() {
this.homestead.setVagrantCmd(this.vagrant_cmd);
},
onEditorUpdate: function() {
this.homestead.setEditor(this.editor);
},
newIconMenuItem: function(icon, label, callback, options = {}) {
try {
let newItem = new PopupMenu.PopupIconMenuItem(label, icon, St.IconType.FULLCOLOR, options);
if (callback) {
newItem.connect("activate", Lang.bind(this, callback));
}
return newItem;
} catch(e) {
global.log(UUID + "::newIconMenuItem: " + e);
}
},
newMenuItem: function(label, callback, options = {}) {
let newItem = new PopupMenu.PopupMenuItem(label, options);
if (callback) {
newItem.connect("activate", Lang.bind(this, callback));
}
return newItem;
},
newSwitchMenuItem: function(label, state, callback) {
let newItem = new PopupMenu.PopupSwitchMenuItem(label, state);
if (callback) {
newItem.connect("activate", Lang.bind(this, callback));
}
return newItem;
},
newSeparator: function() {
return new PopupMenu.PopupSeparatorMenuItem();
},
settingsApiCheck: function() {
const Config = imports.misc.config;
const SETTINGS_API_MIN_VERSION = 2;
const CMD_SETTINGS = "cinnamon-settings applets " + UUID;
let cinnamonVersion = Config.PACKAGE_VERSION.split('.');
let majorVersion = parseInt(cinnamonVersion[0]);
if (majorVersion >= SETTINGS_API_MIN_VERSION) {
return;
}
let mi = new Applet.MenuItem(_("Settings"), Gtk.STOCK_EDIT, Lang.bind(this, function() {
Util.spawnCommandLine(CMD_SETTINGS)
}));
this._applet_context_menu.addMenuItem(mi);
},
on_applet_clicked: function(event) {
try {
if (!this.menu.isOpen) {
this.menu.toggle();
}
} catch(e) {
global.log(UUID + '::on_applet_clicked: ' + e);
}
},
editHomestead: function() {
this.homestead.edit();
this.notification(_("Editing Homestead configuration..."));
},
homesteadToggle: function(event) {
try {
if (event._switch.state) {
this.transitionMenu(_("Rancher: Bringing Homestead up, please wait..."));
this.homestead.up(Lang.bind(this, this.refreshApplet));
this.notification(_("Bringing Homestead up..."));
return true;
}
this.transitionMenu(_("Rancher: Taking Homestead down, please wait..."));
this.homestead.halt(Lang.bind(this, this.refreshApplet));
this.notification(_("Taking Homestead down..."));
} catch(e) {
global.log(UUID + '::homesteadToggle: ' + e);
}
},
homesteadProvision: function() {
this.transitionMenu(_("Rancher: Provisioning Homestead, please wait..."));
this.homestead.provision(Lang.bind(this, this.refreshApplet));
this.notification(_("Provisioning Homestead..."));
},
homesteadDestroy: function() {
this.transitionMenu(_("Rancher: Destroying Homestead, please wait..."));
this.homestead.destroy(Lang.bind(this, this.refreshApplet));
this.notification(_("Destroying Homestead..."));
},
homesteadSuspend: function() {
this.transitionMenu(_("Rancher: Suspending Homestead, please wait..."));
this.homestead.suspend(Lang.bind(this, this.refreshApplet));
this.notification(_("Suspending Homestead..."));
},
homesteadSSH: function() {
this.homestead.ssh();
this.notification(_("Homestead SSH Terminal opened"));
},
homesteadRecompile: function() {
this.transitionMenu(_("Rancher: Recompiling Kernel, please wait..."), true);
this.homestead.recompile(Lang.bind(this, this.refreshApplet));
this.notification(_("Recompiling Kernel..."));
},
refreshApplet: function() {
this.homestead.checkStatus(Lang.bind(this, this.updateApplet));
},
notification: function(message) {
let notification = new MessageTray.Notification(this._msgsrc, "Rancher", message);
notification.setTransient(true);
this._msgsrc.notify(notification);
},
transitionMenu: function(message, refresh = false) {
this.set_applet_icon_path(ICON_DOWN);
this.set_applet_tooltip(message);
this.menu.removeAll();
this.menu.addMenuItem(this.newIconMenuItem('dialog-information', message, null, {reactive: false}));
if (refresh) {
this.menu.addMenuItem(this.newIconMenuItem('view-refresh', _('Refresh this menu'), this.refreshApplet));
}
},
openBrowser: function(url) {
matches = (new RegExp('\\("(.*?)"\\)')).exec(url);
if (matches && matches.length > 0) {
Main.Util.spawnCommandLine("xdg-open http://" + matches[1]);
}
},
openHomesteadGithub: function() {
Main.Util.spawnCommandLine("xdg-open http://laravel.com/docs/homestead");
},
editHosts: function() {
Main.Util.spawnCommandLine("gksudo " + this.editor + " /etc/hosts");
},
updateApplet: function(exists, status) {
try {
text_status = "";
if (!exists) {
this.set_applet_icon_path(ICON_MISSING);
this.set_applet_tooltip(_("Rancher: Homestead missing or not configured."));
this.notification(_("Homestead missing or not configured."));
}
this.set_applet_icon_path(ICON_DOWN);
if (status == Homestead.STATUS_KERNAL_NOT_LOADED) {
this.set_applet_icon_path(ICON_MISSING);
this.set_applet_tooltip(_("Kernel Module not loaded. Needs recompilation."));
this.notification(_("Kernel Module not loaded. Needs recompilation."));
}
if (status == Homestead.STATUS_RUNNING) {
this.set_applet_icon_path(ICON_UP);
this.set_applet_tooltip(_("Rancher: Homestead up."));
this.notification(_("Homestead up."));
text_status = _(" (Running)");
}
if (status == Homestead.STATUS_SAVED) {
this.set_applet_tooltip(_("Rancher: Homestead suspended."));
this.notification(_("Homestead suspended."));
text_status = _(" (Suspended)");
}
if (status == Homestead.STATUS_POWER_OFF) {
this.set_applet_tooltip(_("Rancher: Homestead down."));
this.notification(_("Homestead down."));
text_status = _(" (Down)");
}
if (status == Homestead.STATUS_NOT_CREATED) {
this.set_applet_tooltip(_("Rancher: Homestead not created."));
this.notification(_("Homestead not created"));
text_status = _(" (Not created/Destroyed)");
}
if (status == Homestead.STATUS_HOMESTEAD_MISSING) {
this.set_applet_tooltip(_("Rancher: Homestead not installed."));
this.notification(_("Homestead not installed"));
text_status = _(" (Not created/Destroyed)");
}
this.menu.removeAll();
if (!exists) {
if (status == Homestead.STATUS_HOMESTEAD_MISSING) {
this.menu.addMenuItem(this.newIconMenuItem('apport', _('Homestead not installed'), null, {reactive: false}));
this.menu.addMenuItem(this.newIconMenuItem('emblem-web', _('Click here for installation instructions'), this.openHomesteadGithub));
this.menu.addMenuItem(this.newIconMenuItem('view-refresh', _('Refresh this menu'), this.refreshApplet));
} else {
this.menu.addMenuItem(this.newIconMenuItem('apport', _('Homestead missing or not configured'), null, {reactive: false}));
this.menu.addMenuItem(this.newIconMenuItem('view-refresh', _('Refresh this menu'), this.refreshApplet));
}
}
if (status == Homestead.STATUS_KERNAL_NOT_LOADED) {
this.menu.addMenuItem(this.newIconMenuItem('apport', _('Kernel Module not loaded. Needs recompilation.'), null, {reactive: false}));
this.menu.addMenuItem(this.newIconMenuItem('system-run', _('Recompile Kernel Module.'), this.homesteadRecompile));
return false;
}
this.menu.addMenuItem(this.newSwitchMenuItem(_('Status') + text_status, (status == Homestead.STATUS_RUNNING), this.homesteadToggle));
this.menu.addMenuItem(this.newSeparator());
if (status == Homestead.STATUS_RUNNING) {
this.menu.addMenuItem(this.newIconMenuItem('system-run', _('Run provisioning'), this.homesteadProvision));
this.menu.addMenuItem(this.newIconMenuItem('media-playback-pause', _('Suspend Homestead'), this.homesteadSuspend));
this.menu.addMenuItem(this.newIconMenuItem('utilities-terminal', _('SSH Terminal...'), this.homesteadSSH));
this.menu.addMenuItem(this.newSeparator());
}
if (status != Homestead.STATUS_NOT_CREATED) {
this.menu.addMenuItem(this.newIconMenuItem('list-remove', _('Destroy Homestead'), this.homesteadDestroy));
}
if (exists) {
this.menu.addMenuItem(this.newSeparator());
config = this.homestead.parseConfig();
this.subMenuConfig = new PopupMenu.PopupSubMenuMenuItem(_('Configuration'));
this.subMenuConfig.menu.addMenuItem(this.newIconMenuItem('package_network', _('IP: ') + config.ip, null, {reactive: false}));
this.subMenuConfig.menu.addMenuItem(this.newIconMenuItem('media-memory', _('Memory: ') + config.memory, null, {reactive: false}));
this.subMenuConfig.menu.addMenuItem(this.newIconMenuItem('applications-electronics', _('CPU: ') + config.cpu, null, {reactive: false}));
this.subMenuConfig.menu.addMenuItem(this.newIconMenuItem('virtualbox', _('Provider: ') + config.provider, null, {reactive: false}));
this.subMenuConfig.menu.addMenuItem(this.newSeparator());
this.subMenuConfig.menu.addMenuItem(this.newIconMenuItem('accessories-text-editor', _('Edit Homestead configuration...'), this.editHomestead));
this.menu.addMenuItem(this.subMenuConfig);
this.subMenuSites = new PopupMenu.PopupSubMenuMenuItem(_('Hosted Sites') + ' (' + config.sites.length + ')');
this.subMenuSites.menu.addMenuItem(this.newSeparator());
for (var index = 0; index < config.sites.length; index++) {
if (status == Homestead.STATUS_RUNNING) {
this.subMenuSites.menu.addMenuItem(this.newIconMenuItem('emblem-web', config.sites[index], this.openBrowser));
} else {
this.subMenuSites.menu.addMenuItem(this.newIconMenuItem('emblem-web', config.sites[index] + " (down)", null, {reactive: false}));
}
}
this.subMenuSites.menu.addMenuItem(this.newSeparator());
this.subMenuSites.menu.addMenuItem(this.newIconMenuItem('accessories-text-editor', _('Edit hosts file...'), this.editHosts));
this.menu.addMenuItem(this.subMenuSites);
this.subMenuDatabases = new PopupMenu.PopupSubMenuMenuItem(_('Hosted Databases') + ' (' + config.databases.length + ')');
for (var index = 0; index < config.databases.length; index++) {
if (status == Homestead.STATUS_RUNNING) {
this.subMenuDatabases.menu.addMenuItem(this.newIconMenuItem('drive-harddisk', config.databases[index], null, {reactive: false}));
} else {
this.subMenuDatabases.menu.addMenuItem(this.newIconMenuItem('drive-harddisk', config.databases[index] + " (down)", null, {reactive: false}));
}
}
this.menu.addMenuItem(this.subMenuDatabases);
}
this.menu.addMenuItem(this.newSeparator());
this.menu.addMenuItem(this.newIconMenuItem('view-refresh', _('Refresh this menu'), this.refreshApplet));
} catch(e) {
global.log(UUID + "::updateMenu: " + e);
}
}
}
function main(metadata, orientation, panelHeight, instanceId) {
return new Rancher(metadata, orientation, panelHeight, instanceId);
}