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 "ActivitiesRunner.h"
0008 
0009 #include <QDebug>
0010 #include <KPluginFactory>
0011 
0012 using KActivities::ActivitiesModel;
0013 
0014 #define EXACT_MATCH_SCORE 1
0015 #define NAME_SCORE .8
0016 #define DESCRIPTION_SCORE .5
0017 
0018 BLADE_EXPORT_PLUGIN(activitiesrunner, ActivitiesRunner, "blade-plugin-activities.json")
0019 
0020 ActivitiesRunner::ActivitiesRunner(QObject *parent, const QVariantList &args)
0021     : AbstractRunner(parent)
0022 {
0023     Q_UNUSED(args)
0024 }
0025 
0026 ActivitiesRunner::~ActivitiesRunner()
0027 {
0028 }
0029 
0030 QString ActivitiesRunner::activityData(int row, int role) const
0031 {
0032     const auto index = activities.index(row);
0033     return activities.data(index, role).toString();
0034 }
0035 
0036 void ActivitiesRunner::query()
0037 {
0038     emit startedProcessingQuery();
0039 
0040     const auto queryWords = queryString().split(" ", QString::SkipEmptyParts);
0041 
0042     ResultList results;
0043 
0044     for (int i = 0; i < activities.rowCount(); ++i) {
0045         qreal score = 0.0;
0046 
0047         const auto activityId
0048             = activityData(i, ActivitiesModel::ActivityId);
0049         const auto activityName
0050             = activityData(i, ActivitiesModel::ActivityName).toLower();
0051         const auto activityDescription
0052             = activityData(i, ActivitiesModel::ActivityDescription).toLower();
0053 
0054         if (activityName == queryString() || activityDescription == queryString()) {
0055             score = EXACT_MATCH_SCORE;
0056 
0057         } else {
0058             bool allMatched = true;
0059 
0060             for (const auto &_word: queryWords) {
0061                 auto word = _word.toLower();    // ... I want ranges
0062 
0063                 score += activityName == word               ? EXACT_MATCH_SCORE
0064                        : activityName.contains(word)        ? NAME_SCORE
0065                        : activityDescription.contains(word) ? DESCRIPTION_SCORE
0066                        : (allMatched = false);  // this is evil
0067             }
0068 
0069             if (allMatched) {
0070                 score /= queryWords.count();
0071             } else {
0072                 score = 0;
0073             }
0074         }
0075 
0076         if (score > .2) {
0077             Result result;
0078 
0079             result.title       = activityName;
0080             result.description = activityDescription;
0081             result.matchedText = activityName + " " + activityDescription;
0082             result.url         = "activities://" + activityId;
0083             result.relevance   = score;
0084 
0085             results << result;
0086         }
0087     }
0088 
0089     emit reportNewResults(results);
0090 
0091     emit finishedProcessingQuery();
0092 }
0093 
0094 #include "ActivitiesRunner.moc"
0095