Integrated file selection dialogue with the main code. Improved the
[openvpnui.git] / qml / filebrowse / components / DirPopup.qml
1 import QtQuick 2.0
2 import Sailfish.Silica 1.0
3 import "../pages/functions.js" as Functions
4
5 // This component displays a list of dir options on top of a page
6 Item {
7     id: item
8     property int menuTop: 100
9
10     property int _selectedMenu: 0
11     property Item _contextMenu
12
13     function show()
14     {
15         if (!_contextMenu)
16             _contextMenu = contextMenuComponent.createObject(rect);
17         _selectedMenu = 0;
18
19         // update spaces
20         var rootSpace = engine.diskSpace("/");
21         if (rootSpace.length > 0) {
22             _contextMenu.rootSpaceText = qsTr("Root (%1)").arg(rootSpace[0]);
23             _contextMenu.rootSpaceSubtext = rootSpace[1];
24         } else {
25             _contextMenu.rootSpaceText = qsTr("Root");
26             _contextMenu.rootSpaceSubtext = "";
27         }
28
29         var sdCardSpace = engine.diskSpace(Functions.sdcardPath());
30         if (sdCardSpace.length > 0) {
31             _contextMenu.sdCardSpaceText = qsTr("SD Card (%1)").arg(sdCardSpace[0]);
32             _contextMenu.sdCardSpaceSubtext = sdCardSpace[1];
33         } else {
34             _contextMenu.sdCardSpaceText = qsTr("SD Card");
35             _contextMenu.sdCardSpaceSubtext = "";
36         }
37
38         _contextMenu.show(rect);
39     }
40
41     Column {
42         anchors.fill: parent
43
44         Item {
45             id: spacer
46             width: parent.width
47             height: menuTop
48         }
49         // bg rectangle for context menu so it covers underlying items
50         Rectangle {
51             id: rect
52             color: "black"
53             width: parent.width
54             height: _contextMenu ? _contextMenu.height : 0
55         }
56     }
57
58     Component {
59         id: contextMenuComponent
60         ContextMenu {
61
62             property string sdCardSpaceText: ""
63             property string sdCardSpaceSubtext: ""
64             property string rootSpaceText: ""
65             property string rootSpaceSubtext: ""
66
67             // delayed action so that menu has already closed when page transition happens
68             onClosed: {
69                 if (_selectedMenu == 1) {
70                     Functions.goToHome();
71
72                 } else if (_selectedMenu == 2) {
73                     var sdcard = Functions.sdcardPath();
74                     if (engine.exists(sdcard)) {
75                         Functions.goToFolder(sdcard);
76                     } else {
77                         // this assumes that the page has a notificationPanel
78                         notificationPanel.showText(qsTr("SD Card not found"), sdcard);
79                     }
80
81                 } else if (_selectedMenu == 3) {
82                     var androidSdcard = Functions.androidSdcardPath();
83                     if (engine.exists(androidSdcard)) {
84                         Functions.goToFolder(androidSdcard);
85                     } else {
86                         // this assumes that the page has a notificationPanel
87                         notificationPanel.showText(qsTr("Android Storage not found"), androidSdcard);
88                     }
89
90                 } else if (_selectedMenu == 4) {
91                     Functions.goToRoot();
92                 }
93                 _selectedMenu = 0;
94             }
95
96             MenuItem {
97                 text: qsTr("Home")
98                 onClicked: _selectedMenu = 1
99             }
100             DoubleMenuItem {
101                 text: sdCardSpaceText
102                 subtext: sdCardSpaceSubtext
103                 onClicked: _selectedMenu = 2
104             }
105             MenuItem {
106                 text: qsTr("Android Storage")
107                 onClicked: _selectedMenu = 3
108             }
109             DoubleMenuItem {
110                 text: rootSpaceText
111                 subtext: rootSpaceSubtext
112                 onClicked: _selectedMenu = 4
113             }
114         }
115     }
116
117 }