diff --git a/runners/bookmarks/faviconfromblob.cpp b/runners/bookmarks/faviconfromblob.cpp index a9daffa59..cb71ff4b2 100644 --- a/runners/bookmarks/faviconfromblob.cpp +++ b/runners/bookmarks/faviconfromblob.cpp @@ -1,138 +1,137 @@ /* * Copyright 2007 Glenn Ergeerts * Copyright 2012 Glenn Ergeerts * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Library General Public License as * published by the Free Software Foundation; either version 2, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details * * You should have received a copy of the GNU Library General Public * License along with this program; if not, write to the * Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "faviconfromblob.h" #include #include #include #include #include #include #include #include "bookmarksrunner_defs.h" #include "fetchsqlite.h" #include #include #include #include class StaticQuery : public BuildQuery { public: StaticQuery(const QString &query) : m_query(query) {} - QString query(QSqlDatabase *database) const override { - Q_UNUSED(database); + QString query() const override { return m_query; } private: const QString m_query; }; FaviconFromBlob *FaviconFromBlob::chrome(const QString &profileDirectory, QObject *parent) { QString profileName = QFileInfo(profileDirectory).fileName(); QString faviconCache = QStringLiteral("%1/KRunner-Chrome-Favicons-%2.sqlite") .arg(QStandardPaths::writableLocation(QStandardPaths::CacheLocation), profileName); FetchSqlite *fetchSqlite = new FetchSqlite(profileDirectory + QStringLiteral("/Favicons"), faviconCache, parent); QString faviconQuery; if(fetchSqlite->tables().contains(QStringLiteral("favicon_bitmaps"))) { faviconQuery = QLatin1String("SELECT * FROM favicons " \ "inner join icon_mapping on icon_mapping.icon_id = favicons.id " \ "inner join favicon_bitmaps on icon_mapping.icon_id = favicon_bitmaps.icon_id " \ "WHERE page_url = :url ORDER BY height desc LIMIT 1;"); } else { faviconQuery = QLatin1String("SELECT * FROM favicons " \ "inner join icon_mapping on icon_mapping.icon_id = favicons.id " \ "WHERE page_url = :url LIMIT 1;"); } return new FaviconFromBlob(profileName, new StaticQuery(faviconQuery), QStringLiteral("image_data"), fetchSqlite, parent); } FaviconFromBlob *FaviconFromBlob::firefox(FetchSqlite *fetchSqlite, QObject *parent) { QString faviconQuery = QStringLiteral("SELECT moz_icons.data FROM moz_icons" \ " INNER JOIN moz_icons_to_pages ON moz_icons.id = moz_icons_to_pages.icon_id" \ " INNER JOIN moz_pages_w_icons ON moz_icons_to_pages.page_id = moz_pages_w_icons.id" \ " WHERE moz_pages_w_icons.page_url = :url LIMIT 1;"); return new FaviconFromBlob(QStringLiteral("firefox-default"), new StaticQuery(faviconQuery), QStringLiteral("data"), fetchSqlite, parent); } FaviconFromBlob::FaviconFromBlob(const QString &profileName, BuildQuery *buildQuery, const QString &blobColumn, FetchSqlite *fetchSqlite, QObject *parent) : Favicon(parent), m_buildQuery(buildQuery), m_blobcolumn(blobColumn), m_fetchsqlite(fetchSqlite) { m_profileCacheDirectory = QStringLiteral("%1/KRunner-Favicons-%2") .arg(QStandardPaths::writableLocation(QStandardPaths::CacheLocation), profileName); //qDebug() << "got cache directory: " << m_profileCacheDirectory; cleanCacheDirectory(); QDir().mkpath(m_profileCacheDirectory); } FaviconFromBlob::~FaviconFromBlob() { cleanCacheDirectory(); delete m_buildQuery; } void FaviconFromBlob::prepare() { m_fetchsqlite->prepare(); } void FaviconFromBlob::teardown() { m_fetchsqlite->teardown(); } void FaviconFromBlob::cleanCacheDirectory() { QDir(m_profileCacheDirectory).removeRecursively(); } QIcon FaviconFromBlob::iconFor(const QString &url) { //qDebug() << "got url: " << url; QString fileChecksum = QString::number(qChecksum(url.toLatin1(), url.toLatin1().size())); QFile iconFile( m_profileCacheDirectory + QDir::separator() + fileChecksum + QStringLiteral("_favicon") ); if(iconFile.size() == 0) iconFile.remove(); if(!iconFile.exists()) { QMap bindVariables; bindVariables.insert(QStringLiteral(":url"), url); QList faviconFound = m_fetchsqlite->query(m_buildQuery, bindVariables); if(faviconFound.isEmpty()) return defaultIcon(); QByteArray iconData = faviconFound.first().value(m_blobcolumn).toByteArray(); //qDebug() << "Favicon found: " << iconData.size() << " bytes"; if(iconData.size() <=0) return defaultIcon(); iconFile.open(QFile::WriteOnly); iconFile.write(iconData); iconFile.close(); } return QIcon(iconFile.fileName()); } diff --git a/runners/bookmarks/fetchsqlite.cpp b/runners/bookmarks/fetchsqlite.cpp index ce6cb74f0..deb633dd5 100644 --- a/runners/bookmarks/fetchsqlite.cpp +++ b/runners/bookmarks/fetchsqlite.cpp @@ -1,109 +1,109 @@ /* * Copyright 2007 Glenn Ergeerts * Copyright 2012 Glenn Ergeerts * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Library General Public License as * published by the Free Software Foundation; either version 2, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details * * You should have received a copy of the GNU Library General Public * License along with this program; if not, write to the * Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "fetchsqlite.h" #include #include #include "bookmarksrunner_defs.h" #include #include #include #include FetchSqlite::FetchSqlite(const QString &originalFilePath, const QString ©To, QObject *parent) : QObject(parent), m_databaseFile(copyTo) { m_db = QSqlDatabase::addDatabase(QStringLiteral("QSQLITE"), originalFilePath); m_db.setHostName(QStringLiteral("localhost")); QFile originalFile(originalFilePath); QFile(copyTo).remove(); bool couldCopy = originalFile.copy(copyTo); if(!couldCopy) { //qDebug() << "error copying favicon database from " << originalFile.fileName() << " to " << copyTo; //qDebug() << originalFile.errorString(); } } FetchSqlite::~FetchSqlite() { if(m_db.isOpen()) m_db.close(); QFile(m_databaseFile).remove(); } void FetchSqlite::prepare() { QMutexLocker lock(&m_mutex); m_db.setDatabaseName(m_databaseFile); bool ok = m_db.open(); //qDebug() << "Sqlite Database " << m_databaseFile << " was opened: " << ok; if(!ok) { //qDebug() << "Error: " << m_db.lastError().text(); } } void FetchSqlite::teardown() { QMutexLocker lock(&m_mutex); m_db.close(); } QList FetchSqlite::query(BuildQuery *buildQuery, QMap bindObjects) { - return query(buildQuery->query(&m_db), bindObjects); + return query(buildQuery->query(), bindObjects); } QList FetchSqlite::query(const QString &sql, QMap bindObjects) { QMutexLocker lock(&m_mutex); //qDebug() << "query: " << sql; QSqlQuery query(m_db); query.prepare(sql); foreach(const QString &variableName, bindObjects.keys()) { query.bindValue(variableName, bindObjects.value(variableName)); //qDebug() << "* Bound " << variableName << " to " << query.boundValue(variableName); } if(!query.exec()) { QSqlError error = m_db.lastError(); //qDebug() << "query failed: " << error.text() << " (" << error.type() << ", " << error.number() << ")"; //qDebug() << query.lastQuery(); } QList result; while(query.next()) { QVariantMap recordValues; QSqlRecord record = query.record(); for(int field=0; field * Copyright 2012 Glenn Ergeerts * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Library General Public License as * published by the Free Software Foundation; either version 2, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details * * You should have received a copy of the GNU Library General Public * License along with this program; if not, write to the * Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifndef FETCHSQLITE_H #define FETCHSQLITE_H #include #include #include #include #include #include #include class BuildQuery { public: - virtual QString query(QSqlDatabase *database) const = 0; + virtual QString query() const = 0; virtual ~BuildQuery() {} }; class FetchSqlite : public QObject { Q_OBJECT public: explicit FetchSqlite(const QString &originalFile, const QString ©To, QObject *parent = nullptr); ~FetchSqlite() override; void prepare(); void teardown(); QList query(const QString &sql, QMap bindObjects); QList query(BuildQuery *buildQuery, QMap bindObjects); QList query(const QString &sql); QStringList tables(QSql::TableType type = QSql::Tables); private: QString const m_databaseFile; QMutex m_mutex; QSqlDatabase m_db; }; #endif // FETCHSQLITE_H