Commit ee76c7d7 authored by camilo higuita's avatar camilo higuita

notes syncing, download part now working, still missing update, remove and...

notes syncing, download part now working, still missing update, remove and insert. now using mauikit basemodel
parent 067efb3f
......@@ -42,8 +42,6 @@ set(buho_SRCS
src/utils/htmlparser.cpp
src/models/notes/notes.cpp
src/models/links/links.cpp
src/models/basemodel.cpp
src/models/baselist.cpp
src/syncing/nextnote.cpp
)
......@@ -55,8 +53,6 @@ set(buho_HDRS
src/utils/htmlparser.h
src/models/notes/notes.h
src/models/links/links.h
src/models/basemodel.h
src/models/baselist.h
src/syncing/nextnote.h
src/syncing/abstractnotessyncer.h
)
......
......@@ -23,9 +23,6 @@
#include "./src/buho.h"
#include "./src/linker.h"
#include "./src/models/basemodel.h"
#include "./src/models/baselist.h"
#include "./src/models/notes/notes.h"
#include "./src/models/links/links.h"
......@@ -62,16 +59,10 @@ int main(int argc, char *argv[])
context->setContextProperty("owl", &owl);
Linker linker;
context->setContextProperty("linker", &linker);
qmlRegisterUncreatableMetaObject(OWL::staticMetaObject, "OWL", 1, 0, "KEY", "Error");
qmlRegisterUncreatableType<BaseList>("BaseList", 1, 0, "BaseList", QStringLiteral("BaseList should not be created in QML"));
qmlRegisterType<BaseModel>("BuhoModel", 1, 0, "BuhoModel");
context->setContextProperty("linker", &linker);
qmlRegisterType<Notes>("Notes", 1, 0, "Notes");
qmlRegisterType<Links>("Links", 1, 0, "Links");
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
......
......@@ -41,7 +41,7 @@ void DB::init()
collectionDBPath_dir.mkpath(".");
this->name = QUuid::createUuid().toString();
if(!OWL::fileExists(OWL::CollectionDBPath + OWL::DBName))
if(!FMH::fileExists(OWL::CollectionDBPath + OWL::DBName))
{
this->openDB(this->name);
qDebug()<<"Collection doesn't exists, trying to create it" << OWL::CollectionDBPath + OWL::DBName;
......@@ -153,9 +153,9 @@ bool DB::checkExistance(const QString &tableName, const QString &searchId, const
return false;
}
OWL::DB_LIST DB::getDBData(const QString &queryTxt)
FMH::MODEL_LIST DB::getDBData(const QString &queryTxt)
{
OWL::DB_LIST mapList;
FMH::MODEL_LIST mapList;
auto query = this->getQuery(queryTxt);
......@@ -163,10 +163,10 @@ OWL::DB_LIST DB::getDBData(const QString &queryTxt)
{
while(query.next())
{
OWL::DB data;
for(auto key : OWL::KEYMAP.keys())
if(query.record().indexOf(OWL::KEYMAP[key])>-1)
data.insert(key, query.value(OWL::KEYMAP[key]).toString());
FMH::MODEL data;
for(auto key : FMH::MODEL_NAME.keys())
if(query.record().indexOf(FMH::MODEL_NAME[key])>-1)
data.insert(key, query.value(FMH::MODEL_NAME[key]).toString());
mapList<< data;
}
......
......@@ -36,6 +36,12 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <QVariantMap>
#include "../utils/owl.h"
#ifdef STATIC_MAUIKIT
#include "fmh.h"
#else
#include <MauiKit/fmh.h>
#endif
class DB : public QObject
{
......@@ -53,7 +59,7 @@ public:
static DB *getInstance();
/* utils*/
bool checkExistance(const QString &tableName, const QString &searchId, const QString &search);
OWL::DB_LIST getDBData(const QString &queryTxt);
FMH::MODEL_LIST getDBData(const QString &queryTxt);
QSqlQuery getQuery(const QString &queryTxt);
bool insert(const QString &tableName, const QVariantMap &insertData);
......
......@@ -2,19 +2,19 @@
CREATE TABLE IF NOT EXISTS NOTES (
id TEXT PRIMARY KEY,
title TEXT,
body TEXT,
content TEXT,
color TEXT,
fav INT,
favorite INT,
pin INT,
addDate DATE,
updated DATE
adddate DATE,
modified DATE
);
CREATE TABLE IF NOT EXISTS BOOKS (
url TEXT PRIMARY KEY,
title TEXT NOT NULL,
fav INTEGER NOT NULL,
addDate DATE
favorite INTEGER NOT NULL,
adddate DATE
);
CREATE TABLE IF NOT EXISTS LINKS (
......@@ -23,8 +23,8 @@ url TEXT,
title TEXT,
preview TEXT,
color TEXT,
fav INT,
favorite INT,
pin INT,
addDate DATE,
updated DATE
adddate DATE,
modified DATE
);
#include "linker.h"
#include "owl.h"
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QUrl>
#include <QEventLoop>
#include <QObject>
#ifdef STATIC_MAUIKIT
#include "fmh.h"
#else
#include <MauiKit/fmh.h>
#endif
Linker::Linker(QObject *parent) : QObject(parent)
{
......@@ -72,14 +79,12 @@ void Linker::extract(const QString &url)
else continue;
}
LINK link_data {{OWL::KEYMAP[OWL::KEY::TITLE], title.trimmed()},
{OWL::KEYMAP[OWL::KEY::BODY], data},
{OWL::KEYMAP[OWL::KEY::IMAGE], imgs}};
LINK link_data {{FMH::MODEL_NAME[FMH::MODEL_KEY::TITLE], title.trimmed()},
{FMH::MODEL_NAME[FMH::MODEL_KEY::CONTENT], data},
{FMH::MODEL_NAME[FMH::MODEL_KEY::IMG], imgs}};
emit previewReady(link_data);
}
QStringList Linker::query(const QByteArray &array, const HtmlTag &tag, const QString &attribute)
{
QStringList res;
......
#include "baselist.h"
BaseList::BaseList(QObject *parent) : QObject(parent)
{
}
#ifndef BASELIST_H
#define BASELIST_H
#include <QObject>
#include "owl.h"
class BaseList : public QObject
{
Q_OBJECT
Q_PROPERTY(OWL::KEY sortBy READ getSortBy WRITE setSortBy NOTIFY sortByChanged)
Q_PROPERTY(ORDER order READ getOrder WRITE setOrder NOTIFY orderChanged)
public:
explicit BaseList(QObject *parent = nullptr);
enum ORDER : uint8_t
{
DESC,
ASC
};
Q_ENUM(ORDER)
Q_ENUM(OWL::KEY)
//* To be overrided *//
virtual OWL::DB_LIST items() const {return OWL::DB_LIST({{}});}
virtual void setSortBy(const OWL::KEY &sort)
{
if(this->sort == sort)
return;
this->sort = sort;
emit this->sortByChanged();
}
virtual OWL::KEY getSortBy() const
{
return this->sort;
}
virtual void setOrder(const ORDER &order)
{
if(this->order == order)
return;
this->order = order;
emit this->orderChanged();
}
virtual ORDER getOrder() const
{
return this->order;
}
protected:
OWL::KEY sort = OWL::KEY::UPDATED;
ORDER order = ORDER::DESC;
signals:
void orderChanged();
void sortByChanged();
void preItemAppended();
void postItemAppended();
void preItemRemoved(int index);
void postItemRemoved();
void updateModel(int index, QVector<int> roles);
void preListChanged();
void postListChanged();
public slots:
virtual QVariantMap get(const int &index) const
{
Q_UNUSED(index);
return QVariantMap();
}
virtual bool update(const int &index, const QVariant &value, const int &role)
{
Q_UNUSED(index);
Q_UNUSED(value);
Q_UNUSED(role);
return false;
}
virtual bool update(const QVariantMap &data, const int &index)
{
Q_UNUSED(index);
Q_UNUSED(data);
return false;
}
virtual bool update(const OWL::DB &data)
{
Q_UNUSED(data);
return false;
}
virtual bool insert(const QVariantMap &map)
{
Q_UNUSED(map);
return false;
}
virtual bool remove(const int &index)
{
Q_UNUSED(index);
return false;
}
};
#endif // BASELIST_H
#include "basemodel.h"
#include "baselist.h"
#include "notes/notes.h"
BaseModel::BaseModel(QObject *parent)
: QAbstractListModel(parent),
mList(nullptr)
{}
int BaseModel::rowCount(const QModelIndex &parent) const
{
if (parent.isValid() || !mList)
return 0;
return mList->items().size();
}
QVariant BaseModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid() || !mList)
return QVariant();
return mList->items().at(index.row())[static_cast<OWL::KEY>(role)];
}
bool BaseModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (!mList)
return false;
if (mList->update(index.row(), value, role))
{
emit dataChanged(index, index, QVector<int>() << role);
return true;
}
return false;
}
Qt::ItemFlags BaseModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
return Qt::NoItemFlags;
return Qt::ItemIsEditable; // FIXME: Implement me!
}
QHash<int, QByteArray> BaseModel::roleNames() const
{
QHash<int, QByteArray> names;
for(auto key : OWL::KEYMAP.keys())
names[key] = QString(OWL::KEYMAP[key]).toUtf8();
return names;
}
BaseList *BaseModel::getList() const
{
return this->mList;
}
void BaseModel::setList(BaseList *value)
{
beginResetModel();
if(mList)
mList->disconnect(this);
mList = value;
if(mList)
{
connect(this->mList, &BaseList::preItemAppended, this, [=]()
{
const int index = mList->items().size();
beginInsertRows(QModelIndex(), index, index);
});
connect(this->mList, &BaseList::postItemAppended, this, [=]()
{
endInsertRows();
});
connect(this->mList, &BaseList::preItemRemoved, this, [=](int index)
{
beginRemoveRows(QModelIndex(), index, index);
});
connect(this->mList, &BaseList::postItemRemoved, this, [=]()
{
endRemoveRows();
});
connect(this->mList, &BaseList::updateModel, this, [=](int index, QVector<int> roles)
{
emit this->dataChanged(this->index(index), this->index(index), roles);
});
connect(this->mList, &BaseList::preListChanged, this, [=]()
{
beginResetModel();
});
connect(this->mList, &BaseList::postListChanged, this, [=]()
{
endResetModel();
});
}
endResetModel();
}
#ifndef BASEMODEL_H
#define BASEMODEL_H
#include <QAbstractListModel>
#include <QList>
#include "owl.h"
class Notes;
class BaseList;
class BaseModel : public QAbstractListModel
{
Q_OBJECT
Q_PROPERTY(BaseList *list READ getList WRITE setList)
public:
explicit BaseModel(QObject *parent = nullptr);
// Basic functionality:
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
// Editable:
bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
Qt::ItemFlags flags(const QModelIndex& index) const override;
virtual QHash<int, QByteArray> roleNames() const override;
BaseList* getList() const;
void setList(BaseList *value);
private:
BaseList *mList;
signals:
void listChanged();
};
#endif // NOTESMODEL_H
......@@ -10,7 +10,7 @@
#include <MauiKit/tagging.h>
#endif
Links::Links(QObject *parent) : BaseList(parent)
Links::Links(QObject *parent) : MauiList(parent)
{
this->db = DB::getInstance();
this->tag = Tagging::getInstance();
......@@ -24,7 +24,7 @@ void Links::sortList()
{
emit this->preListChanged();
this->links = this->db->getDBData(QString("select * from links ORDER BY %1 %2").arg(
OWL::KEYMAP[this->sort],
FMH::MODEL_NAME[static_cast<FMH::MODEL_KEY>(this->sort)],
this->order == ORDER::ASC ? "asc" : "desc"));
emit this->postListChanged();
}
......@@ -38,40 +38,68 @@ QVariantMap Links::get(const int &index) const
const auto note = this->links.at(index);
for(auto key : note.keys())
res.insert(OWL::KEYMAP[key], note[key]);
res.insert(FMH::MODEL_NAME[key], note[key]);
return res;
}
OWL::DB_LIST Links::items() const
FMH::MODEL_LIST Links::items() const
{
return this->links;
}
void Links::setSortBy(const Links::SORTBY &sort)
{
if(this->sort == sort)
return;
this->sort = sort;
emit this->sortByChanged();
}
Links::SORTBY Links::getSortBy() const
{
return this->sort;
}
void Links::setOrder(const Links::ORDER &order)
{
if(this->order == order)
return;
this->order = order;
emit this->orderChanged();
}
Links::ORDER Links::getOrder() const
{
return this->order;
}
bool Links::insert(const QVariantMap &link)
{
emit this->preItemAppended();
auto url = link[OWL::KEYMAP[OWL::KEY::LINK]].toString();
auto color = link[OWL::KEYMAP[OWL::KEY::COLOR]].toString();
auto pin = link[OWL::KEYMAP[OWL::KEY::PIN]].toInt();
auto fav = link[OWL::KEYMAP[OWL::KEY::FAV]].toInt();
auto tags = link[OWL::KEYMAP[OWL::KEY::TAG]].toStringList();
auto preview = link[OWL::KEYMAP[OWL::KEY::PREVIEW]].toString();
auto title = link[OWL::KEYMAP[OWL::KEY::TITLE]].toString();
auto url = link[FMH::MODEL_NAME[FMH::MODEL_KEY::LINK]].toString();
auto color = link[FMH::MODEL_NAME[FMH::MODEL_KEY::COLOR]].toString();
auto pin = link[FMH::MODEL_NAME[FMH::MODEL_KEY::PIN]].toInt();
auto fav = link[FMH::MODEL_NAME[FMH::MODEL_KEY::FAV]].toInt();
auto tags = link[FMH::MODEL_NAME[FMH::MODEL_KEY::TAG]].toStringList();
auto preview = link[FMH::MODEL_NAME[FMH::MODEL_KEY::PREVIEW]].toString();
auto title = link[FMH::MODEL_NAME[FMH::MODEL_KEY::TITLE]].toString();
auto image_path = OWL::saveImage(Linker::getUrl(preview), OWL::LinksPath+QUuid::createUuid().toString());
QVariantMap link_map =
{
{OWL::KEYMAP[OWL::KEY::LINK], url},
{OWL::KEYMAP[OWL::KEY::TITLE], title},
{OWL::KEYMAP[OWL::KEY::PIN], pin},
{OWL::KEYMAP[OWL::KEY::FAV], fav},
{OWL::KEYMAP[OWL::KEY::PREVIEW], image_path},
{OWL::KEYMAP[OWL::KEY::COLOR], color},
{OWL::KEYMAP[OWL::KEY::ADD_DATE], QDateTime::currentDateTime().toString()},
{OWL::KEYMAP[OWL::KEY::UPDATED], QDateTime::currentDateTime().toString()}
{FMH::MODEL_NAME[FMH::MODEL_KEY::LINK], url},
{FMH::MODEL_NAME[FMH::MODEL_KEY::TITLE], title},
{FMH::MODEL_NAME[FMH::MODEL_KEY::PIN], pin},
{FMH::MODEL_NAME[FMH::MODEL_KEY::FAV], fav},
{FMH::MODEL_NAME[FMH::MODEL_KEY::PREVIEW], image_path},
{FMH::MODEL_NAME[FMH::MODEL_KEY::COLOR], color},
{FMH::MODEL_NAME[FMH::MODEL_KEY::ADDDATE], QDateTime::currentDateTime().toString()},
{FMH::MODEL_NAME[FMH::MODEL_KEY::MODIFIED], QDateTime::currentDateTime().toString()}
};
......@@ -81,16 +109,16 @@ bool Links::insert(const QVariantMap &link)
this->tag->tagAbstract(tg, OWL::TABLEMAP[OWL::TABLE::LINKS], url, color);
this->links << OWL::DB
this->links << FMH::MODEL
({
{OWL::KEY::LINK, url},
{OWL::KEY::TITLE, title},
{OWL::KEY::COLOR, color},
{OWL::KEY::PREVIEW, image_path},
{OWL::KEY::PIN, QString::number(pin)},
{OWL::KEY::FAV, QString::number(fav)},
{OWL::KEY::UPDATED, QDateTime::currentDateTime().toString()},
{OWL::KEY::ADD_DATE, QDateTime::currentDateTime().toString()}
{FMH::MODEL_KEY::LINK, url},
{FMH::MODEL_KEY::TITLE, title},
{FMH::MODEL_KEY::COLOR, color},
{FMH::MODEL_KEY::PREVIEW, image_path},
{FMH::MODEL_KEY::PIN, QString::number(pin)},
{FMH::MODEL_KEY::FAV, QString::number(fav)},
{FMH::MODEL_KEY::MODIFIED, QDateTime::currentDateTime().toString()},
{FMH::MODEL_KEY::ADDDATE, QDateTime::currentDateTime().toString()}
});
......@@ -107,14 +135,14 @@ bool Links::update(const int &index, const QVariant &value, const int &role)
if(index < 0 || index >= links.size())
return false;
const auto oldValue = this->links[index][static_cast<OWL::KEY>(role)];
const auto oldValue = this->links[index][static_cast<FMH::MODEL_KEY>(role)];
if(oldValue == value.toString())
return false;
qDebug()<< "VALUE TO UPDATE"<< OWL::KEYMAP[static_cast<OWL::KEY>(role)] << oldValue;
qDebug()<< "VALUE TO UPDATE"<< FMH::MODEL_NAME[static_cast<FMH::MODEL_KEY>(role)] << oldValue;
this->links[index].insert(static_cast<OWL::KEY>(role), value.toString());
this->links[index].insert(static_cast<FMH::MODEL_KEY>(role), value.toString());
this->update(this->links[index]);
......@@ -130,10 +158,10 @@ bool Links::update(const QVariantMap &data, const int &index)
QVector<int> roles;
for(auto key : data.keys())
if(newData[OWL::MAPKEY[key]] != data[key].toString())
if(newData[FMH::MODEL_NAME_KEY[key]] != data[key].toString())
{
newData.insert(OWL::MAPKEY[key], data[key].toString());
roles << OWL::MAPKEY[key];
newData.insert(FMH::MODEL_NAME_KEY[key], data[key].toString());
roles << FMH::MODEL_NAME_KEY[key];
}
this->links[index] = newData;
......@@ -149,35 +177,35 @@ bool Links::update(const QVariantMap &data, const int &index)
return false;
}
bool Links::update(const OWL::DB &link)
bool Links::update(const FMH::MODEL &link)
{
auto url = link[OWL::KEY::LINK];
auto color = link[OWL::KEY::COLOR];
auto pin = link[OWL::KEY::PIN].toInt();
auto fav = link[OWL::KEY::FAV].toInt();
auto tags = link[OWL::KEY::TAG].split(",", QString::SkipEmptyParts);
auto updated = link[OWL::KEY::UPDATED];
auto url = link[FMH::MODEL_KEY::LINK];
auto color = link[FMH::MODEL_KEY::COLOR];
auto pin = link[FMH::MODEL_KEY::PIN].toInt();
auto fav = link[FMH::MODEL_KEY::FAV].toInt();
auto tags = link[FMH::MODEL_KEY::TAG].split(",", QString::SkipEmptyParts);
auto updated = link[FMH::MODEL_KEY::MODIFIED];
QVariantMap link_map =
{
{OWL::KEYMAP[OWL::KEY::COLOR], color},
{OWL::KEYMAP[OWL::KEY::PIN], pin},
{OWL::KEYMAP[OWL::KEY::FAV], fav},
{OWL::KEYMAP[OWL::KEY::UPDATED], updated}
{FMH::MODEL_NAME[FMH::MODEL_KEY::COLOR], color},
{FMH::MODEL_NAME[FMH::MODEL_KEY::PIN], pin},
{FMH::MODEL_NAME[FMH::MODEL_KEY::FAV], fav},
{FMH::MODEL_NAME[FMH::MODEL_KEY::MODIFIED], updated}
};
for(auto tg : tags)
this->tag->tagAbstract(tg, OWL::TABLEMAP[OWL::TABLE::LINKS], url, color);
return this->db->update(OWL::TABLEMAP[OWL::TABLE::LINKS], link_map, {{OWL::KEYMAP[OWL::KEY::LINK], url}} );
return this->db->update(OWL::TABLEMAP[OWL::TABLE::LINKS], link_map, {{FMH::MODEL_NAME[FMH::MODEL_KEY::LINK], url}} );
}
bool Links::remove(const int &index)
{
emit this->preItemRemoved(index);
auto linkUrl = this->links.at(index)[OWL::KEY::LINK];
QVariantMap link = {{OWL::KEYMAP[OWL::KEY::LINK], linkUrl}};
auto linkUrl = this->links.at(index)[FMH::MODEL_KEY::LINK];
QVariantMap link = {{FMH::MODEL_NAME[FMH::MODEL_KEY::LINK], linkUrl}};
if(this->db->remove(OWL::TABLEMAP[OWL::TABLE::LINKS], link))
{
......@@ -194,7 +222,7 @@ QVariantList Links::getTags(const int &index)
if(index < 0 || index >= this->links.size())
return QVariantList();
auto link = this->links.at(index)[OWL::KEY::LINK];
auto link = this->links.at(index)[FMH::MODEL_KEY::LINK];
return this->tag->getAbstractTags(OWL::TABLEMAP[OWL::TABLE::LINKS], link);
}
......@@ -2,34 +2,71 @@
#define LINKS_H
#include <QObject>
#include "./../baselist.h"
#ifdef STATIC_MAUIKIT
#include "fmh.h"
#include "mauimodel.h"
#else
#include <MauiKit/fmh.h>
#include <MauiKit/mauilist.h>
#endif
class DB;
class Tagging;
class Links : public BaseList
class Links : public MauiList
{
Q_OBJECT
Q_PROPERTY(SORTBY sortBy READ getSortBy WRITE setSortBy NOTIFY sortByChanged)
Q_PROPERTY(ORDER order READ getOrder WRITE setOrder NOTIFY orderChanged)
public:
enum ORDER : uint8_t
{
DESC,
ASC
};
Q_ENUM(ORDER)
enum SORTBY : uint8_t
{
TITLE = FMH::MODEL_KEY::TITLE,
ADDDATE = FMH::MODEL_KEY::ADDDATE,
MODIFIED = FMH::MODEL_KEY::MODIFIED,
COLOR = FMH::MODEL_KEY::COLOR,
FAVORITE = FMH::MODEL_KEY::FAVORITE,
PIN = FMH::MODEL_KEY::PIN
};
Q_ENUM(SORTBY)
explicit Links(QObject *parent = nullptr);
OWL::DB_LIST items() const override;
FMH::MODEL_LIST items() const override final;
void setSortBy(const SORTBY &sort);
SORTBY getSortBy(