/*
* Copyright (C) 2013 Canonical Ltd
* Copyright (C) 2021 UBports Foundation
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*
* Authored by: Arto Jalkanen
*/
import QtQuick 2.4
import Lomiri.Components 1.3
import Lomiri.Components.Popups 1.3
import Lomiri.Components.Extras.PamAuthentication 0.1
import Qt.labs.settings 1.0
import Lomiri.Content 1.3
import QtQuick.Window 2.12
// This makes the Lomiri Thumbnailer available in all the other QML documents.
import Lomiri.Thumbnailer 0.1
import "ui"
import "backend" as Backend
import "authentication"
MainView {
id: mainView
// objectName for functional testing purposes (autopilot-qt5)
objectName: "filemanager"
applicationName: "filemanager.ubports"
width: phone ? units.gu(40) : units.gu(100)
height: units.gu(75)
theme.name: {
if (globalSettings.theme == 1) {
return "Lomiri.Components.Themes.Ambiance";
} else if (globalSettings.theme == 2) {
return "Lomiri.Components.Themes.SuruDark";
} else {
return "";
}
}
// screenPhysicalWidth & screenPhysicalHeight are from main.cpp and in millimeters
readonly property real screenDiagonal: Math.sqrt(Math.pow(screenPhysicalWidth, 2) + Math.pow(screenPhysicalHeight, 2)) / 25.4
readonly property bool isLargeDisplay: screenDiagonal >= 12 // >= 10 inches
property bool wideAspect: width > units.gu(80)
property bool tallAspect: height > units.gu(60)
property bool fullAccessGranted: noAuthentication || !authentication.requireAuthentication || !globalSettings.requireAuthenticationForFullAccess
property bool isContentHub: false
property bool importMode: true
// This is used when it invokes folderListModel.setPathWithAuthentication()
// We store user's preference only for the current instance of FM
property bool sambaSavePassword: true
QtObject {
id: fileSelector
property var activeTransfer: null
property var fileSelectorComponent: null
}
Component {
id: fileSelectorResultComponent
ContentItem {}
}
AuthenticationHandler {
id: authentication
serviceName: mainView.applicationName
}
property var pageStack: pageStack
function openFileSelector(selectFolderMode) {
var props = {
fileSelectorMode: !selectFolderMode,
folderSelectorMode: selectFolderMode
}
fileSelector.fileSelectorComponent = pageStack.push(Qt.resolvedUrl("./ui/FolderListPage.qml"), props)
}
function cancelFileSelector() {
console.log("Cancel file selector")
exitFileSelectorMode()
fileSelector.fileSelectorComponent = null
fileSelector.activeTransfer.state = ContentTransfer.Aborted
}
function acceptFileSelector(fileUrls) {
console.log("accept file selector " + fileUrls)
if (importMode) {
importFiles(fileSelector.activeTransfer, fileUrls[0])
} else {
exportFiles(fileSelector.activeTransfer, fileUrls)
}
exitFileSelectorMode()
}
function exitFileSelectorMode() {
isContentHub = false
importMode = true
pageStack.pop()
}
function openLocalFile(filePath, share) {
pageStack.push(Qt.resolvedUrl("content-hub/FileOpener.qml"), { fileUrl: "file://" + filePath, share: share} )
}
function startTransfer(activeTransfer, iMode) {
if (activeTransfer.state === ContentTransfer.Charged || !iMode) {
fileSelector.activeTransfer = activeTransfer
isContentHub = true
importMode = iMode
openFileSelector(iMode)
}
}
function importFiles(activeTransfer, destDir) {
var succeededFileNames = []
var failedFileNames = []
var existingFileNames = []
for(var i=0; i < activeTransfer.items.length; i++) {
var item = activeTransfer.items[i]
var destFilename = FmUtils.basename(String(item.url))
console.log("Move file to:" + destDir + " with name: " + destFilename)
if(FmUtils.exists(destDir + "/" + destFilename)) {
console.log("detected existing file: " + destFilename)
existingFileNames.push(destFilename)
}
if(activeTransfer.items[i].move(destDir, destFilename))
succeededFileNames.push(destFilename)
else
failedFileNames.push(destFilename)
}
finishImport(destDir, succeededFileNames, failedFileNames, existingFileNames)
}
function exportFiles(activeTransfer, filesUrls) {
var results = filesUrls.map(function(fileUrl) {
return fileSelectorResultComponent.createObject(mainView, {"url": fileUrl})
})
if (activeTransfer !== null) {
activeTransfer.items = results
activeTransfer.state = ContentTransfer.Charged
console.log("set activeTransfer")
} else {
console.log("activeTransfer null, not setting, testing code")
}
}
Connections {
target: ContentHub
function onExportRequested(transfer) {
startTransfer(transfer, false)
}
function onImportRequested(transfer) {
startTransfer(transfer, true)
}
function onShareRequested(transfer) {
startTransfer(transfer, true)
}
}
PageStack {
id: pageStack
}
/* Settings Storage */
property QtObject globalSettings: Backend.GlobalSettings { }
function error(title, message) {
var props = {
title: title,
text: message
}
PopupUtils.open(Qt.resolvedUrl("dialogs/NotifyDialog.qml"), mainView, props)
}
function finishImport(folder, okUrls, errUrls, existingUrls) {
var okCount = okUrls.length
var errCount = errUrls.length
var existingCount = existingUrls.length
var msg = ""
if(okCount > 0)
msg += i18n.tr("successfully saved: ") + i18n.tr("%1 file", "%1 files", okCount).arg(okCount)
msg += msg.length > 0 ? "
\n" : ""
if(errCount > 0)
msg += i18n.tr("failed to save: ") + i18n.tr("%1 file", "%1 files", errCount).arg(errCount)
msg += msg.length > 0 ? "
\n" : ""
msg += i18n.tr("into: %1").arg(folder)
if(existingCount > 0) {
msg += msg.length > 0 ? "
\n" : ""
msg += i18n.tr("Warning: Content-Hub does not overwrite existing files. The following were detected:")
for(var i=0;i\n" + existingUrls[i]
}
fileSelector.fileSelectorComponent = null
pageStack.currentPage.folderModel.path = folder
pageStack.currentPage.folderModel.refresh()
var props = {
title: i18n.tr("Import Results"),
text: msg
}
var popup = PopupUtils.open(Qt.resolvedUrl("dialogs/NotifyDialog.qml"), mainView, props)
popup.closed.connect(function() {
fileSelector.activeTransfer.state = ContentTransfer.Aborted
})
}
function getUrl(args) {
for (var i = 0; i < args.length; i++) {
var url = args[i]
if (url.startsWith("file://")) {
// any fileName specified ?
const file = url.replace(/^.*[\\\/]/, '');
if (file.indexOf('.') > 0) {
return url.substring(0, url.lastIndexOf("/"));
} else {
return url
}
}
}
return undefined
}
Connections {
target: UriHandler
function onOpened(uris) {
console.log('Open from UriHandler')
const url = getUrl(uris)
if (url) {
console.log("forwarding URL: " + url);
pageStack.currentPage.folderModel.goTo(url)
}
}
}
Component.onCompleted: {
i18n.domain = "lomiri-filemanager-app";
i18n.bindtextdomain("lomiri-filemanager-app", i18nDirectory)
LomiriApplication.applicationName = i18n.tr("File Manager")
let path = ""
if (Qt.application.arguments.length > 0) {
const url = getUrl(Qt.application.arguments)
if (url) {
path = url
}
}
pageStack.push(Qt.resolvedUrl("ui/FolderListPage.qml"), { location: path })
}
}