Commit c1b97eab authored by Méven Car's avatar Méven Car Committed by Méven Car
Browse files

Runner: make recentdocument use KActivityStats data

It is much more versatile over the current recentdocument runner :

 - It is activity aware
 - It has a longer history
 - It includes data from gtk apps
 - We will be able to add some quick filtering in krunner

See previous patch https://phabricator.kde.org/D26111
parent 2d76fee9
......@@ -28,7 +28,7 @@ include(KDEClangFormat)
find_package(KF5 ${KF5_MIN_VERSION} REQUIRED COMPONENTS
Plasma DocTools Runner Notifications NotifyConfig Su NewStuff Wallet
IdleTime Declarative I18n KCMUtils TextWidgets KDELibs4Support Crash GlobalAccel
DBusAddons Wayland CoreAddons People ActivitiesStats)
DBusAddons Wayland CoreAddons People ActivitiesStats Activities)
find_package(KDED CONFIG REQUIRED)
find_package(KF5NetworkManagerQt ${KF5_MIN_VERSION})
......@@ -77,11 +77,6 @@ find_package(Phonon4Qt5 4.6.60 REQUIRED NO_MODULE)
set_package_properties(Phonon4Qt5 PROPERTIES
DESCRIPTION "Qt-based audio library"
TYPE REQUIRED)
find_package(KF5Activities ${KF5_MIN_VERSION})
set_package_properties(KF5Activities PROPERTIES DESCRIPTION "management of Plasma activities"
TYPE OPTIONAL
PURPOSE "Needed by activity related plasmoids."
)
find_package(ZLIB)
set_package_properties(ZLIB PROPERTIES DESCRIPTION "Support for gzip compressed files and data streams"
......
......@@ -10,6 +10,7 @@ target_link_libraries(krunner_recentdocuments
KF5::KIOCore
KF5::KIOWidgets
KF5::I18n
KF5::ActivitiesStats
KF5::Runner
)
......
......@@ -23,13 +23,18 @@
#include <QDir>
#include <QMimeData>
#include <KDesktopFile>
#include <KConfigGroup>
#include <KDirWatch>
#include <KRun>
#include <KRecentDocument>
#include <KLocalizedString>
#include <KIO/OpenFileManagerWindowJob>
#include <KIO/Job>
#include <KShell>
#include <KActivities/Stats/ResultModel>
#include <KActivities/Stats/Terms>
#include <KActivities/Stats/Query>
using namespace KActivities::Stats;
using namespace KActivities::Stats::Terms;
K_EXPORT_PLASMA_RUNNER(recentdocuments, RecentDocuments)
......@@ -40,13 +45,7 @@ RecentDocuments::RecentDocuments(QObject *parent, const QVariantList& args)
{
Q_UNUSED(args);
setObjectName( QStringLiteral("Recent Documents" ));
loadRecentDocuments();
// listen for changes to the list of recent documents
KDirWatch *recentDocWatch = new KDirWatch(this);
recentDocWatch->addDir(KRecentDocument::recentDocumentDirectory(), KDirWatch::WatchFiles);
connect(recentDocWatch, &KDirWatch::created, this, &RecentDocuments::loadRecentDocuments);
connect(recentDocWatch, &KDirWatch::deleted, this, &RecentDocuments::loadRecentDocuments);
connect(recentDocWatch, &KDirWatch::dirty, this, &RecentDocuments::loadRecentDocuments);
addSyntax(Plasma::RunnerSyntax(QStringLiteral(":q:"), i18n("Looks for documents recently used with names matching :q:.")));
addAction(s_openParentDirId, QIcon::fromTheme(QStringLiteral("document-open-folder")), i18n("Open Containing Folder"));
......@@ -56,15 +55,9 @@ RecentDocuments::~RecentDocuments()
{
}
void RecentDocuments::loadRecentDocuments()
{
m_recentdocuments = KRecentDocument::recentDocuments();
}
void RecentDocuments::match(Plasma::RunnerContext &context)
{
if (m_recentdocuments.isEmpty()) {
if (!context.isValid()) {
return;
}
......@@ -73,46 +66,45 @@ void RecentDocuments::match(Plasma::RunnerContext &context)
return;
}
const QString homePath = QDir::homePath();
// avoid duplicates
QSet<QUrl> knownUrls;
for (const QString &document : qAsConst(m_recentdocuments)) {
if (!context.isValid()) {
return;
auto query = UsedResources
| Activity::current()
| Order::RecentlyUsedFirst
| Agent::any()
// we search only on file name, as KActivity does not support better options
| Url("/*/" + term + "*")
| Limit(30);
const auto result = new ResultModel(query);
for (int i = 0; i < result->rowCount(); i++) {
const auto index = result->index(i, 0);
const auto url = QUrl::fromUserInput(result->data(index, ResultModel::ResourceRole).toString(),
QString(),
// We can assume local file thanks to the request Url
QUrl::AssumeLocalFile);
const auto name = result->data(index, ResultModel::TitleRole).toString();
Plasma::QueryMatch match(this);
auto relevance = 0.5;
match.setType(Plasma::QueryMatch::PossibleMatch);
if (url.fileName() == term) {
relevance = 1.0;
match.setType(Plasma::QueryMatch::ExactMatch);
} else if(url.fileName().startsWith(term)) {
relevance = 0.9;
match.setType(Plasma::QueryMatch::PossibleMatch);
}
match.setIconName(KIO::iconNameForUrl(url));
match.setRelevance(relevance);
match.setData(QVariant(url));
match.setText(name);
if (document.contains(term, Qt::CaseInsensitive)) {
KDesktopFile config(document);
const QUrl url = QUrl(config.readUrl());
if (knownUrls.contains(url)) {
continue;
}
knownUrls.insert(url);
QString destUrlString = KShell::tildeCollapse(url.adjusted(QUrl::RemoveFilename).path());
match.setSubtext(destUrlString);
Plasma::QueryMatch match(this);
match.setType(Plasma::QueryMatch::PossibleMatch);
match.setRelevance(1.0);
match.setIconName(config.readIcon());
match.setData(url);
match.setText(config.readName());
QUrl folderUrl = url.adjusted(QUrl::RemoveFilename | QUrl::StripTrailingSlash);
if (folderUrl.isLocalFile()) {
QString folderPath = folderUrl.toLocalFile();
if (folderPath.startsWith(homePath)) {
folderPath.replace(0, homePath.length(), QStringLiteral("~"));
}
match.setSubtext(folderPath);
} else {
match.setSubtext(folderUrl.toDisplayString());
}
context.addMatch(match);
}
context.addMatch(match);
}
}
......@@ -120,14 +112,14 @@ void RecentDocuments::run(const Plasma::RunnerContext &context, const Plasma::Qu
{
Q_UNUSED(context)
const QString url = match.data().toString();
const QUrl url = match.data().toUrl();
if (match.selectedAction() && match.selectedAction() == action(s_openParentDirId)) {
KIO::highlightInFileManager({QUrl(url)});
if (match.selectedAction() == action(s_openParentDirId)) {
KIO::highlightInFileManager({url});
return;
}
auto run = new KRun(QUrl(url), nullptr);
auto run = new KRun(url, nullptr);
run->setRunExecutables(false);
}
......@@ -137,7 +129,9 @@ QList<QAction *> RecentDocuments::actionsForMatch(const Plasma::QueryMatch &matc
QList<QAction *> actions;
if (QUrl(match.data().toString()).isLocalFile()) {
const QUrl url = match.data().toUrl();
if (url.isLocalFile()) {
actions << action(s_openParentDirId);
}
......@@ -147,7 +141,7 @@ QList<QAction *> RecentDocuments::actionsForMatch(const Plasma::QueryMatch &matc
QMimeData * RecentDocuments::mimeDataForMatch(const Plasma::QueryMatch& match)
{
QMimeData *result = new QMimeData();
result->setText(match.data().toString());
result->setUrls({match.data().toUrl()});
return result;
}
......
......@@ -37,12 +37,6 @@ class RecentDocuments : public Plasma::AbstractRunner {
private Q_SLOTS:
QList<QAction *> actionsForMatch(const Plasma::QueryMatch &match) override;
QMimeData * mimeDataForMatch(const Plasma::QueryMatch &match) override;
private Q_SLOTS:
void loadRecentDocuments();
private:
QStringList m_recentdocuments;
};
......
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