filter.cpp 7.99 KB
Newer Older
1
2
/******************************************************************************
 *
3
 *  SPDX-FileCopyrightText: 2008 Szymon Tomasz Stefanek <pragma@kvirc.net>
4
 *
5
 *  SPDX-License-Identifier: GPL-2.0-or-later
6
7
8
9
10
 *
 *******************************************************************************/

#include "core/filter.h"
#include "core/messageitem.h"
11

12
13
#include <AkonadiSearch/PIM/emailquery.h>
#include <AkonadiSearch/PIM/resultiterator.h>
14
#include <KRandom>
15
16
17
18
19
using namespace MessageList::Core;

Filter::Filter(QObject *parent)
    : QObject(parent)
{
20
    generateRandomIdentifier();
21
22
23
24
25
}

bool Filter::containString(const QString &searchInString) const
{
    bool found = false;
Laurent Montel's avatar
Laurent Montel committed
26
    for (const QString &str : qAsConst(mSearchList)) {
27
28
29
30
31
32
33
34
35
36
        if (searchInString.contains(str, Qt::CaseInsensitive)) {
            found = true;
        } else {
            found = false;
            break;
        }
    }
    return found;
}

Laurent Montel's avatar
Laurent Montel committed
37
38
39
40
41
void Filter::setOptions(const QuickSearchLine::SearchOptions &newOptions)
{
    mOptions = newOptions;
}

42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
const QString &Filter::filterName() const
{
    return mFilterName;
}

void Filter::setFilterName(const QString &newFilterName)
{
    mFilterName = newFilterName;
}

void Filter::setIdentifier(const QString &newIdentifier)
{
    mIdentifier = newIdentifier;
}

57
58
59
bool Filter::match(const MessageItem *item) const
{
    if (!mStatus.isEmpty()) {
Laurent Montel's avatar
Laurent Montel committed
60
        for (Akonadi::MessageStatus status : qAsConst(mStatus)) {
61
62
63
64
65
66
67
68
69
70
            if (!(status & item->status())) {
                return false;
            }
        }
    }

    if (!mSearchString.isEmpty()) {
        if (mMatchingItemIds.contains(item->itemId())) {
            return true;
        }
71

72
        bool searchMatches = false;
Laurent Montel's avatar
Laurent Montel committed
73
        bool searchEveryWhere = (mOptions & QuickSearchLine::SearchEveryWhere);
Laurent Montel's avatar
Laurent Montel committed
74
        if (containString(item->subject()) && ((mOptions & QuickSearchLine::SearchAgainstSubject) || searchEveryWhere)) {
75
            searchMatches = true;
Laurent Montel's avatar
Laurent Montel committed
76
        } else if (containString(item->sender()) && ((mOptions & QuickSearchLine::SearchAgainstFrom) || searchEveryWhere)) {
77
            searchMatches = true;
Laurent Montel's avatar
Laurent Montel committed
78
        } else if (containString(item->receiver()) && ((mOptions & QuickSearchLine::SearchAgainstTo) || searchEveryWhere)) {
79
80
81
82
83
84
85
86
            searchMatches = true;
        }
        if (!searchMatches) {
            return false;
        }
    }

    if (!mTagId.isEmpty()) {
Laurent Montel's avatar
Laurent Montel committed
87
        // mTagId is a Akonadi::Tag::url
Laurent Montel's avatar
Laurent Montel committed
88
        const bool tagMatches = item->findTag(mTagId) != nullptr;
89
90
91
92
93
94
95
96
        if (!tagMatches) {
            return false;
        }
    }

    return true;
}

Laurent Montel's avatar
Laurent Montel committed
97
98
99
100
101
102
103
104
105
106
QVector<Akonadi::MessageStatus> Filter::status() const
{
    return mStatus;
}

void Filter::setStatus(const QVector<Akonadi::MessageStatus> &lstStatus)
{
    mStatus = lstStatus;
}

107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
bool Filter::isEmpty() const
{
    if (!mStatus.isEmpty()) {
        return false;
    }

    if (!mSearchString.isEmpty()) {
        return false;
    }

    if (!mTagId.isEmpty()) {
        return false;
    }

    return true;
}

void Filter::clear()
{
    mStatus.clear();
    mSearchString.clear();
    mTagId.clear();
    mMatchingItemIds.clear();
    mSearchList.clear();
}

void Filter::setCurrentFolder(const Akonadi::Collection &folder)
{
    mCurrentFolder = folder;
}

Laurent Montel's avatar
Laurent Montel committed
138
139
140
141
142
const QString &Filter::searchString() const
{
    return mSearchString;
}

143
144
145
146
147
QuickSearchLine::SearchOptions Filter::currentOptions() const
{
    return mOptions;
}

Laurent Montel's avatar
Laurent Montel committed
148
149
150
151
152
153
154
void Filter::save(const KSharedConfig::Ptr &config, const QString &filtername)
{
    KConfigGroup grp(config, "General");
    int numberFilter = grp.readEntry("NumberFilter").toInt();
    KConfigGroup newGroup(config, QStringLiteral("Filter_%1").arg(numberFilter++));
    newGroup.writeEntry("name", filtername);
    newGroup.writeEntry("searchString", mSearchString);
Laurent Montel's avatar
Laurent Montel committed
155
    newGroup.writeEntry("searchOptions", static_cast<int>(mOptions));
Laurent Montel's avatar
Laurent Montel committed
156
    newGroup.writeEntry("tagId", mTagId);
157
    newGroup.writeEntry("identifier", mIdentifier);
Laurent Montel's avatar
Laurent Montel committed
158
159
160
161
162
163
    QList<qint32> lst;
    lst.reserve(mStatus.count());
    for (const auto s : qAsConst(mStatus)) {
        lst << s.toQInt32();
    }
    newGroup.writeEntry("status", lst);
Laurent Montel's avatar
Laurent Montel committed
164
    newGroup.sync();
Laurent Montel's avatar
Laurent Montel committed
165
166
167
168
169
170
171
172
173
174
    grp.writeEntry("NumberFilter", numberFilter);
    grp.sync();
}

Filter *Filter::load(const KSharedConfig::Ptr &config, int filternumber)
{
    KConfigGroup grp(config, "General");
    int numberFilter = grp.readEntry("NumberFilter").toInt();
    if (filternumber < numberFilter) {
        KConfigGroup newGroup(config, QStringLiteral("Filter_%1").arg(filternumber));
175
        return loadFromConfigGroup(newGroup);
Laurent Montel's avatar
Laurent Montel committed
176
177
    }
    return nullptr;
Laurent Montel's avatar
Laurent Montel committed
178
179
}

180
181
182
183
184
185
186
Filter *Filter::loadFromConfigGroup(const KConfigGroup &newGroup)
{
    auto filter = new Filter();
    filter->setSearchString(newGroup.readEntry("searchString"), static_cast<QuickSearchLine::SearchOptions>(newGroup.readEntry("searchOptions").toInt()));
    filter->setTagId(newGroup.readEntry("tagId"));
    filter->setIdentifier(newGroup.readEntry("identifier"));
    filter->setFilterName(newGroup.readEntry("name"));
Laurent Montel's avatar
Laurent Montel committed
187
188
189
190
191
192
193
194
195
196
    QList<qint32> lst;
    lst = newGroup.readEntry("status", QList<qint32>());
    QVector<Akonadi::MessageStatus> messageStatusLst;
    messageStatusLst.reserve(lst.count());
    for (const auto s : qAsConst(lst)) {
        Akonadi::MessageStatus status;
        status.fromQInt32(s);
        messageStatusLst << status;
    }
    filter->setStatus(messageStatusLst);
197
198
199
200
    filter->setOptions(static_cast<QuickSearchLine::SearchOptions>(newGroup.readEntry("searchOptions").toInt()));
    return filter;
}

201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
void Filter::setSearchString(const QString &search, QuickSearchLine::SearchOptions options)
{
    const QString trimStr = search.trimmed();
    if ((mSearchString == trimStr) && (mOptions == options)) {
        return;
    }
    mOptions = options;
    mSearchString = trimStr;
    mMatchingItemIds.clear();

    if (mSearchString.isEmpty()) {
        return;
    }
    bool needToSplitString = false;
    QString newStr = mSearchString;
    if (mSearchString.startsWith(QLatin1Char('"')) && mSearchString.startsWith(QLatin1Char('"'))) {
217
218
        newStr.remove(0, 1);
        newStr.remove(newStr.length() - 1, 1);
219
220
        mSearchList = QStringList() << newStr;
    } else {
221
        const QStringList searchListTmp = mSearchString.split(QLatin1Char(' '), Qt::SkipEmptyParts);
222
223
        mSearchList.clear();
        newStr.clear();
Laurent Montel's avatar
Laurent Montel committed
224
        for (const QString &text : searchListTmp) {
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
            if (text.size() >= 3) {
                mSearchList << text;
                if (!newStr.isEmpty()) {
                    newStr += QLatin1Char(' ');
                }
                newStr += text;
            }
        }
        needToSplitString = true;
    }
    if (!newStr.trimmed().isEmpty()) {
        Akonadi::Search::PIM::EmailQuery query;
        if (options & QuickSearchLine::SearchEveryWhere) {
            query.matches(newStr);
            query.setSplitSearchMatchString(needToSplitString);
        } else if (options & QuickSearchLine::SearchAgainstSubject) {
            query.subjectMatches(newStr);
        } else if (options & QuickSearchLine::SearchAgainstBody) {
            query.bodyMatches(newStr);
        } else if (options & QuickSearchLine::SearchAgainstFrom) {
            query.setFrom(newStr);
        } else if (options & QuickSearchLine::SearchAgainstBcc) {
            query.setBcc(QStringList() << newStr);
        } else if (options & QuickSearchLine::SearchAgainstTo) {
            query.setTo(QStringList() << newStr);
        }

Laurent Montel's avatar
Laurent Montel committed
252
        // If the collection is virtual we're probably trying to filter the search collection, so we just search globally
253
254
255
256
257
258
259
260
261
262
263
        if (mCurrentFolder.isValid() && !mCurrentFolder.isVirtual()) {
            query.addCollection(mCurrentFolder.id());
        }

        Akonadi::Search::PIM::ResultIterator it = query.exec();
        while (it.next()) {
            mMatchingItemIds << it.id();
        }
    }
    Q_EMIT finished();
}
Laurent Montel's avatar
Laurent Montel committed
264
265
266
267
268
269
270
271
272
273

const QString &Filter::tagId() const
{
    return mTagId;
}

void Filter::setTagId(const QString &tagId)
{
    mTagId = tagId;
}
274
275
276
277
278
279
280
281
282
283

void Filter::generateRandomIdentifier()
{
    mIdentifier = KRandom::randomString(16);
}

QString Filter::identifier() const
{
    return mIdentifier;
}