File indexing completed on 2024-04-28 03:55:43
0001 /* 0002 SPDX-FileCopyrightText: 2000 Malte Starostik <malte@kde.org> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "searchprovider.h" 0008 0009 #include <KConfigGroup> 0010 #include <KDesktopFile> 0011 #include <KIO/Global> // KIO::iconNameForUrl 0012 #include <KRandom> 0013 #include <KService> 0014 #include <QFileInfo> 0015 #include <QStandardPaths> 0016 0017 SearchProvider::SearchProvider(const QString &servicePath) 0018 : m_dirty(false) 0019 { 0020 setDesktopEntryName(QFileInfo(servicePath).baseName()); 0021 KDesktopFile parser(servicePath); 0022 setName(parser.readName()); 0023 KConfigGroup group(parser.desktopGroup()); 0024 setKeys(group.readEntry("Keys", QStringList())); 0025 0026 m_query = group.readEntry("Query"); 0027 m_charset = group.readEntry("Charset"); 0028 m_iconName = group.readEntry("Icon"); 0029 m_isHidden = group.readEntry("Hidden", false); 0030 } 0031 0032 SearchProvider::~SearchProvider() 0033 { 0034 } 0035 0036 void SearchProvider::setName(const QString &name) 0037 { 0038 if (KUriFilterSearchProvider::name() == name) { 0039 return; 0040 } 0041 0042 KUriFilterSearchProvider::setName(name); 0043 } 0044 0045 void SearchProvider::setQuery(const QString &query) 0046 { 0047 if (m_query == query) { 0048 return; 0049 } 0050 0051 m_query = query; 0052 } 0053 0054 void SearchProvider::setKeys(const QStringList &keys) 0055 { 0056 if (KUriFilterSearchProvider::keys() == keys) { 0057 return; 0058 } 0059 0060 KUriFilterSearchProvider::setKeys(keys); 0061 0062 QString name = desktopEntryName(); 0063 if (!name.isEmpty()) { 0064 return; 0065 } 0066 0067 // New provider. Set the desktopEntryName. 0068 // Take the longest search shortcut as filename, 0069 // if such a file already exists, append a number and increase it 0070 // until the name is unique 0071 for (const QString &key : keys) { 0072 if (key.length() > name.length()) { 0073 // We should avoid hidden files and directory paths, BUG: 407944 0074 name = key.toLower().remove(QLatin1Char('.')).remove(QLatin1Char('/')); 0075 ; 0076 } 0077 } 0078 0079 const QString path = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QLatin1String("/kf6/searchproviders/"); 0080 bool firstRun = true; 0081 0082 while (true) { 0083 QString check(name); 0084 0085 if (!firstRun) { 0086 check += KRandom::randomString(4); 0087 } 0088 0089 const QString located = 0090 QStandardPaths::locate(QStandardPaths::GenericDataLocation, QLatin1String("kf6/searchproviders/") + check + QLatin1String(".desktop")); 0091 if (located.isEmpty()) { 0092 name = check; 0093 break; 0094 } else if (located.startsWith(path)) { 0095 // If it's a deleted (hidden) entry, overwrite it 0096 if (KService(located).isDeleted()) { 0097 break; 0098 } 0099 } 0100 firstRun = false; 0101 } 0102 0103 setDesktopEntryName(name); 0104 } 0105 0106 void SearchProvider::setCharset(const QString &charset) 0107 { 0108 if (m_charset == charset) { 0109 return; 0110 } 0111 0112 m_charset = charset; 0113 } 0114 0115 QString SearchProvider::iconName() const 0116 { 0117 if (!m_iconName.isEmpty()) { 0118 return m_iconName; 0119 } 0120 0121 return KIO::iconNameForUrl(QUrl(m_query)); 0122 } 0123 0124 void SearchProvider::setDirty(bool dirty) 0125 { 0126 m_dirty = dirty; 0127 }