From cd2987635b9483c4bd941cab048b493fdb8c67b3 Mon Sep 17 00:00:00 2001 From: alex1701c Date: Thu, 5 Dec 2019 08:39:18 +0100 Subject: Fix warnings and deprecations, optimize --- .gitignore | 2 ++ CMakeLists.txt | 2 ++ config.cpp | 36 +++++++++++++++++++------------ config.h | 11 ++++++---- install.sh | 4 ++-- pass.cpp | 68 +++++++++++++++++++++++++++------------------------------- pass.h | 2 +- 7 files changed, 68 insertions(+), 57 deletions(-) diff --git a/.gitignore b/.gitignore index 796b96d..f4774b0 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ /build +/cmake-build-debug +/.idea diff --git a/CMakeLists.txt b/CMakeLists.txt index aa7050a..1718abc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,3 +1,5 @@ +cmake_minimum_required(VERSION 2.8.12) + project(RunnerPass) find_package(ECM 5.12.0 REQUIRED NO_MODULE) diff --git a/config.cpp b/config.cpp index e342161..c218e58 100644 --- a/config.cpp +++ b/config.cpp @@ -20,6 +20,7 @@ #include #include #include +#include #include "config.h" @@ -46,24 +47,27 @@ PassConfigForm::PassConfigForm(QWidget *parent) : QWidget(parent) connect(this->buttonAddAction, &QPushButton::clicked, [this]() { this->addPassAction(this->lineName->text(), this->lineIcon->text(), this->lineRegEx->text()); }); + + // Disable add button if the necessary field are not filled out + connect(this->lineIcon, SIGNAL(textChanged(QString)), this, SLOT(validateAddButton())); + connect(this->lineName, SIGNAL(textChanged(QString)), this, SLOT(validateAddButton())); + connect(this->lineRegEx, SIGNAL(textChanged(QString)), this, SLOT(validateAddButton())); + validateAddButton(); } void PassConfigForm::addPassAction(const QString &name, const QString &icon, const QString ®ex, bool isNew /* = true */) { // Checks - for (auto act: this->passActions()) + for (const auto& act: this->passActions()) if (act.name == name) return; - if (name.isEmpty() || icon.isEmpty() || regex.isEmpty()) - return; - // Widgets auto *listWidget = new QWidget(this); auto *layoutAction = new QHBoxLayout(listWidget); auto *buttonRemoveAction = new QToolButton(listWidget); - buttonRemoveAction->setIcon(QIcon::fromTheme("remove")); + buttonRemoveAction->setIcon(QIcon::fromTheme("delete")); layoutAction->setMargin(0); layoutAction->addStretch(); layoutAction->addWidget(buttonRemoveAction); @@ -91,7 +95,8 @@ void PassConfigForm::addPassAction(const QString &name, const QString &icon, con QVector PassConfigForm::passActions() { QVector passActions; - for(int i = 0; i < this->listSavedActions->count(); ++i) { + const int listSavedActionsCount = this->listSavedActions->count(); + for(int i = 0; i < listSavedActionsCount; ++i) { QListWidgetItem* item = this->listSavedActions->item(i); passActions << item->data(Qt::UserRole).value(); } @@ -100,7 +105,8 @@ QVector PassConfigForm::passActions() void PassConfigForm::clearPassActions() { - for(int i = 0; i < this->listSavedActions->count(); ++i) { + const int listSavedActionsCount = this->listSavedActions->count(); + for(int i = 0; i < listSavedActionsCount; ++i) { QListWidgetItem* item = this->listSavedActions->item(i); delete this->listSavedActions->itemWidget(item); } @@ -115,6 +121,12 @@ void PassConfigForm::clearInputs() this->lineRegEx->clear(); } +void PassConfigForm::validateAddButton() { + this->buttonAddAction->setDisabled(this->lineIcon->text().isEmpty() || + this->lineName->text().isEmpty() || + this->lineRegEx->text().isEmpty()); +} + PassConfig::PassConfig(QWidget *parent, const QVariantList &args) : KCModule(parent, args) @@ -123,8 +135,6 @@ PassConfig::PassConfig(QWidget *parent, const QVariantList &args) : QGridLayout* layout = new QGridLayout(this); layout->addWidget(ui, 0, 0); - load(); - connect(this->ui,SIGNAL(passActionAdded()),this,SLOT(changed())); connect(this->ui,SIGNAL(passActionRemoved()),this,SLOT(changed())); connect(this->ui->checkAdditionalActions,SIGNAL(stateChanged(int)),this,SLOT(changed())); @@ -150,9 +160,8 @@ void PassConfig::load() // Load saved actions this->ui->clearPassActions(); - auto actionGroup = passCfg.group(Config::Group::Actions); - auto groups = actionGroup.groupList(); - Q_FOREACH (auto name, groups) { + const auto actionGroup = passCfg.group(Config::Group::Actions); + for (const auto& name: actionGroup.groupList()) { auto group = actionGroup.group(name); auto passAction = PassAction::fromConfig(group); @@ -177,12 +186,11 @@ void PassConfig::save() passCfg.writeEntry(Config::showActions, showActions); passCfg.writeEntry(Config::showFileContentAction, showFileContentAction); - passCfg.deleteGroup(Config::Group::Actions); if (showActions) { int i = 0; - for (PassAction act: this->ui->passActions()) { + for (PassAction& act: this->ui->passActions()) { auto group = passCfg.group(Config::Group::Actions).group(QString::number(i++)); act.writeToConfig(group); } diff --git a/config.h b/config.h index f11e293..474c7cd 100644 --- a/config.h +++ b/config.h @@ -76,6 +76,9 @@ public: signals: void passActionRemoved(); void passActionAdded(); + +private slots: + void validateAddButton(); }; @@ -84,12 +87,12 @@ class PassConfig : public KCModule Q_OBJECT public: - explicit PassConfig(QWidget* parent = 0, const QVariantList& args = QVariantList()); + explicit PassConfig(QWidget* parent = nullptr, const QVariantList& args = QVariantList()); public Q_SLOTS: - void save(); - void load(); - void defaults(); + void save() override; + void load() override; + void defaults() override; private: PassConfigForm *ui; diff --git a/install.sh b/install.sh index 7d6331c..32ede4d 100755 --- a/install.sh +++ b/install.sh @@ -5,8 +5,8 @@ set -e mkdir -p build cd build -cmake .. -DCMAKE_EXPORT_COMPILE_COMMANDS=1 -DCMAKE_INSTALL_PREFIX=`kf5-config --prefix` -DQT_PLUGIN_INSTALL_DIR=`kf5-config --qt-plugins` -make +cmake .. -DCMAKE_EXPORT_COMPILE_COMMANDS=1 -DCMAKE_INSTALL_PREFIX=`kf5-config --prefix` -DQT_PLUGIN_INSTALL_DIR=`kf5-config --qt-plugins` -DCMAKE_BUILD_TYPE=Release +make -j$(nproc) sudo make install set +e diff --git a/pass.cpp b/pass.cpp index c02f9d0..4ab38a6 100644 --- a/pass.cpp +++ b/pass.cpp @@ -27,8 +27,9 @@ #include #include #include +#include -#include +#include #include "pass.h" #include "config.h" @@ -49,7 +50,7 @@ Pass::Pass(QObject *parent, const QVariantList &args) setDefaultSyntax(Plasma::RunnerSyntax(QString(":q:"), comment)); } -Pass::~Pass() {} +Pass::~Pass() = default; void Pass::reloadConfiguration() { @@ -60,11 +61,10 @@ void Pass::reloadConfiguration() this->showActions = cfg.readEntry(Config::showActions, false); if (showActions) { - auto configActions = cfg.group(Config::Group::Actions); + const auto configActions = cfg.group(Config::Group::Actions); // Create actions for every additional field - auto groups = configActions.groupList(); - Q_FOREACH (auto name, groups) { + for (const auto& name: configActions.groupList() ) { auto group = configActions.group(name); auto passAction = PassAction::fromConfig(group); @@ -89,26 +89,26 @@ void Pass::init() reloadConfiguration(); this->baseDir = QDir(QDir::homePath() + "/.password-store"); - auto baseDir = getenv("PASSWORD_STORE_DIR"); - if (baseDir != nullptr) { - this->baseDir = QDir(baseDir); + auto _baseDir = getenv("PASSWORD_STORE_DIR"); + if (_baseDir != nullptr) { + this->baseDir = QDir(_baseDir); } this->timeout = 45; - auto timeout = getenv("PASSWORD_STORE_CLIP_TIME"); - if (timeout != nullptr) { - QString str(timeout); + auto _timeout = getenv("PASSWORD_STORE_CLIP_TIME"); + if (_timeout != nullptr) { + QString str(_timeout); bool ok; - auto timeout = str.toInt(&ok); + auto _timeoutParsed = str.toInt(&ok); if (ok) { - this->timeout = timeout; + this->timeout = _timeoutParsed; } } this->passOtpIdentifier = "totp::"; - auto passOtpIdentifier = getenv("PASSWORD_STORE_OTP_IDENTIFIER"); - if (passOtpIdentifier != nullptr) { - this->passOtpIdentifier = passOtpIdentifier; + auto _passOtpIdentifier = getenv("PASSWORD_STORE_OTP_IDENTIFIER"); + if (_passOtpIdentifier != nullptr) { + this->passOtpIdentifier = _passOtpIdentifier; } initPasswords(); @@ -123,7 +123,7 @@ void Pass::initPasswords() { QDirIterator it(this->baseDir, QDirIterator::Subdirectories); while (it.hasNext()) { it.next(); - auto fileInfo = it.fileInfo(); + const auto fileInfo = it.fileInfo(); if (fileInfo.isFile() && fileInfo.suffix() == "gpg") { QString password = this->baseDir.relativeFilePath(fileInfo.absoluteFilePath()); // Remove suffix ".gpg" @@ -147,20 +147,17 @@ void Pass::match(Plasma::RunnerContext &context) { if (!context.isValid()) return; - auto input = context.query(); + const auto input = context.query(); QList matches; lock.lockForRead(); - Q_FOREACH (auto password, passwords) { - QRegularExpression re(".*" + input + ".*", QRegularExpression::CaseInsensitiveOption); + QRegularExpression re(".*" + input + ".*", QRegularExpression::CaseInsensitiveOption); + for (const auto& password: passwords) { if (re.match(password).hasMatch()) { Plasma::QueryMatch match(this); - if (input.length() == password.length()) { - match.setType(Plasma::QueryMatch::ExactMatch); - } else { - match.setType(Plasma::QueryMatch::CompletionMatch); - } + match.setType(input.length() == password.length() ? + Plasma::QueryMatch::ExactMatch : Plasma::QueryMatch::CompletionMatch); match.setIcon(QIcon::fromTheme("object-locked")); match.setText(password); matches.append(match); @@ -183,10 +180,10 @@ void Pass::clip(const QString &msg) void Pass::run(const Plasma::RunnerContext &context, const Plasma::QueryMatch &match) { Q_UNUSED(context); - auto regexp = QRegularExpression("^" + QRegularExpression::escape(this->passOtpIdentifier) + ".*"); - auto isOtp = match.text().split('/').filter(regexp).size() > 0; + const auto regexp = QRegularExpression("^" + QRegularExpression::escape(this->passOtpIdentifier) + ".*"); + const auto isOtp = !match.text().split('/').filter(regexp).isEmpty(); - QProcess *pass = new QProcess(); + auto *pass = new QProcess(); QStringList args; if (isOtp) { args << "otp" << "show" << match.text(); @@ -211,7 +208,7 @@ void Pass::run(const Plasma::RunnerContext &context, const Plasma::QueryMatch &m QMessageBox::information(nullptr, match.text(), output); } else { QRegularExpression re(data, QRegularExpression::MultilineOption); - auto matchre = re.match(output); + const auto matchre = re.match(output); if (matchre.hasMatch()) { clip(matchre.captured(1)); @@ -225,9 +222,9 @@ void Pass::run(const Plasma::RunnerContext &context, const Plasma::QueryMatch &m } } } else { - auto string = QString::fromUtf8(output.data()); - auto lines = string.split('\n', QString::SkipEmptyParts); - if (lines.count() > 0) { + const auto string = QString::fromUtf8(output.data()); + const auto lines = string.split('\n', QString::SkipEmptyParts); + if (!lines.isEmpty()) { clip(lines[0]); this->showNotification(match.text()); } @@ -251,12 +248,11 @@ QList Pass::actionsForMatch(const Plasma::QueryMatch &match) void Pass::showNotification(const QString &text, const QString &actionName /* = "" */) { - QString msgPrefix = actionName.isEmpty() ? "":actionName + i18n(" of "); - QString msg = i18n("Password %1 copied to clipboard for %2 seconds", text, timeout); - auto notification = KNotification::event("password-unlocked", "Pass", msgPrefix + msg, + const QString msgPrefix = actionName.isEmpty() ? "":actionName + i18n(" of "); + const QString msg = i18n("Password %1 copied to clipboard for %2 seconds", text, timeout); + KNotification::event("password-unlocked", "Pass", msgPrefix + msg, "object-unlocked", nullptr, KNotification::CloseOnTimeout, "krunner_pass"); - QTimer::singleShot(timeout * 1000, notification, SLOT(quit)); } K_EXPORT_PLASMA_RUNNER(pass, Pass) diff --git a/pass.h b/pass.h index 7b1da33..134be0f 100644 --- a/pass.h +++ b/pass.h @@ -30,7 +30,7 @@ class Pass : public Plasma::AbstractRunner public: Pass(QObject *parent, const QVariantList &args); - ~Pass(); + ~Pass() override; void clip(const QString &msg); void match(Plasma::RunnerContext &) override; -- cgit v1.2.3