2018-10-16 18:50:58 +03:00
|
|
|
import QtQuick 2.0
|
|
|
|
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
|
|
|
|
property string name
|
|
|
|
property url server
|
2018-11-24 21:34:01 +03:00
|
|
|
property url url
|
|
|
|
property string version: "v0.2"
|
2018-11-18 00:28:25 +03:00
|
|
|
property string username
|
|
|
|
property string password
|
|
|
|
property date update
|
|
|
|
property bool unsecureConnection
|
|
|
|
property bool unencryptedConnection
|
|
|
|
|
2018-11-25 22:39:03 +03:00
|
|
|
property var modelData: [ ]
|
2018-10-17 00:52:28 +03:00
|
|
|
property var model: ListModel { }
|
2018-11-18 13:25:28 +03:00
|
|
|
//property string file: StandardPaths.data + "/" + uuid + ".json"
|
|
|
|
//property bool saveFile: false
|
2018-10-16 18:50:58 +03:00
|
|
|
property bool busy: false
|
2018-11-24 21:34:01 +03:00
|
|
|
property int status: 204
|
|
|
|
property string statusText: "No Content"
|
2018-11-25 22:39:03 +03:00
|
|
|
|
|
|
|
onModelDataChanged: {
|
|
|
|
console.log("modelData changed")
|
|
|
|
// TODO
|
|
|
|
}
|
2018-11-25 16:07:37 +03:00
|
|
|
onStatusChanged: {
|
|
|
|
console.log("Network status: " + statusText + " (" + status + ")")
|
|
|
|
}
|
2018-10-16 18:50:58 +03:00
|
|
|
|
2018-11-25 22:39:03 +03:00
|
|
|
Connections {
|
|
|
|
target: appSettings
|
|
|
|
onSortByChanged: sortModelData()
|
|
|
|
}
|
|
|
|
|
2018-11-18 00:28:25 +03:00
|
|
|
ConfigurationGroup {
|
|
|
|
id: account
|
|
|
|
path: "/apps/harbour-nextcloudnotes/accounts/" + uuid
|
|
|
|
}
|
|
|
|
|
|
|
|
Component.onCompleted: {
|
|
|
|
name = account.value("name", "", String)
|
|
|
|
server = account.value("server", "", String)
|
2018-11-24 21:34:01 +03:00
|
|
|
url = server + "/index.php/apps/notes/api/" + version + "/notes"
|
2018-11-18 00:28:25 +03:00
|
|
|
username = account.value("username", "", String)
|
|
|
|
password = account.value("password", "", String)
|
|
|
|
update = account.value("update", "", Date)
|
|
|
|
unsecureConnection = account.value("unsecureConnection", false, Boolean)
|
|
|
|
unencryptedConnection = account.value("unencryptedConnection", false, Boolean)
|
|
|
|
}
|
|
|
|
|
|
|
|
onUuidChanged: account.setValue("uuid", uuid)
|
|
|
|
onNameChanged: account.setValue("name", name)
|
|
|
|
onServerChanged: account.setValue("server", server)
|
|
|
|
onUsernameChanged: account.setValue("username", username)
|
|
|
|
onPasswordChanged: account.setValue("password", password)
|
|
|
|
onUpdateChanged: account.setValue("update", update)
|
|
|
|
onUnsecureConnectionChanged: account.setValue("unsecureConnection", unsecureConnection)
|
|
|
|
onUnencryptedConnectionChanged: account.setValue("unencryptedConnection", unencryptedConnection)
|
|
|
|
|
|
|
|
function clear() {
|
2018-11-18 13:25:28 +03:00
|
|
|
model.clear()
|
2018-11-18 00:28:25 +03:00
|
|
|
account.clear()
|
|
|
|
}
|
|
|
|
|
2018-10-17 00:52:28 +03:00
|
|
|
function callApi(method, data) {
|
|
|
|
busy = true
|
|
|
|
|
2018-11-24 21:34:01 +03:00
|
|
|
var endpoint = url
|
2018-11-18 13:25:28 +03:00
|
|
|
if (data && (method === "GET" || method === "PUT" || method === "DELETE")) {
|
|
|
|
if (data.id) {
|
2018-10-18 01:33:47 +03:00
|
|
|
endpoint = endpoint + "/" + data.id
|
2018-11-18 13:25:28 +03:00
|
|
|
}
|
|
|
|
}
|
2018-10-17 00:52:28 +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-11-18 00:28:25 +03:00
|
|
|
apiReq.setRequestHeader("Authorization", "Basic " + Qt.btoa(username + ":" + password))
|
2018-10-17 00:52:28 +03:00
|
|
|
apiReq.onreadystatechange = function() {
|
|
|
|
if (apiReq.readyState === XMLHttpRequest.DONE) {
|
|
|
|
if (apiReq.status === 200) {
|
2018-11-25 16:07:37 +03:00
|
|
|
//console.log("Network status: " + apiReq.statusText + " (" + apiReq.status + ")")
|
2018-11-18 13:25:28 +03:00
|
|
|
|
2018-11-25 16:07:37 +03:00
|
|
|
var json = JSON.parse(apiReq.responseText)
|
2018-11-18 13:25:28 +03:00
|
|
|
switch(method) {
|
|
|
|
case "GET":
|
|
|
|
if (Array.isArray(json)) {
|
2018-11-24 21:34:01 +03:00
|
|
|
console.log("Received all notes via API: " + endpoint)
|
2018-11-25 16:07:37 +03:00
|
|
|
//model.clear()
|
2018-11-25 22:39:03 +03:00
|
|
|
modelData = json
|
|
|
|
sortModelData()
|
|
|
|
|
2018-11-18 13:25:28 +03:00
|
|
|
for (var element in json) {
|
2018-11-25 16:07:37 +03:00
|
|
|
model.set(element, json[element])
|
2018-11-25 22:39:03 +03:00
|
|
|
model.setProperty(element, "date", getPrettyDate(json[element].modified))
|
2018-11-18 13:25:28 +03:00
|
|
|
}
|
2018-11-25 16:07:37 +03:00
|
|
|
element++
|
|
|
|
while (model.count > element) {
|
|
|
|
model.remove(element)
|
|
|
|
}
|
2018-11-18 13:25:28 +03:00
|
|
|
update = new Date()
|
|
|
|
}
|
|
|
|
else {
|
2018-11-24 21:34:01 +03:00
|
|
|
console.log("Received a single note via API: " + endpoint)
|
2018-11-25 22:39:03 +03:00
|
|
|
addToModelData(json)
|
|
|
|
|
2018-11-25 16:07:37 +03:00
|
|
|
var noteModified = false
|
2018-11-25 22:39:03 +03:00
|
|
|
//json.date = getPrettyDate(json.modified)
|
2018-11-18 13:25:28 +03:00
|
|
|
for (var i = 0; i < model.count; i++) {
|
|
|
|
var listItem = model.get(i)
|
|
|
|
if (listItem.id === json.id){
|
|
|
|
model.set(i, json)
|
2018-11-25 22:39:03 +03:00
|
|
|
model.setProperty(i, "date", getPrettyDate(json.modified))
|
2018-11-25 16:07:37 +03:00
|
|
|
noteModified = true
|
2018-11-18 13:25:28 +03:00
|
|
|
}
|
|
|
|
}
|
2018-11-25 16:07:37 +03:00
|
|
|
if (!noteModified) {
|
2018-11-25 22:39:03 +03:00
|
|
|
//json.date = getPrettyDate(json.modified)
|
2018-11-25 16:07:37 +03:00
|
|
|
model.set(i, json)
|
2018-11-25 22:39:03 +03:00
|
|
|
model.setProperty(i, "date", getPrettyDate(json.modified))
|
2018-11-25 16:07:37 +03:00
|
|
|
}
|
2018-11-18 13:25:28 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "POST":
|
2018-11-24 21:34:01 +03:00
|
|
|
console.log("Created a note via API: " + endpoint)
|
2018-11-25 22:39:03 +03:00
|
|
|
addToModelData(json)
|
|
|
|
|
2018-11-25 16:07:37 +03:00
|
|
|
model.set(model.count, json)
|
2018-11-25 22:39:03 +03:00
|
|
|
model.setProperty(model.count-1, "date", getPrettyDate(json.modified))
|
2018-11-18 13:25:28 +03:00
|
|
|
model.move(model.count-1, 0, 1)
|
|
|
|
break;
|
|
|
|
case "PUT":
|
2018-11-24 21:34:01 +03:00
|
|
|
console.log("Updated a note via API: " + endpoint)
|
2018-11-25 22:39:03 +03:00
|
|
|
addToModelData(json)
|
|
|
|
|
2018-11-25 16:07:37 +03:00
|
|
|
for (i = 0; i < model.count; i++) {
|
|
|
|
listItem = model.get(i)
|
2018-11-18 13:25:28 +03:00
|
|
|
if (listItem.id === json.id){
|
|
|
|
model.set(i, json)
|
2018-11-25 22:39:03 +03:00
|
|
|
model.setProperty(i, "date", getPrettyDate(json.modified))
|
2018-11-18 13:25:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "DELETE":
|
2018-11-24 21:34:01 +03:00
|
|
|
console.log("Deleted a note via API: " + endpoint)
|
2018-11-25 16:07:37 +03:00
|
|
|
for (i = 0; i < model.count; i++) {
|
|
|
|
listItem = model.get(i)
|
2018-11-18 15:11:22 +03:00
|
|
|
if (listItem.id === data.id){
|
|
|
|
model.remove(i)
|
|
|
|
}
|
|
|
|
}
|
2018-11-18 13:25:28 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
console.log("Unsupported method: " + method)
|
|
|
|
break;
|
2018-10-17 00:52:28 +03:00
|
|
|
}
|
2018-10-23 23:15:59 +03:00
|
|
|
}/*
|
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-11-24 21:34:01 +03:00
|
|
|
statusText = apiReq.statusText
|
2018-11-25 16:07:37 +03:00
|
|
|
status = apiReq.status
|
2018-11-24 21:34:01 +03:00
|
|
|
//model.sync()
|
2018-10-17 00:52:28 +03:00
|
|
|
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-11-25 22:39:03 +03:00
|
|
|
function addToModelData(data) {
|
|
|
|
data.date = getPrettyDate(data.modified)
|
|
|
|
for (var i = 0; i < modelData.length; i++) {
|
|
|
|
if (modelData[i].id === data.id) {
|
|
|
|
modelData[i] = data
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (i === modelData.length) {
|
|
|
|
modelData.push(data)
|
|
|
|
sortModelData()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function sortModelData() {
|
|
|
|
switch(appSettings.sortBy) {
|
|
|
|
case "date":
|
|
|
|
modelData.sort(function(a, b) { return b.modified-a.modified } )
|
|
|
|
break
|
|
|
|
case "category":
|
|
|
|
modelData.sort(function(a, b) { return ((a.category > b.category) ? 1 : ((b.category > a.category) ? -1 : 0)) } )
|
|
|
|
break
|
|
|
|
case "title":
|
|
|
|
modelData.sort(function(a, b) { return ((a.title > b.title) ? 1 : ((b.title > a.title) ? -1 : 0)) } )
|
|
|
|
break
|
|
|
|
}
|
|
|
|
//console.log(JSON.stringify(modelData, null, 4))
|
|
|
|
}
|
|
|
|
|
2018-10-16 18:50:58 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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")
|
|
|
|
} else if ((today.getTime() - compDate.getTime()) <= (24 * 60 * 60 *1000)) {
|
|
|
|
return qsTr("Yesterday")
|
|
|
|
} else {
|
|
|
|
return compDate.toLocaleDateString(Qt.locale(), Locale.ShortFormat)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|