Add folder support
This commit is contained in:
83
src/models/folder.cpp
Normal file
83
src/models/folder.cpp
Normal file
@@ -0,0 +1,83 @@
|
||||
#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;
|
||||
}
|
||||
|
||||
33
src/models/folder.h
Normal file
33
src/models/folder.h
Normal file
@@ -0,0 +1,33 @@
|
||||
#ifndef FOLDER_H
|
||||
#define FOLDER_H
|
||||
|
||||
#include <QString>
|
||||
#include <QJsonObject>
|
||||
#include <QJsonArray>
|
||||
|
||||
#include "note.h"
|
||||
|
||||
|
||||
class Folder
|
||||
{
|
||||
private:
|
||||
QString uuid;
|
||||
QString name;
|
||||
QVector<Note*> notes;
|
||||
|
||||
public:
|
||||
Folder(QString name);
|
||||
Folder(QJsonObject obj);
|
||||
~Folder();
|
||||
|
||||
QString getName();
|
||||
void setName(QString name);
|
||||
QString getUUID();
|
||||
QVector<Note*> getNotes();
|
||||
void append(Note *n);
|
||||
int remove(QString uuid);
|
||||
|
||||
QJsonObject toJson();
|
||||
};
|
||||
|
||||
#endif // FOLDER_H
|
||||
@@ -1,9 +1,20 @@
|
||||
#include "note.h"
|
||||
|
||||
Note::Note()
|
||||
Note::Note(QString name)
|
||||
{
|
||||
QUuid uid = QUuid::createUuid();
|
||||
this->uuid = uid.toString(QUuid::StringFormat::WithoutBraces);
|
||||
this->title = name;
|
||||
}
|
||||
|
||||
Note::Note(QJsonObject obj)
|
||||
{
|
||||
QUuid uid = QUuid::createUuid();
|
||||
QString newUuid = uid.toString(QUuid::StringFormat::WithoutBraces);
|
||||
|
||||
this->uuid = obj["uuid"].toString(newUuid);
|
||||
this->title = obj["title"].toString("<untitled note>");
|
||||
this->content = obj["content"].toString();
|
||||
}
|
||||
|
||||
QJsonObject Note::toJson()
|
||||
@@ -14,18 +25,7 @@ QJsonObject Note::toJson()
|
||||
o["content"] = this->content;
|
||||
return o;
|
||||
}
|
||||
|
||||
Note *Note::fromJson(QJsonObject o)
|
||||
{
|
||||
Note *n = new Note();
|
||||
n->uuid = o["uuid"].toString(n->uuid);
|
||||
n->title = o["title"].toString();
|
||||
n->content = o["content"].toString();
|
||||
n->encrypted = o["encrypted"].toBool(false);
|
||||
return n;
|
||||
}
|
||||
|
||||
QString Note::getUuid()
|
||||
QString Note::getUUID()
|
||||
{
|
||||
return uuid;
|
||||
}
|
||||
@@ -37,11 +37,7 @@ QString Note::getTitle()
|
||||
|
||||
QString Note::getContent()
|
||||
{
|
||||
if (!this->encrypted)
|
||||
{
|
||||
return content;
|
||||
}
|
||||
return "";
|
||||
return content;
|
||||
}
|
||||
void Note::setTitle(QString value)
|
||||
{
|
||||
@@ -50,7 +46,5 @@ void Note::setTitle(QString value)
|
||||
|
||||
void Note::setContent(QString value)
|
||||
{
|
||||
if (!this->encrypted) {
|
||||
this->content = value;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,12 +14,13 @@
|
||||
class Note
|
||||
{
|
||||
public:
|
||||
Note();
|
||||
Note(QString name);
|
||||
Note(QJsonObject obj);
|
||||
|
||||
|
||||
QJsonObject toJson();
|
||||
static Note* fromJson(QJsonObject o);
|
||||
|
||||
QString getUuid();
|
||||
QString getUUID();
|
||||
QString getTitle();
|
||||
QString getContent();
|
||||
|
||||
@@ -29,7 +30,6 @@ private:
|
||||
QString uuid;
|
||||
QString title;
|
||||
QString content;
|
||||
bool encrypted;
|
||||
};
|
||||
|
||||
#endif // NOTE_H
|
||||
|
||||
Reference in New Issue
Block a user