Skip to content

Commit

Permalink
FIXUP: Prepare #193
Browse files Browse the repository at this point in the history
  • Loading branch information
apyrgio committed Sep 17, 2024
1 parent a014fee commit 02b4fc2
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 52 deletions.
108 changes: 59 additions & 49 deletions dangerzone/gui/main_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import platform
import subprocess
import tempfile
import traceback
import typing
from multiprocessing.pool import ThreadPool
from typing import List, Optional
Expand Down Expand Up @@ -388,15 +389,25 @@ def closeEvent(self, e: QtGui.QCloseEvent) -> None:


class InstallContainerThread(QtCore.QThread):
finished = QtCore.Signal()
finished = QtCore.Signal(str)

def __init__(self, dangerzone: DangerzoneGui) -> None:
super(InstallContainerThread, self).__init__()
self.dangerzone = dangerzone

def run(self) -> None:
self.dangerzone.isolation_provider.install()
self.finished.emit()
error = None
try:
installed = self.dangerzone.isolation_provider.install()
except Exception as e:
log.error("Installation problemo")
error = "".join(traceback.format_exception(e))
self.finished.emit(error)
else:
if not installed:
# FIXME: Improve this.
self.finished.emit("Failed to install the container image")
self.finished.emit(None)


class WaitingWidget(QtWidgets.QWidget):
Expand All @@ -416,7 +427,6 @@ class WaitingWidgetContainer(WaitingWidget):
#
# Linux states
# - "install_container"
finished = QtCore.Signal()

def __init__(self, dangerzone: DangerzoneGui) -> None:
super(WaitingWidgetContainer, self).__init__()
Expand All @@ -441,17 +451,7 @@ def __init__(self, dangerzone: DangerzoneGui) -> None:
# Error
self.error_text = QTextEdit()
self.error_text.setReadOnly(True)
self.error_text.setStyleSheet(
"""
QTextEdit {
font-family: Consolas, Monospace;
font-size: 12px;
background-color: #fff;
color: #000;
padding: 10px;
}
"""
)
self.error_text.setProperty("style", "container_error")
self.error_text.setVisible(False)
# Enable copying
self.error_text.setTextInteractionFlags(Qt.TextSelectableByMouse)
Expand Down Expand Up @@ -503,56 +503,66 @@ def check_state(self) -> None:
# Update the state
self.state_change(state, error)

def show_error(self, msg: str, details: str | None = None) -> None:
self.label.setText(msg)
if details is not None:
self.error_text.setPlainText(details)
self.error_text.setVisible(True)
else:
self.error_text.setVisible(False)
self.buttons.show()

def show_message(self, msg: str) -> None:
self.label.setText(msg)
self.error_text.setVisible(False)
self.buttons.hide()

def installation_finished(self, error: str | None = None) -> None:
if error:
msg = (
"Installing the Dangerzone container image.<br><br>"
"An unexpected error occurred:"
)
self.show_error(msg, error)
else:
self.finished.emit()

def state_change(self, state: str, error: str | None = None) -> None:
if state == "not_installed":
if platform.system() == "Linux":
self.label.setText(
(
"<strong>Dangerzone requires Podman</strong><br><br>"
"Install it and retry."
)
self.show_error(
"<strong>Dangerzone requires Podman</strong><br><br>"
"Install it and retry."
)
else:
self.label.setText(
(
"<strong>Dangerzone requires Docker Desktop</strong><br><br>"
"<a href='https://www.docker.com/products/docker-desktop'>Download Docker Desktop</a>"
", install it, and open it."
)
self.show_error(
"<strong>Dangerzone requires Docker Desktop</strong><br><br>"
"<a href='https://www.docker.com/products/docker-desktop'>Download Docker Desktop</a>"
", install it, and open it."
)
self.buttons.show()
elif state == "not_running":
if platform.system() == "Linux":
# "not_running" here means that the `podman image ls` command failed.
message = (
self.show_error(
"<strong>Dangerzone requires Podman</strong><br><br>"
"Podman is installed but cannot run properly. See errors below"
"Podman is installed but cannot run properly.<br>"
"See errors below:",
error,
)
if error:
self.error_text.setPlainText(error)
self.error_text.setVisible(True)

self.label.setText(message)

else:
self.label.setText(
(
"<strong>Dangerzone requires Docker Desktop</strong><br><br>"
"Docker is installed but isn't running.<br><br>"
"Open Docker and make sure it's running in the background."
)
self.show_error(
"<strong>Dangerzone requires Docker Desktop</strong><br><br>"
"Docker is installed but isn't running.<br><br>"
"Open Docker and make sure it's running in the background."
)
self.buttons.show()
else:
self.label.setText(
(
"Installing the Dangerzone container image.<br><br>"
"This might take a few minutes..."
)
self.show_message(
"Installing the Dangerzone container image.<br><br>"
"This might take a few minutes...",
)
self.buttons.hide()

self.install_container_t = InstallContainerThread(self.dangerzone)
self.install_container_t.finished.connect(self.finished)
self.install_container_t.finished.connect(self.installation_finished)
self.install_container_t.start()


Expand Down
9 changes: 6 additions & 3 deletions dangerzone/isolation_provider/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def install() -> bool:
startupinfo=get_subprocess_startupinfo(),
)

chunk_size = 10240
chunk_size = 4 << 20
compressed_container_path = get_resource_path("container.tar.gz")
with gzip.open(compressed_container_path) as f:
while True:
Expand All @@ -178,7 +178,11 @@ def install() -> bool:
p.stdin.write(chunk)
else:
break
p.communicate()
_, err = p.communicate()
if p.returncode < 0:
raise RuntimeError(f"Could not install container image: {err}")

log.info("Container image installation finished successfully")

if not Container.is_container_installed():
log.error("Failed to install the container image")
Expand All @@ -192,7 +196,6 @@ def is_container_installed() -> bool:
"""
See if the podman container is installed. Linux only.
"""
# Get the image id
with open(get_resource_path("image-id.txt")) as f:
expected_image_id = f.read().strip()

Expand Down
8 changes: 8 additions & 0 deletions share/dangerzone.css
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,11 @@ QLabel.version {
font-size: 20px;
padding-bottom: 5px; /* align with 'dangerzone' font */
}

QTextEdit[style="container_error"] {
font-family: Consolas, Monospace;
font-size: 12px;
background-color: #ffffff;
color: #000000;
padding: 10px;
}

0 comments on commit 02b4fc2

Please sign in to comment.