| 1 | /* Copyright (C) 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 "MazDBListModel.h" |
| 19 | #include "MazDB.h" |
| 20 | #include <QDebug> |
| 21 | #include <QString> |
| 22 | #include <QStringList> |
| 23 | #include <qqmlinfo.h> |
| 24 | #include "QJsEngineShim.h" |
| 25 | |
| 26 | MazDBListModel::MazDBListModel(QObject *parent) : QAbstractListModel(parent), |
| 27 | m_db(Q_NULLPTR), m_filterPolicy(Contains) |
| 28 | { |
| 29 | m_db = new MazDB(this); |
| 30 | connect(m_db, &MazDB::keyValueChanged, this, &MazDBListModel::handleKeyValueChange); |
| 31 | connect(m_db, &MazDB::filenameChanged, this, &MazDBListModel::sourceChanged); |
| 32 | connect(m_db, &MazDB::keyValueRemoved, this, &MazDBListModel::handleKeyValueRemoved); |
| 33 | } |
| 34 | |
| 35 | int MazDBListModel::count() const |
| 36 | { |
| 37 | return m_keys.size(); |
| 38 | } |
| 39 | |
| 40 | Range *MazDBListModel::range() |
| 41 | { |
| 42 | return &m_range; |
| 43 | } |
| 44 | |
| 45 | QString MazDBListModel::source() const |
| 46 | { |
| 47 | return m_db->filename(); |
| 48 | } |
| 49 | |
| 50 | QString MazDBListModel::filter() const |
| 51 | { |
| 52 | return m_filter; |
| 53 | } |
| 54 | |
| 55 | QHash<int, QByteArray> MazDBListModel::roleNames() const |
| 56 | { |
| 57 | QHash<int, QByteArray> roles; |
| 58 | roles.insert(KeyRole, QByteArrayLiteral("key")); |
| 59 | roles.insert(ValueRole, QByteArrayLiteral("value")); |
| 60 | if (!m_customRoles.isEmpty()) { |
| 61 | int i = ValueRole + 1; |
| 62 | for (const QString &role : m_customRoles) { |
| 63 | roles.insert(i, role.toUtf8()); |
| 64 | ++i; |
| 65 | } |
| 66 | } |
| 67 | return roles; |
| 68 | } |
| 69 | |
| 70 | QVariant MazDBListModel::data(const QModelIndex &index, int role) const |
| 71 | { |
| 72 | QVariant value; |
| 73 | const int row = index.row(); |
| 74 | if (row >= 0 && row < count()) { |
| 75 | QString key = m_keys.value(row); |
| 76 | switch(role) { |
| 77 | case KeyRole: |
| 78 | value = key; |
| 79 | break; |
| 80 | case ValueRole: |
| 81 | value = m_db->get(key); |
| 82 | break; |
| 83 | default: |
| 84 | QHash<int, QByteArray> roles = roleNames(); |
| 85 | if (roles.contains(role) && m_rolesCallBack.isCallable()) { |
| 86 | QJSEngine *engine = qjsEngine(this); |
| 87 | // WARNING! Close your eyes you don't want to see this!!!! |
| 88 | value = const_cast<QJSValue *>(&m_rolesCallBack)->call( |
| 89 | QJSValueList() |
| 90 | << engine->toScriptValue<QVariant>(QString::fromLatin1(roles.value(role))) |
| 91 | << QJSValue(key) |
| 92 | << engine->toScriptValue<QVariant>(m_db->get(key)) |
| 93 | ).toVariant(); |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | return value; |
| 98 | } |
| 99 | |
| 100 | int MazDBListModel::rowCount(const QModelIndex &parent) const |
| 101 | { |
| 102 | return count(); |
| 103 | } |
| 104 | |
| 105 | MazDBListModel::FilterPolicy MazDBListModel::filterPolicy() const |
| 106 | { |
| 107 | return m_filterPolicy; |
| 108 | } |
| 109 | |
| 110 | QStringList MazDBListModel::customRoles() const |
| 111 | { |
| 112 | return m_customRoles; |
| 113 | } |
| 114 | |
| 115 | QStringList MazDBListModel::excludeKeys() const |
| 116 | { |
| 117 | return m_excludeKeys; |
| 118 | } |
| 119 | |
| 120 | QJSValue MazDBListModel::rolesCallBack() const |
| 121 | { |
| 122 | return m_rolesCallBack; |
| 123 | } |
| 124 | |
| 125 | void MazDBListModel::setSource(const QString &src) |
| 126 | { |
| 127 | if (source() != src) { |
| 128 | m_keys.clear(); |
| 129 | m_db->setFilename(src); |
| 130 | load(); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | void MazDBListModel::setFilter(const QString &filter) |
| 135 | { |
| 136 | m_filter = filter; |
| 137 | emit filterChanged(); |
| 138 | reload(); |
| 139 | } |
| 140 | |
| 141 | void MazDBListModel::close() |
| 142 | { |
| 143 | m_db->close(); |
| 144 | } |
| 145 | |
| 146 | void MazDBListModel::clear() |
| 147 | { |
| 148 | beginRemoveRows(QModelIndex(), 0, rowCount() - 1); |
| 149 | m_keys.clear(); |
| 150 | endRemoveRows(); |
| 151 | } |
| 152 | |
| 153 | void MazDBListModel::handleKeyValueChange(const QString &key, const QVariant &value) |
| 154 | { |
| 155 | Q_UNUSED(value); |
| 156 | if (!m_keys.contains(key)) { |
| 157 | if (!insertAllowed(key)) { |
| 158 | return; |
| 159 | } |
| 160 | const int rows = count(); |
| 161 | beginInsertRows(QModelIndex(), rows, rows); |
| 162 | m_keys.append(key); |
| 163 | endInsertRows(); |
| 164 | emit countChanged(); |
| 165 | } else { |
| 166 | emitDataChanged(key); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | void MazDBListModel::handleKeyValueRemoved(const QString &key) |
| 171 | { |
| 172 | if (!m_keys.contains(key)) { |
| 173 | return; |
| 174 | } |
| 175 | |
| 176 | const int index = m_keys.indexOf(key); |
| 177 | beginRemoveRows(QModelIndex(), index, index); |
| 178 | m_keys.removeAt(index); |
| 179 | endRemoveRows(); |
| 180 | emit countChanged(); |
| 181 | } |
| 182 | |
| 183 | void MazDBListModel::load() |
| 184 | { |
| 185 | QStringList keys; |
| 186 | if (m_range.hasRange()) { |
| 187 | MazCallBack func = [&](QString key, QVariant value) { |
| 188 | Q_UNUSED(value); |
| 189 | if (insertAllowed(key)) { |
| 190 | keys << key; |
| 191 | } |
| 192 | return true; |
| 193 | }; |
| 194 | m_db->rangeStream(func, m_range.start(), m_range.end()); |
| 195 | } else { |
| 196 | MazKeyCallBack func = [&](QString key) { |
| 197 | if (insertAllowed(key)) { |
| 198 | keys << key; |
| 199 | } |
| 200 | return true; |
| 201 | }; |
| 202 | m_db->keyStream(func, m_filter); |
| 203 | } |
| 204 | |
| 205 | const int rows = count(); |
| 206 | beginInsertRows(QModelIndex(), rows, rows + m_keys.count() - 1); |
| 207 | m_keys.append(keys); |
| 208 | endInsertRows(); |
| 209 | emit countChanged(); |
| 210 | } |
| 211 | |
| 212 | void MazDBListModel::reload() |
| 213 | { |
| 214 | clear(); |
| 215 | load(); |
| 216 | } |
| 217 | |
| 218 | void MazDBListModel::setFilterPolicy(MazDBListModel::FilterPolicy filterPolicy) |
| 219 | { |
| 220 | if (m_filterPolicy == filterPolicy) |
| 221 | return; |
| 222 | |
| 223 | m_filterPolicy = filterPolicy; |
| 224 | emit filterPolicyChanged(filterPolicy); |
| 225 | } |
| 226 | |
| 227 | void MazDBListModel::setCustomRoles(QStringList customRoles) |
| 228 | { |
| 229 | if (m_customRoles == customRoles) |
| 230 | return; |
| 231 | |
| 232 | m_customRoles = customRoles; |
| 233 | emit customRolesChanged(customRoles); |
| 234 | } |
| 235 | |
| 236 | void MazDBListModel::setRolesCallBack(QJSValue rolesCallBack) |
| 237 | { |
| 238 | m_rolesCallBack = rolesCallBack; |
| 239 | emit rolesCallBackChanged(rolesCallBack); |
| 240 | } |
| 241 | |
| 242 | void MazDBListModel::setExcludeKeys(QStringList keys) |
| 243 | { |
| 244 | if (m_excludeKeys != keys) { |
| 245 | m_excludeKeys = keys; |
| 246 | emit excludeKeysChanged(); |
| 247 | reload(); |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | void MazDBListModel::emitDataChanged(const QString &key) |
| 252 | { |
| 253 | QModelIndex idx = index(m_keys.indexOf(key)); |
| 254 | if (idx.isValid()) { |
| 255 | emit dataChanged(idx, idx); |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | bool MazDBListModel::insertAllowed(const QString &key) |
| 260 | { |
| 261 | if (m_excludeKeys.contains(key)) { |
| 262 | return false; |
| 263 | } |
| 264 | if (m_filter.isEmpty()) { |
| 265 | return true; |
| 266 | } |
| 267 | switch(m_filterPolicy) { |
| 268 | case ExactMatch: |
| 269 | if (m_filter == key) { |
| 270 | return true; |
| 271 | } |
| 272 | break; |
| 273 | case Contains: |
| 274 | if (key.contains(m_filter)) { |
| 275 | return true; |
| 276 | } |
| 277 | break; |
| 278 | }; |
| 279 | return false; |
| 280 | } |
| 281 |