| 1 |
/* |
| 2 |
* Copyright (C) 2013 Canonical Ltd |
| 3 |
* Copyright (C) 2021 UBports Foundation |
| 4 |
* |
| 5 |
* This program is free software: you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License version 3 as |
| 7 |
* published by the Free Software Foundation. |
| 8 |
* |
| 9 |
* This program is distributed in the hope that it will be useful, |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
* GNU General Public License for more details. |
| 13 |
* |
| 14 |
* You should have received a copy of the GNU General Public License |
| 15 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 |
* |
| 17 |
* Authored by: Arto Jalkanen <[email protected]> |
| 18 |
*/ |
| 19 |
import QtQuick 2.4 |
| 20 |
import Lomiri.Components 1.3 |
| 21 |
import Lomiri.Components.Popups 1.3 |
| 22 |
import Lomiri.Components.Extras.PamAuthentication 0.1 |
| 23 |
import Qt.labs.settings 1.0 |
| 24 |
import Lomiri.Content 1.3 |
| 25 |
import QtQuick.Window 2.12 |
| 26 |
|
| 27 |
// This makes the Lomiri Thumbnailer available in all the other QML documents. |
| 28 |
import Lomiri.Thumbnailer 0.1 |
| 29 |
|
| 30 |
import "ui" |
| 31 |
import "backend" as Backend |
| 32 |
import "authentication" |
| 33 |
|
| 34 |
MainView { |
| 35 |
id: mainView |
| 36 |
// objectName for functional testing purposes (autopilot-qt5) |
| 37 |
objectName: "filemanager" |
| 38 |
applicationName: "filemanager.ubports" |
| 39 |
|
| 40 |
width: phone ? units.gu(40) : units.gu(100) |
| 41 |
height: units.gu(75) |
| 42 |
|
| 43 |
theme.name: { |
| 44 |
if (globalSettings.theme == 1) { |
| 45 |
return "Lomiri.Components.Themes.Ambiance"; |
| 46 |
} else if (globalSettings.theme == 2) { |
| 47 |
return "Lomiri.Components.Themes.SuruDark"; |
| 48 |
} else { |
| 49 |
return ""; |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
// screenPhysicalWidth & screenPhysicalHeight are from main.cpp and in millimeters |
| 54 |
readonly property real screenDiagonal: Math.sqrt(Math.pow(screenPhysicalWidth, 2) + Math.pow(screenPhysicalHeight, 2)) / 25.4 |
| 55 |
readonly property bool isLargeDisplay: screenDiagonal >= 12 // >= 10 inches |
| 56 |
|
| 57 |
property bool wideAspect: width > units.gu(80) |
| 58 |
property bool tallAspect: height > units.gu(60) |
| 59 |
|
| 60 |
property bool fullAccessGranted: noAuthentication || !authentication.requireAuthentication || !globalSettings.requireAuthenticationForFullAccess |
| 61 |
property bool isContentHub: false |
| 62 |
property bool importMode: true |
| 63 |
|
| 64 |
// This is used when it invokes folderListModel.setPathWithAuthentication() |
| 65 |
// We store user's preference only for the current instance of FM |
| 66 |
property bool sambaSavePassword: true |
| 67 |
|
| 68 |
QtObject { |
| 69 |
id: fileSelector |
| 70 |
property var activeTransfer: null |
| 71 |
property var fileSelectorComponent: null |
| 72 |
} |
| 73 |
|
| 74 |
Component { |
| 75 |
id: fileSelectorResultComponent |
| 76 |
ContentItem {} |
| 77 |
} |
| 78 |
|
| 79 |
AuthenticationHandler { |
| 80 |
id: authentication |
| 81 |
serviceName: mainView.applicationName |
| 82 |
} |
| 83 |
|
| 84 |
property var pageStack: pageStack |
| 85 |
|
| 86 |
function openFileSelector(selectFolderMode) { |
| 87 |
|
| 88 |
var props = { |
| 89 |
fileSelectorMode: !selectFolderMode, |
| 90 |
folderSelectorMode: selectFolderMode |
| 91 |
} |
| 92 |
fileSelector.fileSelectorComponent = pageStack.push(Qt.resolvedUrl("./ui/FolderListPage.qml"), props) |
| 93 |
} |
| 94 |
|
| 95 |
function cancelFileSelector() { |
| 96 |
console.log("Cancel file selector") |
| 97 |
exitFileSelectorMode() |
| 98 |
fileSelector.fileSelectorComponent = null |
| 99 |
fileSelector.activeTransfer.state = ContentTransfer.Aborted |
| 100 |
} |
| 101 |
|
| 102 |
function acceptFileSelector(fileUrls) { |
| 103 |
console.log("accept file selector " + fileUrls) |
| 104 |
if (importMode) { |
| 105 |
importFiles(fileSelector.activeTransfer, fileUrls[0]) |
| 106 |
} else { |
| 107 |
exportFiles(fileSelector.activeTransfer, fileUrls) |
| 108 |
} |
| 109 |
|
| 110 |
exitFileSelectorMode() |
| 111 |
} |
| 112 |
|
| 113 |
function exitFileSelectorMode() { |
| 114 |
isContentHub = false |
| 115 |
importMode = true |
| 116 |
pageStack.pop() |
| 117 |
} |
| 118 |
|
| 119 |
function openLocalFile(filePath, share) { |
| 120 |
pageStack.push(Qt.resolvedUrl("content-hub/FileOpener.qml"), { fileUrl: "file://" + filePath, share: share} ) |
| 121 |
} |
| 122 |
|
| 123 |
function startTransfer(activeTransfer, iMode) { |
| 124 |
if (activeTransfer.state === ContentTransfer.Charged || !iMode) { |
| 125 |
fileSelector.activeTransfer = activeTransfer |
| 126 |
isContentHub = true |
| 127 |
importMode = iMode |
| 128 |
openFileSelector(iMode) |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
function importFiles(activeTransfer, destDir) { |
| 133 |
var succeededFileNames = [] |
| 134 |
var failedFileNames = [] |
| 135 |
var existingFileNames = [] |
| 136 |
for(var i=0; i < activeTransfer.items.length; i++) { |
| 137 |
var item = activeTransfer.items[i] |
| 138 |
var destFilename = FmUtils.basename(String(item.url)) |
| 139 |
console.log("Move file to:" + destDir + " with name: " + destFilename) |
| 140 |
|
| 141 |
if(FmUtils.exists(destDir + "/" + destFilename)) { |
| 142 |
console.log("detected existing file: " + destFilename) |
| 143 |
existingFileNames.push(destFilename) |
| 144 |
} |
| 145 |
|
| 146 |
if(activeTransfer.items[i].move(destDir, destFilename)) |
| 147 |
succeededFileNames.push(destFilename) |
| 148 |
else |
| 149 |
failedFileNames.push(destFilename) |
| 150 |
} |
| 151 |
finishImport(destDir, succeededFileNames, failedFileNames, existingFileNames) |
| 152 |
} |
| 153 |
|
| 154 |
function exportFiles(activeTransfer, filesUrls) { |
| 155 |
var results = filesUrls.map(function(fileUrl) { |
| 156 |
return fileSelectorResultComponent.createObject(mainView, {"url": fileUrl}) |
| 157 |
}) |
| 158 |
|
| 159 |
if (activeTransfer !== null) { |
| 160 |
activeTransfer.items = results |
| 161 |
activeTransfer.state = ContentTransfer.Charged |
| 162 |
console.log("set activeTransfer") |
| 163 |
} else { |
| 164 |
console.log("activeTransfer null, not setting, testing code") |
| •165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
Connections { |
| 170 |
target: ContentHub |
| 171 |
function onExportRequested(transfer) { |
| 172 |
startTransfer(transfer, false) |
| 173 |
} |
| 174 |
function onImportRequested(transfer) { |
| 175 |
startTransfer(transfer, true) |
| 176 |
} |
| 177 |
function onShareRequested(transfer) { |
| 178 |
startTransfer(transfer, true) |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
PageStack { |
| 183 |
id: pageStack |
| 184 |
} |
| 185 |
|
| 186 |
/* Settings Storage */ |
| 187 |
property QtObject globalSettings: Backend.GlobalSettings { } |
| 188 |
|
| 189 |
|
| 190 |
function error(title, message) { |
| 191 |
var props = { |
| 192 |
title: title, |
| 193 |
text: message |
| 194 |
} |
| 195 |
|
| 196 |
PopupUtils.open(Qt.resolvedUrl("dialogs/NotifyDialog.qml"), mainView, props) |
| 197 |
} |
| 198 |
|
| 199 |
function finishImport(folder, okUrls, errUrls, existingUrls) { |
| 200 |
var okCount = okUrls.length |
| 201 |
var errCount = errUrls.length |
| 202 |
var existingCount = existingUrls.length |
| 203 |
|
| 204 |
var msg = "" |
| 205 |
if(okCount > 0) |
| 206 |
msg += i18n.tr("successfully saved: ") + i18n.tr("%1 file", "%1 files", okCount).arg(okCount) |
| 207 |
msg += msg.length > 0 ? "<br><br>\n" : "" |
| 208 |
if(errCount > 0) |
| 209 |
msg += i18n.tr("failed to save: ") + i18n.tr("%1 file", "%1 files", errCount).arg(errCount) |
| 210 |
msg += msg.length > 0 ? "<br>\n" : "" |
| 211 |
msg += i18n.tr("into: %1").arg(folder) |
| 212 |
|
| 213 |
if(existingCount > 0) { |
| 214 |
msg += msg.length > 0 ? "<br><br>\n" : "" |
| 215 |
msg += i18n.tr("Warning: Content-Hub does not overwrite existing files. The following were detected:") |
| 216 |
for(var i=0;i<existingCount;i++) |
| 217 |
msg += "<br>\n" + existingUrls[i] |
| 218 |
} |
| 219 |
|
| 220 |
fileSelector.fileSelectorComponent = null |
| 221 |
pageStack.currentPage.folderModel.path = folder |
| 222 |
pageStack.currentPage.folderModel.refresh() |
| 223 |
|
| 224 |
var props = { |
| 225 |
title: i18n.tr("Import Results"), |
| 226 |
text: msg |
| 227 |
} |
| 228 |
|
| 229 |
var popup = PopupUtils.open(Qt.resolvedUrl("dialogs/NotifyDialog.qml"), mainView, props) |
| 230 |
popup.closed.connect(function() { |
| 231 |
fileSelector.activeTransfer.state = ContentTransfer.Aborted |
| 232 |
}) |
| 233 |
} |
| 234 |
|
| 235 |
function getUrl(args) { |
| 236 |
for (var i = 0; i < args.length; i++) { |
| 237 |
var url = args[i] |
| 238 |
if (url.startsWith("file://")) { |
| 239 |
// any fileName specified ? |
| 240 |
const file = url.replace(/^.*[\\\/]/, ''); |
| 241 |
if (file.indexOf('.') > 0) { |
| 242 |
return url.substring(0, url.lastIndexOf("/")); |
| 243 |
} else { |
| 244 |
return url |
| 245 |
} |
| 246 |
} |
| 247 |
} |
| •248 |
|
| 249 |
return undefined |
| 250 |
} |
| 251 |
|
| 252 |
Connections { |
| 253 |
target: UriHandler |
| 254 |
function onOpened(uris) { |
| 255 |
console.log('Open from UriHandler') |
| 256 |
const url = getUrl(uris) |
| 257 |
if (url) { |
| 258 |
console.log("forwarding URL: " + url); |
| 259 |
pageStack.currentPage.folderModel.goTo(url) |
| 260 |
} |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
Component.onCompleted: { |
| 265 |
i18n.domain = "lomiri-filemanager-app"; |
| 266 |
i18n.bindtextdomain("lomiri-filemanager-app", i18nDirectory) |
| •267 |
LomiriApplication.applicationName = i18n.tr("File Manager") |
| 268 |
|
| 269 |
let path = "" |
| 270 |
if (Qt.application.arguments.length > 0) { |
| 271 |
const url = getUrl(Qt.application.arguments) |
| 272 |
if (url) { |
| 273 |
path = url |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
pageStack.push(Qt.resolvedUrl("ui/FolderListPage.qml"), { location: path }) |
| 278 |
} |
| 279 |
} |