forked from deinstapel/cpupower
-
Notifications
You must be signed in to change notification settings - Fork 1
/
extension.js
130 lines (117 loc) · 4.99 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
/*
*
* CPUPower for GNOME Shell preferences
* - Creates a widget to set the preferences of the cpupower extension
*
* Copyright (C) 2015
* Martin Koppehel <martin.koppehel@st.ovgu.de>
*
* This file is part of the gnome-shell extension cpupower.
*
* gnome-shell extension cpupower 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 3 of the License, or (at your option)
* any later version.
*
* gnome-shell extension cpupower 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 gnome-shell extension cpupower. If not, see
* <http://www.gnu.org/licenses/>.
*
*/
import * as Main from 'resource:///org/gnome/shell/ui/main.js';
import * as Config from 'resource:///org/gnome/shell/misc/config.js';
import {Extension} from 'resource:///org/gnome/shell/extensions/extension.js';
import * as utils from './src/utils.js';
import {checkInstalled} from './src/utils.js';
import * as notinstalled from './src/notinstalled.js';
import * as update from './src/update.js';
import * as indicator from './src/indicator.js';
import Gio from 'gi://Gio';
import GLib from 'gi://GLib';
const EXTENSIONDIR = import.meta.url.substr('file://'.length, import.meta.url.lastIndexOf('/') - 'file://'.length);
export default class CPUPowerExtension extends Extension {
constructor(...args) {
super(...args);
this.indicatorInstance = null;
this.cpupowerProxy = null;
this.extensionReloadSignalHandler = null;
}
enableIndicator(instance) {
Main.panel.addToStatusArea("cpupower", instance.mainButton);
instance.enable();
}
/* exported enable */
enable() {
const interfaceBinary = GLib.file_get_contents(`${EXTENSIONDIR}/schemas/io.github.martin31821.cpupower.dbus.xml`)[1];
let decoder = new TextDecoder('utf-8');
let interfaceXml = decoder.decode(interfaceBinary);
const CpupowerProxy = Gio.DBusProxy.makeProxyWrapper(interfaceXml);
this.cpupowerProxy = new CpupowerProxy(
Gio.DBus.session,
"io.github.martin31821.cpupower",
"/io/github/martin31821/cpupower",
);
this.extensionReloadSignalHandler = this.cpupowerProxy.connectSignal("ExtensionReloadRequired", () => {
log("Reloading cpupower");
this.disable();
this.enable();
});
try {
checkInstalled((installed, exitCode) => {
if (!installed) {
switch (exitCode) {
case utils.INSTALLER_NEEDS_UPDATE:
this.indicatorInstance = new update.UpdateIndicator(update.UPDATE, function (success) {
if (success) {
// reenable the extension to allow immediate operation.
disable();
enable();
}
}, (inst) => this.enableIndicator(inst));
break;
case utils.INSTALLER_NEEDS_SECURITY_UPDATE:
this.indicatorInstance = new update.UpdateIndicator(update.SECURITY_UPDATE, function (success) {
if (success) {
// reenable the extension to allow immediate operation.
disable();
enable();
}
}, (inst) => this.enableIndicator(inst));
break;
default:
this.indicatorInstance = new notinstalled.NotInstalledIndicator(exitCode, function (success) {
if (success) {
// reenable the extension to allow immediate operation.
disable();
enable();
}
}, (inst) => this.enableIndicator(inst));
break;
}
} else {
this.indicatorInstance = new indicator.CPUFreqIndicator((inst) => this.enableIndicator(inst));
}
});
} catch (e) {
logError(e);
}
}
/* exported disable */
disable() {
if (this.indicatorInstance) {
this.indicatorInstance.disable();
this.indicatorInstance.destroy();
}
if (this.cpupowerProxy && this.extensionReloadSignalHandler) {
this.cpupowerProxy.disconnectSignal(this.extensionReloadSignalHandler);
this.cpupowerProxy = null;
this.extensionReloadSignalHandler = null;
}
}
}