mailmodel 0.1.5

A proof-of-concept mail viewer writting with Qt using QML
import QtQuick 2.7
import QtQuick.Controls 1.4
import QtQuick.Controls.Styles 1.4
import QtQuick.Layouts 1.2
import QtQuick.Window 2.2
import RustCode 1.0

Window {
    visible: true
    width: 1024
    height: 800
    title: qsTr("Mail Viewer")

    MailModel {
        id: mailmodel
        configFile: mailConfigFile
    }

    SystemPalette {
        id: palette
        colorGroup: SystemPalette.Active
    }

    FontMetrics {
        id: systemFont
    }

    ColumnLayout {
        anchors.fill: parent
        SplitView {
            Layout.fillHeight: true
            Layout.fillWidth: true
            handleDelegate: Rectangle {
                width: 3
                color: palette.window
            }
            TreeView {
                id: folders
                headerVisible: false
                frameVisible: false
                TableViewColumn {
                    title: "Name"
                    role: "name"
                    width: folders.width - 20
                    delegate: Text {
                        text: icon(styleData.value) + " " + styleData.value
                        verticalAlignment: Text.AlignVCenter
                    }
                }
                style: TreeViewStyle {
                    highlightedTextColor: palette.highlightedText
                    backgroundColor: palette.base
                    alternateBackgroundColor: palette.alternateBase
                }
                rowDelegate: Rectangle {
                    height: 2.5 * systemFont.font.pixelSize
                    color: styleData.selected
                               ? folders.activeFocus
                                   ? palette.highlight
                                   : Qt.lighter(palette.highlight, 1.5)
                               : rowMouse.containsMouse
                                   ? Qt.lighter(palette.highlight, 1.8)
                                   : palette.base
                    MouseArea {
                        id: rowMouse
                        anchors.fill: parent
                        hoverEnabled: true
                        acceptedButtons: Qt.NoButton
                    }
                }
                model: mailmodel.folders
                onCurrentIndexChanged: {
                    var parent = mailmodel.folders.parent(currentIndex);
                    var path = mailmodel.folders.name(currentIndex);
                    var delimiter = mailmodel.folders.delimiter(currentIndex);
                    while (parent.valid) {
                        path = mailmodel.folders.name(parent) + delimiter + path;
                        parent = mailmodel.folders.parent(parent);
                    }
                    path = path.substr(2);
                    mailmodel.currentFolder = path;
                }
            }
            ThreadsView {
                id: threads
                onCurrentUidChanged: if (currentUid !== undefined) {
                    mailmodel.setEmail(currentUid)
                }
                onFilterChanged: mailmodel.folder_threads.setFilter(filter)
            }
            Email {
            }
            Component.onCompleted: threads.width = threads.Layout.preferredWidth
        }
        Rectangle {
            visible: label.text
            color: "yellow"
            width: childrenRect.width
            height: childrenRect.height
            Label {
                id: label
                text: mailmodel.folder_threads.error || ""
            }
        }
    }
    function expand(treeview, index) {
        treeview.expand(index);
        var rowCount = treeview.model.rowCount(index);
        var row;
        for (row = 0; row < rowCount; ++row) {
            expand(treeview, treeview.model.index(row, 0, index));
        }
    }
    function expandRecursive(treeview, index) {
        treeview.expand(index);
        var rowCount = treeview.model.rowCount(index);
        var row;
        for (row = 0; row < rowCount; ++row) {
            expandRecursive(treeview, treeview.model.index(row, 0, index));
        }
    }
    function expandAll(treeview) {
        expandRecursive(treeview, treeview.rootIndex);
    }
    Component.onCompleted: {
        folders.model.modelReset.connect(function () {
            expandAll(folders);
        });
    }
    function icon(name) {
        if (name.toLowerCase() === "inbox") {
            return "📥";
        }
        if (name.toLowerCase() === "trash") {
            return "🗑";
        }
        if (name.toLowerCase() === "outbox") {
            return "📤";
        }
        return "📂";
    }
}