Commit 6a8fe9b1 authored by camilo higuita's avatar camilo higuita

create booklets now working

parent bfff896b
CREATE TABLE IF NOT EXISTS NOTES ( CREATE TABLE IF NOT EXISTS NOTES (
id TEXT PRIMARY KEY, id TEXT PRIMARY KEY,
title TEXT, title TEXT,
...@@ -15,7 +14,7 @@ id TEXT, ...@@ -15,7 +14,7 @@ id TEXT,
server TEXT, server TEXT,
user TEXT, user TEXT,
stamp TEXT, stamp TEXT,
PRIMARY KEY(server, stamp) PRIMARY KEY(server, stamp),
FOREIGN KEY(id) REFERENCES NOTES(id) FOREIGN KEY(id) REFERENCES NOTES(id)
); );
...@@ -34,12 +33,11 @@ book TEXT, ...@@ -34,12 +33,11 @@ book TEXT,
url TEXT, url TEXT,
title TEXT NOT NULL, title TEXT NOT NULL,
adddate DATE, adddate DATE,
modified DATE modified DATE,
PRIMARY KEY(id, book) PRIMARY KEY(id, book),
FOREIGN KEY(book) REFERENCES BOOKS(id) FOREIGN KEY(book) REFERENCES BOOKS(id)
); );
CREATE TABLE IF NOT EXISTS LINKS ( CREATE TABLE IF NOT EXISTS LINKS (
url TEXT PRIMARY KEY, url TEXT PRIMARY KEY,
title TEXT, title TEXT,
......
...@@ -54,7 +54,7 @@ void Booklet::insert(const QVariantMap &data) ...@@ -54,7 +54,7 @@ void Booklet::insert(const QVariantMap &data)
__booklet[FMH::MODEL_KEY::MODIFIED] = QDateTime::currentDateTime().toString(Qt::TextDate); __booklet[FMH::MODEL_KEY::MODIFIED] = QDateTime::currentDateTime().toString(Qt::TextDate);
__booklet[FMH::MODEL_KEY::ADDDATE] = QDateTime::currentDateTime().toString(Qt::TextDate); __booklet[FMH::MODEL_KEY::ADDDATE] = QDateTime::currentDateTime().toString(Qt::TextDate);
this->syncer->insertBooklet(__booklet); this->syncer->insertBooklet(this->m_book, __booklet);
this->m_list << __booklet; this->m_list << __booklet;
......
...@@ -44,6 +44,16 @@ Books::ORDER Books::getOrder() const ...@@ -44,6 +44,16 @@ Books::ORDER Books::getOrder() const
return this->order; return this->order;
} }
Booklet *Books::getBooklet() const
{
return m_booklet;
}
int Books::getCurrentBook() const
{
return m_currentBook;
}
void Books::sortList() void Books::sortList()
{ {
...@@ -92,3 +102,13 @@ void Books::openBook(const int &index) ...@@ -92,3 +102,13 @@ void Books::openBook(const int &index)
return; return;
this->m_booklet->setBook(this->m_list.at(index)[FMH::MODEL_KEY::ID]); this->m_booklet->setBook(this->m_list.at(index)[FMH::MODEL_KEY::ID]);
} }
void Books::setCurrentBook(int currentBook)
{
if (m_currentBook == currentBook)
return;
m_currentBook = currentBook;
this->openBook(m_currentBook);
emit currentBookChanged(m_currentBook);
}
...@@ -21,6 +21,8 @@ class Books : public MauiList ...@@ -21,6 +21,8 @@ class Books : public MauiList
Q_PROPERTY(SORTBY sortBy READ getSortBy WRITE setSortBy NOTIFY sortByChanged) Q_PROPERTY(SORTBY sortBy READ getSortBy WRITE setSortBy NOTIFY sortByChanged)
Q_PROPERTY(ORDER order READ getOrder WRITE setOrder NOTIFY orderChanged) Q_PROPERTY(ORDER order READ getOrder WRITE setOrder NOTIFY orderChanged)
Q_PROPERTY(Booklet *booklet READ getBooklet NOTIFY bookletChanged) Q_PROPERTY(Booklet *booklet READ getBooklet NOTIFY bookletChanged)
Q_PROPERTY(int currentBook READ getCurrentBook WRITE setCurrentBook NOTIFY currentBookChanged)
public: public:
enum ORDER : uint8_t enum ORDER : uint8_t
...@@ -50,10 +52,9 @@ public: ...@@ -50,10 +52,9 @@ public:
void setOrder(const ORDER &order); void setOrder(const ORDER &order);
ORDER getOrder() const; ORDER getOrder() const;
Booklet * getBooklet() const Booklet * getBooklet() const;
{
return m_booklet; int getCurrentBook() const;
}
private: private:
Syncer *syncer; Syncer *syncer;
...@@ -62,16 +63,21 @@ private: ...@@ -62,16 +63,21 @@ private:
FMH::MODEL_LIST m_list; FMH::MODEL_LIST m_list;
void sortList(); void sortList();
void openBook(const int &index);
SORTBY sort = SORTBY::MODIFIED; SORTBY sort = SORTBY::MODIFIED;
ORDER order = ORDER::DESC; ORDER order = ORDER::DESC;
int m_currentBook;
signals: signals:
void sortByChanged(); void sortByChanged();
void orderChanged(); void orderChanged();
void bookletChanged(Booklet * booklet); void bookletChanged(Booklet * booklet);
void currentBookChanged(int currentBook);
public slots: public slots:
QVariantMap get(const int &index) const; QVariantMap get(const int &index) const;
...@@ -85,7 +91,7 @@ public slots: ...@@ -85,7 +91,7 @@ public slots:
bool update(const QVariantMap &data, const int &index); bool update(const QVariantMap &data, const int &index);
bool remove(const int &index); bool remove(const int &index);
void openBook(const int &index); void setCurrentBook(int currentBook);
}; };
#endif // BOOKS_H #endif // BOOKS_H
...@@ -121,9 +121,15 @@ void Syncer::insertBook(FMH::MODEL &book) ...@@ -121,9 +121,15 @@ void Syncer::insertBook(FMH::MODEL &book)
emit this->bookInserted(book, {STATE::TYPE::LOCAL, STATE::STATUS::OK, "Book inserted locally sucessfully"}); emit this->bookInserted(book, {STATE::TYPE::LOCAL, STATE::STATUS::OK, "Book inserted locally sucessfully"});
} }
void Syncer::insertBooklet(FMH::MODEL &booklet) void Syncer::insertBooklet(const QString &bookId, FMH::MODEL &booklet)
{ {
this->insertBookletLocal(booklet); if(!this->insertBookletLocal(bookId, booklet))
{
qWarning()<< "Could not insert Booklet, Syncer::insertBooklet";
return;
}
emit this->bookletInserted(booklet, {STATE::TYPE::LOCAL, STATE::STATUS::OK, "Booklet inserted locally sucessfully"});
} }
void Syncer::addId(FMH::MODEL &model) void Syncer::addId(FMH::MODEL &model)
...@@ -237,13 +243,24 @@ void Syncer::setConections() ...@@ -237,13 +243,24 @@ void Syncer::setConections()
} }
bool Syncer::insertNoteLocal(FMH::MODEL &note) bool Syncer::insertNoteLocal(FMH::MODEL &note)
{ {
qDebug()<<"TAGS"<< note[FMH::MODEL_KEY::TAG]; if(note.isEmpty())
{
qWarning()<< "Could not insert note locally. The note is empty";
return false;
}
Syncer::addId(note); //adds a local id to the note Syncer::addId(note); //adds a local id to the note
// create a file for the note // create a file for the note
const auto __notePath = Syncer::saveNoteFile(note); const auto __notePath = Syncer::saveNoteFile(OWL::NotesPath, note);
if(__notePath.isEmpty())
{
qWarning()<< "File could not be saved. Syncer::insertNoteLocal.";
return false;
}
note[FMH::MODEL_KEY::URL] = __notePath.toString(); note[FMH::MODEL_KEY::URL] = __notePath.toString();
qDebug()<< "note saved to <<" << __notePath; qDebug()<< "note saved to <<" << __notePath;
...@@ -279,7 +296,7 @@ bool Syncer::updateNoteLocal(const QString &id, const FMH::MODEL &note) ...@@ -279,7 +296,7 @@ bool Syncer::updateNoteLocal(const QString &id, const FMH::MODEL &note)
for(const auto &tg : note[FMH::MODEL_KEY::TAG]) for(const auto &tg : note[FMH::MODEL_KEY::TAG])
this->tag->tagAbstract(tg, OWL::TABLEMAP[OWL::TABLE::NOTES], id); this->tag->tagAbstract(tg, OWL::TABLEMAP[OWL::TABLE::NOTES], id);
this->saveNoteFile(note); this->saveNoteFile(OWL::NotesPath, note);
return this->db->update(OWL::TABLEMAP[OWL::TABLE::NOTES], return this->db->update(OWL::TABLEMAP[OWL::TABLE::NOTES],
FMH::toMap(FMH::filterModel(note, {FMH::MODEL_KEY::TITLE, FMH::toMap(FMH::filterModel(note, {FMH::MODEL_KEY::TITLE,
...@@ -360,13 +377,60 @@ void Syncer::removeBookRemote(const QString &id) ...@@ -360,13 +377,60 @@ void Syncer::removeBookRemote(const QString &id)
} }
bool Syncer::insertBookletLocal(FMH::MODEL &booklet) bool Syncer::insertBookletLocal(const QString &bookId, FMH::MODEL &booklet)
{ {
qDebug()<< "trying to insert booklet" << booklet; qDebug()<< "trying to insert booklet" << booklet;
if(bookId.isEmpty() || booklet.isEmpty())
{
qWarning()<< "Could not insert booklet. Reference to book id or booklet are empty";
return false;
}
Syncer::addId(booklet); //adds a local id to the booklet
// create a file for the note
const auto __bookTitle = [&]() -> QString {
const auto data = this->db->getDBData(QString("select title from books where id = '%1'").arg(bookId));
return data.isEmpty() ? QString() : data.first()[FMH::MODEL_KEY::TITLE]; } ();
if(__bookTitle.isEmpty() || !FMH::fileExists(QUrl::fromLocalFile(OWL::BooksPath+__bookTitle)))
{
qWarning()<< "The book does not exists in the db or the directory is missing. Syncer::insertBookletLocal";
return false;
}
const auto __bookletPath = Syncer::saveNoteFile(OWL::BooksPath+__bookTitle+"/", booklet);
if(__bookletPath.isEmpty())
{
qWarning()<< "File could not be saved. Syncer::insertBookletLocal";
return false;
}
booklet[FMH::MODEL_KEY::URL] = __bookletPath.toString();
booklet[FMH::MODEL_KEY::BOOK] = bookId;
qDebug()<< "booklet saved to <<" << __bookletPath;
auto __bookletMap = FMH::toMap(FMH::filterModel(booklet, {FMH::MODEL_KEY::ID,
FMH::MODEL_KEY::BOOK,
FMH::MODEL_KEY::TITLE,
FMH::MODEL_KEY::URL,
FMH::MODEL_KEY::MODIFIED,
FMH::MODEL_KEY::ADDDATE}));
if(this->db->insert(OWL::TABLEMAP[OWL::TABLE::BOOKLETS], __bookletMap))
{
// for(const auto &tg : booklet[FMH::MODEL_KEY::TAG].split(",", QString::SplitBehavior::SkipEmptyParts))
// this->tag->tagAbstract(tg, OWL::TABLEMAP[OWL::TABLE::NOTES], booklet[FMH::MODEL_KEY::ID], booklet[FMH::MODEL_KEY::COLOR]);
return true;
}
return false; return false;
} }
void Syncer::insertBookletRemote(FMH::MODEL &booklet) void Syncer::insertBookletRemote(const QString &bookId, FMH::MODEL &booklet)
{ {
} }
...@@ -405,17 +469,17 @@ const FMH::MODEL_LIST Syncer::collectAllBooks() ...@@ -405,17 +469,17 @@ const FMH::MODEL_LIST Syncer::collectAllBooks()
return this->db->getDBData("select * from books"); return this->db->getDBData("select * from books");
} }
const QUrl Syncer::saveNoteFile(const FMH::MODEL &note) const QUrl Syncer::saveNoteFile(const QString &dir, const FMH::MODEL &data)
{ {
if(note.isEmpty() || !note.contains(FMH::MODEL_KEY::CONTENT)) if(data.isEmpty() /*|| !data.contains(FMH::MODEL_KEY::CONTENT)*/)
{ {
qWarning() << "the note is empty, therefore it could not be saved into a file"; qWarning() << "the note is empty, therefore it could not be saved into a file";
return QUrl(); return QUrl();
} }
QFile file(OWL::NotesPath+note[FMH::MODEL_KEY::ID]+QStringLiteral(".txt")); QFile file(dir+data[FMH::MODEL_KEY::ID]+QStringLiteral(".txt"));
file.open(QFile::WriteOnly); file.open(QFile::WriteOnly);
file.write(note[FMH::MODEL_KEY::CONTENT].toUtf8()); file.write(data[FMH::MODEL_KEY::CONTENT].toUtf8());
file.close(); file.close();
return QUrl::fromLocalFile(file.fileName()); return QUrl::fromLocalFile(file.fileName());
......
...@@ -174,7 +174,7 @@ public: ...@@ -174,7 +174,7 @@ public:
* @brief insertBooklet * @brief insertBooklet
* @param booklet * @param booklet
*/ */
void insertBooklet(FMH::MODEL &booklet); void insertBooklet(const QString &bookId, FMH::MODEL &booklet);
/** /**
* @brief removeBooklet * @brief removeBooklet
...@@ -254,8 +254,8 @@ protected: ...@@ -254,8 +254,8 @@ protected:
bool removeBookLocal(const QString &id); bool removeBookLocal(const QString &id);
void removeBookRemote(const QString &id); void removeBookRemote(const QString &id);
bool insertBookletLocal(FMH::MODEL &booklet); bool insertBookletLocal(const QString &bookId, FMH::MODEL &booklet);
void insertBookletRemote(FMH::MODEL &booklet); void insertBookletRemote(const QString &bookId, FMH::MODEL &booklet);
bool updateBookletLocal(const QString &id, const FMH::MODEL &booklet); bool updateBookletLocal(const QString &id, const FMH::MODEL &booklet);
void updateBookletRemote(const QString &id, const FMH::MODEL &booklet); void updateBookletRemote(const QString &id, const FMH::MODEL &booklet);
bool removeBookletLocal(const QString &id); bool removeBookletLocal(const QString &id);
...@@ -264,7 +264,7 @@ protected: ...@@ -264,7 +264,7 @@ protected:
const FMH::MODEL_LIST collectAllNotes(); const FMH::MODEL_LIST collectAllNotes();
const FMH::MODEL_LIST collectAllBooks(); const FMH::MODEL_LIST collectAllBooks();
static inline const QUrl saveNoteFile(const FMH::MODEL &note); static inline const QUrl saveNoteFile(const QString &dir, const FMH::MODEL &data);
static inline const QString noteFileContent(const QUrl &path); static inline const QString noteFileContent(const QUrl &path);
signals: signals:
...@@ -282,6 +282,13 @@ signals: ...@@ -282,6 +282,13 @@ signals:
void bookReady(FMH::MODEL book); void bookReady(FMH::MODEL book);
void booksReady(FMH::MODEL_LIST books); void booksReady(FMH::MODEL_LIST books);
//FOR BOOKLETS
void bookletInserted(FMH::MODEL booklet, STATE state);
void bookletUpdated(FMH::MODEL booklet, STATE state);
void bookletRemoved(FMH::MODEL booklet, STATE state);
void bookletReady(FMH::MODEL booklet);
void bookletReady(FMH::MODEL_LIST booklets);
public slots: public slots:
}; };
......
...@@ -19,31 +19,20 @@ namespace OWL ...@@ -19,31 +19,20 @@ namespace OWL
enum class TABLE : uint8_t enum class TABLE : uint8_t
{ {
NOTES, NOTES,
NOTES_TAGS,
NOTES_SYNC, NOTES_SYNC,
TAGS,
BOOKS, BOOKS,
PAGES, BOOKLETS,
BOOKS_PAGES,
LINKS, LINKS,
LINKS_TAGS,
PAGES_TAGS,
NONE NONE
}; };
static const QMap<TABLE,QString> TABLEMAP = static const QMap<TABLE,QString> TABLEMAP =
{ {
{TABLE::NOTES,"notes"}, {TABLE::NOTES,"notes"},
{TABLE::NOTES_TAGS,"notes_tags"},
{TABLE::NOTES_SYNC,"notes_sync"}, {TABLE::NOTES_SYNC,"notes_sync"},
{TABLE::TAGS,"tags"},
{TABLE::BOOKS,"books"}, {TABLE::BOOKS,"books"},
{TABLE::PAGES,"pages"}, {TABLE::BOOKLETS,"booklets"},
{TABLE::BOOKS_PAGES,"books_pages"},
{TABLE::LINKS,"links"}, {TABLE::LINKS,"links"},
{TABLE::LINKS_TAGS,"links_tags"},
{TABLE::PAGES_TAGS,"pages_tags"},
{TABLE::LINKS_TAGS,"links_tags"}
}; };
// enum KEY : uint8_t // enum KEY : uint8_t
......
...@@ -42,6 +42,7 @@ StackView ...@@ -42,6 +42,7 @@ StackView
Books Books
{ {
id: _booksList id: _booksList
currentBook: cardsView.currentIndex
} }
Maui.Page Maui.Page
...@@ -157,6 +158,7 @@ StackView ...@@ -157,6 +158,7 @@ StackView
onClicked: onClicked:
{ {
console.log("BOOKLET CLICKED", index) console.log("BOOKLET CLICKED", index)
cardsView.currentIndex = index
_stackView.push(_bookletComponent) _stackView.push(_bookletComponent)
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment