9#include <QtCore/QPointer>
15#ifdef QGC_UNITTEST_BUILD
16static bool s_testHookArmed =
false;
17static QString s_testNextFile;
21 return s_testHookArmed;
26 s_testHookArmed =
false;
27 const QString file = s_testNextFile;
28 s_testNextFile.clear();
32void QGCFileDialogController::setTestNextFileForAccept(
const QString &file)
34 s_testHookArmed =
true;
35 s_testNextFile = file;
38void QGCFileDialogController::setTestRejectNext()
40 s_testHookArmed =
true;
41 s_testNextFile.clear();
58 qCDebug(QGCFileDialogControllerLog) <<
this;
63 qCDebug(QGCFileDialogControllerLog) <<
this;
68 qCDebug(QGCFileDialogControllerLog) <<
"getFiles" << directoryPath << nameFilters;
70 QDir fileDir(directoryPath);
71 const QFileInfoList fileInfoList = fileDir.entryInfoList(nameFilters, QDir::Files, QDir::Name);
74 for (
const QFileInfo &fileInfo: fileInfoList) {
75 qCDebug(QGCFileDialogControllerLog) <<
"getFiles found" << fileInfo.fileName();
76 files << fileInfo.fileName();
84 return QFile(filename).exists();
89 QString firstFileExtention;
93 bool extensionFound =
true;
94 if (nameFilters.count()) {
95 extensionFound =
false;
96 for (
const QString& nameFilter: nameFilters) {
97 if (nameFilter.startsWith(
"*.")) {
98 const QString fileExtension = nameFilter.right(nameFilter.length() - 2);
99 if (fileExtension !=
"*") {
100 if (firstFileExtention.isEmpty()) {
101 firstFileExtention = fileExtension;
103 if (filename.endsWith(fileExtension)) {
104 extensionFound =
true;
108 }
else if (nameFilter !=
"*") {
109 qCWarning(QGCFileDialogControllerLog) <<
"unsupported name filter format" << nameFilter;
115 QString filenameWithExtension = filename;
116 if (!extensionFound) {
117 filenameWithExtension = QStringLiteral(
"%1.%2").arg(filename).arg(firstFileExtention);
120 return (directoryPath + QStringLiteral(
"/") + filenameWithExtension);
125 QFile::remove(filename);
130#if defined(Q_OS_ANDROID) || defined(Q_OS_IOS)
132 if (fullFolderPath.startsWith(defaultSavePath)) {
133 const int lastDirSepIndex = fullFolderPath.lastIndexOf(QStringLiteral(
"/"));
134 return (QCoreApplication::applicationName() + QStringLiteral(
"/") + fullFolderPath.right(fullFolderPath.length() - lastDirSepIndex));
137 qCWarning(QGCFileDialogControllerLog) << Q_FUNC_INFO <<
"should only be used in mobile builds";
139 return fullFolderPath;
147 if (url.isLocalFile()) {
148 result = url.toLocalFile();
150 result = url.toString();
158 if (result.startsWith(QStringLiteral(
"//"))) {
159 result = result.mid(1);
170 if (missionPath.isEmpty()) {
171 qCWarning(QGCFileDialogControllerLog) <<
"Missions save path is empty";
172 emit
importFailed(tr(
"Missions directory is not configured"));
176 QDir dir(missionPath);
178 qCWarning(QGCFileDialogControllerLog) <<
"Missions save path does not exist";
179 emit
importFailed(tr(
"Missions save path does not exist"));
183 QPointer<QGCFileDialogController> self =
this;
186 QMetaObject::invokeMethod(
188 [filePath, self]() { self->_handleImportResult(filePath); },
189 Qt::QueuedConnection);
193 qCWarning(QGCFileDialogControllerLog) << Q_FUNC_INFO <<
"only supported on Android";
199void QGCFileDialogController::_handleImportResult(
const QString& filePath)
201 if (filePath.isEmpty()) {
202 qCWarning(QGCFileDialogControllerLog) <<
"Import failed: empty file path received from Java";
207 qCDebug(QGCFileDialogControllerLog) <<
"File imported successfully to:" << filePath;
#define QGC_LOGGING_CATEGORY(name, categoryStr)
QString missionSavePath()
void fileImported(const QString &filePath)
static Q_INVOKABLE bool fileExists(const QString &filename)
Check for file existence of specified fully qualified file name.
static Q_INVOKABLE QString fullFolderPathToShortMobilePath(const QString &fullFolderPath)
static Q_INVOKABLE QString takeTestNextFile()
Returns the armed file path (empty for reject) and disarms the hook.
static Q_INVOKABLE QString urlToLocalFile(QUrl url)
static Q_INVOKABLE bool testHookArmed()
Returns true if a test result has been armed for the next dialog open.
static Q_INVOKABLE void deleteFile(const QString &filename)
Deletes the file specified by the fully qualified file name.
~QGCFileDialogController()
static Q_INVOKABLE QStringList getFiles(const QString &directoryPath, const QStringList &nameFilters)
Return all file in the specified path which match the specified extension.
Q_INVOKABLE void importFromNativePicker()
static Q_INVOKABLE QString fullyQualifiedFilename(const QString &directoryPath, const QString &filename, const QStringList &nameFilters=QStringList())
void importFailed(const QString &errorMessage)
QGCFileDialogController(QObject *parent=nullptr)
static SettingsManager * instance()
AppSettings * appSettings() const
void openFileImportDialog(const QString &destPath, std::function< void(const QString &)> callback)