Fix warnings and deprecations, optimize

master
alex1701c 6 years ago
parent e8032af8f0
commit cd2987635b
  1. 2
      .gitignore
  2. 2
      CMakeLists.txt
  3. 36
      config.cpp
  4. 11
      config.h
  5. 4
      install.sh
  6. 68
      pass.cpp
  7. 2
      pass.h

2
.gitignore vendored

@ -1 +1,3 @@
/build
/cmake-build-debug
/.idea

@ -1,3 +1,5 @@
cmake_minimum_required(VERSION 2.8.12)
project(RunnerPass)
find_package(ECM 5.12.0 REQUIRED NO_MODULE)

@ -20,6 +20,7 @@
#include <KPluginFactory>
#include <krunner/abstractrunner.h>
#include <QToolButton>
#include <QtCore/QDir>
#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 &regex, 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<PassAction> PassConfigForm::passActions()
{
QVector<PassAction> 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<PassAction>();
}
@ -100,7 +105,8 @@ QVector<PassAction> 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);
}

@ -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;

@ -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

@ -27,8 +27,9 @@
#include <QMessageBox>
#include <QClipboard>
#include <QDebug>
#include <QApplication>
#include <stdlib.h>
#include <cstdlib>
#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<Plasma::QueryMatch> 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<QAction *> 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)

@ -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;

Loading…
Cancel
Save