Skip to content

Commit

Permalink
added update with local firmware
Browse files Browse the repository at this point in the history
adjusted collapse function

adjusted update local process and presentation

	modified:   nitrokeyapp/device_data.py
	modified:   nitrokeyapp/overview_tab.py
	modified:   nitrokeyapp/qt_utils_mix_in.py
	modified:   nitrokeyapp/ui/overview_tab.ui
  • Loading branch information
Niklap97 authored and jj-so committed Oct 17, 2023
1 parent ad205d3 commit 1b3a9a1
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 19 deletions.
12 changes: 7 additions & 5 deletions nitrokeyapp/device_data.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import logging
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, Optional

from pynitrokey.nk3 import Nitrokey3Device

Expand Down Expand Up @@ -40,14 +40,16 @@ def update(
self,
overview_tab: "OverviewTab",
info_frame: InfoBox,
image: Optional[str] = None,
) -> None:
self.updating = True
overview_tab.update_btns_during_update(False)
try:
self.updating = True
Nk3Context(self.path).update(overview_tab, info_frame)
self.updating = False
Nk3Context(self.path).update(overview_tab, info_frame, image)
logger.info("Successfully updated the Nitrokey 3")
info_frame.set_text("Successfully updated the Nitrokey 3.")
except Exception as e:
self.updating = False
logger.info(f"Failed to update Nitrokey 3 {e}")
info_frame.set_text("Failed to update Nitrokey 3.")
self.updating = False
overview_tab.update_btns_during_update(True)
39 changes: 36 additions & 3 deletions nitrokeyapp/overview_tab.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
from typing import Optional

from PyQt5.QtCore import pyqtSlot
from PyQt5.QtWidgets import QWidget
from PyQt5.QtWidgets import QFileDialog, QWidget

from nitrokeyapp.device_data import DeviceData
from nitrokeyapp.information_box import InfoBox
from nitrokeyapp.qt_utils_mix_in import QtUtilsMixIn
from nitrokeyapp.ui.overview_tab import Ui_OverviewTab
from nitrokeyapp.worker import Worker


class OverviewTab(QWidget):
class OverviewTab(QtUtilsMixIn, QWidget):
def __init__(self, info_box: InfoBox, parent: Optional[QWidget] = None) -> None:
super().__init__(parent)
QWidget.__init__(self, parent)
QtUtilsMixIn.__init__(self)

self.data: Optional[DeviceData] = None
self.info_box = info_box
self.ui = Ui_OverviewTab()
self.ui.setupUi(self)

self.collapse(self.ui.more_options_frame, self.ui.more_options_btn)
self.ui.update_with_file_btn.clicked.connect(self.update_with_file)
self.ui.more_options_btn.clicked.connect(self.show_more_options)
self.ui.pushButtonUpdate.clicked.connect(self.run_update)

self.reset()
Expand Down Expand Up @@ -67,7 +72,35 @@ def set_update_enabled(self, enabled: bool) -> None:
self.ui.pushButtonUpdate.setEnabled(enabled)
self.ui.pushButtonUpdate.setToolTip(tooltip)

def update_btns_during_update(self, enabled: bool) -> None:
tooltip = ""
if enabled:
self.ui.pushButtonUpdate.setEnabled(enabled)
self.ui.pushButtonUpdate.setToolTip(tooltip)
self.ui.update_with_file_btn.setEnabled(enabled)
self.ui.update_with_file_btn.setToolTip(tooltip)
else:
tooltip = "Update is already running. Please wait."
self.ui.pushButtonUpdate.setEnabled(enabled)
self.ui.pushButtonUpdate.setToolTip(tooltip)
self.ui.update_with_file_btn.setEnabled(enabled)
self.ui.update_with_file_btn.setToolTip(tooltip)

def show_more_options(self) -> None:
self.collapse(self.ui.more_options_frame, self.ui.more_options_btn)

@pyqtSlot()
def run_update(self) -> None:
assert self.data
self.data.update(self, self.info_box)

@pyqtSlot()
def update_with_file(self) -> None:
assert self.data
fdialog = QFileDialog()
fdialog.setFileMode(QFileDialog.AnyFile)

if fdialog.exec_():
filenames = fdialog.selectedFiles()
file = filenames[0]
self.data.update(self, self.info_box, image=file)
15 changes: 8 additions & 7 deletions nitrokeyapp/qt_utils_mix_in.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import Optional, Type, TypeVar

from PyQt5 import QtGui, QtWidgets, uic
from PyQt5.QtCore import QObject
from PyQt5.QtCore import QObject, QSize

Q = TypeVar("Q", bound=QObject)

Expand Down Expand Up @@ -61,18 +61,19 @@ def load_ui(self, filename: str, qt_obj: Type) -> bool:
return True

def collapse(
self, gBox: QtWidgets.QWidget, expand_button: QtWidgets.QPushButton
self, frame: QtWidgets.QWidget, expand_button: QtWidgets.QPushButton
) -> None:
# Find out if the state is on or off
gbState = expand_button.isChecked()
if not gbState:
state = expand_button.isChecked()
if not state:
expand_button.setIcon(QtGui.QIcon(":/icons/right_arrow.png"))
gBox.setFixedHeight(15)
expand_button.setIconSize(QSize(12, 12))
frame.setFixedHeight(0)
# Set window Height
# self.setFixedHeight(self.sizeHint().height())
else:
expand_button.setIcon(QtGui.QIcon(":/icons/down_arrow.png"))
oSize = gBox.sizeHint()
gBox.setFixedHeight(oSize.height())
oSize = frame.sizeHint()
frame.setFixedHeight(oSize.height())
# Set window Height
# self.setFixedHeight(self.sizeHint().height())
61 changes: 57 additions & 4 deletions nitrokeyapp/ui/overview_tab.ui
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>492</width>
<height>388</height>
<width>482</width>
<height>366</height>
</rect>
</property>
<property name="sizePolicy">
Expand All @@ -65,7 +65,7 @@
<string notr="true"/>
</property>
<layout class="QVBoxLayout" name="verticalLayout_3">
<item alignment="Qt::AlignTop">
<item>
<widget class="QFrame" name="Nitrokey3">
<property name="enabled">
<bool>true</bool>
Expand Down Expand Up @@ -97,7 +97,7 @@
<property name="midLineWidth">
<number>5</number>
</property>
<layout class="QFormLayout" name="formLayout_3">
<layout class="QFormLayout" name="formLayout">
<item row="0" column="0" colspan="2">
<widget class="QLabel" name="label_nk3">
<property name="sizePolicy">
Expand Down Expand Up @@ -252,6 +252,59 @@
</layout>
</widget>
</item>
<item alignment="Qt::AlignLeft">
<widget class="QPushButton" name="more_options_btn">
<property name="text">
<string>More Options</string>
</property>
<property name="checkable">
<bool>true</bool>
</property>
<property name="checked">
<bool>false</bool>
</property>
<property name="flat">
<bool>true</bool>
</property>
</widget>
</item>
<item alignment="Qt::AlignTop">
<widget class="QFrame" name="more_options_frame">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>440</width>
<height>0</height>
</size>
</property>
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
<property name="lineWidth">
<number>5</number>
</property>
<property name="midLineWidth">
<number>5</number>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QPushButton" name="update_with_file_btn">
<property name="text">
<string>Update with local Firmware</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
</widget>
Expand Down

0 comments on commit 1b3a9a1

Please sign in to comment.