QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
QGCFileDialogController.cc
Go to the documentation of this file.
3#include "SettingsManager.h"
4#include "AppSettings.h"
5
6#include <QtCore/QDir>
7
8QGC_LOGGING_CATEGORY(QGCFileDialogControllerLog, "QMLControls.QGCFileDialogController")
9
11 : QObject(parent)
12{
13 qCDebug(QGCFileDialogControllerLog) << this;
14}
15
17{
18 qCDebug(QGCFileDialogControllerLog) << this;
19}
20
21QStringList QGCFileDialogController::getFiles(const QString &directoryPath, const QStringList &nameFilters)
22{
23 qCDebug(QGCFileDialogControllerLog) << "getFiles" << directoryPath << nameFilters;
24
25 QDir fileDir(directoryPath);
26 const QFileInfoList fileInfoList = fileDir.entryInfoList(nameFilters, QDir::Files, QDir::Name);
27
28 QStringList files;
29 for (const QFileInfo &fileInfo: fileInfoList) {
30 qCDebug(QGCFileDialogControllerLog) << "getFiles found" << fileInfo.fileName();
31 files << fileInfo.fileName();
32 }
33
34 return files;
35}
36
37bool QGCFileDialogController::fileExists(const QString &filename)
38{
39 return QFile(filename).exists();
40}
41
42QString QGCFileDialogController::fullyQualifiedFilename(const QString& directoryPath, const QString& filename, const QStringList& nameFilters)
43{
44 QString firstFileExtention;
45
46 // Check that the filename has one of the specified file extensions
47
48 bool extensionFound = true;
49 if (nameFilters.count()) {
50 extensionFound = false;
51 for (const QString& nameFilter: nameFilters) {
52 if (nameFilter.startsWith("*.")) {
53 const QString fileExtension = nameFilter.right(nameFilter.length() - 2);
54 if (fileExtension != "*") {
55 if (firstFileExtention.isEmpty()) {
56 firstFileExtention = fileExtension;
57 }
58 if (filename.endsWith(fileExtension)) {
59 extensionFound = true;
60 break;
61 }
62 }
63 } else if (nameFilter != "*") {
64 qCWarning(QGCFileDialogControllerLog) << "unsupported name filter format" << nameFilter;
65 }
66 }
67 }
68
69 // Add the extension if it is missing
70 QString filenameWithExtension = filename;
71 if (!extensionFound) {
72 filenameWithExtension = QStringLiteral("%1.%2").arg(filename).arg(firstFileExtention);
73 }
74
75 return (directoryPath + QStringLiteral("/") + filenameWithExtension);
76}
77
78void QGCFileDialogController::deleteFile(const QString &filename)
79{
80 QFile::remove(filename);
81}
82
84{
85#if defined(Q_OS_ANDROID) || defined(Q_OS_IOS)
86 const QString defaultSavePath = SettingsManager::instance()->appSettings()->savePath()->rawValueString();
87 if (fullFolderPath.startsWith(defaultSavePath)) {
88 const int lastDirSepIndex = fullFolderPath.lastIndexOf(QStringLiteral("/"));
89 return (QCoreApplication::applicationName() + QStringLiteral("/") + fullFolderPath.right(fullFolderPath.length() - lastDirSepIndex));
90 }
91#else
92 qCWarning(QGCFileDialogControllerLog) << Q_FUNC_INFO << "should only be used in mobile builds";
93#endif
94 return fullFolderPath;
95}
96
98{
99 // For some strange reason on Qt6 running on Linux files returned by FileDialog are not returned as local file urls.
100 // Seems to be new behavior with Qt6.
101 if (url.isLocalFile()) {
102 return url.toLocalFile();
103 }
104
105 return url.toString();
106}
#define QGC_LOGGING_CATEGORY(name, categoryStr)
Fact *savePath READ savePath CONSTANT Fact * savePath()
static bool fileExists(const QString &filename)
Check for file existence of specified fully qualified file name.
static QString fullFolderPathToShortMobilePath(const QString &fullFolderPath)
static QString urlToLocalFile(QUrl url)
static void deleteFile(const QString &filename)
Deletes the file specified by the fully qualified file name.
static QStringList getFiles(const QString &directoryPath, const QStringList &nameFilters)
Return all file in the specified path which match the specified extension.
static QString fullyQualifiedFilename(const QString &directoryPath, const QString &filename, const QStringList &nameFilters=QStringList())