Commit 19c3d709 authored by Jonah Brüchert's avatar Jonah Brüchert 🌳

More optimization on the c++ side

parent bd3783dc
......@@ -85,20 +85,25 @@ void BookmarksHistoryModel::setQuery()
return;
QString command;
QString b = QStringLiteral("SELECT rowid AS id, url, title, icon, :now - lastVisited AS lastVisitedDelta, %1 AS bookmarked FROM %2 ");
QLatin1String filter = m_filter.isEmpty() ? QLatin1String() : QLatin1String("WHERE url LIKE '%' || :filter || '%' OR title LIKE '%' || :filter || '%'");
bool include_history = m_history && !(m_bookmarks && m_filter.isEmpty());
const QString b = QStringLiteral("SELECT rowid AS id, url, title, icon, :now - lastVisited AS lastVisitedDelta, %1 AS bookmarked FROM %2 ");
const QLatin1String filter = m_filter.isEmpty() ? QLatin1String() : QLatin1String("WHERE url LIKE '%' || :filter || '%' OR title LIKE '%' || :filter || '%'");
const bool includeHistory = m_history && !(m_bookmarks && m_filter.isEmpty());
if (m_bookmarks)
command = b.arg(1).arg(QLatin1String("bookmarks")) + filter;
if (m_bookmarks && include_history)
if (m_bookmarks && includeHistory)
command += QLatin1String("\n UNION \n");
if (include_history)
if (includeHistory)
command += b.arg(0).arg(QLatin1String("history")) + filter;
command += QLatin1String("\n ORDER BY bookmarked DESC, lastVisitedDelta ASC");
if (include_history)
if (includeHistory)
command += QStringLiteral("\n LIMIT %1").arg(QUERY_LIMIT);
qint64 ref = QDateTime::currentSecsSinceEpoch();
const qint64 ref = QDateTime::currentSecsSinceEpoch();
QSqlQuery query;
if (!query.prepare(command)) {
qWarning() << Q_FUNC_INFO << "Failed to prepare SQL statement";
......@@ -109,6 +114,7 @@ void BookmarksHistoryModel::setQuery()
if (!m_filter.isEmpty())
query.bindValue(QStringLiteral(":filter"), m_filter);
query.bindValue(QStringLiteral(":now"), ref);
if (!query.exec()) {
......
......@@ -32,7 +32,7 @@ BrowserManager *BrowserManager::s_instance = nullptr;
BrowserManager::BrowserManager(QObject *parent)
: QObject(parent)
, m_dbmanager(new DBManager)
, m_dbmanager(new DBManager(this))
{
connect(m_dbmanager, &DBManager::databaseTableChanged, this, &BrowserManager::databaseTableChanged);
}
......
......@@ -40,10 +40,10 @@ constexpr int MAX_BROWSER_HISTORY_SIZE = 3000;
DBManager::DBManager(QObject *parent)
: QObject(parent)
{
QString dbpath = QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation);
QString dbname = dbpath + QStringLiteral("/angelfish.sqlite");
const QString dbpath = QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation);
const QString dbname = dbpath + QStringLiteral("/angelfish.sqlite");
if(!QDir().mkpath(dbpath)) {
if (!QDir().mkpath(dbpath)) {
qCritical() << "Database directory does not exist and cannot be created: " << dbpath;
throw std::runtime_error("Database directory does not exist and cannot be created: " + dbpath.toStdString());
}
......@@ -125,12 +125,12 @@ bool DBManager::migrate()
bool DBManager::migrateTo1()
{
// Starting from empty database, let's create the tables.
QString bookmarks = QStringLiteral("CREATE TABLE bookmarks (url TEXT UNIQUE, title TEXT, icon TEXT, lastVisited INT)");
QString history = QStringLiteral("CREATE TABLE history (url TEXT UNIQUE, title TEXT, icon TEXT, lastVisited INT)");
QString icons = QStringLiteral("CREATE TABLE icons (url TEXT UNIQUE, icon BLOB)");
QString idx_bookmarks = QStringLiteral("CREATE UNIQUE INDEX idx_bookmarks_url ON bookmarks(url)");
QString idx_history = QStringLiteral("CREATE UNIQUE INDEX idx_history_url ON history(url)");
QString idx_icons = QStringLiteral("CREATE UNIQUE INDEX idx_icons_url ON icons(url)");
const QString bookmarks = QStringLiteral("CREATE TABLE bookmarks (url TEXT UNIQUE, title TEXT, icon TEXT, lastVisited INT)");
const QString history = QStringLiteral("CREATE TABLE history (url TEXT UNIQUE, title TEXT, icon TEXT, lastVisited INT)");
const QString icons = QStringLiteral("CREATE TABLE icons (url TEXT UNIQUE, icon BLOB)");
const QString idx_bookmarks = QStringLiteral("CREATE UNIQUE INDEX idx_bookmarks_url ON bookmarks(url)");
const QString idx_history = QStringLiteral("CREATE UNIQUE INDEX idx_history_url ON history(url)");
const QString idx_icons = QStringLiteral("CREATE UNIQUE INDEX idx_icons_url ON icons(url)");
if (!execute(bookmarks) || !execute(idx_bookmarks) || !execute(history) || !execute(idx_history) || !execute(icons) || !execute(idx_icons))
return false;
......@@ -155,10 +155,10 @@ void DBManager::trimIcons()
void DBManager::addRecord(const QString &table, const QVariantMap &pagedata)
{
QString url = pagedata.value(QStringLiteral("url")).toString();
QString title = pagedata.value(QStringLiteral("title")).toString();
QString icon = pagedata.value(QStringLiteral("icon")).toString();
qint64 lastVisited = QDateTime::currentSecsSinceEpoch();
const QString url = pagedata.value(QStringLiteral("url")).toString();
const QString title = pagedata.value(QStringLiteral("title")).toString();
const QString icon = pagedata.value(QStringLiteral("icon")).toString();
const qint64 lastVisited = QDateTime::currentSecsSinceEpoch();
if (url.isEmpty() || url == QStringLiteral("about:blank"))
return;
......
......@@ -86,7 +86,6 @@ void DesktopFileGenerator::storeIcon(const QString &url, const QString &fileName
const QString providerIconName = url.mid(prefix_favicon.size());
const QSize szRequested;
QSize szObtained;
const QString iconLocation = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation)
+ QStringLiteral("/icons/hicolor/16x16/apps/");
......@@ -101,7 +100,7 @@ void DesktopFileGenerator::storeIcon(const QString &url, const QString &fileName
switch (provider->imageType()) {
case QQmlImageProviderBase::Image: {
QImage image = provider->requestImage(providerIconName, &szObtained, szRequested);
const QImage image = provider->requestImage(providerIconName, nullptr, szRequested);
if (!image.save(imageFile, "PNG")) {
qWarning() << Q_FUNC_INFO << "Failed to save image" << url;
return;
......@@ -109,7 +108,7 @@ void DesktopFileGenerator::storeIcon(const QString &url, const QString &fileName
break;
}
case QQmlImageProviderBase::Pixmap: {
QPixmap image = provider->requestPixmap(providerIconName, &szObtained, szRequested);
const QPixmap image = provider->requestPixmap(providerIconName, nullptr, szRequested);
if (!image.save(imageFile, "PNG")) {
qWarning() << Q_FUNC_INFO << "Failed to save pixmap" << url;
return;
......
......@@ -50,7 +50,7 @@ QString IconImageProvider::providerId()
QString IconImageProvider::storeImage(const QString &iconSource)
{
QLatin1String prefix_favicon = QLatin1String("image://favicon/");
const QLatin1String prefix_favicon = QLatin1String("image://favicon/");
if (!iconSource.startsWith(prefix_favicon)) {
// don't know what to do with it, return as it is
qWarning() << Q_FUNC_INFO << "Don't know how to store image" << iconSource;
......@@ -58,7 +58,7 @@ QString IconImageProvider::storeImage(const QString &iconSource)
}
// new uri for image
QString url = QStringLiteral("image://%1/%2").arg(providerId(), iconSource.mid(prefix_favicon.size()));
const QString url = QStringLiteral("image://%1/%2").arg(providerId(), iconSource.mid(prefix_favicon.size()));
// check if we have that image already
QSqlQuery query_check;
......@@ -80,7 +80,7 @@ QString IconImageProvider::storeImage(const QString &iconSource)
// Store new icon
QQuickImageProvider *provider = dynamic_cast<QQuickImageProvider *>(s_engine->imageProvider(QStringLiteral("favicon")));
if (provider == nullptr) {
if (!provider) {
qWarning() << Q_FUNC_INFO << "Failed to load image provider" << url;
return iconSource; // as something is wrong
}
......@@ -89,12 +89,11 @@ QString IconImageProvider::storeImage(const QString &iconSource)
QBuffer buffer(&data);
buffer.open(QIODevice::WriteOnly);
QSize szRequested;
QSize szObtained;
QString providerIconName = iconSource.mid(prefix_favicon.size());
const QSize szRequested;
const QString providerIconName = iconSource.mid(prefix_favicon.size());
switch (provider->imageType()) {
case QQmlImageProviderBase::Image: {
QImage image = provider->requestImage(providerIconName, &szObtained, szRequested);
const QImage image = provider->requestImage(providerIconName, nullptr, szRequested);
if (!image.save(&buffer, "PNG")) {
qWarning() << Q_FUNC_INFO << "Failed to save image" << url;
return iconSource; // as something is wrong
......@@ -102,7 +101,7 @@ QString IconImageProvider::storeImage(const QString &iconSource)
break;
}
case QQmlImageProviderBase::Pixmap: {
QPixmap image = provider->requestPixmap(providerIconName, &szObtained, szRequested);
const QPixmap image = provider->requestPixmap(providerIconName, nullptr, szRequested);
if (!image.save(&buffer, "PNG")) {
qWarning() << Q_FUNC_INFO << "Failed to save pixmap" << url;
return iconSource; // as something is wrong
......
......@@ -51,8 +51,8 @@ void SqlQueryModel::generateRoleNames()
QVariant SqlQueryModel::data(const QModelIndex &index, int role) const
{
if (role > Qt::UserRole) {
int columnIdx = role - Qt::UserRole - 1;
QModelIndex modelIndex = this->index(index.row(), columnIdx);
const int columnIdx = role - Qt::UserRole - 1;
const QModelIndex modelIndex = this->index(index.row(), columnIdx);
return QSqlQueryModel::data(modelIndex, Qt::DisplayRole);
}
return {};
......
......@@ -145,7 +145,7 @@ bool TabsModel::loadTabs()
{
if (!m_privateMode) {
beginResetModel();
QString input = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + QStringLiteral("/angelfish/tabs.json");
const QString input = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + QStringLiteral("/angelfish/tabs.json");
QFile inputFile(input);
if (!inputFile.exists()) {
......@@ -325,7 +325,7 @@ void TabsModel::setIsMobile(int index, bool isMobile)
m_tabs[index].setIsMobile(isMobile);
QModelIndex mindex = createIndex(index, index);
const QModelIndex mindex = createIndex(index, index);
emit dataChanged(mindex, mindex, {RoleNames::IsMobileRole});
saveTabs();
}
......@@ -338,7 +338,7 @@ void TabsModel::setUrl(int index, const QString &url)
m_tabs[index].setUrl(url);
QModelIndex mindex = createIndex(index, index);
const QModelIndex mindex = createIndex(index, index);
emit dataChanged(mindex, mindex, {RoleNames::UrlRole});
saveTabs();
}
......
......@@ -48,9 +48,9 @@ QString UrlUtils::urlScheme(const QString &url)
QString UrlUtils::urlHostPort(const QString &url)
{
QUrl u(url);
const QUrl u(url);
QString r = u.host();
QStringList common = { QLatin1String("www."),
const QStringList common = { QLatin1String("www."),
QLatin1String("m."),
QLatin1String("mobile.") };
for (const auto &i: common) {
......@@ -60,9 +60,11 @@ QString UrlUtils::urlHostPort(const QString &url)
}
}
int p = u.port(-1);
const int p = u.port(-1);
if (p > 0)
r = QStringLiteral("%1:%2").arg(r).arg(p);
return r;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment