Files
harbour-quill/qml/components/DavClient.qml
T
kaneda 8259297a27 0.3.0: ricerca nel testo, caselle di spunta toccabili, tag
- Ricerca nel testo: la cartella viene percorsa ricorsivamente e le note .md
  scaricate una alla volta (il WebDAV non cerca nel contenuto), con avanzamento,
  possibilità di fermare e il passaggio che contiene la parola. La ricerca per
  titolo nella lista resta istantanea.
- Caselle di spunta: "- [ ]" e "- [x]" diventano caselle toccabili
  nell'anteprima; il tocco riscrive quella sola riga e salva subito, e l'editor
  segue (senza, uscendo riscriverebbe la versione vecchia sul server).
- Tag: front-matter (tags: [a, b], lista a trattini o separata da virgole) e
  #tag nel testo; pastiglie in cima all'anteprima, #tag toccabili, nuova
  schermata Tag con il conteggio per tag. Il blocco front-matter non viene più
  stampato come testo.
- tests/qml_contract_check.py: confronta i nomi JS e le proprietà usati dalla
  QML con i sorgenti, le pagine raggiungibili e i parametri dei gestori dei
  segnali — la classe di errori che la compilazione non vede.
- Catalogo italiano aggiornato (25 stringhe nuove); repo ripulito dai residui di
  build e .gitignore esteso.
2026-11-08 20:55:22 +01:00

257 lines
8.2 KiB
QML

import QtQuick 2.2
import "PropfindParser.js" as Prop
// Minimal WebDAV client over XMLHttpRequest. One request at a time
// (busy flag); 30 s watchdog turns hangs into a failed() signal.
Item {
id: dav
property bool busy: false
signal listReady(var entries)
signal fileReady(string content)
signal writeDone()
signal deleteDone()
signal connOk()
signal failed(string message)
// Vault scan: search across the text and tag listing need the CONTENT of
// many notes, and WebDAV has no content search. So the tree is walked and
// the .md files are downloaded one at a time (same watchdog, same busy
// flag), emitting each file as it lands. The scan has its own signals so it
// never disturbs the list/read signals of the page underneath.
signal scanStart(int count)
signal scanProgress(int done, int all)
signal scanFile(string path, string content)
signal scanFinished(int done, int errs)
property bool scanning: false
property int scanRead: 0
property int scanErrors: 0
property var scanFolders: []
property var scanFiles: []
property string scanRoot: ""
// Bounds: a scan must never eat the phone's memory or the battery.
property int scanMaxFiles: 500
property int scanMaxBytes: 262144
property var currentXhr: null
Timer {
id: watchdog
interval: 30000
repeat: false
onTriggered: {
if (dav.currentXhr) {
dav.currentXhr.abort()
dav.currentXhr = null
}
dav.busy = false
// A scan cut by the watchdog would otherwise never report back.
if (dav.scanning) {
dav.scanning = false
dav.scanFolders = []
dav.scanFiles = []
dav.scanFinished(dav.scanRead, dav.scanErrors + 1)
}
dav.failed(qsTr("Network timeout"))
}
}
function authHeader() {
var user = appSettings.username
if (!user) return ""
return "Basic " + Qt.btoa(user + ":" + appSettings.password)
}
function encodeRel(rel) {
if (!rel) return rel
var parts = rel.split("/")
var out = []
for (var i = 0; i < parts.length; i++) out.push(encodeURIComponent(parts[i]))
return out.join("/")
}
function fullUrl(rel) {
var base = String(appSettings.serverUrl).replace(/\s+$/, "")
if (!base) return ""
if (rel && rel !== "") {
if (base.charAt(base.length - 1) !== "/") base += "/"
base += encodeRel(rel)
}
return base
}
function request(method, url, body, onDone) {
if (busy) {
failed(qsTr("Request already in progress"))
return
}
busy = true
var xhr = new XMLHttpRequest()
currentXhr = xhr
xhr.open(method, url, true)
var auth = authHeader()
if (auth) xhr.setRequestHeader("Authorization", auth)
if (method === "PROPFIND") xhr.setRequestHeader("Depth", "1")
if (method === "PUT") xhr.setRequestHeader("Content-Type", "text/markdown; charset=utf-8")
xhr.onreadystatechange = function () {
if (xhr.readyState !== 4) return
watchdog.stop()
if (currentXhr === xhr) {
currentXhr = null
busy = false
onDone(xhr)
}
// else: aborted by the watchdog — already signalled
}
xhr.send(body || "")
watchdog.restart()
}
function listDir(relPath) {
request("PROPFIND", fullUrl(relPath || ""), "", function (xhr) {
if (xhr.status >= 200 && xhr.status < 300) {
var entries = Prop.parse(xhr.responseText, appSettings.serverUrl)
listReady(entries)
} else {
failed(statusMessage(xhr.status))
}
})
}
function readFile(relPath) {
request("GET", fullUrl(relPath), "", function (xhr) {
if (xhr.status >= 200 && xhr.status < 300) {
fileReady(xhr.responseText)
} else {
failed(statusMessage(xhr.status))
}
})
}
function writeFile(relPath, content) {
request("PUT", fullUrl(relPath), content, function (xhr) {
if (xhr.status >= 200 && xhr.status < 300) {
writeDone()
} else {
failed(statusMessage(xhr.status))
}
})
}
function deleteFile(relPath) {
request("DELETE", fullUrl(relPath), "", function (xhr) {
if (xhr.status >= 200 && xhr.status < 300) {
deleteDone()
} else {
failed(statusMessage(xhr.status))
}
})
}
function testConnection() {
request("PROPFIND", fullUrl(""), "", function (xhr) {
if (xhr.status >= 200 && xhr.status < 300) {
connOk()
} else {
failed(statusMessage(xhr.status))
}
})
}
function isNote(name) {
return /\.(md|markdown)$/i.test(String(name))
}
// Walk rootPath (recursively) and download every .md file: first the folder
// listings, then the contents. cancelScan() stops it cleanly; the watchdog
// closes it if the network hangs.
function scanVault(rootPath) {
if (busy || scanning) {
failed(qsTr("Request already in progress"))
return
}
scanning = true
scanRoot = rootPath || ""
scanFolders = [scanRoot]
scanFiles = []
scanRead = 0
scanErrors = 0
nextScanFolder()
}
function cancelScan() {
if (!scanning) return
scanning = false
scanFolders = []
scanFiles = []
if (currentXhr) {
currentXhr.abort()
currentXhr = null
}
watchdog.stop()
busy = false
scanFinished(scanRead, scanErrors)
}
function nextScanFolder() {
if (!scanning) return
if (scanFolders.length === 0) {
scanStart(scanFiles.length)
readNextScanFile()
return
}
var folder = scanFolders.shift()
request("PROPFIND", fullUrl(folder), "", function (xhr) {
if (!dav.scanning) return
if (xhr.status >= 200 && xhr.status < 300) {
var entries = Prop.parse(xhr.responseText, appSettings.serverUrl)
for (var i = 0; i < entries.length; i++) {
var e = entries[i]
if (e.isDir) {
dav.scanFolders.push(e.path)
} else if (dav.isNote(e.name) && e.size <= dav.scanMaxBytes
&& dav.scanFiles.length < dav.scanMaxFiles) {
dav.scanFiles.push({ path: e.path, name: e.name, mtime: e.mtime })
}
}
} else {
dav.scanErrors++
}
dav.nextScanFolder()
})
}
function readNextScanFile() {
if (!scanning) return
if (scanFiles.length === 0) {
scanning = false
scanFinished(scanRead, scanErrors)
return
}
var f = scanFiles.shift()
request("GET", fullUrl(f.path), "", function (xhr) {
if (!dav.scanning) return
if (xhr.status >= 200 && xhr.status < 300) {
dav.scanRead++
dav.scanFile(f.path, xhr.responseText)
} else {
dav.scanErrors++
}
dav.scanProgress(dav.scanRead, dav.scanRead + dav.scanFiles.length)
dav.readNextScanFile()
})
}
function statusMessage(status) {
if (status === 401) return qsTr("Invalid credentials (401)")
if (status === 403) return qsTr("Access denied (403)")
if (status === 404) return qsTr("Folder not found (404) — check the URL")
if (status === 405) return qsTr("WebDAV not available (405) — the URL must point to a DAV folder")
if (status === 0) return qsTr("Server unreachable")
if (status >= 500) return qsTr("Server error (%1)").arg(status)
return qsTr("HTTP error %1").arg(status)
}
}