Integrated file selection dialogue with the main code. Improved the
[openvpnui.git] / qml / filebrowse / pages / DirectoryPage.qml
diff --git a/qml/filebrowse/pages/DirectoryPage.qml b/qml/filebrowse/pages/DirectoryPage.qml
new file mode 100644 (file)
index 0000000..eb55286
--- /dev/null
@@ -0,0 +1,237 @@
+import QtQuick 2.0
+import Sailfish.Silica 1.0
+import harbour.file.browser.FileModel 1.0
+import "functions.js" as Functions
+import "../components"
+
+Page {
+    id: page
+    allowedOrientations: Orientation.All
+    property string dir: "/"
+    property string initialDir: ""
+    property bool initial: false // this is set to true if the page is initial page
+    property int _selectedMenu: 0
+
+    FileModel {
+        id: fileModel
+        dir: page.dir
+        // page.status does not exactly work - root folder seems to be active always??
+        active: page.status === PageStatus.Active
+    }
+
+    SilicaListView {
+        id: fileList
+        anchors.fill: parent
+
+        model: fileModel
+
+        VerticalScrollDecorator { flickable: fileList }
+
+        PullDownMenu {
+            id: pullMenu;
+
+            onActiveChanged: {
+                switch (_selectedMenu) {
+                    case 1:
+                        Functions.cancel()
+                        break;
+                    case 2:
+                        pageStack.push(Qt.resolvedUrl("SearchPage.qml"), { dir: page.dir });
+                        break;
+                    case 3:
+                        fileModel.showAll = true
+                        menuShowAll.visible = false
+                        menuShowFiltered.visible = true
+                        break;
+                    case 4:
+                        fileModel.showAll = false
+                        menuShowFiltered.visible = false
+                        menuShowAll.visible = true
+                        break;
+                }
+                _selectedMenu = 0
+            }
+
+            MenuItem {
+                text: qsTr("Cancel")
+                onClicked: _selectedMenu = 1
+            }
+            MenuItem {
+                text: qsTr("Search")
+                onClicked: _selectedMenu = 2
+            }
+            MenuItem {
+                id: menuShowAll
+                visible: !fileModel.showAll
+                text: qsTr("Show all files")
+                onClicked: _selectedMenu = 3
+            }
+            MenuItem {
+                id: menuShowFiltered
+                visible: fileModel.showAll
+                text: qsTr("Show only ") + engine.extensionFilter + qsTr(" files")
+                onClicked: _selectedMenu = 4
+            }
+        }
+
+        header: PageHeader {
+            title: Functions.formatPathForTitle(page.dir) + " " +
+                   Functions.unicodeBlackDownPointingTriangle()
+            MouseArea {
+                anchors.fill: parent
+                onClicked: dirPopup.show();
+            }
+        }
+
+        delegate: ListItem {
+            id: fileItem
+            menu: contextMenu
+            width: ListView.view.width
+            contentHeight: listLabel.height+listSize.height + 13
+
+            Image {
+                id: listIcon
+                anchors.left: parent.left
+                anchors.leftMargin: Theme.paddingLarge
+                anchors.top: parent.top
+                anchors.topMargin: 11
+                source: "../images/small-"+fileIcon+".png"
+            }
+            Label {
+                id: listLabel
+                anchors.left: listIcon.right
+                anchors.leftMargin: 10
+                anchors.right: parent.right
+                anchors.rightMargin: Theme.paddingLarge
+                anchors.top: parent.top
+                anchors.topMargin: 5
+                text: filename
+                elide: Text.ElideRight
+            }
+            Label {
+                id: listSize
+                anchors.left: listIcon.right
+                anchors.leftMargin: 10
+                anchors.top: listLabel.bottom
+                text: !(isLink && isDir) ? size : Functions.unicodeArrow()+" "+symLinkTarget
+                color: Theme.secondaryColor
+                font.pixelSize: Theme.fontSizeExtraSmall
+            }
+            Label {
+                visible: !(isLink && isDir)
+                anchors.top: listLabel.bottom
+                anchors.horizontalCenter: parent.horizontalCenter
+                text: filekind+permissions
+                color: Theme.secondaryColor
+                font.pixelSize: Theme.fontSizeExtraSmall
+            }
+            Label {
+                visible: !(isLink && isDir)
+                anchors.top: listLabel.bottom
+                anchors.right: listLabel.right
+                text: modified
+                color: Theme.secondaryColor
+                font.pixelSize: Theme.fontSizeExtraSmall
+            }
+
+            onClicked: {
+                if (model.isDir) {
+                    pageStack.push(Qt.resolvedUrl("DirectoryPage.qml"),
+                                   { dir: fileModel.appendPath(listLabel.text) });
+                }
+                else {
+                    Functions.cancel()
+                    Functions.fileSelect(fileModel.appendPath(listLabel.text))
+                    //pageStack.push(Qt.resolvedUrl("FilePage.qml"),
+                    //               { file: fileModel.appendPath(listLabel.text) });
+                }
+            }
+
+            // delete file after remorse time
+            ListView.onRemove: animateRemoval(fileItem)
+            function deleteFile(deleteFilename) {
+                remorseAction(qsTr("Deleting"), function() {
+                    progressPanel.showText(qsTr("Deleting"));
+                    engine.deleteFiles([ deleteFilename ]);
+                }, 5000)
+            }
+
+            // context menu is activated with long press
+            Component {
+                 id: contextMenu
+                 ContextMenu {
+                     MenuItem {
+                         visible: true
+                         text: qsTr("Properties")
+                         onClicked:  {
+                             pageStack.push(Qt.resolvedUrl("FilePage.qml"),
+                                            { file: fileModel.fileNameAt(index) });
+                         }
+                     }
+                 }
+             }
+        }
+
+        // text if no files or error message
+        Text {
+            width: parent.width
+            anchors.leftMargin: Theme.paddingLarge
+            anchors.rightMargin: Theme.paddingLarge
+            horizontalAlignment: Qt.AlignHCenter
+            y: -fileList.contentY + 100
+            visible: fileModel.fileCount === 0 || fileModel.errorMessage !== ""
+            text: fileModel.errorMessage !== "" ? fileModel.errorMessage : (fileModel.showAll ? qsTr("No files") : qsTr("No key files"))
+            color: Theme.highlightColor
+        }
+    }
+
+    // update cover
+    onStatusChanged: {
+        if (status === PageStatus.Activating) {
+            // go to Home on startup
+            if (page.initial) {
+                page.initial = false;
+                Functions.goToInitial(dir);
+            }
+        }
+    }
+
+    DirPopup {
+        id: dirPopup
+        anchors.fill: parent
+        menuTop: 100
+    }
+
+    // connect signals from engine to panels
+    Connections {
+        target: engine
+        onProgressChanged: progressPanel.text = engine.progressFilename
+        onWorkerDone: progressPanel.hide()
+        onWorkerErrorOccurred: {
+            // the error signal goes to all pages in pagestack, show it only in the active one
+            if (progressPanel.open) {
+                progressPanel.hide();
+                if (message === "Unknown error")
+                    filename = qsTr("Trying to move between phone and SD Card? It doesn't work, try copying.");
+                else if (message === "Failure to write block")
+                    filename = qsTr("Perhaps the storage is full?");
+
+                notificationPanel.showText(message, filename);
+            }
+        }
+    }
+
+    NotificationPanel {
+        id: notificationPanel
+        page: page
+    }
+
+    ProgressPanel {
+        id: progressPanel
+        page: page
+        onCancelled: engine.cancel()
+    }
+
+}
+
+