mirror of
https://github.com/laurent22/joplin.git
synced 2024-12-24 10:27:10 +02:00
Update item in client
This commit is contained in:
parent
6a66c07a28
commit
28320ace18
@ -51,6 +51,8 @@ bool BaseModel::load(const QString &id) {
|
||||
bool BaseModel::save(bool trackChanges) {
|
||||
bool isNew = this->isNew();
|
||||
|
||||
qDebug() << "SAVING" << valuesToString();
|
||||
|
||||
if (!changedFields_.size() && !isNew) return true;
|
||||
|
||||
QStringList fields = changedFields();
|
||||
@ -64,7 +66,7 @@ bool BaseModel::save(bool trackChanges) {
|
||||
// If it's a new entry and the ID is a UUID, we need to create this
|
||||
// ID now. If the ID is an INT, it will be automatically set by
|
||||
// SQLite.
|
||||
if (isNew && primaryKeyIsUuid()) {
|
||||
if (isNew && primaryKeyIsUuid() && !valueIsSet(primaryKey())) {
|
||||
values[primaryKey()] = uuid::createUuid();
|
||||
}
|
||||
|
||||
@ -184,6 +186,7 @@ bool BaseModel::trackChanges() const {
|
||||
}
|
||||
|
||||
bool BaseModel::isNew() const {
|
||||
qDebug() << "EEEEEEEEEEEEEEEEEEEEEE" << isNew_ << primaryKey() << valueIsSet(primaryKey()) << values_["id"].toString();
|
||||
if (isNew_ == 0) return false;
|
||||
if (isNew_ == 1) return true;
|
||||
return !valueIsSet(primaryKey());
|
||||
@ -259,10 +262,8 @@ void BaseModel::loadSqlQuery(const QSqlQuery &query) {
|
||||
}
|
||||
|
||||
if (field.type == QMetaType::QString) {
|
||||
//values_.insert(field.name, Value(query.value(idx).toString()));
|
||||
setValue(field.name, query.value(idx).toString());
|
||||
} else if (field.type == QMetaType::Int) {
|
||||
//values_.insert(field.name, Value(query.value(idx).toInt()));
|
||||
setValue(field.name, query.value(idx).toInt());
|
||||
} else {
|
||||
qCritical() << "Unsupported value type" << field.name;
|
||||
@ -284,20 +285,21 @@ void BaseModel::loadJsonObject(const QJsonObject &jsonObject) {
|
||||
QVector<BaseModel::Field> fields = BaseModel::tableFields(table());
|
||||
|
||||
foreach (BaseModel::Field field, fields) {
|
||||
if (field.type == QMetaType::QString) {
|
||||
//values_.insert(field.name, Value(jsonObject[field.name].toString()));
|
||||
setValue(field.name, jsonObject[field.name].toString());
|
||||
} else if (field.type == QMetaType::Int) {
|
||||
//values_.insert(field.name, Value(jsonObject[field.name].toInt()));
|
||||
setValue(field.name, jsonObject[field.name].toInt());
|
||||
} else {
|
||||
qCritical() << "Unsupported value type" << field.name;
|
||||
}
|
||||
setValue(field.name, jsonObject[field.name], field.type);
|
||||
}
|
||||
|
||||
isNew_ = 1;
|
||||
}
|
||||
|
||||
void BaseModel::patchJsonObject(const QJsonObject &jsonObject) {
|
||||
QVector<BaseModel::Field> fields = BaseModel::tableFields(table());
|
||||
|
||||
foreach (BaseModel::Field field, fields) {
|
||||
if (!jsonObject.contains(field.name)) continue;
|
||||
setValue(field.name, jsonObject[field.name], field.type);
|
||||
}
|
||||
}
|
||||
|
||||
QHash<QString, BaseModel::Value> BaseModel::values() const {
|
||||
return values_;
|
||||
}
|
||||
@ -330,11 +332,30 @@ void BaseModel::setValue(const QString &name, int value) {
|
||||
setValue(name, Value(value));
|
||||
}
|
||||
|
||||
void BaseModel::setValue(const QString &name, const QJsonValue &value, QMetaType::Type type) {
|
||||
if (type == QMetaType::QString) {
|
||||
setValue(name, value.toString());
|
||||
} else if (type == QMetaType::Int) {
|
||||
setValue(name, value.toInt());
|
||||
} else {
|
||||
qCritical() << "Unsupported value type" << name << type;
|
||||
}
|
||||
}
|
||||
|
||||
BaseModel::Value BaseModel::id() const {
|
||||
if (!valueIsSet(primaryKey())) return QVariant();
|
||||
return value(primaryKey());
|
||||
}
|
||||
|
||||
QString BaseModel::valuesToString() const {
|
||||
QString s;
|
||||
for (QHash<QString, Value>::const_iterator it = values_.begin(); it != values_.end(); ++it) {
|
||||
if (s != "") s += "\n";
|
||||
s += it.key() + " = " + it.value().toString();
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
QString BaseModel::tableName(Table t) {
|
||||
if (t == jop::FoldersTable) return "folders";
|
||||
if (t == jop::NotesTable) return "notes";
|
||||
|
@ -60,6 +60,7 @@ public:
|
||||
|
||||
void loadSqlQuery(const QSqlQuery& query);
|
||||
void loadJsonObject(const QJsonObject& jsonObject);
|
||||
void patchJsonObject(const QJsonObject& jsonObject);
|
||||
QHash<QString, Value> values() const;
|
||||
Value value(const QString& name) const;
|
||||
bool valueIsSet(const QString& name) const;
|
||||
@ -67,7 +68,9 @@ public:
|
||||
void setValue(const QString& name, const QVariant& value);
|
||||
void setValue(const QString& name, const QString& value);
|
||||
void setValue(const QString& name, int value);
|
||||
void setValue(const QString& name, const QJsonValue& value, QMetaType::Type type);
|
||||
Value id() const;
|
||||
QString valuesToString() const;
|
||||
|
||||
static QString tableName(Table t);
|
||||
|
||||
|
@ -47,9 +47,9 @@ bool FolderModel::setData(const QModelIndex &index, const QVariant &value, int r
|
||||
if (!folder.save()) return false;
|
||||
cache_.clear();
|
||||
|
||||
QVector<int> roles;
|
||||
roles << Qt::DisplayRole;
|
||||
emit dataChanged(this->index(0), this->index(rowCount() - 1), roles);
|
||||
// QVector<int> roles;
|
||||
// roles << Qt::DisplayRole;
|
||||
// emit dataChanged(this->index(0), this->index(rowCount() - 1), roles);
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -165,6 +165,9 @@ void FolderModel::deleteData(const int index) {
|
||||
emit dataChanged(this->index(0), this->index(rowCount() - 1), roles);
|
||||
}
|
||||
|
||||
// TODO: instead of clearing the whole cache every time, the individual items
|
||||
// could be created/updated/deleted
|
||||
|
||||
void FolderModel::dispatcher_folderCreated(const QString &folderId) {
|
||||
qDebug() << "FolderModel Folder created" << folderId;
|
||||
|
||||
@ -189,6 +192,13 @@ void FolderModel::dispatcher_folderCreated(const QString &folderId) {
|
||||
|
||||
void FolderModel::dispatcher_folderUpdated(const QString &folderId) {
|
||||
qDebug() << "FolderModel Folder udpated" << folderId;
|
||||
|
||||
cache_.clear();
|
||||
|
||||
QVector<int> roles;
|
||||
roles << Qt::DisplayRole;
|
||||
emit dataChanged(this->index(0), this->index(rowCount() - 1), roles);
|
||||
|
||||
//synchronizerTimer_.start(1000 * 3);
|
||||
}
|
||||
|
||||
|
@ -9,7 +9,7 @@ Synchronizer::Synchronizer(const QString &apiUrl, Database &database) : api_(api
|
||||
qDebug() << api_.baseUrl();
|
||||
state_ = Idle;
|
||||
uploadsRemaining_ = 0;
|
||||
downloadsRemaining_ = 0;
|
||||
//downloadsRemaining_ = 0;
|
||||
connect(&api_, SIGNAL(requestDone(QJsonObject,QString)), this, SLOT(api_requestDone(QJsonObject,QString)));
|
||||
}
|
||||
|
||||
@ -52,12 +52,14 @@ void Synchronizer::checkNextState() {
|
||||
|
||||
case DownloadingChanges:
|
||||
|
||||
if (downloadsRemaining_ < 0) qCritical() << "Mismatch on download operations done" << downloadsRemaining_;
|
||||
|
||||
if (downloadsRemaining_ <= 0) {
|
||||
downloadsRemaining_ = 0;
|
||||
switchState(Idle);
|
||||
}
|
||||
|
||||
// if (downloadsRemaining_ < 0) qCritical() << "Mismatch on download operations done" << downloadsRemaining_;
|
||||
|
||||
// if (downloadsRemaining_ <= 0) {
|
||||
// downloadsRemaining_ = 0;
|
||||
// switchState(Idle);
|
||||
// }
|
||||
break;
|
||||
|
||||
case Idle:
|
||||
@ -200,48 +202,41 @@ void Synchronizer::api_requestDone(const QJsonObject& response, const QString& t
|
||||
|
||||
} else if (category == "download") {
|
||||
if (action == "getSynchronizer") {
|
||||
downloadsRemaining_ = 0;
|
||||
QJsonArray items = response["items"].toArray();
|
||||
foreach (QJsonValue item, items) {
|
||||
QJsonObject obj = item.toObject();
|
||||
QString maxRevId = "";
|
||||
foreach (QJsonValue it, items) {
|
||||
QJsonObject obj = it.toObject();
|
||||
QString itemId = obj["item_id"].toString();
|
||||
QString itemType = obj["item_type"].toString();
|
||||
QString operationType = obj["type"].toString();
|
||||
QString revId = obj["id"].toString();
|
||||
QJsonObject item = obj["item"].toObject();
|
||||
|
||||
QString path = itemType + "s";
|
||||
|
||||
if (itemType == "folder") {
|
||||
if (operationType == "create") {
|
||||
api_.get(path + "/" + itemId, QUrlQuery(), QUrlQuery(), "download:createFolder:" + itemId + ":" + revId);
|
||||
Folder folder;
|
||||
folder.loadJsonObject(item);
|
||||
folder.save(false);
|
||||
}
|
||||
|
||||
if (operationType == "update") {
|
||||
//QUrlQuery data;
|
||||
|
||||
api_.patch(path + "/" + itemId, QUrlQuery(), QUrlQuery(), "download:createFolder:" + itemId + ":" + revId);
|
||||
}
|
||||
|
||||
// TODO: update, delete
|
||||
|
||||
downloadsRemaining_++;
|
||||
}
|
||||
|
||||
checkNextState();
|
||||
|
||||
} else {
|
||||
downloadsRemaining_--;
|
||||
|
||||
Settings settings;
|
||||
|
||||
if (action == "createFolder") {
|
||||
Folder folder;
|
||||
folder.loadJsonObject(response);
|
||||
folder.load(itemId);
|
||||
folder.patchJsonObject(item);
|
||||
folder.save(false);
|
||||
}
|
||||
}
|
||||
|
||||
//settings.setValue("lastRevId", arg2);
|
||||
if (revId > maxRevId) maxRevId = revId;
|
||||
}
|
||||
|
||||
if (maxRevId != "") {
|
||||
Settings settings;
|
||||
settings.setValue("lastRevId", maxRevId);
|
||||
}
|
||||
|
||||
checkNextState();
|
||||
|
||||
}
|
||||
} else {
|
||||
qCritical() << "Invalid category" << category;
|
||||
|
Loading…
Reference in New Issue
Block a user