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) // Senza questo filtro la cartella appena aperta compare come // prima voce di se stessa (Depth:1 la include) e .obsidian/ // .trash sporcano la navigazione. listReady(Prop.listing(entries, relPath || "")) } 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.listing(Prop.parse(xhr.responseText, appSettings.serverUrl), folder) 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) } }