Skip to content

Commit

Permalink
Merge branch 'svuorela-svuorela/clazy-and-other-minor-fixes'
Browse files Browse the repository at this point in the history
  • Loading branch information
annejan committed Aug 31, 2023
2 parents 1da3f14 + 98d3922 commit 42cdda0
Show file tree
Hide file tree
Showing 18 changed files with 84 additions and 129 deletions.
7 changes: 2 additions & 5 deletions main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@
* @return
*/
int main(int argc, char *argv[]) {
#if QT_VERSION >= QT_VERSION_CHECK(5, 6, 0) && \
QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
#endif
Expand Down Expand Up @@ -102,12 +101,10 @@ int main(int argc, char *argv[]) {
&MainWindow::messageAvailable);
#endif

#if (QT_VERSION >= QT_VERSION_CHECK(5, 7, 0))
QGuiApplication::setDesktopFileName("qtpass.desktop");
#endif

// Center the MainWindow on the screen the mouse pointer is currently on
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
#if QT_VERSION < QT_VERSION_CHECK(5, 12, 0)
static int cursorScreen =
app.desktop()->screenNumber(app.desktop()->cursor().pos());
QPoint cursorScreenCenter =
Expand Down
5 changes: 3 additions & 2 deletions src/configdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "mainwindow.h"
#include "qtpasssettings.h"
#include "ui_configdialog.h"
#include "util.h"
#include <QClipboard>
#include <QDir>
#include <QFileDialog>
Expand Down Expand Up @@ -304,7 +305,7 @@ QString ConfigDialog::selectExecutable() {
dialog.setFileMode(QFileDialog::ExistingFile);
dialog.setOption(QFileDialog::ReadOnly);
if (dialog.exec())
return dialog.selectedFiles().first();
return dialog.selectedFiles().constFirst();

return QString();
}
Expand All @@ -319,7 +320,7 @@ QString ConfigDialog::selectFolder() {
dialog.setFilter(QDir::NoFilter);
dialog.setOption(QFileDialog::ShowDirsOnly);
if (dialog.exec())
return dialog.selectedFiles().first();
return dialog.selectedFiles().constFirst();

return QString();
}
Expand Down
2 changes: 1 addition & 1 deletion src/filecontent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ FileContent FileContent::parse(const QString &fileContent,
QString password = lines.takeFirst();
QStringList remainingData, remainingDataDisplay;
NamedValues namedValues;
for (const QString &line : lines) {
for (const QString &line : qAsConst(lines)) {
if (line.contains(":")) {
int colon = line.indexOf(':');
QString name = line.left(colon);
Expand Down
26 changes: 11 additions & 15 deletions src/imitatepass.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "imitatepass.h"
#include "qtpasssettings.h"
#include "util.h"
#include <QDirIterator>
#include <QRegularExpression>
#include <utility>
Expand Down Expand Up @@ -118,7 +119,7 @@ void ImitatePass::Insert(QString file, QString newValue, bool overwrite) {
if (!overwrite)
executeGit(GIT_ADD, {"add", pgit(file)});
QString path = QDir(QtPassSettings::getPassStore()).relativeFilePath(file);
path.replace(QRegularExpression("\\.gpg$"), "");
path.replace(Util::endsWithGpg(), "");
QString msg =
QString(overwrite ? "Edit" : "Add") + " for " + path + " using QtPass.";
GitCommit(file, msg);
Expand Down Expand Up @@ -153,12 +154,8 @@ void ImitatePass::Remove(QString file, bool isDir) {
GitCommit(file, "Remove for " + file + " using QtPass.");
} else {
if (isDir) {
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
QDir dir(file);
dir.removeRecursively();
#else
removeDir(QtPassSettings::getPassStore() + file);
#endif
} else
QFile(file).remove();
}
Expand Down Expand Up @@ -253,7 +250,7 @@ void ImitatePass::Init(QString path, const QList<UserInfo> &users) {
if (addFile)
executeGit(GIT_ADD, {"add", pgit(gpgIdFile)});
QString commitPath = gpgIdFile;
commitPath.replace(QRegularExpression("\\.gpg$"), "");
commitPath.replace(Util::endsWithGpg(), "");
GitCommit(gpgIdFile, "Added " + commitPath + " using QtPass.");
if (!signingKeys.isEmpty()) {
if (addSigFile)
Expand Down Expand Up @@ -371,12 +368,11 @@ void ImitatePass::reencryptPath(const QString &dir) {
exec.executeBlocking(QtPassSettings::getGpgExecutable(), args, &keys, &err);
QStringList actualKeys;
keys += err;
static const QRegularExpression newLines{"[\r\n]"};
#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
QStringList key =
keys.split(QRegularExpression("[\r\n]"), Qt::SkipEmptyParts);
QStringList key = keys.split(newLines, Qt::SkipEmptyParts);
#else
QStringList key =
keys.split(QRegularExpression("[\r\n]"), QString::SkipEmptyParts);
QStringList key = keys.split(newLines, QString::SkipEmptyParts);
#endif
QListIterator<QString> itr(key);
while (itr.hasNext()) {
Expand Down Expand Up @@ -429,7 +425,7 @@ void ImitatePass::reencryptPath(const QString &dir) {
{"add", pgit(fileName)});
QString path =
QDir(QtPassSettings::getPassStore()).relativeFilePath(fileName);
path.replace(QRegularExpression("\\.gpg$"), "");
path.replace(Util::endsWithGpg(), "");
exec.executeBlocking(QtPassSettings::getGitExecutable(),
{"commit", pgit(fileName), "-m",
"Edit for " + path + " using QtPass."});
Expand Down Expand Up @@ -509,12 +505,12 @@ void ImitatePass::Move(const QString src, const QString dest,
executeGit(GIT_MOVE, args);

QString relSrc = QDir(QtPassSettings::getPassStore()).relativeFilePath(src);
relSrc.replace(QRegularExpression("\\.gpg$"), "");
relSrc.replace(Util::endsWithGpg(), "");
QString relDest =
QDir(QtPassSettings::getPassStore()).relativeFilePath(destFile);
relDest.replace(QRegularExpression("\\.gpg$"), "");
relDest.replace(Util::endsWithGpg(), "");
QString message = QString("Moved for %1 to %2 using QtPass.");
message = message.arg(relSrc).arg(relDest);
message = message.arg(relSrc, relDest);
GitCommit("", message);
} else {
QDir qDir;
Expand All @@ -540,7 +536,7 @@ void ImitatePass::Copy(const QString src, const QString dest,
executeGit(GIT_COPY, args);

QString message = QString("copied from %1 to %2 using QTPass.");
message = message.arg(src).arg(dest);
message = message.arg(src, dest);
GitCommit("", message);
} else {
QDir qDir;
Expand Down
21 changes: 10 additions & 11 deletions src/keygendialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,14 @@ void KeygenDialog::on_name_textChanged(const QString &arg1) {
void KeygenDialog::replace(const QString &key, const QString &value) {
QStringList clear;
QString expert = ui->plainTextEdit->toPlainText();
static const QRegularExpression newLines{"[\r\n]"};
#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
QStringList lines =
expert.split(QRegularExpression("[\r\n]"), Qt::SkipEmptyParts);
const QStringList lines = expert.split(newLines, Qt::SkipEmptyParts);
#else
QStringList lines =
expert.split(QRegularExpression("[\r\n]"), QString::SkipEmptyParts);
const QStringList lines =
expert.split(newLines, QString::SkipEmptyParts);
#endif
foreach (QString line, lines) {
for (QString line : lines) {
line.replace(QRegularExpression(key + ":.*"), key + ": " + value);
if (key == "Passphrase")
line.replace("%no-protection", "Passphrase: " + value);
Expand All @@ -108,14 +108,13 @@ void KeygenDialog::replace(const QString &key, const QString &value) {
void KeygenDialog::no_protection(bool enable) {
QStringList clear;
QString expert = ui->plainTextEdit->toPlainText();
static const QRegularExpression newLines{"[\r\n]"};
#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
QStringList lines =
expert.split(QRegularExpression("[\r\n]"), Qt::SkipEmptyParts);
const QStringList lines = expert.split(newLines, Qt::SkipEmptyParts);
#else
QStringList lines =
expert.split(QRegularExpression("[\r\n]"), QString::SkipEmptyParts);
const QStringList lines = expert.split(newLines, QString::SkipEmptyParts);
#endif
foreach (QString line, lines) {
for (QString line : lines) {
bool remove = false;
if (!enable) {
if (line.indexOf("%no-protection") == 0)
Expand Down Expand Up @@ -145,7 +144,7 @@ void KeygenDialog::done(int r) {
}

// check email
QRegularExpression mailre(
static const QRegularExpression mailre(
QRegularExpression::anchoredPattern(
R"(\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b)"),
QRegularExpression::CaseInsensitiveOption);
Expand Down
20 changes: 5 additions & 15 deletions src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@

#include "configdialog.h"
#include "filecontent.h"
#include "keygendialog.h"
#include "passworddialog.h"
#include "qpushbuttonasqrcode.h"
#include "qpushbuttonshowpassword.h"
#include "qpushbuttonwithclipboard.h"
#include "qtpass.h"
#include "qtpasssettings.h"
#include "settingsconstants.h"
#include "trayicon.h"
#include "ui_mainwindow.h"
#include "usersdialog.h"
Expand Down Expand Up @@ -114,13 +112,10 @@ MainWindow::MainWindow(const QString &searchText, QWidget *parent)
initToolBarButtons();
initStatusBar();

#if QT_VERSION >= QT_VERSION_CHECK(5, 2, 0)
ui->lineEdit->setClearButtonEnabled(true);
#endif

setUiElementsEnabled(true);

QRandomGenerator(static_cast<uint>(QTime::currentTime().msec()));
QTimer::singleShot(10, this, SLOT(focusInput()));

ui->lineEdit->setText(searchText);
Expand Down Expand Up @@ -325,7 +320,7 @@ QString MainWindow::getFile(const QModelIndex &index, bool forPass) {
QString filePath = model.filePath(proxyModel.mapToSource(index));
if (forPass) {
filePath = QDir(QtPassSettings::getPassStore()).relativeFilePath(filePath);
filePath.replace(QRegularExpression("\\.gpg$"), "");
filePath.replace(Util::endsWithGpg(), "");
}
return filePath;
}
Expand Down Expand Up @@ -542,7 +537,7 @@ void MainWindow::onTimeoutSearch() {
deselect();
}

query.replace(QRegularExpression(" "), ".*");
query.replace(QStringLiteral(" "), ".*");
QRegularExpression regExp(query, QRegularExpression::CaseInsensitiveOption);
proxyModel.setFilterRegularExpression(regExp);
ui->treeView->setRootIndex(proxyModel.mapFromSource(
Expand Down Expand Up @@ -681,8 +676,7 @@ void MainWindow::onDelete() {
if (QMessageBox::question(
this, isDir ? tr("Delete folder?") : tr("Delete password?"),
tr("Are you sure you want to delete %1%2?")
.arg(QDir::separator() + file)
.arg(isDir ? dirMessage : "?"),
.arg(QDir::separator() + file, isDir ? dirMessage : "?"),
QMessageBox::Yes | QMessageBox::No) != QMessageBox::Yes)
return;

Expand Down Expand Up @@ -801,8 +795,7 @@ void MainWindow::on_profileBox_currentIndexChanged(QString name) {

QtPassSettings::setProfile(name);

QtPassSettings::setPassStore(
QtPassSettings::getProfiles().value(name).value("path"));
QtPassSettings::setPassStore(QtPassSettings::getProfiles().value(name).value("path"));
QtPassSettings::setPassSigningKey(
QtPassSettings::getProfiles().value(name).value("signingKey"));
ui->statusBar->showMessage(tr("Profile changed to %1").arg(name), 2000);
Expand Down Expand Up @@ -1161,10 +1154,7 @@ void MainWindow::addToGridLayout(int position, const QString &field,
line->setSizePolicy(
QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum));
line->setObjectName(trimmedField);
trimmedValue.replace(
QRegularExpression(
"((?:https?|ftp|ssh|sftp|ftps|webdav|webdavs)://\\S+)"),
R"(<a href="\1">\1</a>)");
trimmedValue.replace(Util::protocolRegex(), R"(<a href="\1">\1</a>)");
line->setText(trimmedValue);
line->setReadOnly(true);
line->setStyleSheet(lineStyle);
Expand Down
2 changes: 0 additions & 2 deletions src/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@ class SingleApplication;
#endif

#ifdef __APPLE__
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
// http://doc.qt.io/qt-5/qkeysequence.html#qt_set_sequence_auto_mnemonic
void qt_set_sequence_auto_mnemonic(bool b);
#endif
#endif

namespace Ui {
class MainWindow;
Expand Down
34 changes: 12 additions & 22 deletions src/pass.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "pass.h"
#include "qtpasssettings.h"
#include "util.h"
#include <QDir>
#include <QRegularExpression>
#include <QRandomGenerator>

#ifdef QT_DEBUG
#include "debughelper.h"
Expand Down Expand Up @@ -81,12 +83,12 @@ QString Pass::Generate_b(unsigned int length, const QString &charset) {
if (QtPassSettings::isUseSymbols())
args.append("--symbols");
args.append(QString::number(length));
QString p_out;
// TODO(bezet): try-catch here(2 statuses to merge o_O)
if (exec.executeBlocking(QtPassSettings::getPwgenExecutable(), args,
&passwd) == 0)
passwd.remove(QRegularExpression("[\\n\\r]"));
else {
&passwd) == 0) {
static const QRegularExpression literalNewLines{"[\\n\\r]"};
passwd.remove(literalNewLines);
} else {
passwd.clear();
#ifdef QT_DEBUG
qDebug() << __FILE__ << ":" << __LINE__ << "\t"
Expand Down Expand Up @@ -130,7 +132,7 @@ QList<UserInfo> Pass::listKeys(QStringList keystrings, bool secret) {
QStringList args = {"--no-tty", "--with-colons", "--with-fingerprint"};
args.append(secret ? "--list-secret-keys" : "--list-keys");

foreach (QString keystring, keystrings) {
for (const QString &keystring : qAsConst(keystrings)) {
if (!keystring.isEmpty()) {
args.append(keystring);
}
Expand All @@ -139,15 +141,14 @@ QList<UserInfo> Pass::listKeys(QStringList keystrings, bool secret) {
if (exec.executeBlocking(QtPassSettings::getGpgExecutable(), args, &p_out) !=
0)
return users;
static const QRegularExpression newLines{"[\r\n]"};
#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
QStringList keys =
p_out.split(QRegularExpression("[\r\n]"), Qt::SkipEmptyParts);
const QStringList keys = p_out.split(newLines, Qt::SkipEmptyParts);
#else
QStringList keys =
p_out.split(QRegularExpression("[\r\n]"), QString::SkipEmptyParts);
const QStringList keys = p_out.split(newLines, QString::SkipEmptyParts);
#endif
UserInfo current_user;
foreach (QString key, keys) {
for (const QString &key : keys) {
QStringList props = key.split(':');
if (props.size() < 10)
continue;
Expand Down Expand Up @@ -353,19 +354,8 @@ quint32 Pass::boundedRandom(quint32 bound) {
quint32 randval;
const quint32 max_mod_bound = (1 + ~bound) % bound;

#if QT_VERSION < QT_VERSION_CHECK(5, 10, 0)
static int fd = -1;
if (fd == -1) {
assert((fd = open("/dev/urandom", O_RDONLY)) >= 0);
}
#endif

do {
#if QT_VERSION < QT_VERSION_CHECK(5, 10, 0)
assert(read(fd, &randval, sizeof(randval)) == sizeof(randval));
#else
randval = QRandomGenerator::system()->generate();
#endif
} while (randval < max_mod_bound);

return randval % bound;
Expand Down
7 changes: 0 additions & 7 deletions src/pass.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,6 @@
#include <cassert>
#include <map>

#if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
#include <QRandomGenerator>
#else
#include <fcntl.h>
#include <unistd.h>
#endif

/*!
\class Pass
\brief Acts as an abstraction for pass or pass imitation
Expand Down
4 changes: 2 additions & 2 deletions src/passworddialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,13 @@ void PasswordDialog::setPassword(QString password) {
QWidget *previous = ui->checkBoxShow;
// first set templated values
NamedValues namedValues = fileContent.getNamedValues();
for (QLineEdit *line : templateLines) {
for (QLineEdit *line : qAsConst(templateLines)) {
line->setText(namedValues.takeValue(line->objectName()));
previous = line;
}
// show remaining values (if there are)
otherLines.clear();
for (const NamedValue &nv : namedValues) {
for (const NamedValue &nv : qAsConst(namedValues)) {
auto *line = new QLineEdit();
line->setObjectName(nv.name);
line->setText(nv.value);
Expand Down
Loading

0 comments on commit 42cdda0

Please sign in to comment.