contactsmodel.cpp 4.89 KB
Newer Older
Camilo higuita's avatar
Camilo higuita committed
1
#include "contactsmodel.h"
Camilo higuita's avatar
Camilo higuita committed
2
#include "./src/interfaces/synchroniser.h"
Camilo higuita's avatar
Camilo higuita committed
3 4 5

ContactsModel::ContactsModel(QObject *parent) : BaseList(parent)
{
Camilo higuita's avatar
Camilo higuita committed
6
    this->syncer = new Synchroniser(this);
Camilo higuita's avatar
Camilo higuita committed
7
    connect(this, &ContactsModel::queryChanged, this, &ContactsModel::setList);
8
    this->setList();
Camilo higuita's avatar
Camilo higuita committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
}

FMH::MODEL_LIST ContactsModel::items() const
{
    return this->list;
}

void ContactsModel::setQuery(const QString &query)
{
    if(this->query == query)
        return;

    this->query = query;
    qDebug()<< "setting query"<< this->query;

    emit this->queryChanged();
}

QString ContactsModel::getQuery() const
{
    return this->query;
}

void ContactsModel::setSortBy(const SORTBY &sort)
{
    if(this->sort == sort)
        return;

    this->sort = sort;

    this->preListChanged();
    this->sortList();
    this->postListChanged();
    emit this->sortByChanged();
}

ContactsModel::SORTBY ContactsModel::getSortBy() const
{
    return this->sort;
}

void ContactsModel::sortList()
{
    if(this->sort == ContactsModel::SORTBY::NONE)
        return;

    const auto key = static_cast<FMH::MODEL_KEY>(this->sort);
    qSort(this->list.begin(), this->list.end(), [key](const FMH::MODEL &e1, const FMH::MODEL &e2) -> bool
    {
        auto role = key;

        switch(role)
        {
        case FMH::MODEL_KEY::FAV:
        {
            if(e1[role].toInt() > e2[role].toInt())
                return true;
            break;
        }

        case FMH::MODEL_KEY::ADDDATE:
70
        case FMH::MODEL_KEY::MODIFIED:
Camilo higuita's avatar
Camilo higuita committed
71 72 73 74 75 76 77 78 79 80 81 82 83
        {
            auto currentTime = QDateTime::currentDateTime();

            auto date1 = QDateTime::fromString(e1[role], Qt::TextDate);
            auto date2 = QDateTime::fromString(e2[role], Qt::TextDate);

            if(date1.secsTo(currentTime) <  date2.secsTo(currentTime))
                return true;

            break;
        }

        case FMH::MODEL_KEY::TITLE:
84 85 86 87 88 89
        case FMH::MODEL_KEY::N:
        case FMH::MODEL_KEY::TEL:
        case FMH::MODEL_KEY::ORG:
        case FMH::MODEL_KEY::EMAIL:
        case FMH::MODEL_KEY::GENDER:
        case FMH::MODEL_KEY::ADR:
Camilo higuita's avatar
Camilo higuita committed
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
        {
            const auto str1 = QString(e1[role]).toLower();
            const auto str2 = QString(e2[role]).toLower();

            if(str1 < str2)
                return true;
            break;
        }

        default:
            if(e1[role] < e2[role])
                return true;
        }

        return false;
    });
}

void ContactsModel::setList()
{
    emit this->preListChanged();

112
    this->list = this->syncer->getContacts(this->query);
Camilo higuita's avatar
Camilo higuita committed
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
    this->sortList();
    emit this->postListChanged();
}

QVariantMap ContactsModel::get(const int &index) const
{
    if(index >= this->list.size() || index < 0)
        return QVariantMap();

    QVariantMap res;
    const auto item = this->list.at(index);

    for(auto key : item.keys())
        res.insert(FMH::MODEL_NAME[key], item[key]);

    return res;
}

131 132
bool ContactsModel::insert(const QVariantMap &map)
{
Camilo higuita's avatar
Camilo higuita committed
133 134 135 136 137 138 139 140 141 142 143 144
    if(map.isEmpty())
        return false;

    auto model = FM::toModel(map);
    if(!this->syncer->insertContact(model))
        return false;

    emit this->preListChanged();
    this->setList();
    emit this->postListChanged();

    return true;
145 146
}

147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
bool ContactsModel::update(const QVariantMap &map, const int &index)
{
    if(index >= this->list.size() || index < 0)
        return false;

    const auto newItem = FM::toModel(map);
    const auto oldItem = this->list[index];

    if(this->syncer->updateContact(newItem))
    {
        QVector<int> roles;

        for(auto key : newItem.keys())
            if(newItem[key] != oldItem[key])
                roles << key;

        this->list[index] = newItem;
        emit this->updateModel(index, roles);

        return true;
    }

    return false;
}

Camilo higuita's avatar
Camilo higuita committed
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
void ContactsModel::append(const QVariantMap &item)
{
    if(item.isEmpty())
        return;

    emit this->preItemAppended();

    FMH::MODEL model;
    for(auto key : item.keys())
        model.insert(FMH::MODEL_NAME_KEY[key], item[key].toString());

    qDebug() << "Appending item to list" << item;
    this->list << model;

    qDebug()<< this->list;

    emit this->postItemAppended();
}

void ContactsModel::append(const QVariantMap &item, const int &at)
{
    if(item.isEmpty())
        return;

    if(at > this->list.size() || at < 0)
        return;

    qDebug()<< "trying to append at" << at << item["title"];

    emit this->preItemAppendedAt(at);

    FMH::MODEL model;
    for(auto key : item.keys())
        model.insert(FMH::MODEL_NAME_KEY[key], item[key].toString());

    this->list.insert(at, model);

    emit this->postItemAppended();
}

void ContactsModel::appendQuery(const QString &query)
{
    if(query.isEmpty() || query == this->query)
        return;

    this->query = query;

    emit this->preListChanged();

    emit this->postListChanged();
}


void ContactsModel::clear()
{
    emit this->preListChanged();

    this->list.clear();

    emit this->postListChanged();
}