Debian Codegraph @

src/app/qml/backend/FolderListModel.qml

raw
1 /*
2 * This file is part of Liri.
3 *
4 * Copyright (C) 2015 Michael Spencer <[email protected]>
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 import QtQuick 2.4
21 import Lomiri.Components 1.3
22 import Lomiri.Components.Popups 1.3
23 import Lomiri.FileManager.Archives 0.1
24 import Lomiri.FileManager.folderlistmodel 1.0
25 import Lomiri.FileManager.PlacesModel 0.1
26
27 QtObject {
28 id: folderModel
29
30 property var primSelItem
31 property alias path: __model.path
32 property string title: FmUtils.pathName(path)
33 property string folder: FmUtils.pathName(path)
34
35 property alias count: __model.count
36
37 property alias busy: __model.awaitingResults
38 property alias canGoBack: __model.canGoBack
39 property alias canGoForward: __model.canGoForward
40 property bool canGoHome: __model.canGoHome
41 property bool canGoUp: __model.canGoUp
42
43 property bool showHiddenFiles: false
44 property string sortingMethod: "Name" // or "Date"
45 property bool sortAscending: true
46
47 property alias model: __model
48 property alias places: __places
49 property alias archives: __archives
50
51 function goTo(location) {
52 // This allows us to enter "~" as a shortcut to the home folder
53 // when entering a location on the Go To dialog
54 path = location.replace("~", places.locationHome)
55
56 refresh()
57 }
58
59 /* Go to the home directory */
60 function goHome() {
61 model.goHome()
62 path = model.path
63 }
64
65 /* Go to last folder visited */
66 function goBack() {
67 model.goBack()
68 path = model.path
69 }
70
71 /* Go to next folder visited */
72 function goForward() {
73 model.goForward()
74 path = model.path
75 }
76
77 /* Go up one directory */
78 function goUp() {
79 goTo(model.parentPath)
80 }
81
82 function refresh() {
83 model.refresh()
84 }
85
86 function getArchiveType(fileName) {
87 var archiveExtensions = ["zip", "tar", "tar.gz", "tar.bz2"]
88 for(var i in archiveExtensions) {
89 if (fileName.endsWith(archiveExtensions[i])){
90 return archiveExtensions[i];
91 }
92 }
93 return "";
94 }
95
96 function extractArchive(filePath, fileName, archiveType) {
97 console.log("Extract accepted for filePath, fileName", filePath, fileName)
98 PopupUtils.open(Qt.resolvedUrl("../dialogs/ExtractingDialog.qml"), mainView, { "fileName" : fileName, "archives": archives })
99 console.log("Extracting...")
100
101 var parentDirectory = filePath.substring(0, filePath.lastIndexOf("/"))
102 var fileNameWithoutExtension = fileName.substring(0, fileName.lastIndexOf(archiveType) - 1)
103 var extractDirectory = parentDirectory + "/" + fileNameWithoutExtension
104
105 // Add numbers if the directory already exist: myfile, myfile-1, myfile-2, etc.
106 while (folderModel.model.existsDir(extractDirectory)) {
107 var i = 0
108 while ("1234567890".indexOf(extractDirectory.charAt(extractDirectory.length - i - 1)) !== -1) {
109 i++
110 }
111 if (i === 0 || extractDirectory.charAt(extractDirectory.length - i - 1) !== "-") {
112 extractDirectory += "-1"
113 } else {
114 extractDirectory = extractDirectory.substring(0, extractDirectory.lastIndexOf("-") + 1) + (parseInt(extractDirectory.substring(extractDirectory.length - i)) + 1)
115 }
116 }
117
118 folderModel.model.mkdir(extractDirectory) // This is needed for the tar command as the given destination has to be an already existing directory
119
120 archives.extract(filePath, extractDirectory)
121 }
122
123 function setPathWithAuthentication(currentPath, currentUserName, currentPassword, savePassword) {
124 __model.setPathWithAuthentication(currentPath, currentUserName, currentPassword, savePassword)
125 }
126
127 // TODO: Set onlyAllowedPaths for restricted user accounts
128 property QtObject __model: FolderListModel {
129 id: __model
130
131 enableExternalFSWatcher: true
132
133 // Properties to emulate a model entry for use by FileDetailsPopover
134 property bool isDir: true
135 property string fileName: FmUtils.pathName(model.path)
136 property string filePath: model.path
137 property string fileSize: i18n.tr("%1 file", "%1 files", model.count).arg(model.count)
138 property bool isReadable: true
139 property bool isExecutable: true
140 property bool isWritable: true
141
142 property bool isCurAllowedPath: true
143
144 property var accessedDate
145 property var modifiedDate
146 property var creationDate
147
148 property string mimeTypeDescription: i18n.tr("Folder")
149 property string iconName: "folder"
150
151 onOnlyAllowedPathsChanged: __checkIfIsWritable()
152 onPathChanged: __checkIfIsWritable()
153
154 Component.onCompleted: {
155 // Add default allowed paths: XDG folders
156 addAllowedDirectory(places.locationDocuments)
157 addAllowedDirectory(places.locationDownloads)
158 addAllowedDirectory(places.locationMusic)
159 addAllowedDirectory(places.locationPictures)
160 addAllowedDirectory(places.locationVideos)
161
162 // Add default allowed paths: user's external media (i.e. "/media/USER/**")
163 addAllowedDirectory(places.userMountLocation)
164 }
165
166 function __checkIfIsWritable() {
167 if (model.path) {
168 model.isWritable = model.curPathIsWritable() && (!model.onlyAllowedPaths || model.isAllowedPath(path))
169 model.isCurAllowedPath = !model.onlyAllowedPaths || model.isAllowedPath(path)
170 model.accessedDate = model.curPathAccessedDate()
171 model.modifiedDate = model.curPathModifiedDate()
172 model.creationDate = model.curPathCreatedDate()
173 }
174 }
175 }
176
177 property QtObject __places: PlacesModel {
178 id: __places
179 }
180
181 property QtObject __archives: Archives {
182 id: __archives
183 }
184 }