-
Notifications
You must be signed in to change notification settings - Fork 1
/
qparseobject.cpp
116 lines (103 loc) · 2.96 KB
/
qparseobject.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include "qparseobject.h"
#include "qparse.h"
#include "qparserequest.h"
#include "qparsereply.h"
#include <QDebug>
QParseObject::QParseObject( QObject* parent )
: QObject(parent)
, objectId()
, createdAt()
, updatedAt()
, updating(false)
, saving(false) {
}
QParseObject::QParseObject(QJsonObject jsonData, QObject* parent)
: QObject(parent)
, objectId()
, createdAt()
, updatedAt()
, updating(false)
, saving(false) {
objectId = jsonData["objectId"].toString();
createdAt = QParseDate( jsonData["createdAt"].toString() );
updatedAt = QParseDate( jsonData["updatedAt"].toString() );
}
QString QParseObject::getObjectId() const {
return objectId;
}
QParseDate QParseObject::getCreatedAt() const {
return createdAt;
}
QParseDate QParseObject::getUpdatedAt() const {
return updatedAt;
}
bool QParseObject::isUpdating() {
return updating;
}
void QParseObject::update() {
if ( objectId.isEmpty() || updating || saving ) return;
updating = true;
QParseRequest* update = new QParseRequest(parseClassName());
update->setParseObject( this );
QParseReply* reply = QParse::instance()->get( update );
connect( reply, &QParseReply::finished, this, &QParseObject::onUpdateReply );
}
bool QParseObject::isSaving() {
return saving;
}
void QParseObject::save() {
if ( updating || saving ) return;
saving = true;
QParseRequest* save = new QParseRequest(parseClassName());
save->setParseObject( this );
QParseReply* reply;
if ( objectId.isEmpty() ) {
// create the object
reply = QParse::instance()->post( save );
} else {
// updated the object
reply = QParse::instance()->put( save );
}
connect( reply, &QParseReply::finished, this, &QParseObject::onSaveReply );
}
QJsonObject QParseObject::toJson( bool onlyChanged ) {
Q_UNUSED( onlyChanged )
QJsonObject data;
foreach( QString property, parseProperties() ) {
QVariant value = this->property( property.toLatin1().data() );
// handle PARSE specific data
if ( value.canConvert<QParseObject*>() ) {
// pointer to Parse object
data[property] = value.value<QParseObject*>()->getJsonPointer();
} else if ( value.canConvert<QParseDate>() ) {
// Parse Date type
data[property] = value.value<QParseDate>().toJson();
} else if ( value.canConvert<QParseFile*>() ) {
// Parse File type
data[property] = value.value<QParseFile*>()->toJson();
} else {
data[property] = QJsonValue::fromVariant(this->property( property.toLatin1().data() ));
}
}
return data;
}
QJsonObject QParseObject::getJsonPointer() {
QJsonObject pointer;
pointer["__type"] = "Pointer";
pointer["className"] = parseClassName();
pointer["objectId"] = getObjectId();
return pointer;
}
void QParseObject::onUpdateReply( QParseReply* reply ) {
updating = false;
emit updatingChanged( updating );
emit updatingDone();
reply->deleteLater();
}
void QParseObject::onSaveReply( QParseReply* reply ) {
saving = false;
// if id.isEmpty a new object has been created
emit savingChanged( saving );
emit savingDone();
reply->deleteLater();
}