File indexing completed on 2024-11-10 04:40:28
0001 /* 0002 SPDX-FileCopyrightText: 2010 Volker Krause <vkrause@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include "persistentsearchattribute.h" 0008 #include "collection.h" 0009 0010 #include "private/imapparser_p.h" 0011 0012 #include <QString> 0013 #include <QStringList> 0014 0015 using namespace Akonadi; 0016 0017 class Akonadi::PersistentSearchAttributePrivate 0018 { 0019 public: 0020 QString queryString; 0021 QList<qint64> queryCollections; 0022 bool remote = false; 0023 bool recursive = false; 0024 }; 0025 0026 PersistentSearchAttribute::PersistentSearchAttribute() 0027 : d(new PersistentSearchAttributePrivate()) 0028 { 0029 } 0030 0031 PersistentSearchAttribute::~PersistentSearchAttribute() = default; 0032 0033 QString PersistentSearchAttribute::queryString() const 0034 { 0035 return d->queryString; 0036 } 0037 0038 void PersistentSearchAttribute::setQueryString(const QString &query) 0039 { 0040 d->queryString = query; 0041 } 0042 0043 QList<qint64> PersistentSearchAttribute::queryCollections() const 0044 { 0045 return d->queryCollections; 0046 } 0047 0048 void PersistentSearchAttribute::setQueryCollections(const QList<Collection> &collections) 0049 { 0050 d->queryCollections.clear(); 0051 d->queryCollections.reserve(collections.count()); 0052 for (const Collection &collection : collections) { 0053 d->queryCollections << collection.id(); 0054 } 0055 } 0056 0057 void PersistentSearchAttribute::setQueryCollections(const QList<qint64> &collectionsIds) 0058 { 0059 d->queryCollections = collectionsIds; 0060 } 0061 0062 bool PersistentSearchAttribute::isRecursive() const 0063 { 0064 return d->recursive; 0065 } 0066 0067 void PersistentSearchAttribute::setRecursive(bool recursive) 0068 { 0069 d->recursive = recursive; 0070 } 0071 0072 bool PersistentSearchAttribute::isRemoteSearchEnabled() const 0073 { 0074 return d->remote; 0075 } 0076 0077 void PersistentSearchAttribute::setRemoteSearchEnabled(bool enabled) 0078 { 0079 d->remote = enabled; 0080 } 0081 0082 QByteArray PersistentSearchAttribute::type() const 0083 { 0084 static const QByteArray sType("PERSISTENTSEARCH"); 0085 return sType; 0086 } 0087 0088 Attribute *PersistentSearchAttribute::clone() const 0089 { 0090 auto attr = new PersistentSearchAttribute; 0091 attr->setQueryString(queryString()); 0092 attr->setQueryCollections(queryCollections()); 0093 attr->setRecursive(isRecursive()); 0094 attr->setRemoteSearchEnabled(isRemoteSearchEnabled()); 0095 return attr; 0096 } 0097 0098 QByteArray PersistentSearchAttribute::serialized() const 0099 { 0100 QStringList cols; 0101 cols.reserve(d->queryCollections.count()); 0102 for (qint64 colId : std::as_const(d->queryCollections)) { 0103 cols << QString::number(colId); 0104 } 0105 0106 QList<QByteArray> l; 0107 // ### eventually replace with the AKONADI_PARAM_PERSISTENTSEARCH_XXX constants 0108 l.append("QUERYSTRING"); 0109 l.append(ImapParser::quote(d->queryString.toUtf8())); 0110 l.append("QUERYCOLLECTIONS"); 0111 l.append("(" + cols.join(QLatin1Char(' ')).toLatin1() + ')'); 0112 if (d->remote) { 0113 l.append("REMOTE"); 0114 } 0115 if (d->recursive) { 0116 l.append("RECURSIVE"); 0117 } 0118 return "(" + ImapParser::join(l, " ") + ')'; // krazy:exclude=doublequote_chars 0119 } 0120 0121 void PersistentSearchAttribute::deserialize(const QByteArray &data) 0122 { 0123 QList<QByteArray> l; 0124 ImapParser::parseParenthesizedList(data, l); 0125 const int listSize(l.size()); 0126 for (int i = 0; i < listSize; ++i) { 0127 const QByteArray &key = l.at(i); 0128 if (key == QByteArrayLiteral("QUERYLANGUAGE")) { 0129 // Skip the value 0130 ++i; 0131 } else if (key == QByteArrayLiteral("QUERYSTRING")) { 0132 d->queryString = QString::fromUtf8(l.at(i + 1)); 0133 ++i; 0134 } else if (key == QByteArrayLiteral("QUERYCOLLECTIONS")) { 0135 QList<QByteArray> ids; 0136 ImapParser::parseParenthesizedList(l.at(i + 1), ids); 0137 d->queryCollections.clear(); 0138 d->queryCollections.reserve(ids.count()); 0139 for (const QByteArray &id : std::as_const(ids)) { 0140 d->queryCollections << id.toLongLong(); 0141 } 0142 ++i; 0143 } else if (key == QByteArrayLiteral("REMOTE")) { 0144 d->remote = true; 0145 } else if (key == QByteArrayLiteral("RECURSIVE")) { 0146 d->recursive = true; 0147 } 0148 } 0149 }