diff --git a/src/notesmodel.cpp b/src/notesmodel.cpp index afc04ef..df0d530 100644 --- a/src/notesmodel.cpp +++ b/src/notesmodel.cpp @@ -150,7 +150,7 @@ void NotesModel::setAccount(const QString& account) { //qDebug() << account << m_dir.path(); } } - +/* const QList NotesModel::noteIds() { QList ids; if (m_fileDir.exists() && !account().isEmpty()) { @@ -178,8 +178,8 @@ bool NotesModel::noteExists(const int id) { int NotesModel::noteModified(const int id) { return Note::modified(QJsonObject::fromVariantMap(getNoteById(id))); } - -const QVariantMap NotesModel::getNoteById(const int id) const { +*/ +const QVariantMap NotesModel::note(const int id) const { QVariantMap json; QFileInfo fileinfo(m_fileDir, QString("%1.%2").arg(id).arg(m_fileSuffix)); QFile file(fileinfo.filePath()); @@ -198,6 +198,28 @@ const QVariantMap NotesModel::getNoteById(const int id) const { return json; } +bool NotesModel::setNote(const QVariantMap ¬e, int id) const { + bool ok; + if (id < 0) { + id = note.value(m_roleNames[IdRole]).toInt(&ok); + } + else { + ok = true; + } + if (id >= 0 && ok) { + ok = false; + QFileInfo fileinfo(m_fileDir, QString("%1.%2").arg(id).arg(m_fileSuffix)); + QFile file(fileinfo.filePath()); + if (file.open(QIODevice::ReadWrite | QIODevice::Truncate | QIODevice::Text)) { + QByteArray data = QJsonDocument(QJsonObject::fromVariantMap(note)).toJson(); + if (file.write(data) == data.size()) { + ok = true; + } + } + } + return ok; +} + bool NotesModel::getAllNotes(const QStringList &exclude) { bool success = true; if (mp_notesApi) @@ -352,44 +374,60 @@ Qt::ItemFlags NotesModel::flags(const QModelIndex &index) const { } int NotesModel::rowCount(const QModelIndex &parent) const { - return m_notes.size(); + if (parent.column() == 0 && m_fileDir.exists() && !account().isEmpty()) { + return static_cast(m_fileDir.count()); + } + else { + return 0; + } } -QVariant NotesModel::data(const QModelIndex &index, int role) const { - //qDebug(); - QVariant data; - if (index.isValid() && index.row() <= m_notes.size()) { - QMap::const_iterator i = m_notes.cbegin(); - i += index.row(); - data = i.value()[(m_roleNames[role])]; - } - return data; +QVariant NotesModel::data(const QModelIndex &index, int role) { + if (role == ModifiedStringRole) + return itemData(index).value(role); } bool NotesModel::setData(const QModelIndex &index, const QVariant &value, int role) { - //qDebug(); - if (index.isValid() && index.row() <= m_notes.size()) { - QMap::iterator i = m_notes.begin(); - i += index.row(); - i.value()[m_roleNames[role]] = QJsonValue::fromVariant(value); - emit dataChanged(index, index, QVector( role )); - return true; - } - return false; + return setItemData(index, QMap{ { role, value } } ); } -QMap NotesModel::itemData(const QModelIndex &index) const { - //qDebug(); +QMap NotesModel::itemData(const QModelIndex &index) { QMap map; - if (index.isValid() && index.row() <= m_notes.size()) { - for (int role = IdRole; role < NoneRole; ++role) { - map.insert(role, data(index, role)); + if (index.isValid() && index.row() < m_files.size()) { + QMap::iterator i = m_files.begin(); + i += index.row(); + if (i.value().isReadable()) { + QJsonObject json = QJsonDocument::fromJson(i.value().readAll()).object(); + for (int role = IdRole; role <= ErrorMessageRole; ++role) { + map.insert(role, json.value(m_roleNames[role])); + } + } + else { + qDebug() << "File not readable: " << i.value().fileName(); } } return map; } bool NotesModel::setItemData(const QModelIndex &index, const QMap &roles) { + if (index.isValid() && index.row() < m_files.size()) { + QMap::iterator i = m_files.begin(); + i += index.row(); + if (i.value().isReadable() && i.value().isWritable()) { + QJsonObject json = QJsonDocument::fromJson(i.value().readAll()).object(); + QMapIterator i(roles); + while (i.hasNext()) { + i.next(); + json.insert(m_roleNames[i.key()], QJsonValue::fromVariant(i.value())); + } + + } + } + else { + qDebug() << "File not writable: " << i.value().fileName(); + } + + //qDebug(); bool retval = true; QMapIterator role(roles); diff --git a/src/notesmodel.h b/src/notesmodel.h index 325ec15..7bdb6fa 100644 --- a/src/notesmodel.h +++ b/src/notesmodel.h @@ -3,6 +3,7 @@ #include #include +#include #include #include #include "note.h" @@ -67,12 +68,12 @@ public: Qt::ItemFlags flags(const QModelIndex &index) const; virtual int rowCount(const QModelIndex &parent = QModelIndex()) const; - virtual QVariant data(const QModelIndex &index, int role) const; + virtual QVariant data(const QModelIndex &index, int role); virtual QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const; virtual bool setData(const QModelIndex &index, const QVariant &value, int role); virtual bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex()); virtual bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()); - QMap itemData(const QModelIndex &index) const; + QMap itemData(const QModelIndex &index); virtual bool setItemData(const QModelIndex &index, const QMap &roles); void setNotesApi(NotesApi* notesApi); @@ -81,7 +82,8 @@ public: QString account() const; void setAccount(const QString& account); - Q_INVOKABLE const QVariantMap getNoteById(const int id) const; + Q_INVOKABLE const QVariantMap note(const int id) const; + Q_INVOKABLE bool setNote(const QVariantMap& note, int id = -1) const; public slots: Q_INVOKABLE bool getAllNotes(const QStringList& exclude = QStringList());