import QtQuick 2.2 import Sailfish.Silica 1.0 import "../components/Markdown.js" as Markdown import "../components" // Search across the TEXT of the notes (WebDAV cannot do it: the folder is // walked and the .md files are downloaded one at a time). // // The scan runs once, when the page opens; typing afterwards filters what is // already in memory, so the field stays responsive. Notes are kept in memory up // to a budget: past it a note is still listed (and searchable by title) but its // text is not held, and a banner says so. // // With tagFilter set the page becomes a tag listing: no search field, only the // notes that carry that tag. Page { id: page property var dav property string rootPath: "" // folder to scan ("" = whole vault root) property string tagFilter: "" // when set: notes carrying this tag property string term: "" property bool scanning: false property int total: 0 property int read: 0 property int errors: 0 property int matched: 0 property var notes: [] property real storedBytes: 0 property real maxStoredBytes: 16777216 // 16 MB of note text property bool truncated: false property string coverText: tagFilter !== "" ? "#" + tagFilter : qsTr("Search") ListModel { id: results } onTermChanged: refresh() Component.onCompleted: start() function start() { if (!dav) return results.clear() notes = [] storedBytes = 0 truncated = false matched = 0 total = 0 read = 0 errors = 0 scanning = true dav.scanVault(rootPath) } function addNote(notePath, content) { var name = notePath.substring(notePath.lastIndexOf("/") + 1) var text = content || "" if (storedBytes + text.length > maxStoredBytes) { text = "" truncated = true } else { storedBytes += text.length } notes.push({ path: notePath, name: name, content: text }) } // Rebuild the result list from what is in memory. function refresh() { results.clear() matched = 0 var t = term.trim().toLowerCase() for (var i = 0; i < notes.length; i++) { var n = notes[i] var hitTitle = false var hit = null if (tagFilter !== "") { if (!n.content) continue if (!Markdown.hasTag(n.content, tagFilter)) continue } else { if (t === "") { // No query yet: the whole scanned tree, folder paths // included, so the page doubles as an index of the vault. } else { hitTitle = n.name.toLowerCase().indexOf(t) >= 0 hit = n.content ? Markdown.searchText(n.content, t) : null if (!hitTitle && !hit) continue } } results.append({ name: n.name, path: n.path, snippet: hit ? hit.snippet : "" }) matched++ if (matched >= 500) break } } Connections { target: page.dav onScanStart: { page.total = count page.scanning = count > 0 } onScanProgress: { page.read = done page.total = all } onScanFile: page.addNote(path, content) onScanFinished: { page.read = done page.errors = errs page.scanning = false page.refresh() if (page.truncated) banner.show(qsTr("Only the first notes are kept in memory"), true) } onFailed: banner.show(message, true) } SilicaListView { id: listView anchors.fill: parent model: results header: Column { width: parent.width PageHeader { title: page.tagFilter !== "" ? "#" + page.tagFilter : qsTr("Search in the text") } SearchField { visible: page.tagFilter === "" width: parent.width placeholderText: qsTr("Search the notes") text: page.term onTextChanged: { if (text !== page.term) page.term = text } } Label { visible: page.scanning x: Theme.paddingLarge width: parent.width - 2 * Theme.paddingLarge text: page.total > 0 ? qsTr("Reading notes: %1 of %2").arg(page.read).arg(page.total) : qsTr("Looking for notes...") color: Theme.secondaryColor font.pixelSize: Theme.fontSizeSmall } Label { visible: !page.scanning x: Theme.paddingLarge width: parent.width - 2 * Theme.paddingLarge text: (page.matched === 1 ? qsTr("1 note") : qsTr("%1 notes").arg(page.matched)) + (page.errors > 0 ? qsTr(" ยท %1 unreadable").arg(page.errors) : "") color: Theme.secondaryColor font.pixelSize: Theme.fontSizeSmall } } ViewPlaceholder { enabled: !page.scanning && results.count === 0 text: page.tagFilter !== "" ? qsTr("No note with this tag") : qsTr("No note contains this text") } delegate: ListItem { id: resultItem contentHeight: model.snippet !== "" ? Theme.itemSizeLarge : Theme.itemSizeMedium onClicked: pageStack.push(Qt.resolvedUrl("EditorPage.qml"), { dav: page.dav, notePath: model.path, noteName: model.name }) Column { anchors.verticalCenter: parent.verticalCenter x: Theme.paddingLarge width: parent.width - 2 * Theme.paddingLarge Label { text: model.name font.pixelSize: Theme.fontSizeMedium truncationMode: TruncationMode.Fade } Label { visible: model.snippet !== "" text: model.snippet color: Theme.secondaryColor font.pixelSize: Theme.fontSizeExtraSmall maximumLineCount: 2 truncationMode: TruncationMode.Fade } } } VerticalScrollDecorator { id: decorator } PullDownMenu { MenuItem { text: qsTr("Search again") onClicked: page.start() } MenuItem { visible: page.dav && page.dav.scanning text: qsTr("Stop") onClicked: page.dav.cancelScan() } } BusyIndicator { anchors.centerIn: parent size: BusyIndicatorSize.Large running: page.scanning } } onStatusChanged: { // Leaving the page must not leave a scan running in the background: // it would keep the network busy for the next screen. if (status === PageStatus.Inactive && page.dav && page.dav.scanning) page.dav.cancelScan() } Banner { id: banner } }