1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
|
/*
SPDX-FileCopyrightText: 2000 Malte Starostik <malte@kde.org>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "searchprovider.h"
#include <KConfigGroup>
#include <KDesktopFile>
#include <KIO/Global> // KIO::iconNameForUrl
#include <KRandom>
#include <KService>
#include <QFileInfo>
#include <QStandardPaths>
SearchProvider::SearchProvider(const QString &servicePath)
: m_dirty(false)
{
setDesktopEntryName(QFileInfo(servicePath).baseName());
KDesktopFile parser(servicePath);
setName(parser.readName());
KConfigGroup group(parser.desktopGroup());
setKeys(group.readEntry(QStringLiteral("Keys"), QStringList()));
m_query = group.readEntry(QStringLiteral("Query"));
m_charset = group.readEntry(QStringLiteral("Charset"));
m_iconName = group.readEntry(QStringLiteral("Icon"));
m_isHidden = group.readEntry(QStringLiteral("Hidden"), false);
}
SearchProvider::~SearchProvider()
{
}
void SearchProvider::setName(const QString &name)
{
if (KUriFilterSearchProvider::name() == name) {
return;
}
KUriFilterSearchProvider::setName(name);
}
void SearchProvider::setQuery(const QString &query)
{
if (m_query == query) {
return;
}
m_query = query;
}
void SearchProvider::setKeys(const QStringList &keys)
{
if (KUriFilterSearchProvider::keys() == keys) {
return;
}
KUriFilterSearchProvider::setKeys(keys);
QString name = desktopEntryName();
if (!name.isEmpty()) {
return;
}
// New provider. Set the desktopEntryName.
// Take the longest search shortcut as filename,
// if such a file already exists, append a number and increase it
// until the name is unique
for (const QString &key : keys) {
if (key.length() > name.length()) {
// We should avoid hidden files and directory paths, BUG: 407944
name = key.toLower().remove(QLatin1Char('.')).remove(QLatin1Char('/'));
;
}
}
const QString path = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QLatin1String("/kf6/searchproviders/");
bool firstRun = true;
while (true) {
QString check(name);
if (!firstRun) {
check += KRandom::randomString(4);
}
const QString located =
QStandardPaths::locate(QStandardPaths::GenericDataLocation, QLatin1String("kf6/searchproviders/") + check + QLatin1String(".desktop"));
if (located.isEmpty()) {
name = check;
break;
} else if (located.startsWith(path)) {
// If it's a deleted (hidden) entry, overwrite it
if (KService(located).isDeleted()) {
break;
}
}
firstRun = false;
}
setDesktopEntryName(name);
}
void SearchProvider::setCharset(const QString &charset)
{
if (m_charset == charset) {
return;
}
m_charset = charset;
}
QString SearchProvider::iconName() const
{
if (!m_iconName.isEmpty()) {
return m_iconName;
}
return KIO::iconNameForUrl(QUrl(m_query));
}
void SearchProvider::setDirty(bool dirty)
{
m_dirty = dirty;
}
|