From f863fb474c8668ee6c1467e435219f40bf235c2e Mon Sep 17 00:00:00 2001 From: Marius Kittler Date: Wed, 13 Mar 2019 19:09:31 +0100 Subject: [PATCH] Adapt to c++utilities v5 --- CMakeLists.txt | 8 +++++--- cli/cli.cpp | 16 ++++++---------- gui/mainwindow.cpp | 35 ++++++++++++++++------------------- main.cpp | 1 + model/entrymodel.cpp | 10 +++------- quickgui/controller.cpp | 23 ++++++++++++----------- quickgui/controller.h | 2 +- quickgui/initiatequick.cpp | 1 + 8 files changed, 45 insertions(+), 51 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 03c65de..2786909 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -114,18 +114,20 @@ set(REQUIRED_ICONS window-close) # find c++utilities -find_package(c++utilities 4.10.0 REQUIRED) +set(CONFIGURATION_PACKAGE_SUFFIX "" + CACHE STRING "sets the suffix for find_package() calls to packages configured via c++utilities") +find_package(c++utilities${CONFIGURATION_PACKAGE_SUFFIX} 5.0.0 REQUIRED) use_cpp_utilities() # apply basic configuration include(BasicConfig) # find qtutilities -find_package(qtutilities 5.11.0 REQUIRED) +find_package(qtutilities${CONFIGURATION_PACKAGE_SUFFIX} 6.0.0 REQUIRED) use_qt_utilities() # find passwordfile -find_package(passwordfile 4.0.0 REQUIRED) +find_package(passwordfile${CONFIGURATION_PACKAGE_SUFFIX} 5.0.0 REQUIRED) use_password_file() # require at least Qt 5.8 for the Qt Quick GUI diff --git a/cli/cli.cpp b/cli/cli.cpp index af2491f..8df48b3 100644 --- a/cli/cli.cpp +++ b/cli/cli.cpp @@ -9,7 +9,6 @@ #include #include #include -#include #if defined(PLATFORM_UNIX) #include @@ -260,10 +259,9 @@ void InteractiveCli::openFile(const string &file, PasswordFileOpenFlags openFlag } catch (const CryptoException &) { m_o << "error occured when decrypting file \"" << file << "\"" << endl; throw; - } catch (...) { - const char *what = catchIoFailure(); + } catch (const std::ios_base::failure &) { m_o << "IO error occured when opening file \"" << file << "\"" << endl; - throw ios_base::failure(what); + throw; } } catch (const std::exception &e) { if (*e.what() != 0) { @@ -312,10 +310,9 @@ void InteractiveCli::saveFile() } catch (const CryptoException &) { m_o << "error occured when encrypting file \"" << m_file.path() << "\"" << endl; throw; - } catch (...) { - const char *what = catchIoFailure(); + } catch (const std::ios_base::failure &) { m_o << "IO error occured when saving file \"" << m_file.path() << "\"" << endl; - throw ios_base::failure(what); + throw; } } catch (const exception &e) { if (*e.what() != 0) { @@ -341,10 +338,9 @@ void InteractiveCli::createFile(const string &file) m_file.generateRootEntry(); m_currentEntry = m_file.rootEntry(); m_o << "file \"" << file << "\" created and opened" << endl; - } catch (...) { - const char *what = catchIoFailure(); + } catch (const std::ios_base::failure &) { m_o << "IO error occured when creating file \"" << file << "\"" << endl; - throw ios_base::failure(what); + throw; } } catch (const exception &e) { if (*e.what() != 0) { diff --git a/gui/mainwindow.cpp b/gui/mainwindow.cpp index 4914428..bf689aa 100644 --- a/gui/mainwindow.cpp +++ b/gui/mainwindow.cpp @@ -22,7 +22,6 @@ #include #include -#include #include #include @@ -425,17 +424,15 @@ bool MainWindow::openFile(const QString &path, PasswordFileOpenFlags openFlags) m_file.setPath(path.toStdString()); try { m_file.open(m_openFlags = openFlags); - } catch (...) { - // catch std::ios_base::failure - const char *const ioError = catchIoFailure(); - + } catch (const std::ios_base::failure &failure) { // try read-only if (!(m_openFlags & PasswordFileOpenFlags::ReadOnly)) { return openFile(path, m_openFlags | PasswordFileOpenFlags::ReadOnly); } // show error message - const QString errmsg = tr("An IO error occured when opening the specified file \"%1\".\n\n(%2)").arg(path, QString::fromLocal8Bit(ioError)); + const QString errmsg + = tr("An IO error occured when opening the specified file \"%1\".\n\n(%2)").arg(path, QString::fromLocal8Bit(failure.what())); m_ui->statusBar->showMessage(errmsg, 5000); QMessageBox::critical(this, QApplication::applicationName(), errmsg); return false; @@ -480,9 +477,9 @@ bool MainWindow::openFile(const QString &path, PasswordFileOpenFlags openFlags) m_file.load(); } catch (const CryptoException &e) { msg = tr("The file couldn't be decrypted.\nOpenSSL error queue: %1").arg(QString::fromLocal8Bit(e.what())); - } catch (...) { + } catch (const std::ios_base::failure &failure) { try { - msg = QString::fromLocal8Bit(catchIoFailure()); + msg = QString::fromLocal8Bit(failure.what()); } catch (const runtime_error &e) { msg = tr("Unable to parse the file. %1").arg(QString::fromLocal8Bit(e.what())); } @@ -544,9 +541,9 @@ void MainWindow::createFile(const QString &path, const QString &password) try { m_openFlags = PasswordFileOpenFlags::Default; m_file.create(); - } catch (...) { - catchIoFailure(); - QMessageBox::critical(this, QApplication::applicationName(), tr("The file %1 couldn't be created.").arg(path)); + } catch (const std::ios_base::failure &failure) { + QMessageBox::critical(this, QApplication::applicationName(), + tr("The file %1 couldn't be created: %2").arg(path, QString::fromLocal8Bit(failure.what()))); return; } m_file.generateRootEntry(); @@ -742,8 +739,8 @@ bool MainWindow::askForCreatingFile() try { m_file.create(); updateWindowTitle(); - } catch (...) { - QMessageBox::critical(this, QApplication::applicationName(), QString::fromLocal8Bit(catchIoFailure())); + } catch (const std::ios_base::failure &failure) { + QMessageBox::critical(this, QApplication::applicationName(), QString::fromLocal8Bit(failure.what())); return false; } } @@ -830,9 +827,9 @@ bool MainWindow::saveFile() if (m_ui->actionAlwaysCreateBackup->isChecked()) { try { m_file.doBackup(); - } catch (...) { + } catch (const std::ios_base::failure &failure) { const QString message( - tr("The backup file couldn't be created because in IO error occured: %1").arg(QString::fromLocal8Bit(catchIoFailure()))); + tr("The backup file couldn't be created because in IO error occured: %1").arg(QString::fromLocal8Bit(failure.what()))); QMessageBox::critical(this, QApplication::applicationName(), message); m_ui->statusBar->showMessage(message, 7000); return false; @@ -865,8 +862,8 @@ bool MainWindow::saveFile() m_file.save(saveOptions()); } catch (const CryptoException &ex) { msg = tr("The password list couldn't be saved due to encryption failure.\nOpenSSL error queue: %1").arg(QString::fromLocal8Bit(ex.what())); - } catch (...) { - msg = QString::fromLocal8Bit(catchIoFailure()); + } catch (const std::ios_base::failure &failure) { + msg = QString::fromLocal8Bit(failure.what()); } // show status if (!msg.isEmpty()) { @@ -900,8 +897,8 @@ void MainWindow::exportFile() QString errmsg; try { m_file.exportToTextfile(targetPath.toStdString()); - } catch (...) { - errmsg = tr("The password list couldn't be exported. %1").arg(QString::fromLocal8Bit(catchIoFailure())); + } catch (const std::ios_base::failure &failure) { + errmsg = tr("The password list couldn't be exported. %1").arg(QString::fromLocal8Bit(failure.what())); } if (errmsg.isEmpty()) { m_ui->statusBar->showMessage(tr("The password list has been exported."), 5000); diff --git a/main.cpp b/main.cpp index d8af8ae..f0e86ef 100644 --- a/main.cpp +++ b/main.cpp @@ -7,6 +7,7 @@ #endif #include "resources/config.h" +#include "resources/qtconfig.h" #include diff --git a/model/entrymodel.cpp b/model/entrymodel.cpp index da591d9..0643391 100644 --- a/model/entrymodel.cpp +++ b/model/entrymodel.cpp @@ -7,8 +7,6 @@ #include #include -#include - #include #include #include @@ -246,8 +244,7 @@ QVariant EntryModel::data(const QModelIndex &index, int role) const // FIXME: make conversion to QByteArray more efficient const auto str(ss.str()); return QByteArray(str.data(), str.size()); - } catch (...) { - IoUtilities::catchIoFailure(); + } catch (const std::ios_base::failure &) { return false; } } @@ -318,9 +315,8 @@ bool EntryModel::setData(const QModelIndex &index, const QVariant &value, int ro } catch (const Io::ParsingException &parsingError) { cerr << "EntryModel::setData: parsing exception: " << parsingError.what() << endl; return false; - } catch (...) { - const char *const errorMessage(IoUtilities::catchIoFailure()); - cerr << "EntryModel::setData: IO exception: " << errorMessage << endl; + } catch (const std::ios_base::failure &failure) { + cerr << "EntryModel::setData: IO exception: " << failure.what() << endl; return false; } } diff --git a/quickgui/controller.cpp b/quickgui/controller.cpp index abba7ed..fd7fb84 100644 --- a/quickgui/controller.cpp +++ b/quickgui/controller.cpp @@ -6,7 +6,6 @@ #include -#include #include #include @@ -155,7 +154,7 @@ void Controller::load(const QString &filePath) } catch (const runtime_error &e) { emit fileError(tr("A parsing error occured when opening the file: ") + QString::fromLocal8Bit(e.what()), QStringLiteral("load")); } catch (...) { - emitIoError(tr("loading")); + emitFileError(tr("loading")); } } @@ -172,7 +171,7 @@ void Controller::create(const QString &filePath) } m_file.create(); } catch (...) { - emitIoError(tr("creating")); + emitFileError(tr("creating")); } m_file.generateRootEntry(); @@ -187,7 +186,7 @@ void Controller::close() m_file.close(); resetFileStatus(); } catch (...) { - emitIoError(tr("closing")); + emitFileError(tr("closing")); } } @@ -221,7 +220,7 @@ void Controller::save() return; } - m_file.fileStream().openFromFileDescriptor(newFileDescriptor, ios_base::out | ios_base::trunc | ios_base::binary); + m_file.fileStream().open(newFileDescriptor, ios_base::out | ios_base::trunc | ios_base::binary); m_file.write(flags); } else { #endif @@ -236,7 +235,7 @@ void Controller::save() } catch (const runtime_error &e) { emit fileError(tr("An internal error occured when saving the file: ") + QString::fromLocal8Bit(e.what()), QStringLiteral("save")); } catch (...) { - emitIoError(tr("saving")); + emitFileError(tr("saving")); } } @@ -273,10 +272,10 @@ void Controller::handleFileSelectionAcceptedDescriptor(const QString &nativeUrl, try { qDebug() << "Opening fd for native url: " << nativeUrl; m_file.setPath(fileName.toStdString()); - m_file.fileStream().openFromFileDescriptor(fileDescriptor, ios_base::in | ios_base::binary); + m_file.fileStream().open(fileDescriptor, ios_base::in | ios_base::binary); m_file.opened(); } catch (...) { - emitIoError(tr("opening from native file descriptor")); + emitFileError(tr("opening from native file descriptor")); } emit filePathChanged(m_filePath = m_fileName = fileName); handleFileSelectionAccepted(QString(), existing); @@ -424,11 +423,13 @@ void Controller::setFileOpen(bool fileOpen) } } -void Controller::emitIoError(const QString &when) +void Controller::emitFileError(const QString &when) { try { - const auto *const msg = catchIoFailure(); - emit fileError(tr("An IO error occured when %1 the file %2: ").arg(when, m_filePath) + QString::fromLocal8Bit(msg), QStringLiteral("load")); + throw; + } catch (const std::ios_base::failure &failure) { + emit fileError( + tr("An IO error occured when %1 the file %2: ").arg(when, m_filePath) + QString::fromLocal8Bit(failure.what()), QStringLiteral("load")); } catch (const exception &e) { emit fileError(tr("An unknown exception occured when %1 the file %2: ").arg(when, m_filePath) + QString::fromLocal8Bit(e.what()), QStringLiteral("load")); diff --git a/quickgui/controller.h b/quickgui/controller.h index d56801a..f75085a 100644 --- a/quickgui/controller.h +++ b/quickgui/controller.h @@ -136,7 +136,7 @@ private: void resetFileStatus(); void updateWindowTitle(); void setFileOpen(bool fileOpen); - void emitIoError(const QString &when); + void emitFileError(const QString &when); QModelIndex ensureSourceEntryIndex(const QModelIndex &entryIndexMaybeFromFilterModel) const; QSettings &m_settings; diff --git a/quickgui/initiatequick.cpp b/quickgui/initiatequick.cpp index 5c83b1c..a0a1215 100644 --- a/quickgui/initiatequick.cpp +++ b/quickgui/initiatequick.cpp @@ -5,6 +5,7 @@ #endif #include "resources/config.h" +#include "resources/qtconfig.h" // enable inline helper functions for Qt Quick provided by qtutilities #define QT_UTILITIES_GUI_QTQUICK