| 1 | #include "Identities.h" |
| 2 | #include <SnapStandardPaths.h> |
| 3 | #include <QVariant> |
| 4 | #include <MazDBBatch.h> |
| 5 | |
| 6 | namespace { |
| 7 | static const QString prefix = QStringLiteral("/identity/"); |
| 8 | static const QString metaKey = QStringLiteral("/meta"); |
| 9 | } |
| 10 | |
| 11 | Identities::Identities(QObject *parent) : QObject(parent), |
| 12 | m_db(Q_NULLPTR), m_defaultIdentity(0) |
| 13 | { |
| 14 | m_sourcePath = SnapStandardPaths::writableLocation(SnapStandardPaths::AppConfigLocation) + QStringLiteral("/mazdb/identities.db"); |
| 15 | emit sourcePathChanged(m_sourcePath); |
| 16 | m_db = new MazDB(m_sourcePath, this); |
| 17 | } |
| 18 | |
| 19 | bool Identities::add(const QVariantMap &map) |
| 20 | { |
| 21 | if (!map.contains(QStringLiteral("parentId"))) { |
| 22 | emit error(InvalidParent); |
| 23 | m_errorString = "Missing parent accountId"; |
| 24 | return false; |
| 25 | } |
| 26 | |
| 27 | QString parentId = map.value(QStringLiteral("parentId")).toString(); |
| 28 | |
| 29 | QVariantMap metaInfo; |
| 30 | QVariantMap identity = map; |
| 31 | |
| 32 | auto batch = m_db->batch(); |
| 33 | int count = 1; |
| 34 | if (!m_db->get(metaKey).isValid()) { |
| 35 | // this is a new account identity |
| 36 | metaInfo["count"] = count; |
| 37 | metaInfo["default"] = count; |
| 38 | emit defaultIdentityChanged(count); |
| 39 | } else { |
| 40 | metaInfo = m_db->get(metaKey).toMap(); |
| 41 | count = metaInfo.value("count").toInt(); |
| 42 | metaInfo["count"] = ++count; |
| 43 | } |
| 44 | batch->put(metaKey, metaInfo); |
| 45 | |
| 46 | identity["id"] = count; |
| 47 | |
| 48 | batch->put(key(QString::number(count)), identity); |
| 49 | bool result = batch->write(); |
| 50 | if (result) |
| 51 | emit identitiesChanged(); |
| 52 | return result; |
| 53 | } |
| 54 | |
| 55 | bool Identities::update(const QVariantMap &map) |
| 56 | { |
| 57 | if (!map.contains(QStringLiteral("parentId"))) { |
| 58 | emit error(InvalidParent); |
| 59 | m_errorString = "Missing parent accountId"; |
| 60 | return false; |
| 61 | } |
| 62 | if (!map.contains(QStringLiteral("id"))) { |
| 63 | emit error(InvalidId); |
| 64 | m_errorString = "Missing identity id"; |
| 65 | return false; |
| 66 | } |
| 67 | bool result = m_db->put(key(QString::number(map["id"].toInt())), map); |
| 68 | if (result) |
| 69 | emit identitiesChanged(); |
| 70 | return result; |
| 71 | } |
| 72 | |
| 73 | bool Identities::remove(const int &id) |
| 74 | { |
| 75 | QString k = key(QString::number(id)); |
| 76 | if (!m_db->get(k).isValid()) { |
| 77 | return false; |
| 78 | } |
| 79 | bool result = m_db->del(k); |
| 80 | if (result) |
| 81 | emit identitiesChanged(); |
| 82 | return result; |
| 83 | } |
| 84 | |
| 85 | bool Identities::remove(const QVariantMap &map) |
| 86 | { |
| 87 | return remove(map["id"].toInt()); |
| 88 | } |
| 89 | |
| 90 | bool Identities::removeAccount(const int &id) |
| 91 | { |
| 92 | auto batch = m_db->batch(); |
| 93 | MazCallBack func = [=](QString key, QVariant val) { |
| 94 | QVariantMap identity = val.toMap(); |
| 95 | if (identity["parentId"].toInt() == id) { |
| 96 | batch->del(key); |
| 97 | } |
| 98 | return true; |
| 99 | }; |
| 100 | m_db->readStream(func, prefix); |
| 101 | bool result = batch->write(); |
| 102 | if (result) |
| 103 | emit identitiesChanged(); |
| 104 | return result; |
| 105 | } |
| 106 | |
| 107 | QVariantMap Identities::get(const int &id) |
| 108 | { |
| 109 | QString k = key(QString::number(id)); |
| 110 | if (!m_db->get(k).isValid()) { |
| 111 | return QVariantMap(); |
| 112 | } |
| 113 | return m_db->get(k).toMap(); |
| 114 | } |
| 115 | |
| 116 | IdentityList Identities::getAllForAccount(const int &id) |
| 117 | { |
| 118 | QList<QVariantMap> identities; |
| 119 | MazValueCallBack func = [this, &identities, &id](QVariant val) { |
| 120 | const QVariantMap identity = val.toMap(); |
| 121 | if (identity["parentId"].toInt() == id) { |
| 122 | identities.append(identity); |
| 123 | } |
| 124 | return true; |
| 125 | }; |
| 126 | m_db->valueStream(func, prefix); |
| 127 | return identities; |
| 128 | } |
| 129 | |
| 130 | int Identities::defaultIdentity() const |
| 131 | { |
| 132 | return m_db->get(metaKey, 0).toMap().value("default").toInt(); |
| 133 | } |
| 134 | |
| 135 | int Identities::determineBestIdentityFromMessage(const QMailMessage &message) |
| 136 | { |
| 137 | |
| 138 | auto identities = getAllForAccount(message.parentAccountId().toULongLong()); |
| 139 | |
| 140 | auto identityInRecipients = [=](QMailAddressList list, const QString &match) -> bool { |
| 141 | for (const auto &address : list) { |
| 142 | if (address.address() == match) { |
| 143 | return true; |
| 144 | } |
| 145 | } |
| 146 | return false; |
| 147 | }; |
| 148 | |
| 149 | for (const auto &i : identities) { |
| 150 | const QString email = i["email"].toString(); |
| 151 | if (message.from().address() == email || // we also check "from" here because it could be a draft message |
| 152 | identityInRecipients(message.to(), email) || |
| 153 | identityInRecipients(message.cc(), email) || |
| 154 | identityInRecipients(message.bcc(), email)) { |
| 155 | return i["id"].toInt(); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | return -1; |
| 160 | } |
| 161 | |
| 162 | void Identities::setDefaultIdentity(int defaultIdentity) |
| 163 | { |
| 164 | QVariantMap meta = m_db->get(metaKey).toMap(); |
| 165 | meta["default"] = defaultIdentity; |
| 166 | m_db->putSync(metaKey, meta); |
| 167 | emit defaultIdentityChanged(defaultIdentity); |
| 168 | } |
| 169 | |
| 170 | QString Identities::key(const QString &k) |
| 171 | { |
| 172 | QString result(prefix); |
| 173 | if (k.startsWith('/')) { |
| 174 | result.append(k.mid(1)); |
| 175 | } else { |
| 176 | result.append(k); |
| 177 | } |
| 178 | return result; |
| 179 | } |