Integrated file selection dialogue with the main code. Improved the
[openvpnui.git] / qml / filebrowse / pages / functions.js
1
2 // Go to root using the optional operationType parameter
3 // @param operationType PageStackAction.Immediate or Animated, Animated is default)
4 function goToRoot(operationType)
5 {
6 if (operationType !== PageStackAction.Immediate &&
7 operationType !== PageStackAction.Animated)
8 operationType = PageStackAction.Animated;
9
10 // find the first page
11 var firstPage = pageStack.previousPage();
12 if (!firstPage)
13 return;
14 while (pageStack.previousPage(firstPage)) {
15 firstPage = pageStack.previousPage(firstPage);
16 }
17
18 var start = engine.startDepth();
19 for (var up = 0; up < start; up++) {
20 firstPage = pageStack.nextPage(firstPage);
21 }
22
23 // pop to first page
24 pageStack.pop(firstPage, operationType);
25 }
26
27 function cancel()
28 {
29 // find the first page
30 var firstPage = pageStack.previousPage();
31 if (!firstPage)
32 return;
33 while (pageStack.previousPage(firstPage)) {
34 firstPage = pageStack.previousPage(firstPage);
35 }
36
37 var start = engine.startDepth() - 1;
38 for (var up = 0; up < start; up++) {
39 firstPage = pageStack.nextPage(firstPage);
40 }
41
42 // pop to first page
43 pageStack.pop(firstPage, PageStackAction.Animated);
44 }
45
46 function fileSelect(fullPath)
47 {
48 // set the selected file
49 engine.setSelectedFilename(fullPath);
50
51 // find the first page
52 var firstPage = pageStack.previousPage();
53 if (!firstPage)
54 return;
55 while (pageStack.previousPage(firstPage)) {
56 firstPage = pageStack.previousPage(firstPage);
57 }
58
59 var start = engine.startDepth() - 1;
60 for (var up = 0; up < start; up++) {
61 firstPage = pageStack.nextPage(firstPage);
62 }
63
64 // pop to first page
65 pageStack.pop(firstPage, PageStackAction.Animated);
66 }
67
68 // returns true if string s1 starts with string s2
69 function startsWith(s1, s2)
70 {
71 if (!s1 || !s2)
72 return false;
73
74 var start = s1.substring(0, s2.length);
75 return start === s2;
76 }
77
78 function goToFolder(folder)
79 {
80 // first, go to root so that the page stack has only one page
81 goToRoot(PageStackAction.Immediate);
82
83 // open the folders one by one
84 var dirs = folder.split("/");
85 var path = "";
86 for (var i = 1; i < dirs.length; ++i) {
87 path += "/"+dirs[i];
88 // animate the last push
89 var action = (i < dirs.length-1) ? PageStackAction.Immediate : PageStackAction.Animated;
90 pageStack.push(Qt.resolvedUrl("DirectoryPage.qml"), { dir: path }, action);
91 }
92 }
93
94 // Goes to Home folder
95 function goToHome()
96 {
97 goToFolder(engine.homeFolder());
98 }
99
100 function goToInitial(folder, filter)
101 {
102 engine.extensionFilter = filter;
103
104 // open the folders one by one
105 var dirs = folder.split("/");
106 var path = "";
107 for (var i = 0; i < dirs.length; ++i) {
108 path += "/"+dirs[i];
109 // animate the last push
110 var action = (i < dirs.length-1) ? PageStackAction.Immediate : PageStackAction.Animated;
111 pageStack.push(Qt.resolvedUrl("DirectoryPage.qml"), { dir: path }, action);
112 }
113 }
114
115 function sdcardPath()
116 {
117 return "/run/user/100000/media/sdcard";
118 }
119
120 function androidSdcardPath()
121 {
122 return "/data/sdcard";
123 }
124
125 function formatPathForTitle(path)
126 {
127 if (path === "/")
128 return "File Browser: /";
129
130 var i = path.lastIndexOf("/");
131 if (i < -1)
132 return path;
133
134 return path.substring(i+1)+"/";
135 }
136
137 // returns the text after the last / in a path
138 function lastPartOfPath(path)
139 {
140 if (path === "/")
141 return "";
142
143 var i = path.lastIndexOf("/");
144 if (i < -1)
145 return path;
146
147 return path.substring(i+1);
148 }
149
150 function formatPathForSearch(path)
151 {
152 if (path === "/")
153 return "root";
154
155 var i = path.lastIndexOf("/");
156 if (i < -1)
157 return path;
158
159 return path.substring(i+1);
160 }
161
162 function unicodeArrow()
163 {
164 return "\u2192"; // unicode for arrow symbol
165 }
166
167 function unicodeBlackDownPointingTriangle()
168 {
169 return "\u25bc"; // unicode for down pointing triangle symbol
170 }
171
172 function folderFromFile(path)
173 {
174 if ((path === "Select") || (path === ""))
175 path = engine.homeFolder() + "/.";
176
177 var i = path.lastIndexOf("/");
178 if ((i < 0) || (i >= (path.length - 1)))
179 return path;
180
181 return path.substring(0, i);
182 }