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
|
/* extension.js
*
* 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/>.
*
* SPDX-License-Identifier: GPL-3.0-only
*/
/* exported init */
import GLib from 'gi://GLib';
import Gio from 'gi://Gio';
import Clutter from 'gi://Clutter';
import St from 'gi://St';
import Pango from 'gi://Pango';
import { panel } from 'resource:///org/gnome/shell/ui/main.js';
import * as PopupMenu from 'resource:///org/gnome/shell/ui/popupMenu.js';
import * as ModalDialog from 'resource:///org/gnome/shell/ui/modalDialog.js';
import { Extension, gettext as _, } from 'resource:///org/gnome/shell/extensions/extension.js';
const GRUB_CONFIG_PATH = "/boot/grub/grub.cfg";
export default class RebootToWindowsExtension extends Extension {
menu;
proxy;
rebootToWindowsItem;
counter;
seconds;
counterIntervalId;
messageIntervalId;
sourceId;
manager;
constructor(metadata) {
super(metadata);
}
modifySystemItem() {
this.menu =
panel.statusArea.quickSettings._system?.quickSettingsItems[0].menu;
// if(this.manager == null){
// return
// }
this.proxy = this.manager(Gio.DBus.system, 'org.freedesktop.login1', '/org/freedesktop/login1');
this.rebootToWindowsItem = new PopupMenu.PopupMenuItem(`${_('Restart to Windows')}...`);
this.rebootToWindowsItem.connect('activate', () => {
this.counter = 60;
this.seconds = this.counter;
const dialog = this.buildDialog();
dialog.open();
this.counterIntervalId = setInterval(() => {
if (this.counter > 0) {
this.counter--;
if (this.counter % 10 === 0) {
this.seconds = this.counter;
}
}
else {
this.clearIntervals();
this.reboot();
}
}, 1000);
});
this.menu.addMenuItem(this.rebootToWindowsItem, 2);
}
queueModifySystemItem() {
this.sourceId = GLib.idle_add(GLib.PRIORITY_DEFAULT, () => {
if (!panel.statusArea.quickSettings._system)
return GLib.SOURCE_CONTINUE;
this.modifySystemItem();
return GLib.SOURCE_REMOVE;
});
}
enable() {
const ManagerInterface = `<node>
<interface name="org.freedesktop.login1.Manager">
<method name="Reboot">
<arg type="b" direction="in"/>
</method>
</interface>
</node>`;
this.manager = Gio.DBusProxy.makeProxyWrapper(ManagerInterface);
if (!panel.statusArea.quickSettings._system) {
this.queueModifySystemItem();
}
else {
this.modifySystemItem();
}
}
disable() {
this.manager = null;
this.clearIntervals();
this.rebootToWindowsItem?.destroy();
this.rebootToWindowsItem = null;
this.proxy = null;
if (this.sourceId) {
GLib.Source.remove(this.sourceId);
this.sourceId = null;
}
}
get_windows_grub_entry(file_path) {
const file = Gio.File.new_for_path(file_path);
let boot_entry = "";
let content = file.load_contents(null)[1];
const contentsText = new TextDecoder('utf-8').decode(content);
const arr = contentsText.split(/\r?\n/);
let menu_pattern = new RegExp("^\\s*menuentry ['\"]([^'\"]*)['\"]");
arr.forEach((line) => {
let matches = menu_pattern.exec(line);
if (matches != null && !matches[1].toLowerCase().search("windows")) {
boot_entry = matches[1];
}
});
return boot_entry;
}
async reboot() {
let windows_grub_entry = this.get_windows_grub_entry(GRUB_CONFIG_PATH);
const [, argv] = GLib.shell_parse_argv(`pkexec grub-reboot "${windows_grub_entry}"`);
const proc = Gio.Subprocess.new(argv, Gio.SubprocessFlags.NONE);
await proc.wait_check_async(null);
this.proxy?.RebootRemote(true);
}
buildDialog() {
const dialog = new ModalDialog.ModalDialog({ styleClass: 'modal-dialog' });
dialog.setButtons([
{
label: _('Cancel'),
action: () => {
this.clearIntervals();
dialog.close();
},
key: Clutter.KEY_Escape,
default: false,
},
{
label: _('Restart'),
action: () => {
this.clearIntervals();
this.reboot();
},
default: false,
},
]);
const dialogTitle = new St.Label({
text: _('Restart into Windows'),
// style_class: 'dialog-title' // TODO investigate why css classes are not working
style: 'font-weight: bold;font-size:18px',
});
let dialogMessage = new St.Label({
text: this.getDialogMessageText(),
});
dialogMessage.clutterText.ellipsize = Pango.EllipsizeMode.NONE;
dialogMessage.clutterText.lineWrap = true;
const titleBox = new St.BoxLayout({
xAlign: Clutter.ActorAlign.CENTER,
});
titleBox.add_child(new St.Label({ text: ' ' }));
titleBox.add_child(dialogTitle);
let box = new St.BoxLayout({ yExpand: true, vertical: true });
box.add_child(titleBox);
box.add_child(new St.Label({ text: ' ' }));
box.add_child(dialogMessage);
this.messageIntervalId = setInterval(() => {
dialogMessage?.set_text(this.getDialogMessageText());
}, 500);
dialog.contentLayout.add_child(box);
return dialog;
}
getDialogMessageText() {
return _(`The system will restart automatically in %d seconds.`).replace('%d', String(this.seconds));
}
clearIntervals() {
clearInterval(this.counterIntervalId);
clearInterval(this.messageIntervalId);
}
}
|