-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add New Entry Attachments dialog and functionality
Fixes #11506
- Loading branch information
w15dev
committed
Jan 7, 2025
1 parent
17dc022
commit eae01f6
Showing
8 changed files
with
252 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* Copyright (C) 2021 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 | ||
* 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 "NewEntryAttachmentsDialog.h" | ||
#include "core/EntryAttachments.h" | ||
#include "ui_NewEntryAttachmentsDialog.h" | ||
|
||
#include <QMessageBox> | ||
#include <QPushButton> | ||
|
||
#include <optional> | ||
|
||
NewEntryAttachmentsDialog::NewEntryAttachmentsDialog(QPointer<EntryAttachments> attachments, QWidget* parent) | ||
: QDialog(parent) | ||
, m_attachments(std::move(attachments)) | ||
, m_ui(new Ui::NewEntryAttachmentsDialog) | ||
{ | ||
m_ui->setupUi(this); | ||
|
||
connect(m_ui->dialogButtons, SIGNAL(accepted()), this, SLOT(saveAttachment())); | ||
connect(m_ui->dialogButtons, SIGNAL(rejected()), this, SLOT(reject())); | ||
connect(m_ui->titleEdit, SIGNAL(textChanged(const QString&)), this, SLOT(fileNameTextChanged(const QString&))); | ||
|
||
fileNameTextChanged(m_ui->titleEdit->text()); | ||
} | ||
|
||
NewEntryAttachmentsDialog::~NewEntryAttachmentsDialog() = default; | ||
|
||
std::optional<QString> NewEntryAttachmentsDialog::ValidateFileName(const QString& fileName) const | ||
{ | ||
if (fileName.isEmpty()) { | ||
return tr("Attachment name cannot be empty"); | ||
} | ||
|
||
if (m_attachments->hasKey(fileName)) { | ||
return tr("Attachment with the same name already exists"); | ||
} | ||
|
||
return std::nullopt; | ||
} | ||
|
||
void NewEntryAttachmentsDialog::saveAttachment() | ||
{ | ||
auto fileName = m_ui->titleEdit->text(); | ||
auto text = m_ui->attachmentTextEdit->toPlainText().toUtf8(); | ||
|
||
if (auto error = ValidateFileName(fileName); error) { | ||
QMessageBox::warning(this, tr("Save attachment"), error.value()); | ||
return; | ||
} | ||
|
||
m_attachments->set(fileName, text); | ||
|
||
accept(); | ||
} | ||
|
||
void NewEntryAttachmentsDialog::fileNameTextChanged(const QString& fileName) | ||
{ | ||
const auto error = ValidateFileName(fileName); | ||
|
||
m_ui->errorLabel->setText(error.value_or(QString{})); | ||
m_ui->errorLabel->setVisible(error.has_value()); | ||
|
||
if (auto okButton = m_ui->dialogButtons->button(QDialogButtonBox::Ok); okButton) { | ||
okButton->setDisabled(error.has_value()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright (C) 2021 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 | ||
* 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 NEWENTRYATTACHMENTSWIDGET_H | ||
#define NEWENTRYATTACHMENTSWIDGET_H | ||
|
||
#include <QDialog> | ||
#include <QPointer> | ||
|
||
#include <optional> | ||
#include <qwidget.h> | ||
|
||
namespace Ui | ||
{ | ||
class NewEntryAttachmentsDialog; | ||
} | ||
|
||
class QByteArray; | ||
class EntryAttachments; | ||
|
||
class NewEntryAttachmentsDialog : public QDialog | ||
{ | ||
Q_OBJECT | ||
public: | ||
explicit NewEntryAttachmentsDialog(QPointer<EntryAttachments> attachments, QWidget* parent = nullptr); | ||
~NewEntryAttachmentsDialog() override; | ||
|
||
private slots: | ||
void saveAttachment(); | ||
void fileNameTextChanged(const QString& fileName); | ||
|
||
private: | ||
std::optional<QString> ValidateFileName(const QString& fileName) const; | ||
|
||
private: | ||
QPointer<EntryAttachments> m_attachments; | ||
|
||
QScopedPointer<Ui::NewEntryAttachmentsDialog> m_ui; | ||
}; | ||
|
||
#endif // NEWENTRYATTACHMENTSWIDGET_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<ui version="4.0"> | ||
<class>NewEntryAttachmentsDialog</class> | ||
<widget class="QDialog" name="NewEntryAttachmentsDialog"> | ||
<property name="geometry"> | ||
<rect> | ||
<x>0</x> | ||
<y>0</y> | ||
<width>402</width> | ||
<height>300</height> | ||
</rect> | ||
</property> | ||
<property name="windowTitle"> | ||
<string>New entry attachments</string> | ||
</property> | ||
<layout class="QVBoxLayout" name="verticalLayout"> | ||
<item> | ||
<widget class="QLineEdit" name="titleEdit"> | ||
<property name="placeholderText"> | ||
<string>File name</string> | ||
</property> | ||
</widget> | ||
</item> | ||
<item> | ||
<widget class="QLabel" name="errorLabel"> | ||
<property name="enabled"> | ||
<bool>true</bool> | ||
</property> | ||
<property name="styleSheet"> | ||
<string notr="true">color: #FF9696</string> | ||
</property> | ||
<property name="text"> | ||
<string/> | ||
</property> | ||
</widget> | ||
</item> | ||
<item> | ||
<widget class="QTextEdit" name="attachmentTextEdit"> | ||
<property name="placeholderText"> | ||
<string>File contents...</string> | ||
</property> | ||
</widget> | ||
</item> | ||
<item> | ||
<widget class="QDialogButtonBox" name="dialogButtons"> | ||
<property name="standardButtons"> | ||
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set> | ||
</property> | ||
</widget> | ||
</item> | ||
</layout> | ||
</widget> | ||
<resources/> | ||
<connections/> | ||
</ui> |