Integrated file selection dialogue with the main code. Improved the
[openvpnui.git] / qml / filebrowse / pages / DirectoryPage.qml
1 import QtQuick 2.0
2 import Sailfish.Silica 1.0
3 import harbour.file.browser.FileModel 1.0
4 import "functions.js" as Functions
5 import "../components"
6
7 Page {
8     id: page
9     allowedOrientations: Orientation.All
10     property string dir: "/"
11     property string initialDir: ""
12     property bool initial: false // this is set to true if the page is initial page
13     property int _selectedMenu: 0
14
15     FileModel {
16         id: fileModel
17         dir: page.dir
18         // page.status does not exactly work - root folder seems to be active always??
19         active: page.status === PageStatus.Active
20     }
21
22     SilicaListView {
23         id: fileList
24         anchors.fill: parent
25
26         model: fileModel
27
28         VerticalScrollDecorator { flickable: fileList }
29
30         PullDownMenu {
31             id: pullMenu;
32
33             onActiveChanged: {
34                 switch (_selectedMenu) {
35                     case 1:
36                         Functions.cancel()
37                         break;
38                     case 2:
39                         pageStack.push(Qt.resolvedUrl("SearchPage.qml"), { dir: page.dir });
40                         break;
41                     case 3:
42                         fileModel.showAll = true
43                         menuShowAll.visible = false
44                         menuShowFiltered.visible = true
45                         break;
46                     case 4:
47                         fileModel.showAll = false
48                         menuShowFiltered.visible = false
49                         menuShowAll.visible = true
50                         break;
51                 }
52                 _selectedMenu = 0
53             }
54
55             MenuItem {
56                 text: qsTr("Cancel")
57                 onClicked: _selectedMenu = 1
58             }
59             MenuItem {
60                 text: qsTr("Search")
61                 onClicked: _selectedMenu = 2
62             }
63             MenuItem {
64                 id: menuShowAll
65                 visible: !fileModel.showAll
66                 text: qsTr("Show all files")
67                 onClicked: _selectedMenu = 3
68             }
69             MenuItem {
70                 id: menuShowFiltered
71                 visible: fileModel.showAll
72                 text: qsTr("Show only ") + engine.extensionFilter + qsTr(" files")
73                 onClicked: _selectedMenu = 4
74             }
75         }
76
77         header: PageHeader {
78             title: Functions.formatPathForTitle(page.dir) + " " +
79                    Functions.unicodeBlackDownPointingTriangle()
80             MouseArea {
81                 anchors.fill: parent
82                 onClicked: dirPopup.show();
83             }
84         }
85
86         delegate: ListItem {
87             id: fileItem
88             menu: contextMenu
89             width: ListView.view.width
90             contentHeight: listLabel.height+listSize.height + 13
91
92             Image {
93                 id: listIcon
94                 anchors.left: parent.left
95                 anchors.leftMargin: Theme.paddingLarge
96                 anchors.top: parent.top
97                 anchors.topMargin: 11
98                 source: "../images/small-"+fileIcon+".png"
99             }
100             Label {
101                 id: listLabel
102                 anchors.left: listIcon.right
103                 anchors.leftMargin: 10
104                 anchors.right: parent.right
105                 anchors.rightMargin: Theme.paddingLarge
106                 anchors.top: parent.top
107                 anchors.topMargin: 5
108                 text: filename
109                 elide: Text.ElideRight
110             }
111             Label {
112                 id: listSize
113                 anchors.left: listIcon.right
114                 anchors.leftMargin: 10
115                 anchors.top: listLabel.bottom
116                 text: !(isLink && isDir) ? size : Functions.unicodeArrow()+" "+symLinkTarget
117                 color: Theme.secondaryColor
118                 font.pixelSize: Theme.fontSizeExtraSmall
119             }
120             Label {
121                 visible: !(isLink && isDir)
122                 anchors.top: listLabel.bottom
123                 anchors.horizontalCenter: parent.horizontalCenter
124                 text: filekind+permissions
125                 color: Theme.secondaryColor
126                 font.pixelSize: Theme.fontSizeExtraSmall
127             }
128             Label {
129                 visible: !(isLink && isDir)
130                 anchors.top: listLabel.bottom
131                 anchors.right: listLabel.right
132                 text: modified
133                 color: Theme.secondaryColor
134                 font.pixelSize: Theme.fontSizeExtraSmall
135             }
136
137             onClicked: {
138                 if (model.isDir) {
139                     pageStack.push(Qt.resolvedUrl("DirectoryPage.qml"),
140                                    { dir: fileModel.appendPath(listLabel.text) });
141                 }
142                 else {
143                     Functions.cancel()
144                     Functions.fileSelect(fileModel.appendPath(listLabel.text))
145                     //pageStack.push(Qt.resolvedUrl("FilePage.qml"),
146                     //               { file: fileModel.appendPath(listLabel.text) });
147                 }
148             }
149
150             // delete file after remorse time
151             ListView.onRemove: animateRemoval(fileItem)
152             function deleteFile(deleteFilename) {
153                 remorseAction(qsTr("Deleting"), function() {
154                     progressPanel.showText(qsTr("Deleting"));
155                     engine.deleteFiles([ deleteFilename ]);
156                 }, 5000)
157             }
158
159             // context menu is activated with long press
160             Component {
161                  id: contextMenu
162                  ContextMenu {
163                      MenuItem {
164                          visible: true
165                          text: qsTr("Properties")
166                          onClicked:  {
167                              pageStack.push(Qt.resolvedUrl("FilePage.qml"),
168                                             { file: fileModel.fileNameAt(index) });
169                          }
170                      }
171                  }
172              }
173         }
174
175         // text if no files or error message
176         Text {
177             width: parent.width
178             anchors.leftMargin: Theme.paddingLarge
179             anchors.rightMargin: Theme.paddingLarge
180             horizontalAlignment: Qt.AlignHCenter
181             y: -fileList.contentY + 100
182             visible: fileModel.fileCount === 0 || fileModel.errorMessage !== ""
183             text: fileModel.errorMessage !== "" ? fileModel.errorMessage : (fileModel.showAll ? qsTr("No files") : qsTr("No key files"))
184             color: Theme.highlightColor
185         }
186     }
187
188     // update cover
189     onStatusChanged: {
190         if (status === PageStatus.Activating) {
191             // go to Home on startup
192             if (page.initial) {
193                 page.initial = false;
194                 Functions.goToInitial(dir);
195             }
196         }
197     }
198
199     DirPopup {
200         id: dirPopup
201         anchors.fill: parent
202         menuTop: 100
203     }
204
205     // connect signals from engine to panels
206     Connections {
207         target: engine
208         onProgressChanged: progressPanel.text = engine.progressFilename
209         onWorkerDone: progressPanel.hide()
210         onWorkerErrorOccurred: {
211             // the error signal goes to all pages in pagestack, show it only in the active one
212             if (progressPanel.open) {
213                 progressPanel.hide();
214                 if (message === "Unknown error")
215                     filename = qsTr("Trying to move between phone and SD Card? It doesn't work, try copying.");
216                 else if (message === "Failure to write block")
217                     filename = qsTr("Perhaps the storage is full?");
218
219                 notificationPanel.showText(message, filename);
220             }
221         }
222     }
223
224     NotificationPanel {
225         id: notificationPanel
226         page: page
227     }
228
229     ProgressPanel {
230         id: progressPanel
231         page: page
232         onCancelled: engine.cancel()
233     }
234
235 }
236
237