2018-10-16 18:50:58 +03:00
|
|
|
import QtQuick 2.0
|
|
|
|
import Sailfish.Silica 1.0
|
|
|
|
|
2018-10-17 00:52:28 +03:00
|
|
|
Item {
|
2018-10-16 18:50:58 +03:00
|
|
|
property string name
|
2018-10-17 00:52:28 +03:00
|
|
|
property var model: ListModel { }
|
|
|
|
|
|
|
|
property string json
|
2018-10-16 18:50:58 +03:00
|
|
|
property string file: StandardPaths.data + "/" + name + ".json"
|
|
|
|
property bool saveFile: false
|
|
|
|
property bool busy: false
|
|
|
|
property date lastUpdate: new Date(0)
|
|
|
|
|
2018-10-17 00:52:28 +03:00
|
|
|
function callApi(method, data) {
|
|
|
|
busy = true
|
|
|
|
|
|
|
|
var endpoint = ""
|
|
|
|
if (data && (method === "GET" || method === "PUT" || method === "DELETE"))
|
|
|
|
if (data.id)
|
|
|
|
endpoint = "/" + data.id
|
|
|
|
|
|
|
|
var apiReq = new XMLHttpRequest
|
|
|
|
apiReq.open(method, appSettings.server + endpoint, true)
|
2018-10-17 10:41:09 +03:00
|
|
|
apiReq.setRequestHeader('User-Agent', 'SailfishOS/harbour-nextcloudnotes')
|
2018-10-17 00:52:28 +03:00
|
|
|
apiReq.setRequestHeader('OCS-APIRequest', 'true')
|
|
|
|
apiReq.setRequestHeader("Content-Type", "application/json");
|
|
|
|
apiReq.setRequestHeader("Authorization", "Basic " + Qt.btoa(appSettings.username + ":" + appSettings.password))
|
|
|
|
apiReq.onreadystatechange = function() {
|
|
|
|
if (apiReq.readyState === XMLHttpRequest.DONE) {
|
|
|
|
if (apiReq.status === 200) {
|
|
|
|
console.log("Successfull request!")
|
|
|
|
console.log(apiReq.responseText)
|
|
|
|
// TODO handle non arrays
|
|
|
|
var elements = JSON.parse(apiReq.responseText)
|
|
|
|
for (var element in elements) {
|
|
|
|
model.append(elements[element])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (apiReq.status === 404) {
|
|
|
|
console.log("Note does not exist!")
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
console.log("Unknown error: " + apiReq.statusText + " (" + apiReq.status + ")")
|
|
|
|
}
|
|
|
|
busy = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (method === "GET") {
|
|
|
|
apiReq.send()
|
|
|
|
}
|
|
|
|
else if (method === "POST" || method === "PUT" || method === "DELETE") {
|
|
|
|
apiReq.send(JSON.stringify(data))
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
console.log("Unsupported method: " + method)
|
|
|
|
apiReq.abort()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getNotes() {
|
|
|
|
callApi("GET")
|
|
|
|
}
|
|
|
|
|
|
|
|
function getNote(id) {
|
|
|
|
if (id)
|
|
|
|
callApi("GET", { 'id': id } )
|
|
|
|
}
|
|
|
|
|
|
|
|
function createNote(data) {
|
|
|
|
callApi("POST", data)
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateNote(id, data) {
|
|
|
|
if (id) {
|
|
|
|
data.id = id
|
|
|
|
callApi("PUT", data)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function deleteNote(id) {
|
|
|
|
if (id)
|
|
|
|
callApi("DELETE", { 'id': id } )
|
|
|
|
}
|
|
|
|
|
2018-10-16 18:50:58 +03:00
|
|
|
onJsonChanged: refresh()
|
|
|
|
|
|
|
|
function flush() {
|
|
|
|
json = ""
|
|
|
|
var filePut = new XMLHttpRequest
|
|
|
|
filePut.open("PUT", file)
|
|
|
|
filePut.send(json)
|
2018-10-17 00:52:28 +03:00
|
|
|
model.clear()
|
2018-10-16 18:50:58 +03:00
|
|
|
lastUpdate = new Date(0)
|
|
|
|
status = 200
|
|
|
|
}
|
|
|
|
|
|
|
|
function refresh() {
|
|
|
|
search("")
|
|
|
|
}
|
|
|
|
|
|
|
|
function search(query) {
|
2018-10-17 00:52:28 +03:00
|
|
|
model.clear()
|
2018-10-16 18:50:58 +03:00
|
|
|
var elements = parseJson()
|
|
|
|
for (var element in elements) {
|
|
|
|
elements[element].section = ""
|
|
|
|
var match = false
|
|
|
|
for (var child in elements[element]) {
|
|
|
|
if (elements[element][child]) {
|
|
|
|
match = (elements[element][child].toString().toLowerCase().indexOf(query) >= 0) || match
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (query === "" || match)
|
2018-10-17 00:52:28 +03:00
|
|
|
model.append(elements[element])
|
2018-10-16 18:50:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function parseJson() {
|
|
|
|
var elements = JSON.parse(json)
|
|
|
|
if (elements === null) {
|
|
|
|
console.log("Error parsing " + name + "-JSON")
|
|
|
|
elements = ""
|
|
|
|
json = ""
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
else {
|
2018-10-17 00:52:28 +03:00
|
|
|
model.clear()
|
2018-10-16 18:50:58 +03:00
|
|
|
return elements
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-17 00:52:28 +03:00
|
|
|
/*Component.onCompleted: {
|
2018-10-16 18:50:58 +03:00
|
|
|
if (saveFile) {
|
|
|
|
if (name === "") {
|
|
|
|
saveFile = false
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
busy = true
|
|
|
|
var fileReq = new XMLHttpRequest
|
|
|
|
fileReq.open("GET", file)
|
|
|
|
fileReq.onreadystatechange = function() {
|
|
|
|
if (fileReq.readyState === XMLHttpRequest.DONE) {
|
|
|
|
if (fileReq.responseText === "") {
|
|
|
|
update()
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
console.log("Loaded " + name + " from local JSON file")
|
|
|
|
json = fileReq.responseText
|
|
|
|
busy = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fileReq.send()
|
|
|
|
}
|
|
|
|
}
|
2018-10-17 00:52:28 +03:00
|
|
|
}*/
|
2018-10-16 18:50:58 +03:00
|
|
|
}
|