Skip to content

Commit

Permalink
Clazy cleanup and other minor fixes
Browse files Browse the repository at this point in the history
 - Code is now clazy clean with
     CLAZY_CHECKS="no-connect-by-name,no-overloaded-signal"
 - Given the minimum Qt version is 5.10, remove ifdef's for older
   versions
 - Some more range based for and related cleanups.
 - Some include cleanups
  • Loading branch information
svuorela committed Aug 21, 2023
1 parent 1e5a217 commit bd1f363
Show file tree
Hide file tree
Showing 19 changed files with 84 additions and 157 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 @@ -303,7 +304,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 @@ -318,7 +319,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
53 changes: 11 additions & 42 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 @@ -112,7 +113,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 @@ -147,12 +148,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 @@ -201,39 +198,12 @@ 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.");
}
reencryptPath(path);
}

/**
* @brief ImitatePass::removeDir delete folder recursive.
* @param dirName which folder.
* @return was removal succesful?
*/
bool ImitatePass::removeDir(const QString &dirName) {
bool result = true;
QDir dir(dirName);

if (dir.exists(dirName)) {
Q_FOREACH (QFileInfo info,
dir.entryInfoList(QDir::NoDotAndDotDot | QDir::System |
QDir::Hidden | QDir::AllDirs | QDir::Files,
QDir::DirsFirst)) {
if (info.isDir())
result = removeDir(info.absoluteFilePath());
else
result = QFile::remove(info.absoluteFilePath());

if (!result)
return result;
}
result = dir.rmdir(dirName);
}
return result;
}

/**
* @brief ImitatePass::reencryptPath reencrypt all files under the chosen
* directory
Expand Down Expand Up @@ -267,12 +237,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 @@ -325,7 +294,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 @@ -405,12 +374,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 @@ -436,7 +405,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
2 changes: 0 additions & 2 deletions src/imitatepass.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
class ImitatePass : public Pass, private simpleTransaction {
Q_OBJECT

bool removeDir(const QString &dirName);

void GitCommit(const QString &file, const QString &msg);

void executeGit(PROCESS id, const QStringList &args,
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
19 changes: 5 additions & 14 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 @@ -800,7 +794,7 @@ void MainWindow::on_profileBox_currentIndexChanged(QString name) {

QtPassSettings::setProfile(name);

QtPassSettings::setPassStore(QtPassSettings::getProfiles()[name]);
QtPassSettings::setPassStore(QtPassSettings::getProfiles().value(name));
ui->statusBar->showMessage(tr("Profile changed to %1").arg(name), 2000);

QtPassSettings::getPass()->updateEnv();
Expand Down Expand Up @@ -1157,10 +1151,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
Loading

3 comments on commit bd1f363

@barracuda156
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@svuorela Could we unbreak building with Qt4?

@svuorela
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@barracuda156

Given the code is using PMF connect syntax already and QRegularExpression, I don't see how this commit make it regress in qt4 building ?

Also, the readme says it requires Qt5.10 or later.

@barracuda156
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@svuorela You are right, it has been broken much earlier. I have opened an issue for that: #665

Please sign in to comment.