| 1 | /* Copyright (C) 2016 - 2017 Dan Chapman <[email protected]> |
| 2 | |
| 3 | This file is part of Dekko email client for Ubuntu devices |
| 4 | |
| 5 | This program is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU General Public License as |
| 7 | published by the Free Software Foundation; either version 2 of |
| 8 | the License or (at your option) version 3 |
| 9 | |
| 10 | This program is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | GNU General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU General Public License |
| 16 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | #include "MailboxSearch.h" |
| 19 | |
| 20 | MailboxSearch::MailboxSearch(QObject *parent) : QObject(parent), |
| 21 | m_results(0), m_service(0), m_location(Local), m_limit(50), m_sortOrder(Qt::DescendingOrder), |
| 22 | m_sortBy(TimeStamp), m_searchBody(false), m_globalSearch(false) |
| 23 | { |
| 24 | m_results = new QQmlObjectListModel<MinimalMessage>(this); |
| 25 | m_service = new SearchService(this); |
| 26 | connect(m_service, &SearchService::statusChanged, this, &MailboxSearch::statusChanged); |
| 27 | connect(m_service, &SearchService::messageIdsMatched, this, &MailboxSearch::handleMatchedResults); |
| 28 | connect(this, &MailboxSearch::searchQueryChanged, [=](){ |
| 29 | QueryConfiguration qc; |
| 30 | qc.key = QMailMessageKey(m_msgKey & searchKey()); |
| 31 | qc.qeury = m_searchQuery; |
| 32 | qc.sortKey = querySortKey(); |
| 33 | qc.spec = querySpec(); |
| 34 | m_service->search(qc); |
| 35 | }); |
| 36 | } |
| 37 | |
| 38 | QObject *MailboxSearch::result() const |
| 39 | { |
| 40 | return m_results; |
| 41 | } |
| 42 | |
| 43 | QVariant MailboxSearch::messageKey() const |
| 44 | { |
| 45 | return m_msgKey; |
| 46 | } |
| 47 | |
| 48 | void MailboxSearch::setMessageKey(QVariant &key) |
| 49 | { |
| 50 | if (key.canConvert<QMailMessageKey>()) { |
| 51 | m_msgKey = key.value<QMailMessageKey>(); |
| 52 | emit messageKeyChanged(); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | MailboxSearch::Location MailboxSearch::location() const |
| 57 | { |
| 58 | return m_location; |
| 59 | } |
| 60 | |
| 61 | int MailboxSearch::limit() const |
| 62 | { |
| 63 | return m_limit; |
| 64 | } |
| 65 | |
| 66 | Qt::SortOrder MailboxSearch::sortOrder() const |
| 67 | { |
| 68 | return m_sortOrder; |
| 69 | } |
| 70 | |
| 71 | MailboxSearch::SortBy MailboxSearch::sortBy() const |
| 72 | { |
| 73 | return m_sortBy; |
| 74 | } |
| 75 | |
| 76 | bool MailboxSearch::searchBody() const |
| 77 | { |
| 78 | return m_searchBody; |
| 79 | } |
| 80 | |
| 81 | QString MailboxSearch::searchQuery() const |
| 82 | { |
| 83 | return m_searchQuery; |
| 84 | } |
| 85 | |
| 86 | bool MailboxSearch::globalSearch() const |
| 87 | { |
| 88 | return m_globalSearch; |
| 89 | } |
| 90 | |
| 91 | QMailMessageSortKey MailboxSearch::querySortKey() |
| 92 | { |
| 93 | switch(m_sortBy) { |
| 94 | case Id: |
| 95 | return QMailMessageSortKey::id(m_sortOrder); |
| 96 | case Sender: |
| 97 | return QMailMessageSortKey::sender(m_sortOrder); |
| 98 | case Subject: |
| 99 | return QMailMessageSortKey::subject(m_sortOrder); |
| 100 | case Recipients: |
| 101 | return QMailMessageSortKey::recipients(m_sortOrder); |
| 102 | case TimeStamp: |
| 103 | return QMailMessageSortKey::timeStamp(m_sortOrder); |
| 104 | case ListId: |
| 105 | return QMailMessageSortKey::listId(m_sortOrder); |
| 106 | } |
| 107 | Q_UNREACHABLE(); |
| 108 | } |
| 109 | |
| 110 | QMailSearchAction::SearchSpecification MailboxSearch::querySpec() |
| 111 | { |
| 112 | switch(m_location) { |
| 113 | case LocalAndRemote: // just do local for now |
| 114 | case Local: |
| 115 | return QMailSearchAction::SearchSpecification::Local; |
| 116 | case Remote: |
| 117 | return QMailSearchAction::SearchSpecification::Remote; |
| 118 | } |
| 119 | Q_UNREACHABLE(); |
| 120 | } |
| 121 | |
| 122 | QMailMessageKey MailboxSearch::searchKey() |
| 123 | { |
| 124 | QMailMessageKey searchKey; |
| 125 | searchKey |= QMailMessageKey::sender(m_searchQuery, QMailDataComparator::Includes); |
| 126 | searchKey |= QMailMessageKey::recipients(m_searchQuery, QMailDataComparator::Includes); |
| 127 | searchKey |= QMailMessageKey::subject(m_searchQuery, QMailDataComparator::Includes); |
| 128 | if (m_searchBody) { |
| 129 | searchKey |= QMailMessageKey::preview(m_searchQuery, QMailDataComparator::Includes); |
| 130 | } |
| 131 | return searchKey; |
| 132 | } |
| 133 | |
| 134 | void MailboxSearch::search(const QString &query) |
| 135 | { |
| 136 | if (query.isEmpty()) { |
| 137 | return; |
| 138 | } |
| 139 | setSearchQuery(query); |
| 140 | } |
| 141 | |
| 142 | void MailboxSearch::cancelSearch() |
| 143 | { |
| 144 | m_service->cancel(); |
| 145 | m_results->clear(); |
| 146 | } |
| 147 | |
| 148 | void MailboxSearch::setLocation(const Location &location) |
| 149 | { |
| 150 | if (m_location == location) |
| 151 | return; |
| 152 | |
| 153 | m_location = location; |
| 154 | emit locationChanged(location); |
| 155 | } |
| 156 | |
| 157 | void MailboxSearch::setLimit(const int &limit) |
| 158 | { |
| 159 | if (m_limit == limit) |
| 160 | return; |
| 161 | |
| 162 | m_limit = limit; |
| 163 | emit limitChanged(limit); |
| 164 | } |
| 165 | |
| 166 | void MailboxSearch::setSortOrder(const Qt::SortOrder &sortOrder) |
| 167 | { |
| 168 | if (m_sortOrder == sortOrder) |
| 169 | return; |
| 170 | |
| 171 | m_sortOrder = sortOrder; |
| 172 | emit sortOrderChanged(sortOrder); |
| 173 | } |
| 174 | |
| 175 | void MailboxSearch::setSortBy(const MailboxSearch::SortBy &sortBy) |
| 176 | { |
| 177 | if (m_sortBy == sortBy) |
| 178 | return; |
| 179 | |
| 180 | m_sortBy = sortBy; |
| 181 | emit sortByChanged(sortBy); |
| 182 | } |
| 183 | |
| 184 | void MailboxSearch::setSearchBody(const bool searchBody) |
| 185 | { |
| 186 | if (m_searchBody == searchBody) |
| 187 | return; |
| 188 | |
| 189 | m_searchBody = searchBody; |
| 190 | emit searchBodyChanged(searchBody); |
| 191 | } |
| 192 | |
| 193 | void MailboxSearch::setSearchQuery(const QString &searchQuery) |
| 194 | { |
| 195 | if (m_searchQuery == searchQuery) |
| 196 | return; |
| 197 | |
| 198 | m_searchQuery = searchQuery; |
| 199 | emit searchQueryChanged(searchQuery); |
| 200 | } |
| 201 | |
| 202 | void MailboxSearch::setGlobalSearch(const bool globalSearch) |
| 203 | { |
| 204 | if (m_globalSearch == globalSearch) |
| 205 | return; |
| 206 | |
| 207 | m_globalSearch = globalSearch; |
| 208 | emit globalSearchChanged(globalSearch); |
| 209 | } |
| 210 | |
| 211 | void MailboxSearch::handleMatchedResults(const QMailMessageIdList &idList) |
| 212 | { |
| 213 | // the results are already sorted for us |
| 214 | m_results->clear(); |
| 215 | Q_FOREACH(auto &id, idList) { |
| 216 | MinimalMessage *msg = new MinimalMessage(); |
| 217 | msg->setMessageId(id); |
| 218 | m_results->append(msg); |
| 219 | } |
| 220 | } |
| 221 |