Skip to content

Commit

Permalink
Pass parent to dialogs. Modify button text on Add.
Browse files Browse the repository at this point in the history
  • Loading branch information
varjolintu committed Apr 28, 2024
1 parent 44b1adc commit 5018674
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 24 deletions.
4 changes: 4 additions & 0 deletions share/translations/keepassxc_en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -992,6 +992,10 @@ Do you want to delete the entry?
Do you want to overwrite the passkey in %1 - %2?</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Register</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>BrowserSettingsWidget</name>
Expand Down
5 changes: 3 additions & 2 deletions src/browser/BrowserService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,7 @@ QJsonObject BrowserService::showPasskeysRegisterPrompt(const QJsonObject& public
// If no entry is selected, show the import dialog for manual entry selection
auto selectedEntry = confirmDialog.getSelectedEntry();
if (!selectedEntry) {
PasskeyImporter passkeyImporter;
PasskeyImporter passkeyImporter(m_currentDatabaseWidget);
const auto result = passkeyImporter.showImportDialog(db,
nullptr,
origin,
Expand All @@ -672,7 +672,8 @@ QJsonObject BrowserService::showPasskeysRegisterPrompt(const QJsonObject& public
userId,
publicKeyCredentials.key,
tr("KeePassXC - Passkey credentials"),
tr("Register a new passkey to this entry:"));
tr("Register a new passkey to this entry:"),
tr("Register"));
if (!result) {
return getPasskeyError(ERROR_PASSKEYS_REQUEST_CANCELED);
}
Expand Down
4 changes: 2 additions & 2 deletions src/gui/DatabaseWidget.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2023 KeePassXC Team <[email protected]>
* Copyright (C) 2024 KeePassXC Team <[email protected]>
* Copyright (C) 2010 Felix Geyer <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
Expand Down Expand Up @@ -1411,7 +1411,7 @@ void DatabaseWidget::switchToPasskeys()

void DatabaseWidget::showImportPasskeyDialog(bool isEntry)
{
PasskeyImporter passkeyImporter;
PasskeyImporter passkeyImporter(this);

if (isEntry) {
auto currentEntry = currentSelectedEntry();
Expand Down
11 changes: 8 additions & 3 deletions src/gui/passkeys/PasskeyExporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,18 @@
#include <QJsonDocument>
#include <QJsonObject>

PasskeyExporter::PasskeyExporter(QWidget* parent)
: m_parent(parent)
{
}

void PasskeyExporter::showExportDialog(const QList<Entry*>& items)
{
if (items.isEmpty()) {
return;
}

PasskeyExportDialog passkeyExportDialog;
PasskeyExportDialog passkeyExportDialog(m_parent);
passkeyExportDialog.setEntries(items);
auto ret = passkeyExportDialog.exec();

Expand Down Expand Up @@ -69,7 +74,7 @@ void PasskeyExporter::exportSelectedEntry(const Entry* entry, const QString& fol
{
const auto fullPath = QString("%1/%2.passkey").arg(folder, Tools::cleanFilename(entry->title()));
if (QFile::exists(fullPath)) {
auto dialogResult = MessageBox::warning(nullptr,
auto dialogResult = MessageBox::warning(m_parent,
tr("KeePassXC: Passkey Export"),
tr("File \"%1.passkey\" already exists.\n"
"Do you want to overwrite it?\n")
Expand All @@ -84,7 +89,7 @@ void PasskeyExporter::exportSelectedEntry(const Entry* entry, const QString& fol
QFile passkeyFile(fullPath);
if (!passkeyFile.open(QIODevice::WriteOnly)) {
MessageBox::information(
nullptr, tr("Cannot open file"), tr("Cannot open file \"%1\" for writing.").arg(fullPath));
m_parent, tr("Cannot open file"), tr("Cannot open file \"%1\" for writing.").arg(fullPath));
return;
}

Expand Down
9 changes: 7 additions & 2 deletions src/gui/passkeys/PasskeyExporter.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2023 KeePassXC Team <[email protected]>
* Copyright (C) 2024 KeePassXC Team <[email protected]>
*
* 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
Expand All @@ -20,6 +20,8 @@

#include <QList>
#include <QObject>
#include <QPointer>
#include <QWidget>

class Entry;

Expand All @@ -28,12 +30,15 @@ class PasskeyExporter : public QObject
Q_OBJECT

public:
explicit PasskeyExporter() = default;
explicit PasskeyExporter(QWidget* parent = nullptr);

void showExportDialog(const QList<Entry*>& items);

private:
void exportSelectedEntry(const Entry* entry, const QString& folder);

private:
QPointer<QWidget> m_parent;
};

#endif // KEEPASSXC_PASSKEYEXPORTER_H
7 changes: 6 additions & 1 deletion src/gui/passkeys/PasskeyImportDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ void PasskeyImportDialog::setInfo(const QString& relyingParty,
const QSharedPointer<Database>& database,
bool isEntry,
const QString& titleText,
const QString& infoText)
const QString& infoText,
const QString& importButtonText)
{
m_ui->relyingPartyLabel->setText(tr("Relying Party: %1").arg(relyingParty));
m_ui->usernameLabel->setText(tr("Username: %1").arg(username));
Expand Down Expand Up @@ -80,6 +81,10 @@ void PasskeyImportDialog::setInfo(const QString& relyingParty,
if (!infoText.isEmpty()) {
m_ui->infoLabel->setText(infoText);
}

if (!importButtonText.isEmpty()) {
m_ui->importButton->setText(importButtonText);
}
}

QSharedPointer<Database> PasskeyImportDialog::getSelectedDatabase() const
Expand Down
3 changes: 2 additions & 1 deletion src/gui/passkeys/PasskeyImportDialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ class PasskeyImportDialog : public QDialog
const QSharedPointer<Database>& database,
bool isEntry,
const QString& titleText = {},
const QString& infoText = {});
const QString& infoText = {},
const QString& importButtonText = {});
QSharedPointer<Database> getSelectedDatabase() const;
QUuid getSelectedEntryUuid() const;
QUuid getSelectedGroupUuid() const;
Expand Down
22 changes: 14 additions & 8 deletions src/gui/passkeys/PasskeyImporter.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2023 KeePassXC Team <[email protected]>
* Copyright (C) 2024 KeePassXC Team <[email protected]>
*
* 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
Expand Down Expand Up @@ -30,6 +30,11 @@

static const QString IMPORTED_PASSKEYS_GROUP = QStringLiteral("Imported Passkeys");

PasskeyImporter::PasskeyImporter(QWidget* parent)
: m_parent(parent)
{
}

void PasskeyImporter::importPasskey(QSharedPointer<Database>& database, Entry* entry)
{
auto filter = QString("%1 (*.passkey);;%2 (*)").arg(tr("Passkey file"), tr("All files"));
Expand All @@ -56,7 +61,7 @@ void PasskeyImporter::importSelectedFile(QFile& file, QSharedPointer<Database>&
const auto fileData = file.readAll();
const auto passkeyObject = browserMessageBuilder()->getJsonObject(fileData);
if (passkeyObject.isEmpty()) {
MessageBox::information(nullptr,
MessageBox::information(m_parent,
tr("Cannot import passkey"),
tr("Cannot import passkey file \"%1\". Data is missing.").arg(file.fileName()));
return;
Expand All @@ -70,16 +75,15 @@ void PasskeyImporter::importSelectedFile(QFile& file, QSharedPointer<Database>&
<< "credentialId"
<< "userHandle"
<< "privateKey");

if (!missingKeys.isEmpty()) {
MessageBox::information(nullptr,
MessageBox::information(m_parent,
tr("Cannot import passkey"),
tr("Cannot import passkey file \"%1\".\nThe following data is missing:\n%2")
.arg(file.fileName(), missingKeys.join(", ")));
} else if (!privateKey.startsWith("-----BEGIN PRIVATE KEY-----")
|| !privateKey.trimmed().endsWith("-----END PRIVATE KEY-----")) {
MessageBox::information(
nullptr,
m_parent,
tr("Cannot import passkey"),
tr("Cannot import passkey file \"%1\". Private key is missing or malformed.").arg(file.fileName()));
} else {
Expand All @@ -101,10 +105,12 @@ bool PasskeyImporter::showImportDialog(QSharedPointer<Database>& database,
const QString& userHandle,
const QString& privateKey,
const QString& titleText,
const QString& infoText)
const QString& infoText,
const QString& importButtonText)
{
PasskeyImportDialog passkeyImportDialog;
passkeyImportDialog.setInfo(relyingParty, username, database, entry != nullptr, titleText, infoText);
PasskeyImportDialog passkeyImportDialog(m_parent);
passkeyImportDialog.setInfo(
relyingParty, username, database, entry != nullptr, titleText, infoText, importButtonText);

auto ret = passkeyImportDialog.exec();
if (ret != QDialog::Accepted) {
Expand Down
11 changes: 8 additions & 3 deletions src/gui/passkeys/PasskeyImporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
#include "core/Database.h"
#include <QFile>
#include <QObject>
#include <QPointer>
#include <QUuid>
#include <QWidget>

class Entry;

Expand All @@ -30,7 +32,7 @@ class PasskeyImporter : public QObject
Q_OBJECT

public:
explicit PasskeyImporter() = default;
explicit PasskeyImporter(QWidget* parent = nullptr);

void importPasskey(QSharedPointer<Database>& database, Entry* entry = nullptr);
bool showImportDialog(QSharedPointer<Database>& database,
Expand All @@ -42,12 +44,15 @@ class PasskeyImporter : public QObject
const QString& userHandle,
const QString& privateKey,
const QString& titleText = {},
const QString& infoText = {});
const QString& infoText = {},
const QString& importButtonText = {});

private:
void importSelectedFile(QFile& file, QSharedPointer<Database>& database, Entry* entry);

Group* getDefaultGroup(QSharedPointer<Database>& database) const;

private:
QPointer<QWidget> m_parent;
};

#endif // KEEPASSXC_PASSKEYIMPORTER_H
4 changes: 2 additions & 2 deletions src/gui/reports/ReportsWidgetPasskeys.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ void ReportsWidgetPasskeys::selectionChanged()

void ReportsWidgetPasskeys::importPasskey()
{
PasskeyImporter passkeyImporter;
PasskeyImporter passkeyImporter(this);
passkeyImporter.importPasskey(m_db);

updateEntries();
Expand All @@ -300,6 +300,6 @@ void ReportsWidgetPasskeys::exportPasskey()
return;
}

PasskeyExporter passkeyExporter;
PasskeyExporter passkeyExporter(this);
passkeyExporter.showExportDialog(getSelectedEntries());
}

0 comments on commit 5018674

Please sign in to comment.