notes.cpp 5.19 KB
Newer Older
Camilo higuita's avatar
Camilo higuita committed
1
#include "notes.h"
2
#include "syncer.h"
3

Camilo higuita's avatar
Camilo higuita committed
4 5
#ifdef STATIC_MAUIKIT
#include "tagging.h"
6
#include "fm.h"
Camilo higuita's avatar
Camilo higuita committed
7 8
#else
#include <MauiKit/tagging.h>
9
#include <MauiKit/fm.h>
Camilo higuita's avatar
Camilo higuita committed
10 11
#endif

12
Notes::Notes(QObject *parent) : MauiList(parent),
13
    syncer(new Syncer(this))
Camilo higuita's avatar
Camilo higuita committed
14
{
15
    qDebug()<< "CREATING NOTES LIST";
16 17 18 19
    this->sortList();

    connect(this, &Notes::sortByChanged, this, &Notes::sortList);
    connect(this, &Notes::orderChanged, this, &Notes::sortList);
Camilo higuita's avatar
Camilo higuita committed
20 21
}

22
void Notes::sortList()
Camilo higuita's avatar
Camilo higuita committed
23
{
camilo higuita's avatar
camilo higuita committed
24
    emit this->preListChanged();  
25 26 27
//    this->notes = this->db->getDBData(QString("select * from notes ORDER BY %1 %2").arg(
//                                          FMH::MODEL_NAME[static_cast<FMH::MODEL_KEY>(this->sort)],
//                                      this->order == ORDER::ASC ? "asc" : "desc"));
28
    emit this->postListChanged();
Camilo higuita's avatar
Camilo higuita committed
29 30
}

31
FMH::MODEL_LIST Notes::items() const
Camilo higuita's avatar
Camilo higuita committed
32
{
33
    return this->notes;
Camilo higuita's avatar
Camilo higuita committed
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
void Notes::setSortBy(const Notes::SORTBY &sort)
{
    if(this->sort == sort)
        return;

    this->sort = sort;
    emit this->sortByChanged();
}

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

void Notes::setOrder(const Notes::ORDER &order)
{
    if(this->order == order)
        return;

    this->order = order;
    emit this->orderChanged();
}

Notes::ORDER Notes::getOrder() const
{
    return this->order;
}

64
bool Notes::insert(const QVariantMap &note)
Camilo higuita's avatar
Camilo higuita committed
65
{
66 67
    emit this->preItemAppended();

68 69 70
    auto __note = FM::toModel(note);
    __note[FMH::MODEL_KEY::MODIFIED] = QDateTime::currentDateTime().toString(Qt::TextDate);
    __note[FMH::MODEL_KEY::ADDDATE] = QDateTime::currentDateTime().toString(Qt::TextDate);
Camilo higuita's avatar
Camilo higuita committed
71

72
    this->syncer->insertNote(__note);
73

74
    this->notes << __note;
75

76 77
    emit this->postItemAppended();
    return true;
Camilo higuita's avatar
Camilo higuita committed
78 79
}

80
bool Notes::update(const int &index, const QVariant &value, const int &role)
Camilo higuita's avatar
Camilo higuita committed
81 82 83 84
{
    if(index < 0 || index >= notes.size())
        return false;

85
    const auto oldValue = this->notes[index][static_cast<FMH::MODEL_KEY>(role)];
86 87

    if(oldValue == value.toString())
Camilo higuita's avatar
Camilo higuita committed
88 89
        return false;

90
    this->notes[index].insert(static_cast<FMH::MODEL_KEY>(role), value.toString());
Camilo higuita's avatar
Camilo higuita committed
91

92
    this->update(this->notes[index]);
Camilo higuita's avatar
Camilo higuita committed
93 94 95 96

    return true;
}

97 98 99 100 101 102 103
bool Notes::update(const QVariantMap &data, const int &index)
{
    if(index < 0 || index >= this->notes.size())
        return false;

    auto newData = this->notes[index];
    QVector<int> roles;
Camilo higuita's avatar
Camilo higuita committed
104

105
    for(auto key : data.keys())
106
        if(newData[FMH::MODEL_NAME_KEY[key]] != data[key].toString())
107
        {
108 109
            newData.insert(FMH::MODEL_NAME_KEY[key], data[key].toString());
            roles << FMH::MODEL_NAME_KEY[key];
110 111 112 113 114 115 116 117 118 119 120 121 122
        }

    this->notes[index] = newData;

    if(this->update(newData))
    {
        emit this->updateModel(index, roles);
        return true;
    }

    return false;
}

123
bool Notes::update(const FMH::MODEL &note)
Camilo higuita's avatar
Camilo higuita committed
124
{
125 126 127 128 129 130 131 132
    auto id = note[FMH::MODEL_KEY::ID];
    auto title = note[FMH::MODEL_KEY::TITLE];
    auto content = note[FMH::MODEL_KEY::CONTENT];
    auto color = note[FMH::MODEL_KEY::COLOR];
    auto pin = note[FMH::MODEL_KEY::PIN].toInt();
    auto favorite = note[FMH::MODEL_KEY::FAVORITE].toInt();
    auto tags = note[FMH::MODEL_KEY::TAG].split(",", QString::SkipEmptyParts);
    auto modified =note[FMH::MODEL_KEY::MODIFIED];
Camilo higuita's avatar
Camilo higuita committed
133 134 135

    QVariantMap note_map =
    {
136 137 138 139 140 141
        {FMH::MODEL_NAME[FMH::MODEL_KEY::TITLE], title},
        {FMH::MODEL_NAME[FMH::MODEL_KEY::CONTENT], content},
        {FMH::MODEL_NAME[FMH::MODEL_KEY::COLOR], color},
        {FMH::MODEL_NAME[FMH::MODEL_KEY::PIN], pin},
        {FMH::MODEL_NAME[FMH::MODEL_KEY::FAVORITE], favorite},
        {FMH::MODEL_NAME[FMH::MODEL_KEY::MODIFIED], modified}
Camilo higuita's avatar
Camilo higuita committed
142 143
    };

144 145
//    for(auto tg : tags)
//        this->tag->tagAbstract(tg, OWL::TABLEMAP[OWL::TABLE::NOTES], id, color);
Camilo higuita's avatar
Camilo higuita committed
146

147 148
//    return this->db->update(OWL::TABLEMAP[OWL::TABLE::NOTES], note_map, {{FMH::MODEL_NAME[FMH::MODEL_KEY::ID], id}} );
    return false;
Camilo higuita's avatar
Camilo higuita committed
149 150
}

151
bool Notes::remove(const int &index)
Camilo higuita's avatar
Camilo higuita committed
152
{
153
    emit this->preItemRemoved(index);
154 155
    auto id = this->notes.at(index)[FMH::MODEL_KEY::ID];
    QVariantMap note = {{FMH::MODEL_NAME[FMH::MODEL_KEY::ID], id}};
156

157 158 159 160 161 162
//    if(this->db->remove(OWL::TABLEMAP[OWL::TABLE::NOTES], note))
//    {
//        this->notes.removeAt(index);
//        emit this->postItemRemoved();
//        return true;
//    }
163

164
    return false;
Camilo higuita's avatar
Camilo higuita committed
165 166
}

camilo higuita's avatar
camilo higuita committed
167 168
void Notes::setAccount(const QVariantMap &account)
{
169 170 171 172 173
//    this->m_account = account;
//    const auto data = FM::toModel(this->m_account);
//    syncer->setCredentials(data[FMH::MODEL_KEY::USER], data[FMH::MODEL_KEY::PASSWORD], QUrl(data[FMH::MODEL_KEY::SERVER]).host());
//    syncer->getNotes();
//    emit accountChanged();
camilo higuita's avatar
camilo higuita committed
174 175 176 177 178 179 180
}

QVariantMap Notes::getAccount() const
{
    return this->m_account;
}

181
QVariantList Notes::getTags(const int &index)
Camilo higuita's avatar
Camilo higuita committed
182
{
183 184
//    if(index < 0 || index >= this->notes.size())
//        return QVariantList();
185

186 187 188
//    auto id = this->notes.at(index)[FMH::MODEL_KEY::ID];
//    return this->tag->getAbstractTags(OWL::TABLEMAP[OWL::TABLE::NOTES], id);
    return QVariantList();
Camilo higuita's avatar
Camilo higuita committed
189
}
190 191 192 193 194 195 196 197 198 199

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

    QVariantMap res;
    const auto note = this->notes.at(index);

    for(auto key : note.keys())
200
        res.insert(FMH::MODEL_NAME[key], note[key]);
201 202 203

    return res;
}