Files
WorkPad/src/models/folder.cpp
Aurélie Delhaie 7b17dfb044 Add folder support
2022-10-22 19:33:54 +02:00

84 lines
1.4 KiB
C++

#include "folder.h"
Folder::Folder(QString name)
{
QUuid uid = QUuid::createUuid();
this->uuid = uid.toString(QUuid::StringFormat::WithoutBraces);
this->name = name;
}
Folder::Folder(QJsonObject obj)
{
QUuid uid = QUuid::createUuid();
QString newUuid = uid.toString(QUuid::StringFormat::WithoutBraces);
this->uuid = obj["uuid"].toString(newUuid);
this->name = obj["name"].toString("<unamed folder>");
foreach (QJsonValue value, obj["notes"].toArray()) {
this->notes.append(new Note(value.toObject()));
}
}
Folder::~Folder()
{
foreach (Note *n, notes)
{
delete n;
}
}
QString Folder::getName()
{
return name;
}
void Folder::setName(QString name)
{
this->name = name;
}
QString Folder::getUUID()
{
return uuid;
}
QVector<Note *> Folder::getNotes()
{
return notes;
}
void Folder::append(Note *n)
{
notes.append(n);
}
int Folder::remove(QString uuid)
{
int res = 0;
int idx = -1;
for (int i = 0; i < notes.length(); i++)
{
if (notes[i]->getUUID() == uuid)
{
idx = i;
res = 1;
}
}
notes.removeAt(idx);
return res;
}
QJsonObject Folder::toJson()
{
QJsonObject folder;
QJsonArray arr;
foreach (Note *n, notes) {
arr.append(n->toJson());
}
folder["name"] = this->name;
folder["uuid"] = this->uuid;
folder["notes"] = arr;
return folder;
}