Skip to content

Commit ba1270c

Browse files
committed
Clean up the code and make some more minor optimisations
This also includes replacing some more Qt containers by their STL counterparts.
1 parent b3b1ac6 commit ba1270c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+676
-836
lines changed

src/AddRecordDialog.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,6 @@ void AddRecordDialog::populateFields()
228228
else
229229
tbitem->setIcon(kName, QIcon(":/icons/field"));
230230

231-
QString defaultValue = QString::fromStdString(f.defaultValue());
232231
QString toolTip;
233232

234233
if (auto_increment && contains(pk, f.name()))
@@ -248,9 +247,10 @@ void AddRecordDialog::populateFields()
248247

249248
// Display Role is used for displaying the default values.
250249
// Only when they are changed, the User Role is updated and then used in the INSERT query.
251-
if (!defaultValue.isEmpty()) {
252-
tbitem->setData(kValue, Qt::DisplayRole, QString::fromStdString(f.defaultValue()));
253-
toolTip.append(tr("Default value:\t %1\n").arg (defaultValue));
250+
if (!f.defaultValue().empty()) {
251+
QString defaultValue = QString::fromStdString(f.defaultValue());
252+
tbitem->setData(kValue, Qt::DisplayRole, defaultValue);
253+
toolTip.append(tr("Default value:\t %1\n").arg(defaultValue));
254254
} else
255255
tbitem->setData(kValue, Qt::DisplayRole, Settings::getValue("databrowser", "null_text"));
256256

@@ -271,7 +271,7 @@ void AddRecordDialog::populateFields()
271271

272272
void AddRecordDialog::accept()
273273
{
274-
if(!pdb.executeSQL(ui->sqlTextEdit->text()))
274+
if(!pdb.executeSQL(ui->sqlTextEdit->text().toStdString()))
275275
{
276276
QMessageBox::warning(
277277
this,
@@ -306,7 +306,7 @@ void AddRecordDialog::updateSqlText()
306306
if (isNumeric && item->data(kType, Qt::UserRole).toInt() != sqlb::Field::TextAffinity)
307307
vals << value.toString();
308308
else
309-
vals << QString("'%1'").arg(value.toString().replace("'", "''"));
309+
vals << sqlb::escapeString(value.toString());
310310
}
311311
}
312312

src/Application.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ QString Application::versionString()
204204
// date in order to avoid confusion about what is more important, version number or build date, and about different
205205
// build dates for the same version. This also should help making release builds reproducible out of the box.
206206
#if PATCH_VERSION >= 99
207-
return QString("%1 (%2)").arg(APP_VERSION).arg(__DATE__);
207+
return QString("%1 (%2)").arg(APP_VERSION, __DATE__);
208208
#else
209209
return QString("%1").arg(APP_VERSION);
210210
#endif
@@ -213,12 +213,12 @@ QString Application::versionString()
213213
// Functions for documenting the shortcuts in the user interface using native names
214214
static QString shortcutsTip(const QList<QKeySequence>& keys)
215215
{
216-
QString tip("");
216+
QString tip;
217217

218218
if (!keys.isEmpty()) {
219219
tip = " [";
220220

221-
for (auto shortcut : keys)
221+
for (const auto& shortcut : keys)
222222
tip.append(shortcut.toString(QKeySequence::NativeText) + ", ");
223223
tip.chop(2);
224224

src/Application.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class Application : public QApplication
1717
explicit Application(int& argc, char** argv);
1818
~Application() override;
1919

20-
bool dontShowMainWindow() { return m_dontShowMainWindow; }
20+
bool dontShowMainWindow() const { return m_dontShowMainWindow; }
2121

2222
MainWindow* mainWindow() { return m_mainWindow; }
2323

src/CipherDialog.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ CipherDialog::~CipherDialog()
6161
CipherSettings CipherDialog::getCipherSettings() const
6262
{
6363
CipherSettings::KeyFormats keyFormat = CipherSettings::getKeyFormat(ui->comboKeyFormat->currentIndex());
64-
QString password = ui->editPassword->text();
64+
std::string password = ui->editPassword->text().toStdString();
6565
int pageSize = ui->comboPageSize->itemData(ui->comboPageSize->currentIndex()).toInt();
6666

6767
CipherSettings cipherSettings;
@@ -70,8 +70,8 @@ CipherSettings CipherDialog::getCipherSettings() const
7070
cipherSettings.setPassword(password);
7171
cipherSettings.setPageSize(pageSize);
7272
cipherSettings.setKdfIterations(ui->spinKdfIterations->value());
73-
cipherSettings.setHmacAlgorithm(QString("HMAC_") + ui->comboHmacAlgorithm->currentText());
74-
cipherSettings.setKdfAlgorithm(QString("PBKDF2_HMAC_") + ui->comboKdfAlgorithm->currentText());
73+
cipherSettings.setHmacAlgorithm("HMAC_" + ui->comboHmacAlgorithm->currentText().toStdString());
74+
cipherSettings.setKdfAlgorithm("PBKDF2_HMAC_" + ui->comboKdfAlgorithm->currentText().toStdString());
7575

7676
return cipherSettings;
7777
}

src/CipherSettings.cpp

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "CipherSettings.h"
2+
#include "sqlitedb.h"
23

34
CipherSettings::CipherSettings()
45
: keyFormat(Passphrase),
@@ -7,46 +8,17 @@ CipherSettings::CipherSettings()
78
{
89
}
910

10-
CipherSettings::KeyFormats CipherSettings::getKeyFormat() const
11-
{
12-
return keyFormat;
13-
}
14-
15-
void CipherSettings::setKeyFormat(const KeyFormats &value)
16-
{
17-
keyFormat = value;
18-
}
19-
20-
QString CipherSettings::getPassword() const
11+
std::string CipherSettings::getPassword() const
2112
{
2213
if(keyFormat == Passphrase)
2314
{
24-
QString tempPassword = password;
25-
26-
tempPassword.replace("'", "''");
27-
28-
return QString("'%1'").arg(tempPassword);
15+
return sqlb::escapeString(password);
2916
} else {
3017
// Remove the '0x' part at the beginning
31-
return QString("\"x'%1'\"").arg(password.mid(2));
18+
return "\"x'" + password.substr(2) + "'\"";
3219
}
3320
}
3421

35-
void CipherSettings::setPassword(const QString &value)
36-
{
37-
password = value;
38-
}
39-
40-
int CipherSettings::getPageSize() const
41-
{
42-
return pageSize;
43-
}
44-
45-
void CipherSettings::setPageSize(int value)
46-
{
47-
pageSize = value;
48-
}
49-
5022
CipherSettings::KeyFormats CipherSettings::getKeyFormat(int rawKeyFormat)
5123
{
5224
return static_cast<CipherSettings::KeyFormats>(rawKeyFormat);

src/CipherSettings.h

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#ifndef CIPHERSETTINGS_H
22
#define CIPHERSETTINGS_H
33

4-
#include <QString>
4+
#include <string>
55

66
class CipherSettings
77
{
@@ -14,33 +14,33 @@ class CipherSettings
1414
RawKey
1515
};
1616

17-
KeyFormats getKeyFormat() const;
18-
void setKeyFormat(const KeyFormats &value);
17+
KeyFormats getKeyFormat() const { return keyFormat; }
18+
void setKeyFormat(const KeyFormats &value) { keyFormat = value; }
1919

20-
QString getPassword() const;
21-
void setPassword(const QString &value);
20+
std::string getPassword() const;
21+
void setPassword(const std::string& value) { password = value; }
2222

23-
int getPageSize() const;
24-
void setPageSize(int value);
23+
int getPageSize() const { return pageSize; }
24+
void setPageSize(int value) { pageSize = value; }
2525

2626
int getKdfIterations() const { return kdfIterations; }
2727
void setKdfIterations(int value) { kdfIterations = value; }
2828

29-
QString getHmacAlgorithm() const { return hmacAlgorithm; }
30-
void setHmacAlgorithm(const QString &value) { hmacAlgorithm = value; }
29+
std::string getHmacAlgorithm() const { return hmacAlgorithm; }
30+
void setHmacAlgorithm(const std::string& value) { hmacAlgorithm = value; }
3131

32-
QString getKdfAlgorithm() const { return kdfAlgorithm; }
33-
void setKdfAlgorithm(const QString &value) { kdfAlgorithm = value; }
32+
std::string getKdfAlgorithm() const { return kdfAlgorithm; }
33+
void setKdfAlgorithm(const std::string& value) { kdfAlgorithm = value; }
3434

3535
static KeyFormats getKeyFormat(int rawKeyFormat);
3636

3737
private:
3838
KeyFormats keyFormat;
39-
QString password;
39+
std::string password;
4040
int pageSize;
4141
int kdfIterations;
42-
QString hmacAlgorithm;
43-
QString kdfAlgorithm;
42+
std::string hmacAlgorithm;
43+
std::string kdfAlgorithm;
4444
};
4545

4646
#endif // CIPHERSETTINGS_H

src/ColumnDisplayFormatDialog.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,12 @@ void ColumnDisplayFormatDialog::accept()
111111
// Execute a query using the display format and check that it only returns one column.
112112
int customNumberColumns = 0;
113113

114-
DBBrowserDB::execCallback callback = [&customNumberColumns](int numberColumns, QStringList, QStringList) -> bool {
114+
DBBrowserDB::execCallback callback = [&customNumberColumns](int numberColumns, std::vector<QByteArray>, std::vector<QByteArray>) -> bool {
115115
customNumberColumns = numberColumns;
116116
// Return false so the query is not aborted and no error is reported.
117117
return false;
118118
};
119-
if(!pdb.executeSQL(QString("SELECT %1 FROM %2 LIMIT 1").arg(ui->editDisplayFormat->text(), QString::fromStdString(curTable.toString())),
119+
if(!pdb.executeSQL("SELECT " + ui->editDisplayFormat->text().toStdString() + " FROM " + curTable.toString() + " LIMIT 1",
120120
false, true, callback))
121121
errorMessage = tr("Error in custom display format. Message from database engine:\n\n%1").arg(pdb.lastError());
122122
else if(customNumberColumns != 1)

src/CondFormat.cpp

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "CondFormat.h"
22
#include "Settings.h"
33
#include "Data.h"
4+
#include "sqlitedb.h"
45

56
#include <QAbstractTableModel>
67

@@ -48,8 +49,11 @@ CondFormat::CondFormat(const QString& filter,
4849
m_font.fromString(model->data(index, Qt::FontRole).toString());
4950
}
5051

51-
QString CondFormat::filterToSqlCondition(const QString& value, const QString& encoding)
52+
std::string CondFormat::filterToSqlCondition(const QString& value, const QString& encoding)
5253
{
54+
if(value.isEmpty())
55+
return {};
56+
5357
// Check for any special comparison operators at the beginning of the value string. If there are none default to LIKE.
5458
QString op = "LIKE";
5559
QString val, val2;
@@ -89,15 +93,15 @@ QString CondFormat::filterToSqlCondition(const QString& value, const QString& en
8993
numeric = true;
9094
val = "''";
9195
} else {
92-
value.mid(2).toFloat(&numeric);
96+
value.midRef(2).toFloat(&numeric);
9397
op = value.left(2);
9498
val = value.mid(2);
9599
}
96-
} else if(value.left(1) == ">" || value.left(1) == "<") {
97-
value.mid(1).toFloat(&numeric);
98-
op = value.left(1);
100+
} else if(value.front() == ">" || value.front() == "<") {
101+
value.midRef(1).toFloat(&numeric);
102+
op = value.front();
99103
val = value.mid(1);
100-
} else if(value.left(1) == "=") {
104+
} else if(value.front() == "=") {
101105
val = value.mid(1);
102106

103107
// Check if value to compare with is 'NULL'
@@ -111,7 +115,7 @@ QString CondFormat::filterToSqlCondition(const QString& value, const QString& en
111115
op = "IS";
112116
numeric = true;
113117
}
114-
} else if(value.left(1) == "/" && value.right(1) == "/" && value.length() > 2) {
118+
} else if(value.front() == "/" && value.back() == "/" && value.size() > 2) {
115119
val = value.mid(1, value.length() - 2);
116120
op = "REGEXP";
117121
numeric = false;
@@ -138,18 +142,18 @@ QString CondFormat::filterToSqlCondition(const QString& value, const QString& en
138142
if(val.isEmpty())
139143
val = value;
140144

141-
if(val == "" || val == "%" || val == "%%")
142-
return QString();
145+
if(val.isEmpty() || val == "%" || val == "%%")
146+
return {};
143147
else {
144148
// Quote and escape value, but only if it's not numeric and not the empty string sequence
145149
if(!numeric && val != "''")
146-
val = QString("'%1'").arg(val.replace("'", "''"));
150+
val = sqlb::escapeString(val);
147151

148152
QString whereClause(op + " " + QString(encodeString(val.toUtf8(), encoding)));
149153
if (!val2.isEmpty())
150154
whereClause += " AND " + QString(encodeString(val2.toUtf8(), encoding));
151155
whereClause += " " + escape;
152-
return whereClause;
156+
return whereClause.toStdString();
153157
}
154158
}
155159

src/CondFormat.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,18 @@ class CondFormat
4343
const QModelIndex index,
4444
const QString& encoding = QString());
4545

46-
static QString filterToSqlCondition(const QString& value, const QString& encoding = QString());
46+
static std::string filterToSqlCondition(const QString& value, const QString& encoding = QString());
4747

4848
private:
49-
QString m_sqlCondition;
49+
std::string m_sqlCondition;
5050
QString m_filter;
5151
QColor m_bgColor;
5252
QColor m_fgColor;
5353
QFont m_font;
5454
Alignment m_align;
5555

5656
public:
57-
QString sqlCondition() const { return m_sqlCondition; }
57+
std::string sqlCondition() const { return m_sqlCondition; }
5858
QString filter() const { return m_filter; }
5959

6060
QColor backgroundColor() const { return m_bgColor; }

src/CondFormatManager.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@ CondFormatManager::CondFormatManager(const std::vector<CondFormat>& condFormats,
5656
ui->tableCondFormats->resizeColumnToContents(col);
5757
}
5858

59-
connect(ui->buttonAdd, SIGNAL(clicked(bool)), this, SLOT(addNewItem()));
60-
connect(ui->buttonRemove, SIGNAL(clicked(bool)), this, SLOT(removeItem()));
59+
connect(ui->buttonAdd, &QToolButton::clicked, this, &CondFormatManager::addNewItem);
60+
connect(ui->buttonRemove, &QToolButton::clicked, this, &CondFormatManager::removeItem);
6161

62-
connect(ui->buttonDown, SIGNAL(clicked(bool)), this, SLOT(downItem()));
63-
connect(ui->buttonUp, SIGNAL(clicked(bool)), this, SLOT(upItem()));
62+
connect(ui->buttonDown, &QToolButton::clicked, this, &CondFormatManager::downItem);
63+
connect(ui->buttonUp, &QToolButton::clicked, this, &CondFormatManager::upItem);
6464

6565
connect(ui->tableCondFormats, &QTreeWidget::itemClicked, this, &CondFormatManager::itemClicked);
6666
}
@@ -181,7 +181,7 @@ void CondFormatManager::downItem()
181181
moveItem(+1);
182182
}
183183

184-
std::vector<CondFormat> CondFormatManager::getCondFormats()
184+
std::vector<CondFormat> CondFormatManager::getCondFormats() const
185185
{
186186
std::vector<CondFormat> result;
187187

0 commit comments

Comments
 (0)