diff --git a/runners/bookmarks/browser.h b/runners/bookmarks/browser.h --- a/runners/bookmarks/browser.h +++ b/runners/bookmarks/browser.h @@ -20,7 +20,11 @@ #ifndef BROWSER_H #define BROWSER_H + #include +#include +#include +#include #include #include "bookmarkmatch.h" @@ -31,8 +35,35 @@ virtual QList 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; + } }; diff --git a/runners/bookmarks/browsers/chrome.cpp b/runners/bookmarks/browsers/chrome.cpp --- a/runners/bookmarks/browsers/chrome.cpp +++ b/runners/bookmarks/browsers/chrome.cpp @@ -52,6 +52,7 @@ { 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 @@ for (const QJsonValue &folder : entries) { parseFolder(folder.toObject(), profileBookmarks); } + updateCacheFile(profile.faviconSource(), profile.faviconCache()); profile.favicon()->prepare(); } } diff --git a/runners/bookmarks/browsers/chromefindprofile.cpp b/runners/bookmarks/browsers/chromefindprofile.cpp --- a/runners/bookmarks/browsers/chromefindprofile.cpp +++ b/runners/bookmarks/browsers/chromefindprofile.cpp @@ -61,7 +61,7 @@ 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; diff --git a/runners/bookmarks/browsers/findprofile.h b/runners/bookmarks/browsers/findprofile.h --- a/runners/bookmarks/browsers/findprofile.h +++ b/runners/bookmarks/browsers/findprofile.h @@ -21,16 +21,28 @@ #define FIND_PROFILE_H #include #include +#include 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 { diff --git a/runners/bookmarks/browsers/firefox.cpp b/runners/bookmarks/browsers/firefox.cpp --- a/runners/bookmarks/browsers/firefox.cpp +++ b/runners/bookmarks/browsers/firefox.cpp @@ -37,35 +37,37 @@ 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; diff --git a/runners/bookmarks/faviconfromblob.cpp b/runners/bookmarks/faviconfromblob.cpp --- a/runners/bookmarks/faviconfromblob.cpp +++ b/runners/bookmarks/faviconfromblob.cpp @@ -40,7 +40,7 @@ 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"))) { diff --git a/runners/bookmarks/fetchsqlite.h b/runners/bookmarks/fetchsqlite.h --- a/runners/bookmarks/fetchsqlite.h +++ b/runners/bookmarks/fetchsqlite.h @@ -35,7 +35,7 @@ { Q_OBJECT public: - explicit FetchSqlite(const QString &originalFile, const QString ©To, QObject *parent = nullptr); + explicit FetchSqlite(const QString &databaseFile, QObject *parent = nullptr); ~FetchSqlite() override; void prepare(); void teardown(); diff --git a/runners/bookmarks/fetchsqlite.cpp b/runners/bookmarks/fetchsqlite.cpp --- a/runners/bookmarks/fetchsqlite.cpp +++ b/runners/bookmarks/fetchsqlite.cpp @@ -30,21 +30,13 @@ #include #include -FetchSqlite::FetchSqlite(const QString &originalFilePath, const QString ©To, 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 @@ QMutexLocker lock(&m_mutex); auto db = openDbConnection(m_databaseFile); + if (!db.isValid()) { + return QList(); + } //qDebug() << "query: " << sql; QSqlQuery query(db); diff --git a/runners/bookmarks/tests/testchromebookmarks.cpp b/runners/bookmarks/tests/testchromebookmarks.cpp --- a/runners/bookmarks/tests/testchromebookmarks.cpp +++ b/runners/bookmarks/tests/testchromebookmarks.cpp @@ -29,7 +29,7 @@ void TestChromeBookmarks::initTestCase() { - m_findBookmarksInCurrentDirectory.reset(new FakeFindProfile(QList({Profile("chrome-config-home/Chrome-Bookmarks-Sample.json", new FallbackFavicon())}))); + m_findBookmarksInCurrentDirectory.reset(new FakeFindProfile(QList({Profile("chrome-config-home/Chrome-Bookmarks-Sample.json", "Sample", new FallbackFavicon())}))); } void TestChromeBookmarks::bookmarkFinderShouldFindEachProfileDirectory() @@ -60,7 +60,7 @@ void TestChromeBookmarks::itShouldGracefullyExitWhenFileIsNotFound() { - FakeFindProfile finder(QList() << Profile("FileNotExisting.json", nullptr)); + FakeFindProfile finder(QList() << 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::itShouldFindBookmarksFromAllProfiles() { FakeFindProfile findBookmarksFromAllProfiles(QList() - << 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 matches = chrome->match("any", true);