Replace MessageBox with MessageWidget in remaining classes.

Chnage to one method to set MessageWidget text passing type as
parameter.
Only messages with questions requiring user input reamin using
MessageBox dialog.
Use signal/slots to set message in MessageWidget and hide message,
signal/slots only used when required.Maybe need to change all calls to
signals/slots in the future.
This commit is contained in:
Pedro Alves 2015-01-21 16:19:05 +00:00 committed by Janek Bevendorff
parent c2826bb1af
commit 13c85cdfcb
No known key found for this signature in database
GPG Key ID: CFEC2F6850BFFA53
20 changed files with 169 additions and 77 deletions

View File

@ -54,7 +54,7 @@ void ChangeMasterKeyWidget::createKeyFile()
QString errorMsg;
bool created = FileKey::create(fileName, &errorMsg);
if (!created) {
m_ui->messageWidget->showMessageError(tr("Unable to create Key File : ").append(errorMsg));
m_ui->messageWidget->showMessage(tr("Unable to create Key File : ").append(errorMsg), MessageWidget::Error);
}
else {
m_ui->keyFileCombo->setEditText(fileName);
@ -112,7 +112,7 @@ void ChangeMasterKeyWidget::generateKey()
m_key.addKey(PasswordKey(m_ui->enterPasswordEdit->text()));
}
else {
m_ui->messageWidget->showMessageError(tr("Different passwords supplied."));
m_ui->messageWidget->showMessage(tr("Different passwords supplied."), MessageWidget::Error);
m_ui->enterPasswordEdit->setText("");
m_ui->repeatPasswordEdit->setText("");
return;
@ -123,13 +123,14 @@ void ChangeMasterKeyWidget::generateKey()
QString errorMsg;
QString fileKeyName = m_ui->keyFileCombo->currentText();
if (!fileKey.load(fileKeyName, &errorMsg)) {
m_ui->messageWidget->showMessageError(
tr("Failed to set %1 as the Key file:\n%2: ").arg(fileKeyName, errorMsg));
m_ui->messageWidget->showMessage(
tr("Failed to set %1 as the Key file:\n%2").arg(fileKeyName, errorMsg), MessageWidget::Error);
return;
}
m_key.addKey(fileKey);
}
m_ui->messageWidget->hideMessage();
Q_EMIT editFinished(true);
}

View File

@ -108,8 +108,8 @@ void DatabaseOpenWidget::openDatabase()
QFile file(m_filename);
if (!file.open(QIODevice::ReadOnly)) {
m_ui->messageWidget->showMessageError(
tr("Unable to open the database.").append("\n").append(file.errorString()));
m_ui->messageWidget->showMessage(
tr("Unable to open the database.").append("\n").append(file.errorString()), MessageWidget::Error);
return;
}
if (m_db) {
@ -126,8 +126,8 @@ void DatabaseOpenWidget::openDatabase()
Q_EMIT editFinished(true);
}
else {
m_ui->messageWidget->showMessageError(tr("Unable to open the database.")
.append("\n").append(reader.errorString()));
m_ui->messageWidget->showMessage(tr("Unable to open the database.")
.append("\n").append(reader.errorString()), MessageWidget::Error);
m_ui->editPassword->clear();
}
}
@ -147,7 +147,8 @@ CompositeKey DatabaseOpenWidget::databaseKey()
QString keyFilename = m_ui->comboKeyFile->currentText();
QString errorMsg;
if (!key.load(keyFilename, &errorMsg)) {
m_ui->messageWidget->showMessageError(tr("Can't open key file").append(":\n").append(errorMsg));
m_ui->messageWidget->showMessage(tr("Can't open key file").append(":\n")
.append(errorMsg), MessageWidget::Error);
return CompositeKey();
}
masterKey.addKey(key);

View File

@ -116,7 +116,7 @@ void DatabaseTabWidget::openDatabase(const QString& fileName, const QString& pw,
QFileInfo fileInfo(fileName);
QString canonicalFilePath = fileInfo.canonicalFilePath();
if (canonicalFilePath.isEmpty()) {
MessageBox::warning(this, tr("Warning"), tr("File not found!"));
Q_EMIT messageGlobal(tr("File not found!"), MessageWidget::Error);
return;
}
@ -136,8 +136,9 @@ void DatabaseTabWidget::openDatabase(const QString& fileName, const QString& pw,
QFile file(fileName);
if (!file.open(QIODevice::ReadWrite)) {
if (!file.open(QIODevice::ReadOnly)) {
MessageBox::warning(this, tr("Error"), tr("Unable to open the database.").append("\n")
.append(file.errorString()));
// can't open
Q_EMIT messageGlobal(
tr("Unable to open the database.").append("\n").append(file.errorString()), MessageWidget::Error);
return;
}
else {
@ -184,6 +185,10 @@ void DatabaseTabWidget::openDatabase(const QString& fileName, const QString& pw,
insertDatabase(db, dbStruct);
if (dbStruct.readOnly) {
Q_EMIT messageTab(tr("File opened in read only mode."), MessageWidget::Warning);
}
updateLastDatabases(dbStruct.filePath);
if (!pw.isNull() || !keyFile.isEmpty()) {
@ -192,6 +197,7 @@ void DatabaseTabWidget::openDatabase(const QString& fileName, const QString& pw,
else {
dbStruct.dbWidget->switchToOpenDatabase(dbStruct.filePath);
}
Q_EMIT messageDismissGlobal();
}
void DatabaseTabWidget::mergeDatabase()
@ -332,6 +338,7 @@ bool DatabaseTabWidget::saveDatabase(Database* db)
dbStruct.modified = false;
dbStruct.dbWidget->databaseSaved();
updateTabName(db);
Q_EMIT messageDismissTab();
return true;
}
else {
@ -341,8 +348,8 @@ bool DatabaseTabWidget::saveDatabase(Database* db)
}
}
else {
MessageBox::critical(this, tr("Error"), tr("Writing the database failed.") + "\n\n"
+ saveFile.errorString());
Q_EMIT messageTab(tr("Writing the database failed.\n")
.append("\n").append(saveFile.errorString()), MessageWidget::Error);
return false;
}
}
@ -486,8 +493,9 @@ void DatabaseTabWidget::exportToCsv()
CsvExporter csvExporter;
if (!csvExporter.exportDatabase(fileName, db)) {
MessageBox::critical(this, tr("Error"), tr("Writing the CSV file failed.") + "\n\n"
+ csvExporter.errorString());
Q_EMIT messageGlobal(
tr("Writing the CSV file failed.").append("\n\n")
.append(csvExporter.errorString()), MessageWidget::Error);
}
}

View File

@ -23,12 +23,14 @@
#include "format/KeePass2Writer.h"
#include "gui/DatabaseWidget.h"
#include "gui/MessageWidget.h"
class DatabaseWidget;
class DatabaseWidgetStateSync;
class DatabaseOpenWidget;
class QFile;
class QLockFile;
class MessageWidget;
struct DatabaseManagerStruct
{
@ -84,6 +86,10 @@ Q_SIGNALS:
void activateDatabaseChanged(DatabaseWidget* dbWidget);
void databaseLocked(DatabaseWidget* dbWidget);
void databaseUnlocked(DatabaseWidget* dbWidget);
void messageGlobal(const QString&, MessageWidget::MessageType type);
void messageTab(const QString&, MessageWidget::MessageType type);
void messageDismissGlobal();
void messageDismissTab();
private Q_SLOTS:
void updateTabName(Database* db);

View File

@ -60,7 +60,14 @@ DatabaseWidget::DatabaseWidget(Database* db, QWidget* parent)
, m_newParent(nullptr)
{
m_mainWidget = new QWidget(this);
QLayout* layout = new QHBoxLayout(m_mainWidget);
m_messageWidget = new MessageWidget(this);
m_messageWidget->setHidden(true);
QVBoxLayout* mainLayout = new QVBoxLayout();
QLayout* layout = new QHBoxLayout();
mainLayout->addWidget(m_messageWidget);
mainLayout->addLayout(layout);
m_splitter = new QSplitter(m_mainWidget);
m_splitter->setChildrenCollapsible(false);
@ -105,7 +112,7 @@ DatabaseWidget::DatabaseWidget(Database* db, QWidget* parent)
m_splitter->setStretchFactor(1, 70);
layout->addWidget(m_splitter);
m_mainWidget->setLayout(layout);
m_mainWidget->setLayout(mainLayout);
m_editEntryWidget = new EditEntryWidget();
m_editEntryWidget->setObjectName("editEntryWidget");
@ -1220,3 +1227,15 @@ void DatabaseWidget::closeUnlockDialog()
{
m_unlockDatabaseDialog->close();
}
void DatabaseWidget::showMessage(const QString& text, MessageWidget::MessageType type)
{
m_messageWidget->showMessage(text, type);
}
void DatabaseWidget::hideMessage()
{
if (m_messageWidget->isVisible()) {
m_messageWidget->animatedHide();
}
}

View File

@ -26,6 +26,7 @@
#include "core/Uuid.h"
#include "gui/entry/EntryModel.h"
#include "gui/MessageWidget.h"
class ChangeMasterKeyWidget;
class DatabaseOpenWidget;
@ -43,9 +44,14 @@ class QMenu;
class QSplitter;
class QLabel;
class UnlockDatabaseWidget;
class MessageWidget;
class UnlockDatabaseDialog;
class QFileSystemWatcher;
namespace Ui {
class SearchWidget;
}
class DatabaseWidget : public QStackedWidget
{
Q_OBJECT
@ -145,6 +151,8 @@ public Q_SLOTS:
void search(const QString& searchtext);
void setSearchCaseSensitive(bool state);
void endSearch();
void showMessage(const QString& text, MessageWidget::MessageType type);
void hideMessage();
private Q_SLOTS:
void entryActivationSignalReceived(Entry* entry, EntryModel::ModelColumn column);
@ -192,6 +200,7 @@ private:
QString m_filename;
Uuid m_groupBeforeLock;
Uuid m_entryBeforeLock;
MessageWidget* m_messageWidget;
// Search state
QString m_lastSearchText;

View File

@ -149,3 +149,4 @@ QVariant DatabaseWidgetStateSync::intListToVariant(const QList<int>& list)
return result;
}

View File

@ -89,19 +89,9 @@ bool EditWidget::readOnly() const
return m_readOnly;
}
void EditWidget::showMessageError(const QString& text)
void EditWidget::showMessage(const QString& text, MessageWidget::MessageType type)
{
m_ui->messageWidget->showMessageError(text);
}
void EditWidget::showMessageWarning(const QString& text)
{
m_ui->messageWidget->showMessageWarning(text);
}
void EditWidget::showMessageInformation(const QString& text)
{
m_ui->messageWidget->showMessageInformation(text);
m_ui->messageWidget->showMessage(text, type);
}
void EditWidget::hideMessage()

View File

@ -21,6 +21,7 @@
#include <QScopedPointer>
#include "gui/DialogyWidget.h"
#include "gui/MessageWidget.h"
class QLabel;
@ -48,10 +49,8 @@ Q_SIGNALS:
void accepted();
void rejected();
protected:
void showMessageError(const QString& text);
void showMessageWarning(const QString& text);
void showMessageInformation(const QString& text);
protected Q_SLOTS:
void showMessage(const QString& text, MessageWidget::MessageType type);
void hideMessage();
private:

View File

@ -248,7 +248,7 @@ void EditWidgetIcons::addCustomIcon()
m_ui->customIconsView->setCurrentIndex(index);
}
else {
MessageBox::critical(this, tr("Error"), tr("Can't read icon"));
Q_EMIT messageEditEntry(tr("Can't read icon"), MessageWidget::Error);
}
}
}
@ -302,9 +302,8 @@ void EditWidgetIcons::removeCustomIcon()
}
}
else {
MessageBox::information(this, tr("Can't delete icon!"),
tr("Can't delete icon. Still used by %1 items.")
.arg(iconUsedCount));
Q_EMIT messageEditEntry(
tr("Can't delete icon. Still used by %1 items.").arg(iconUsedCount), MessageWidget::Error);
}
}
}

View File

@ -26,6 +26,7 @@
#include "core/Global.h"
#include "core/Uuid.h"
#include "gui/MessageWidget.h"
class Database;
class DefaultIconModel;
@ -58,6 +59,10 @@ public:
public Q_SLOTS:
void setUrl(const QString &url);
Q_SIGNALS:
void messageEditEntry(QString, MessageWidget::MessageType);
void messageEditEntryDismiss();
private Q_SLOTS:
void downloadFavicon();
void fetchFavicon(QUrl url);

View File

@ -65,8 +65,9 @@ void KeePass1OpenWidget::openDatabase()
Q_EMIT editFinished(true);
}
else {
MessageBox::warning(this, tr("Error"), tr("Unable to open the database.").append("\n")
.append(reader.errorString()));
m_ui->messageWidget->showMessage(tr("Unable to open the database.").append("\n")
.append(reader.errorString()), MessageWidget::Error);
m_ui->editPassword->clear();
}
}

View File

@ -104,6 +104,7 @@ MainWindow::MainWindow()
#endif
setWindowIcon(filePath()->applicationIcon());
m_ui->globalMessageWidget->setHidden(true);
QAction* toggleViewAction = m_ui->toolBar->toggleViewAction();
toggleViewAction->setText(tr("Show toolbar"));
m_ui->menuView->addAction(toggleViewAction);
@ -277,6 +278,11 @@ MainWindow::MainWindow()
connect(m_ui->actionAbout, SIGNAL(triggered()), SLOT(showAboutDialog()));
connect(m_ui->tabWidget, SIGNAL(messageGlobal(QString,MessageWidget::MessageType)), this, SLOT(displayGlobalMessage(QString, MessageWidget::MessageType)));
connect(m_ui->tabWidget, SIGNAL(messageDismissGlobal()), this, SLOT(hideGlobalMessage()));
connect(m_ui->tabWidget, SIGNAL(messageTab(QString,MessageWidget::MessageType)), this, SLOT(displayTabMessage(QString, MessageWidget::MessageType)));
connect(m_ui->tabWidget, SIGNAL(messageDismissTab()), this, SLOT(hideTabMessage()));
updateTrayIcon();
}
@ -787,3 +793,28 @@ bool MainWindow::isTrayIconEnabled() const
&& QSystemTrayIcon::isSystemTrayAvailable();
#endif
}
void MainWindow::displayGlobalMessage(const QString& text, MessageWidget::MessageType type)
{
m_ui->globalMessageWidget->showMessage(text, type);
}
void MainWindow::displayTabMessage(const QString& text, MessageWidget::MessageType type)
{
//if (m_ui->stackedWidget->currentIndex() == 0) {
m_ui->tabWidget->currentDatabaseWidget()->showMessage(text, type);
//}
}
void MainWindow::hideGlobalMessage()
{
m_ui->globalMessageWidget->hideMessage();
}
void MainWindow::hideTabMessage()
{
if (m_ui->stackedWidget->currentIndex() == 0) {
m_ui->tabWidget->currentDatabaseWidget()->hideMessage();
}
}

View File

@ -71,6 +71,10 @@ private Q_SLOTS:
void toggleWindow();
void lockDatabasesAfterInactivity();
void repairDatabase();
void displayGlobalMessage(const QString& text, MessageWidget::MessageType type);
void displayTabMessage(const QString& text, MessageWidget::MessageType type);
void hideGlobalMessage();
void hideTabMessage();
private:
static void setShortcut(QAction* action, QKeySequence::StandardKey standard, int fallback = 0);

View File

@ -2,6 +2,9 @@
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="enabled">
<bool>true</bool>
</property>
<property name="geometry">
<rect>
<x>0</x>
@ -14,6 +17,9 @@
<string notr="true">KeePassXC</string>
</property>
<widget class="QWidget" name="centralwidget">
<property name="enabled">
<bool>true</bool>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<property name="leftMargin">
<number>0</number>
@ -27,8 +33,24 @@
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="MessageWidget" name="globalMessageWidget" native="true">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
</item>
<item>
<widget class="QStackedWidget" name="stackedWidget">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="currentIndex">
<number>2</number>
</property>
@ -104,7 +126,7 @@
<x>0</x>
<y>0</y>
<width>800</width>
<height>26</height>
<height>29</height>
</rect>
</property>
<widget class="QMenu" name="menuFile">
@ -452,6 +474,12 @@
</action>
</widget>
<customwidgets>
<customwidget>
<class>MessageWidget</class>
<extends>QWidget</extends>
<header>gui/MessageWidget.h</header>
<container>1</container>
</customwidget>
<customwidget>
<class>DatabaseTabWidget</class>
<extends>QTabWidget</extends>

View File

@ -23,32 +23,14 @@ MessageWidget::MessageWidget(QWidget* parent)
}
void MessageWidget::showMessageError(const QString& text)
{
showMessage(text, MessageType::Error);
}
void MessageWidget::showMessageWarning(const QString& text)
{
showMessage(text, MessageType::Warning);
}
void MessageWidget::showMessageInformation(const QString& text)
{
showMessage(text, MessageType::Information);
}
void MessageWidget::showMessagePositive(const QString& text)
{
showMessage(text, MessageType::Positive);
}
void MessageWidget::showMessage(const QString& text, MessageType type)
void MessageWidget::showMessage(const QString& text, MessageWidget::MessageType type)
{
setMessageType(type);
setText(text);
animatedShow();
}
void MessageWidget::hideMessage()
{
animatedHide();
}

View File

@ -7,15 +7,15 @@
class MessageWidget : public KMessageWidget
{
Q_OBJECT
public:
explicit MessageWidget(QWidget* parent = 0);
void showMessageError(const QString& text);
void showMessageWarning(const QString& text);
void showMessageInformation(const QString& text);
void showMessagePositive(const QString& text);
private:
void showMessage(const QString& text, MessageType type);
public Q_SLOTS:
void showMessage(const QString& text, MessageWidget::MessageType type);
void hideMessage();
};
#endif // MESSAGEWIDGET_H

View File

@ -77,6 +77,9 @@ EditEntryWidget::EditEntryWidget(QWidget* parent)
connect(this, SIGNAL(accepted()), SLOT(saveEntry()));
connect(this, SIGNAL(rejected()), SLOT(cancel()));
connect(m_iconsWidget, SIGNAL(messageEditEntry(QString, MessageWidget::MessageType)), SLOT(showMessage(QString, MessageWidget::MessageType)));
connect(m_iconsWidget, SIGNAL(messageEditEntryDismiss()), SLOT(hideMessage()));
}
EditEntryWidget::~EditEntryWidget()
@ -401,7 +404,7 @@ void EditEntryWidget::saveEntry()
}
if (!passwordsEqual()) {
showMessageError(tr("Different passwords supplied."));
showMessage(tr("Different passwords supplied."), MessageWidget::Error);
return;
}
@ -633,13 +636,13 @@ void EditEntryWidget::insertAttachment()
QFile file(filename);
if (!file.open(QIODevice::ReadOnly)) {
showMessageError(tr("Unable to open file").append(":\n").append(file.errorString()));
showMessage(tr("Unable to open file").append(":\n").append(file.errorString()), MessageWidget::Error);
return;
}
QByteArray data;
if (!Tools::readAllFromDevice(&file, data)) {
showMessageError(tr("Unable to open file").append(":\n").append(file.errorString()));
showMessage(tr("Unable to open file").append(":\n").append(file.errorString()), MessageWidget::Error);
return;
}
@ -666,11 +669,11 @@ void EditEntryWidget::saveCurrentAttachment()
QFile file(savePath);
if (!file.open(QIODevice::WriteOnly)) {
showMessageError(tr("Unable to save the attachment:\n").append(file.errorString()));
showMessage(tr("Unable to save the attachment:\n").append(file.errorString()), MessageWidget::Error);
return;
}
if (file.write(attachmentData) != attachmentData.size()) {
showMessageError(tr("Unable to save the attachment:\n").append(file.errorString()));
showMessage(tr("Unable to save the attachment:\n").append(file.errorString()), MessageWidget::Error);
return;
}
}
@ -691,12 +694,12 @@ void EditEntryWidget::openAttachment(const QModelIndex& index)
QTemporaryFile* file = new QTemporaryFile(tmpFileTemplate, this);
if (!file->open()) {
showMessageError(tr("Unable to save the attachment:\n").append(file->errorString()));
showMessage(tr("Unable to save the attachment:\n").append(file->errorString()), MessageWidget::Error);
return;
}
if (file->write(attachmentData) != attachmentData.size()) {
showMessageError(tr("Unable to save the attachment:\n").append(file->errorString()));
showMessage(tr("Unable to save the attachment:\n").append(file->errorString()), MessageWidget::Error);
return;
}

View File

@ -43,6 +43,9 @@ EditGroupWidget::EditGroupWidget(QWidget* parent)
connect(this, SIGNAL(accepted()), SLOT(save()));
connect(this, SIGNAL(rejected()), SLOT(cancel()));
connect(m_editGroupWidgetIcons, SIGNAL(messageEditEntry(QString, MessageWidget::MessageType)), SLOT(showMessage(QString, MessageWidget::MessageType)));
connect(m_editGroupWidgetIcons, SIGNAL(messageEditEntryDismiss()), SLOT(hideMessage()));
}
EditGroupWidget::~EditGroupWidget()

View File

@ -45,6 +45,8 @@ public:
Q_SIGNALS:
void editFinished(bool accepted);
void messageEditEntry(QString, MessageWidget::MessageType);
void messageEditEntryDismiss();
private Q_SLOTS:
void save();