1
0
Fork 0
mirror of https://github.com/kbumsik/VirtScreen.git synced 2025-02-12 11:21:53 +00:00

QML: better password control

This commit is contained in:
Bumsik Kim 2018-05-13 05:28:49 -04:00
parent 05f95f842d
commit b75cd4ed2b
No known key found for this signature in database
GPG key ID: E31041C8EC5B01C6
2 changed files with 92 additions and 31 deletions

View file

@ -21,9 +21,10 @@ ApplicationWindow {
Material.accent: Material.Teal
// Material.background: Material.Grey
property int margin: 8
width: 380
height: 525
property int margin: 8
property int popupWidth: width - 26
// hide screen when loosing focus
property bool autoClose: true
@ -193,7 +194,7 @@ ApplicationWindow {
focus: true
x: (parent.width - width) / 2
y: (parent.width - height) / 2 //(window.height) / 2
width: window.width - 26
width: popupWidth
ColumnLayout {
anchors.fill: parent
@ -237,6 +238,44 @@ ApplicationWindow {
}
}
Dialog {
id: passwordDialog
title: "New password"
focus: true
modal: true
standardButtons: Dialog.Ok | Dialog.Cancel
x: (parent.width - width) / 2
y: (parent.width - height) / 2 //(window.height) / 2
width: popupWidth
ColumnLayout {
anchors.fill: parent
TextField {
id: passwordFIeld
focus: true
anchors.left: parent.left
anchors.right: parent.right
placeholderText: "New Password";
echoMode: TextInput.Password;
}
Keys.onPressed: {
event.accepted = true;
if (event.key == Qt.Key_Return || event.key == Qt.Key_Enter) {
passwordDialog.accept();
}
}
}
onAccepted: {
backend.createVNCPassword(passwordFIeld.text);
passwordFIeld.text = "";
}
onRejected: passwordFIeld.text = ""
}
StackLayout {
width: parent.width
anchors.top: tabBar.bottom
@ -458,18 +497,22 @@ ApplicationWindow {
anchors.left: parent.left
anchors.right: parent.right
Label { id: passwordLabel; text: "Password" }
TextField {
anchors.left: passwordLabel.right
anchors.right: parent.right
anchors.margins: margin
Label { text: "Password"; Layout.fillWidth: true }
placeholderText: "Password";
text: backend.vncPassword;
echoMode: TextInput.Password;
onTextEdited: {
backend.vncPassword = text;
}
Button {
text: "Delete"
font.capitalization: Font.MixedCase
highlighted: false
enabled: backend.vncUsePassword
onClicked: backend.deleteVNCPassword()
}
Button {
text: "New"
font.capitalization: Font.MixedCase
highlighted: true
enabled: !backend.vncUsePassword
onClicked: passwordDialog.open()
}
}
}

View file

@ -354,7 +354,7 @@ class Backend(QObject):
_virtScreenIndex: int
# VNC server properties
_vncPort: int
_vncPassword: str = ""
_vncUsePassword: bool = False
_vncState: VNCState
_vncAutoStart: bool
_ipAddresses: List[str] = []
@ -367,6 +367,7 @@ class Backend(QObject):
# Signals
onVirtScreenCreatedChanged = pyqtSignal(bool)
onVirtScreenIndexChanged = pyqtSignal(int)
onVncUsePasswordChanged = pyqtSignal(bool)
onVncStateChanged = pyqtSignal(VNCState)
onVncAutoStartChanged = pyqtSignal(bool)
onIPAddressesChanged = pyqtSignal()
@ -453,12 +454,18 @@ class Backend(QObject):
def vncPort(self, port):
self._vncPort = port
@pyqtProperty(str)
def vncPassword(self):
return self._vncPassword
@vncPassword.setter
def vncPassword(self, vncPassword):
self._vncPassword = vncPassword
@pyqtProperty(bool, notify=onVncUsePasswordChanged)
def vncUsePassword(self):
if os.path.isfile(X11VNC_PASSWORD_PATH):
self._vncUsePassword = True
else:
if self._vncUsePassword:
self.vncUsePassword = False
return self._vncUsePassword
@vncUsePassword.setter
def vncUsePassword(self, use):
self._vncUsePassword = use
self.onVncUsePasswordChanged.emit(use)
@pyqtProperty(VNCState, notify=onVncStateChanged)
def vncState(self):
@ -514,6 +521,27 @@ class Backend(QObject):
self.xrandr.delete_virtual_screen()
self.virtScreenCreated = False
@pyqtSlot(str)
def createVNCPassword(self, password):
if password:
p = SubprocessWrapper()
try:
p.run(f"x11vnc -storepasswd {password} {X11VNC_PASSWORD_PATH}")
except:
print("Failed creating password")
return
self.vncUsePassword = True
else:
print("Empty password")
@pyqtSlot()
def deleteVNCPassword(self):
if os.path.isfile(X11VNC_PASSWORD_PATH):
os.remove(X11VNC_PASSWORD_PATH)
self.vncUsePassword = False
else:
print("Failed deleting the password file")
@pyqtSlot()
def startVNC(self):
# Check if a virtual screen created
@ -538,23 +566,13 @@ class Backend(QObject):
print("VNC Exited.")
self.vncState = self.VNCState.OFF
atexit.unregister(self.stopVNC)
# Set password
password = False
if self.vncPassword:
print("There is password. Creating.")
password = True
p = SubprocessWrapper()
try:
p.run(f"x11vnc -storepasswd {self.vncPassword} {X11VNC_PASSWORD_PATH}")
except:
password = False
logfile = open(X11VNC_LOG_PATH, "wb")
self.vncServer = ProcessProtocol(_onConnected, _onReceived, _onReceived, _onEnded, logfile)
port = self.vncPort
virt = self.xrandr.get_virtual_screen()
clip = f"{virt.width}x{virt.height}+{virt.x_offset}+{virt.y_offset}"
arg = f"x11vnc -multiptr -repeat -rfbport {port} -clip {clip}"
if password:
if self.vncUsePassword:
arg += f" -rfbauth {X11VNC_PASSWORD_PATH}"
self.vncServer.run(arg)
# auto stop on exit