Skip to content

Commit

Permalink
Make from Configuration form a main window, with menu bar
Browse files Browse the repository at this point in the history
Initial attempt
Issue #274

Signed-off-by: Szczepan Zalega <szczepan@nitrokey.com>
  • Loading branch information
szszszsz committed Mar 29, 2018
1 parent fe9e651 commit d56dd53
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 31 deletions.
19 changes: 14 additions & 5 deletions src/GUI/Tray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@ TODO
#include "nitrokey-applet.h"
#include <libnitrokey/NitrokeyManager.h>
#include <QMenu>
#include <QMenuBar>

Tray::Tray(QObject *_parent, bool _debug_mode, bool _extended_config,
StorageActions *actions) :
QObject(_parent),
trayMenu(nullptr),
trayMenuPasswdSubMenu(nullptr),
file_menu(nullptr),
worker(nullptr)
{
main_window = _parent;
Expand All @@ -50,8 +52,6 @@ Tray::Tray(QObject *_parent, bool _debug_mode, bool _extended_config,
initActionsForStick20();
initCommonActions();

generateMenu(true);

mapper_TOTP = new QSignalMapper(this);
mapper_HOTP = new QSignalMapper(this);
mapper_PWS = new QSignalMapper(this);
Expand Down Expand Up @@ -148,9 +148,7 @@ bool Tray::eventFilter(QObject *obj, QEvent *event) {
return QObject::eventFilter(obj, event);
}


void Tray::generateMenu(bool init, std::function<void(QMenu *)> run_before) {
{
static QMutex mtx;
QMutexLocker locker(&mtx);

Expand Down Expand Up @@ -189,8 +187,12 @@ bool Tray::eventFilter(QObject *obj, QEvent *event) {

trayMenu->addAction(quitAction);
trayIcon->setContextMenu(trayMenu.get());

if (file_menu != nullptr){
trayMenu->setTitle(tr("Menu"));
file_menu->addMenu(trayMenu.get());
}
}
}

void Tray::initActionsForStick10() {
UnlockPasswordSafeAction = new QAction(tr("Unlock password safe"), main_window);
Expand Down Expand Up @@ -687,3 +689,10 @@ void Tray::setAdmin_mode(bool _admin_mode) {
ExtendedConfigActive = _admin_mode;
}

void Tray::setFile_menu(QMenuBar *file_menu) {
Tray::file_menu = file_menu;
generateMenu(true);
if (trayMenu!= nullptr)
file_menu->addMenu(trayMenu.get());
}

8 changes: 7 additions & 1 deletion src/GUI/Tray.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public slots:
#include <QSystemTrayIcon>
#include <QAction>
#include <memory>
#include <QtWidgets/QMainWindow>
#include "StorageActions.h"

class Tray : public QObject {
Expand Down Expand Up @@ -104,8 +105,13 @@ private slots:
// QMenu *trayMenuTOTPSubMenu;
// QMenu *trayMenuHOTPSubMenu;
QMenu *trayMenuSubSpecialConfigure;
QMenuBar* file_menu;
public:
void setFile_menu(QMenuBar *file_menu);

private:

QAction *quitAction;
QAction *quitAction;
QAction *configureAction;
QAction *resetAction;
QAction *configureActionStick20;
Expand Down
67 changes: 43 additions & 24 deletions src/ui/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,13 @@
#include "src/core/SecureString.h"

#include <QString>
#include <src/core/ScopedGuard.h>
#include "src/core/ScopedGuard.h"
#include "src/core/ThreadWorker.h"
#include "hotpslot.h"

#include <QFuture>
#include <QtConcurrent/QtConcurrent>


using nm = nitrokey::NitrokeyManager;
static const QString communication_error_message = QApplication::tr("Communication error. Please reinsert the device.");
Expand Down Expand Up @@ -95,7 +101,7 @@ void MainWindow::load_settings(){
ui->spin_PWS_time->setValue(settings.value("clipboard/PWS_time", 60).toInt());
ui->spin_OTP_time->setValue(settings.value("clipboard/OTP_time", 120).toInt());
ui->cb_device_connection_message->setChecked(settings.value("main/connection_message", true).toBool());
ui->cb_check_symlink->setChecked(settings.value("storage/check_symlink", true).toBool());
ui->cb_check_symlink->setChecked(settings.value("storage/check_symlink", false).toBool());
#ifndef Q_OS_LINUX
ui->cb_check_symlink->setEnabled(false);
#endif
Expand Down Expand Up @@ -206,6 +212,12 @@ MainWindow::MainWindow(QWidget *parent):
first_run();

load_settings();

// tray.regenerateMenu();
tray.setFile_menu(menuBar());
ui->tabWidget->setEnabled(false);
// startConfiguration();
show();
}

void MainWindow::first_run(){
Expand Down Expand Up @@ -283,7 +295,10 @@ void MainWindow::initialTimeReset() {
return;
}

if (!libada::i()->is_time_synchronized()) {
QFuture<bool> is_time_synchronized = QtConcurrent::run(libada::i().get(), &libada::is_time_synchronized);
is_time_synchronized.waitForFinished();

if (!is_time_synchronized.result()) {
bool answer = csApplet()->detailedYesOrNoBox(tr("Time is out-of-sync") + " - " + RESET_NITROKEYS_TIME, WARNING_DEVICES_CLOCK_NOT_DESYNCHRONIZED,
false);
if (answer) {
Expand Down Expand Up @@ -1110,12 +1125,13 @@ void MainWindow::SetupPasswordSafeConfig(void) {

ui->PWS_ComboBoxSelectSlot->setEnabled(PWS_Access);
ui->PWS_ButtonEnable->setVisible(!PWS_Access);
ui->PWS_Lock->setVisible(PWS_Access);

ui->PWS_EditSlotName->setMaxLength(PWS_SLOTNAME_LENGTH);
ui->PWS_EditPassword->setMaxLength(PWS_PASSWORD_LENGTH);
ui->PWS_EditLoginName->setMaxLength(PWS_LOGINNAME_LENGTH);

ui->PWS_CheckBoxHideSecret->setChecked(TRUE);
ui->PWS_CheckBoxHideSecret->setChecked(true);
ui->PWS_EditPassword->setEchoMode(QLineEdit::Password);
}

Expand Down Expand Up @@ -1504,41 +1520,27 @@ void MainWindow::on_DeviceDisconnected() {
tray.showTrayMessage(tr("Nitrokey disconnected"));
}

if(this->isVisible()){
this->close();
csApplet()->messageBox(tr("Closing window due to device disconnection"));
}
ui->tabWidget->setEnabled(false);
}

#include "src/core/ThreadWorker.h"
#include "hotpslot.h"

void MainWindow::on_DeviceConnected() {
if (debug_mode)
qDebug("on_DeviceConnected");

if (debug_mode)
emit ShortOperationBegins(tr("Connecting device"));

//TODO share device state to improve performance
ui->statusBar->showMessage(tr("Device connected. Waiting for initialization..."));

try{
libada::i()->get_status();
auto result = QtConcurrent::run(libada::i().get(), &libada::get_status);
result.waitForFinished();
}
catch (LongOperationInProgressException &e){
long_operation_in_progress = true;
return;
}

QSettings settings;
if(settings.value("main/connection_message", true).toBool()){

auto connected_device_model = libada::i()->isStorageDeviceConnected() ?
tr("Nitrokey Storage connected") :
tr("Nitrokey Pro connected");
ui->statusBar->showMessage(connected_device_model);
tray.showTrayMessage(tr("Nitrokey connected"), connected_device_model);
}

initialTimeReset();

new ThreadWorker(
Expand Down Expand Up @@ -1589,7 +1591,19 @@ void MainWindow::on_DeviceConnected() {
));
}
#endif
}, this);
}, this);
startConfiguration();
ui->tabWidget->setEnabled(true);

QSettings settings;
if(settings.value("main/connection_message", true).toBool()){

auto connected_device_model = libada::i()->isStorageDeviceConnected() ?
tr("Nitrokey Storage connected") :
tr("Nitrokey Pro connected");
ui->statusBar->showMessage(connected_device_model);
tray.showTrayMessage(tr("Nitrokey connected"), connected_device_model);
}
}

void MainWindow::on_KeepDeviceOnline() {
Expand Down Expand Up @@ -1722,3 +1736,8 @@ void MainWindow::on_btn_select_debug_file_path_clicked()
auto filename = QFileDialog::getSaveFileName(this, tr("Debug file location (will be overwritten)"));
ui->edit_debug_file_path->setText(filename);
}

void MainWindow::on_PWS_Lock_clicked()
{
startLockDeviceAction(true);
}
2 changes: 2 additions & 0 deletions src/ui/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@ private slots:

void on_btn_select_debug_file_path_clicked();

void on_PWS_Lock_clicked();

public:
void generateOTPConfig(OTPSlot *slot);
unsigned int get_supported_secret_length_base32() const;
Expand Down
24 changes: 23 additions & 1 deletion src/ui/mainwindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@
<property name="movable">
<bool>false</bool>
</property>
<widget class="QWidget" name="tab_5">
<attribute name="title">
<string>Start</string>
</attribute>
</widget>
<widget class="QWidget" name="tab">
<attribute name="title">
<string>OTP Slot Configuration</string>
Expand Down Expand Up @@ -1283,10 +1288,17 @@
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="PWS_Lock">
<property name="text">
<string>Lock Device</string>
</property>
</widget>
</item>
<item>
<widget class="QProgressBar" name="PWS_progressBar">
<property name="value">
<number>24</number>
<number>42</number>
</property>
</widget>
</item>
Expand Down Expand Up @@ -1626,6 +1638,16 @@
<bool>false</bool>
</property>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>932</width>
<height>20</height>
</rect>
</property>
</widget>
</widget>
<layoutdefault spacing="6" margin="11"/>
<tabstops>
Expand Down

0 comments on commit d56dd53

Please sign in to comment.