2018-11-26 19:21:02 +03:00
|
|
|
import QtQuick 2.5
|
2018-10-16 18:50:58 +03:00
|
|
|
import Sailfish.Silica 1.0
|
2018-11-18 00:28:25 +03:00
|
|
|
import Nemo.Configuration 1.0
|
2018-10-16 18:50:58 +03:00
|
|
|
|
2018-10-17 00:52:28 +03:00
|
|
|
Item {
|
2018-11-18 00:28:25 +03:00
|
|
|
property string uuid
|
|
|
|
|
2019-01-26 20:09:28 +03:00
|
|
|
property string response
|
2018-12-06 15:37:29 +03:00
|
|
|
property var categories: [ ]
|
2018-11-30 20:40:03 +03:00
|
|
|
property string file: StandardPaths.data + "/" + uuid + ".json"
|
|
|
|
property bool saveFile: false
|
2018-12-22 10:03:15 +03:00
|
|
|
property bool busy: jobsRunning > 0
|
|
|
|
property int jobsRunning: 0
|
2018-12-29 03:40:11 +03:00
|
|
|
property int status: 0 //204
|
2018-11-24 21:34:01 +03:00
|
|
|
property string statusText: "No Content"
|
2018-11-25 22:39:03 +03:00
|
|
|
|
2018-11-25 16:07:37 +03:00
|
|
|
onStatusChanged: {
|
|
|
|
console.log("Network status: " + statusText + " (" + status + ")")
|
|
|
|
}
|
2018-12-07 02:30:18 +03:00
|
|
|
onUuidChanged: {
|
2018-12-29 03:40:11 +03:00
|
|
|
appSettings.currentAccount = uuid
|
2018-12-07 02:30:18 +03:00
|
|
|
account.path = "/apps/harbour-nextcloudnotes/accounts/" + uuid
|
2018-12-29 03:40:11 +03:00
|
|
|
onUuidChanged: console.log("Account : " + account.name)
|
2018-12-07 02:30:18 +03:00
|
|
|
}
|
2019-02-01 21:17:07 +03:00
|
|
|
/*function clear() {
|
2018-11-18 00:28:25 +03:00
|
|
|
account.clear()
|
2019-02-01 21:17:07 +03:00
|
|
|
}*/
|
2018-11-18 00:28:25 +03:00
|
|
|
|
2018-12-20 20:53:29 +03:00
|
|
|
function apiCall(method, data) {
|
2018-12-22 10:03:15 +03:00
|
|
|
jobsRunning++
|
2018-10-17 00:52:28 +03:00
|
|
|
|
2018-12-27 22:38:14 +03:00
|
|
|
var endpoint = account.server + "/index.php/apps/notes/api/" + account.version + "/notes"
|
2018-12-20 20:53:29 +03:00
|
|
|
if (data) {
|
|
|
|
if (method === "POST" || method === "PUT") {
|
2018-12-29 03:40:11 +03:00
|
|
|
console.log("Adding note...")
|
2018-12-20 20:53:29 +03:00
|
|
|
}
|
|
|
|
else if (data.id && method === "DELETE") {
|
2018-12-29 03:40:11 +03:00
|
|
|
console.log("Deleting note...")
|
2018-12-20 20:53:29 +03:00
|
|
|
}
|
|
|
|
if (method === "GET" || method === "PUT" || method === "DELETE") {
|
|
|
|
if (data.id) {
|
|
|
|
endpoint = endpoint + "/" + data.id
|
|
|
|
}
|
2018-11-18 13:25:28 +03:00
|
|
|
}
|
|
|
|
}
|
2018-10-17 00:52:28 +03:00
|
|
|
|
2018-12-07 02:30:18 +03:00
|
|
|
console.log("Calling " + endpoint)
|
2018-12-11 01:14:15 +03:00
|
|
|
var apiReq = new XMLHttpRequest
|
2018-10-18 01:33:47 +03:00
|
|
|
apiReq.open(method, 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')
|
2018-10-18 01:33:47 +03:00
|
|
|
apiReq.setRequestHeader("Content-Type", "application/json")
|
2018-12-27 22:38:14 +03:00
|
|
|
apiReq.setRequestHeader("Authorization", "Basic " + Qt.btoa(account.username + ":" + account.password))
|
2018-12-11 01:14:15 +03:00
|
|
|
apiReq.withCredentials = true
|
2018-12-20 20:53:29 +03:00
|
|
|
apiReq.timeout = 5000
|
2018-10-17 00:52:28 +03:00
|
|
|
apiReq.onreadystatechange = function() {
|
|
|
|
if (apiReq.readyState === XMLHttpRequest.DONE) {
|
2018-12-20 20:53:29 +03:00
|
|
|
statusText = apiReq.statusText
|
|
|
|
status = apiReq.status
|
2018-10-17 00:52:28 +03:00
|
|
|
if (apiReq.status === 200) {
|
2019-01-26 20:09:28 +03:00
|
|
|
response = apiReq.responseText
|
2019-02-24 18:40:47 +03:00
|
|
|
//console.log(response)
|
2018-12-20 20:53:29 +03:00
|
|
|
}
|
|
|
|
else if(apiReq.status === 0) {
|
|
|
|
statusText = qsTr("Unable to connect")
|
|
|
|
}
|
|
|
|
/*
|
2018-10-17 13:58:55 +03:00
|
|
|
else if (apiReq.status === 304) {
|
|
|
|
console.log("ETag does not differ!")
|
|
|
|
}
|
|
|
|
else if (apiReq.status === 401) {
|
|
|
|
console.log("Unauthorized!")
|
|
|
|
}
|
2018-10-17 00:52:28 +03:00
|
|
|
else if (apiReq.status === 404) {
|
|
|
|
console.log("Note does not exist!")
|
2018-10-23 23:15:59 +03:00
|
|
|
}*/
|
2018-10-17 00:52:28 +03:00
|
|
|
else {
|
2018-11-25 16:07:37 +03:00
|
|
|
//console.log("Network error: " + apiReq.statusText + " (" + apiReq.status + ")")
|
2018-10-17 00:52:28 +03:00
|
|
|
}
|
2018-12-22 10:03:15 +03:00
|
|
|
jobsRunning--
|
2018-10-17 00:52:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-20 20:53:29 +03:00
|
|
|
function getNote(id) {
|
|
|
|
var dict
|
|
|
|
if (id) {
|
|
|
|
for (var i = 0; i < model.count; i++) {
|
|
|
|
dict = model.get(i)
|
|
|
|
if (dict.id === id) {
|
|
|
|
return dict
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getNotesFromApi() {
|
|
|
|
apiCall("GET")
|
2018-10-17 00:52:28 +03:00
|
|
|
}
|
|
|
|
|
2018-12-20 20:53:29 +03:00
|
|
|
function getNoteFromApi(id) {
|
2018-12-03 22:33:37 +03:00
|
|
|
if (id) {
|
2018-12-20 20:53:29 +03:00
|
|
|
apiCall("GET", { 'id': id } )
|
2018-12-03 22:33:37 +03:00
|
|
|
}
|
2018-10-17 00:52:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function createNote(data) {
|
2018-12-03 22:33:37 +03:00
|
|
|
if (data)
|
2018-12-20 20:53:29 +03:00
|
|
|
apiCall("POST", data)
|
2018-10-17 00:52:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function updateNote(id, data) {
|
2018-12-03 22:33:37 +03:00
|
|
|
if (id && data) {
|
2018-10-17 00:52:28 +03:00
|
|
|
data.id = id
|
2018-12-20 20:53:29 +03:00
|
|
|
apiCall("PUT", data)
|
2018-10-17 00:52:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function deleteNote(id) {
|
|
|
|
if (id)
|
2018-12-20 20:53:29 +03:00
|
|
|
apiCall("DELETE", { 'id': id } )
|
2018-11-26 19:21:02 +03:00
|
|
|
}
|
|
|
|
|
2018-11-24 21:34:01 +03:00
|
|
|
// source: https://stackoverflow.com/a/14339782
|
2018-11-25 22:39:03 +03:00
|
|
|
function getPrettyDate(date) {
|
2018-11-24 21:34:01 +03:00
|
|
|
var today = new Date()
|
|
|
|
today.setHours(0)
|
|
|
|
today.setMinutes(0)
|
|
|
|
today.setSeconds(0)
|
|
|
|
today.setMilliseconds(0)
|
|
|
|
var compDate = new Date(date*1000)
|
|
|
|
compDate.setHours(0)
|
|
|
|
compDate.setMinutes(0)
|
|
|
|
compDate.setSeconds(0)
|
|
|
|
compDate.setMilliseconds(0)
|
|
|
|
if (compDate.getTime() === today.getTime()) {
|
|
|
|
return qsTr("Today")
|
2018-12-20 20:53:29 +03:00
|
|
|
} else if ((today.getTime() - compDate.getTime()) === (24 * 60 * 60 * 1000)) {
|
2018-11-24 21:34:01 +03:00
|
|
|
return qsTr("Yesterday")
|
2018-12-20 20:53:29 +03:00
|
|
|
} else if ((today.getTime() - compDate.getTime()) <= (7 * 24 * 60 * 60 * 1000)) {
|
|
|
|
return compDate.toLocaleDateString(Qt.locale(), "dddd")
|
|
|
|
} else if (today.getFullYear() === compDate.getFullYear()) {
|
|
|
|
return compDate.toLocaleDateString(Qt.locale(), "MMMM")
|
2018-11-24 21:34:01 +03:00
|
|
|
} else {
|
2018-12-20 20:53:29 +03:00
|
|
|
return compDate.toLocaleDateString(Qt.locale(), "MMMM yyyy")
|
2018-11-24 21:34:01 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-17 00:52:28 +03:00
|
|
|
/*Component.onCompleted: {
|
2018-10-16 18:50:58 +03:00
|
|
|
if (saveFile) {
|
2018-11-15 00:13:47 +03:00
|
|
|
if (account.name === "") {
|
2018-10-16 18:50:58 +03:00
|
|
|
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 {
|
2018-11-15 00:13:47 +03:00
|
|
|
console.log("Loaded " + account.name + " from local JSON file")
|
2018-10-16 18:50:58 +03:00
|
|
|
json = fileReq.responseText
|
|
|
|
busy = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fileReq.send()
|
|
|
|
}
|
|
|
|
}
|
2018-10-17 00:52:28 +03:00
|
|
|
}*/
|
2018-10-16 18:50:58 +03:00
|
|
|
}
|