Commit 61b02b16 authored by Alexander Lohnau's avatar Alexander Lohnau 💬
Browse files

BookmarksRunner: Change caching databases

Summary:
The copying/updating of the cache has been removed from the FetchSqlite class.
This allows the browsers to have handle the caching differently.

The firefox sqlite files are only copied if they changed. Before they were copied
for each match session.

The chrome favicon database is also only copied if needed
and the check is also made in the prepare method and not just when
the profile is initialized.

Test Plan:
For both firefox and chrome:
Plugin shows icons for results.
Add new bookmarks, close browser, the new bookmarks should show up with icons.

Reviewers: #plasma, ngraham, meven, broulik

Reviewed By: meven

Subscribers: plasma-devel

Tags: #plasma

Differential Revision: https://phabricator.kde.org/D29726
parent be84a82a
......@@ -20,7 +20,11 @@
#ifndef BROWSER_H
#define BROWSER_H
#include <QObject>
#include <QFile>
#include <QFileInfo>
#include <QDateTime>
#include <QString>
#include "bookmarkmatch.h"
......@@ -31,8 +35,35 @@ public:
virtual QList<BookmarkMatch> match(const QString& term, bool addEveryThing) = 0;
virtual void prepare() {}
enum CacheResult{
Error,
Copied,
Unchanged
};
public Q_SLOTS:
virtual void teardown() {}
protected:
/*
* Updates the cached file if the source has been modified
*/
CacheResult updateCacheFile(const QString &source, const QString &cache) {
if (source.isEmpty() || cache.isEmpty()) {
return Error;
}
QFileInfo cacheInfo(cache);
if (!QFileInfo::exists(cache) || !cacheInfo.isFile()) {
return QFile(source).copy(cache) ? Copied : Error;
}
QFileInfo sourceInfo(source);
if (sourceInfo.lastModified() > cacheInfo.lastModified()) {
QFile::remove(cache);
return QFile(source).copy(cache) ? Copied : Error;
}
return Unchanged;
}
};
......
......@@ -52,6 +52,7 @@ Chrome::Chrome( FindProfile* findProfile, QObject* parent )
{
const auto profiles = findProfile->find();
for(const Profile &profile : profiles) {
updateCacheFile(profile.faviconSource(), profile.faviconCache());
m_profileBookmarks << new ProfileBookmarks(profile);
m_watcher->addFile(profile.path());
}
......@@ -114,6 +115,7 @@ void Chrome::prepare()
for (const QJsonValue &folder : entries) {
parseFolder(folder.toObject(), profileBookmarks);
}
updateCacheFile(profile.faviconSource(), profile.faviconCache());
profile.favicon()->prepare();
}
}
......
......@@ -61,7 +61,7 @@ QList<Profile> FindChromeProfile::find()
for(const QString &profile : profilesConfig.keys()) {
const QString profilePath = QStringLiteral("%1/%2").arg(configDirectory, profile);
const QString profileBookmarksPath = QStringLiteral("%1/%2").arg(profilePath, QStringLiteral("Bookmarks"));
profiles << Profile(profileBookmarksPath, FaviconFromBlob::chrome(profilePath, this));
profiles << Profile(profileBookmarksPath, profile, FaviconFromBlob::chrome(profilePath, this));
}
return profiles;
......
......@@ -21,16 +21,28 @@
#define FIND_PROFILE_H
#include <QString>
#include <QList>
#include <QStandardPaths>
class Favicon;
class Profile {
public:
Profile(const QString &path, Favicon *favicon) : m_path(path), m_favicon(favicon) {}
Profile(const QString &path, const QString &name, Favicon *favicon) : m_path(path), m_name(name), m_favicon(favicon){
// Remove "Bookmarks" from end of path
m_faviconSource = path.chopped(9) + QStringLiteral("Favicons");
m_faviconCache = QStringLiteral("%1/KRunner-Chrome-Favicons-%2.sqlite")
.arg(QStandardPaths::writableLocation(QStandardPaths::CacheLocation), name);
}
inline QString path() const { return m_path; }
inline QString name() const { return m_name; }
inline Favicon *favicon() const { return m_favicon; }
inline QString faviconSource() const { return m_faviconSource; }
inline QString faviconCache() const { return m_faviconCache; }
private:
QString m_path;
QString m_name;
Favicon *m_favicon;
QString m_faviconSource;
QString m_faviconCache;
};
class FindProfile {
......
......@@ -37,35 +37,37 @@ Firefox::Firefox(QObject *parent) :
m_fetchsqlite_fav(nullptr)
{
reloadConfiguration();
m_dbCacheFile = QStandardPaths::writableLocation(QStandardPaths::CacheLocation)
+ QStringLiteral("/bookmarkrunnerfirefoxdbfile.sqlite");
m_dbCacheFile_fav = QStandardPaths::writableLocation(QStandardPaths::CacheLocation)
+ QStringLiteral("/bookmarkrunnerfirefoxfavdbfile.sqlite");
}
Firefox::~Firefox()
{
if (!m_dbCacheFile.isEmpty()) {
// Delete the cached databases
if (!m_dbFile.isEmpty()) {
QFile db_CacheFile(m_dbCacheFile);
if (db_CacheFile.exists()) {
//qDebug() << "Cache file was removed: " << db_CacheFile.remove();
db_CacheFile.remove();
}
}
if (!m_dbFile_fav.isEmpty()) {
QFile db_CacheFileFav(m_dbCacheFile_fav);
if (db_CacheFileFav.exists()) {
db_CacheFileFav.remove();
}
}
//qDebug() << "Deleted Firefox Bookmarks Browser";
}
void Firefox::prepare()
{
if (m_dbCacheFile.isEmpty()) {
m_dbCacheFile = QStandardPaths::writableLocation(QStandardPaths::CacheLocation)
+ QStringLiteral("/bookmarkrunnerfirefoxdbfile.sqlite");
}
if (m_dbCacheFile_fav.isEmpty()) {
m_dbCacheFile_fav = QStandardPaths::writableLocation(QStandardPaths::CacheLocation)
+ QStringLiteral("/bookmarkrunnerfirefoxfavdbfile.sqlite");
}
if (!m_dbFile.isEmpty()) {
m_fetchsqlite = new FetchSqlite(m_dbFile, m_dbCacheFile);
if (updateCacheFile(m_dbFile, m_dbCacheFile) != Error) {
m_fetchsqlite = new FetchSqlite(m_dbCacheFile);
m_fetchsqlite->prepare();
}
if (!m_dbFile_fav.isEmpty()) {
m_fetchsqlite_fav = new FetchSqlite(m_dbFile_fav, m_dbCacheFile_fav);
if (updateCacheFile(m_dbFile_fav, m_dbCacheFile_fav) != Error) {
m_fetchsqlite_fav = new FetchSqlite(m_dbCacheFile_fav);
m_fetchsqlite_fav->prepare();
delete m_favicon;
......
......@@ -40,7 +40,7 @@ FaviconFromBlob *FaviconFromBlob::chrome(const QString &profileDirectory, QObjec
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);
FetchSqlite *fetchSqlite = new FetchSqlite(faviconCache, parent);
QString faviconQuery;
if(fetchSqlite->tables().contains(QLatin1String("favicon_bitmaps"))) {
......
......@@ -30,21 +30,13 @@
#include <thread>
#include <sstream>
FetchSqlite::FetchSqlite(const QString &originalFilePath, const QString &copyTo, QObject *parent) :
QObject(parent), m_databaseFile(copyTo)
FetchSqlite::FetchSqlite(const QString &databaseFile, QObject *parent) :
QObject(parent), m_databaseFile(databaseFile)
{
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()
{
QFile(m_databaseFile).remove();
}
void FetchSqlite::prepare()
......@@ -93,6 +85,9 @@ QList<QVariantMap> FetchSqlite::query(const QString &sql, QMap<QString, QVariant
QMutexLocker lock(&m_mutex);
auto db = openDbConnection(m_databaseFile);
if (!db.isValid()) {
return QList<QVariantMap>();
}
//qDebug() << "query: " << sql;
QSqlQuery query(db);
......
......@@ -35,7 +35,7 @@ class FetchSqlite : public QObject
{
Q_OBJECT
public:
explicit FetchSqlite(const QString &originalFile, const QString &copyTo, QObject *parent = nullptr);
explicit FetchSqlite(const QString &databaseFile, QObject *parent = nullptr);
~FetchSqlite() override;
void prepare();
void teardown();
......
......@@ -29,7 +29,7 @@ using namespace Plasma;
void TestChromeBookmarks::initTestCase()
{
m_findBookmarksInCurrentDirectory.reset(new FakeFindProfile(QList<Profile>({Profile("chrome-config-home/Chrome-Bookmarks-Sample.json", new FallbackFavicon())})));
m_findBookmarksInCurrentDirectory.reset(new FakeFindProfile(QList<Profile>({Profile("chrome-config-home/Chrome-Bookmarks-Sample.json", "Sample", new FallbackFavicon())})));
}
void TestChromeBookmarks::bookmarkFinderShouldFindEachProfileDirectory()
......@@ -60,7 +60,7 @@ void TestChromeBookmarks::itShouldFindNothingWhenPrepareIsNotCalled()
void TestChromeBookmarks::itShouldGracefullyExitWhenFileIsNotFound()
{
FakeFindProfile finder(QList<Profile>() << Profile("FileNotExisting.json", nullptr));
FakeFindProfile finder(QList<Profile>() << Profile("FileNotExisting.json", QString(), nullptr));
Chrome *chrome = new Chrome(&finder, this);
chrome->prepare();
QCOMPARE(chrome->match("any", true).size(), 0);
......@@ -109,8 +109,8 @@ void TestChromeBookmarks::itShouldClearResultAfterCallingTeardown()
void TestChromeBookmarks::itShouldFindBookmarksFromAllProfiles()
{
FakeFindProfile findBookmarksFromAllProfiles(QList<Profile>()
<< Profile("chrome-config-home/Chrome-Bookmarks-Sample.json", new FallbackFavicon(this))
<< Profile("chrome-config-home/Chrome-Bookmarks-SecondProfile.json", new FallbackFavicon(this)) );
<< Profile("chrome-config-home/Chrome-Bookmarks-Sample.json", "Sample", new FallbackFavicon(this))
<< Profile("chrome-config-home/Chrome-Bookmarks-SecondProfile.json", "SecondProfile", new FallbackFavicon(this)) );
Chrome *chrome = new Chrome(&findBookmarksFromAllProfiles, this);
chrome->prepare();
QList<BookmarkMatch> matches = chrome->match("any", true);
......
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