File indexing completed on 2024-12-15 05:02:05

0001 /*
0002     SPDX-FileCopyrightText: 2016 Ivan Čukić <ivan.cukic(at)kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 
0007 #include "BalooRunner.h"
0008 
0009 #include <QDebug>
0010 #include <QDir>
0011 #include <KPluginFactory>
0012 #include <QMimeDatabase>
0013 
0014 #include <KLocalizedString>
0015 
0016 #include <Baloo/Query>
0017 
0018 BLADE_EXPORT_PLUGIN(baloorunner, BalooRunner, "blade-plugin-baloo.json")
0019 
0020 BalooRunner::BalooRunner(QObject *parent, const QVariantList &args)
0021     : AbstractRunner(parent)
0022 {
0023     Q_UNUSED(args)
0024 }
0025 
0026 BalooRunner::~BalooRunner()
0027 {
0028 }
0029 
0030 void BalooRunner::query()
0031 {
0032     qDebug() << queryString();
0033     emit startedProcessingQuery();
0034 
0035     queryType("Document", i18n("Document"));
0036     queryType("Image",    i18n("Image"));
0037     queryType("Audio",    i18n("Audio"));
0038     queryType("Video",    i18n("Video"));
0039     queryType("Folder",   i18n("Folder"));
0040 
0041     emit finishedProcessingQuery();
0042 }
0043 
0044 void BalooRunner::queryType(const QString &type, const QString &localizedType)
0045 {
0046     Baloo::Query query;
0047 
0048     query.setSearchString(queryString());
0049     query.setType(type);
0050     query.setLimit(10);
0051 
0052     Baloo::ResultIterator it = query.exec();
0053 
0054     ResultList results;
0055 
0056     QMimeDatabase mimeDb;
0057 
0058     // Comment by Vishesh:
0059     // KRunner is absolutely retarded and allows plugins to set the global
0060     // relevance levels. so Baloo should not set the relevance of results too
0061     // high because then Applications will often appear after if the application
0062     // runner has not a higher relevance. So stupid.
0063     // Each runner plugin should not have to know about the others.
0064     // Anyway, that's why we're starting with .75
0065     float relevance = .75;
0066     while (it.next()) {
0067         Result result;
0068 
0069         const auto localUrl = it.filePath();
0070         const auto url = QUrl::fromLocalFile(localUrl);
0071 
0072         const auto iconName = mimeDb.mimeTypeForFile(localUrl).iconName();
0073 
0074         result.icon      = iconName;
0075         // result.resultId  = it.filePath();
0076         result.title     = url.fileName();
0077         result.url       = QUrl::fromLocalFile(it.filePath());
0078         result.relevance = relevance;
0079         relevance -= 0.05;
0080 
0081         auto folderPath
0082             = url.adjusted(QUrl::RemoveFilename | QUrl::StripTrailingSlash)
0083                   .toLocalFile();
0084 
0085         if (folderPath.startsWith(QDir::homePath())) {
0086             folderPath.replace(0, QDir::homePath().length(), QStringLiteral("~"));
0087         }
0088 
0089         result.description = folderPath;
0090 
0091         results << result;
0092     }
0093 
0094     emit reportNewResults(results);
0095 
0096 }
0097 
0098 #include "BalooRunner.moc"
0099