Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Create new KeepassHTTP KeyAcceptDialog #850

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/http/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ if(WITH_XC_HTTP)

set(keepasshttp_SOURCES
AccessControlDialog.cpp
KeyAcceptDialog.cpp
EntryConfig.cpp
HttpPasswordGeneratorWidget.cpp
HttpSettings.cpp
Expand Down
150 changes: 150 additions & 0 deletions src/http/KeyAcceptDialog.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/*
* Copyright (C) 2013 Francois Ferrand
* Copyright (C) 2017 KeePassXC Team <team@keepassxc.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 or (at your option)
* version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "KeyAcceptDialog.h"
#include "core/Entry.h"
#include "ui_KeyAcceptDialog.h"
#include <QDialogButtonBox>
#include <QPushButton>
#include <QStandardItem>
#include <QStandardItemModel>
#include <Qt>

KeyAcceptDialog::KeyAcceptDialog(QWidget* parent)
: QDialog(parent)
, ui(new Ui::KeyAcceptDialog())
{
this->setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint);

ui->setupUi(this);
connect(ui->buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
connect(ui->buttonBox, SIGNAL(rejected()), this, SLOT(reject()));

connect(ui->keyNameLineEdit, SIGNAL(textChanged(const QString)), this, SLOT(keyEditChanged(const QString)));

QStandardItemModel* listViewModel = new QStandardItemModel(ui->databasesListView);
ui->databasesListView->setModel(listViewModel);
connect(listViewModel, SIGNAL(itemChanged(QStandardItem*)), this, SLOT(modelItemChanged(QStandardItem*)));
}

KeyAcceptDialog::~KeyAcceptDialog()
{
}

void KeyAcceptDialog::setItems(const QList<QString>& items)
{
QStandardItemModel* listViewModel = qobject_cast<QStandardItemModel*>(ui->databasesListView->model());

listViewModel->clear();

for (QString item : items) {
QStandardItem* listItem = new QStandardItem(item);
listItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsUserCheckable);
listItem->setData(Qt::Unchecked, Qt::CheckStateRole);

listViewModel->appendRow(listItem);
}
}

void KeyAcceptDialog::setItemEnabled(int itemIndex, bool enabled)
{
QStandardItemModel* listViewModel = qobject_cast<QStandardItemModel*>(ui->databasesListView->model());

QStandardItem* item = listViewModel->item(itemIndex);

if (item) {
item->setEnabled(enabled);
}
}

void KeyAcceptDialog::setItemChecked(int itemIndex, bool checked)
{
QStandardItemModel* listViewModel = qobject_cast<QStandardItemModel*>(ui->databasesListView->model());

QStandardItem* item = listViewModel->item(itemIndex);

if (item) {
if (checked) {
item->setCheckState(Qt::Checked);
} else {
item->setCheckState(Qt::Unchecked);
}
}
}

QList<int> KeyAcceptDialog::getCheckedItems()
{
QStandardItemModel* listViewModel = qobject_cast<QStandardItemModel*>(ui->databasesListView->model());

int numRows = listViewModel->rowCount();

QList<int> resultList;

for (int row = 0; row < numRows; row++) {
QStandardItem* item = listViewModel->item(row);
if (item->checkState() == Qt::Checked) {
resultList << row;
}
}

return resultList;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it okay to stack-allocate (? - I am not sure if that is what happens, and whether Qt transforms that into something else) resultList here and then return it to the caller, where the caller will simply let it go out of scope?

I.e. will the object be destroyed once it runs out of scope in the caller, and is this the proper way to do it or should this be done differently?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Answered on second question

}

QString KeyAcceptDialog::getKeyName()
{
return ui->keyNameLineEdit->text();
}

void KeyAcceptDialog::checkAcceptable()
{
QStandardItemModel* listViewModel = qobject_cast<QStandardItemModel*>(ui->databasesListView->model());
int numRows = listViewModel->rowCount();
bool hasChecked = false;
for (int row = 0; row < numRows; row++) {
QStandardItem* item = listViewModel->item(row);
if (item->checkState() == Qt::Checked) {
hasChecked = true;
break;
}
}

if (getKeyName().isEmpty()) {
ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
ui->buttonBox->button(QDialogButtonBox::Ok)->setToolTip(tr("Make sure the key name is not empty!"));
} else if (!hasChecked) {
ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
ui->buttonBox->button(QDialogButtonBox::Ok)->setToolTip(tr("Check at least one database"));
} else {
ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(true);
ui->buttonBox->button(QDialogButtonBox::Ok)->setToolTip("");
}
}

void KeyAcceptDialog::keyEditChanged(const QString& text)
{
(void)text;

checkAcceptable();
}

void KeyAcceptDialog::modelItemChanged(QStandardItem* item)
{
(void)item;

checkAcceptable();
}
56 changes: 56 additions & 0 deletions src/http/KeyAcceptDialog.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (C) 2013 Francois Ferrand
* Copyright (C) 2017 KeePassXC Team <team@keepassxc.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 or (at your option)
* version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef KEYACCEPTDIALOG_H
#define KEYACCEPTDIALOG_H

#include <QDialog>
#include <QScopedPointer>
#include <QStandardItem>

namespace Ui
{
class KeyAcceptDialog;
}

class KeyAcceptDialog : public QDialog
{
Q_OBJECT

public:
explicit KeyAcceptDialog(QWidget* parent = nullptr);
~KeyAcceptDialog();

void setItems(const QList<QString>& items);
void setItemChecked(int itemIndex, bool checked);
void setItemEnabled(int itemIndex, bool enabled);
QList<int> getCheckedItems();

QString getKeyName();

void checkAcceptable();

private slots:
void keyEditChanged(const QString& text);
void modelItemChanged(QStandardItem* item);

private:
QScopedPointer<Ui::KeyAcceptDialog> ui;
};

#endif // KEYACCEPTDIALOG_H
99 changes: 99 additions & 0 deletions src/http/KeyAcceptDialog.ui
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>KeyAcceptDialog</class>
<widget class="QDialog" name="KeyAcceptDialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>469</width>
<height>292</height>
</rect>
</property>
<property name="windowTitle">
<string>KeePassXC: New key association request</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="label">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Maximum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;You have received an association request for the above key.&lt;br&gt;If you would like to allow it access to your KeePassXC database,&lt;br&gt;give it a unique name to identify and accept it.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="keyNameLineEdit"/>
</item>
<item>
<widget class="QLabel" name="label_2">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Maximum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Which databases should this key be valid for?&lt;br&gt;(The currently selected database is pre-selected; grayed-out databases are not opened - open them to enable accepting key!)&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QListView" name="databasesListView"/>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections>
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>KeyAcceptDialog</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>248</x>
<y>254</y>
</hint>
<hint type="destinationlabel">
<x>157</x>
<y>274</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonBox</sender>
<signal>rejected()</signal>
<receiver>KeyAcceptDialog</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>316</x>
<y>260</y>
</hint>
<hint type="destinationlabel">
<x>286</x>
<y>274</y>
</hint>
</hints>
</connection>
</connections>
</ui>
Loading