Merge pull request #21 from quickgui/actionbuttons
Add new action buttons for running VMs
This commit is contained in:
commit
c9737423a9
|
@ -0,0 +1,6 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
|
||||||
|
<!-- "console" by Austin Andrews @Templarian from materialdesignicons.com -->
|
||||||
|
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="24" height="24" viewBox="0 0 24 24">
|
||||||
|
<path fill="#000000" d="M20,19V7H4V19H20M20,3A2,2 0 0,1 22,5V19A2,2 0 0,1 20,21H4A2,2 0 0,1 2,19V5C2,3.89 2.9,3 4,3H20M13,17V15H18V17H13M9.58,13L5.57,9H8.4L11.7,12.3C12.09,12.69 12.09,13.33 11.7,13.72L8.42,17H5.59L9.58,13Z" />
|
||||||
|
</svg>
|
After Width: | Height: | Size: 595 B |
|
@ -1,8 +1,10 @@
|
||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
|
import 'dart:convert';
|
||||||
import 'dart:core';
|
import 'dart:core';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:path/path.dart' as path;
|
import 'package:path/path.dart' as path;
|
||||||
import 'package:file_picker/file_picker.dart';
|
import 'package:file_picker/file_picker.dart';
|
||||||
|
import 'package:flutter_svg/flutter_svg.dart';
|
||||||
import 'dart:io';
|
import 'dart:io';
|
||||||
|
|
||||||
import '../globals.dart';
|
import '../globals.dart';
|
||||||
|
@ -23,12 +25,16 @@ class Manager extends StatefulWidget {
|
||||||
class _ManagerState extends State<Manager> with PreferencesMixin {
|
class _ManagerState extends State<Manager> with PreferencesMixin {
|
||||||
List<String> _currentVms = [];
|
List<String> _currentVms = [];
|
||||||
Map<String, VmInfo> _activeVms = {};
|
Map<String, VmInfo> _activeVms = {};
|
||||||
final List<String> _spicyVms = [];
|
bool _spicy = false;
|
||||||
|
final List<String> _sshVms = [];
|
||||||
|
String? _terminalEmulator;
|
||||||
Timer? refreshTimer;
|
Timer? refreshTimer;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
|
_getTerminalEmulator();
|
||||||
|
_detectSpice();
|
||||||
getPreference<String>(prefWorkingDirectory).then((pref) {
|
getPreference<String>(prefWorkingDirectory).then((pref) {
|
||||||
setState(() {
|
setState(() {
|
||||||
if (pref == null) {
|
if (pref == null) {
|
||||||
|
@ -38,7 +44,6 @@ class _ManagerState extends State<Manager> with PreferencesMixin {
|
||||||
});
|
});
|
||||||
Future.delayed(Duration.zero, () => _getVms(context)); // Reload VM list when we enter the page.
|
Future.delayed(Duration.zero, () => _getVms(context)); // Reload VM list when we enter the page.
|
||||||
});
|
});
|
||||||
|
|
||||||
refreshTimer = Timer.periodic(const Duration(seconds: 5), (Timer t) {
|
refreshTimer = Timer.periodic(const Duration(seconds: 5), (Timer t) {
|
||||||
_getVms(context);
|
_getVms(context);
|
||||||
}); // Reload VM list every 5 seconds.
|
}); // Reload VM list every 5 seconds.
|
||||||
|
@ -50,6 +55,24 @@ class _ManagerState extends State<Manager> with PreferencesMixin {
|
||||||
super.dispose();
|
super.dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void _getTerminalEmulator() async {
|
||||||
|
ProcessResult result = Process.runSync('x-terminal-emulator', ['-h']);
|
||||||
|
RegExp pattern = RegExp(r"usage:\s+([^\s]+)", multiLine: true, caseSensitive: false);
|
||||||
|
RegExpMatch? match = pattern.firstMatch(result.stdout);
|
||||||
|
if (match != null) {
|
||||||
|
setState(() {
|
||||||
|
_terminalEmulator = match.group(1);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void _detectSpice() async {
|
||||||
|
ProcessResult result = await Process.run('which', ['spicy']);
|
||||||
|
setState(() {
|
||||||
|
_spicy = result.exitCode == 0;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
VmInfo _parseVmInfo(name) {
|
VmInfo _parseVmInfo(name) {
|
||||||
VmInfo info = VmInfo();
|
VmInfo info = VmInfo();
|
||||||
List<String> lines = File(name + '/' + name + '.ports').readAsLinesSync();
|
List<String> lines = File(name + '/' + name + '.ports').readAsLinesSync();
|
||||||
|
@ -107,8 +130,21 @@ class _ManagerState extends State<Manager> with PreferencesMixin {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<bool> _detectSsh(int port) async {
|
||||||
|
bool isSSH = false;
|
||||||
|
try {
|
||||||
|
Socket socket = await Socket.connect('localhost', port);
|
||||||
|
isSSH = await socket.any((event) => utf8.decode(event).contains('SSH'));
|
||||||
|
socket.close();
|
||||||
|
return isSSH;
|
||||||
|
} catch (exception) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Widget _buildVmList() {
|
Widget _buildVmList() {
|
||||||
List<Widget> _widgetList = [];
|
List<Widget> _widgetList = [];
|
||||||
|
final Color buttonColor = Theme.of(context).brightness == Brightness.dark ? Colors.white70 : Theme.of(context).colorScheme.primary;
|
||||||
_widgetList.add(
|
_widgetList.add(
|
||||||
Row(
|
Row(
|
||||||
mainAxisAlignment: MainAxisAlignment.center,
|
mainAxisAlignment: MainAxisAlignment.center,
|
||||||
|
@ -122,7 +158,7 @@ class _ManagerState extends State<Manager> with PreferencesMixin {
|
||||||
ElevatedButton(
|
ElevatedButton(
|
||||||
style: ElevatedButton.styleFrom(
|
style: ElevatedButton.styleFrom(
|
||||||
primary: Theme.of(context).canvasColor,
|
primary: Theme.of(context).canvasColor,
|
||||||
onPrimary: Theme.of(context).brightness == Brightness.dark ? Colors.white70 : Theme.of(context).colorScheme.primary,
|
onPrimary: buttonColor
|
||||||
),
|
),
|
||||||
onPressed: () async {
|
onPressed: () async {
|
||||||
String? result = await FilePicker.platform.getDirectoryPath();
|
String? result = await FilePicker.platform.getDirectoryPath();
|
||||||
|
@ -141,7 +177,7 @@ class _ManagerState extends State<Manager> with PreferencesMixin {
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
List<List<Widget>> rows = _currentVms.map((vm) {
|
List<List<Widget>> rows = _currentVms.map((vm) {
|
||||||
return _buildRow(vm);
|
return _buildRow(vm, buttonColor);
|
||||||
}).toList();
|
}).toList();
|
||||||
for (var row in rows) {
|
for (var row in rows) {
|
||||||
_widgetList.addAll(row);
|
_widgetList.addAll(row);
|
||||||
|
@ -153,18 +189,30 @@ class _ManagerState extends State<Manager> with PreferencesMixin {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
List<Widget> _buildRow(String currentVm) {
|
List<Widget> _buildRow(String currentVm, Color buttonColor) {
|
||||||
final bool active = _activeVms.containsKey(currentVm);
|
final bool active = _activeVms.containsKey(currentVm);
|
||||||
final bool spicy = _spicyVms.contains(currentVm);
|
final bool sshy = _sshVms.contains(currentVm);
|
||||||
|
VmInfo vmInfo = VmInfo();
|
||||||
String connectInfo = '';
|
String connectInfo = '';
|
||||||
if (active) {
|
if (active) {
|
||||||
VmInfo vmInfo = _activeVms[currentVm]!;
|
vmInfo = _activeVms[currentVm]!;
|
||||||
if (vmInfo.sshPort != null) {
|
|
||||||
connectInfo += context.t('SSH port') + ': ' + vmInfo.sshPort! + ' ';
|
|
||||||
}
|
|
||||||
if (vmInfo.spicePort != null) {
|
if (vmInfo.spicePort != null) {
|
||||||
connectInfo += context.t('SPICE port') + ': ' + vmInfo.spicePort! + ' ';
|
connectInfo += context.t('SPICE port') + ': ' + vmInfo.spicePort! + ' ';
|
||||||
}
|
}
|
||||||
|
if (vmInfo.sshPort != null && _terminalEmulator != null) {
|
||||||
|
connectInfo += context.t('SSH port') + ': ' + vmInfo.sshPort! + ' ';
|
||||||
|
_detectSsh(int.parse(vmInfo.sshPort!)).then((sshRunning) {
|
||||||
|
if (sshRunning && !sshy) {
|
||||||
|
setState(() {
|
||||||
|
_sshVms.add(currentVm);
|
||||||
|
});
|
||||||
|
} else if (!sshRunning && sshy) {
|
||||||
|
setState(() {
|
||||||
|
_sshVms.remove(currentVm);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return <Widget>[
|
return <Widget>[
|
||||||
ListTile(
|
ListTile(
|
||||||
|
@ -172,41 +220,24 @@ class _ManagerState extends State<Manager> with PreferencesMixin {
|
||||||
trailing: Row(
|
trailing: Row(
|
||||||
mainAxisSize: MainAxisSize.min,
|
mainAxisSize: MainAxisSize.min,
|
||||||
children: <Widget>[
|
children: <Widget>[
|
||||||
IconButton(
|
|
||||||
icon: Icon(Icons.monitor,
|
|
||||||
color: spicy ? Colors.red : null, semanticLabel: spicy ? context.t('Using SPICE display') : context.t('Click to use SPICE display')),
|
|
||||||
tooltip: spicy ? context.t('Using SPICE display') : context.t('Use SPICE display'),
|
|
||||||
onPressed: () {
|
|
||||||
if (spicy) {
|
|
||||||
setState(() {
|
|
||||||
_spicyVms.remove(currentVm);
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
setState(() {
|
|
||||||
_spicyVms.add(currentVm);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
IconButton(
|
IconButton(
|
||||||
icon: Icon(
|
icon: Icon(
|
||||||
active ? Icons.play_arrow : Icons.play_arrow_outlined,
|
active ? Icons.play_arrow : Icons.play_arrow_outlined,
|
||||||
color: active ? Colors.green : null,
|
color: active ? Colors.green : buttonColor,
|
||||||
semanticLabel: active ? 'Running' : 'Run',
|
semanticLabel: active ? 'Running' : 'Run',
|
||||||
),
|
),
|
||||||
onPressed: () async {
|
onPressed: active ? null : () async {
|
||||||
if (!active) {
|
Map<String, VmInfo> activeVms = _activeVms;
|
||||||
Map<String, VmInfo> activeVms = _activeVms;
|
List<String> args = ['--vm', currentVm + '.conf'];
|
||||||
List<String> args = ['--vm', currentVm + '.conf'];
|
if (_spicy) {
|
||||||
if (spicy) {
|
args.addAll(['--display', 'spice']);
|
||||||
args.addAll(['--display', 'spice']);
|
|
||||||
}
|
|
||||||
await Process.start('quickemu', args);
|
|
||||||
VmInfo info = _parseVmInfo(currentVm);
|
|
||||||
activeVms[currentVm] = info;
|
|
||||||
setState(() {
|
|
||||||
_activeVms = activeVms;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
await Process.start('quickemu', args);
|
||||||
|
VmInfo info = _parseVmInfo(currentVm);
|
||||||
|
activeVms[currentVm] = info;
|
||||||
|
setState(() {
|
||||||
|
_activeVms = activeVms;
|
||||||
|
});
|
||||||
}),
|
}),
|
||||||
IconButton(
|
IconButton(
|
||||||
icon: Icon(
|
icon: Icon(
|
||||||
|
@ -214,41 +245,113 @@ class _ManagerState extends State<Manager> with PreferencesMixin {
|
||||||
color: active ? Colors.red : null,
|
color: active ? Colors.red : null,
|
||||||
semanticLabel: active ? 'Stop' : 'Not running',
|
semanticLabel: active ? 'Stop' : 'Not running',
|
||||||
),
|
),
|
||||||
onPressed: () {
|
onPressed: !active ? null : () {
|
||||||
if (active) {
|
showDialog<bool>(
|
||||||
showDialog<bool>(
|
context: context,
|
||||||
context: context,
|
builder: (BuildContext context) => AlertDialog(
|
||||||
builder: (BuildContext context) => AlertDialog(
|
title: Text(context.t('Stop The Virtual Machine?')),
|
||||||
title: Text(context.t('Stop The Virtual Machine?')),
|
content: Text('${context.t('You are about to terminate the virtual machine')} $currentVm'),
|
||||||
content: Text('${context.t('You are about to terminate the virtual machine')} $currentVm'),
|
actions: <Widget>[
|
||||||
actions: <Widget>[
|
TextButton(
|
||||||
TextButton(
|
onPressed: () => Navigator.pop(context, false),
|
||||||
onPressed: () => Navigator.pop(context, false),
|
child: Text(context.t('Cancel')),
|
||||||
child: Text(context.t('Cancel')),
|
),
|
||||||
),
|
TextButton(
|
||||||
TextButton(
|
onPressed: () => Navigator.pop(context, true),
|
||||||
onPressed: () => Navigator.pop(context, true),
|
child: Text(context.t('OK')),
|
||||||
child: Text(context.t('OK')),
|
),
|
||||||
),
|
],
|
||||||
],
|
),
|
||||||
),
|
).then((result) {
|
||||||
).then((result) {
|
result = result ?? false;
|
||||||
result = result ?? false;
|
if (result) {
|
||||||
if (result) {
|
Process.run('killall', [currentVm]);
|
||||||
Process.run('killall', [currentVm]);
|
setState(() {
|
||||||
setState(() {
|
_activeVms.remove(currentVm);
|
||||||
_activeVms.remove(currentVm);
|
});
|
||||||
});
|
}
|
||||||
}
|
});
|
||||||
});
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
)),
|
)),
|
||||||
if (connectInfo.isNotEmpty)
|
if (connectInfo.isNotEmpty)
|
||||||
ListTile(
|
ListTile(
|
||||||
title: Text(connectInfo),
|
title: Text(
|
||||||
|
connectInfo,
|
||||||
|
style: TextStyle(fontSize: 12)
|
||||||
|
),
|
||||||
|
trailing: Row(
|
||||||
|
mainAxisSize: MainAxisSize.min,
|
||||||
|
children: <Widget>[
|
||||||
|
IconButton(
|
||||||
|
icon: Icon(
|
||||||
|
Icons.monitor,
|
||||||
|
color: _spicy ? buttonColor : null,
|
||||||
|
semanticLabel: 'Connect display with SPICE',
|
||||||
|
),
|
||||||
|
tooltip: _spicy? 'Connect display with SPICE' : 'SPICE client not found',
|
||||||
|
onPressed: !_spicy? null : () {
|
||||||
|
Process.start('spicy', ['-p', vmInfo.spicePort!]);
|
||||||
|
},
|
||||||
|
),
|
||||||
|
IconButton(
|
||||||
|
icon: SvgPicture.asset(
|
||||||
|
'assets/images/console.svg',
|
||||||
|
semanticsLabel: 'Connect with SSH',
|
||||||
|
color: sshy ? buttonColor : Colors.grey
|
||||||
|
),
|
||||||
|
tooltip: sshy ? 'Connect with SSH' : 'SSH server not detected on guest',
|
||||||
|
onPressed: !sshy ? null : () {
|
||||||
|
TextEditingController _usernameController = TextEditingController();
|
||||||
|
showDialog<bool>(
|
||||||
|
context: context,
|
||||||
|
builder: (BuildContext context) => AlertDialog(
|
||||||
|
title: Text('Launch SSH connection to $currentVm'),
|
||||||
|
content: TextField(
|
||||||
|
controller: _usernameController,
|
||||||
|
decoration: const InputDecoration(hintText: "SSH username"),
|
||||||
|
),
|
||||||
|
actions: <Widget>[
|
||||||
|
TextButton(
|
||||||
|
onPressed: () => Navigator.pop(context, false),
|
||||||
|
child: const Text('Cancel'),
|
||||||
|
),
|
||||||
|
TextButton(
|
||||||
|
onPressed: () => Navigator.pop(context, true),
|
||||||
|
child: const Text('Connect'),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
).then((result) {
|
||||||
|
result = result ?? false;
|
||||||
|
if (result) {
|
||||||
|
List<String> sshArgs = ['ssh', '-p', vmInfo.sshPort!, _usernameController.text + '@localhost'];
|
||||||
|
switch(_terminalEmulator) {
|
||||||
|
case 'gnome-terminal':
|
||||||
|
case 'mate-terminal':
|
||||||
|
sshArgs.insert(0, '--');
|
||||||
|
break;
|
||||||
|
case 'xterm':
|
||||||
|
case 'konsole':
|
||||||
|
sshArgs.insert(0, '-e');
|
||||||
|
break;
|
||||||
|
case 'terminator':
|
||||||
|
case 'xfce4-terminal':
|
||||||
|
sshArgs.insert(0, '-x');
|
||||||
|
break;
|
||||||
|
case 'guake':
|
||||||
|
String command = sshArgs.join(' ');
|
||||||
|
sshArgs = ['-e', command];
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
Process.start(_terminalEmulator!, sshArgs);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
},
|
||||||
|
),
|
||||||
|
]
|
||||||
|
)
|
||||||
),
|
),
|
||||||
const Divider()
|
const Divider()
|
||||||
];
|
];
|
||||||
|
|
|
@ -51,6 +51,7 @@ dependencies:
|
||||||
flutter_localizations:
|
flutter_localizations:
|
||||||
sdk: flutter
|
sdk: flutter
|
||||||
desktop_notifications: ^0.6.1
|
desktop_notifications: ^0.6.1
|
||||||
|
flutter_svg: ^0.23.0
|
||||||
|
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
flutter_test:
|
flutter_test:
|
||||||
|
|
Loading…
Reference in New Issue